diff options
author | Anna Henningsen <anna@addaleax.net> | 2018-05-14 17:30:27 +0200 |
---|---|---|
committer | Myles Borins <mylesborins@google.com> | 2018-05-22 14:13:28 -0400 |
commit | fb7a77524246feada872674b34bef80f4f7e649e (patch) | |
tree | b28f63fab9eeda7b4b7fa7934c53ba3cc7049ab5 /src/node_util.cc | |
parent | e56716e39614248a70b2031d903bb9115b038442 (diff) | |
download | node-new-fb7a77524246feada872674b34bef80f4f7e649e.tar.gz |
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); |