From d313bf7d471cac082d2122b8aaca6a1cb2b3baab Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Tue, 8 Dec 2020 11:59:03 +0100 Subject: [PATCH] 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 Reviewed-By: Richard Lau Reviewed-By: Gireesh Punathil Reviewed-By: Colin Ihrig Reviewed-By: James M Snell Reviewed-By: Rich Trott --- src/api/environment.cc | 8 ++++++++ src/node.h | 2 ++ test/cctest/test_environment.cc | 3 +++ 3 files changed, 13 insertions(+) diff --git a/src/api/environment.cc b/src/api/environment.cc index 648f0184c35c76..46e3360ef9c023 100644 --- a/src/api/environment.cc +++ b/src/api/environment.cc @@ -438,6 +438,14 @@ Environment* GetCurrentEnvironment(Local 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()); } diff --git a/src/node.h b/src/node.h index c9487bfa79b692..5313f49e0bc2c0 100644 --- a/src/node.h +++ b/src/node.h @@ -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 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); diff --git a/test/cctest/test_environment.cc b/test/cctest/test_environment.cc index dcd59a22d3b213..baa03a5899aa25 100644 --- a/test/cctest/test_environment.cc +++ b/test/cctest/test_environment.cc @@ -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 main_ret = node::LoadEnvironment(environment.get(),