From a7aff77a97e62a9fbc1a2dccb7cf2f4e131f5328 Mon Sep 17 00:00:00 2001 From: Gus Caplan Date: Fri, 13 Apr 2018 11:49:41 -0700 Subject: [PATCH] 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 PR-URL: https://github.com/nodejs/node/pull/20016 Reviewed-By: Ben Noordhuis Reviewed-By: Guy Bedford Reviewed-By: Colin Ihrig --- common.gypi | 2 +- deps/v8/AUTHORS | 1 + deps/v8/include/v8.h | 5 +++++ deps/v8/src/api.cc | 4 ++++ deps/v8/test/cctest/test-api.cc | 29 +++++++++++++++++++++++++++++ 5 files changed, 40 insertions(+), 1 deletion(-) diff --git a/common.gypi b/common.gypi index 9148252d519b94..08e4f36cec4d24 100644 --- a/common.gypi +++ b/common.gypi @@ -27,7 +27,7 @@ # Reset this number to 0 on major V8 upgrades. # Increment by one for each non-official patch applied to deps/v8. - 'v8_embedder_string': '-node.1', + 'v8_embedder_string': '-node.2', # Enable disassembler for `--print-code` v8 options 'v8_enable_disassembler': 1, diff --git a/deps/v8/AUTHORS b/deps/v8/AUTHORS index 3251716f2a55f5..4b5163961d282e 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 d520c866042a7a..c6e9250e4e938b 100644 --- a/deps/v8/include/v8.h +++ b/deps/v8/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/deps/v8/src/api.cc b/deps/v8/src/api.cc index 8b177d041d447f..6dd669ee1133bf 100644 --- a/deps/v8/src/api.cc +++ b/deps/v8/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/deps/v8/test/cctest/test-api.cc b/deps/v8/test/cctest/test-api.cc index f126433578e336..261ada0f2bb308 100644 --- a/deps/v8/test/cctest/test-api.cc +++ b/deps/v8/test/cctest/test-api.cc @@ -27274,6 +27274,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);