GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — ios/NativeScript/runtime/Runtime.h at main · NativeScript/ios

Expand file tree

/

Runtime.h

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
210 lines (172 loc) · 8.31 KB
/

Runtime.h

Copy path

File metadata and controls

210 lines (172 loc) · 8.31 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#ifndef Runtime_h
#define Runtime_h
#include <atomic>
#include <optional>
#include "Caches.h"
#include "Common.h"
#include "EventLoop.h"
#include "MetadataBuilder.h"
#include "ModuleInternal.h"
#include "SpinLock.h"
#include "libplatform/libplatform.h"
// Declared rather than included: js_native_api_types.h pins NAPI_VERSION for
// the whole translation unit, and Runtime.h reaches nearly all of them.
typedef struct napi_env__* napi_env;
namespace tns {
// Per-isolate caps handed to Isolate::New. Every entry is optional; an absent
// one leaves V8's own default in place. Values are bytes.
struct IsolateLimits {
std::optional<size_t> maxOldGenerationSizeBytes;
std::optional<size_t> maxYoungGenerationSizeBytes;
// Only honored by a V8 build that defines
// V8_HAS_JS_DISPATCH_TABLE_RESERVATION_PARAM; ignored otherwise.
std::optional<size_t> jsDispatchTableReservationBytes;
};
struct RuntimeOptions {
IsolateLimits limits;
// Positive for a worker runtime.
int workerId = -1;
};
class Runtime {
public:
// Creates the runtime's isolate, homed on the calling thread. Init must
// follow on that thread, with the isolate's Locker held; the isolate is not
// published to other threads until then.
explicit Runtime(const RuntimeOptions& options = {});
~Runtime();
void Init();
void RunMainScript();
v8::Isolate* GetIsolate();
const int WorkerId();
inline bool IsRuntimeWorker() { return workerId_ > 0; }
inline CFRunLoopRef RuntimeLoop() { return runtimeLoop_; }
/*
* Scheduler bound to this runtime's runloop. Producers on any thread post;
* entries run only on the runtime's home thread.
*/
inline std::shared_ptr<EventLoop> GetEventLoop() const { return eventLoop_; }
// Runs `moduleName` as an entry module; throws NativeScriptException on
// failure (see ModuleInternal::RunModule).
void RunModule(const std::string moduleName);
void RunScript(const std::string script);
// This isolate's CommonJS loader, or null before Init() has built it.
ModuleInternal* GetModuleInternal() const { return moduleInternal_.get(); }
// Polls the main entry's evaluation promise, entering the isolate itself.
// kNone for a classic-script entry (it settled synchronously and needs no
// boot backstop). Used by the boot handoff to decide whether the process may
// fall off main() yet.
EntryEvaluationState PollMainEntryEvaluation(std::string* rejectionReason);
// Whether V8 has been asked to terminate execution on this isolate, entering
// it the same way PollMainEntryEvaluation does. The boot pump asks so it can
// stop waiting on work that can no longer make progress.
bool IsExecutionTerminating();
// Whether termination has been *requested* for this runtime's isolate.
//
// v8::Isolate::IsExecutionTerminating only reports a termination that has
// already been materialized, which takes running JS to happen: a graph parked
// on a promise nothing will settle queues no microtasks and no tasks, so a
// pump waiting on it would never observe the terminate and would spend its
// entire deadline first. This flag is set on the requesting thread, before
// TerminateExecution, so the pumps can bail on the request itself.
//
// One-way on purpose: a worker never un-terminates, so there is no reset and
// no window in which a stale `true` could strand live work.
void RequestTermination() {
terminationRequested_.store(true, std::memory_order_relaxed);
}
bool IsTerminationRequested() const {
return terminationRequested_.load(std::memory_order_relaxed);
}
// Drops the main entry's module from the registry, entering the isolate the
// same way PollMainEntryEvaluation does. Boot calls it before reporting a
// fatal so a half-evaluated entry cannot be handed to a later import.
void EvictMainEntry();
static void Initialize();
static Runtime* GetCurrentRuntime() { return currentRuntime_; }
static Runtime* GetRuntime(v8::Isolate* isolate);
// The Node-API environment for this runtime's context. Null before Init
// creates it and after ~Runtime destroys it.
inline napi_env GetNapiEnv() { return napiEnv_; }
static bool IsWorker() {
if (currentRuntime_ == nullptr) {
return false;
}
return currentRuntime_->IsRuntimeWorker();
}
static v8::Platform* GetPlatform() { return platform_; }
static id GetAppConfigValue(std::string key);
// Convenience accessor for whether to show the JS error display UI.
// Reads the boolean `showErrorDisplay` from the nativescript.config (aka,
// bundled package.json). Defaults to false when not present.
static bool showErrorDisplay();
static bool IsAlive(const v8::Isolate* isolate);
// Forwards a system memory pressure level to every live isolate. Safe from
// any thread.
static void NotifyMemoryPressure(v8::MemoryPressureLevel level);
// Resolves the env while holding the registry lock, so a possibly-stale
// pointer (e.g. a thread-local left behind when a Runtime was destroyed on
// another thread) is never dereferenced outside it. The returned env's
// validity is governed by the Node-API threading contract: it is only safe
// to use on the runtime's own thread, where teardown cannot race it.
static napi_env GetNapiEnvIfAlive(const Runtime* runtime);
// Milliseconds since this runtime's time origin, on the monotonic clock.
double PerformanceNowMillis();
// Wall-clock milliseconds since the Unix epoch at the moment the time origin
// was captured, on the same base as Date.now(); this is
// performance.timeOrigin.
inline double TimeOriginMillis() const { return timeOriginRealtimeMs_; }
// The monotonic clock reading (seconds, V8 platform units) of the time
// origin, for mapping platform-supplied timestamps onto the performance
// timeline.
inline double TimeOriginMonotonicSeconds() const {
return timeOriginMonotonic_;
}
private:
static thread_local Runtime* currentRuntime_;
// Lives until the process dies and is never deleted: V8 holds it by raw
// pointer for every isolate, and exit() runs static destructors while the
// main thread and worker threads are still tearing isolates down.
static v8::Platform* platform_;
// Isolates whose Caches and RUNTIME slots are written. Init publishes an
// isolate only after writing them, and the teardown removes it before
// clearing them, so a thread that finds an isolate here may read its slots
// while holding this lock.
static std::vector<v8::Isolate*> isolates_;
static SpinMutex isolatesMutex_;
static bool v8Initialized_;
static std::atomic<int> nextIsolateId;
void DefineGlobalObject(v8::Local<v8::Context> context, bool isWorker);
void DefineCollectFunction(v8::Local<v8::Context> context);
void DefineNativeScriptVersion(v8::Isolate* isolate,
v8::Local<v8::ObjectTemplate> globalTemplate);
void DefineTimeMethod(v8::Isolate* isolate,
v8::Local<v8::ObjectTemplate> globalTemplate);
void DefineDrainMicrotaskMethod(v8::Isolate* isolate,
v8::Local<v8::ObjectTemplate> globalTemplate);
void DefineQueueMacrotaskMethod(v8::Isolate* isolate,
v8::Local<v8::ObjectTemplate> globalTemplate);
void DefineDateTimeConfigurationChangeNotificationMethod(
v8::Isolate* isolate, v8::Local<v8::ObjectTemplate> globalTemplate);
static void DrainRejectionsObserver(CFRunLoopObserverRef observer,
CFRunLoopActivity activity, void* info);
v8::Isolate* isolate_ = nullptr;
bool initialized_ = false;
std::atomic<bool> terminationRequested_{false};
napi_env napiEnv_ = nullptr;
std::unique_ptr<ModuleInternal> moduleInternal_;
int workerId_;
CFRunLoopRef runtimeLoop_;
std::shared_ptr<EventLoop> eventLoop_;
// Drains unhandled promise rejections once per runloop turn
// (kCFRunLoopBeforeWaiting). Torn down before isolate disposal in ~Runtime.
CFRunLoopObserverRef rejectionObserver_ = nullptr;
double timeOriginMonotonic_;
double timeOriginRealtimeMs_;
// Keeps Caches::Workers alive for as long as this runtime exists: ~Runtime
// reads it, and a worker runtime can be deleted on its own thread while
// exit() is already running static destructors on another.
std::shared_ptr<ConcurrentMap<int, std::shared_ptr<Caches::WorkerState>>>
workerCache_;
};
} // namespace tns
#endif /* Runtime_h */
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.