Repository navigation
Expand file tree
/
Copy pathRuntime.h
More file actions
395 lines (326 loc) · 15 KB
/
Copy pathRuntime.h
File metadata and controls
395 lines (326 loc) · 15 KB
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
#ifndef RUNTIME_H_
#define RUNTIME_H_
#include "v8.h"
#include "JniLocalRef.h"
#include "ObjectManager.h"
#include "SimpleAllocator.h"
#include "WeakRef.h"
#include "ArrayBufferHelper.h"
#include "Profiler.h"
#include "ModuleInternal.h"
#include "File.h"
#include "Timers.h"
#include "EventLoop.h"
#include <atomic>
#include <condition_variable>
#include <memory>
#include <mutex>
#include <android/looper.h>
#include <fcntl.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 {
class PromiseRejectionTracker;
class RuntimeState;
class Runtime {
public:
enum IsolateData {
RUNTIME = 0,
CONSTANTS = 1,
WORKER_WRAPPER = 2
};
/*
* What happens to an unprevented uncaught JS error (sync throw in a
* native-initiated callback, or an unhandled promise rejection):
* Report (default) - report and continue, never crash; Throw - hand
* it to the native layer as a real Java exception (pre-9.1 behavior).
* Configured via `uncaughtErrorPolicy` in the app's package.json.
*/
enum class UncaughtErrorPolicy {
Report,
Throw
};
~Runtime();
static Runtime* GetRuntime(int runtimeId);
static Runtime* GetRuntime(v8::Isolate* isolate);
static Runtime* GetRuntimeFromIsolateData(v8::Isolate* isolate);
/*
* The runtime for this isolate, or null. Unlike the accessors above it
* neither throws nor locks -- it only reads the isolate's own data
* slot -- which is what makes it usable from the places that must not
* throw: GC weak callbacks, teardown paths, and the error handlers
* that run while a runtime is going away.
*/
static Runtime* TryGetRuntime(v8::Isolate* isolate) {
return static_cast<Runtime*>(
isolate->GetData((uint32_t)IsolateData::RUNTIME));
}
/*
* The runtime whose home thread is the calling thread, or null. Set at
* the end of PrepareV8Runtime; may be stale after a Runtime destroyed
* on another thread, so consumers must validate through
* GetNapiEnvIfAlive rather than dereference it.
*/
static Runtime* GetCurrentRuntime() {
return s_currentRuntime;
}
/*
* The Node-API environment for this runtime's context. Null before
* PrepareV8Runtime creates it and after DestroyRuntime destroys it.
*/
napi_env GetNapiEnv() const {
return m_napiEnv;
}
/*
* Resolves the env while holding the registry lock, so a possibly-
* stale pointer (e.g. the 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);
static ObjectManager* GetObjectManager(v8::Isolate* isolate);
static void Init(JavaVM* vm, void* reserved);
static void Init(JNIEnv* _env, jobject obj, int runtimeId, jstring filesPath, jstring nativeLibsDir, jboolean verboseLoggingEnabled, jboolean isDebuggable, jstring packageName, jobjectArray args, jstring callingDir, int maxLogcatObjectSize,
bool forceLog);
static void SetManualInstrumentationMode(jstring mode);
void Init(JNIEnv* env, jstring filesPath, jstring nativeLibsDir, bool verboseLoggingEnabled, bool isDebuggable, jstring packageName, jobjectArray args, jstring callingDir, int maxLogcatObjectSize, bool forceLog);
v8::Isolate* GetIsolate() const;
/*
* Per-runtime storage for subsystem state bound to this isolate; see
* RuntimeState.h. Released in DestroyRuntime while the isolate is
* still alive, so state holding v8::Persistents can be torn down.
*/
RuntimeState* GetState() const {
return m_state.get();
}
bool IsMainThread() const {
return m_isMainThread;
}
jobject GetJavaRuntime() const;
ObjectManager* GetObjectManager() const;
void RunModule(JNIEnv* _env, jobject obj, jstring scriptFile);
void RunModule(const char *moduleName);
void RunWorker(const std::string& filePath);
jobject RunScript(JNIEnv* _env, jobject obj, jstring scriptFile);
jobject CallJSMethodNative(JNIEnv* _env, jobject obj, jint javaObjectID, jstring methodName, jint retType, jboolean isConstructor, jobjectArray packagedArgs);
void CreateJSInstanceNative(JNIEnv* _env, jobject obj, jobject javaObject, jint javaObjectID, jstring className);
jint GenerateNewObjectId(JNIEnv* env, jobject obj);
void AdjustAmountOfExternalAllocatedMemory();
bool NotifyGC(JNIEnv* env, jobject obj);
bool TryCallGC();
/*
* Reports a Java-side exception to JS: dispatches the WHATWG `error`
* event first and, unless a listener called preventDefault(), calls
* the __onUncaughtError/__onDiscardedError shim. Returns JNI_TRUE
* when a listener prevented the default, so the Java caller can treat
* the exception as fully handled (e.g. skip crashing the process).
*/
jboolean PassExceptionToJsNative(JNIEnv* env, jobject obj, jthrowable exception, jstring message, jstring fullStackTrace, jstring jsStackTrace, jboolean isDiscarded);
void DestroyRuntime();
int GetId();
v8::Local<v8::Context> GetContext();
static v8::Platform* platform;
std::string ReadFileText(const std::string& filePath);
/*
* `ok` distinguishes an unreadable file from an empty one — callers that
* compile what they read must not treat the first as valid empty source.
*/
std::string ReadFileText(const std::string& filePath, bool& ok);
/*
* The main runtime's event loop, set once when the main runtime
* initializes. __runOnMainThread posts its (own-isolate) closures
* here from any runtime's thread.
*/
static std::shared_ptr<EventLoop> GetMainEventLoop() {
return s_mainEventLoop;
}
static JavaVM* GetJVM() {
return s_jvm;
}
/*
* Scheduler bound to this runtime's looper. Child workers hold a
* weak_ptr to their parent runtime's loop for worker -> parent
* delivery (messages, errors, cleanup notifications).
*/
std::shared_ptr<EventLoop> GetEventLoop() const {
return m_eventLoop;
}
/*
* This runtime's CommonJS loader. `ns:module`'s createRequire mints its
* requires through it, so the require it hands out is the very one the
* loader builds for every module.
*/
ModuleInternal* GetModuleInternal() {
return &m_module;
}
/*
* Milliseconds since this runtime's time origin, on the monotonic
* clock. Not inline: v8::Platform is only forward-declared through
* v8.h here.
*/
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.
*/
double TimeOriginMillis() const {
return m_timeOriginRealtimeMs;
}
/*
* The time origin as a CLOCK_MONOTONIC reading in milliseconds -- the
* same clock and epoch Choreographer stamps frames with. Subtracting
* it from such a timestamp maps it onto this isolate's performance
* timeline.
*/
double TimeOriginMonotonicMillis() const {
return m_timeOriginMonotonic * 1000.0;
}
/*
* WHATWG events state, the Android analogue of the iOS runtime's
* Caches members of the same names. The backing event target is set
* by Events::Init, the three dispatch closures by ErrorEvents::Init,
* and the rejection tracker is created in PrepareV8Runtime. All are
* released in DestroyRuntime before the isolate is disposed.
*
* Internal EventTarget instance backing the global. Holds the real
* listener store, so native layers dispatch through it without going
* through overwritable globals (future native consumers like
* AbortSignal dispatch through it too).
*/
v8::Global<v8::Object>& GlobalEventTarget() {
return m_globalEventTarget;
}
/*
* Error-events dispatch closures returned by the bootstrap IIFE
* (ErrorEvents::Init). They close over the internal listener store,
* so native dispatch keeps working even if app code overwrites
* globalThis.dispatchEvent.
*/
v8::Global<v8::Function>& DispatchErrorEventFunc() {
return m_dispatchErrorEventFunc;
}
v8::Global<v8::Function>& DispatchUnhandledRejectionFunc() {
return m_dispatchUnhandledRejectionFunc;
}
v8::Global<v8::Function>& DispatchRejectionHandledFunc() {
return m_dispatchRejectionHandledFunc;
}
v8::Global<v8::Function>& DispatchNativeUncaughtErrorFunc() {
return m_dispatchNativeUncaughtErrorFunc;
}
PromiseRejectionTracker* PromiseRejections() const {
return m_promiseRejections.get();
}
UncaughtErrorPolicy GetUncaughtErrorPolicy() const {
return m_uncaughtErrorPolicy;
}
bool GetDiscardUncaughtJsExceptions() const {
return m_discardUncaughtJsExceptions;
}
/*
* Depth of in-flight JS->Java calls on this runtime's thread. When a
* JS callback invoked from Java throws while the depth is non-zero,
* the whole chain is JS-initiated (JS -> Java -> JS), so the throw
* must propagate back to the outer JS catch instead of being
* contained at the boundary. Maintained by CallbackHandlers around
* the JS->Java invocation sites.
*/
int JavaCallDepth() const {
return m_javaCallDepth;
}
void EnterJavaCall() {
++m_javaCallDepth;
}
void LeaveJavaCall() {
--m_javaCallDepth;
}
private:
Runtime(JNIEnv* env, jobject runtime, int id);
int m_id;
jobject m_runtime;
v8::Isolate* m_isolate;
ObjectManager* m_objectManager;
ModuleInternal m_module;
ArrayBufferHelper m_arrayBufferHelper;
WeakRef m_weakRef;
Profiler m_profiler;
std::shared_ptr<EventLoop> m_eventLoop;
std::unique_ptr<RuntimeState> m_state;
napi_env m_napiEnv = nullptr;
v8::Global<v8::Object> m_globalEventTarget;
v8::Global<v8::Function> m_dispatchErrorEventFunc;
v8::Global<v8::Function> m_dispatchUnhandledRejectionFunc;
v8::Global<v8::Function> m_dispatchRejectionHandledFunc;
v8::Global<v8::Function> m_dispatchNativeUncaughtErrorFunc;
std::unique_ptr<PromiseRejectionTracker> m_promiseRejections;
UncaughtErrorPolicy m_uncaughtErrorPolicy = UncaughtErrorPolicy::Report;
bool m_discardUncaughtJsExceptions = false;
int m_javaCallDepth = 0;
int64_t m_lastUsedMemory;
v8::Persistent<v8::Function>* m_gcFunc;
volatile bool m_runGC;
v8::Persistent<v8::Context>* m_context;
// Decided by ElectMainRuntime, before anything can read it.
bool m_isMainThread = false;
// This isolate's performance time origin, captured at isolate
// creation: the monotonic clock reading now() is relative to, and the
// wall-clock milliseconds that reading corresponds to.
double m_timeOriginMonotonic {0};
double m_timeOriginRealtimeMs {0};
v8::Isolate* PrepareV8Runtime(const std::string& filesPath, const std::string& nativeLibsDir, const std::string& packageName, bool isDebuggable, const std::string& callingDir, const std::string& profilerOutputDir, const int maxLogcatObjectSize, const bool forceLog);
/*
* Decides whether this runtime is the main one and, if so, performs the
* once-per-process V8 initialization -- both inside one critical
* section, so two concurrent bootstraps cannot each elect themselves
* and overwrite Runtime::platform and s_mainEventLoop.
*
* A runtime that loses the election blocks here until the main runtime
* is ready, because everything after this point depends on state only
* the main runtime publishes -- most of all the metadata tree, which it
* alone builds and every other runtime reads. Throws if the main
* runtime failed to initialize.
*/
void ElectMainRuntime();
// Publishes (or, on failure, withdraws) the main runtime's readiness
// and wakes anything blocked in ElectMainRuntime.
static void SignalMainRuntimeReady(bool failed);
/*
* Unwinds an initialization that threw after the isolate existed. The
* Java-side rollback only unwinds Java state, which would otherwise
* leave the isolate in the runtime caches and the half-built Runtime
* holding everything it had allocated.
*/
void UnwindFailedInit();
jobject ConvertJsValueToJavaObject(JEnv& env, const v8::Local<v8::Value>& value, int classReturnType);
static int GetAndroidVersion();
static int m_androidVersion;
static robin_hood::unordered_map<int, Runtime*> s_id2RuntimeCache;
static robin_hood::unordered_map<v8::Isolate*, Runtime*> s_isolate2RuntimesCache;
/*
* Guards the two caches above: runtimes are now constructed and
* destroyed concurrently on native-spawned worker threads.
*/
static std::mutex s_runtimeCacheMutex;
static JavaVM* s_jvm;
static jmethodID GET_USED_MEMORY_METHOD_ID;
/*
* The main runtime has finished initializing. Distinct from the
* election below: the elected runtime is not usable by others until it
* has built the metadata they all read.
*/
static std::atomic<bool> s_mainThreadInitialized;
// Guards main-runtime election and the V8 initialization that goes
// with it; s_mainInitReady signals the flags above and below.
static std::mutex s_mainInitMutex;
static std::condition_variable s_mainInitReady;
static bool s_mainRuntimeElected;
static bool s_mainRuntimeFailed;
static std::shared_ptr<EventLoop> s_mainEventLoop;
static thread_local Runtime* s_currentRuntime;
};
}
#endif /*#ifndef RUNTIME_H_*/