Repository navigation
Expand file tree
/
Copy pathImportInternal.h
More file actions
113 lines (97 loc) · 4.45 KB
/
Copy pathImportInternal.h
File metadata and controls
113 lines (97 loc) · 4.45 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
#ifndef NATIVESCRIPT_COMMON_LOADER_IMPORT_INTERNAL_H_
#define NATIVESCRIPT_COMMON_LOADER_IMPORT_INTERNAL_H_
#include <v8.h>
#include <cstdint>
#include <optional>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
// Import state that holds V8 handles: the promises of import() calls still
// loading, and JSON modules waiting to be evaluated. Isolate thread only.
namespace nativescript::common::loader {
struct ModuleLoaderState;
// An import's attributes: its `type` (absent, "json", or a value the loader
// does not support), and the first key other than `type`, which the loader
// supports none of.
struct ImportAttributes {
enum class Type { kJavaScript, kJson, kUnsupported };
Type type = Type::kJavaScript;
// The value, for kUnsupported.
std::string unsupportedType;
std::optional<std::string> unsupportedKey;
std::string unsupportedKeyValue;
};
// An import()'s attributes, to check once the type of the module it waits on
// is known, with what a warning names.
struct PendingAttributeCheck {
ImportAttributes attributes;
std::string specifier;
std::string importer;
};
struct ImportWaiter {
v8::Global<v8::Promise::Resolver> resolver;
// Unset once checked, or when the module's type was known when it queued.
std::optional<PendingAttributeCheck> check;
};
struct PendingJsonModule {
v8::Global<v8::Module> module;
v8::Global<v8::Value> value;
};
// An import() of a module whose evaluation was on the stack when it was
// made, waiting for that evaluation to return.
struct ParkedImport {
v8::Global<v8::Context> context;
v8::Global<v8::Module> module;
v8::Global<v8::Promise::Resolver> resolver;
std::string normalizedSpec;
bool json = false;
};
struct ImportState {
// Keys of http(s) and blob: modules an import() is loading or evaluating.
// Another import() of one waits in `waiters` instead of starting again.
std::unordered_set<std::string> inFlight;
std::unordered_map<std::string, std::vector<ImportWaiter>> waiters;
// The promise of each file import() whose graph walk is running, by id:
// the walk's completion may be dropped off the isolate's thread, so it
// holds only the id.
std::unordered_map<uint64_t, v8::Global<v8::Promise::Resolver>> fileImports;
uint64_t nextFileImportId = 0;
// The parsed value of each JSON module not yet evaluated, by the module's
// identity hash. Its evaluation steps take the entry.
std::unordered_multimap<int, PendingJsonModule> pendingJson;
// Module URLs an import-attribute warning has been logged for.
std::unordered_set<std::string> attributeWarnings;
std::vector<ParkedImport> parkedImports;
// A microtask, and a loop task, that settle parkedImports are queued.
bool parkedMicrotaskQueued = false;
bool parkedRetryPosted = false;
};
// The JSON module `text` parses to, registered under `key` and named `url`,
// not yet linked: a synthetic module whose default export is the parsed
// value. A UTF-8 BOM is dropped first, as the UTF-8 decode of a fetched body
// drops it. A registered module that has not errored is returned as it is.
// Text that is not JSON throws a SyntaxError naming `url`. `filePath` is the
// file it was read from, empty for a served one. Isolate thread, with
// `context` entered.
v8::MaybeLocal<v8::Module> CreateJsonModule(
v8::Isolate* isolate, v8::Local<v8::Context> context, std::string text,
const std::string& key, const std::string& url, std::string filePath);
// The import of a CommonJS file, named `url`: a synthetic module whose
// `default` and `module.exports` exports, as in Node, are
// platform::RequireFileForImport of the file the registry records for it,
// read when it evaluates. Register it before it
// evaluates. Isolate thread.
v8::Local<v8::Module> CreateCommonJsModule(v8::Isolate* isolate,
v8::Local<v8::String> url);
// InvalidateModules is about to remove `key`: rejects the import() calls
// waiting on it, in `context`.
void RejectInvalidatedImports(v8::Isolate* isolate,
v8::Local<v8::Context> context,
ModuleLoaderState& state, const std::string& key);
// Drops the parsed value of the JSON module registered under `key`, which
// the registry is about to erase.
void ForgetPendingJson(v8::Isolate* isolate, ModuleLoaderState& state,
const std::string& key);
} // namespace nativescript::common::loader
#endif // NATIVESCRIPT_COMMON_LOADER_IMPORT_INTERNAL_H_