Skip to content

Commit cded81b

Browse files
committed
src: expose isolate_data before env
1 parent 59f00d7 commit cded81b

17 files changed

+277
-34
lines changed

lib/internal/per_context/domexception.js

Lines changed: 5 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 { is_dom_exception } = privateSymbols;
18+
1619
function throwInvalidThisError(Base, type) {
1720
const err = new Base();
1821
const key = 'ERR_INVALID_THIS';
@@ -52,6 +55,8 @@ class DOMException {
5255
constructor(message = '', options = 'Error') {
5356
ErrorCaptureStackTrace(this);
5457

58+
this[is_dom_exception] = true;
59+
5560
if (options && typeof options === 'object') {
5661
const { name } = options;
5762
internalsMap.set(this, {

src/api/embed_helpers.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ CommonEnvironmentSetup::CommonEnvironmentSetup(
153153
return;
154154
}
155155

156-
Local<Context> context = NewContext(isolate);
156+
Local<Context> context = NewContext(isolate, impl_->isolate_data.get());
157157
impl_->main_context.Reset(isolate, context);
158158
if (context.IsEmpty()) {
159159
errors->push_back("Failed to initialize V8 Context");

src/api/environment.cc

Lines changed: 50 additions & 11 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

@@ -614,21 +616,24 @@ MaybeLocal<Object> GetPerContextExports(Local<Context> context) {
614616
return handle_scope.Escape(existing_value.As<Object>());
615617

616618
Local<Object> exports = Object::New(isolate);
617-
if (context->Global()->SetPrivate(context, key, exports).IsNothing() ||
618-
InitializePrimordials(context).IsNothing())
619+
if (isolate_data == nullptr ||
620+
context->Global()->SetPrivate(context, key, exports).IsNothing() ||
621+
InitializePrimordials(context, isolate_data).IsNothing()) {
619622
return MaybeLocal<Object>();
623+
}
620624
return handle_scope.Escape(exports);
621625
}
622626

623627
// Any initialization logic should be performed in
624628
// InitializeContext, because embedders don't necessarily
625629
// call NewContext and so they will experience breakages.
626630
Local<Context> NewContext(Isolate* isolate,
631+
IsolateData* isolate_data,
627632
Local<ObjectTemplate> object_template) {
628633
auto context = Context::New(isolate, nullptr, object_template);
629634
if (context.IsEmpty()) return context;
630635

631-
if (InitializeContext(context).IsNothing()) {
636+
if (InitializeContext(context, isolate_data).IsNothing()) {
632637
return Local<Context>();
633638
}
634639

@@ -745,7 +750,8 @@ Maybe<void> InitializeBaseContextForSnapshot(Local<Context> context) {
745750
return JustVoid();
746751
}
747752

748-
Maybe<void> InitializeMainContextForSnapshot(Local<Context> context) {
753+
Maybe<void> InitializeMainContextForSnapshot(Local<Context> context,
754+
IsolateData* isolate_data) {
749755
Isolate* isolate = context->GetIsolate();
750756
HandleScope handle_scope(isolate);
751757

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

764-
Maybe<void> InitializePrimordials(Local<Context> context) {
794+
Maybe<void> InitializePrimordials(Local<Context> context,
795+
IsolateData* isolate_data) {
765796
// Run per-context JS files.
766797
Isolate* isolate = context->GetIsolate();
767798
Context::Scope context_scope(context);
@@ -772,12 +803,19 @@ Maybe<void> InitializePrimordials(Local<Context> context) {
772803

773804
// Create primordials first and make it available to per-context scripts.
774805
Local<Object> primordials = Object::New(isolate);
806+
775807
if (primordials->SetPrototypeV2(context, Null(isolate)).IsNothing() ||
776-
!GetPerContextExports(context).ToLocal(&exports) ||
808+
!GetPerContextExports(context, isolate_data).ToLocal(&exports) ||
777809
exports->Set(context, primordials_string, primordials).IsNothing()) {
778810
return Nothing<void>();
779811
}
780812

813+
Local<Object> private_symbols =
814+
InitializePrivateSymbols(context, isolate_data);
815+
if (private_symbols.IsEmpty()) {
816+
return Nothing<void>();
817+
}
818+
781819
static const char* context_files[] = {"internal/per_context/primordials",
782820
"internal/per_context/domexception",
783821
"internal/per_context/messageport",
@@ -793,7 +831,7 @@ Maybe<void> InitializePrimordials(Local<Context> context) {
793831
builtin_loader.SetEagerCompile();
794832

795833
for (const char** module = context_files; *module != nullptr; module++) {
796-
Local<Value> arguments[] = {exports, primordials};
834+
Local<Value> arguments[] = {exports, primordials, private_symbols};
797835
if (builtin_loader
798836
.CompileAndCall(
799837
context, *module, arraysize(arguments), arguments, nullptr)
@@ -807,8 +845,9 @@ Maybe<void> InitializePrimordials(Local<Context> context) {
807845
}
808846

809847
// This initializes the main context (i.e. vm contexts are not included).
810-
Maybe<bool> InitializeContext(Local<Context> context) {
811-
if (InitializeMainContextForSnapshot(context).IsNothing()) {
848+
Maybe<bool> InitializeContext(Local<Context> context,
849+
IsolateData* isolate_data) {
850+
if (InitializeMainContextForSnapshot(context, isolate_data).IsNothing()) {
812851
return Nothing<bool>();
813852
}
814853

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,
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);
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)