1 |
|
|
#include "node_native_module_env.h" |
2 |
|
|
#include "env-inl.h" |
3 |
|
|
#include "node_external_reference.h" |
4 |
|
|
|
5 |
|
|
namespace node { |
6 |
|
|
namespace native_module { |
7 |
|
|
|
8 |
|
|
using v8::Context; |
9 |
|
|
using v8::DEFAULT; |
10 |
|
|
using v8::Function; |
11 |
|
|
using v8::FunctionCallbackInfo; |
12 |
|
|
using v8::IntegrityLevel; |
13 |
|
|
using v8::Isolate; |
14 |
|
|
using v8::Local; |
15 |
|
|
using v8::MaybeLocal; |
16 |
|
|
using v8::Name; |
17 |
|
|
using v8::None; |
18 |
|
|
using v8::Object; |
19 |
|
|
using v8::PropertyCallbackInfo; |
20 |
|
|
using v8::Set; |
21 |
|
|
using v8::SideEffectType; |
22 |
|
|
using v8::String; |
23 |
|
|
using v8::Value; |
24 |
|
|
|
25 |
|
|
// TODO(joyeecheung): make these more general and put them into util.h |
26 |
|
4 |
Local<Set> ToJsSet(Local<Context> context, const std::set<std::string>& in) { |
27 |
|
4 |
Isolate* isolate = context->GetIsolate(); |
28 |
|
4 |
Local<Set> out = Set::New(isolate); |
29 |
✓✓ |
453 |
for (auto const& x : in) { |
30 |
|
1347 |
out->Add(context, OneByteString(isolate, x.c_str(), x.size())) |
31 |
|
|
.ToLocalChecked(); |
32 |
|
|
} |
33 |
|
4 |
return out; |
34 |
|
|
} |
35 |
|
|
|
36 |
|
17 |
bool NativeModuleEnv::Add(const char* id, const UnionBytes& source) { |
37 |
|
17 |
return NativeModuleLoader::GetInstance()->Add(id, source); |
38 |
|
|
} |
39 |
|
|
|
40 |
|
|
bool NativeModuleEnv::Exists(const char* id) { |
41 |
|
|
return NativeModuleLoader::GetInstance()->Exists(id); |
42 |
|
|
} |
43 |
|
|
|
44 |
|
36 |
Local<Object> NativeModuleEnv::GetSourceObject(Local<Context> context) { |
45 |
|
36 |
return NativeModuleLoader::GetInstance()->GetSourceObject(context); |
46 |
|
|
} |
47 |
|
|
|
48 |
|
647 |
Local<String> NativeModuleEnv::GetConfigString(Isolate* isolate) { |
49 |
|
647 |
return NativeModuleLoader::GetInstance()->GetConfigString(isolate); |
50 |
|
|
} |
51 |
|
|
|
52 |
|
1 |
void NativeModuleEnv::GetModuleCategories( |
53 |
|
|
Local<Name> property, const PropertyCallbackInfo<Value>& info) { |
54 |
|
1 |
Environment* env = Environment::GetCurrent(info); |
55 |
|
1 |
Isolate* isolate = env->isolate(); |
56 |
|
1 |
Local<Context> context = env->context(); |
57 |
|
1 |
Local<Object> result = Object::New(isolate); |
58 |
|
|
|
59 |
|
|
// Copy from the per-process categories |
60 |
|
|
std::set<std::string> cannot_be_required = |
61 |
|
2 |
NativeModuleLoader::GetInstance()->GetCannotBeRequired(); |
62 |
|
|
std::set<std::string> can_be_required = |
63 |
|
1 |
NativeModuleLoader::GetInstance()->GetCanBeRequired(); |
64 |
|
|
|
65 |
✗✓ |
1 |
if (!env->owns_process_state()) { |
66 |
|
|
can_be_required.erase("trace_events"); |
67 |
|
|
cannot_be_required.insert("trace_events"); |
68 |
|
|
} |
69 |
|
|
|
70 |
|
|
result |
71 |
|
1 |
->Set(context, |
72 |
|
|
OneByteString(isolate, "cannotBeRequired"), |
73 |
|
4 |
ToJsSet(context, cannot_be_required)) |
74 |
|
|
.FromJust(); |
75 |
|
|
result |
76 |
|
1 |
->Set(context, |
77 |
|
|
OneByteString(isolate, "canBeRequired"), |
78 |
|
4 |
ToJsSet(context, can_be_required)) |
79 |
|
|
.FromJust(); |
80 |
|
2 |
info.GetReturnValue().Set(result); |
81 |
|
1 |
} |
82 |
|
|
|
83 |
|
1 |
void NativeModuleEnv::GetCacheUsage(const FunctionCallbackInfo<Value>& args) { |
84 |
|
1 |
Environment* env = Environment::GetCurrent(args); |
85 |
|
1 |
Isolate* isolate = env->isolate(); |
86 |
|
1 |
Local<Context> context = env->context(); |
87 |
|
1 |
Local<Object> result = Object::New(isolate); |
88 |
|
|
result |
89 |
|
1 |
->Set(env->context(), |
90 |
|
|
OneByteString(isolate, "compiledWithCache"), |
91 |
|
4 |
ToJsSet(context, env->native_modules_with_cache)) |
92 |
|
|
.FromJust(); |
93 |
|
|
result |
94 |
|
1 |
->Set(env->context(), |
95 |
|
|
OneByteString(isolate, "compiledWithoutCache"), |
96 |
|
4 |
ToJsSet(context, env->native_modules_without_cache)) |
97 |
|
|
.FromJust(); |
98 |
|
|
|
99 |
|
|
result |
100 |
|
1 |
->Set(env->context(), |
101 |
|
|
OneByteString(isolate, "compiledInSnapshot"), |
102 |
|
1 |
ToV8Value(env->context(), env->native_modules_in_snapshot) |
103 |
|
4 |
.ToLocalChecked()) |
104 |
|
|
.FromJust(); |
105 |
|
|
|
106 |
|
1 |
args.GetReturnValue().Set(result); |
107 |
|
1 |
} |
108 |
|
|
|
109 |
|
644 |
void NativeModuleEnv::ModuleIdsGetter(Local<Name> property, |
110 |
|
|
const PropertyCallbackInfo<Value>& info) { |
111 |
|
644 |
Isolate* isolate = info.GetIsolate(); |
112 |
|
|
|
113 |
|
|
std::vector<std::string> ids = |
114 |
|
644 |
NativeModuleLoader::GetInstance()->GetModuleIds(); |
115 |
|
1932 |
info.GetReturnValue().Set( |
116 |
|
1288 |
ToV8Value(isolate->GetCurrentContext(), ids).ToLocalChecked()); |
117 |
|
644 |
} |
118 |
|
|
|
119 |
|
611 |
void NativeModuleEnv::ConfigStringGetter( |
120 |
|
|
Local<Name> property, const PropertyCallbackInfo<Value>& info) { |
121 |
|
611 |
info.GetReturnValue().Set(GetConfigString(info.GetIsolate())); |
122 |
|
611 |
} |
123 |
|
|
|
124 |
|
392361 |
void NativeModuleEnv::RecordResult(const char* id, |
125 |
|
|
NativeModuleLoader::Result result, |
126 |
|
|
Environment* env) { |
127 |
✓✓ |
392361 |
if (result == NativeModuleLoader::Result::kWithCache) { |
128 |
|
370679 |
env->native_modules_with_cache.insert(id); |
129 |
|
|
} else { |
130 |
|
21682 |
env->native_modules_without_cache.insert(id); |
131 |
|
|
} |
132 |
|
392361 |
} |
133 |
|
384534 |
void NativeModuleEnv::CompileFunction(const FunctionCallbackInfo<Value>& args) { |
134 |
|
384534 |
Environment* env = Environment::GetCurrent(args); |
135 |
✗✓ |
769068 |
CHECK(args[0]->IsString()); |
136 |
|
1153602 |
node::Utf8Value id_v(env->isolate(), args[0].As<String>()); |
137 |
|
384534 |
const char* id = *id_v; |
138 |
|
|
NativeModuleLoader::Result result; |
139 |
|
|
MaybeLocal<Function> maybe = |
140 |
|
|
NativeModuleLoader::GetInstance()->CompileAsModule( |
141 |
|
384534 |
env->context(), id, &result); |
142 |
|
384534 |
RecordResult(id, result, env); |
143 |
|
|
Local<Function> fn; |
144 |
✓✗ |
384534 |
if (maybe.ToLocal(&fn)) { |
145 |
|
769068 |
args.GetReturnValue().Set(fn); |
146 |
|
|
} |
147 |
|
384534 |
} |
148 |
|
|
|
149 |
|
|
// Returns Local<Function> of the compiled module if return_code_cache |
150 |
|
|
// is false (we are only compiling the function). |
151 |
|
|
// Otherwise return a Local<Object> containing the cache. |
152 |
|
11536 |
MaybeLocal<Function> NativeModuleEnv::LookupAndCompile( |
153 |
|
|
Local<Context> context, |
154 |
|
|
const char* id, |
155 |
|
|
std::vector<Local<String>>* parameters, |
156 |
|
|
Environment* optional_env) { |
157 |
|
|
NativeModuleLoader::Result result; |
158 |
|
|
MaybeLocal<Function> maybe = |
159 |
|
|
NativeModuleLoader::GetInstance()->LookupAndCompile( |
160 |
|
11536 |
context, id, parameters, &result); |
161 |
✓✓ |
11536 |
if (optional_env != nullptr) { |
162 |
|
7827 |
RecordResult(id, result, optional_env); |
163 |
|
|
} |
164 |
|
11536 |
return maybe; |
165 |
|
|
} |
166 |
|
|
|
167 |
|
4 |
void HasCachedBuiltins(const FunctionCallbackInfo<Value>& args) { |
168 |
✓✗ |
4 |
args.GetReturnValue().Set( |
169 |
|
|
v8::Boolean::New(args.GetIsolate(), has_code_cache)); |
170 |
|
4 |
} |
171 |
|
|
|
172 |
|
|
// TODO(joyeecheung): It is somewhat confusing that Class::Initialize |
173 |
|
|
// is used to initialize to the binding, but it is the current convention. |
174 |
|
|
// Rename this across the code base to something that makes more sense. |
175 |
|
611 |
void NativeModuleEnv::Initialize(Local<Object> target, |
176 |
|
|
Local<Value> unused, |
177 |
|
|
Local<Context> context, |
178 |
|
|
void* priv) { |
179 |
|
611 |
Environment* env = Environment::GetCurrent(context); |
180 |
|
|
|
181 |
|
|
target |
182 |
|
611 |
->SetAccessor(env->context(), |
183 |
|
|
env->config_string(), |
184 |
|
|
ConfigStringGetter, |
185 |
|
|
nullptr, |
186 |
|
|
MaybeLocal<Value>(), |
187 |
|
|
DEFAULT, |
188 |
|
|
None, |
189 |
|
2444 |
SideEffectType::kHasNoSideEffect) |
190 |
|
|
.Check(); |
191 |
|
|
target |
192 |
|
611 |
->SetAccessor(env->context(), |
193 |
|
|
FIXED_ONE_BYTE_STRING(env->isolate(), "moduleIds"), |
194 |
|
|
ModuleIdsGetter, |
195 |
|
|
nullptr, |
196 |
|
|
MaybeLocal<Value>(), |
197 |
|
|
DEFAULT, |
198 |
|
|
None, |
199 |
|
2444 |
SideEffectType::kHasNoSideEffect) |
200 |
|
|
.Check(); |
201 |
|
|
|
202 |
|
|
target |
203 |
|
611 |
->SetAccessor(env->context(), |
204 |
|
|
FIXED_ONE_BYTE_STRING(env->isolate(), "moduleCategories"), |
205 |
|
|
GetModuleCategories, |
206 |
|
|
nullptr, |
207 |
|
|
Local<Value>(), |
208 |
|
|
DEFAULT, |
209 |
|
|
None, |
210 |
|
1222 |
SideEffectType::kHasNoSideEffect) |
211 |
|
|
.Check(); |
212 |
|
|
|
213 |
|
611 |
env->SetMethod(target, "getCacheUsage", NativeModuleEnv::GetCacheUsage); |
214 |
|
611 |
env->SetMethod(target, "compileFunction", NativeModuleEnv::CompileFunction); |
215 |
|
611 |
env->SetMethod(target, "hasCachedBuiltins", HasCachedBuiltins); |
216 |
|
|
// internalBinding('native_module') should be frozen |
217 |
|
611 |
target->SetIntegrityLevel(context, IntegrityLevel::kFrozen).FromJust(); |
218 |
|
611 |
} |
219 |
|
|
|
220 |
|
4794 |
void NativeModuleEnv::RegisterExternalReferences( |
221 |
|
|
ExternalReferenceRegistry* registry) { |
222 |
|
4794 |
registry->Register(ConfigStringGetter); |
223 |
|
4794 |
registry->Register(ModuleIdsGetter); |
224 |
|
4794 |
registry->Register(GetModuleCategories); |
225 |
|
4794 |
registry->Register(GetCacheUsage); |
226 |
|
4794 |
registry->Register(CompileFunction); |
227 |
|
4794 |
registry->Register(HasCachedBuiltins); |
228 |
|
4794 |
} |
229 |
|
|
|
230 |
|
|
} // namespace native_module |
231 |
|
|
} // namespace node |
232 |
|
|
|
233 |
|
4863 |
NODE_MODULE_CONTEXT_AWARE_INTERNAL( |
234 |
|
|
native_module, node::native_module::NativeModuleEnv::Initialize) |
235 |
|
4794 |
NODE_MODULE_EXTERNAL_REFERENCE( |
236 |
|
|
native_module, |
237 |
|
|
node::native_module::NativeModuleEnv::RegisterExternalReferences) |