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

Side by Side Diff: src/runtime.cc

Issue 6519050: Call accessors in the global object prototype when initializing global (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Address comment. 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.
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 //
1083 1089 // Check for conflicting re-declarations. We cannot have
1084 // Check for conflicting re-declarations. We cannot have 1090 // conflicting types in case of intercepted properties because
1085 // conflicting types in case of intercepted properties because 1091 // they are absent.
1086 // they are absent. 1092 if (lookup.IsProperty() &&
1087 if (lookup.type() != INTERCEPTOR && 1093 (lookup.type() != INTERCEPTOR) &&
1088 (lookup.IsReadOnly() || is_const_property)) { 1094 (lookup.IsReadOnly() || is_const_property)) {
1089 const char* type = (lookup.IsReadOnly()) ? "const" : "var"; 1095 const char* type = (lookup.IsReadOnly()) ? "const" : "var";
1090 return ThrowRedeclarationError(type, name); 1096 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));
1102 } 1097 }
1098 RETURN_IF_EMPTY_HANDLE(SetProperty(global, name, value, attributes));
1103 } 1099 }
1104 1100
1105 ASSERT(!Top::has_pending_exception()); 1101 ASSERT(!Top::has_pending_exception());
1106 return Heap::undefined_value(); 1102 return Heap::undefined_value();
1107 } 1103 }
1108 1104
1109 1105
1110 static MaybeObject* Runtime_DeclareContextSlot(Arguments args) { 1106 static MaybeObject* Runtime_DeclareContextSlot(Arguments args) {
1111 HandleScope scope; 1107 HandleScope scope;
1112 ASSERT(args.length() == 4); 1108 ASSERT(args.length() == 4);
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
1179 context->set_extension(*context_ext); 1175 context->set_extension(*context_ext);
1180 } 1176 }
1181 ASSERT(*context_ext != NULL); 1177 ASSERT(*context_ext != NULL);
1182 1178
1183 // Declare the property by setting it to the initial value if provided, 1179 // 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 1180 // or undefined, and use the correct mode (e.g. READ_ONLY attribute for
1185 // constant declarations). 1181 // constant declarations).
1186 ASSERT(!context_ext->HasLocalProperty(*name)); 1182 ASSERT(!context_ext->HasLocalProperty(*name));
1187 Handle<Object> value(Heap::undefined_value()); 1183 Handle<Object> value(Heap::undefined_value());
1188 if (*initial_value != NULL) value = initial_value; 1184 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 }
1189 RETURN_IF_EMPTY_HANDLE(SetProperty(context_ext, name, value, mode)); 1199 RETURN_IF_EMPTY_HANDLE(SetProperty(context_ext, name, value, mode));
1190 } 1200 }
1191 1201
1192 return Heap::undefined_value(); 1202 return Heap::undefined_value();
1193 } 1203 }
1194 1204
1195 1205
1196 static MaybeObject* Runtime_InitializeVarGlobal(Arguments args) { 1206 static MaybeObject* Runtime_InitializeVarGlobal(Arguments args) {
1197 NoHandleAllocation nha; 1207 NoHandleAllocation nha;
1198 1208
1199 // Determine if we need to assign to the variable if it already 1209 // Determine if we need to assign to the variable if it already
1200 // exists (based on the number of arguments). 1210 // exists (based on the number of arguments).
1201 RUNTIME_ASSERT(args.length() == 1 || args.length() == 2); 1211 RUNTIME_ASSERT(args.length() == 1 || args.length() == 2);
1202 bool assign = args.length() == 2; 1212 bool assign = args.length() == 2;
1203 1213
1204 CONVERT_ARG_CHECKED(String, name, 0); 1214 CONVERT_ARG_CHECKED(String, name, 0);
1205 GlobalObject* global = Top::context()->global(); 1215 GlobalObject* global = Top::context()->global();
1206 1216
1207 // According to ECMA-262, section 12.2, page 62, the property must 1217 // According to ECMA-262, section 12.2, page 62, the property must
1208 // not be deletable. 1218 // not be deletable.
1209 PropertyAttributes attributes = DONT_DELETE; 1219 PropertyAttributes attributes = DONT_DELETE;
1210 1220
1211 // Lookup the property locally in the global object. If it isn't 1221 // 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. 1222 // there, there is a property with this name in the prototype chain.
1213 // We follow Safari and Firefox behavior and only set the property 1223 // We follow Safari and Firefox behavior and only set the property
1214 // locally if there is an explicit initialization value that we have 1224 // locally if there is an explicit initialization value that we have
1215 // to assign to the property. When adding the property we take 1225 // 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 1226 // Note that objects can have hidden prototypes, so we need to traverse
1221 // the whole chain of hidden prototypes to do a 'local' lookup. 1227 // the whole chain of hidden prototypes to do a 'local' lookup.
1222 JSObject* real_holder = global; 1228 JSObject* real_holder = global;
1223 LookupResult lookup; 1229 LookupResult lookup;
1224 while (true) { 1230 while (true) {
1225 real_holder->LocalLookup(*name, &lookup); 1231 real_holder->LocalLookup(*name, &lookup);
1226 if (lookup.IsProperty()) { 1232 if (lookup.IsProperty()) {
1227 // Determine if this is a redeclaration of something read-only. 1233 // Determine if this is a redeclaration of something read-only.
1228 if (lookup.IsReadOnly()) { 1234 if (lookup.IsReadOnly()) {
1229 // If we found readonly property on one of hidden prototypes, 1235 // 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()) 1276 if (!proto->IsJSObject())
1271 break; 1277 break;
1272 1278
1273 if (!JSObject::cast(proto)->map()->is_hidden_prototype()) 1279 if (!JSObject::cast(proto)->map()->is_hidden_prototype())
1274 break; 1280 break;
1275 1281
1276 real_holder = JSObject::cast(proto); 1282 real_holder = JSObject::cast(proto);
1277 } 1283 }
1278 1284
1279 global = Top::context()->global(); 1285 global = Top::context()->global();
1280 if (assign) { 1286 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(); 1287 return Heap::undefined_value();
1286 } 1288 }
1287 1289
1288 1290
1289 static MaybeObject* Runtime_InitializeConstGlobal(Arguments args) { 1291 static MaybeObject* Runtime_InitializeConstGlobal(Arguments args) {
1290 // All constants are declared with an initial value. The name 1292 // All constants are declared with an initial value. The name
1291 // of the constant is the first argument and the initial value 1293 // of the constant is the first argument and the initial value
1292 // is the second. 1294 // is the second.
1293 RUNTIME_ASSERT(args.length() == 2); 1295 RUNTIME_ASSERT(args.length() == 2);
1294 CONVERT_ARG_CHECKED(String, name, 0); 1296 CONVERT_ARG_CHECKED(String, name, 0);
(...skipping 9906 matching lines...) Expand 10 before | Expand all | Expand 10 after
11201 } else { 11203 } else {
11202 // Handle last resort GC and make sure to allow future allocations 11204 // Handle last resort GC and make sure to allow future allocations
11203 // to grow the heap without causing GCs (if possible). 11205 // to grow the heap without causing GCs (if possible).
11204 Counters::gc_last_resort_from_js.Increment(); 11206 Counters::gc_last_resort_from_js.Increment();
11205 Heap::CollectAllGarbage(false); 11207 Heap::CollectAllGarbage(false);
11206 } 11208 }
11207 } 11209 }
11208 11210
11209 11211
11210 } } // namespace v8::internal 11212 } } // 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