Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(67)

Side by Side Diff: src/runtime.cc

Issue 6537021: Revert change to const and global variable declarations. It causes (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Created 9 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | test/mjsunit/regress/regress-1170.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2010 the V8 project authors. All rights reserved. 1 // Copyright 2010 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
(...skipping 1033 matching lines...) Expand 10 before | Expand all | Expand 10 after
1044 const char* type = (is_read_only) ? "const" : "var"; 1044 const char* type = (is_read_only) ? "const" : "var";
1045 return ThrowRedeclarationError(type, name); 1045 return ThrowRedeclarationError(type, name);
1046 }; 1046 };
1047 // The property already exists without conflicting: Go to 1047 // The property already exists without conflicting: Go to
1048 // the next declaration. 1048 // the next declaration.
1049 continue; 1049 continue;
1050 } 1050 }
1051 // Fall-through and introduce the absent property by using 1051 // Fall-through and introduce the absent property by using
1052 // SetProperty. 1052 // SetProperty.
1053 } else { 1053 } else {
1054 // For const properties, we treat a callback with this name
1055 // even in the prototype as a conflicting declaration.
1056 if (is_const_property && (lookup.type() == CALLBACKS)) {
1057 return ThrowRedeclarationError("const", name);
1058 }
1059 // Otherwise, we check for locally conflicting declarations.
1060 if (is_local && (is_read_only || is_const_property)) { 1054 if (is_local && (is_read_only || is_const_property)) {
1061 const char* type = (is_read_only) ? "const" : "var"; 1055 const char* type = (is_read_only) ? "const" : "var";
1062 return ThrowRedeclarationError(type, name); 1056 return ThrowRedeclarationError(type, name);
1063 } 1057 }
1064 // The property already exists without conflicting: Go to 1058 // The property already exists without conflicting: Go to
1065 // the next declaration. 1059 // the next declaration.
1066 continue; 1060 continue;
1067 } 1061 }
1068 } 1062 }
1069 } else { 1063 } else {
1070 // Copy the function and update its context. Use it as value. 1064 // Copy the function and update its context. Use it as value.
1071 Handle<SharedFunctionInfo> shared = 1065 Handle<SharedFunctionInfo> shared =
1072 Handle<SharedFunctionInfo>::cast(value); 1066 Handle<SharedFunctionInfo>::cast(value);
1073 Handle<JSFunction> function = 1067 Handle<JSFunction> function =
1074 Factory::NewFunctionFromSharedFunctionInfo(shared, context, TENURED); 1068 Factory::NewFunctionFromSharedFunctionInfo(shared, context, TENURED);
1075 value = function; 1069 value = function;
1076 } 1070 }
1077 1071
1078 LookupResult lookup; 1072 LookupResult lookup;
1079 global->LocalLookup(*name, &lookup); 1073 global->LocalLookup(*name, &lookup);
1080 1074
1081 PropertyAttributes attributes = is_const_property 1075 PropertyAttributes attributes = is_const_property
1082 ? static_cast<PropertyAttributes>(base | READ_ONLY) 1076 ? static_cast<PropertyAttributes>(base | READ_ONLY)
1083 : base; 1077 : base;
1084 1078
1085 // There's a local property that we need to overwrite because 1079 if (lookup.IsProperty()) {
1086 // we're either declaring a function or there's an interceptor 1080 // There's a local property that we need to overwrite because
1087 // that claims the property is absent. 1081 // we're either declaring a function or there's an interceptor
1088 // 1082 // that claims the property is absent.
1089 // Check for conflicting re-declarations. We cannot have 1083
1090 // conflicting types in case of intercepted properties because 1084 // Check for conflicting re-declarations. We cannot have
1091 // they are absent. 1085 // conflicting types in case of intercepted properties because
1092 if (lookup.IsProperty() && 1086 // they are absent.
1093 (lookup.type() != INTERCEPTOR) && 1087 if (lookup.type() != INTERCEPTOR &&
1094 (lookup.IsReadOnly() || is_const_property)) { 1088 (lookup.IsReadOnly() || is_const_property)) {
1095 const char* type = (lookup.IsReadOnly()) ? "const" : "var"; 1089 const char* type = (lookup.IsReadOnly()) ? "const" : "var";
1096 return ThrowRedeclarationError(type, name); 1090 return ThrowRedeclarationError(type, name);
1091 }
1092 RETURN_IF_EMPTY_HANDLE(SetProperty(global, name, value, attributes));
1093 } else {
1094 // If a property with this name does not already exist on the
1095 // global object add the property locally. We take special
1096 // precautions to always add it as a local property even in case
1097 // of callbacks in the prototype chain (this rules out using
1098 // SetProperty). Also, we must use the handle-based version to
1099 // avoid GC issues.
1100 RETURN_IF_EMPTY_HANDLE(
1101 SetLocalPropertyIgnoreAttributes(global, name, value, attributes));
1097 } 1102 }
1098 RETURN_IF_EMPTY_HANDLE(SetProperty(global, name, value, attributes));
1099 } 1103 }
1100 1104
1101 ASSERT(!Top::has_pending_exception()); 1105 ASSERT(!Top::has_pending_exception());
1102 return Heap::undefined_value(); 1106 return Heap::undefined_value();
1103 } 1107 }
1104 1108
1105 1109
1106 static MaybeObject* Runtime_DeclareContextSlot(Arguments args) { 1110 static MaybeObject* Runtime_DeclareContextSlot(Arguments args) {
1107 HandleScope scope; 1111 HandleScope scope;
1108 ASSERT(args.length() == 4); 1112 ASSERT(args.length() == 4);
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
1175 context->set_extension(*context_ext); 1179 context->set_extension(*context_ext);
1176 } 1180 }
1177 ASSERT(*context_ext != NULL); 1181 ASSERT(*context_ext != NULL);
1178 1182
1179 // Declare the property by setting it to the initial value if provided, 1183 // Declare the property by setting it to the initial value if provided,
1180 // or undefined, and use the correct mode (e.g. READ_ONLY attribute for 1184 // or undefined, and use the correct mode (e.g. READ_ONLY attribute for
1181 // constant declarations). 1185 // constant declarations).
1182 ASSERT(!context_ext->HasLocalProperty(*name)); 1186 ASSERT(!context_ext->HasLocalProperty(*name));
1183 Handle<Object> value(Heap::undefined_value()); 1187 Handle<Object> value(Heap::undefined_value());
1184 if (*initial_value != NULL) value = initial_value; 1188 if (*initial_value != NULL) value = initial_value;
1185 // Declaring a const context slot is a conflicting declaration if
1186 // there is a callback with that name in a prototype. It is
1187 // allowed to introduce const variables in
1188 // JSContextExtensionObjects. They are treated specially in
1189 // SetProperty and no setters are invoked for those since they are
1190 // not real JSObjects.
1191 if (initial_value->IsTheHole() &&
1192 !context_ext->IsJSContextExtensionObject()) {
1193 LookupResult lookup;
1194 context_ext->Lookup(*name, &lookup);
1195 if (lookup.IsProperty() && (lookup.type() == CALLBACKS)) {
1196 return ThrowRedeclarationError("const", name);
1197 }
1198 }
1199 RETURN_IF_EMPTY_HANDLE(SetProperty(context_ext, name, value, mode)); 1189 RETURN_IF_EMPTY_HANDLE(SetProperty(context_ext, name, value, mode));
1200 } 1190 }
1201 1191
1202 return Heap::undefined_value(); 1192 return Heap::undefined_value();
1203 } 1193 }
1204 1194
1205 1195
1206 static MaybeObject* Runtime_InitializeVarGlobal(Arguments args) { 1196 static MaybeObject* Runtime_InitializeVarGlobal(Arguments args) {
1207 NoHandleAllocation nha; 1197 NoHandleAllocation nha;
1208 1198
1209 // Determine if we need to assign to the variable if it already 1199 // Determine if we need to assign to the variable if it already
1210 // exists (based on the number of arguments). 1200 // exists (based on the number of arguments).
1211 RUNTIME_ASSERT(args.length() == 1 || args.length() == 2); 1201 RUNTIME_ASSERT(args.length() == 1 || args.length() == 2);
1212 bool assign = args.length() == 2; 1202 bool assign = args.length() == 2;
1213 1203
1214 CONVERT_ARG_CHECKED(String, name, 0); 1204 CONVERT_ARG_CHECKED(String, name, 0);
1215 GlobalObject* global = Top::context()->global(); 1205 GlobalObject* global = Top::context()->global();
1216 1206
1217 // According to ECMA-262, section 12.2, page 62, the property must 1207 // According to ECMA-262, section 12.2, page 62, the property must
1218 // not be deletable. 1208 // not be deletable.
1219 PropertyAttributes attributes = DONT_DELETE; 1209 PropertyAttributes attributes = DONT_DELETE;
1220 1210
1221 // Lookup the property locally in the global object. If it isn't 1211 // Lookup the property locally in the global object. If it isn't
1222 // there, there is a property with this name in the prototype chain. 1212 // there, there is a property with this name in the prototype chain.
1223 // We follow Safari and Firefox behavior and only set the property 1213 // We follow Safari and Firefox behavior and only set the property
1224 // locally if there is an explicit initialization value that we have 1214 // locally if there is an explicit initialization value that we have
1225 // to assign to the property. 1215 // to assign to the property. When adding the property we take
1216 // special precautions to always add it as a local property even in
1217 // case of callbacks in the prototype chain (this rules out using
1218 // SetProperty). We have SetLocalPropertyIgnoreAttributes for
1219 // this.
1226 // Note that objects can have hidden prototypes, so we need to traverse 1220 // Note that objects can have hidden prototypes, so we need to traverse
1227 // the whole chain of hidden prototypes to do a 'local' lookup. 1221 // the whole chain of hidden prototypes to do a 'local' lookup.
1228 JSObject* real_holder = global; 1222 JSObject* real_holder = global;
1229 LookupResult lookup; 1223 LookupResult lookup;
1230 while (true) { 1224 while (true) {
1231 real_holder->LocalLookup(*name, &lookup); 1225 real_holder->LocalLookup(*name, &lookup);
1232 if (lookup.IsProperty()) { 1226 if (lookup.IsProperty()) {
1233 // Determine if this is a redeclaration of something read-only. 1227 // Determine if this is a redeclaration of something read-only.
1234 if (lookup.IsReadOnly()) { 1228 if (lookup.IsReadOnly()) {
1235 // If we found readonly property on one of hidden prototypes, 1229 // If we found readonly property on one of hidden prototypes,
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
1276 if (!proto->IsJSObject()) 1270 if (!proto->IsJSObject())
1277 break; 1271 break;
1278 1272
1279 if (!JSObject::cast(proto)->map()->is_hidden_prototype()) 1273 if (!JSObject::cast(proto)->map()->is_hidden_prototype())
1280 break; 1274 break;
1281 1275
1282 real_holder = JSObject::cast(proto); 1276 real_holder = JSObject::cast(proto);
1283 } 1277 }
1284 1278
1285 global = Top::context()->global(); 1279 global = Top::context()->global();
1286 if (assign) return global->SetProperty(*name, args[1], attributes); 1280 if (assign) {
1281 return global->SetLocalPropertyIgnoreAttributes(*name,
1282 args[1],
1283 attributes);
1284 }
1287 return Heap::undefined_value(); 1285 return Heap::undefined_value();
1288 } 1286 }
1289 1287
1290 1288
1291 static MaybeObject* Runtime_InitializeConstGlobal(Arguments args) { 1289 static MaybeObject* Runtime_InitializeConstGlobal(Arguments args) {
1292 // All constants are declared with an initial value. The name 1290 // All constants are declared with an initial value. The name
1293 // of the constant is the first argument and the initial value 1291 // of the constant is the first argument and the initial value
1294 // is the second. 1292 // is the second.
1295 RUNTIME_ASSERT(args.length() == 2); 1293 RUNTIME_ASSERT(args.length() == 2);
1296 CONVERT_ARG_CHECKED(String, name, 0); 1294 CONVERT_ARG_CHECKED(String, name, 0);
(...skipping 9906 matching lines...) Expand 10 before | Expand all | Expand 10 after
11203 } else { 11201 } else {
11204 // Handle last resort GC and make sure to allow future allocations 11202 // Handle last resort GC and make sure to allow future allocations
11205 // to grow the heap without causing GCs (if possible). 11203 // to grow the heap without causing GCs (if possible).
11206 Counters::gc_last_resort_from_js.Increment(); 11204 Counters::gc_last_resort_from_js.Increment();
11207 Heap::CollectAllGarbage(false); 11205 Heap::CollectAllGarbage(false);
11208 } 11206 }
11209 } 11207 }
11210 11208
11211 11209
11212 } } // namespace v8::internal 11210 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « no previous file | test/mjsunit/regress/regress-1170.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698