embedding: make NewIsolate()
API more flexible
Split the API up into its essential parts, namely setting up the creation parameters for the Isolate, creating it, and performing Node.js-specific customization afterwards. PR-URL: https://github.com/nodejs/node/pull/26525 Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
This commit is contained in:
parent
0e3adddd0d
commit
b21e7c7bcf
@ -161,30 +161,25 @@ void FreeArrayBufferAllocator(ArrayBufferAllocator* allocator) {
|
|||||||
delete allocator;
|
delete allocator;
|
||||||
}
|
}
|
||||||
|
|
||||||
Isolate* NewIsolate(ArrayBufferAllocator* allocator, uv_loop_t* event_loop) {
|
void SetIsolateCreateParams(Isolate::CreateParams* params,
|
||||||
Isolate::CreateParams params;
|
ArrayBufferAllocator* allocator) {
|
||||||
params.array_buffer_allocator = allocator;
|
if (allocator != nullptr)
|
||||||
|
params->array_buffer_allocator = allocator;
|
||||||
|
|
||||||
double total_memory = uv_get_total_memory();
|
double total_memory = uv_get_total_memory();
|
||||||
if (total_memory > 0) {
|
if (total_memory > 0) {
|
||||||
// V8 defaults to 700MB or 1.4GB on 32 and 64 bit platforms respectively.
|
// V8 defaults to 700MB or 1.4GB on 32 and 64 bit platforms respectively.
|
||||||
// This default is based on browser use-cases. Tell V8 to configure the
|
// This default is based on browser use-cases. Tell V8 to configure the
|
||||||
// heap based on the actual physical memory.
|
// heap based on the actual physical memory.
|
||||||
params.constraints.ConfigureDefaults(total_memory, 0);
|
params->constraints.ConfigureDefaults(total_memory, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef NODE_ENABLE_VTUNE_PROFILING
|
#ifdef NODE_ENABLE_VTUNE_PROFILING
|
||||||
params.code_event_handler = vTune::GetVtuneCodeEventHandler();
|
params->code_event_handler = vTune::GetVtuneCodeEventHandler();
|
||||||
#endif
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
Isolate* isolate = Isolate::Allocate();
|
void SetIsolateUpForNode(v8::Isolate* isolate) {
|
||||||
if (isolate == nullptr) return nullptr;
|
|
||||||
|
|
||||||
// Register the isolate on the platform before the isolate gets initialized,
|
|
||||||
// so that the isolate can access the platform during initialization.
|
|
||||||
per_process::v8_platform.Platform()->RegisterIsolate(isolate, event_loop);
|
|
||||||
Isolate::Initialize(isolate, params);
|
|
||||||
|
|
||||||
isolate->AddMessageListenerWithErrorLevel(
|
isolate->AddMessageListenerWithErrorLevel(
|
||||||
OnMessage,
|
OnMessage,
|
||||||
Isolate::MessageErrorLevel::kMessageError |
|
Isolate::MessageErrorLevel::kMessageError |
|
||||||
@ -193,7 +188,29 @@ Isolate* NewIsolate(ArrayBufferAllocator* allocator, uv_loop_t* event_loop) {
|
|||||||
isolate->SetMicrotasksPolicy(MicrotasksPolicy::kExplicit);
|
isolate->SetMicrotasksPolicy(MicrotasksPolicy::kExplicit);
|
||||||
isolate->SetFatalErrorHandler(OnFatalError);
|
isolate->SetFatalErrorHandler(OnFatalError);
|
||||||
isolate->SetAllowWasmCodeGenerationCallback(AllowWasmCodeGenerationCallback);
|
isolate->SetAllowWasmCodeGenerationCallback(AllowWasmCodeGenerationCallback);
|
||||||
|
isolate->SetPromiseRejectCallback(task_queue::PromiseRejectCallback);
|
||||||
v8::CpuProfiler::UseDetailedSourcePositionsForProfiling(isolate);
|
v8::CpuProfiler::UseDetailedSourcePositionsForProfiling(isolate);
|
||||||
|
}
|
||||||
|
|
||||||
|
Isolate* NewIsolate(ArrayBufferAllocator* allocator, uv_loop_t* event_loop) {
|
||||||
|
return NewIsolate(allocator, event_loop, GetMainThreadMultiIsolatePlatform());
|
||||||
|
}
|
||||||
|
|
||||||
|
Isolate* NewIsolate(ArrayBufferAllocator* allocator,
|
||||||
|
uv_loop_t* event_loop,
|
||||||
|
MultiIsolatePlatform* platform) {
|
||||||
|
Isolate::CreateParams params;
|
||||||
|
SetIsolateCreateParams(¶ms, allocator);
|
||||||
|
|
||||||
|
Isolate* isolate = Isolate::Allocate();
|
||||||
|
if (isolate == nullptr) return nullptr;
|
||||||
|
|
||||||
|
// Register the isolate on the platform before the isolate gets initialized,
|
||||||
|
// so that the isolate can access the platform during initialization.
|
||||||
|
platform->RegisterIsolate(isolate, event_loop);
|
||||||
|
Isolate::Initialize(isolate, params);
|
||||||
|
|
||||||
|
SetIsolateUpForNode(isolate);
|
||||||
|
|
||||||
return isolate;
|
return isolate;
|
||||||
}
|
}
|
||||||
|
@ -253,10 +253,6 @@ Environment::Environment(IsolateData* isolate_data,
|
|||||||
if (options_->no_force_async_hooks_checks) {
|
if (options_->no_force_async_hooks_checks) {
|
||||||
async_hooks_.no_force_checks();
|
async_hooks_.no_force_checks();
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO(addaleax): the per-isolate state should not be controlled by
|
|
||||||
// a single Environment.
|
|
||||||
isolate()->SetPromiseRejectCallback(task_queue::PromiseRejectCallback);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
CompileFnEntry::CompileFnEntry(Environment* env, uint32_t id)
|
CompileFnEntry::CompileFnEntry(Environment* env, uint32_t id)
|
||||||
|
15
src/node.h
15
src/node.h
@ -270,9 +270,24 @@ class NODE_EXTERN MultiIsolatePlatform : public v8::Platform {
|
|||||||
void* data) = 0;
|
void* data) = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Set up some Node.js-specific defaults for `params`, in particular
|
||||||
|
// the ArrayBuffer::Allocator if it is provided, memory limits, and
|
||||||
|
// possibly a code event handler.
|
||||||
|
NODE_EXTERN void SetIsolateCreateParams(v8::Isolate::CreateParams* params,
|
||||||
|
ArrayBufferAllocator* allocator
|
||||||
|
= nullptr);
|
||||||
|
// Set a number of callbacks for the `isolate`, in particular the Node.js
|
||||||
|
// uncaught exception listener.
|
||||||
|
NODE_EXTERN void SetIsolateUpForNode(v8::Isolate* isolate);
|
||||||
// Creates a new isolate with Node.js-specific settings.
|
// Creates a new isolate with Node.js-specific settings.
|
||||||
|
// This is a convenience method equivalent to using SetIsolateCreateParams(),
|
||||||
|
// Isolate::Allocate(), MultiIsolatePlatform::RegisterIsolate(),
|
||||||
|
// Isolate::Initialize(), and SetIsolateUpForNode().
|
||||||
NODE_EXTERN v8::Isolate* NewIsolate(ArrayBufferAllocator* allocator,
|
NODE_EXTERN v8::Isolate* NewIsolate(ArrayBufferAllocator* allocator,
|
||||||
struct uv_loop_s* event_loop);
|
struct uv_loop_s* event_loop);
|
||||||
|
NODE_EXTERN v8::Isolate* NewIsolate(ArrayBufferAllocator* allocator,
|
||||||
|
struct uv_loop_s* event_loop,
|
||||||
|
MultiIsolatePlatform* platform);
|
||||||
|
|
||||||
// Creates a new context with Node.js-specific tweaks.
|
// Creates a new context with Node.js-specific tweaks.
|
||||||
NODE_EXTERN v8::Local<v8::Context> NewContext(
|
NODE_EXTERN v8::Local<v8::Context> NewContext(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user