Skip to content

Commit 963cb3a

Browse files
helloshuangziaddaleax
authored andcommitted
src: add public API to create isolate and context
PR-URL: nodejs#20639 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com> Reviewed-By: Khaidi Chu <i@2333.moe> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
1 parent 617f0d2 commit 963cb3a

3 files changed

Lines changed: 67 additions & 21 deletions

File tree

src/node.cc

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4439,10 +4439,21 @@ int EmitExit(Environment* env) {
44394439
}
44404440

44414441

4442+
ArrayBufferAllocator* CreateArrayBufferAllocator() {
4443+
return new ArrayBufferAllocator();
4444+
}
4445+
4446+
4447+
void FreeArrayBufferAllocator(ArrayBufferAllocator* allocator) {
4448+
delete allocator;
4449+
}
4450+
4451+
44424452
IsolateData* CreateIsolateData(Isolate* isolate, uv_loop_t* loop) {
44434453
return new IsolateData(isolate, loop, nullptr);
44444454
}
44454455

4456+
44464457
IsolateData* CreateIsolateData(
44474458
Isolate* isolate,
44484459
uv_loop_t* loop,
@@ -4451,6 +4462,15 @@ IsolateData* CreateIsolateData(
44514462
}
44524463

44534464

4465+
IsolateData* CreateIsolateData(
4466+
Isolate* isolate,
4467+
uv_loop_t* loop,
4468+
MultiIsolatePlatform* platform,
4469+
ArrayBufferAllocator* allocator) {
4470+
return new IsolateData(isolate, loop, platform, allocator->zero_fill_field());
4471+
}
4472+
4473+
44544474
void FreeIsolateData(IsolateData* isolate_data) {
44554475
delete isolate_data;
44564476
}
@@ -4594,26 +4614,35 @@ bool AllowWasmCodeGenerationCallback(
45944614
return wasm_code_gen->IsUndefined() || wasm_code_gen->IsTrue();
45954615
}
45964616

4597-
inline int Start(uv_loop_t* event_loop,
4598-
int argc, const char* const* argv,
4599-
int exec_argc, const char* const* exec_argv) {
4617+
Isolate* NewIsolate(ArrayBufferAllocator* allocator) {
46004618
Isolate::CreateParams params;
4601-
ArrayBufferAllocator allocator;
4602-
params.array_buffer_allocator = &allocator;
4619+
params.array_buffer_allocator = allocator;
46034620
#ifdef NODE_ENABLE_VTUNE_PROFILING
46044621
params.code_event_handler = vTune::GetVtuneCodeEventHandler();
46054622
#endif
46064623

4607-
Isolate* const isolate = Isolate::New(params);
4624+
Isolate* isolate = Isolate::New(params);
46084625
if (isolate == nullptr)
4609-
return 12; // Signal internal error.
4626+
return nullptr;
46104627

46114628
isolate->AddMessageListener(OnMessage);
46124629
isolate->SetAbortOnUncaughtExceptionCallback(ShouldAbortOnUncaughtException);
46134630
isolate->SetMicrotasksPolicy(v8::MicrotasksPolicy::kExplicit);
46144631
isolate->SetFatalErrorHandler(OnFatalError);
46154632
isolate->SetAllowWasmCodeGenerationCallback(AllowWasmCodeGenerationCallback);
46164633

4634+
return isolate;
4635+
}
4636+
4637+
inline int Start(uv_loop_t* event_loop,
4638+
int argc, const char* const* argv,
4639+
int exec_argc, const char* const* exec_argv) {
4640+
std::unique_ptr<ArrayBufferAllocator, decltype(&FreeArrayBufferAllocator)>
4641+
allocator(CreateArrayBufferAllocator(), &FreeArrayBufferAllocator);
4642+
Isolate* const isolate = NewIsolate(allocator.get());
4643+
if (isolate == nullptr)
4644+
return 12; // Signal internal error.
4645+
46174646
{
46184647
Mutex::ScopedLock scoped_lock(node_isolate_mutex);
46194648
CHECK_EQ(node_isolate, nullptr);
@@ -4625,15 +4654,18 @@ inline int Start(uv_loop_t* event_loop,
46254654
Locker locker(isolate);
46264655
Isolate::Scope isolate_scope(isolate);
46274656
HandleScope handle_scope(isolate);
4628-
IsolateData isolate_data(
4629-
isolate,
4630-
event_loop,
4631-
v8_platform.Platform(),
4632-
allocator.zero_fill_field());
4657+
std::unique_ptr<IsolateData, decltype(&FreeIsolateData)> isolate_data(
4658+
CreateIsolateData(
4659+
isolate,
4660+
event_loop,
4661+
v8_platform.Platform(),
4662+
allocator.get()),
4663+
&FreeIsolateData);
46334664
if (track_heap_objects) {
46344665
isolate->GetHeapProfiler()->StartTrackingHeapObjects(true);
46354666
}
4636-
exit_code = Start(isolate, &isolate_data, argc, argv, exec_argc, exec_argv);
4667+
exit_code =
4668+
Start(isolate, isolate_data.get(), argc, argv, exec_argc, exec_argv);
46374669
}
46384670

46394671
{

src/node.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,11 @@ NODE_EXTERN void Init(int* argc,
214214
int* exec_argc,
215215
const char*** exec_argv);
216216

217+
class ArrayBufferAllocator;
218+
219+
NODE_EXTERN ArrayBufferAllocator* CreateArrayBufferAllocator();
220+
NODE_EXTERN void FreeArrayBufferAllocator(ArrayBufferAllocator* allocator);
221+
217222
class IsolateData;
218223
class Environment;
219224

@@ -229,16 +234,33 @@ class MultiIsolatePlatform : public v8::Platform {
229234
virtual void UnregisterIsolate(IsolateData* isolate_data) = 0;
230235
};
231236

237+
// Creates a new isolate with Node.js-specific settings.
238+
NODE_EXTERN v8::Isolate* NewIsolate(ArrayBufferAllocator* allocator);
239+
240+
// Creates a new context with Node.js-specific tweaks. Currently, it removes
241+
// the `v8BreakIterator` property from the global `Intl` object if present.
242+
// See https://github.com/nodejs/node/issues/14909 for more info.
243+
NODE_EXTERN v8::Local<v8::Context> NewContext(
244+
v8::Isolate* isolate,
245+
v8::Local<v8::ObjectTemplate> object_template =
246+
v8::Local<v8::ObjectTemplate>());
247+
232248
// If `platform` is passed, it will be used to register new Worker instances.
233249
// It can be `nullptr`, in which case creating new Workers inside of
234250
// Environments that use this `IsolateData` will not work.
251+
// TODO(helloshuangzi): switch to default parameters.
235252
NODE_EXTERN IsolateData* CreateIsolateData(
236253
v8::Isolate* isolate,
237254
struct uv_loop_s* loop);
238255
NODE_EXTERN IsolateData* CreateIsolateData(
239256
v8::Isolate* isolate,
240257
struct uv_loop_s* loop,
241258
MultiIsolatePlatform* platform);
259+
NODE_EXTERN IsolateData* CreateIsolateData(
260+
v8::Isolate* isolate,
261+
struct uv_loop_s* loop,
262+
MultiIsolatePlatform* platform,
263+
ArrayBufferAllocator* allocator);
242264
NODE_EXTERN void FreeIsolateData(IsolateData* isolate_data);
243265

244266
NODE_EXTERN Environment* CreateEnvironment(IsolateData* isolate_data,

src/node_internals.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -232,14 +232,6 @@ inline v8::Local<TypeName> PersistentToLocal(
232232
v8::Isolate* isolate,
233233
const Persistent<TypeName>& persistent);
234234

235-
// Creates a new context with Node.js-specific tweaks. Currently, it removes
236-
// the `v8BreakIterator` property from the global `Intl` object if present.
237-
// See https://github.com/nodejs/node/issues/14909 for more info.
238-
v8::Local<v8::Context> NewContext(
239-
v8::Isolate* isolate,
240-
v8::Local<v8::ObjectTemplate> object_template =
241-
v8::Local<v8::ObjectTemplate>());
242-
243235
// Convert a struct sockaddr to a { address: '1.2.3.4', port: 1234 } JS object.
244236
// Sets address and port properties on the info object and returns it.
245237
// If |info| is omitted, a new object is returned.

0 commit comments

Comments
 (0)