OLD | NEW |
---|---|
1 // Copyright 2011 the V8 project authors. All rights reserved. | 1 // Copyright 2011 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 1159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1170 // assign to it when evaluating the assignment for "const x = | 1170 // assign to it when evaluating the assignment for "const x = |
1171 // <expr>" the initial value is the hole. | 1171 // <expr>" the initial value is the hole. |
1172 bool is_const_property = value->IsTheHole(); | 1172 bool is_const_property = value->IsTheHole(); |
1173 bool is_function_declaration = false; | 1173 bool is_function_declaration = false; |
1174 if (value->IsUndefined() || is_const_property) { | 1174 if (value->IsUndefined() || is_const_property) { |
1175 // Lookup the property in the global object, and don't set the | 1175 // Lookup the property in the global object, and don't set the |
1176 // value of the variable if the property is already there. | 1176 // value of the variable if the property is already there. |
1177 LookupResult lookup; | 1177 LookupResult lookup; |
1178 global->Lookup(*name, &lookup); | 1178 global->Lookup(*name, &lookup); |
1179 if (lookup.IsProperty()) { | 1179 if (lookup.IsProperty()) { |
1180 // Determine if the property is local by comparing the holder | 1180 // We found an existing property. Unless it was an interceptor |
1181 // against the global object. The information will be used to | 1181 // that claims the property is absent, skip this declaration. |
1182 // avoid throwing re-declaration errors when declaring | 1182 if (lookup.type() != INTERCEPTOR) { |
1183 // variables or constants that exist in the prototype chain. | |
1184 bool is_local = (*global == lookup.holder()); | |
1185 // Get the property attributes and determine if the property is | |
1186 // read-only. | |
1187 PropertyAttributes attributes = global->GetPropertyAttribute(*name); | |
1188 bool is_read_only = (attributes & READ_ONLY) != 0; | |
1189 if (lookup.type() == INTERCEPTOR) { | |
1190 // If the interceptor says the property is there, we | |
1191 // just return undefined without overwriting the property. | |
1192 // Otherwise, we continue to setting the property. | |
1193 if (attributes != ABSENT) { | |
1194 // Check if the existing property conflicts with regards to const. | |
1195 if (is_local && (is_read_only || is_const_property)) { | |
1196 const char* type = (is_read_only) ? "const" : "var"; | |
1197 return ThrowRedeclarationError(isolate, type, name); | |
1198 }; | |
1199 // The property already exists without conflicting: Go to | |
1200 // the next declaration. | |
1201 continue; | |
1202 } | |
1203 // Fall-through and introduce the absent property by using | |
1204 // SetProperty. | |
1205 } else { | |
1206 // For const properties, we treat a callback with this name | |
1207 // even in the prototype as a conflicting declaration. | |
1208 if (is_const_property && (lookup.type() == CALLBACKS)) { | |
1209 return ThrowRedeclarationError(isolate, "const", name); | |
1210 } | |
1211 // Otherwise, we check for locally conflicting declarations. | |
1212 if (is_local && (is_read_only || is_const_property)) { | |
1213 const char* type = (is_read_only) ? "const" : "var"; | |
1214 return ThrowRedeclarationError(isolate, type, name); | |
1215 } | |
1216 // The property already exists without conflicting: Go to | |
1217 // the next declaration. | |
1218 continue; | 1183 continue; |
1219 } | 1184 } |
1185 PropertyAttributes attributes = global->GetPropertyAttribute(*name); | |
1186 if (attributes != ABSENT) { | |
1187 continue; | |
1188 } | |
1189 // Fall-through and introduce the absent property by using | |
1190 // SetProperty. | |
1220 } | 1191 } |
1221 } else { | 1192 } else { |
1222 is_function_declaration = true; | 1193 is_function_declaration = true; |
1223 // Copy the function and update its context. Use it as value. | 1194 // Copy the function and update its context. Use it as value. |
1224 Handle<SharedFunctionInfo> shared = | 1195 Handle<SharedFunctionInfo> shared = |
1225 Handle<SharedFunctionInfo>::cast(value); | 1196 Handle<SharedFunctionInfo>::cast(value); |
1226 Handle<JSFunction> function = | 1197 Handle<JSFunction> function = |
1227 isolate->factory()->NewFunctionFromSharedFunctionInfo(shared, | 1198 isolate->factory()->NewFunctionFromSharedFunctionInfo(shared, |
1228 context, | 1199 context, |
1229 TENURED); | 1200 TENURED); |
1230 value = function; | 1201 value = function; |
1231 } | 1202 } |
1232 | 1203 |
1233 LookupResult lookup; | 1204 LookupResult lookup; |
1234 global->LocalLookup(*name, &lookup); | 1205 global->LocalLookup(*name, &lookup); |
1235 | 1206 |
1236 // There's a local property that we need to overwrite because | |
1237 // we're either declaring a function or there's an interceptor | |
1238 // that claims the property is absent. | |
1239 // | |
1240 // Check for conflicting re-declarations. We cannot have | |
1241 // conflicting types in case of intercepted properties because | |
1242 // they are absent. | |
1243 if (lookup.IsProperty() && | |
1244 (lookup.type() != INTERCEPTOR) && | |
1245 (lookup.IsReadOnly() || is_const_property)) { | |
1246 const char* type = (lookup.IsReadOnly()) ? "const" : "var"; | |
1247 return ThrowRedeclarationError(isolate, type, name); | |
1248 } | |
1249 | |
1250 // Compute the property attributes. According to ECMA-262, section | 1207 // Compute the property attributes. According to ECMA-262, section |
1251 // 13, page 71, the property must be read-only and | 1208 // 13, page 71, the property must be read-only and |
1252 // non-deletable. However, neither SpiderMonkey nor KJS creates the | 1209 // non-deletable. However, neither SpiderMonkey nor KJS creates the |
1253 // property as read-only, so we don't either. | 1210 // property as read-only, so we don't either. |
1254 int attr = NONE; | 1211 int attr = NONE; |
1255 if ((flags & kDeclareGlobalsEvalFlag) == 0) { | 1212 if ((flags & kDeclareGlobalsEvalFlag) == 0) { |
1256 attr |= DONT_DELETE; | 1213 attr |= DONT_DELETE; |
1257 } | 1214 } |
1258 bool is_native = (flags & kDeclareGlobalsNativeFlag) != 0; | 1215 bool is_native = (flags & kDeclareGlobalsNativeFlag) != 0; |
1259 if (is_const_property || (is_native && is_function_declaration)) { | 1216 if (is_const_property || (is_native && is_function_declaration)) { |
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1424 // not be deletable. | 1381 // not be deletable. |
1425 PropertyAttributes attributes = DONT_DELETE; | 1382 PropertyAttributes attributes = DONT_DELETE; |
1426 | 1383 |
1427 // Lookup the property locally in the global object. If it isn't | 1384 // Lookup the property locally in the global object. If it isn't |
1428 // there, there is a property with this name in the prototype chain. | 1385 // there, there is a property with this name in the prototype chain. |
1429 // We follow Safari and Firefox behavior and only set the property | 1386 // We follow Safari and Firefox behavior and only set the property |
1430 // locally if there is an explicit initialization value that we have | 1387 // locally if there is an explicit initialization value that we have |
1431 // to assign to the property. | 1388 // to assign to the property. |
1432 // Note that objects can have hidden prototypes, so we need to traverse | 1389 // Note that objects can have hidden prototypes, so we need to traverse |
1433 // the whole chain of hidden prototypes to do a 'local' lookup. | 1390 // the whole chain of hidden prototypes to do a 'local' lookup. |
1434 JSObject* real_holder = global; | 1391 Object* object = global; |
1435 LookupResult lookup; | 1392 LookupResult lookup; |
1436 while (true) { | 1393 while (object->IsJSObject() && |
1394 JSObject::cast(object)->map()->is_hidden_prototype()) { | |
1395 JSObject* real_holder = JSObject::cast(object); | |
Kevin Millikin (Chromium)
2011/09/13 11:09:15
This code is still a bit confusing. I suggest ren
Jakob Kummerow
2011/09/13 13:21:38
Done.
| |
1437 real_holder->LocalLookup(*name, &lookup); | 1396 real_holder->LocalLookup(*name, &lookup); |
1438 if (lookup.IsProperty()) { | 1397 if (lookup.IsProperty() && lookup.type() == INTERCEPTOR) { |
1439 // Determine if this is a redeclaration of something read-only. | 1398 HandleScope handle_scope(isolate); |
1440 if (lookup.IsReadOnly()) { | 1399 Handle<JSObject> holder(real_holder); |
1441 // If we found readonly property on one of hidden prototypes, | 1400 PropertyAttributes intercepted = holder->GetPropertyAttribute(*name); |
1442 // just shadow it. | 1401 real_holder = *holder; |
Kevin Millikin (Chromium)
2011/09/13 11:09:15
And I think this line just needs a short comment t
Jakob Kummerow
2011/09/13 13:21:38
Done.
| |
1443 if (real_holder != isolate->context()->global()) break; | 1402 if (intercepted != ABSENT && (intercepted & READ_ONLY) == 0) { |
1444 return ThrowRedeclarationError(isolate, "const", name); | 1403 // Found an interceptor that's not read only. |
1445 } | 1404 if (assign) { |
1446 | 1405 return real_holder->SetProperty( |
1447 // Determine if this is a redeclaration of an intercepted read-only | 1406 &lookup, *name, args[2], attributes, strict_mode); |
1448 // property and figure out if the property exists at all. | 1407 } else { |
1449 bool found = true; | 1408 return isolate->heap()->undefined_value(); |
1450 PropertyType type = lookup.type(); | |
1451 if (type == INTERCEPTOR) { | |
1452 HandleScope handle_scope(isolate); | |
1453 Handle<JSObject> holder(real_holder); | |
1454 PropertyAttributes intercepted = holder->GetPropertyAttribute(*name); | |
1455 real_holder = *holder; | |
1456 if (intercepted == ABSENT) { | |
1457 // The interceptor claims the property isn't there. We need to | |
1458 // make sure to introduce it. | |
1459 found = false; | |
1460 } else if ((intercepted & READ_ONLY) != 0) { | |
1461 // The property is present, but read-only. Since we're trying to | |
1462 // overwrite it with a variable declaration we must throw a | |
1463 // re-declaration error. However if we found readonly property | |
1464 // on one of hidden prototypes, just shadow it. | |
1465 if (real_holder != isolate->context()->global()) break; | |
1466 return ThrowRedeclarationError(isolate, "const", name); | |
1467 } | 1409 } |
1468 } | 1410 } |
1469 | |
1470 if (found && !assign) { | |
1471 // The global property is there and we're not assigning any value | |
1472 // to it. Just return. | |
1473 return isolate->heap()->undefined_value(); | |
1474 } | |
1475 | |
1476 // Assign the value (or undefined) to the property. | |
1477 Object* value = (assign) ? args[2] : isolate->heap()->undefined_value(); | |
1478 return real_holder->SetProperty( | |
1479 &lookup, *name, value, attributes, strict_mode); | |
1480 } | 1411 } |
1481 | 1412 object = real_holder->GetPrototype(); |
1482 Object* proto = real_holder->GetPrototype(); | |
1483 if (!proto->IsJSObject()) | |
1484 break; | |
1485 | |
1486 if (!JSObject::cast(proto)->map()->is_hidden_prototype()) | |
1487 break; | |
1488 | |
1489 real_holder = JSObject::cast(proto); | |
1490 } | 1413 } |
1491 | 1414 |
1492 global = isolate->context()->global(); | 1415 global = isolate->context()->global(); |
Kevin Millikin (Chromium)
2011/09/13 11:09:15
Also a comment here that says something like 'Relo
Jakob Kummerow
2011/09/13 13:21:38
Done.
| |
1493 if (assign) { | 1416 if (assign) { |
1494 return global->SetProperty(*name, args[2], attributes, strict_mode); | 1417 return global->SetProperty(*name, args[2], attributes, strict_mode); |
1495 } | 1418 } |
1496 return isolate->heap()->undefined_value(); | 1419 return isolate->heap()->undefined_value(); |
1497 } | 1420 } |
1498 | 1421 |
1499 | 1422 |
1500 RUNTIME_FUNCTION(MaybeObject*, Runtime_InitializeConstGlobal) { | 1423 RUNTIME_FUNCTION(MaybeObject*, Runtime_InitializeConstGlobal) { |
1501 // All constants are declared with an initial value. The name | 1424 // All constants are declared with an initial value. The name |
1502 // of the constant is the first argument and the initial value | 1425 // of the constant is the first argument and the initial value |
(...skipping 16 matching lines...) Expand all Loading... | |
1519 // prototype chain (this rules out using SetProperty). | 1442 // prototype chain (this rules out using SetProperty). |
1520 // We use SetLocalPropertyIgnoreAttributes instead | 1443 // We use SetLocalPropertyIgnoreAttributes instead |
1521 LookupResult lookup; | 1444 LookupResult lookup; |
1522 global->LocalLookup(*name, &lookup); | 1445 global->LocalLookup(*name, &lookup); |
1523 if (!lookup.IsProperty()) { | 1446 if (!lookup.IsProperty()) { |
1524 return global->SetLocalPropertyIgnoreAttributes(*name, | 1447 return global->SetLocalPropertyIgnoreAttributes(*name, |
1525 *value, | 1448 *value, |
1526 attributes); | 1449 attributes); |
1527 } | 1450 } |
1528 | 1451 |
1529 // Determine if this is a redeclaration of something not | 1452 // Determine if this is a re-initialization of something not |
1530 // read-only. In case the result is hidden behind an interceptor we | 1453 // read-only. In case the result is hidden behind an interceptor we |
1531 // need to ask it for the property attributes. | 1454 // need to ask it for the property attributes. |
1532 if (!lookup.IsReadOnly()) { | 1455 if (!lookup.IsReadOnly() && lookup.type() == INTERCEPTOR) { |
1533 if (lookup.type() != INTERCEPTOR) { | |
1534 return ThrowRedeclarationError(isolate, "var", name); | |
1535 } | |
1536 | |
1537 PropertyAttributes intercepted = global->GetPropertyAttribute(*name); | 1456 PropertyAttributes intercepted = global->GetPropertyAttribute(*name); |
1538 | 1457 |
1539 // Throw re-declaration error if the intercepted property is present | 1458 // Ignore the re-initialization if the intercepted property is present |
1540 // but not read-only. | 1459 // but not read-only. |
1541 if (intercepted != ABSENT && (intercepted & READ_ONLY) == 0) { | 1460 if (intercepted != ABSENT && (intercepted & READ_ONLY) == 0) { |
1542 return ThrowRedeclarationError(isolate, "var", name); | 1461 return isolate->heap()->undefined_value(); |
1543 } | 1462 } |
1544 | 1463 |
1545 // Restore global object from context (in case of GC) and continue | 1464 // Restore global object from context (in case of GC) and continue |
1546 // with setting the value because the property is either absent or | 1465 // with setting the value because the property is either absent or |
1547 // read-only. We also have to do redo the lookup. | 1466 // read-only. We also have to do redo the lookup. |
1548 HandleScope handle_scope(isolate); | 1467 HandleScope handle_scope(isolate); |
1549 Handle<GlobalObject> global(isolate->context()->global()); | 1468 Handle<GlobalObject> global(isolate->context()->global()); |
1550 | 1469 |
1551 // BUG 1213575: Handle the case where we have to set a read-only | 1470 // BUG 1213575: Handle the case where we have to set a read-only |
1552 // property through an interceptor and only do it if it's | 1471 // property through an interceptor and only do it if it's |
1553 // uninitialized, e.g. the hole. Nirk... | 1472 // uninitialized, e.g. the hole. Nirk... |
1554 // Passing non-strict mode because the property is writable. | 1473 // Passing non-strict mode because the property is writable. |
1555 RETURN_IF_EMPTY_HANDLE(isolate, | 1474 RETURN_IF_EMPTY_HANDLE(isolate, |
1556 SetProperty(global, | 1475 SetProperty(global, |
1557 name, | 1476 name, |
1558 value, | 1477 value, |
1559 attributes, | 1478 attributes, |
1560 kNonStrictMode)); | 1479 kNonStrictMode)); |
1561 return *value; | 1480 return *value; |
1562 } | 1481 } |
1563 | 1482 |
1564 // Set the value, but only we're assigning the initial value to a | 1483 // Set the value, but only we're assigning the initial value to a |
1565 // constant. For now, we determine this by checking if the | 1484 // constant. For now, we determine this by checking if the |
1566 // current value is the hole. | 1485 // current value is the hole. |
1567 // Strict mode handling not needed (const disallowed in strict mode). | 1486 // Strict mode handling not needed (const disallowed in strict mode). |
1568 PropertyType type = lookup.type(); | 1487 PropertyType type = lookup.type(); |
1569 if (type == FIELD) { | 1488 if (type == FIELD) { |
1570 FixedArray* properties = global->properties(); | 1489 FixedArray* properties = global->properties(); |
1571 int index = lookup.GetFieldIndex(); | 1490 int index = lookup.GetFieldIndex(); |
1572 if (properties->get(index)->IsTheHole()) { | 1491 if (properties->get(index)->IsTheHole() || !lookup.IsReadOnly()) { |
1573 properties->set(index, *value); | 1492 properties->set(index, *value); |
1574 } | 1493 } |
1575 } else if (type == NORMAL) { | 1494 } else if (type == NORMAL) { |
1576 if (global->GetNormalizedProperty(&lookup)->IsTheHole()) { | 1495 if (global->GetNormalizedProperty(&lookup)->IsTheHole() || |
1496 !lookup.IsReadOnly()) { | |
1577 global->SetNormalizedProperty(&lookup, *value); | 1497 global->SetNormalizedProperty(&lookup, *value); |
1578 } | 1498 } |
1579 } else { | 1499 } else { |
1580 // Ignore re-initialization of constants that have already been | 1500 // Ignore re-initialization of constants that have already been |
1581 // assigned a function value. | 1501 // assigned a function value. |
1582 ASSERT(lookup.IsReadOnly() && type == CONSTANT_FUNCTION); | 1502 ASSERT(lookup.IsReadOnly() && type == CONSTANT_FUNCTION); |
1583 } | 1503 } |
1584 | 1504 |
1585 // Use the set value as the result of the operation. | 1505 // Use the set value as the result of the operation. |
1586 return *value; | 1506 return *value; |
(...skipping 11547 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
13134 } else { | 13054 } else { |
13135 // Handle last resort GC and make sure to allow future allocations | 13055 // Handle last resort GC and make sure to allow future allocations |
13136 // to grow the heap without causing GCs (if possible). | 13056 // to grow the heap without causing GCs (if possible). |
13137 isolate->counters()->gc_last_resort_from_js()->Increment(); | 13057 isolate->counters()->gc_last_resort_from_js()->Increment(); |
13138 isolate->heap()->CollectAllGarbage(false); | 13058 isolate->heap()->CollectAllGarbage(false); |
13139 } | 13059 } |
13140 } | 13060 } |
13141 | 13061 |
13142 | 13062 |
13143 } } // namespace v8::internal | 13063 } } // namespace v8::internal |
OLD | NEW |