src: add way to get IsolateData and allocator from Environment

Add a way to get the current `IsolateData*` and, from it, the current
Node.js `ArrayBufferAllocator` if there is one. This can be useful
for re-using either one of these structures as an embedder.

PR-URL: https://github.com/nodejs/node/pull/36441
Reviewed-By: Daniel Bevenius <daniel.bevenius@gmail.com>
Reviewed-By: Richard Lau <rlau@redhat.com>
Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
pull/36509/head
Anna Henningsen 2020-12-08 11:59:03 +01:00 committed by Node.js GitHub Bot
parent 7069d22343
commit d313bf7d47
3 changed files with 13 additions and 0 deletions

View File

@ -438,6 +438,14 @@ Environment* GetCurrentEnvironment(Local<Context> context) {
return Environment::GetCurrent(context);
}
IsolateData* GetEnvironmentIsolateData(Environment* env) {
return env->isolate_data();
}
ArrayBufferAllocator* GetArrayBufferAllocator(IsolateData* isolate_data) {
return isolate_data->node_allocator();
}
MultiIsolatePlatform* GetMultiIsolatePlatform(Environment* env) {
return GetMultiIsolatePlatform(env->isolate_data());
}

View File

@ -464,6 +464,8 @@ NODE_EXTERN void DefaultProcessExitHandler(Environment* env, int exit_code);
// This may return nullptr if context is not associated with a Node instance.
NODE_EXTERN Environment* GetCurrentEnvironment(v8::Local<v8::Context> context);
NODE_EXTERN IsolateData* GetEnvironmentIsolateData(Environment* env);
NODE_EXTERN ArrayBufferAllocator* GetArrayBufferAllocator(IsolateData* data);
NODE_EXTERN void OnFatalError(const char* location, const char* message);
NODE_EXTERN void PromiseRejectCallback(v8::PromiseRejectMessage message);

View File

@ -586,6 +586,9 @@ TEST_F(NodeZeroIsolateTestFixture, CtrlCWithOnlySafeTerminationTest) {
{}),
node::FreeEnvironment};
CHECK(environment);
EXPECT_EQ(node::GetEnvironmentIsolateData(environment.get()),
isolate_data.get());
EXPECT_EQ(node::GetArrayBufferAllocator(isolate_data.get()), nullptr);
v8::Local<v8::Value> main_ret =
node::LoadEnvironment(environment.get(),