Repository navigation
Expand file tree
/
Copy pathGraphLoad.h
More file actions
159 lines (141 loc) · 7.62 KB
/
Copy pathGraphLoad.h
File metadata and controls
159 lines (141 loc) · 7.62 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
#ifndef NATIVESCRIPT_COMMON_LOADER_GRAPH_LOAD_H_
#define NATIVESCRIPT_COMMON_LOADER_GRAPH_LOAD_H_
#include <v8.h>
#include <cstdint>
#include <functional>
#include <memory>
#include <string>
#include "ns/RuntimeLiveness.h"
#include "ns/http/ModuleFetch.h"
#include "ns/loader/Resolver.h"
// The module graph walk: from a root, discover and compile the module's whole
// static import closure, so that linking finds every module registered and
// never blocks.
//
// Every edge resolves through ResolveModuleSpecifier, as the resolver's do,
// so both register a module under the same key:
// - an http(s) edge is fetched off-thread (http::FetchModuleBodyAsync); a
// body of 32 KiB or more is compiled on the compile pool, the rest on the
// isolate's thread;
// - an .mjs file of 32 KiB or more (256 KiB with a fresh code cache) is read,
// and compiled or its cache consumed, on the compile pool; any other file is
// read and compiled inline;
// - builtins, blobs and specifiers that resolve to nothing are left to the
// resolver, which reports them.
//
// Compiling runs no JavaScript, so the walk cannot change evaluation order.
// A module that fails to fetch or compile is left unregistered, and the
// resolver (or the caller's own load) reports it with its own message. Only
// an http(s) root reports its failure through the walk.
//
// Every function runs on the isolate's thread, under its Locker.
namespace nativescript::common::loader {
// What a walk starts from. A caller says which kind of root it holds; the
// walk never infers it from a string.
struct ModuleGraphRoot {
bool http = false;
std::string url; // http: the absolute URL, NormalizeHttpModuleUrl spelling
// file: the file read and probed, used whole; canonical unless `key` is
// empty.
std::string path;
// file: the registry key; empty for a FilePath root.
std::string key;
std::string query; // file: the query `key` carries after `path`
// A path a native caller holds (an entry module, a worker entry, require()
// of an ES module): the whole string names the file, canonicalized
// (CanonicalFilePath) when the walk starts, and the module is the one
// without a query.
static ModuleGraphRoot FilePath(const std::string& path);
static ModuleGraphRoot HttpUrl(const std::string& url);
// A kFile or kHttp resolution; any other kind gives a root that walks
// nothing.
static ModuleGraphRoot FromResolution(const ModuleResolution& resolution);
};
// Runs at most once, on the isolate's thread, with the isolate entered and
// the walk's context entered. `ok` is false only when an http(s) root failed
// to fetch or compile; `errorMessage` then says why, and `exception` is the
// exception the root failed with when it was a JSON root that did not parse
// (empty otherwise). It never runs for a walk started after loader::Quiesce,
// and is destroyed unrun, on any thread, when the runtime is torn down before
// the walk completes, so it must own no V8 state: a caller keeps its promise
// resolvers in its own isolate state.
using GraphLoadCompletion = std::function<void(
bool ok, const std::string& errorMessage, v8::Local<v8::Value> exception,
v8::Local<v8::Context> context)>;
// Starts a walk in `context`. Fetches stop at `fetchDeadline` (nullopt:
// unbounded): a caller that waits for the walk passes its own deadline, so no
// fetch outlives the wait. A graph of small local files completes inside
// this call.
void StartModuleGraphLoad(v8::Isolate* isolate, v8::Local<v8::Context> context,
ModuleGraphRoot root,
http::ModuleFetchDeadline fetchDeadline,
GraphLoadCompletion onComplete);
// Held across a synchronous module load (require() of an ES module, a boot or
// worker entry), its evaluation included. Walks started before the outermost
// scope opened neither continue nor evaluate inside it: they resume as
// event-loop tasks once it closes.
class SyncModuleLoadScope {
public:
explicit SyncModuleLoadScope(v8::Isolate* isolate);
~SyncModuleLoadScope();
SyncModuleLoadScope(const SyncModuleLoadScope&) = delete;
SyncModuleLoadScope& operator=(const SyncModuleLoadScope&) = delete;
private:
v8::Isolate* isolate_;
};
// Starts a walk inside a SyncModuleLoadScope and waits for it, up to
// `timeoutSeconds` (60 when not positive), pumping the isolate's loop:
// kPlatformWork while an http(s) fetch is outstanding, otherwise only the
// nestable V8 tasks that deliver file loads, sleeping on them in between.
// True when the walk completed, whether or not its root loaded; false on a
// timeout, and on termination even when the walk completed in the same slice.
// The caller's own load then reports what is missing.
//
// Off the loop's home thread (multithreaded JavaScript) it never pumps, since
// the caller holds the Locker the loop needs: true only when the walk
// completed inside the call and termination was not requested.
bool RunModuleGraphLoadPumped(v8::Isolate* isolate,
v8::Local<v8::Context> context,
const ModuleGraphRoot& root,
double timeoutSeconds);
// Whether a walk in any isolate has started and has neither completed nor
// been stopped by loader::Quiesce. Any thread.
bool HasPendingGraphWork();
// The module registered for the http(s) URL `url` (NormalizeHttpModuleUrl
// spelling), fetched and compiled synchronously when the walk did not
// register it. Logs that fallback unconditionally: it blocks the thread for a
// whole round trip and means the walk missed an edge. Empty, with an
// exception pending, when the fetch, the compile or a JSON module's parse
// fails. Never instantiates or evaluates.
v8::MaybeLocal<v8::Module> LoadHttpModuleForUrl(v8::Isolate* isolate,
v8::Local<v8::Context> context,
const std::string& url);
// The resolver's step for a file key the registry does not hold: when a
// background load of it has finished (never one still running, which the
// resolver must not wait for), compiles and registers its result. The
// registered module when that left a usable one; empty otherwise, with no
// exception pending, and the caller compiles the file itself.
v8::Local<v8::Module> FinalizeFinishedFileModule(v8::Isolate* isolate,
v8::Local<v8::Context> context,
const std::string& key);
// The runtime's liveness token, created the first time it is needed; null
// while the isolate has no loop. Background work that outlives a call
// captures it (see ns/RuntimeLiveness.h). loader::Quiesce closes it.
//
// The token keeps the loop it was created with: a runtime that replaces its
// loop afterwards aborts here, the next time a walk posts through the token
// or waits on the loop. A walk that only meets registered modules does
// neither and is not checked. Isolate thread.
std::shared_ptr<RuntimeLiveness> LivenessFor(v8::Isolate* isolate);
// Which way each large HTTP body was compiled, across all isolates. Debug
// diagnostics only.
struct StreamedCompileStats {
uint64_t finalized = 0; // compiled on the pool, then finalized
uint64_t discarded = 0; // compiled on the pool, key registered meanwhile
uint64_t fellBack = 0; // compiled on the isolate's thread instead
int64_t running = 0; // pool compiles in progress
uint64_t teardownWaits = 0; // teardowns that waited for a pool compile
};
StreamedCompileStats GetStreamedCompileStats();
} // namespace nativescript::common::loader
#endif // NATIVESCRIPT_COMMON_LOADER_GRAPH_LOAD_H_