Skip to content

Commit c2dab8e

Browse files
codebytereBethGriggs
authored andcommitted
src,test: add public wrapper for Environment::GetCurrent
PR-URL: #23676 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Refael Ackermann <[email protected]> Reviewed-By: Gireesh Punathil <[email protected]> Signed-off-by: Beth Griggs <[email protected]>
1 parent 7cceecf commit c2dab8e

File tree

3 files changed

+11
-0
lines changed

3 files changed

+11
-0
lines changed

src/node.cc

+5
Original file line numberDiff line numberDiff line change
@@ -2801,6 +2801,11 @@ void FreeEnvironment(Environment* env) {
28012801
}
28022802

28032803

2804+
Environment* GetCurrentEnvironment(Local<Context> context) {
2805+
return Environment::GetCurrent(context);
2806+
}
2807+
2808+
28042809
MultiIsolatePlatform* GetMainThreadMultiIsolatePlatform() {
28052810
return v8_platform.Platform();
28062811
}

src/node.h

+3
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,9 @@ NODE_EXTERN Environment* CreateEnvironment(IsolateData* isolate_data,
292292
NODE_EXTERN void LoadEnvironment(Environment* env);
293293
NODE_EXTERN void FreeEnvironment(Environment* env);
294294

295+
// This may return nullptr if context is not associated with a Node instance.
296+
NODE_EXTERN Environment* GetCurrentEnvironment(v8::Local<v8::Context> context);
297+
295298
// This returns the MultiIsolatePlatform used in the main thread of Node.js.
296299
// If NODE_USE_V8_PLATFORM haven't been defined when Node.js was built,
297300
// it returns nullptr.

test/cctest/test_environment.cc

+3
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,16 @@ TEST_F(EnvironmentTest, NonNodeJSContext) {
8080
node::Environment* env = *test_env;
8181
EXPECT_EQ(node::Environment::GetCurrent(isolate_), env);
8282
EXPECT_EQ(node::Environment::GetCurrent(env->context()), env);
83+
EXPECT_EQ(node::GetCurrentEnvironment(env->context()), env);
8384

8485
v8::Local<v8::Context> context = v8::Context::New(isolate_);
8586
EXPECT_EQ(node::Environment::GetCurrent(context), nullptr);
87+
EXPECT_EQ(node::GetCurrentEnvironment(context), nullptr);
8688
EXPECT_EQ(node::Environment::GetCurrent(isolate_), env);
8789

8890
v8::Context::Scope context_scope(context);
8991
EXPECT_EQ(node::Environment::GetCurrent(context), nullptr);
92+
EXPECT_EQ(node::GetCurrentEnvironment(context), nullptr);
9093
EXPECT_EQ(node::Environment::GetCurrent(isolate_), nullptr);
9194
}
9295

0 commit comments

Comments
 (0)