From 8a032fc50c51eea618f68fdf33ce7f5ed75025a2 Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Wed, 5 Jun 2019 17:06:32 +0200 Subject: [PATCH] src: expose DOMException to internalBinding('message') for testing Instead of using a hack to get it in the test. PR-URL: https://github.com/nodejs/node/pull/28072 Reviewed-By: Anna Henningsen Reviewed-By: Rich Trott --- src/node_messaging.cc | 34 +++++++++++++++++++++++----------- test/wpt/test-url.js | 17 ++--------------- 2 files changed, 25 insertions(+), 26 deletions(-) diff --git a/src/node_messaging.cc b/src/node_messaging.cc index fa583a2570315b..0e782ef72675ef 100644 --- a/src/node_messaging.cc +++ b/src/node_messaging.cc @@ -186,27 +186,30 @@ uint32_t Message::AddWASMModule(WasmModuleObject::TransferrableModule&& mod) { namespace { -void ThrowDataCloneException(Local context, Local message) { +MaybeLocal GetDOMException(Local context) { Isolate* isolate = context->GetIsolate(); - Local argv[] = { - message, - FIXED_ONE_BYTE_STRING(isolate, "DataCloneError") - }; - Local exception; - Local per_context_bindings; Local domexception_ctor_val; if (!GetPerContextExports(context).ToLocal(&per_context_bindings) || !per_context_bindings->Get(context, FIXED_ONE_BYTE_STRING(isolate, "DOMException")) .ToLocal(&domexception_ctor_val)) { - return; + return MaybeLocal(); } - CHECK(domexception_ctor_val->IsFunction()); Local domexception_ctor = domexception_ctor_val.As(); - if (!domexception_ctor->NewInstance(context, arraysize(argv), argv) - .ToLocal(&exception)) { + return domexception_ctor; +} + +void ThrowDataCloneException(Local context, Local message) { + Isolate* isolate = context->GetIsolate(); + Local argv[] = {message, + FIXED_ONE_BYTE_STRING(isolate, "DataCloneError")}; + Local exception; + Local domexception_ctor; + if (!GetDOMException(context).ToLocal(&domexception_ctor) || + !domexception_ctor->NewInstance(context, arraysize(argv), argv) + .ToLocal(&exception)) { return; } isolate->ThrowException(exception); @@ -900,6 +903,15 @@ static void InitMessaging(Local target, env->SetMethod(target, "receiveMessageOnPort", MessagePort::ReceiveMessage); env->SetMethod(target, "moveMessagePortToContext", MessagePort::MoveToContext); + + { + Local domexception = GetDOMException(context).ToLocalChecked(); + target + ->Set(context, + FIXED_ONE_BYTE_STRING(env->isolate(), "DOMException"), + domexception) + .Check(); + } } } // anonymous namespace diff --git a/test/wpt/test-url.js b/test/wpt/test-url.js index 4b909988ddb5ee..5d5240ce181393 100644 --- a/test/wpt/test-url.js +++ b/test/wpt/test-url.js @@ -3,29 +3,16 @@ // Flags: --expose-internals require('../common'); -const assert = require('assert'); const { WPTRunner } = require('../common/wpt'); - +const { internalBinding } = require('internal/test/binding'); +const { DOMException } = internalBinding('messaging'); const runner = new WPTRunner('url'); // Copy global descriptors from the global object runner.copyGlobalsFromObject(global, ['URL', 'URLSearchParams']); // Needed by urlsearchparams-constructor.any.js -let DOMException; runner.defineGlobal('DOMException', { get() { - // A 'hack' to get the DOMException constructor since we don't have it - // on the global object. - if (DOMException === undefined) { - const port = new (require('worker_threads').MessagePort)(); - const ab = new ArrayBuffer(1); - try { - port.postMessage(ab, [ab, ab]); - } catch (err) { - DOMException = err.constructor; - } - assert.strictEqual(DOMException.name, 'DOMException'); - } return DOMException; } });