Skip to content

Commit 6156b29

Browse files
fix(runtime): entered context in Runtime entry points
Added Context::Scope to Runtime::RunMainScript, Runtime::RunModule, and Runtime::RunScript to ensure the runtime's context is explicitly entered before delegating to ModuleInternal. While the context is already entered once in Runtime::Init and remains current for the runtime's lifetime, this change adds a defensive context scope at each entry point for consistency and future-proofing.
1 parent 9c45bf1 commit 6156b29

File tree

1 file changed

+23
-7
lines changed

1 file changed

+23
-7
lines changed

NativeScript/runtime/Runtime.mm

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@
2222
#include "DisposerPHV.h"
2323
#include "IsolateWrapper.h"
2424

25+
#include <mutex>
2526
#include <unordered_map>
27+
#include "DevFlags.h"
28+
#include "HMRSupport.h"
2629
#include "ModuleBinding.hpp"
2730
#include "ModuleInternalCallbacks.h"
2831
#include "URLImpl.h"
2932
#include "URLPatternImpl.h"
3033
#include "URLSearchParamsImpl.h"
31-
#include <mutex>
32-
#include "HMRSupport.h"
33-
#include "DevFlags.h"
3434

3535
#define STRINGIZE(x) #x
3636
#define STRINGIZE_VALUE_OF(x) STRINGIZE(x)
@@ -128,7 +128,7 @@ static void InitializeImportMetaObject(Local<Context> context, Local<Module> mod
128128
std::atomic<int> Runtime::nextIsolateId{0};
129129
SimpleAllocator allocator_;
130130
NSDictionary* AppPackageJson = nil;
131-
static std::unordered_map<std::string, id> AppConfigCache; // generic cache for app config values
131+
static std::unordered_map<std::string, id> AppConfigCache; // generic cache for app config values
132132
static std::mutex AppConfigCacheMutex;
133133

134134
// Global flag to track when JavaScript errors occur during execution
@@ -301,8 +301,8 @@ void DisposeIsolateWhenPossible(Isolate* isolate) {
301301
DefineDrainMicrotaskMethod(isolate, globalTemplate);
302302
// queueMicrotask(callback) per spec
303303
{
304-
Local<FunctionTemplate> qmtTemplate = FunctionTemplate::New(
305-
isolate, [](const FunctionCallbackInfo<Value>& info) {
304+
Local<FunctionTemplate> qmtTemplate =
305+
FunctionTemplate::New(isolate, [](const FunctionCallbackInfo<Value>& info) {
306306
auto* isolate = info.GetIsolate();
307307
if (info.Length() < 1 || !info[0]->IsFunction()) {
308308
isolate->ThrowException(Exception::TypeError(
@@ -434,13 +434,23 @@ void DisposeIsolateWhenPossible(Isolate* isolate) {
434434
v8::Locker locker(isolate);
435435
Isolate::Scope isolate_scope(isolate);
436436
HandleScope handle_scope(isolate);
437+
438+
auto cache = Caches::Get(isolate);
439+
auto context = cache->GetContext();
440+
Context::Scope context_scope(context);
441+
437442
this->moduleInternal_->RunModule(isolate, "./");
438443
}
439444

440445
void Runtime::RunModule(const std::string moduleName) {
441446
Isolate* isolate = this->GetIsolate();
442447
Isolate::Scope isolate_scope(isolate);
443448
HandleScope handle_scope(isolate);
449+
450+
auto cache = Caches::Get(isolate);
451+
auto context = cache->GetContext();
452+
Context::Scope context_scope(context);
453+
444454
this->moduleInternal_->RunModule(isolate, moduleName);
445455
}
446456

@@ -449,6 +459,11 @@ void DisposeIsolateWhenPossible(Isolate* isolate) {
449459
v8::Locker locker(isolate);
450460
Isolate::Scope isolate_scope(isolate);
451461
HandleScope handle_scope(isolate);
462+
463+
auto cache = Caches::Get(isolate);
464+
auto context = cache->GetContext();
465+
Context::Scope context_scope(context);
466+
452467
this->moduleInternal_->RunScript(isolate, script);
453468
}
454469

@@ -488,7 +503,8 @@ void DisposeIsolateWhenPossible(Isolate* isolate) {
488503
result = AppPackageJson[nsKey];
489504
}
490505

491-
// Store in cache (can cache nil as NSNull to differentiate presence if desired; for now, cache as-is)
506+
// Store in cache (can cache nil as NSNull to differentiate presence if desired; for now, cache
507+
// as-is)
492508
{
493509
std::lock_guard<std::mutex> lock(AppConfigCacheMutex);
494510
AppConfigCache[key] = result;

0 commit comments

Comments
 (0)