|
| 1 | +diff --git a/src/objects/js-objects.cc b/src/objects/js-objects.cc |
| 2 | +index d05eb55..e575536 100644 |
| 3 | +--- a/src/objects/js-objects.cc |
| 4 | ++++ b/src/objects/js-objects.cc |
| 5 | +@@ -4263,9 +4263,25 @@ Maybe<bool> JSObject::CreateDataProperty(Isolate* isolate, |
| 6 | + DirectHandle<JSObject> object, |
| 7 | + PropertyKey key, |
| 8 | + DirectHandle<Object> value, |
| 9 | +- Maybe<ShouldThrow> should_throw) { |
| 10 | ++ Maybe<ShouldThrow> should_throw, |
| 11 | ++ EnforceDefineSemantics semantics) { |
| 12 | + if (!key.is_element()) { |
| 13 | +- if (TryFastAddDataProperty(isolate, object, key.name(), value, NONE)) { |
| 14 | ++ if (semantics == EnforceDefineSemantics::kDefine && |
| 15 | ++ object->map()->has_named_interceptor()) { |
| 16 | ++ // Neither the transition fast path nor the checks below call the |
| 17 | ++ // interceptor. As in [[DefineOwnProperty]], its definer goes first, and |
| 18 | ++ // the checks run only if it declines. |
| 19 | ++ LookupIterator it(isolate, object, key, LookupIterator::OWN); |
| 20 | ++ while (it.state() == LookupIterator::ACCESS_CHECK && it.HasAccess()) { |
| 21 | ++ it.Next(); |
| 22 | ++ } |
| 23 | ++ if (it.state() == LookupIterator::INTERCEPTOR) { |
| 24 | ++ return DefineOwnPropertyIgnoreAttributes( |
| 25 | ++ &it, value, NONE, should_throw, DONT_FORCE_FIELD, |
| 26 | ++ EnforceDefineSemantics::kDefine); |
| 27 | ++ } |
| 28 | ++ } else if (TryFastAddDataProperty(isolate, object, key.name(), value, |
| 29 | ++ NONE)) { |
| 30 | + return Just(true); |
| 31 | + } |
| 32 | + } |
| 33 | +diff --git a/src/objects/js-objects.h b/src/objects/js-objects.h |
| 34 | +index 0efd658..5d70c7b 100644 |
| 35 | +--- a/src/objects/js-objects.h |
| 36 | ++++ b/src/objects/js-objects.h |
| 37 | +@@ -540,11 +540,13 @@ V8_OBJECT class JSObject : public JSReceiver { |
| 38 | + PropertyAttributes attributes = NONE); |
| 39 | + |
| 40 | + // Adds or reconfigures a property to attributes NONE. It will fail when it |
| 41 | +- // cannot. |
| 42 | ++ // cannot. With kDefine, a named interceptor's definer is asked first. [[Set]] |
| 43 | ++ // passes kSet: a [[Set]] that adds a property does not call the definer. |
| 44 | + V8_WARN_UNUSED_RESULT static Maybe<bool> CreateDataProperty( |
| 45 | + Isolate* isolate, DirectHandle<JSObject> object, PropertyKey key, |
| 46 | + DirectHandle<Object> value, |
| 47 | +- Maybe<ShouldThrow> should_throw = Just(kDontThrow)); |
| 48 | ++ Maybe<ShouldThrow> should_throw = Just(kDontThrow), |
| 49 | ++ EnforceDefineSemantics semantics = EnforceDefineSemantics::kDefine); |
| 50 | + |
| 51 | + V8_EXPORT_PRIVATE static void AddProperty(Isolate* isolate, |
| 52 | + DirectHandle<JSObject> object, |
| 53 | +diff --git a/src/objects/objects.cc b/src/objects/objects.cc |
| 54 | +index e55ab41..9b5eda9 100644 |
| 55 | +--- a/src/objects/objects.cc |
| 56 | ++++ b/src/objects/objects.cc |
| 57 | +@@ -2659,6 +2659,13 @@ Maybe<bool> Object::SetSuperProperty(LookupIterator* it, |
| 58 | + should_throw)) { |
| 59 | + return Nothing<bool>(); |
| 60 | + } |
| 61 | ++ if (IsJSObject(*receiver)) { |
| 62 | ++ // A [[Set]] that adds a property does not call the interceptor's |
| 63 | ++ // definer. |
| 64 | ++ return JSObject::CreateDataProperty( |
| 65 | ++ isolate, Cast<JSObject>(receiver), it->GetKey(), value, |
| 66 | ++ should_throw, EnforceDefineSemantics::kSet); |
| 67 | ++ } |
| 68 | + return JSReceiver::CreateDataProperty(isolate, receiver, it->GetKey(), |
| 69 | + value, should_throw); |
| 70 | + } |
| 71 | +diff --git a/test/cctest/test-api-interceptors.cc b/test/cctest/test-api-interceptors.cc |
| 72 | +index a62476a..a2abe50 100644 |
| 73 | +--- a/test/cctest/test-api-interceptors.cc |
| 74 | ++++ b/test/cctest/test-api-interceptors.cc |
| 75 | +@@ -2946,6 +2946,80 @@ THREADED_TEST(PropertyDefinerCallbackInDefineNamedOwnIC) { |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | ++namespace { |
| 80 | ++int create_data_property_definer_calls = 0; |
| 81 | ++v8::Intercepted CountDefinerCallsAndContinueCallback( |
| 82 | ++ Local<Name> name, const v8::PropertyDescriptor& desc, |
| 83 | ++ const v8::PropertyCallbackInfo<Boolean>& info) { |
| 84 | ++ create_data_property_definer_calls++; |
| 85 | ++ CHECK(desc.has_value()); |
| 86 | ++ return v8::Intercepted::kNo; |
| 87 | ++} |
| 88 | ++ |
| 89 | ++void CheckPropertyDefinerCallbackInCreateDataProperty(Local<Context> context) { |
| 90 | ++ v8::Local<v8::FunctionTemplate> templ = |
| 91 | ++ v8::FunctionTemplate::New(CcTest::isolate()); |
| 92 | ++ templ->InstanceTemplate()->SetHandler(v8::NamedPropertyHandlerConfiguration( |
| 93 | ++ nullptr, nullptr, nullptr, nullptr, nullptr, |
| 94 | ++ CountDefinerCallsAndContinueCallback)); |
| 95 | ++ context->Global() |
| 96 | ++ ->Set(context, v8_str("Cls"), |
| 97 | ++ templ->GetFunction(context).ToLocalChecked()) |
| 98 | ++ .FromJust(); |
| 99 | ++ |
| 100 | ++ // Every instance, including those created once the field initializer has |
| 101 | ++ // feedback. |
| 102 | ++ create_data_property_definer_calls = 0; |
| 103 | ++ CHECK(CompileRun("class WithFields extends Cls {" |
| 104 | ++ " field = 1;" |
| 105 | ++ " ['computed'] = 2;" |
| 106 | ++ "}" |
| 107 | ++ "let ok = true;" |
| 108 | ++ "for (let i = 0; i < 20; i++) {" |
| 109 | ++ " const o = new WithFields();" |
| 110 | ++ " ok = ok && o.field === 1 && o.computed === 2;" |
| 111 | ++ "}" |
| 112 | ++ "ok") |
| 113 | ++ ->IsTrue()); |
| 114 | ++ CHECK_EQ(40, create_data_property_definer_calls); |
| 115 | ++ |
| 116 | ++ create_data_property_definer_calls = 0; |
| 117 | ++ Local<Object> obj = |
| 118 | ++ templ->InstanceTemplate()->NewInstance(context).ToLocalChecked(); |
| 119 | ++ CHECK(obj->CreateDataProperty(context, v8_str("api"), v8_num(3)).FromJust()); |
| 120 | ++ CHECK_EQ(1, create_data_property_definer_calls); |
| 121 | ++ CHECK_EQ(3, obj->Get(context, v8_str("api")) |
| 122 | ++ .ToLocalChecked() |
| 123 | ++ ->Int32Value(context) |
| 124 | ++ .FromJust()); |
| 125 | ++ |
| 126 | ++ // Assignments do not call the definer. |
| 127 | ++ context->Global()->Set(context, v8_str("obj"), obj).FromJust(); |
| 128 | ++ create_data_property_definer_calls = 0; |
| 129 | ++ CHECK_EQ(4, CompileRun("for (let i = 0; i < 20; i++) obj['p' + i] = 4;" |
| 130 | ++ "obj.p19") |
| 131 | ++ ->Int32Value(context) |
| 132 | ++ .FromJust()); |
| 133 | ++ CHECK_EQ(0, create_data_property_definer_calls); |
| 134 | ++} |
| 135 | ++} // namespace |
| 136 | ++ |
| 137 | ++THREADED_TEST(PropertyDefinerCallbackInCreateDataProperty) { |
| 138 | ++ { |
| 139 | ++ LocalContext env; |
| 140 | ++ v8::HandleScope scope(env.isolate()); |
| 141 | ++ CheckPropertyDefinerCallbackInCreateDataProperty(env.local()); |
| 142 | ++ } |
| 143 | ++ |
| 144 | ++ { |
| 145 | ++ i::v8_flags.lazy_feedback_allocation = false; |
| 146 | ++ i::FlagList::EnforceFlagImplications(); |
| 147 | ++ LocalContext env; |
| 148 | ++ v8::HandleScope scope(env.isolate()); |
| 149 | ++ CheckPropertyDefinerCallbackInCreateDataProperty(env.local()); |
| 150 | ++ } |
| 151 | ++} |
| 152 | ++ |
| 153 | + namespace { |
| 154 | + v8::Intercepted EmptyPropertyDescriptorCallback( |
| 155 | + Local<Name> name, const v8::PropertyCallbackInfo<v8::Value>& info) { |
0 commit comments