Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions NativeScript/runtime/Runtime.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#ifndef Runtime_h
#define Runtime_h

#include <atomic>

#include "Caches.h"
#include "Common.h"
#include "MetadataBuilder.h"
Expand Down Expand Up @@ -59,6 +61,7 @@ class Runtime {
static thread_local Runtime* currentRuntime_;
static std::shared_ptr<v8::Platform> platform_;
static std::vector<v8::Isolate*> isolates_;
static std::atomic<int> nextRuntimeId_;
static SpinMutex isolatesMutex_;
static bool v8Initialized_;
static std::atomic<int> nextIsolateId;
Expand Down
24 changes: 17 additions & 7 deletions NativeScript/runtime/Runtime.mm
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@
#include "DisposerPHV.h"
#include "IsolateWrapper.h"

#include <mutex>
#include <unordered_map>
#include "DevFlags.h"
#include "HMRSupport.h"
#include "ModuleBinding.hpp"
#include "ModuleInternalCallbacks.h"
#include "URLImpl.h"
#include "URLPatternImpl.h"
#include "URLSearchParamsImpl.h"
#include <mutex>
#include "HMRSupport.h"
#include "DevFlags.h"

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

// Global flag to track when JavaScript errors occur during execution
Expand Down Expand Up @@ -261,6 +261,9 @@ void DisposeIsolateWhenPossible(Isolate* isolate) {
v8Initialized_ = true;
}

int runtimeId = Runtime::nextRuntimeId_.fetch_add(1, std::memory_order_relaxed);
this->SetWorkerId(runtimeId);

startTime = platform_->MonotonicallyIncreasingTime();
realtimeOrigin = platform_->CurrentClockTimeMillis();

Expand Down Expand Up @@ -301,8 +304,8 @@ void DisposeIsolateWhenPossible(Isolate* isolate) {
DefineDrainMicrotaskMethod(isolate, globalTemplate);
// queueMicrotask(callback) per spec
{
Local<FunctionTemplate> qmtTemplate = FunctionTemplate::New(
isolate, [](const FunctionCallbackInfo<Value>& info) {
Local<FunctionTemplate> qmtTemplate =
FunctionTemplate::New(isolate, [](const FunctionCallbackInfo<Value>& info) {
auto* isolate = info.GetIsolate();
if (info.Length() < 1 || !info[0]->IsFunction()) {
isolate->ThrowException(Exception::TypeError(
Expand Down Expand Up @@ -434,6 +437,11 @@ void DisposeIsolateWhenPossible(Isolate* isolate) {
v8::Locker locker(isolate);
Isolate::Scope isolate_scope(isolate);
HandleScope handle_scope(isolate);

auto cache = Caches::Get(isolate);
auto context = cache->GetContext();
Context::Scope context_scope(context);

this->moduleInternal_->RunModule(isolate, "./");
}

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

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

} // namespace tns