Skip to content

Commit

Permalink
keep open the context scope
Browse files Browse the repository at this point in the history
  • Loading branch information
mmomtchev committed Jul 5, 2022
1 parent 1d321b0 commit 8f64b81
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 13 deletions.
33 changes: 20 additions & 13 deletions src/js_native_api_v8.cc
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,26 @@ class EnvironmentInstanceData {
std::unique_ptr<node::CommonEnvironmentSetup>&& setup)
: setup_(std::move(setup)),
locker(setup_->isolate()),
isolate_scope(setup_->isolate()) {}
isolate_scope(setup_->isolate()),
handle_scope(setup_->isolate()),
context_scope(setup_->context()),
seal_scope(nullptr) {}
node::CommonEnvironmentSetup* setup() { return setup_.get(); }
inline void seal() {
seal_scope =
std::make_unique<node::DebugSealHandleScope>(setup_->isolate());
}

private:
std::unique_ptr<node::CommonEnvironmentSetup> setup_;
v8::Locker locker;
v8::Isolate::Scope isolate_scope;
v8::HandleScope handle_scope;
v8::Context::Scope context_scope;
// As this handle scope will remain open for the lifetime
// of the environment, we seal it to prevent it from
// becoming everyone's favorite trash bin
std::unique_ptr<node::DebugSealHandleScope> seal_scope;
};

class HandleScopeWrapper {
Expand Down Expand Up @@ -869,32 +882,26 @@ napi_status NAPI_CDECL napi_create_environment(napi_platform platform,
auto wrapper = reinterpret_cast<v8impl::PlatformWrapper*>(platform);
std::vector<std::string> errors_vec;

auto instance_data = new v8impl::EnvironmentInstanceData(
node::CommonEnvironmentSetup::Create(wrapper->platform.get(),
&errors_vec,
wrapper->args,
wrapper->exec_args));

if (instance_data->setup() == nullptr) {
auto setup = node::CommonEnvironmentSetup::Create(
wrapper->platform.get(), &errors_vec, wrapper->args, wrapper->exec_args);
if (setup == nullptr) {
HANDLE_ERRORS_VECTOR(errors, errors_vec);
return napi_generic_failure;
}

v8::HandleScope handle_scope(instance_data->setup()->isolate());
v8::Local<v8::Context> context = instance_data->setup()->context();
v8::Context::Scope context_scope(context);
auto instance_data = new v8impl::EnvironmentInstanceData(std::move(setup));

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

std::string filename =
wrapper->args.size() > 1 ? wrapper->args[1] : "<internal>";
auto env__ = new node_napi_env__(context, filename);
auto env__ = new node_napi_env__(instance_data->setup()->context(), filename);
env__->instance_data = reinterpret_cast<void*>(instance_data);
env__->node_env()->AddCleanupHook(
[](void* arg) { static_cast<napi_env>(arg)->Unref(); },
static_cast<void*>(env__));
*result = env__;
instance_data->seal();

if (loadenv_ret.IsEmpty()) return napi_pending_exception;
return napi_ok;
Expand Down
6 changes: 6 additions & 0 deletions test/embedding/napi_embedding.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ int callMe(napi_env env) {
return -1;
}

napi_value object;
if (napi_create_object(env, &object) != napi_ok) {
fprintf(stderr, "Failed creating an object\n");
return -1;
}

napi_close_handle_scope(env, scope);
return 0;
}
Expand Down

0 comments on commit 8f64b81

Please sign in to comment.