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

Side by Side Diff: src/runtime.cc

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

Powered by Google App Engine
This is Rietveld 408576698