Skip to content

Commit 71c0601

Browse files
fix(runtime): enter context in RunMainScript
Runtime::RunMainScript already acquires the isolate lock and scopes the isolate and handle, but it does not explicitly enter the runtime’s v8::Context before running the main module.
1 parent 9c45bf1 commit 71c0601

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

NativeScript/runtime/Runtime.mm

Lines changed: 13 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,6 +434,11 @@ 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

@@ -488,7 +493,8 @@ void DisposeIsolateWhenPossible(Isolate* isolate) {
488493
result = AppPackageJson[nsKey];
489494
}
490495

491-
// Store in cache (can cache nil as NSNull to differentiate presence if desired; for now, cache as-is)
496+
// Store in cache (can cache nil as NSNull to differentiate presence if desired; for now, cache
497+
// as-is)
492498
{
493499
std::lock_guard<std::mutex> lock(AppConfigCacheMutex);
494500
AppConfigCache[key] = result;

0 commit comments

Comments
 (0)