Skip to content

Commit 2ec57d7

Browse files
committed
src: support (de)serialization of DOMException
1 parent 59f00d7 commit 2ec57d7

16 files changed

+334
-29
lines changed

lib/internal/per_context/domexception.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ const {
1313
TypeError,
1414
} = primordials;
1515

16+
/* eslint-disable no-undef */
17+
const isDomException = privateSymbols?.is_dom_exception;
18+
1619
function throwInvalidThisError(Base, type) {
1720
const err = new Base();
1821
const key = 'ERR_INVALID_THIS';
@@ -52,6 +55,10 @@ class DOMException {
5255
constructor(message = '', options = 'Error') {
5356
ErrorCaptureStackTrace(this);
5457

58+
if (isDomException) {
59+
this[isDomException] = true;
60+
}
61+
5562
if (options && typeof options === 'object') {
5663
const { name } = options;
5764
internalsMap.set(this, {

src/api/environment.cc

Lines changed: 53 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <cstdlib>
2+
#include "env_properties.h"
23
#include "node.h"
34
#include "node_builtins.h"
45
#include "node_context_data.h"
@@ -599,7 +600,8 @@ std::unique_ptr<MultiIsolatePlatform> MultiIsolatePlatform::Create(
599600
page_allocator);
600601
}
601602

602-
MaybeLocal<Object> GetPerContextExports(Local<Context> context) {
603+
MaybeLocal<Object> GetPerContextExports(Local<Context> context,
604+
IsolateData* isolate_data) {
603605
Isolate* isolate = context->GetIsolate();
604606
EscapableHandleScope handle_scope(isolate);
605607

@@ -615,20 +617,22 @@ MaybeLocal<Object> GetPerContextExports(Local<Context> context) {
615617

616618
Local<Object> exports = Object::New(isolate);
617619
if (context->Global()->SetPrivate(context, key, exports).IsNothing() ||
618-
InitializePrimordials(context).IsNothing())
620+
InitializePrimordials(context, isolate_data).IsNothing()) {
619621
return MaybeLocal<Object>();
622+
}
620623
return handle_scope.Escape(exports);
621624
}
622625

623626
// Any initialization logic should be performed in
624627
// InitializeContext, because embedders don't necessarily
625628
// call NewContext and so they will experience breakages.
626629
Local<Context> NewContext(Isolate* isolate,
630+
IsolateData* isolate_data,
627631
Local<ObjectTemplate> object_template) {
628632
auto context = Context::New(isolate, nullptr, object_template);
629633
if (context.IsEmpty()) return context;
630634

631-
if (InitializeContext(context).IsNothing()) {
635+
if (InitializeContext(context, isolate_data).IsNothing()) {
632636
return Local<Context>();
633637
}
634638

@@ -745,7 +749,8 @@ Maybe<void> InitializeBaseContextForSnapshot(Local<Context> context) {
745749
return JustVoid();
746750
}
747751

748-
Maybe<void> InitializeMainContextForSnapshot(Local<Context> context) {
752+
Maybe<void> InitializeMainContextForSnapshot(Local<Context> context,
753+
IsolateData* isolate_data) {
749754
Isolate* isolate = context->GetIsolate();
750755
HandleScope handle_scope(isolate);
751756

@@ -758,10 +763,35 @@ Maybe<void> InitializeMainContextForSnapshot(Local<Context> context) {
758763
if (InitializeBaseContextForSnapshot(context).IsNothing()) {
759764
return Nothing<void>();
760765
}
761-
return InitializePrimordials(context);
766+
return InitializePrimordials(context, isolate_data);
767+
}
768+
769+
Local<Object> InitializePrivateSymbols(Local<Context> context,
770+
IsolateData* isolate_data) {
771+
Isolate* isolate = context->GetIsolate();
772+
EscapableHandleScope scope(isolate);
773+
Context::Scope context_scope(context);
774+
775+
Local<ObjectTemplate> private_symbols;
776+
Local<Object> private_symbols_object;
777+
private_symbols = ObjectTemplate::New(isolate);
778+
#define V(PropertyName, _) \
779+
private_symbols->Set(isolate, #PropertyName, isolate_data->PropertyName());
780+
781+
PER_ISOLATE_PRIVATE_SYMBOL_PROPERTIES(V)
782+
#undef V
783+
784+
if (!private_symbols->NewInstance(context).ToLocal(&private_symbols_object) ||
785+
private_symbols_object->SetPrototypeV2(context, Null(isolate))
786+
.IsNothing()) {
787+
return Local<Object>();
788+
}
789+
790+
return scope.Escape(private_symbols_object);
762791
}
763792

764-
Maybe<void> InitializePrimordials(Local<Context> context) {
793+
Maybe<void> InitializePrimordials(Local<Context> context,
794+
IsolateData* isolate_data) {
765795
// Run per-context JS files.
766796
Isolate* isolate = context->GetIsolate();
767797
Context::Scope context_scope(context);
@@ -772,12 +802,18 @@ Maybe<void> InitializePrimordials(Local<Context> context) {
772802

773803
// Create primordials first and make it available to per-context scripts.
774804
Local<Object> primordials = Object::New(isolate);
805+
775806
if (primordials->SetPrototypeV2(context, Null(isolate)).IsNothing() ||
776-
!GetPerContextExports(context).ToLocal(&exports) ||
807+
!GetPerContextExports(context, isolate_data).ToLocal(&exports) ||
777808
exports->Set(context, primordials_string, primordials).IsNothing()) {
778809
return Nothing<void>();
779810
}
780811

812+
Local<Object> private_symbols;
813+
if (isolate_data != nullptr) {
814+
private_symbols = InitializePrivateSymbols(context, isolate_data);
815+
}
816+
781817
static const char* context_files[] = {"internal/per_context/primordials",
782818
"internal/per_context/domexception",
783819
"internal/per_context/messageport",
@@ -793,7 +829,13 @@ Maybe<void> InitializePrimordials(Local<Context> context) {
793829
builtin_loader.SetEagerCompile();
794830

795831
for (const char** module = context_files; *module != nullptr; module++) {
796-
Local<Value> arguments[] = {exports, primordials};
832+
Local<Value> arguments[3];
833+
arguments[0] = exports;
834+
arguments[1] = primordials;
835+
arguments[2] = private_symbols.IsEmpty() ?
836+
Local<Value>(Undefined(isolate)) :
837+
Local<Value>(private_symbols);
838+
797839
if (builtin_loader
798840
.CompileAndCall(
799841
context, *module, arraysize(arguments), arguments, nullptr)
@@ -807,8 +849,9 @@ Maybe<void> InitializePrimordials(Local<Context> context) {
807849
}
808850

809851
// This initializes the main context (i.e. vm contexts are not included).
810-
Maybe<bool> InitializeContext(Local<Context> context) {
811-
if (InitializeMainContextForSnapshot(context).IsNothing()) {
852+
Maybe<bool> InitializeContext(Local<Context> context,
853+
IsolateData* isolate_data) {
854+
if (InitializeMainContextForSnapshot(context, isolate_data).IsNothing()) {
812855
return Nothing<bool>();
813856
}
814857

src/env_properties.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
V(decorated_private_symbol, "node:decorated") \
2424
V(transfer_mode_private_symbol, "node:transfer_mode") \
2525
V(host_defined_option_symbol, "node:host_defined_option_symbol") \
26+
V(is_dom_exception, "node:is_dom_exception") \
2627
V(js_transferable_wrapper_private_symbol, "node:js_transferable_wrapper") \
2728
V(entry_point_module_private_symbol, "node:entry_point_module") \
2829
V(entry_point_promise_private_symbol, "node:entry_point_promise") \

src/node.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -590,12 +590,14 @@ NODE_EXTERN v8::Isolate* NewIsolate(
590590
// Creates a new context with Node.js-specific tweaks.
591591
NODE_EXTERN v8::Local<v8::Context> NewContext(
592592
v8::Isolate* isolate,
593+
IsolateData* isolate_data = nullptr,
593594
v8::Local<v8::ObjectTemplate> object_template =
594595
v8::Local<v8::ObjectTemplate>());
595596

596597
// Runs Node.js-specific tweaks on an already constructed context
597598
// Return value indicates success of operation
598-
NODE_EXTERN v8::Maybe<bool> InitializeContext(v8::Local<v8::Context> context);
599+
NODE_EXTERN v8::Maybe<bool> InitializeContext(
600+
v8::Local<v8::Context> context, IsolateData* isolate_data = nullptr);
599601

600602
// If `platform` is passed, it will be used to register new Worker instances.
601603
// It can be `nullptr`, in which case creating new Workers inside of

src/node_builtins.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,7 @@ MaybeLocal<Function> BuiltinLoader::LookupAndCompile(Local<Context> context,
402402
parameters = {
403403
FIXED_ONE_BYTE_STRING(isolate, "exports"),
404404
FIXED_ONE_BYTE_STRING(isolate, "primordials"),
405+
FIXED_ONE_BYTE_STRING(isolate, "privateSymbols"),
405406
};
406407
} else if (strncmp(id, "internal/main/", strlen("internal/main/")) == 0 ||
407408
strncmp(id,

src/node_internals.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,10 @@ std::string GetHumanReadableProcessName();
113113
v8::Maybe<void> InitializeBaseContextForSnapshot(
114114
v8::Local<v8::Context> context);
115115
v8::Maybe<void> InitializeContextRuntime(v8::Local<v8::Context> context);
116-
v8::Maybe<void> InitializePrimordials(v8::Local<v8::Context> context);
116+
v8::Maybe<void> InitializePrimordials(v8::Local<v8::Context> context,
117+
IsolateData* isolate_data);
118+
v8::Local<v8::Object> InitializePrivateSymbols(v8::Local<v8::Context> context,
119+
IsolateData* isolate_data);
117120

118121
class NodeArrayBufferAllocator : public ArrayBufferAllocator {
119122
public:
@@ -340,7 +343,8 @@ v8::Isolate* NewIsolate(v8::Isolate::CreateParams* params,
340343
// was provided by the embedder.
341344
v8::MaybeLocal<v8::Value> StartExecution(Environment* env,
342345
StartExecutionCallback cb = nullptr);
343-
v8::MaybeLocal<v8::Object> GetPerContextExports(v8::Local<v8::Context> context);
346+
v8::MaybeLocal<v8::Object> GetPerContextExports(
347+
v8::Local<v8::Context> context, IsolateData* isolate_data = nullptr);
344348
void MarkBootstrapComplete(const v8::FunctionCallbackInfo<v8::Value>& args);
345349

346350
class InitializationResultImpl final : public InitializationResult {

src/node_main_instance.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ NodeMainInstance::CreateMainEnvironment(ExitCode* exit_code) {
140140
crypto::InitCryptoOnce(isolate_);
141141
#endif // HAVE_OPENSSL
142142
} else {
143-
context = NewContext(isolate_);
143+
context = NewContext(isolate_, isolate_data_.get());
144144
CHECK(!context.IsEmpty());
145145
Context::Scope context_scope(context);
146146
env.reset(

0 commit comments

Comments
 (0)