diff --git a/include/v8.h b/include/v8.h index c24445f0d9..eb1f08a609 100644 --- a/include/v8.h +++ b/include/v8.h @@ -7870,6 +7870,11 @@ class V8_EXPORT ExtensionConfiguration { const char** names_; }; +#ifndef V8_CONTEXT_PROXY_INTERNAL_FIELD_COUNT +// The number of required internal fields for global proxy objects can be +// defined by embedder. +#define V8_CONTEXT_PROXY_INTERNAL_FIELD_COUNT 2 +#endif /** * A sandboxed execution context with its own set of built-in objects @@ -8068,6 +8073,9 @@ class V8_EXPORT Context { Local context_; }; + static const int kProxyInternalFieldCount = + V8_CONTEXT_PROXY_INTERNAL_FIELD_COUNT; + private: friend class Value; friend class Script; diff --git a/src/api-natives.cc b/src/api-natives.cc index ab1b06256e..5a78003140 100644 --- a/src/api-natives.cc +++ b/src/api-natives.cc @@ -641,6 +641,7 @@ Handle ApiNatives::CreateApiFunction( case GlobalProxyType: type = JS_GLOBAL_PROXY_TYPE; instance_size += JSGlobalProxy::kSize; + DCHECK_EQ(instance_size, JSGlobalProxy::kSizeWithInternalFields); break; default: UNREACHABLE(); diff --git a/src/api.cc b/src/api.cc index ad41d819b7..b7a5b2e4ec 100644 --- a/src/api.cc +++ b/src/api.cc @@ -6075,6 +6075,9 @@ static i::Handle CreateEnvironment( proxy_constructor->set_prototype_template( *Utils::OpenHandle(*global_template)); + proxy_template->SetInternalFieldCount( + v8::Context::kProxyInternalFieldCount); + // Migrate security handlers from global_template to // proxy_template. Temporarily removing access check // information from the global template. diff --git a/src/bootstrapper.cc b/src/bootstrapper.cc index a4cb79d546..e484c80203 100644 --- a/src/bootstrapper.cc +++ b/src/bootstrapper.cc @@ -960,8 +960,9 @@ Handle Genesis::CreateNewGlobals( if (global_proxy_template.IsEmpty()) { Handle name = Handle(heap()->empty_string()); Handle code = isolate()->builtins()->Illegal(); - global_proxy_function = factory()->NewFunction( - name, code, JS_GLOBAL_PROXY_TYPE, JSGlobalProxy::kSize); + global_proxy_function = + factory()->NewFunction(name, code, JS_GLOBAL_PROXY_TYPE, + JSGlobalProxy::kSizeWithInternalFields); } else { Handle data = v8::Utils::OpenHandle(*global_proxy_template); @@ -4494,9 +4495,11 @@ Genesis::Genesis(Isolate* isolate, Handle global_proxy_function = isolate->factory()->NewFunctionFromSharedFunctionInfo( initial_map, shared, factory()->undefined_value()); - DCHECK_EQ(global_proxy_data->internal_field_count(), 0); + DCHECK_EQ(global_proxy_data->internal_field_count(), + v8::Context::kProxyInternalFieldCount); Handle global_proxy_map = isolate->factory()->NewMap( - JS_GLOBAL_PROXY_TYPE, JSGlobalProxy::kSize, FAST_HOLEY_SMI_ELEMENTS); + JS_GLOBAL_PROXY_TYPE, JSGlobalProxy::kSizeWithInternalFields, + FAST_HOLEY_SMI_ELEMENTS); JSFunction::SetInitialMap(global_proxy_function, global_proxy_map, factory()->null_value()); global_proxy_map->set_is_access_check_needed(true); diff --git a/src/factory.cc b/src/factory.cc index 4636030744..749188f6bf 100644 --- a/src/factory.cc +++ b/src/factory.cc @@ -2182,7 +2182,8 @@ Handle Factory::NewJSProxy(Handle target, Handle Factory::NewUninitializedJSGlobalProxy() { // Create an empty shell of a JSGlobalProxy that needs to be reinitialized // via ReinitializeJSGlobalProxy later. - Handle map = NewMap(JS_GLOBAL_PROXY_TYPE, JSGlobalProxy::kSize); + Handle map = + NewMap(JS_GLOBAL_PROXY_TYPE, JSGlobalProxy::kSizeWithInternalFields); // Maintain invariant expected from any JSGlobalProxy. map->set_is_access_check_needed(true); CALL_HEAP_FUNCTION( diff --git a/src/objects.h b/src/objects.h index 08e1e568a2..9e3c0372ce 100644 --- a/src/objects.h +++ b/src/objects.h @@ -8512,6 +8512,8 @@ class JSGlobalProxy : public JSObject { static const int kNativeContextOffset = JSObject::kHeaderSize; static const int kHashOffset = kNativeContextOffset + kPointerSize; static const int kSize = kHashOffset + kPointerSize; + static const int kSizeWithInternalFields = + kSize + v8::Context::kProxyInternalFieldCount * kPointerSize; private: DISALLOW_IMPLICIT_CONSTRUCTORS(JSGlobalProxy); diff --git a/test/cctest/test-api.cc b/test/cctest/test-api.cc index 7abfa99de6..ee202c2e99 100644 --- a/test/cctest/test-api.cc +++ b/test/cctest/test-api.cc @@ -25977,3 +25977,14 @@ THREADED_TEST(ImmutableProtoWithParent) { ->Equals(context.local(), original_proto) .FromJust()); } + +TEST(InternalFieldsOnGlobalProxy) { + v8::Isolate* isolate = CcTest::isolate(); + v8::HandleScope scope(isolate); + + v8::Local obj_template = v8::ObjectTemplate::New(isolate); + + v8::Local context = Context::New(isolate, nullptr, obj_template); + v8::Local global = context->Global(); + CHECK_EQ(v8::Context::kProxyInternalFieldCount, global->InternalFieldCount()); +}