Skip to content

Commit 39d546a

Browse files
devsnekCommit Bot
authored andcommitted
[api] introduce v8::Value::IsModuleNamespaceObject
This allows an embedder to check if a Value is a module namespace object. Cq-Include-Trybots: luci.chromium.try:linux_chromium_rel_ng Change-Id: Idffceff451dd5f5c6a53d4cb3ce02c1c2c5b653c Reviewed-on: https://chromium-review.googlesource.com/1011762 Reviewed-by: Georg Neis <[email protected]> Commit-Queue: Georg Neis <[email protected]> Cr-Commit-Position: refs/heads/master@{#52597}
1 parent 42049b4 commit 39d546a

4 files changed

Lines changed: 39 additions & 0 deletions

File tree

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ Felix Geisendörfer <[email protected]>
7070
Filipe David Manana <[email protected]>
7171
Franziska Hinkelmann <[email protected]>
7272
Geoffrey Garside <[email protected]>
73+
Gus Caplan <[email protected]>
7374
Gwang Yoon Hwang <[email protected]>
7475
Henrique Ferreiro <[email protected]>
7576
Hirofumi Mako <[email protected]>

include/v8.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2378,6 +2378,11 @@ class V8_EXPORT Value : public Data {
23782378

23792379
bool IsWebAssemblyCompiledModule() const;
23802380

2381+
/**
2382+
* Returns true if the value is a Module Namespace Object.
2383+
*/
2384+
bool IsModuleNamespaceObject() const;
2385+
23812386
V8_WARN_UNUSED_RESULT MaybeLocal<BigInt> ToBigInt(
23822387
Local<Context> context) const;
23832388
V8_WARN_UNUSED_RESULT MaybeLocal<Boolean> ToBoolean(

src/api.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3583,6 +3583,10 @@ bool Value::IsSetIterator() const {
35833583

35843584
bool Value::IsPromise() const { return Utils::OpenHandle(this)->IsJSPromise(); }
35853585

3586+
bool Value::IsModuleNamespaceObject() const {
3587+
return Utils::OpenHandle(this)->IsJSModuleNamespace();
3588+
}
3589+
35863590
MaybeLocal<String> Value::ToString(Local<Context> context) const {
35873591
auto obj = Utils::OpenHandle(this);
35883592
if (obj->IsString()) return ToApiHandle<String>(obj);

test/cctest/test-api.cc

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27239,6 +27239,35 @@ TEST(ImportMeta) {
2723927239
CHECK(result->StrictEquals(Local<v8::Value>::Cast(v8::Utils::ToLocal(meta))));
2724027240
}
2724127241

27242+
TEST(GetModuleNamespace) {
27243+
LocalContext context;
27244+
v8::Isolate* isolate = context->GetIsolate();
27245+
v8::HandleScope scope(isolate);
27246+
27247+
Local<String> url = v8_str("www.google.com");
27248+
Local<String> source_text = v8_str("export default 5; export const a = 10;");
27249+
v8::ScriptOrigin origin(url, Local<v8::Integer>(), Local<v8::Integer>(),
27250+
Local<v8::Boolean>(), Local<v8::Integer>(),
27251+
Local<v8::Value>(), Local<v8::Boolean>(),
27252+
Local<v8::Boolean>(), True(isolate));
27253+
v8::ScriptCompiler::Source source(source_text, origin);
27254+
Local<Module> module =
27255+
v8::ScriptCompiler::CompileModule(isolate, &source).ToLocalChecked();
27256+
module->InstantiateModule(context.local(), UnexpectedModuleResolveCallback)
27257+
.ToChecked();
27258+
module->Evaluate(context.local()).ToLocalChecked();
27259+
27260+
Local<Value> ns_val = module->GetModuleNamespace();
27261+
CHECK(ns_val->IsModuleNamespaceObject());
27262+
Local<Object> ns = ns_val.As<Object>();
27263+
CHECK(ns->Get(context.local(), v8_str("default"))
27264+
.ToLocalChecked()
27265+
->StrictEquals(v8::Number::New(isolate, 5)));
27266+
CHECK(ns->Get(context.local(), v8_str("a"))
27267+
.ToLocalChecked()
27268+
->StrictEquals(v8::Number::New(isolate, 10)));
27269+
}
27270+
2724227271
TEST(GlobalTemplateWithDoubleProperty) {
2724327272
v8::Isolate* isolate = CcTest::isolate();
2724427273
v8::HandleScope handle_scope(isolate);

0 commit comments

Comments
 (0)