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 1193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1204 // assign to it when evaluating the assignment for "const x = | 1204 // assign to it when evaluating the assignment for "const x = |
1205 // <expr>" the initial value is the hole. | 1205 // <expr>" the initial value is the hole. |
1206 bool is_const_property = value->IsTheHole(); | 1206 bool is_const_property = value->IsTheHole(); |
1207 bool is_function_declaration = false; | 1207 bool is_function_declaration = false; |
1208 if (value->IsUndefined() || is_const_property) { | 1208 if (value->IsUndefined() || is_const_property) { |
1209 // Lookup the property in the global object, and don't set the | 1209 // Lookup the property in the global object, and don't set the |
1210 // value of the variable if the property is already there. | 1210 // value of the variable if the property is already there. |
1211 LookupResult lookup; | 1211 LookupResult lookup; |
1212 global->Lookup(*name, &lookup); | 1212 global->Lookup(*name, &lookup); |
1213 if (lookup.IsProperty()) { | 1213 if (lookup.IsProperty()) { |
1214 // We found an existing property. Unless it was an interceptor | 1214 // Determine if the property is local by comparing the holder |
1215 // that claims the property is absent, skip this declaration. | 1215 // against the global object. The information will be used to |
1216 if (lookup.type() != INTERCEPTOR) { | 1216 // avoid throwing re-declaration errors when declaring |
| 1217 // variables or constants that exist in the prototype chain. |
| 1218 bool is_local = (*global == lookup.holder()); |
| 1219 // Get the property attributes and determine if the property is |
| 1220 // read-only. |
| 1221 PropertyAttributes attributes = global->GetPropertyAttribute(*name); |
| 1222 bool is_read_only = (attributes & READ_ONLY) != 0; |
| 1223 if (lookup.type() == INTERCEPTOR) { |
| 1224 // If the interceptor says the property is there, we |
| 1225 // just return undefined without overwriting the property. |
| 1226 // Otherwise, we continue to setting the property. |
| 1227 if (attributes != ABSENT) { |
| 1228 // Check if the existing property conflicts with regards to const. |
| 1229 if (is_local && (is_read_only || is_const_property)) { |
| 1230 const char* type = (is_read_only) ? "const" : "var"; |
| 1231 return ThrowRedeclarationError(isolate, type, name); |
| 1232 }; |
| 1233 // The property already exists without conflicting: Go to |
| 1234 // the next declaration. |
| 1235 continue; |
| 1236 } |
| 1237 // Fall-through and introduce the absent property by using |
| 1238 // SetProperty. |
| 1239 } else { |
| 1240 // For const properties, we treat a callback with this name |
| 1241 // even in the prototype as a conflicting declaration. |
| 1242 if (is_const_property && (lookup.type() == CALLBACKS)) { |
| 1243 return ThrowRedeclarationError(isolate, "const", name); |
| 1244 } |
| 1245 // Otherwise, we check for locally conflicting declarations. |
| 1246 if (is_local && (is_read_only || is_const_property)) { |
| 1247 const char* type = (is_read_only) ? "const" : "var"; |
| 1248 return ThrowRedeclarationError(isolate, type, name); |
| 1249 } |
| 1250 // The property already exists without conflicting: Go to |
| 1251 // the next declaration. |
1217 continue; | 1252 continue; |
1218 } | 1253 } |
1219 PropertyAttributes attributes = global->GetPropertyAttribute(*name); | |
1220 if (attributes != ABSENT) { | |
1221 continue; | |
1222 } | |
1223 // Fall-through and introduce the absent property by using | |
1224 // SetProperty. | |
1225 } | 1254 } |
1226 } else { | 1255 } else { |
1227 is_function_declaration = true; | 1256 is_function_declaration = true; |
1228 // Copy the function and update its context. Use it as value. | 1257 // Copy the function and update its context. Use it as value. |
1229 Handle<SharedFunctionInfo> shared = | 1258 Handle<SharedFunctionInfo> shared = |
1230 Handle<SharedFunctionInfo>::cast(value); | 1259 Handle<SharedFunctionInfo>::cast(value); |
1231 Handle<JSFunction> function = | 1260 Handle<JSFunction> function = |
1232 isolate->factory()->NewFunctionFromSharedFunctionInfo(shared, | 1261 isolate->factory()->NewFunctionFromSharedFunctionInfo(shared, |
1233 context, | 1262 context, |
1234 TENURED); | 1263 TENURED); |
1235 value = function; | 1264 value = function; |
1236 } | 1265 } |
1237 | 1266 |
1238 LookupResult lookup; | 1267 LookupResult lookup; |
1239 global->LocalLookup(*name, &lookup); | 1268 global->LocalLookup(*name, &lookup); |
1240 | 1269 |
| 1270 // There's a local property that we need to overwrite because |
| 1271 // we're either declaring a function or there's an interceptor |
| 1272 // that claims the property is absent. |
| 1273 // |
| 1274 // Check for conflicting re-declarations. We cannot have |
| 1275 // conflicting types in case of intercepted properties because |
| 1276 // they are absent. |
| 1277 if (lookup.IsProperty() && |
| 1278 (lookup.type() != INTERCEPTOR) && |
| 1279 (lookup.IsReadOnly() || is_const_property)) { |
| 1280 const char* type = (lookup.IsReadOnly()) ? "const" : "var"; |
| 1281 return ThrowRedeclarationError(isolate, type, name); |
| 1282 } |
| 1283 |
1241 // Compute the property attributes. According to ECMA-262, section | 1284 // Compute the property attributes. According to ECMA-262, section |
1242 // 13, page 71, the property must be read-only and | 1285 // 13, page 71, the property must be read-only and |
1243 // non-deletable. However, neither SpiderMonkey nor KJS creates the | 1286 // non-deletable. However, neither SpiderMonkey nor KJS creates the |
1244 // property as read-only, so we don't either. | 1287 // property as read-only, so we don't either. |
1245 int attr = NONE; | 1288 int attr = NONE; |
1246 if ((flags & kDeclareGlobalsEvalFlag) == 0) { | 1289 if ((flags & kDeclareGlobalsEvalFlag) == 0) { |
1247 attr |= DONT_DELETE; | 1290 attr |= DONT_DELETE; |
1248 } | 1291 } |
1249 bool is_native = (flags & kDeclareGlobalsNativeFlag) != 0; | 1292 bool is_native = (flags & kDeclareGlobalsNativeFlag) != 0; |
1250 if (is_const_property || (is_native && is_function_declaration)) { | 1293 if (is_const_property || (is_native && is_function_declaration)) { |
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1415 // not be deletable. | 1458 // not be deletable. |
1416 PropertyAttributes attributes = DONT_DELETE; | 1459 PropertyAttributes attributes = DONT_DELETE; |
1417 | 1460 |
1418 // Lookup the property locally in the global object. If it isn't | 1461 // Lookup the property locally in the global object. If it isn't |
1419 // there, there is a property with this name in the prototype chain. | 1462 // there, there is a property with this name in the prototype chain. |
1420 // We follow Safari and Firefox behavior and only set the property | 1463 // We follow Safari and Firefox behavior and only set the property |
1421 // locally if there is an explicit initialization value that we have | 1464 // locally if there is an explicit initialization value that we have |
1422 // to assign to the property. | 1465 // to assign to the property. |
1423 // Note that objects can have hidden prototypes, so we need to traverse | 1466 // Note that objects can have hidden prototypes, so we need to traverse |
1424 // the whole chain of hidden prototypes to do a 'local' lookup. | 1467 // the whole chain of hidden prototypes to do a 'local' lookup. |
1425 Object* object = global; | 1468 JSObject* real_holder = global; |
1426 LookupResult lookup; | 1469 LookupResult lookup; |
1427 while (object->IsJSObject() && | 1470 while (true) { |
1428 JSObject::cast(object)->map()->is_hidden_prototype()) { | 1471 real_holder->LocalLookup(*name, &lookup); |
1429 JSObject* raw_holder = JSObject::cast(object); | 1472 if (lookup.IsProperty()) { |
1430 raw_holder->LocalLookup(*name, &lookup); | 1473 // Determine if this is a redeclaration of something read-only. |
1431 if (lookup.IsProperty() && lookup.type() == INTERCEPTOR) { | 1474 if (lookup.IsReadOnly()) { |
1432 HandleScope handle_scope(isolate); | 1475 // If we found readonly property on one of hidden prototypes, |
1433 Handle<JSObject> holder(raw_holder); | 1476 // just shadow it. |
1434 PropertyAttributes intercepted = holder->GetPropertyAttribute(*name); | 1477 if (real_holder != isolate->context()->global()) break; |
1435 // Update the raw pointer in case it's changed due to GC. | 1478 return ThrowRedeclarationError(isolate, "const", name); |
1436 raw_holder = *holder; | 1479 } |
1437 if (intercepted != ABSENT && (intercepted & READ_ONLY) == 0) { | 1480 |
1438 // Found an interceptor that's not read only. | 1481 // Determine if this is a redeclaration of an intercepted read-only |
1439 if (assign) { | 1482 // property and figure out if the property exists at all. |
1440 return raw_holder->SetProperty( | 1483 bool found = true; |
1441 &lookup, *name, args[2], attributes, strict_mode); | 1484 PropertyType type = lookup.type(); |
1442 } else { | 1485 if (type == INTERCEPTOR) { |
1443 return isolate->heap()->undefined_value(); | 1486 HandleScope handle_scope(isolate); |
| 1487 Handle<JSObject> holder(real_holder); |
| 1488 PropertyAttributes intercepted = holder->GetPropertyAttribute(*name); |
| 1489 real_holder = *holder; |
| 1490 if (intercepted == ABSENT) { |
| 1491 // The interceptor claims the property isn't there. We need to |
| 1492 // make sure to introduce it. |
| 1493 found = false; |
| 1494 } else if ((intercepted & READ_ONLY) != 0) { |
| 1495 // The property is present, but read-only. Since we're trying to |
| 1496 // overwrite it with a variable declaration we must throw a |
| 1497 // re-declaration error. However if we found readonly property |
| 1498 // on one of hidden prototypes, just shadow it. |
| 1499 if (real_holder != isolate->context()->global()) break; |
| 1500 return ThrowRedeclarationError(isolate, "const", name); |
1444 } | 1501 } |
1445 } | 1502 } |
| 1503 |
| 1504 if (found && !assign) { |
| 1505 // The global property is there and we're not assigning any value |
| 1506 // to it. Just return. |
| 1507 return isolate->heap()->undefined_value(); |
| 1508 } |
| 1509 |
| 1510 // Assign the value (or undefined) to the property. |
| 1511 Object* value = (assign) ? args[2] : isolate->heap()->undefined_value(); |
| 1512 return real_holder->SetProperty( |
| 1513 &lookup, *name, value, attributes, strict_mode); |
1446 } | 1514 } |
1447 object = raw_holder->GetPrototype(); | 1515 |
| 1516 Object* proto = real_holder->GetPrototype(); |
| 1517 if (!proto->IsJSObject()) |
| 1518 break; |
| 1519 |
| 1520 if (!JSObject::cast(proto)->map()->is_hidden_prototype()) |
| 1521 break; |
| 1522 |
| 1523 real_holder = JSObject::cast(proto); |
1448 } | 1524 } |
1449 | 1525 |
1450 // Reload global in case the loop above performed a GC. | |
1451 global = isolate->context()->global(); | 1526 global = isolate->context()->global(); |
1452 if (assign) { | 1527 if (assign) { |
1453 return global->SetProperty(*name, args[2], attributes, strict_mode); | 1528 return global->SetProperty(*name, args[2], attributes, strict_mode); |
1454 } | 1529 } |
1455 return isolate->heap()->undefined_value(); | 1530 return isolate->heap()->undefined_value(); |
1456 } | 1531 } |
1457 | 1532 |
1458 | 1533 |
1459 RUNTIME_FUNCTION(MaybeObject*, Runtime_InitializeConstGlobal) { | 1534 RUNTIME_FUNCTION(MaybeObject*, Runtime_InitializeConstGlobal) { |
1460 // All constants are declared with an initial value. The name | 1535 // All constants are declared with an initial value. The name |
(...skipping 17 matching lines...) Expand all Loading... |
1478 // prototype chain (this rules out using SetProperty). | 1553 // prototype chain (this rules out using SetProperty). |
1479 // We use SetLocalPropertyIgnoreAttributes instead | 1554 // We use SetLocalPropertyIgnoreAttributes instead |
1480 LookupResult lookup; | 1555 LookupResult lookup; |
1481 global->LocalLookup(*name, &lookup); | 1556 global->LocalLookup(*name, &lookup); |
1482 if (!lookup.IsProperty()) { | 1557 if (!lookup.IsProperty()) { |
1483 return global->SetLocalPropertyIgnoreAttributes(*name, | 1558 return global->SetLocalPropertyIgnoreAttributes(*name, |
1484 *value, | 1559 *value, |
1485 attributes); | 1560 attributes); |
1486 } | 1561 } |
1487 | 1562 |
1488 // Determine if this is a re-initialization of something not | 1563 // Determine if this is a redeclaration of something not |
1489 // read-only. In case the result is hidden behind an interceptor we | 1564 // read-only. In case the result is hidden behind an interceptor we |
1490 // need to ask it for the property attributes. | 1565 // need to ask it for the property attributes. |
1491 if (!lookup.IsReadOnly() && lookup.type() == INTERCEPTOR) { | 1566 if (!lookup.IsReadOnly()) { |
| 1567 if (lookup.type() != INTERCEPTOR) { |
| 1568 return ThrowRedeclarationError(isolate, "var", name); |
| 1569 } |
| 1570 |
1492 PropertyAttributes intercepted = global->GetPropertyAttribute(*name); | 1571 PropertyAttributes intercepted = global->GetPropertyAttribute(*name); |
1493 | 1572 |
1494 // Ignore the re-initialization if the intercepted property is present | 1573 // Throw re-declaration error if the intercepted property is present |
1495 // but not read-only. | 1574 // but not read-only. |
1496 if (intercepted != ABSENT && (intercepted & READ_ONLY) == 0) { | 1575 if (intercepted != ABSENT && (intercepted & READ_ONLY) == 0) { |
1497 return isolate->heap()->undefined_value(); | 1576 return ThrowRedeclarationError(isolate, "var", name); |
1498 } | 1577 } |
1499 | 1578 |
1500 // Restore global object from context (in case of GC) and continue | 1579 // Restore global object from context (in case of GC) and continue |
1501 // with setting the value because the property is either absent or | 1580 // with setting the value because the property is either absent or |
1502 // read-only. We also have to do redo the lookup. | 1581 // read-only. We also have to do redo the lookup. |
1503 HandleScope handle_scope(isolate); | 1582 HandleScope handle_scope(isolate); |
1504 Handle<GlobalObject> global(isolate->context()->global()); | 1583 Handle<GlobalObject> global(isolate->context()->global()); |
1505 | 1584 |
1506 // BUG 1213575: Handle the case where we have to set a read-only | 1585 // BUG 1213575: Handle the case where we have to set a read-only |
1507 // property through an interceptor and only do it if it's | 1586 // property through an interceptor and only do it if it's |
1508 // uninitialized, e.g. the hole. Nirk... | 1587 // uninitialized, e.g. the hole. Nirk... |
1509 // Passing non-strict mode because the property is writable. | 1588 // Passing non-strict mode because the property is writable. |
1510 RETURN_IF_EMPTY_HANDLE(isolate, | 1589 RETURN_IF_EMPTY_HANDLE(isolate, |
1511 SetProperty(global, | 1590 SetProperty(global, |
1512 name, | 1591 name, |
1513 value, | 1592 value, |
1514 attributes, | 1593 attributes, |
1515 kNonStrictMode)); | 1594 kNonStrictMode)); |
1516 return *value; | 1595 return *value; |
1517 } | 1596 } |
1518 | 1597 |
1519 // Set the value, but only we're assigning the initial value to a | 1598 // Set the value, but only we're assigning the initial value to a |
1520 // constant. For now, we determine this by checking if the | 1599 // constant. For now, we determine this by checking if the |
1521 // current value is the hole. | 1600 // current value is the hole. |
1522 // Strict mode handling not needed (const disallowed in strict mode). | 1601 // Strict mode handling not needed (const disallowed in strict mode). |
1523 PropertyType type = lookup.type(); | 1602 PropertyType type = lookup.type(); |
1524 if (type == FIELD) { | 1603 if (type == FIELD) { |
1525 FixedArray* properties = global->properties(); | 1604 FixedArray* properties = global->properties(); |
1526 int index = lookup.GetFieldIndex(); | 1605 int index = lookup.GetFieldIndex(); |
1527 if (properties->get(index)->IsTheHole() || !lookup.IsReadOnly()) { | 1606 if (properties->get(index)->IsTheHole()) { |
1528 properties->set(index, *value); | 1607 properties->set(index, *value); |
1529 } | 1608 } |
1530 } else if (type == NORMAL) { | 1609 } else if (type == NORMAL) { |
1531 if (global->GetNormalizedProperty(&lookup)->IsTheHole() || | 1610 if (global->GetNormalizedProperty(&lookup)->IsTheHole()) { |
1532 !lookup.IsReadOnly()) { | |
1533 global->SetNormalizedProperty(&lookup, *value); | 1611 global->SetNormalizedProperty(&lookup, *value); |
1534 } | 1612 } |
1535 } else { | 1613 } else { |
1536 // Ignore re-initialization of constants that have already been | 1614 // Ignore re-initialization of constants that have already been |
1537 // assigned a function value. | 1615 // assigned a function value. |
1538 ASSERT(lookup.IsReadOnly() && type == CONSTANT_FUNCTION); | 1616 ASSERT(lookup.IsReadOnly() && type == CONSTANT_FUNCTION); |
1539 } | 1617 } |
1540 | 1618 |
1541 // Use the set value as the result of the operation. | 1619 // Use the set value as the result of the operation. |
1542 return *value; | 1620 return *value; |
(...skipping 11603 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
13146 } else { | 13224 } else { |
13147 // Handle last resort GC and make sure to allow future allocations | 13225 // Handle last resort GC and make sure to allow future allocations |
13148 // to grow the heap without causing GCs (if possible). | 13226 // to grow the heap without causing GCs (if possible). |
13149 isolate->counters()->gc_last_resort_from_js()->Increment(); | 13227 isolate->counters()->gc_last_resort_from_js()->Increment(); |
13150 isolate->heap()->CollectAllGarbage(false); | 13228 isolate->heap()->CollectAllGarbage(false); |
13151 } | 13229 } |
13152 } | 13230 } |
13153 | 13231 |
13154 | 13232 |
13155 } } // namespace v8::internal | 13233 } } // namespace v8::internal |
OLD | NEW |