From f8776d2632076c4ac871ea731d906d6d3848774c Mon Sep 17 00:00:00 2001 From: Gus Caplan Date: Fri, 13 Apr 2018 11:49:41 -0700 Subject: [PATCH 1/2] deps: cherry-pick 39d546a from upstream V8 Original commit message: [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 Commit-Queue: Georg Neis Cr-Commit-Position: refs/heads/master@{#52597} Refs: https://github.com/v8/v8/commit/39d546a24022b62b00aedf7b556ac6c9e2306aab --- deps/v8/AUTHORS | 1 + deps/v8/include/v8.h | 5 +++++ deps/v8/src/api.cc | 4 ++++ deps/v8/test/cctest/test-api.cc | 29 +++++++++++++++++++++++++++++ 4 files changed, 39 insertions(+) diff --git a/deps/v8/AUTHORS b/deps/v8/AUTHORS index b2b01df888281e..76a27ac1952ace 100644 --- a/deps/v8/AUTHORS +++ b/deps/v8/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/deps/v8/include/v8.h b/deps/v8/include/v8.h index 277cbd442a124c..0149ed3ca2406e 100644 --- a/deps/v8/include/v8.h +++ b/deps/v8/include/v8.h @@ -2354,6 +2354,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 ToBoolean( Local context) const; V8_WARN_UNUSED_RESULT MaybeLocal ToNumber( diff --git a/deps/v8/src/api.cc b/deps/v8/src/api.cc index 8531cd5c057bb7..ffe9bef52056be 100644 --- a/deps/v8/src/api.cc +++ b/deps/v8/src/api.cc @@ -3584,6 +3584,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/deps/v8/test/cctest/test-api.cc b/deps/v8/test/cctest/test-api.cc index b13208950f8d61..fe1aca5a0fb440 100644 --- a/deps/v8/test/cctest/test-api.cc +++ b/deps/v8/test/cctest/test-api.cc @@ -27041,6 +27041,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); From 8daa1c90ff7e5cc25b8a90a83cda43f8f56bb68f Mon Sep 17 00:00:00 2001 From: Gus Caplan Date: Fri, 13 Apr 2018 12:22:41 -0700 Subject: [PATCH 2/2] util: introduce types.isModuleNamespaceObject --- src/node_types.cc | 1 + test/parallel/test-util-types.js | 10 +++++++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/node_types.cc b/src/node_types.cc index 4b9f86f0d54d8c..bd7ea9cf23fff7 100644 --- a/src/node_types.cc +++ b/src/node_types.cc @@ -34,6 +34,7 @@ namespace { V(SharedArrayBuffer) \ V(Proxy) \ V(WebAssemblyCompiledModule) \ + V(ModuleNamespaceObject) \ #define V(type) \ diff --git a/test/parallel/test-util-types.js b/test/parallel/test-util-types.js index aa45d1604bfe00..924869896869c0 100644 --- a/test/parallel/test-util-types.js +++ b/test/parallel/test-util-types.js @@ -1,4 +1,4 @@ -// Flags: --harmony-bigint +// Flags: --harmony-bigint --experimental-vm-modules /* global SharedArrayBuffer */ 'use strict'; const common = require('../common'); @@ -126,3 +126,11 @@ for (const [ value, _method ] of [ assert.deepStrictEqual(yup, expected[testedFunc]); } } + +(async () => { + const m = new vm.Module(''); + await m.link(() => 0); + m.instantiate(); + await m.evaluate(); + assert.ok(types.isModuleNamespaceObject(m.namespace)); +})();