Repository navigation
Expand file tree
/
Copy pathEvents.cpp
More file actions
46 lines (39 loc) · 1.93 KB
/
Copy pathEvents.cpp
File metadata and controls
46 lines (39 loc) · 1.93 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
#include "Events.h"
#include "ArgConverter.h"
#include "BuiltinLoader.h"
#include "NativeScriptException.h"
#include "Runtime.h"
using namespace std;
using namespace tns;
using namespace v8;
void Events::Init(Local<Context> context) {
// The builtin installs Event/EventTarget and the global EventTarget
// methods; its exports carry the internal EventTarget instance backing the
// global (cached here so native dispatch survives app code overwriting
// globalThis.dispatchEvent) and the CustomEvent interface the lazy-global
// tier places. Run through GetExports so that tier's read shares this run.
auto isolate = v8::Isolate::GetCurrent();
auto runtime = Runtime::TryGetRuntime(isolate);
if (runtime == nullptr) {
throw NativeScriptException("Events::Init: no runtime for isolate");
}
Local<Object> exports;
if (!BuiltinLoader::GetExports(context, BuiltinId::kEvents, nullptr).ToLocal(&exports)) {
throw NativeScriptException("Events::Init: the event-primitives bootstrap failed");
}
Local<Value> globalEventTarget;
if (!exports->Get(context, ArgConverter::ConvertToV8String(isolate, "globalEventTarget"))
.ToLocal(&globalEventTarget) ||
!globalEventTarget->IsObject()) {
throw NativeScriptException("Events::Init: the event-primitives bootstrap did not return the backing target");
}
runtime->GlobalEventTarget().Reset(isolate, globalEventTarget.As<Object>());
// AbortController/AbortSignal (internal/abort-signal.js) build directly on
// the event primitives installed above; the listener-mutation hook key for
// its GC-liveness accounting comes from the events builtin's exports, via
// require("internal/events").
Local<Value> abortResult;
if (!BuiltinLoader::RunBuiltin(context, BuiltinId::kAbortSignal).ToLocal(&abortResult)) {
throw NativeScriptException("Events::Init: the abort-signal bootstrap failed");
}
}