Skip to content

Commit 968043e

Browse files
feat(runtime): improve initialization with context scope management and runtime IDs (#315)
- Add Context::Scope RAII pattern to RunMainScript for proper context handling - Introduce atomic runtime ID generation for unique runtime identification - Ensures proper context scope management during script execution - Runtime IDs use thread-safe atomic operations for main and worker threads
1 parent 8d2c6c6 commit 968043e

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

NativeScript/runtime/Runtime.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#ifndef Runtime_h
22
#define Runtime_h
33

4+
#include <atomic>
5+
46
#include "Caches.h"
57
#include "Common.h"
68
#include "MetadataBuilder.h"
@@ -59,6 +61,7 @@ class Runtime {
5961
static thread_local Runtime* currentRuntime_;
6062
static std::shared_ptr<v8::Platform> platform_;
6163
static std::vector<v8::Isolate*> isolates_;
64+
static std::atomic<int> nextRuntimeId_;
6265
static SpinMutex isolatesMutex_;
6366
static bool v8Initialized_;
6467
static std::atomic<int> nextIsolateId;

NativeScript/runtime/Runtime.mm

Lines changed: 17 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
@@ -261,6 +261,9 @@ void DisposeIsolateWhenPossible(Isolate* isolate) {
261261
v8Initialized_ = true;
262262
}
263263

264+
int runtimeId = Runtime::nextRuntimeId_.fetch_add(1, std::memory_order_relaxed);
265+
this->SetWorkerId(runtimeId);
266+
264267
startTime = platform_->MonotonicallyIncreasingTime();
265268
realtimeOrigin = platform_->CurrentClockTimeMillis();
266269

@@ -301,8 +304,8 @@ void DisposeIsolateWhenPossible(Isolate* isolate) {
301304
DefineDrainMicrotaskMethod(isolate, globalTemplate);
302305
// queueMicrotask(callback) per spec
303306
{
304-
Local<FunctionTemplate> qmtTemplate = FunctionTemplate::New(
305-
isolate, [](const FunctionCallbackInfo<Value>& info) {
307+
Local<FunctionTemplate> qmtTemplate =
308+
FunctionTemplate::New(isolate, [](const FunctionCallbackInfo<Value>& info) {
306309
auto* isolate = info.GetIsolate();
307310
if (info.Length() < 1 || !info[0]->IsFunction()) {
308311
isolate->ThrowException(Exception::TypeError(
@@ -434,6 +437,11 @@ void DisposeIsolateWhenPossible(Isolate* isolate) {
434437
v8::Locker locker(isolate);
435438
Isolate::Scope isolate_scope(isolate);
436439
HandleScope handle_scope(isolate);
440+
441+
auto cache = Caches::Get(isolate);
442+
auto context = cache->GetContext();
443+
Context::Scope context_scope(context);
444+
437445
this->moduleInternal_->RunModule(isolate, "./");
438446
}
439447

@@ -488,7 +496,8 @@ void DisposeIsolateWhenPossible(Isolate* isolate) {
488496
result = AppPackageJson[nsKey];
489497
}
490498

491-
// Store in cache (can cache nil as NSNull to differentiate presence if desired; for now, cache as-is)
499+
// Store in cache (can cache nil as NSNull to differentiate presence if desired; for now, cache
500+
// as-is)
492501
{
493502
std::lock_guard<std::mutex> lock(AppConfigCacheMutex);
494503
AppConfigCache[key] = result;
@@ -624,6 +633,7 @@ void DisposeIsolateWhenPossible(Isolate* isolate) {
624633
std::vector<Isolate*> Runtime::isolates_;
625634
bool Runtime::v8Initialized_ = false;
626635
thread_local Runtime* Runtime::currentRuntime_ = nullptr;
636+
std::atomic<int> Runtime::nextRuntimeId_ = {0};
627637
SpinMutex Runtime::isolatesMutex_;
628638

629639
} // namespace tns

0 commit comments

Comments
 (0)