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 <anna@addaleax.net> Reviewed-By: Rich Trott <rtrott@gmail.com>
This commit is contained in:
parent
0c1fd20693
commit
890223dede
@ -186,27 +186,30 @@ uint32_t Message::AddWASMModule(WasmModuleObject::TransferrableModule&& mod) {
|
||||
|
||||
namespace {
|
||||
|
||||
void ThrowDataCloneException(Local<Context> context, Local<String> message) {
|
||||
MaybeLocal<Function> GetDOMException(Local<Context> context) {
|
||||
Isolate* isolate = context->GetIsolate();
|
||||
Local<Value> argv[] = {
|
||||
message,
|
||||
FIXED_ONE_BYTE_STRING(isolate, "DataCloneError")
|
||||
};
|
||||
Local<Value> exception;
|
||||
|
||||
Local<Object> per_context_bindings;
|
||||
Local<Value> 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<Function>();
|
||||
}
|
||||
|
||||
CHECK(domexception_ctor_val->IsFunction());
|
||||
Local<Function> domexception_ctor = domexception_ctor_val.As<Function>();
|
||||
if (!domexception_ctor->NewInstance(context, arraysize(argv), argv)
|
||||
.ToLocal(&exception)) {
|
||||
return domexception_ctor;
|
||||
}
|
||||
|
||||
void ThrowDataCloneException(Local<Context> context, Local<String> message) {
|
||||
Isolate* isolate = context->GetIsolate();
|
||||
Local<Value> argv[] = {message,
|
||||
FIXED_ONE_BYTE_STRING(isolate, "DataCloneError")};
|
||||
Local<Value> exception;
|
||||
Local<Function> 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<Object> target,
|
||||
env->SetMethod(target, "receiveMessageOnPort", MessagePort::ReceiveMessage);
|
||||
env->SetMethod(target, "moveMessagePortToContext",
|
||||
MessagePort::MoveToContext);
|
||||
|
||||
{
|
||||
Local<Function> domexception = GetDOMException(context).ToLocalChecked();
|
||||
target
|
||||
->Set(context,
|
||||
FIXED_ONE_BYTE_STRING(env->isolate(), "DOMException"),
|
||||
domexception)
|
||||
.Check();
|
||||
}
|
||||
}
|
||||
|
||||
} // anonymous namespace
|
||||
|
@ -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;
|
||||
}
|
||||
});
|
||||
|
Loading…
x
Reference in New Issue
Block a user