From 4286dcf17f062034117043a2640b620210b61f57 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Wed, 12 Sep 2018 15:01:50 +0200 Subject: src: refactor `Environment::GetCurrent()` usage Make `Environment::GetCurrent()` return `nullptr` if the current `Context` is not a Node.js context, and for the relevant usage of this function, either: - Switch to the better `GetCurrent(args)` variant - Turn functions in to no-ops where it makes sense - Make it a `CHECK`, i.e. an API requirement, where it make sense - Leave a `TODO` comment for verifying what, if anything, is to be done PR-URL: https://github.com/nodejs/node/pull/22819 Reviewed-By: Gus Caplan Reviewed-By: Gireesh Punathil Reviewed-By: Eugene Ostroukhov Reviewed-By: Refael Ackermann --- test/cctest/test_environment.cc | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'test/cctest') diff --git a/test/cctest/test_environment.cc b/test/cctest/test_environment.cc index 07170ac267..fa0f94a5ad 100644 --- a/test/cctest/test_environment.cc +++ b/test/cctest/test_environment.cc @@ -70,6 +70,26 @@ TEST_F(EnvironmentTest, MultipleEnvironmentsPerIsolate) { EXPECT_TRUE(called_cb_2); } +TEST_F(EnvironmentTest, NonNodeJSContext) { + const v8::HandleScope handle_scope(isolate_); + const Argv argv; + Env test_env {handle_scope, argv}; + + EXPECT_EQ(node::Environment::GetCurrent(v8::Local()), nullptr); + + node::Environment* env = *test_env; + EXPECT_EQ(node::Environment::GetCurrent(isolate_), env); + EXPECT_EQ(node::Environment::GetCurrent(env->context()), env); + + v8::Local context = v8::Context::New(isolate_); + EXPECT_EQ(node::Environment::GetCurrent(context), nullptr); + EXPECT_EQ(node::Environment::GetCurrent(isolate_), env); + + v8::Context::Scope context_scope(context); + EXPECT_EQ(node::Environment::GetCurrent(context), nullptr); + EXPECT_EQ(node::Environment::GetCurrent(isolate_), nullptr); +} + static void at_exit_callback1(void* arg) { called_cb_1 = true; if (arg) { -- cgit v1.2.3