diff --git a/NativeScript/napi/v8/jsr.cpp b/NativeScript/napi/v8/jsr.cpp index 98af6b0b..4cfc3aae 100644 --- a/NativeScript/napi/v8/jsr.cpp +++ b/NativeScript/napi/v8/jsr.cpp @@ -11,6 +11,7 @@ #include "libplatform/libplatform.h" #include "v8-fast-api-calls.h" +#include "v8-module-loader.h" #ifdef __ANDROID__ #include "NativeScriptAssert.h" @@ -262,6 +263,9 @@ napi_status js_free_runtime(jsr_ns_runtime runtime) { // the locker has been destroyed. { v8::Locker locker(jsr->isolate); + // The ES module cache holds thread-local Globals per isolate. Release them + // now; left to thread exit, they would be reset against a disposed isolate. + v8impl::CleanupESModuleSystem(jsr->isolate); while (v8::Isolate::GetCurrent() == jsr->isolate) { jsr->isolate->Exit(); } diff --git a/NativeScript/runtime/apple/Runtime.cpp b/NativeScript/runtime/apple/Runtime.cpp index 66dc7869..ae779a64 100644 --- a/NativeScript/runtime/apple/Runtime.cpp +++ b/NativeScript/runtime/apple/Runtime.cpp @@ -148,8 +148,26 @@ Runtime::~Runtime() { napi_close_handle_scope(env_, globalScope_); globalScope_ = nullptr; } +#ifndef TARGET_ENGINE_V8 + js_free_napi_env(env_); +#endif + } +#ifdef TARGET_ENGINE_V8 + { + // NapiScope enters env_->context(), a Local that aliases the env's own + // persistent slot. Freeing the env destroys that slot, so exiting the + // scope afterwards reads a dead handle and V8 aborts with "Cannot exit + // non-entered context". Enter a HandleScope-owned copy instead, so the + // env's finalizers still run inside the context. + v8::Locker locker(env_->isolate); + v8::Isolate::Scope isolateScope(env_->isolate); + v8::HandleScope handleScope(env_->isolate); + v8::Local context = + v8::Local::New(env_->isolate, env_->context()); + v8::Context::Scope contextScope(context); js_free_napi_env(env_); } +#endif js_free_runtime(runtime_); } else {