OLD | NEW |
---|---|
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 Loading... | |
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 if (lookup.IsProperty()) { |
Kevin Millikin (Chromium)
2011/02/17 16:01:17
I think you can now write this as:
if (lookup.IsP
Mads Ager (chromium)
2011/02/17 16:19:21
Done.
| |
1080 // There's a local property that we need to overwrite because | 1086 // There's a local property that we need to overwrite because |
1081 // we're either declaring a function or there's an interceptor | 1087 // we're either declaring a function or there's an interceptor |
1082 // that claims the property is absent. | 1088 // that claims the property is absent. |
1083 | 1089 |
1084 // Check for conflicting re-declarations. We cannot have | 1090 // Check for conflicting re-declarations. We cannot have |
1085 // conflicting types in case of intercepted properties because | 1091 // conflicting types in case of intercepted properties because |
1086 // they are absent. | 1092 // they are absent. |
1087 if (lookup.type() != INTERCEPTOR && | 1093 if (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 } | 1097 } |
1092 RETURN_IF_EMPTY_HANDLE(SetProperty(global, name, value, attributes)); | 1098 RETURN_IF_EMPTY_HANDLE(SetProperty(global, name, value, attributes)); |
1093 } else { | 1099 } else { |
1094 // If a property with this name does not already exist on the | 1100 RETURN_IF_EMPTY_HANDLE(SetProperty(global, name, value, attributes)); |
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 } | 1101 } |
1103 } | 1102 } |
1104 | 1103 |
1105 ASSERT(!Top::has_pending_exception()); | 1104 ASSERT(!Top::has_pending_exception()); |
1106 return Heap::undefined_value(); | 1105 return Heap::undefined_value(); |
1107 } | 1106 } |
1108 | 1107 |
1109 | 1108 |
1110 static MaybeObject* Runtime_DeclareContextSlot(Arguments args) { | 1109 static MaybeObject* Runtime_DeclareContextSlot(Arguments args) { |
1111 HandleScope scope; | 1110 HandleScope scope; |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1179 context->set_extension(*context_ext); | 1178 context->set_extension(*context_ext); |
1180 } | 1179 } |
1181 ASSERT(*context_ext != NULL); | 1180 ASSERT(*context_ext != NULL); |
1182 | 1181 |
1183 // Declare the property by setting it to the initial value if provided, | 1182 // 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 | 1183 // or undefined, and use the correct mode (e.g. READ_ONLY attribute for |
1185 // constant declarations). | 1184 // constant declarations). |
1186 ASSERT(!context_ext->HasLocalProperty(*name)); | 1185 ASSERT(!context_ext->HasLocalProperty(*name)); |
1187 Handle<Object> value(Heap::undefined_value()); | 1186 Handle<Object> value(Heap::undefined_value()); |
1188 if (*initial_value != NULL) value = initial_value; | 1187 if (*initial_value != NULL) value = initial_value; |
1188 // Declaring a const context slot is a conflicting declaration if | |
1189 // there is a callback with that name in a prototype. It is | |
1190 // allowed to introduce const variables in | |
1191 // JSContextExtensionObjects. They are treated specially in | |
1192 // SetProperty and no setters are invoked for those since they are | |
1193 // not real JSObjects. | |
1194 if (initial_value->IsTheHole() && | |
1195 !context_ext->IsJSContextExtensionObject()) { | |
1196 LookupResult lookup; | |
1197 context_ext->Lookup(*name, &lookup); | |
1198 if (lookup.IsProperty() && (lookup.type() == CALLBACKS)) { | |
1199 return ThrowRedeclarationError("const", name); | |
1200 } | |
1201 } | |
1189 RETURN_IF_EMPTY_HANDLE(SetProperty(context_ext, name, value, mode)); | 1202 RETURN_IF_EMPTY_HANDLE(SetProperty(context_ext, name, value, mode)); |
1190 } | 1203 } |
1191 | 1204 |
1192 return Heap::undefined_value(); | 1205 return Heap::undefined_value(); |
1193 } | 1206 } |
1194 | 1207 |
1195 | 1208 |
1196 static MaybeObject* Runtime_InitializeVarGlobal(Arguments args) { | 1209 static MaybeObject* Runtime_InitializeVarGlobal(Arguments args) { |
1197 NoHandleAllocation nha; | 1210 NoHandleAllocation nha; |
1198 | 1211 |
1199 // Determine if we need to assign to the variable if it already | 1212 // Determine if we need to assign to the variable if it already |
1200 // exists (based on the number of arguments). | 1213 // exists (based on the number of arguments). |
1201 RUNTIME_ASSERT(args.length() == 1 || args.length() == 2); | 1214 RUNTIME_ASSERT(args.length() == 1 || args.length() == 2); |
1202 bool assign = args.length() == 2; | 1215 bool assign = args.length() == 2; |
1203 | 1216 |
1204 CONVERT_ARG_CHECKED(String, name, 0); | 1217 CONVERT_ARG_CHECKED(String, name, 0); |
1205 GlobalObject* global = Top::context()->global(); | 1218 GlobalObject* global = Top::context()->global(); |
1206 | 1219 |
1207 // According to ECMA-262, section 12.2, page 62, the property must | 1220 // According to ECMA-262, section 12.2, page 62, the property must |
1208 // not be deletable. | 1221 // not be deletable. |
1209 PropertyAttributes attributes = DONT_DELETE; | 1222 PropertyAttributes attributes = DONT_DELETE; |
1210 | 1223 |
1211 // Lookup the property locally in the global object. If it isn't | 1224 // 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. | 1225 // there, there is a property with this name in the prototype chain. |
1213 // We follow Safari and Firefox behavior and only set the property | 1226 // We follow Safari and Firefox behavior and only set the property |
1214 // locally if there is an explicit initialization value that we have | 1227 // locally if there is an explicit initialization value that we have |
1215 // to assign to the property. When adding the property we take | 1228 // 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 | 1229 // Note that objects can have hidden prototypes, so we need to traverse |
1221 // the whole chain of hidden prototypes to do a 'local' lookup. | 1230 // the whole chain of hidden prototypes to do a 'local' lookup. |
1222 JSObject* real_holder = global; | 1231 JSObject* real_holder = global; |
1223 LookupResult lookup; | 1232 LookupResult lookup; |
1224 while (true) { | 1233 while (true) { |
1225 real_holder->LocalLookup(*name, &lookup); | 1234 real_holder->LocalLookup(*name, &lookup); |
1226 if (lookup.IsProperty()) { | 1235 if (lookup.IsProperty()) { |
1227 // Determine if this is a redeclaration of something read-only. | 1236 // Determine if this is a redeclaration of something read-only. |
1228 if (lookup.IsReadOnly()) { | 1237 if (lookup.IsReadOnly()) { |
1229 // If we found readonly property on one of hidden prototypes, | 1238 // If we found readonly property on one of hidden prototypes, |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1270 if (!proto->IsJSObject()) | 1279 if (!proto->IsJSObject()) |
1271 break; | 1280 break; |
1272 | 1281 |
1273 if (!JSObject::cast(proto)->map()->is_hidden_prototype()) | 1282 if (!JSObject::cast(proto)->map()->is_hidden_prototype()) |
1274 break; | 1283 break; |
1275 | 1284 |
1276 real_holder = JSObject::cast(proto); | 1285 real_holder = JSObject::cast(proto); |
1277 } | 1286 } |
1278 | 1287 |
1279 global = Top::context()->global(); | 1288 global = Top::context()->global(); |
1280 if (assign) { | 1289 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(); | 1290 return Heap::undefined_value(); |
1286 } | 1291 } |
1287 | 1292 |
1288 | 1293 |
1289 static MaybeObject* Runtime_InitializeConstGlobal(Arguments args) { | 1294 static MaybeObject* Runtime_InitializeConstGlobal(Arguments args) { |
1290 // All constants are declared with an initial value. The name | 1295 // All constants are declared with an initial value. The name |
1291 // of the constant is the first argument and the initial value | 1296 // of the constant is the first argument and the initial value |
1292 // is the second. | 1297 // is the second. |
1293 RUNTIME_ASSERT(args.length() == 2); | 1298 RUNTIME_ASSERT(args.length() == 2); |
1294 CONVERT_ARG_CHECKED(String, name, 0); | 1299 CONVERT_ARG_CHECKED(String, name, 0); |
(...skipping 9906 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
11201 } else { | 11206 } else { |
11202 // Handle last resort GC and make sure to allow future allocations | 11207 // Handle last resort GC and make sure to allow future allocations |
11203 // to grow the heap without causing GCs (if possible). | 11208 // to grow the heap without causing GCs (if possible). |
11204 Counters::gc_last_resort_from_js.Increment(); | 11209 Counters::gc_last_resort_from_js.Increment(); |
11205 Heap::CollectAllGarbage(false); | 11210 Heap::CollectAllGarbage(false); |
11206 } | 11211 } |
11207 } | 11212 } |
11208 | 11213 |
11209 | 11214 |
11210 } } // namespace v8::internal | 11215 } } // namespace v8::internal |
OLD | NEW |