From 70cc5da0f11a024cf5be1ff20fd885556c1d2153 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Mon, 14 May 2018 17:30:27 +0200 Subject: 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 Reviewed-By: Gus Caplan Reviewed-By: James M Snell Reviewed-By: Matteo Collina Reviewed-By: Ruben Bridgewater --- src/node_util.cc | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) (limited to 'src/node_util.cc') 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& args) { args.GetReturnValue().Set(ret); } +static void PreviewEntries(const FunctionCallbackInfo& args) { + if (!args[0]->IsObject()) + return; + + bool is_key_value; + Local entries; + if (!args[0].As()->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 = env->context(); + + Local pairs = Array::New(env->isolate(), length / 2); + for (uint32_t i = 0; i < length / 2; i++) { + Local 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& args) { auto context = args.GetIsolate()->GetCurrentContext(); @@ -188,6 +217,7 @@ void Initialize(Local 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); -- cgit v1.2.3