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

Side by Side Diff: src/runtime.cc

Issue 6534029: Change behavior of global declarations in the presence of setters. (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/cctest/test-decls.cc » ('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.
1054 if (is_local && (is_read_only || is_const_property)) { 1060 if (is_local && (is_read_only || is_const_property)) {
1055 const char* type = (is_read_only) ? "const" : "var"; 1061 const char* type = (is_read_only) ? "const" : "var";
1056 return ThrowRedeclarationError(type, name); 1062 return ThrowRedeclarationError(type, name);
1057 } 1063 }
1058 // The property already exists without conflicting: Go to 1064 // The property already exists without conflicting: Go to
1059 // the next declaration. 1065 // the next declaration.
1060 continue; 1066 continue;
1061 } 1067 }
1062 } 1068 }
1063 } else { 1069 } else {
1064 // Copy the function and update its context. Use it as value. 1070 // Copy the function and update its context. Use it as value.
1065 Handle<SharedFunctionInfo> shared = 1071 Handle<SharedFunctionInfo> shared =
1066 Handle<SharedFunctionInfo>::cast(value); 1072 Handle<SharedFunctionInfo>::cast(value);
1067 Handle<JSFunction> function = 1073 Handle<JSFunction> function =
1068 Factory::NewFunctionFromSharedFunctionInfo(shared, context, TENURED); 1074 Factory::NewFunctionFromSharedFunctionInfo(shared, context, TENURED);
1069 value = function; 1075 value = function;
1070 } 1076 }
1071 1077
1072 LookupResult lookup; 1078 LookupResult lookup;
1073 global->LocalLookup(*name, &lookup); 1079 global->LocalLookup(*name, &lookup);
1074 1080
1075 PropertyAttributes attributes = is_const_property 1081 PropertyAttributes attributes = is_const_property
1076 ? static_cast<PropertyAttributes>(base | READ_ONLY) 1082 ? static_cast<PropertyAttributes>(base | READ_ONLY)
1077 : base; 1083 : base;
1078 1084
1079 if (lookup.IsProperty()) { 1085 // There's a local property that we need to overwrite because
1080 // There's a local property that we need to overwrite because 1086 // we're either declaring a function or there's an interceptor
1081 // we're either declaring a function or there's an interceptor 1087 // that claims the property is absent.
1082 // that claims the property is absent. 1088 //
1089 // Check for conflicting re-declarations. We cannot have
1090 // conflicting types in case of intercepted properties because
1091 // they are absent.
1092 if (lookup.IsProperty() &&
1093 (lookup.type() != INTERCEPTOR) &&
1094 (lookup.IsReadOnly() || is_const_property)) {
1095 const char* type = (lookup.IsReadOnly()) ? "const" : "var";
1096 return ThrowRedeclarationError(type, name);
1097 }
1083 1098
1084 // Check for conflicting re-declarations. We cannot have 1099 // Safari does not allow the invocation of callback setters for
1085 // conflicting types in case of intercepted properties because 1100 // function declarations. To mimic this behavior, we do not allow
1086 // they are absent. 1101 // the invocation of setters for function values. This makes a
1087 if (lookup.type() != INTERCEPTOR && 1102 // difference for global functions with the same names as event
1088 (lookup.IsReadOnly() || is_const_property)) { 1103 // handlers such as "function onload() {}". Firefox does call the
1089 const char* type = (lookup.IsReadOnly()) ? "const" : "var"; 1104 // onload setter in those case and Safari does not. We follow
1090 return ThrowRedeclarationError(type, name); 1105 // Safari for compatibility.
1091 } 1106 if (value->IsJSFunction()) {
1107 RETURN_IF_EMPTY_HANDLE(SetLocalPropertyIgnoreAttributes(global,
1108 name,
1109 value,
1110 attributes));
1111 } else {
1092 RETURN_IF_EMPTY_HANDLE(SetProperty(global, name, value, attributes)); 1112 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));
1102 } 1113 }
1103 } 1114 }
1104 1115
1105 ASSERT(!Top::has_pending_exception()); 1116 ASSERT(!Top::has_pending_exception());
1106 return Heap::undefined_value(); 1117 return Heap::undefined_value();
1107 } 1118 }
1108 1119
1109 1120
1110 static MaybeObject* Runtime_DeclareContextSlot(Arguments args) { 1121 static MaybeObject* Runtime_DeclareContextSlot(Arguments args) {
1111 HandleScope scope; 1122 HandleScope scope;
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
1179 context->set_extension(*context_ext); 1190 context->set_extension(*context_ext);
1180 } 1191 }
1181 ASSERT(*context_ext != NULL); 1192 ASSERT(*context_ext != NULL);
1182 1193
1183 // Declare the property by setting it to the initial value if provided, 1194 // Declare the property by setting it to the initial value if provided,
1184 // or undefined, and use the correct mode (e.g. READ_ONLY attribute for 1195 // or undefined, and use the correct mode (e.g. READ_ONLY attribute for
1185 // constant declarations). 1196 // constant declarations).
1186 ASSERT(!context_ext->HasLocalProperty(*name)); 1197 ASSERT(!context_ext->HasLocalProperty(*name));
1187 Handle<Object> value(Heap::undefined_value()); 1198 Handle<Object> value(Heap::undefined_value());
1188 if (*initial_value != NULL) value = initial_value; 1199 if (*initial_value != NULL) value = initial_value;
1200 // Declaring a const context slot is a conflicting declaration if
1201 // there is a callback with that name in a prototype. It is
1202 // allowed to introduce const variables in
1203 // JSContextExtensionObjects. They are treated specially in
1204 // SetProperty and no setters are invoked for those since they are
1205 // not real JSObjects.
1206 if (initial_value->IsTheHole() &&
1207 !context_ext->IsJSContextExtensionObject()) {
1208 LookupResult lookup;
1209 context_ext->Lookup(*name, &lookup);
1210 if (lookup.IsProperty() && (lookup.type() == CALLBACKS)) {
1211 return ThrowRedeclarationError("const", name);
1212 }
1213 }
1189 RETURN_IF_EMPTY_HANDLE(SetProperty(context_ext, name, value, mode)); 1214 RETURN_IF_EMPTY_HANDLE(SetProperty(context_ext, name, value, mode));
1190 } 1215 }
1191 1216
1192 return Heap::undefined_value(); 1217 return Heap::undefined_value();
1193 } 1218 }
1194 1219
1195 1220
1196 static MaybeObject* Runtime_InitializeVarGlobal(Arguments args) { 1221 static MaybeObject* Runtime_InitializeVarGlobal(Arguments args) {
1197 NoHandleAllocation nha; 1222 NoHandleAllocation nha;
1198 1223
1199 // Determine if we need to assign to the variable if it already 1224 // Determine if we need to assign to the variable if it already
1200 // exists (based on the number of arguments). 1225 // exists (based on the number of arguments).
1201 RUNTIME_ASSERT(args.length() == 1 || args.length() == 2); 1226 RUNTIME_ASSERT(args.length() == 1 || args.length() == 2);
1202 bool assign = args.length() == 2; 1227 bool assign = args.length() == 2;
1203 1228
1204 CONVERT_ARG_CHECKED(String, name, 0); 1229 CONVERT_ARG_CHECKED(String, name, 0);
1205 GlobalObject* global = Top::context()->global(); 1230 GlobalObject* global = Top::context()->global();
1206 1231
1207 // According to ECMA-262, section 12.2, page 62, the property must 1232 // According to ECMA-262, section 12.2, page 62, the property must
1208 // not be deletable. 1233 // not be deletable.
1209 PropertyAttributes attributes = DONT_DELETE; 1234 PropertyAttributes attributes = DONT_DELETE;
1210 1235
1211 // Lookup the property locally in the global object. If it isn't 1236 // Lookup the property locally in the global object. If it isn't
1212 // there, there is a property with this name in the prototype chain. 1237 // there, there is a property with this name in the prototype chain.
1213 // We follow Safari and Firefox behavior and only set the property 1238 // We follow Safari and Firefox behavior and only set the property
1214 // locally if there is an explicit initialization value that we have 1239 // locally if there is an explicit initialization value that we have
1215 // to assign to the property. When adding the property we take 1240 // to assign to the property.
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.
1220 // Note that objects can have hidden prototypes, so we need to traverse 1241 // Note that objects can have hidden prototypes, so we need to traverse
1221 // the whole chain of hidden prototypes to do a 'local' lookup. 1242 // the whole chain of hidden prototypes to do a 'local' lookup.
1222 JSObject* real_holder = global; 1243 JSObject* real_holder = global;
1223 LookupResult lookup; 1244 LookupResult lookup;
1224 while (true) { 1245 while (true) {
1225 real_holder->LocalLookup(*name, &lookup); 1246 real_holder->LocalLookup(*name, &lookup);
1226 if (lookup.IsProperty()) { 1247 if (lookup.IsProperty()) {
1227 // Determine if this is a redeclaration of something read-only. 1248 // Determine if this is a redeclaration of something read-only.
1228 if (lookup.IsReadOnly()) { 1249 if (lookup.IsReadOnly()) {
1229 // If we found readonly property on one of hidden prototypes, 1250 // If we found readonly property on one of hidden prototypes,
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
1270 if (!proto->IsJSObject()) 1291 if (!proto->IsJSObject())
1271 break; 1292 break;
1272 1293
1273 if (!JSObject::cast(proto)->map()->is_hidden_prototype()) 1294 if (!JSObject::cast(proto)->map()->is_hidden_prototype())
1274 break; 1295 break;
1275 1296
1276 real_holder = JSObject::cast(proto); 1297 real_holder = JSObject::cast(proto);
1277 } 1298 }
1278 1299
1279 global = Top::context()->global(); 1300 global = Top::context()->global();
1280 if (assign) { 1301 if (assign) return global->SetProperty(*name, args[1], attributes);
1281 return global->SetLocalPropertyIgnoreAttributes(*name,
1282 args[1],
1283 attributes);
1284 }
1285 return Heap::undefined_value(); 1302 return Heap::undefined_value();
1286 } 1303 }
1287 1304
1288 1305
1289 static MaybeObject* Runtime_InitializeConstGlobal(Arguments args) { 1306 static MaybeObject* Runtime_InitializeConstGlobal(Arguments args) {
1290 // All constants are declared with an initial value. The name 1307 // All constants are declared with an initial value. The name
1291 // of the constant is the first argument and the initial value 1308 // of the constant is the first argument and the initial value
1292 // is the second. 1309 // is the second.
1293 RUNTIME_ASSERT(args.length() == 2); 1310 RUNTIME_ASSERT(args.length() == 2);
1294 CONVERT_ARG_CHECKED(String, name, 0); 1311 CONVERT_ARG_CHECKED(String, name, 0);
(...skipping 9906 matching lines...) Expand 10 before | Expand all | Expand 10 after
11201 } else { 11218 } else {
11202 // Handle last resort GC and make sure to allow future allocations 11219 // Handle last resort GC and make sure to allow future allocations
11203 // to grow the heap without causing GCs (if possible). 11220 // to grow the heap without causing GCs (if possible).
11204 Counters::gc_last_resort_from_js.Increment(); 11221 Counters::gc_last_resort_from_js.Increment();
11205 Heap::CollectAllGarbage(false); 11222 Heap::CollectAllGarbage(false);
11206 } 11223 }
11207 } 11224 }
11208 11225
11209 11226
11210 } } // namespace v8::internal 11227 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « no previous file | test/cctest/test-decls.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698