1 |
|
|
#include "node_native_module_env.h" |
2 |
|
|
#include "env-inl.h" |
3 |
|
|
|
4 |
|
|
namespace node { |
5 |
|
|
namespace native_module { |
6 |
|
|
|
7 |
|
|
using v8::Context; |
8 |
|
|
using v8::DEFAULT; |
9 |
|
|
using v8::Function; |
10 |
|
|
using v8::FunctionCallbackInfo; |
11 |
|
|
using v8::IntegrityLevel; |
12 |
|
|
using v8::Isolate; |
13 |
|
|
using v8::Local; |
14 |
|
|
using v8::Maybe; |
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 |
✓✓ |
379 |
for (auto const& x : in) { |
30 |
|
1125 |
out->Add(context, OneByteString(isolate, x.c_str(), x.size())) |
31 |
|
750 |
.ToLocalChecked(); |
32 |
|
|
} |
33 |
|
4 |
return out; |
34 |
|
|
} |
35 |
|
|
|
36 |
|
4970 |
bool NativeModuleEnv::Exists(const char* id) { |
37 |
|
4970 |
return NativeModuleLoader::GetInstance()->Exists(id); |
38 |
|
|
} |
39 |
|
|
|
40 |
|
362 |
Local<Object> NativeModuleEnv::GetSourceObject(Local<Context> context) { |
41 |
|
362 |
return NativeModuleLoader::GetInstance()->GetSourceObject(context); |
42 |
|
|
} |
43 |
|
|
|
44 |
|
5537 |
Local<String> NativeModuleEnv::GetConfigString(Isolate* isolate) { |
45 |
|
5537 |
return NativeModuleLoader::GetInstance()->GetConfigString(isolate); |
46 |
|
|
} |
47 |
|
|
|
48 |
|
1 |
void NativeModuleEnv::GetModuleCategories( |
49 |
|
|
Local<Name> property, const PropertyCallbackInfo<Value>& info) { |
50 |
|
1 |
Environment* env = Environment::GetCurrent(info); |
51 |
|
1 |
Isolate* isolate = env->isolate(); |
52 |
|
1 |
Local<Context> context = env->context(); |
53 |
|
1 |
Local<Object> result = Object::New(isolate); |
54 |
|
|
|
55 |
|
|
// Copy from the per-process categories |
56 |
|
|
std::set<std::string> cannot_be_required = |
57 |
|
1 |
NativeModuleLoader::GetInstance()->GetCannotBeRequired(); |
58 |
|
|
std::set<std::string> can_be_required = |
59 |
|
2 |
NativeModuleLoader::GetInstance()->GetCanBeRequired(); |
60 |
|
|
|
61 |
✗✓ |
1 |
if (!env->owns_process_state()) { |
62 |
|
|
can_be_required.erase("trace_events"); |
63 |
|
|
cannot_be_required.insert("trace_events"); |
64 |
|
|
} |
65 |
|
|
|
66 |
|
|
result |
67 |
|
|
->Set(context, |
68 |
|
|
OneByteString(isolate, "cannotBeRequired"), |
69 |
|
4 |
ToJsSet(context, cannot_be_required)) |
70 |
|
2 |
.FromJust(); |
71 |
|
|
result |
72 |
|
|
->Set(context, |
73 |
|
|
OneByteString(isolate, "canBeRequired"), |
74 |
|
4 |
ToJsSet(context, can_be_required)) |
75 |
|
2 |
.FromJust(); |
76 |
|
3 |
info.GetReturnValue().Set(result); |
77 |
|
1 |
} |
78 |
|
|
|
79 |
|
1 |
void NativeModuleEnv::GetCacheUsage(const FunctionCallbackInfo<Value>& args) { |
80 |
|
1 |
Environment* env = Environment::GetCurrent(args); |
81 |
|
1 |
Isolate* isolate = env->isolate(); |
82 |
|
1 |
Local<Context> context = env->context(); |
83 |
|
1 |
Local<Object> result = Object::New(isolate); |
84 |
|
|
result |
85 |
|
|
->Set(env->context(), |
86 |
|
|
OneByteString(isolate, "compiledWithCache"), |
87 |
|
5 |
ToJsSet(context, env->native_modules_with_cache)) |
88 |
|
2 |
.FromJust(); |
89 |
|
|
result |
90 |
|
|
->Set(env->context(), |
91 |
|
|
OneByteString(isolate, "compiledWithoutCache"), |
92 |
|
5 |
ToJsSet(context, env->native_modules_without_cache)) |
93 |
|
2 |
.FromJust(); |
94 |
|
2 |
args.GetReturnValue().Set(result); |
95 |
|
1 |
} |
96 |
|
|
|
97 |
|
5180 |
void NativeModuleEnv::ModuleIdsGetter(Local<Name> property, |
98 |
|
|
const PropertyCallbackInfo<Value>& info) { |
99 |
|
5180 |
Isolate* isolate = info.GetIsolate(); |
100 |
|
|
|
101 |
|
|
std::vector<std::string> ids = |
102 |
|
5180 |
NativeModuleLoader::GetInstance()->GetModuleIds(); |
103 |
|
|
info.GetReturnValue().Set( |
104 |
|
15540 |
ToV8Value(isolate->GetCurrentContext(), ids).ToLocalChecked()); |
105 |
|
5179 |
} |
106 |
|
|
|
107 |
|
5175 |
void NativeModuleEnv::ConfigStringGetter( |
108 |
|
|
Local<Name> property, const PropertyCallbackInfo<Value>& info) { |
109 |
|
15525 |
info.GetReturnValue().Set(GetConfigString(info.GetIsolate())); |
110 |
|
5175 |
} |
111 |
|
|
|
112 |
|
407828 |
void NativeModuleEnv::RecordResult(const char* id, |
113 |
|
|
NativeModuleLoader::Result result, |
114 |
|
|
Environment* env) { |
115 |
✓✓ |
407828 |
if (result == NativeModuleLoader::Result::kWithCache) { |
116 |
|
378843 |
env->native_modules_with_cache.insert(id); |
117 |
|
|
} else { |
118 |
|
28985 |
env->native_modules_without_cache.insert(id); |
119 |
|
|
} |
120 |
|
407828 |
} |
121 |
|
392295 |
void NativeModuleEnv::CompileFunction(const FunctionCallbackInfo<Value>& args) { |
122 |
|
392295 |
Environment* env = Environment::GetCurrent(args); |
123 |
✗✓ |
1176885 |
CHECK(args[0]->IsString()); |
124 |
|
784590 |
node::Utf8Value id_v(env->isolate(), args[0].As<String>()); |
125 |
|
392294 |
const char* id = *id_v; |
126 |
|
|
NativeModuleLoader::Result result; |
127 |
|
|
MaybeLocal<Function> maybe = |
128 |
|
|
NativeModuleLoader::GetInstance()->CompileAsModule( |
129 |
|
392295 |
env->context(), id, &result); |
130 |
|
392295 |
RecordResult(id, result, env); |
131 |
✓✗ |
392295 |
if (!maybe.IsEmpty()) { |
132 |
|
784590 |
args.GetReturnValue().Set(maybe.ToLocalChecked()); |
133 |
|
392295 |
} |
134 |
|
392295 |
} |
135 |
|
|
|
136 |
|
|
// Returns Local<Function> of the compiled module if return_code_cache |
137 |
|
|
// is false (we are only compiling the function). |
138 |
|
|
// Otherwise return a Local<Object> containing the cache. |
139 |
|
17647 |
MaybeLocal<Function> NativeModuleEnv::LookupAndCompile( |
140 |
|
|
Local<Context> context, |
141 |
|
|
const char* id, |
142 |
|
|
std::vector<Local<String>>* parameters, |
143 |
|
|
Environment* optional_env) { |
144 |
|
|
NativeModuleLoader::Result result; |
145 |
|
|
MaybeLocal<Function> maybe = |
146 |
|
|
NativeModuleLoader::GetInstance()->LookupAndCompile( |
147 |
|
17647 |
context, id, parameters, &result); |
148 |
✓✓ |
17647 |
if (optional_env != nullptr) { |
149 |
|
15533 |
RecordResult(id, result, optional_env); |
150 |
|
|
} |
151 |
|
17647 |
return maybe; |
152 |
|
|
} |
153 |
|
|
|
154 |
|
|
// TODO(joyeecheung): It is somewhat confusing that Class::Initialize |
155 |
|
|
// is used to initialize to the binding, but it is the current convention. |
156 |
|
|
// Rename this across the code base to something that makes more sense. |
157 |
|
5180 |
void NativeModuleEnv::Initialize(Local<Object> target, |
158 |
|
|
Local<Value> unused, |
159 |
|
|
Local<Context> context, |
160 |
|
|
void* priv) { |
161 |
|
5180 |
Environment* env = Environment::GetCurrent(context); |
162 |
|
|
|
163 |
|
|
target |
164 |
|
|
->SetAccessor(env->context(), |
165 |
|
|
env->config_string(), |
166 |
|
|
ConfigStringGetter, |
167 |
|
|
nullptr, |
168 |
|
|
MaybeLocal<Value>(), |
169 |
|
|
DEFAULT, |
170 |
|
|
None, |
171 |
|
20720 |
SideEffectType::kHasNoSideEffect) |
172 |
|
10360 |
.Check(); |
173 |
|
|
target |
174 |
|
|
->SetAccessor(env->context(), |
175 |
|
|
FIXED_ONE_BYTE_STRING(env->isolate(), "moduleIds"), |
176 |
|
|
ModuleIdsGetter, |
177 |
|
|
nullptr, |
178 |
|
|
MaybeLocal<Value>(), |
179 |
|
|
DEFAULT, |
180 |
|
|
None, |
181 |
|
20720 |
SideEffectType::kHasNoSideEffect) |
182 |
|
10360 |
.Check(); |
183 |
|
|
|
184 |
|
|
target |
185 |
|
|
->SetAccessor(env->context(), |
186 |
|
|
FIXED_ONE_BYTE_STRING(env->isolate(), "moduleCategories"), |
187 |
|
|
GetModuleCategories, |
188 |
|
|
nullptr, |
189 |
|
|
env->as_callback_data(), |
190 |
|
|
DEFAULT, |
191 |
|
|
None, |
192 |
|
25900 |
SideEffectType::kHasNoSideEffect) |
193 |
|
10360 |
.Check(); |
194 |
|
|
|
195 |
|
5180 |
env->SetMethod(target, "getCacheUsage", NativeModuleEnv::GetCacheUsage); |
196 |
|
5180 |
env->SetMethod(target, "compileFunction", NativeModuleEnv::CompileFunction); |
197 |
|
|
// internalBinding('native_module') should be frozen |
198 |
|
10360 |
target->SetIntegrityLevel(context, IntegrityLevel::kFrozen).FromJust(); |
199 |
|
5180 |
} |
200 |
|
|
|
201 |
|
|
} // namespace native_module |
202 |
|
|
} // namespace node |
203 |
|
|
|
204 |
|
5030 |
NODE_MODULE_CONTEXT_AWARE_INTERNAL( |
205 |
|
|
native_module, node::native_module::NativeModuleEnv::Initialize) |