Repository navigation
Expand file tree
/
Copy pathRequireEsm.cpp
More file actions
105 lines (92 loc) · 3.91 KB
/
Copy pathRequireEsm.cpp
File metadata and controls
105 lines (92 loc) · 3.91 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
#include "ns/loader/RequireEsm.h"
#include <string>
#include "loader/LoaderStateInternal.h"
#include "ns/JsError.h"
#include "ns/V8Helpers.h"
#include "ns/loader/LoaderState.h"
#include "ns/loader/ModuleRegistry.h"
namespace nativescript::common::loader {
namespace {
constexpr const char* kFacadeSpecifier = "original";
constexpr const char* kFacadeSource =
"export * from 'original'; export { default } from 'original'; "
"export const __esModule = true;";
// Only a facade's InstantiateModule is given this resolver, so no other
// module can reach the pending target.
v8::MaybeLocal<v8::Module> ResolveFacadeTarget(v8::Local<v8::Context>,
v8::Local<v8::String> specifier,
v8::Local<v8::FixedArray>,
v8::Local<v8::Module>) {
v8::Isolate* isolate = v8::Isolate::GetCurrent();
ModuleLoaderState* state = LoaderStateFor(isolate);
if (state == nullptr || state->pendingFacadeTarget.IsEmpty() ||
ToString(isolate, specifier) != kFacadeSpecifier) {
isolate->ThrowException(v8::Exception::Error(ToV8String(
isolate,
"require(esm) facade could not be linked to its target module")));
return v8::MaybeLocal<v8::Module>();
}
return state->pendingFacadeTarget.Get(isolate);
}
} // namespace
v8::Local<v8::Module> GetOrCreateRequireFacade(v8::Local<v8::Context> context,
v8::Local<v8::Module> target,
const std::string& label) {
v8::Isolate* isolate = v8::Isolate::GetCurrent();
ModuleLoaderState* state = LoaderStateFor(isolate);
if (state == nullptr || state->closing) {
throw JsError("The module loader is shutting down");
}
v8::Local<v8::Module> cached = FindRequireFacade(isolate, target);
if (!cached.IsEmpty()) {
return cached;
}
v8::EscapableHandleScope handleScope(isolate);
const std::string url = "ns:require-facade:" + label;
v8::ScriptOrigin origin(ToV8String(isolate, url), 0, 0, false, -1,
v8::Local<v8::Value>(), false, false, true);
v8::ScriptCompiler::Source source(ToV8String(isolate, kFacadeSource), origin);
v8::TryCatch tryCatch(isolate);
v8::Local<v8::Module> facade;
if (!v8::ScriptCompiler::CompileModule(isolate, &source).ToLocal(&facade)) {
JsError::ThrowCaught(isolate, tryCatch,
"Cannot compile the require() facade for " + label);
}
bool linked = false;
{
struct PendingTarget {
ModuleLoaderState* state;
~PendingTarget() { state->pendingFacadeTarget.Reset(); }
} pending{state};
state->pendingFacadeTarget.Reset(isolate, target);
linked = facade->InstantiateModule(context, ResolveFacadeTarget)
.FromMaybe(false);
}
if (!linked) {
JsError::ThrowCaught(isolate, tryCatch,
"Cannot link the require() facade for " + label);
}
v8::Local<v8::Value> result;
if (!facade->Evaluate(context).ToLocal(&result)) {
JsError::ThrowCaught(isolate, tryCatch,
"Cannot evaluate the require() facade for " + label);
}
if (result->IsPromise()) {
v8::Local<v8::Promise> promise = result.As<v8::Promise>();
if (promise->State() == v8::Promise::kRejected) {
promise->MarkAsHandled();
throw JsError::FromValue(
isolate, promise->Result(),
"Cannot evaluate the require() facade for " + label);
}
if (promise->State() == v8::Promise::kPending) {
throw JsError("The require() facade for " + label +
" did not evaluate synchronously");
}
}
// Not registered: nothing resolves to a facade by name, and its source has
// no import.meta or import() for a host callback to look it up by.
AddRequireFacade(isolate, target, facade);
return handleScope.Escape(facade);
}
} // namespace nativescript::common::loader