Repository navigation
Expand file tree
/
Copy pathLoaderStateInternal.h
More file actions
85 lines (73 loc) · 2.87 KB
/
Copy pathLoaderStateInternal.h
File metadata and controls
85 lines (73 loc) · 2.87 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
#ifndef NATIVESCRIPT_COMMON_LOADER_LOADER_STATE_INTERNAL_H_
#define NATIVESCRIPT_COMMON_LOADER_LOADER_STATE_INTERNAL_H_
#include <v8.h>
#include <cstdint>
#include <memory>
#include <string>
#include <unordered_map>
#include "loader/GraphLoadInternal.h"
#include "loader/ImportInternal.h"
#include "loader/RegistryTable.h"
#include "ns/IsolateState.h"
#include "ns/LoaderConfig.h"
#include "ns/loader/CodeCache.h"
#include "ns/loader/ImportMap.h"
namespace nativescript::common::loader {
// How import took a .js file, for as long as it still has this Stat.
struct DetectedModuleFormat {
uint64_t size = 0;
std::string freshnessToken;
ModuleFormat format = ModuleFormat::kNone;
};
struct V8ModuleTraits {
using Env = v8::Isolate*;
using Ref = v8::Local<v8::Module>;
using Stored = v8::Global<v8::Module>;
static bool IsEmpty(const Ref& module) { return module.IsEmpty(); }
static int Hash(const Ref& module) { return module->GetIdentityHash(); }
static Ref Get(Env isolate, const Stored& stored) {
return stored.Get(isolate);
}
static void Store(Env isolate, Stored& stored, const Ref& module) {
stored.Reset(isolate, module);
}
static bool IsStoredEmpty(const Stored& stored) { return stored.IsEmpty(); }
// Global == Local and Global == Global compare the objects the handles
// name without making a handle.
static bool Holds(const Stored& stored, const Ref& module) {
return stored == module;
}
static bool SameStored(const Stored& a, const Stored& b) { return a == b; }
// The registry's writes run in the caller's HandleScope and return no
// handle, so the Local read here must not outlive the call.
static int StoredHash(Env isolate, const Stored& stored) {
v8::HandleScope handleScope(isolate);
return stored.Get(isolate)->GetIdentityHash();
}
};
// One isolate's loader state. It holds v8::Globals, so the runtime closes its
// state bag while the isolate is still alive.
struct ModuleLoaderState {
#ifndef NDEBUG
// Aborts when an installed loader's state closes before loader::Quiesce.
~ModuleLoaderState();
#endif
std::shared_ptr<const LoaderConfig> config;
bool closing = false;
LoaderVocabulary vocabulary;
RegistryTable<V8ModuleTraits> registry;
GraphLoaderState graph;
ImportState imports;
// By path; only files with a freshness token.
std::unordered_map<std::string, DetectedModuleFormat> detectedFormats;
// The target of the require(esm) facade being instantiated, for its
// resolver; empty otherwise.
v8::Global<v8::Module> pendingFacadeTarget;
};
// Null for a null isolate: callers pass v8::Isolate::TryGetCurrent().
inline ModuleLoaderState* LoaderStateFor(v8::Isolate* isolate) {
return isolate != nullptr ? IsolateState::For<ModuleLoaderState>(isolate)
: nullptr;
}
} // namespace nativescript::common::loader
#endif // NATIVESCRIPT_COMMON_LOADER_LOADER_STATE_INTERNAL_H_