Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit e0b438a

Browse filesBrowse files
helloshuangziaddaleax
authored andcommitted
src: add public API to create isolate and context
PR-URL: #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 886116f commit e0b438a
Copy full SHA for e0b438a

File tree

Expand file treeCollapse file tree

3 files changed

+67
-21
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

3 files changed

+67
-21
lines changed
Open diff view settings
Collapse file

‎src/node.cc‎

Copy file name to clipboardExpand all lines: src/node.cc
+45-13Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4453,10 +4453,21 @@ int EmitExit(Environment* env) {
44534453
}
44544454

44554455

4456+
ArrayBufferAllocator* CreateArrayBufferAllocator() {
4457+
return new ArrayBufferAllocator();
4458+
}
4459+
4460+
4461+
void FreeArrayBufferAllocator(ArrayBufferAllocator* allocator) {
4462+
delete allocator;
4463+
}
4464+
4465+
44564466
IsolateData* CreateIsolateData(Isolate* isolate, uv_loop_t* loop) {
44574467
return new IsolateData(isolate, loop, nullptr);
44584468
}
44594469

4470+
44604471
IsolateData* CreateIsolateData(
44614472
Isolate* isolate,
44624473
uv_loop_t* loop,
@@ -4465,6 +4476,15 @@ IsolateData* CreateIsolateData(
44654476
}
44664477

44674478

4479+
IsolateData* CreateIsolateData(
4480+
Isolate* isolate,
4481+
uv_loop_t* loop,
4482+
MultiIsolatePlatform* platform,
4483+
ArrayBufferAllocator* allocator) {
4484+
return new IsolateData(isolate, loop, platform, allocator->zero_fill_field());
4485+
}
4486+
4487+
44684488
void FreeIsolateData(IsolateData* isolate_data) {
44694489
delete isolate_data;
44704490
}
@@ -4608,26 +4628,35 @@ bool AllowWasmCodeGenerationCallback(
46084628
return wasm_code_gen->IsUndefined() || wasm_code_gen->IsTrue();
46094629
}
46104630

4611-
inline int Start(uv_loop_t* event_loop,
4612-
int argc, const char* const* argv,
4613-
int exec_argc, const char* const* exec_argv) {
4631+
Isolate* NewIsolate(ArrayBufferAllocator* allocator) {
46144632
Isolate::CreateParams params;
4615-
ArrayBufferAllocator allocator;
4616-
params.array_buffer_allocator = &allocator;
4633+
params.array_buffer_allocator = allocator;
46174634
#ifdef NODE_ENABLE_VTUNE_PROFILING
46184635
params.code_event_handler = vTune::GetVtuneCodeEventHandler();
46194636
#endif
46204637

4621-
Isolate* const isolate = Isolate::New(params);
4638+
Isolate* isolate = Isolate::New(params);
46224639
if (isolate == nullptr)
4623-
return 12; // Signal internal error.
4640+
return nullptr;
46244641

46254642
isolate->AddMessageListener(OnMessage);
46264643
isolate->SetAbortOnUncaughtExceptionCallback(ShouldAbortOnUncaughtException);
46274644
isolate->SetMicrotasksPolicy(v8::MicrotasksPolicy::kExplicit);
46284645
isolate->SetFatalErrorHandler(OnFatalError);
46294646
isolate->SetAllowWasmCodeGenerationCallback(AllowWasmCodeGenerationCallback);
46304647

4648+
return isolate;
4649+
}
4650+
4651+
inline int Start(uv_loop_t* event_loop,
4652+
int argc, const char* const* argv,
4653+
int exec_argc, const char* const* exec_argv) {
4654+
std::unique_ptr<ArrayBufferAllocator, decltype(&FreeArrayBufferAllocator)>
4655+
allocator(CreateArrayBufferAllocator(), &FreeArrayBufferAllocator);
4656+
Isolate* const isolate = NewIsolate(allocator.get());
4657+
if (isolate == nullptr)
4658+
return 12; // Signal internal error.
4659+
46314660
{
46324661
Mutex::ScopedLock scoped_lock(node_isolate_mutex);
46334662
CHECK_EQ(node_isolate, nullptr);
@@ -4639,15 +4668,18 @@ inline int Start(uv_loop_t* event_loop,
46394668
Locker locker(isolate);
46404669
Isolate::Scope isolate_scope(isolate);
46414670
HandleScope handle_scope(isolate);
4642-
IsolateData isolate_data(
4643-
isolate,
4644-
event_loop,
4645-
v8_platform.Platform(),
4646-
allocator.zero_fill_field());
4671+
std::unique_ptr<IsolateData, decltype(&FreeIsolateData)> isolate_data(
4672+
CreateIsolateData(
4673+
isolate,
4674+
event_loop,
4675+
v8_platform.Platform(),
4676+
allocator.get()),
4677+
&FreeIsolateData);
46474678
if (track_heap_objects) {
46484679
isolate->GetHeapProfiler()->StartTrackingHeapObjects(true);
46494680
}
4650-
exit_code = Start(isolate, &isolate_data, argc, argv, exec_argc, exec_argv);
4681+
exit_code =
4682+
Start(isolate, isolate_data.get(), argc, argv, exec_argc, exec_argv);
46514683
}
46524684

46534685
{
Collapse file

‎src/node.h‎

Copy file name to clipboardExpand all lines: src/node.h
+22Lines 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,
Collapse file

‎src/node_internals.h‎

Copy file name to clipboardExpand all lines: src/node_internals.h
-8Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.