lib,src: support DOMException ser-des · nodejs/node@6390f70 · GitHub
Skip to content

Commit 6390f70

Browse files
legendecasjazelly
authored andcommitted
lib,src: support DOMException ser-des
Added serialization and deserialization support for `DOMException`. Co-Authored-By: jazelly <xzha4350@gmail.com> PR-URL: #58649 Fixes: #49181 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Ethan Arrowood <ethan@arrowood.dev> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Jason Zhang <xzha4350@gmail.com>
1 parent afbaf92 commit 6390f70

6 files changed

Lines changed: 137 additions & 2 deletions

File tree

lib/eslint.config_partial.mjs

Lines changed: 12 additions & 0 deletions

lib/internal/per_context/domexception.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,18 @@ const {
1212
SymbolToStringTag,
1313
TypeError,
1414
} = primordials;
15+
const {
16+
transfer_mode_private_symbol,
17+
} = privateSymbols;
18+
const {
19+
messaging_clone_symbol,
20+
messaging_deserialize_symbol,
21+
} = perIsolateSymbols;
22+
23+
/**
24+
* Maps to BaseObject::TransferMode::kCloneable
25+
*/
26+
const kCloneable = 2;
1527

1628
function throwInvalidThisError(Base, type) {
1729
const err = new Base();
@@ -50,6 +62,7 @@ const disusedNamesSet = new SafeSet()
5062

5163
class DOMException {
5264
constructor(message = '', options = 'Error') {
65+
this[transfer_mode_private_symbol] = kCloneable;
5366
ErrorCaptureStackTrace(this);
5467

5568
if (options && typeof options === 'object') {
@@ -76,6 +89,28 @@ class DOMException {
7689
}
7790
}
7891

92+
[messaging_clone_symbol]() {
93+
// See serialization steps in https://webidl.spec.whatwg.org/#dom-domexception-domexception
94+
const internals = internalsMap.get(this);
95+
return {
96+
data: {
97+
message: internals.message,
98+
name: internals.name,
99+
stack: this.stack,
100+
},
101+
deserializeInfo: 'internal/worker/clone_dom_exception:DOMException',
102+
};
103+
}
104+
105+
[messaging_deserialize_symbol](data) {
106+
// See deserialization steps in https://webidl.spec.whatwg.org/#dom-domexception-domexception
107+
internalsMap.set(this, {
108+
message: data.message,
109+
name: data.name,
110+
});
111+
this.stack = data.stack;
112+
}
113+
79114
get name() {
80115
const internals = internalsMap.get(this);
81116
if (internals === undefined) {
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
'use strict';
2+
3+
// Delegate to the actual DOMException implementation.
4+
module.exports = {
5+
DOMException: internalBinding('messaging').DOMException,
6+
};

src/api/environment.cc

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -787,13 +787,13 @@ MaybeLocal<Object> InitializePrivateSymbols(Local<Context> context,
787787
Context::Scope context_scope(context);
788788

789789
Local<ObjectTemplate> private_symbols = ObjectTemplate::New(isolate);
790-
Local<Object> private_symbols_object;
791790
#define V(PropertyName, _) \
792791
private_symbols->Set(isolate, #PropertyName, isolate_data->PropertyName());
793792

794793
PER_ISOLATE_PRIVATE_SYMBOL_PROPERTIES(V)
795794
#undef V
796795

796+
Local<Object> private_symbols_object;
797797
if (!private_symbols->NewInstance(context).ToLocal(&private_symbols_object) ||
798798
private_symbols_object->SetPrototypeV2(context, Null(isolate))
799799
.IsNothing()) {
@@ -803,6 +803,32 @@ MaybeLocal<Object> InitializePrivateSymbols(Local<Context> context,
803803
return scope.Escape(private_symbols_object);
804804
}
805805

806+
MaybeLocal<Object> InitializePerIsolateSymbols(Local<Context> context,
807+
IsolateData* isolate_data) {
808+
CHECK(isolate_data);
809+
Isolate* isolate = context->GetIsolate();
810+
EscapableHandleScope scope(isolate);
811+
Context::Scope context_scope(context);
812+
813+
Local<ObjectTemplate> per_isolate_symbols = ObjectTemplate::New(isolate);
814+
#define V(PropertyName, _) \
815+
per_isolate_symbols->Set( \
816+
isolate, #PropertyName, isolate_data->PropertyName());
817+
818+
PER_ISOLATE_SYMBOL_PROPERTIES(V)
819+
#undef V
820+
821+
Local<Object> per_isolate_symbols_object;
822+
if (!per_isolate_symbols->NewInstance(context).ToLocal(
823+
&per_isolate_symbols_object) ||
824+
per_isolate_symbols_object->SetPrototypeV2(context, Null(isolate))
825+
.IsNothing()) {
826+
return MaybeLocal<Object>();
827+
}
828+
829+
return scope.Escape(per_isolate_symbols_object);
830+
}
831+
806832
Maybe<void> InitializePrimordials(Local<Context> context,
807833
IsolateData* isolate_data) {
808834
// Run per-context JS files.
@@ -832,6 +858,12 @@ Maybe<void> InitializePrimordials(Local<Context> context,
832858
return Nothing<void>();
833859
}
834860

861+
Local<Object> per_isolate_symbols;
862+
if (!InitializePerIsolateSymbols(context, isolate_data)
863+
.ToLocal(&per_isolate_symbols)) {
864+
return Nothing<void>();
865+
}
866+
835867
static const char* context_files[] = {"internal/per_context/primordials",
836868
"internal/per_context/domexception",
837869
"internal/per_context/messageport",
@@ -847,7 +879,8 @@ Maybe<void> InitializePrimordials(Local<Context> context,
847879
builtin_loader.SetEagerCompile();
848880

849881
for (const char** module = context_files; *module != nullptr; module++) {
850-
Local<Value> arguments[] = {exports, primordials, private_symbols};
882+
Local<Value> arguments[] = {
883+
exports, primordials, private_symbols, per_isolate_symbols};
851884

852885
if (builtin_loader
853886
.CompileAndCall(

src/node_builtins.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,7 @@ MaybeLocal<Function> BuiltinLoader::LookupAndCompile(Local<Context> context,
415415
FIXED_ONE_BYTE_STRING(isolate, "exports"),
416416
FIXED_ONE_BYTE_STRING(isolate, "primordials"),
417417
FIXED_ONE_BYTE_STRING(isolate, "privateSymbols"),
418+
FIXED_ONE_BYTE_STRING(isolate, "perIsolateSymbols"),
418419
};
419420
} else if (strncmp(id, "internal/main/", strlen("internal/main/")) == 0 ||
420421
strncmp(id,
Lines changed: 48 additions & 0 deletions

0 commit comments

Comments
 (0)