diff options
author | Anna Henningsen <anna@addaleax.net> | 2018-05-14 17:30:27 +0200 |
---|---|---|
committer | Anna Henningsen <anna@addaleax.net> | 2018-05-19 01:02:14 +0200 |
commit | 70cc5da0f11a024cf5be1ff20fd885556c1d2153 (patch) | |
tree | 0c6e6293015808b5dc89c21934372edd56a784c8 /src/node_util.cc | |
parent | 143a2f8d67a81154bdb2849999837fb0a5c13d8e (diff) | |
download | android-node-v8-70cc5da0f11a024cf5be1ff20fd885556c1d2153.tar.gz android-node-v8-70cc5da0f11a024cf5be1ff20fd885556c1d2153.tar.bz2 android-node-v8-70cc5da0f11a024cf5be1ff20fd885556c1d2153.zip |
lib,src: use V8 API for collection inspection
Use a new public V8 API for inspecting weak collections and
collection iterators, rather than using V8-internal functions
to achieve this. This currently comes with a slight modification of
the output for inspecting iterators generated by `Set().entries()`.
Fixes: https://github.com/nodejs/node/issues/20409
PR-URL: https://github.com/nodejs/node/pull/20719
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: Gus Caplan <me@gus.host>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Diffstat (limited to 'src/node_util.cc')
-rw-r--r-- | src/node_util.cc | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/src/node_util.cc b/src/node_util.cc index 662809fb85..2db6858645 100644 --- a/src/node_util.cc +++ b/src/node_util.cc @@ -49,6 +49,35 @@ static void GetProxyDetails(const FunctionCallbackInfo<Value>& args) { args.GetReturnValue().Set(ret); } +static void PreviewEntries(const FunctionCallbackInfo<Value>& args) { + if (!args[0]->IsObject()) + return; + + bool is_key_value; + Local<Array> entries; + if (!args[0].As<Object>()->PreviewEntries(&is_key_value).ToLocal(&entries)) + return; + if (!is_key_value) + return args.GetReturnValue().Set(entries); + + uint32_t length = entries->Length(); + CHECK_EQ(length % 2, 0); + + Environment* env = Environment::GetCurrent(args); + Local<Context> context = env->context(); + + Local<Array> pairs = Array::New(env->isolate(), length / 2); + for (uint32_t i = 0; i < length / 2; i++) { + Local<Array> pair = Array::New(env->isolate(), 2); + pair->Set(context, 0, entries->Get(context, i * 2).ToLocalChecked()) + .FromJust(); + pair->Set(context, 1, entries->Get(context, i * 2 + 1).ToLocalChecked()) + .FromJust(); + pairs->Set(context, i, pair).FromJust(); + } + args.GetReturnValue().Set(pairs); +} + // Side effect-free stringification that will never throw exceptions. static void SafeToString(const FunctionCallbackInfo<Value>& args) { auto context = args.GetIsolate()->GetCurrentContext(); @@ -188,6 +217,7 @@ void Initialize(Local<Object> target, env->SetMethod(target, "getPromiseDetails", GetPromiseDetails); env->SetMethod(target, "getProxyDetails", GetProxyDetails); env->SetMethod(target, "safeToString", SafeToString); + env->SetMethod(target, "previewEntries", PreviewEntries); env->SetMethod(target, "startSigintWatchdog", StartSigintWatchdog); env->SetMethod(target, "stopSigintWatchdog", StopSigintWatchdog); |