Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 the V8 project authors. All rights reserved. | 1 // Copyright 2015 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "src/objects.h" | 5 #include "src/objects.h" |
| 6 | 6 |
| 7 #include <cmath> | 7 #include <cmath> |
| 8 #include <iomanip> | 8 #include <iomanip> |
| 9 #include <memory> | 9 #include <memory> |
| 10 #include <sstream> | 10 #include <sstream> |
| (...skipping 7403 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 7414 } | 7414 } |
| 7415 | 7415 |
| 7416 | 7416 |
| 7417 Maybe<bool> JSReceiver::SetIntegrityLevel(Handle<JSReceiver> receiver, | 7417 Maybe<bool> JSReceiver::SetIntegrityLevel(Handle<JSReceiver> receiver, |
| 7418 IntegrityLevel level, | 7418 IntegrityLevel level, |
| 7419 ShouldThrow should_throw) { | 7419 ShouldThrow should_throw) { |
| 7420 DCHECK(level == SEALED || level == FROZEN); | 7420 DCHECK(level == SEALED || level == FROZEN); |
| 7421 | 7421 |
| 7422 if (receiver->IsJSObject()) { | 7422 if (receiver->IsJSObject()) { |
| 7423 Handle<JSObject> object = Handle<JSObject>::cast(receiver); | 7423 Handle<JSObject> object = Handle<JSObject>::cast(receiver); |
| 7424 | |
| 7424 if (!object->HasSloppyArgumentsElements()) { // Fast path. | 7425 if (!object->HasSloppyArgumentsElements()) { // Fast path. |
| 7426 // prevent memory leaks by not adding unnecessary transitions | |
| 7427 Maybe<bool> test = JSObject::TestIntegrityLevel(object, level); | |
| 7428 MAYBE_RETURN(test, Nothing<bool>()); | |
| 7429 if (test.FromJust()) return test; | |
| 7430 | |
| 7425 if (level == SEALED) { | 7431 if (level == SEALED) { |
| 7426 return JSObject::PreventExtensionsWithTransition<SEALED>(object, | 7432 return JSObject::PreventExtensionsWithTransition<SEALED>(object, |
| 7427 should_throw); | 7433 should_throw); |
| 7428 } else { | 7434 } else { |
| 7429 return JSObject::PreventExtensionsWithTransition<FROZEN>(object, | 7435 return JSObject::PreventExtensionsWithTransition<FROZEN>(object, |
| 7430 should_throw); | 7436 should_throw); |
| 7431 } | 7437 } |
| 7432 } | 7438 } |
| 7433 } | 7439 } |
| 7434 | 7440 |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 7470 ? no_conf | 7476 ? no_conf |
| 7471 : no_conf_no_write; | 7477 : no_conf_no_write; |
| 7472 MAYBE_RETURN( | 7478 MAYBE_RETURN( |
| 7473 DefineOwnProperty(isolate, receiver, key, &desc, THROW_ON_ERROR), | 7479 DefineOwnProperty(isolate, receiver, key, &desc, THROW_ON_ERROR), |
| 7474 Nothing<bool>()); | 7480 Nothing<bool>()); |
| 7475 } | 7481 } |
| 7476 } | 7482 } |
| 7477 return Just(true); | 7483 return Just(true); |
| 7478 } | 7484 } |
| 7479 | 7485 |
| 7486 namespace { | |
| 7480 | 7487 |
| 7481 Maybe<bool> JSReceiver::TestIntegrityLevel(Handle<JSReceiver> object, | 7488 template <typename Dictionary> |
| 7482 IntegrityLevel level) { | 7489 bool TestDictionaryPropertiesIntegrityLevel(Dictionary dict, Isolate* isolate, |
| 7490 PropertyAttributes level) { | |
| 7483 DCHECK(level == SEALED || level == FROZEN); | 7491 DCHECK(level == SEALED || level == FROZEN); |
| 7484 Isolate* isolate = object->GetIsolate(); | |
| 7485 | 7492 |
| 7486 Maybe<bool> extensible = JSReceiver::IsExtensible(object); | 7493 uint32_t capacity = dict->Capacity(); |
| 7494 for (uint32_t i = 0; i < capacity; i++) { | |
| 7495 if (!dict->IsKey(isolate, dict->KeyAt(i)) || dict->IsDeleted(i)) continue; | |
|
Toon Verwaest
2017/06/21 18:58:07
Private symbols can also occur as keys in NameDict
| |
| 7496 PropertyDetails details = dict->DetailsAt(i); | |
| 7497 if (details.IsConfigurable()) return false; | |
| 7498 if (level == FROZEN && details.kind() == kData && !details.IsReadOnly()) { | |
| 7499 return false; | |
| 7500 } | |
| 7501 } | |
| 7502 return true; | |
| 7503 } | |
| 7504 | |
| 7505 bool TestFastPropertiesIntegrityLevel(Map* map, PropertyAttributes level) { | |
| 7506 DCHECK(level == SEALED || level == FROZEN); | |
| 7507 DCHECK(map->instance_type() > LAST_CUSTOM_ELEMENTS_RECEIVER); | |
|
Toon Verwaest
2017/06/21 18:58:07
DCHECK_LT(LAST_CUSTOM_ELEMENTS_RECEIVER, map->inst
| |
| 7508 DCHECK(!map->is_dictionary_map()); | |
| 7509 | |
| 7510 DescriptorArray* descriptors = map->instance_descriptors(); | |
| 7511 int number_of_own_descriptors = map->NumberOfOwnDescriptors(); | |
| 7512 for (int i = 0; i < number_of_own_descriptors; i++) { | |
| 7513 if (descriptors->GetKey(i)->IsPrivate()) continue; | |
| 7514 PropertyDetails details = descriptors->GetDetails(i); | |
| 7515 if (details.IsConfigurable()) return false; | |
| 7516 if (level == FROZEN && details.kind() == kData && !details.IsReadOnly()) { | |
| 7517 return false; | |
| 7518 } | |
| 7519 } | |
| 7520 return true; | |
| 7521 } | |
| 7522 | |
| 7523 bool TestPropertiesIntegrityLevel(JSObject* object, PropertyAttributes level) { | |
| 7524 DCHECK(object->map()->instance_type() > LAST_CUSTOM_ELEMENTS_RECEIVER); | |
| 7525 | |
| 7526 if (object->HasFastProperties()) { | |
| 7527 return TestFastPropertiesIntegrityLevel(object->map(), level); | |
| 7528 } | |
| 7529 | |
| 7530 return TestDictionaryPropertiesIntegrityLevel(object->property_dictionary(), | |
| 7531 object->GetIsolate(), level); | |
| 7532 } | |
| 7533 | |
| 7534 bool TestElementsIntegrityLevel(JSObject* object, PropertyAttributes level) { | |
| 7535 DCHECK(!object->HasSloppyArgumentsElements()); | |
| 7536 | |
| 7537 ElementsKind kind = object->GetElementsKind(); | |
| 7538 | |
| 7539 if (IsDictionaryElementsKind(kind)) { | |
| 7540 return TestDictionaryPropertiesIntegrityLevel( | |
| 7541 SeededNumberDictionary::cast(object->elements()), object->GetIsolate(), | |
| 7542 level); | |
| 7543 } | |
| 7544 | |
| 7545 ElementsAccessor* accessor = ElementsAccessor::ForKind(kind); | |
| 7546 // Only DICTIONARY_ELEMENTS and SLOW_SLOPPY_ARGUMENTS_ELEMENTS have | |
| 7547 // PropertyAttributes so just test if empty | |
| 7548 return accessor->NumberOfElements(object) == 0; | |
| 7549 } | |
| 7550 | |
| 7551 bool FastTestIntegrityLevel(JSObject* object, PropertyAttributes level) { | |
| 7552 DCHECK(object->map()->instance_type() > LAST_CUSTOM_ELEMENTS_RECEIVER); | |
| 7553 | |
| 7554 return !object->map()->is_extensible() && | |
| 7555 TestElementsIntegrityLevel(object, level) && | |
| 7556 TestPropertiesIntegrityLevel(object, level); | |
| 7557 } | |
| 7558 | |
| 7559 Maybe<bool> GenericTestIntegrityLevel(Handle<JSReceiver> receiver, | |
| 7560 PropertyAttributes level) { | |
| 7561 DCHECK(level == SEALED || level == FROZEN); | |
| 7562 | |
| 7563 Maybe<bool> extensible = JSReceiver::IsExtensible(receiver); | |
| 7487 MAYBE_RETURN(extensible, Nothing<bool>()); | 7564 MAYBE_RETURN(extensible, Nothing<bool>()); |
| 7488 if (extensible.FromJust()) return Just(false); | 7565 if (extensible.FromJust()) return Just(false); |
| 7489 | 7566 |
| 7567 Isolate* isolate = receiver->GetIsolate(); | |
| 7568 | |
| 7490 Handle<FixedArray> keys; | 7569 Handle<FixedArray> keys; |
| 7491 ASSIGN_RETURN_ON_EXCEPTION_VALUE( | 7570 ASSIGN_RETURN_ON_EXCEPTION_VALUE( |
| 7492 isolate, keys, JSReceiver::OwnPropertyKeys(object), Nothing<bool>()); | 7571 isolate, keys, JSReceiver::OwnPropertyKeys(receiver), Nothing<bool>()); |
| 7493 | 7572 |
| 7494 for (int i = 0; i < keys->length(); ++i) { | 7573 for (int i = 0; i < keys->length(); ++i) { |
| 7495 Handle<Object> key(keys->get(i), isolate); | 7574 Handle<Object> key(keys->get(i), isolate); |
| 7496 PropertyDescriptor current_desc; | 7575 PropertyDescriptor current_desc; |
| 7497 Maybe<bool> owned = JSReceiver::GetOwnPropertyDescriptor( | 7576 Maybe<bool> owned = JSReceiver::GetOwnPropertyDescriptor( |
| 7498 isolate, object, key, ¤t_desc); | 7577 isolate, receiver, key, ¤t_desc); |
| 7499 MAYBE_RETURN(owned, Nothing<bool>()); | 7578 MAYBE_RETURN(owned, Nothing<bool>()); |
| 7500 if (owned.FromJust()) { | 7579 if (owned.FromJust()) { |
| 7501 if (current_desc.configurable()) return Just(false); | 7580 if (current_desc.configurable()) return Just(false); |
| 7502 if (level == FROZEN && | 7581 if (level == FROZEN && |
| 7503 PropertyDescriptor::IsDataDescriptor(¤t_desc) && | 7582 PropertyDescriptor::IsDataDescriptor(¤t_desc) && |
| 7504 current_desc.writable()) { | 7583 current_desc.writable()) { |
| 7505 return Just(false); | 7584 return Just(false); |
| 7506 } | 7585 } |
| 7507 } | 7586 } |
| 7508 } | 7587 } |
| 7509 return Just(true); | 7588 return Just(true); |
| 7510 } | 7589 } |
| 7511 | 7590 |
| 7591 } // namespace | |
| 7592 | |
| 7593 Maybe<bool> JSReceiver::TestIntegrityLevel(Handle<JSReceiver> receiver, | |
| 7594 IntegrityLevel level) { | |
| 7595 if (receiver->map()->instance_type() > LAST_CUSTOM_ELEMENTS_RECEIVER) { | |
| 7596 return JSObject::TestIntegrityLevel(Handle<JSObject>::cast(receiver), | |
| 7597 level); | |
| 7598 } | |
| 7599 return GenericTestIntegrityLevel(receiver, level); | |
| 7600 } | |
| 7601 | |
| 7602 Maybe<bool> JSObject::TestIntegrityLevel(Handle<JSObject> object, | |
| 7603 IntegrityLevel level) { | |
| 7604 if (object->map()->instance_type() > LAST_CUSTOM_ELEMENTS_RECEIVER && | |
| 7605 !object->HasSloppyArgumentsElements()) { | |
| 7606 return Just(FastTestIntegrityLevel(*object, level)); | |
| 7607 } | |
| 7608 return GenericTestIntegrityLevel(Handle<JSReceiver>::cast(object), level); | |
| 7609 } | |
| 7512 | 7610 |
| 7513 Maybe<bool> JSReceiver::PreventExtensions(Handle<JSReceiver> object, | 7611 Maybe<bool> JSReceiver::PreventExtensions(Handle<JSReceiver> object, |
| 7514 ShouldThrow should_throw) { | 7612 ShouldThrow should_throw) { |
| 7515 if (object->IsJSProxy()) { | 7613 if (object->IsJSProxy()) { |
| 7516 return JSProxy::PreventExtensions(Handle<JSProxy>::cast(object), | 7614 return JSProxy::PreventExtensions(Handle<JSProxy>::cast(object), |
| 7517 should_throw); | 7615 should_throw); |
| 7518 } | 7616 } |
| 7519 DCHECK(object->IsJSObject()); | 7617 DCHECK(object->IsJSObject()); |
| 7520 return JSObject::PreventExtensions(Handle<JSObject>::cast(object), | 7618 return JSObject::PreventExtensions(Handle<JSObject>::cast(object), |
| 7521 should_throw); | 7619 should_throw); |
| (...skipping 12651 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 20173 // not | 20271 // not |
| 20174 // depend on this. | 20272 // depend on this. |
| 20175 return DICTIONARY_ELEMENTS; | 20273 return DICTIONARY_ELEMENTS; |
| 20176 } | 20274 } |
| 20177 DCHECK_LE(kind, LAST_ELEMENTS_KIND); | 20275 DCHECK_LE(kind, LAST_ELEMENTS_KIND); |
| 20178 return kind; | 20276 return kind; |
| 20179 } | 20277 } |
| 20180 } | 20278 } |
| 20181 } // namespace internal | 20279 } // namespace internal |
| 20182 } // namespace v8 | 20280 } // namespace v8 |
| OLD | NEW |