1 |
|
|
#include "node_errors.h" |
2 |
|
|
#include "util-inl.h" |
3 |
|
|
#include "base_object-inl.h" |
4 |
|
|
|
5 |
|
|
namespace node { |
6 |
|
|
namespace util { |
7 |
|
|
|
8 |
|
|
using v8::ALL_PROPERTIES; |
9 |
|
|
using v8::Array; |
10 |
|
|
using v8::ArrayBufferView; |
11 |
|
|
using v8::Boolean; |
12 |
|
|
using v8::Context; |
13 |
|
|
using v8::Function; |
14 |
|
|
using v8::FunctionCallbackInfo; |
15 |
|
|
using v8::FunctionTemplate; |
16 |
|
|
using v8::Global; |
17 |
|
|
using v8::IndexFilter; |
18 |
|
|
using v8::Integer; |
19 |
|
|
using v8::Isolate; |
20 |
|
|
using v8::KeyCollectionMode; |
21 |
|
|
using v8::Local; |
22 |
|
|
using v8::Object; |
23 |
|
|
using v8::ONLY_CONFIGURABLE; |
24 |
|
|
using v8::ONLY_ENUMERABLE; |
25 |
|
|
using v8::ONLY_WRITABLE; |
26 |
|
|
using v8::Private; |
27 |
|
|
using v8::Promise; |
28 |
|
|
using v8::PropertyFilter; |
29 |
|
|
using v8::Proxy; |
30 |
|
|
using v8::SKIP_STRINGS; |
31 |
|
|
using v8::SKIP_SYMBOLS; |
32 |
|
|
using v8::String; |
33 |
|
|
using v8::Uint32; |
34 |
|
|
using v8::Value; |
35 |
|
|
|
36 |
|
139949 |
static void GetOwnNonIndexProperties( |
37 |
|
|
const FunctionCallbackInfo<Value>& args) { |
38 |
|
139949 |
Environment* env = Environment::GetCurrent(args); |
39 |
|
139949 |
Local<Context> context = env->context(); |
40 |
|
|
|
41 |
✗✓ |
279898 |
CHECK(args[0]->IsObject()); |
42 |
✗✓ |
279898 |
CHECK(args[1]->IsUint32()); |
43 |
|
|
|
44 |
|
279898 |
Local<Object> object = args[0].As<Object>(); |
45 |
|
|
|
46 |
|
|
Local<Array> properties; |
47 |
|
|
|
48 |
|
|
PropertyFilter filter = |
49 |
|
419847 |
static_cast<PropertyFilter>(args[1].As<Uint32>()->Value()); |
50 |
|
|
|
51 |
✓✓ |
279898 |
if (!object->GetPropertyNames( |
52 |
|
|
context, KeyCollectionMode::kOwnOnly, |
53 |
|
|
filter, |
54 |
|
139949 |
IndexFilter::kSkipIndices) |
55 |
|
419847 |
.ToLocal(&properties)) { |
56 |
|
139951 |
return; |
57 |
|
|
} |
58 |
|
279894 |
args.GetReturnValue().Set(properties); |
59 |
|
|
} |
60 |
|
|
|
61 |
|
954 |
static void GetConstructorName( |
62 |
|
|
const FunctionCallbackInfo<Value>& args) { |
63 |
✗✓ |
1908 |
CHECK(args[0]->IsObject()); |
64 |
|
|
|
65 |
|
1908 |
Local<Object> object = args[0].As<Object>(); |
66 |
|
954 |
Local<String> name = object->GetConstructorName(); |
67 |
|
|
|
68 |
|
1908 |
args.GetReturnValue().Set(name); |
69 |
|
954 |
} |
70 |
|
|
|
71 |
|
61 |
static void GetPromiseDetails(const FunctionCallbackInfo<Value>& args) { |
72 |
|
|
// Return undefined if it's not a Promise. |
73 |
✗✓ |
122 |
if (!args[0]->IsPromise()) |
74 |
|
61 |
return; |
75 |
|
|
|
76 |
|
61 |
auto isolate = args.GetIsolate(); |
77 |
|
|
|
78 |
|
122 |
Local<Promise> promise = args[0].As<Promise>(); |
79 |
|
|
|
80 |
|
61 |
int state = promise->State(); |
81 |
✓✓ |
183 |
Local<Value> values[2] = { Integer::New(isolate, state) }; |
82 |
|
61 |
size_t number_of_values = 1; |
83 |
✓✓ |
61 |
if (state != Promise::PromiseState::kPending) |
84 |
|
100 |
values[number_of_values++] = promise->Result(); |
85 |
|
61 |
Local<Array> ret = Array::New(isolate, values, number_of_values); |
86 |
|
122 |
args.GetReturnValue().Set(ret); |
87 |
|
|
} |
88 |
|
|
|
89 |
|
205281 |
static void GetProxyDetails(const FunctionCallbackInfo<Value>& args) { |
90 |
|
|
// Return undefined if it's not a proxy. |
91 |
✓✓ |
410562 |
if (!args[0]->IsProxy()) |
92 |
|
410462 |
return; |
93 |
|
|
|
94 |
|
200 |
Local<Proxy> proxy = args[0].As<Proxy>(); |
95 |
|
|
|
96 |
|
|
Local<Value> ret[] = { |
97 |
|
100 |
proxy->GetTarget(), |
98 |
|
100 |
proxy->GetHandler() |
99 |
|
300 |
}; |
100 |
|
|
|
101 |
|
|
args.GetReturnValue().Set( |
102 |
|
400 |
Array::New(args.GetIsolate(), ret, arraysize(ret))); |
103 |
|
|
} |
104 |
|
|
|
105 |
|
122 |
static void PreviewEntries(const FunctionCallbackInfo<Value>& args) { |
106 |
✗✓ |
244 |
if (!args[0]->IsObject()) |
107 |
|
|
return; |
108 |
|
|
|
109 |
|
122 |
Environment* env = Environment::GetCurrent(args); |
110 |
|
|
bool is_key_value; |
111 |
|
|
Local<Array> entries; |
112 |
✗✓ |
488 |
if (!args[0].As<Object>()->PreviewEntries(&is_key_value).ToLocal(&entries)) |
113 |
|
|
return; |
114 |
|
|
// Fast path for WeakMap and WeakSet. |
115 |
✓✓ |
122 |
if (args.Length() == 1) |
116 |
|
16 |
return args.GetReturnValue().Set(entries); |
117 |
|
|
|
118 |
|
|
Local<Value> ret[] = { |
119 |
|
|
entries, |
120 |
|
|
Boolean::New(env->isolate(), is_key_value) |
121 |
|
342 |
}; |
122 |
|
|
return args.GetReturnValue().Set( |
123 |
|
342 |
Array::New(env->isolate(), ret, arraysize(ret))); |
124 |
|
|
} |
125 |
|
|
|
126 |
|
135 |
inline Local<Private> IndexToPrivateSymbol(Environment* env, uint32_t index) { |
127 |
|
|
#define V(name, _) &Environment::name, |
128 |
|
|
static Local<Private> (Environment::*const methods[])() const = { |
129 |
|
|
PER_ISOLATE_PRIVATE_SYMBOL_PROPERTIES(V) |
130 |
|
|
}; |
131 |
|
|
#undef V |
132 |
✗✓ |
135 |
CHECK_LT(index, arraysize(methods)); |
133 |
✓✗ |
135 |
return (env->*methods[index])(); |
134 |
|
|
} |
135 |
|
|
|
136 |
|
129 |
static void GetHiddenValue(const FunctionCallbackInfo<Value>& args) { |
137 |
|
129 |
Environment* env = Environment::GetCurrent(args); |
138 |
|
|
|
139 |
✗✓ |
258 |
CHECK(args[0]->IsObject()); |
140 |
✗✓ |
258 |
CHECK(args[1]->IsUint32()); |
141 |
|
|
|
142 |
|
258 |
Local<Object> obj = args[0].As<Object>(); |
143 |
|
516 |
auto index = args[1]->Uint32Value(env->context()).FromJust(); |
144 |
|
129 |
auto private_symbol = IndexToPrivateSymbol(env, index); |
145 |
|
258 |
auto maybe_value = obj->GetPrivate(env->context(), private_symbol); |
146 |
|
|
|
147 |
|
258 |
args.GetReturnValue().Set(maybe_value.ToLocalChecked()); |
148 |
|
129 |
} |
149 |
|
|
|
150 |
|
6 |
static void SetHiddenValue(const FunctionCallbackInfo<Value>& args) { |
151 |
|
6 |
Environment* env = Environment::GetCurrent(args); |
152 |
|
|
|
153 |
✗✓ |
12 |
CHECK(args[0]->IsObject()); |
154 |
✗✓ |
12 |
CHECK(args[1]->IsUint32()); |
155 |
|
|
|
156 |
|
12 |
Local<Object> obj = args[0].As<Object>(); |
157 |
|
24 |
auto index = args[1]->Uint32Value(env->context()).FromJust(); |
158 |
|
6 |
auto private_symbol = IndexToPrivateSymbol(env, index); |
159 |
|
12 |
auto maybe_value = obj->SetPrivate(env->context(), private_symbol, args[2]); |
160 |
|
|
|
161 |
|
18 |
args.GetReturnValue().Set(maybe_value.FromJust()); |
162 |
|
6 |
} |
163 |
|
|
|
164 |
|
56 |
void ArrayBufferViewHasBuffer(const FunctionCallbackInfo<Value>& args) { |
165 |
✗✓ |
112 |
CHECK(args[0]->IsArrayBufferView()); |
166 |
|
280 |
args.GetReturnValue().Set(args[0].As<ArrayBufferView>()->HasBuffer()); |
167 |
|
56 |
} |
168 |
|
|
|
169 |
✗✓ |
894 |
class WeakReference : public BaseObject { |
170 |
|
|
public: |
171 |
|
322 |
WeakReference(Environment* env, Local<Object> object, Local<Object> target) |
172 |
|
644 |
: BaseObject(env, object) { |
173 |
|
322 |
MakeWeak(); |
174 |
|
322 |
target_.Reset(env->isolate(), target); |
175 |
|
322 |
target_.SetWeak(); |
176 |
|
322 |
} |
177 |
|
|
|
178 |
|
322 |
static void New(const FunctionCallbackInfo<Value>& args) { |
179 |
|
322 |
Environment* env = Environment::GetCurrent(args); |
180 |
✗✓ |
322 |
CHECK(args.IsConstructCall()); |
181 |
✗✓ |
644 |
CHECK(args[0]->IsObject()); |
182 |
|
966 |
new WeakReference(env, args.This(), args[0].As<Object>()); |
183 |
|
322 |
} |
184 |
|
|
|
185 |
|
2004 |
static void Get(const FunctionCallbackInfo<Value>& args) { |
186 |
|
2004 |
WeakReference* weak_ref = Unwrap<WeakReference>(args.Holder()); |
187 |
|
2004 |
Isolate* isolate = args.GetIsolate(); |
188 |
✓✓ |
4008 |
if (!weak_ref->target_.IsEmpty()) |
189 |
|
6009 |
args.GetReturnValue().Set(weak_ref->target_.Get(isolate)); |
190 |
|
2004 |
} |
191 |
|
|
|
192 |
|
1002 |
static void IncRef(const FunctionCallbackInfo<Value>& args) { |
193 |
|
1002 |
WeakReference* weak_ref = Unwrap<WeakReference>(args.Holder()); |
194 |
|
1002 |
weak_ref->reference_count_++; |
195 |
✗✓ |
3006 |
if (weak_ref->target_.IsEmpty()) return; |
196 |
✓✓ |
1002 |
if (weak_ref->reference_count_ == 1) weak_ref->target_.ClearWeak(); |
197 |
|
|
} |
198 |
|
|
|
199 |
|
1000 |
static void DecRef(const FunctionCallbackInfo<Value>& args) { |
200 |
|
1000 |
WeakReference* weak_ref = Unwrap<WeakReference>(args.Holder()); |
201 |
✗✓ |
1000 |
CHECK_GE(weak_ref->reference_count_, 1); |
202 |
|
1000 |
weak_ref->reference_count_--; |
203 |
✗✓ |
3000 |
if (weak_ref->target_.IsEmpty()) return; |
204 |
✓✓ |
1000 |
if (weak_ref->reference_count_ == 0) weak_ref->target_.SetWeak(); |
205 |
|
|
} |
206 |
|
|
|
207 |
|
|
SET_MEMORY_INFO_NAME(WeakReference) |
208 |
|
|
SET_SELF_SIZE(WeakReference) |
209 |
|
|
SET_NO_MEMORY_INFO() |
210 |
|
|
|
211 |
|
|
private: |
212 |
|
|
Global<Object> target_; |
213 |
|
|
uint64_t reference_count_ = 0; |
214 |
|
|
}; |
215 |
|
|
|
216 |
|
5476 |
static void GuessHandleType(const FunctionCallbackInfo<Value>& args) { |
217 |
|
5476 |
Environment* env = Environment::GetCurrent(args); |
218 |
|
|
int fd; |
219 |
✗✓ |
27380 |
if (!args[0]->Int32Value(env->context()).To(&fd)) return; |
220 |
✗✓ |
5476 |
CHECK_GE(fd, 0); |
221 |
|
|
|
222 |
|
5476 |
uv_handle_type t = uv_guess_handle(fd); |
223 |
|
5476 |
const char* type = nullptr; |
224 |
|
|
|
225 |
✓✓✓✓ ✓✓✗ |
5476 |
switch (t) { |
226 |
|
|
case UV_TCP: |
227 |
|
4 |
type = "TCP"; |
228 |
|
4 |
break; |
229 |
|
|
case UV_TTY: |
230 |
|
31 |
type = "TTY"; |
231 |
|
31 |
break; |
232 |
|
|
case UV_UDP: |
233 |
|
6 |
type = "UDP"; |
234 |
|
6 |
break; |
235 |
|
|
case UV_FILE: |
236 |
|
2570 |
type = "FILE"; |
237 |
|
2570 |
break; |
238 |
|
|
case UV_NAMED_PIPE: |
239 |
|
2860 |
type = "PIPE"; |
240 |
|
2860 |
break; |
241 |
|
|
case UV_UNKNOWN_HANDLE: |
242 |
|
5 |
type = "UNKNOWN"; |
243 |
|
5 |
break; |
244 |
|
|
default: |
245 |
|
|
ABORT(); |
246 |
|
|
} |
247 |
|
|
|
248 |
|
16428 |
args.GetReturnValue().Set(OneByteString(env->isolate(), type)); |
249 |
|
|
} |
250 |
|
|
|
251 |
|
5208 |
void Initialize(Local<Object> target, |
252 |
|
|
Local<Value> unused, |
253 |
|
|
Local<Context> context, |
254 |
|
|
void* priv) { |
255 |
|
5208 |
Environment* env = Environment::GetCurrent(context); |
256 |
|
|
|
257 |
|
|
#define V(name, _) \ |
258 |
|
|
target->Set(context, \ |
259 |
|
|
FIXED_ONE_BYTE_STRING(env->isolate(), #name), \ |
260 |
|
|
Integer::NewFromUnsigned(env->isolate(), index++)).Check(); |
261 |
|
|
{ |
262 |
|
5208 |
uint32_t index = 0; |
263 |
|
114576 |
PER_ISOLATE_PRIVATE_SYMBOL_PROPERTIES(V) |
264 |
|
|
} |
265 |
|
|
#undef V |
266 |
|
|
|
267 |
|
|
#define V(name) \ |
268 |
|
|
target->Set(context, \ |
269 |
|
|
FIXED_ONE_BYTE_STRING(env->isolate(), #name), \ |
270 |
|
|
Integer::New(env->isolate(), Promise::PromiseState::name)) \ |
271 |
|
|
.FromJust() |
272 |
|
20832 |
V(kPending); |
273 |
|
20832 |
V(kFulfilled); |
274 |
|
20832 |
V(kRejected); |
275 |
|
|
#undef V |
276 |
|
|
|
277 |
|
5208 |
env->SetMethodNoSideEffect(target, "getHiddenValue", GetHiddenValue); |
278 |
|
5208 |
env->SetMethod(target, "setHiddenValue", SetHiddenValue); |
279 |
|
5208 |
env->SetMethodNoSideEffect(target, "getPromiseDetails", GetPromiseDetails); |
280 |
|
5208 |
env->SetMethodNoSideEffect(target, "getProxyDetails", GetProxyDetails); |
281 |
|
5208 |
env->SetMethodNoSideEffect(target, "previewEntries", PreviewEntries); |
282 |
|
|
env->SetMethodNoSideEffect(target, "getOwnNonIndexProperties", |
283 |
|
5208 |
GetOwnNonIndexProperties); |
284 |
|
5208 |
env->SetMethodNoSideEffect(target, "getConstructorName", GetConstructorName); |
285 |
|
|
|
286 |
|
5208 |
env->SetMethod(target, "arrayBufferViewHasBuffer", ArrayBufferViewHasBuffer); |
287 |
|
5208 |
Local<Object> constants = Object::New(env->isolate()); |
288 |
|
20832 |
NODE_DEFINE_CONSTANT(constants, ALL_PROPERTIES); |
289 |
|
20832 |
NODE_DEFINE_CONSTANT(constants, ONLY_WRITABLE); |
290 |
|
20832 |
NODE_DEFINE_CONSTANT(constants, ONLY_ENUMERABLE); |
291 |
|
20832 |
NODE_DEFINE_CONSTANT(constants, ONLY_CONFIGURABLE); |
292 |
|
20832 |
NODE_DEFINE_CONSTANT(constants, SKIP_STRINGS); |
293 |
|
20832 |
NODE_DEFINE_CONSTANT(constants, SKIP_SYMBOLS); |
294 |
|
|
target->Set(context, |
295 |
|
|
FIXED_ONE_BYTE_STRING(env->isolate(), "propertyFilter"), |
296 |
|
15624 |
constants).Check(); |
297 |
|
|
|
298 |
|
|
Local<String> should_abort_on_uncaught_toggle = |
299 |
|
5208 |
FIXED_ONE_BYTE_STRING(env->isolate(), "shouldAbortOnUncaughtToggle"); |
300 |
✗✓ |
20832 |
CHECK(target |
301 |
|
|
->Set(env->context(), |
302 |
|
|
should_abort_on_uncaught_toggle, |
303 |
|
|
env->should_abort_on_uncaught_toggle().GetJSArray()) |
304 |
|
|
.FromJust()); |
305 |
|
|
|
306 |
|
|
Local<String> weak_ref_string = |
307 |
|
5208 |
FIXED_ONE_BYTE_STRING(env->isolate(), "WeakReference"); |
308 |
|
|
Local<FunctionTemplate> weak_ref = |
309 |
|
5208 |
env->NewFunctionTemplate(WeakReference::New); |
310 |
|
10416 |
weak_ref->InstanceTemplate()->SetInternalFieldCount(1); |
311 |
|
5208 |
weak_ref->SetClassName(weak_ref_string); |
312 |
|
5208 |
env->SetProtoMethod(weak_ref, "get", WeakReference::Get); |
313 |
|
5208 |
env->SetProtoMethod(weak_ref, "incRef", WeakReference::IncRef); |
314 |
|
5208 |
env->SetProtoMethod(weak_ref, "decRef", WeakReference::DecRef); |
315 |
|
|
target->Set(context, weak_ref_string, |
316 |
|
15624 |
weak_ref->GetFunction(context).ToLocalChecked()).Check(); |
317 |
|
|
|
318 |
|
5208 |
env->SetMethod(target, "guessHandleType", GuessHandleType); |
319 |
|
5208 |
} |
320 |
|
|
|
321 |
|
|
} // namespace util |
322 |
|
|
} // namespace node |
323 |
|
|
|
324 |
|
5058 |
NODE_MODULE_CONTEXT_AWARE_INTERNAL(util, node::util::Initialize) |