Skip to content

Commit 2620063

Browse files
committed
keep open the context scope
1 parent 1d321b0 commit 2620063

File tree

2 files changed

+30
-13
lines changed

2 files changed

+30
-13
lines changed

src/js_native_api_v8.cc

+24-13
Original file line numberDiff line numberDiff line change
@@ -128,13 +128,26 @@ class EnvironmentInstanceData {
128128
std::unique_ptr<node::CommonEnvironmentSetup>&& setup)
129129
: setup_(std::move(setup)),
130130
locker(setup_->isolate()),
131-
isolate_scope(setup_->isolate()) {}
131+
isolate_scope(setup_->isolate()),
132+
handle_scope(setup_->isolate()),
133+
context_scope(setup_->context()),
134+
seal_scope(nullptr) {}
132135
node::CommonEnvironmentSetup* setup() { return setup_.get(); }
136+
inline void seal() {
137+
seal_scope =
138+
std::make_unique<node::DebugSealHandleScope>(setup_->isolate());
139+
}
133140

134141
private:
135142
std::unique_ptr<node::CommonEnvironmentSetup> setup_;
136143
v8::Locker locker;
137144
v8::Isolate::Scope isolate_scope;
145+
v8::HandleScope handle_scope;
146+
v8::Context::Scope context_scope;
147+
// As this handle scope will remain open for the lifetime
148+
// of the environment, we seal it to prevent it from
149+
// becoming everyone's favorite trash bin
150+
std::unique_ptr<node::DebugSealHandleScope> seal_scope;
138151
};
139152

140153
class HandleScopeWrapper {
@@ -869,32 +882,30 @@ napi_status NAPI_CDECL napi_create_environment(napi_platform platform,
869882
auto wrapper = reinterpret_cast<v8impl::PlatformWrapper*>(platform);
870883
std::vector<std::string> errors_vec;
871884

872-
auto instance_data = new v8impl::EnvironmentInstanceData(
873-
node::CommonEnvironmentSetup::Create(wrapper->platform.get(),
874-
&errors_vec,
875-
wrapper->args,
876-
wrapper->exec_args));
877-
878-
if (instance_data->setup() == nullptr) {
885+
auto setup = node::CommonEnvironmentSetup::Create(
886+
wrapper->platform.get(),
887+
&errors_vec,
888+
wrapper->args,
889+
wrapper->exec_args);
890+
if (setup == nullptr) {
879891
HANDLE_ERRORS_VECTOR(errors, errors_vec);
880892
return napi_generic_failure;
881893
}
882-
883-
v8::HandleScope handle_scope(instance_data->setup()->isolate());
884-
v8::Local<v8::Context> context = instance_data->setup()->context();
885-
v8::Context::Scope context_scope(context);
894+
auto instance_data = new v8impl::EnvironmentInstanceData(std::move(setup));
886895

887896
v8::MaybeLocal<v8::Value> loadenv_ret =
888897
node::LoadEnvironment(instance_data->setup()->env(), main_script);
889898

890899
std::string filename =
891900
wrapper->args.size() > 1 ? wrapper->args[1] : "<internal>";
892-
auto env__ = new node_napi_env__(context, filename);
901+
auto env__ =
902+
new node_napi_env__(instance_data->setup()->context(), filename);
893903
env__->instance_data = reinterpret_cast<void*>(instance_data);
894904
env__->node_env()->AddCleanupHook(
895905
[](void* arg) { static_cast<napi_env>(arg)->Unref(); },
896906
static_cast<void*>(env__));
897907
*result = env__;
908+
instance_data->seal();
898909

899910
if (loadenv_ret.IsEmpty()) return napi_pending_exception;
900911
return napi_ok;

test/embedding/napi_embedding.c

+6
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,12 @@ int callMe(napi_env env) {
8585
return -1;
8686
}
8787

88+
napi_value object;
89+
if (napi_create_object(env, &object) != napi_ok) {
90+
fprintf(stderr, "Failed creating an object\n");
91+
return -1;
92+
}
93+
8894
napi_close_handle_scope(env, scope);
8995
return 0;
9096
}

0 commit comments

Comments
 (0)