39d546a24022b62b00aedf7b556ac6c9e2306aab diff --git a/AUTHORS b/AUTHORS index 3251716f2a..4b5163961d 100644 --- a/AUTHORS +++ b/AUTHORS @@ -70,6 +70,7 @@ Felix Geisendörfer Filipe David Manana Franziska Hinkelmann Geoffrey Garside +Gus Caplan Gwang Yoon Hwang Henrique Ferreiro Hirofumi Mako diff --git a/include/v8.h b/include/v8.h index 573e80176d..aeeebba304 100644 --- a/include/v8.h +++ b/include/v8.h @@ -2378,6 +2378,11 @@ class V8_EXPORT Value : public Data { bool IsWebAssemblyCompiledModule() const; + /** + * Returns true if the value is a Module Namespace Object. + */ + bool IsModuleNamespaceObject() const; + V8_WARN_UNUSED_RESULT MaybeLocal ToBigInt( Local context) const; V8_WARN_UNUSED_RESULT MaybeLocal ToBoolean( diff --git a/src/api.cc b/src/api.cc index 8b177d041d..6dd669ee11 100644 --- a/src/api.cc +++ b/src/api.cc @@ -3583,6 +3583,10 @@ bool Value::IsSetIterator() const { bool Value::IsPromise() const { return Utils::OpenHandle(this)->IsJSPromise(); } +bool Value::IsModuleNamespaceObject() const { + return Utils::OpenHandle(this)->IsJSModuleNamespace(); +} + MaybeLocal Value::ToString(Local context) const { auto obj = Utils::OpenHandle(this); if (obj->IsString()) return ToApiHandle(obj); diff --git a/test/cctest/test-api.cc b/test/cctest/test-api.cc index 54afc61f4c..b7483a7c5e 100644 --- a/test/cctest/test-api.cc +++ b/test/cctest/test-api.cc @@ -27239,6 +27239,35 @@ TEST(ImportMeta) { CHECK(result->StrictEquals(Local::Cast(v8::Utils::ToLocal(meta)))); } +TEST(GetModuleNamespace) { + LocalContext context; + v8::Isolate* isolate = context->GetIsolate(); + v8::HandleScope scope(isolate); + + Local url = v8_str("www.google.com"); + Local source_text = v8_str("export default 5; export const a = 10;"); + v8::ScriptOrigin origin(url, Local(), Local(), + Local(), Local(), + Local(), Local(), + Local(), True(isolate)); + v8::ScriptCompiler::Source source(source_text, origin); + Local module = + v8::ScriptCompiler::CompileModule(isolate, &source).ToLocalChecked(); + module->InstantiateModule(context.local(), UnexpectedModuleResolveCallback) + .ToChecked(); + module->Evaluate(context.local()).ToLocalChecked(); + + Local ns_val = module->GetModuleNamespace(); + CHECK(ns_val->IsModuleNamespaceObject()); + Local ns = ns_val.As(); + CHECK(ns->Get(context.local(), v8_str("default")) + .ToLocalChecked() + ->StrictEquals(v8::Number::New(isolate, 5))); + CHECK(ns->Get(context.local(), v8_str("a")) + .ToLocalChecked() + ->StrictEquals(v8::Number::New(isolate, 10))); +} + TEST(GlobalTemplateWithDoubleProperty) { v8::Isolate* isolate = CcTest::isolate(); v8::HandleScope handle_scope(isolate);