Skip to content

Commit

Permalink
src,lib: replace DOMException with native impl
Browse files Browse the repository at this point in the history
Refs: nodejs#41038
Signed-off-by: James M Snell <jasnell@gmail.com>
  • Loading branch information
jasnell committed Dec 2, 2021
1 parent 218f13d commit 65ecdd4
Show file tree
Hide file tree
Showing 19 changed files with 73 additions and 257 deletions.
2 changes: 1 addition & 1 deletion lib/internal/abort_controller.js
Expand Up @@ -40,7 +40,7 @@ const {

const {
DOMException,
} = internalBinding('messaging');
} = internalBinding('errors');

const {
clearTimeout,
Expand Down
122 changes: 0 additions & 122 deletions lib/internal/per_context/domexception.js

This file was deleted.

4 changes: 2 additions & 2 deletions lib/internal/util.js
Expand Up @@ -468,12 +468,12 @@ function createDeferredPromise() {

let _DOMException;
const lazyDOMExceptionClass = () => {
_DOMException ??= internalBinding('messaging').DOMException;
_DOMException ??= internalBinding('errors').DOMException;
return _DOMException;
};

const lazyDOMException = hideStackFrames((message, name) => {
_DOMException ??= internalBinding('messaging').DOMException;
_DOMException ??= internalBinding('errors').DOMException;
return new _DOMException(message, name);
});

Expand Down
2 changes: 1 addition & 1 deletion lib/internal/webstreams/readablestream.js
Expand Up @@ -40,7 +40,7 @@ const {

const {
DOMException,
} = internalBinding('messaging');
} = internalBinding('errors');

const {
isArrayBufferView,
Expand Down
88 changes: 3 additions & 85 deletions lib/internal/webstreams/transfer.js
@@ -1,9 +1,7 @@
'use strict';

const {
ObjectDefineProperties,
PromiseResolve,
ReflectConstruct,
} = primordials;

const {
Expand All @@ -13,7 +11,7 @@ const {

const {
DOMException,
} = internalBinding('messaging');
} = internalBinding('errors');

const {
ReadableStream,
Expand All @@ -34,72 +32,6 @@ const {

const assert = require('internal/assert');

const {
makeTransferable,
kClone,
kDeserialize,
} = require('internal/worker/js_transferable');

// This class is a bit of a hack. The Node.js implementation of
// DOMException is not transferable/cloneable. This provides us
// with a variant that is. Unfortunately, it means playing around
// a bit with the message, name, and code properties and the
// prototype. We can revisit this if DOMException is ever made
// properly cloneable.
class CloneableDOMException extends DOMException {
constructor(message, name) {
super(message, name);
this[kDeserialize]({
message: this.message,
name: this.name,
code: this.code,
});
// eslint-disable-next-line no-constructor-return
return makeTransferable(this);
}

[kClone]() {
return {
data: {
message: this.message,
name: this.name,
code: this.code,
},
deserializeInfo:
'internal/webstreams/transfer:InternalCloneableDOMException'
};
}

[kDeserialize]({ message, name, code }) {
ObjectDefineProperties(this, {
message: {
configurable: true,
enumerable: true,
get() { return message; },
},
name: {
configurable: true,
enumerable: true,
get() { return name; },
},
code: {
configurable: true,
enumerable: true,
get() { return code; },
},
});
}
}

function InternalCloneableDOMException() {
return makeTransferable(
ReflectConstruct(
CloneableDOMException,
[],
DOMException));
}
InternalCloneableDOMException[kDeserialize] = () => {};

class CrossRealmTransformReadableSource {
constructor(port) {
this[kState] = {
Expand Down Expand Up @@ -133,7 +65,7 @@ class CrossRealmTransformReadableSource {
};

port.onmessageerror = () => {
const error = new CloneableDOMException(
const error = new DOMException(
'Internal transferred ReadableStream error',
'DataCloneError');
port.postMessage({ type: 'error', value: error });
Expand All @@ -156,10 +88,6 @@ class CrossRealmTransformReadableSource {
try {
this[kState].port.postMessage({ type: 'error', value: reason });
} catch (error) {
if (error instanceof DOMException) {
// eslint-disable-next-line no-ex-assign
error = new CloneableDOMException(error.message, error.name);
}
this[kState].port.postMessage({ type: 'error', value: error });
throw error;
} finally {
Expand Down Expand Up @@ -200,7 +128,7 @@ class CrossRealmTransformWritableSink {
}
};
port.onmessageerror = () => {
const error = new CloneableDOMException(
const error = new DOMException(
'Internal transferred ReadableStream error',
'DataCloneError');
port.postMessage({ type: 'error', value: error });
Expand Down Expand Up @@ -229,10 +157,6 @@ class CrossRealmTransformWritableSink {
try {
this[kState].port.postMessage({ type: 'chunk', value: chunk });
} catch (error) {
if (error instanceof DOMException) {
// eslint-disable-next-line no-ex-assign
error = new CloneableDOMException(error.message, error.name);
}
this[kState].port.postMessage({ type: 'error', value: error });
this[kState].port.close();
throw error;
Expand All @@ -248,10 +172,6 @@ class CrossRealmTransformWritableSink {
try {
this[kState].port.postMessage({ type: 'error', value: reason });
} catch (error) {
if (error instanceof DOMException) {
// eslint-disable-next-line no-ex-assign
error = new CloneableDOMException(error.message, error.name);
}
this[kState].port.postMessage({ type: 'error', value: error });
throw error;
} finally {
Expand Down Expand Up @@ -294,6 +214,4 @@ module.exports = {
newCrossRealmWritableSink,
CrossRealmTransformWritableSink,
CrossRealmTransformReadableSource,
CloneableDOMException,
InternalCloneableDOMException,
};
2 changes: 1 addition & 1 deletion lib/internal/webstreams/transformstream.js
Expand Up @@ -22,7 +22,7 @@ const {

const {
DOMException,
} = internalBinding('messaging');
} = internalBinding('errors');

const {
createDeferredPromise,
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/webstreams/writablestream.js
Expand Up @@ -28,7 +28,7 @@ const {

const {
DOMException,
} = internalBinding('messaging');
} = internalBinding('errors');

const {
createDeferredPromise,
Expand Down
6 changes: 5 additions & 1 deletion lib/internal/worker/io.js
Expand Up @@ -32,8 +32,12 @@ const {
receiveMessageOnPort: receiveMessageOnPort_,
stopMessagePort,
checkMessagePort,
DOMException,
} = internalBinding('messaging');

const {
DOMException,
} = internalBinding('errors');

const {
getEnvMessagePort
} = internalBinding('worker');
Expand Down
1 change: 0 additions & 1 deletion src/api/environment.cc
Expand Up @@ -669,7 +669,6 @@ Maybe<bool> InitializePrimordials(Local<Context> context) {
}

static const char* context_files[] = {"internal/per_context/primordials",
"internal/per_context/domexception",
"internal/per_context/messageport",
nullptr};

Expand Down
21 changes: 21 additions & 0 deletions src/node_errors.cc
Expand Up @@ -1175,6 +1175,27 @@ BaseObjectPtr<DOMException> DOMException::Create(
return MakeBaseObject<DOMException>(env, obj, message, name);
}

BaseObjectPtr<DOMException> DOMException::Create(
Environment* env,
Local<Value> message,
const std::string& name) {
HandleScope scope(env->isolate());

Local<Function> ctor;
if (!GetConstructorTemplate(env)->GetFunction(env->context()).ToLocal(&ctor))
return BaseObjectPtr<DOMException>();

Local<Object> obj;
if (!ctor->NewInstance(env->context()).ToLocal(&obj))
return BaseObjectPtr<DOMException>();

return MakeBaseObject<DOMException>(
env,
obj,
message,
String::NewFromUtf8(env->isolate(), name.c_str()).ToLocalChecked());
}

void DOMException::New(const FunctionCallbackInfo<Value>& args) {
CHECK(args.IsConstructCall());

Expand Down
5 changes: 5 additions & 0 deletions src/node_errors.h
Expand Up @@ -298,6 +298,11 @@ class DOMException : public BaseObject {
const std::string& message,
const std::string& name = "DOMException");

static BaseObjectPtr<DOMException> Create(
Environment* env,
v8::Local<v8::Value> message,
const std::string& name = "DOMException");

static void New(const v8::FunctionCallbackInfo<v8::Value>& args);
static void GetMessage(const v8::FunctionCallbackInfo<v8::Value>& args);
static void GetName(const v8::FunctionCallbackInfo<v8::Value>& args);
Expand Down

0 comments on commit 65ecdd4

Please sign in to comment.