diff options
author | Anna Henningsen <anna@addaleax.net> | 2018-05-19 00:55:54 +0200 |
---|---|---|
committer | Ruben Bridgewater <ruben@bridgewater.de> | 2018-07-16 10:46:09 +0200 |
commit | db495896249b29a93d8013b5ee2067ecf87ed081 (patch) | |
tree | fbd6aef6130b6e23535ae48dd285ae73452d69ae /src/node_util.cc | |
parent | df97126173918ad589c5ceb234204f66d0c5afac (diff) | |
download | android-node-v8-db495896249b29a93d8013b5ee2067ecf87ed081.tar.gz android-node-v8-db495896249b29a93d8013b5ee2067ecf87ed081.tar.bz2 android-node-v8-db495896249b29a93d8013b5ee2067ecf87ed081.zip |
console,util: avoid pair array generation in C++
Use a plain `[key, value, key, value]`-style list instead
of an array of pairs for inspecting collections.
This also fixes a bug with `console.table()` where
inspecting a non-key-value `MapIterator` would have
led to odd results.
PR-URL: https://github.com/nodejs/node/pull/20831
Refs: https://github.com/nodejs/node/pull/20719#discussion_r189342513
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: Gus Caplan <me@gus.host>
Reviewed-By: Minwoo Jung <minwoo@nodesource.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Diffstat (limited to 'src/node_util.cc')
-rw-r--r-- | src/node_util.cc | 25 |
1 files changed, 6 insertions, 19 deletions
diff --git a/src/node_util.cc b/src/node_util.cc index 724bb3603c..ef7dc8a818 100644 --- a/src/node_util.cc +++ b/src/node_util.cc @@ -53,29 +53,16 @@ static void PreviewEntries(const FunctionCallbackInfo<Value>& args) { if (!args[0]->IsObject()) return; + Environment* env = Environment::GetCurrent(args); 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); + Local<Array> ret = Array::New(env->isolate(), 2); + ret->Set(env->context(), 0, entries).FromJust(); + ret->Set(env->context(), 1, v8::Boolean::New(env->isolate(), is_key_value)) + .FromJust(); + return args.GetReturnValue().Set(ret); } // Side effect-free stringification that will never throw exceptions. |