From 9027a87f621c0a2fd013419aca45f634040dbfb8 Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 16 Jul 2020 12:28:09 -0400 Subject: [PATCH] util: print External address from inspect MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes: https://github.com/nodejs/node/issues/28250 PR-URL: https://github.com/nodejs/node/pull/34398 Backport-PR-URL: https://github.com/nodejs/node/pull/34583 Reviewed-By: Anna Henningsen Reviewed-By: Gus Caplan Reviewed-By: Juan José Arboleda Reviewed-By: James M Snell Reviewed-By: Tobias Nießen --- lib/internal/util/inspect.js | 7 +++++-- src/node_util.cc | 16 ++++++++++++++++ test/parallel/test-util-inspect.js | 4 ++-- 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/lib/internal/util/inspect.js b/lib/internal/util/inspect.js index fdd70802a8e55e..de164f28ef5814 100644 --- a/lib/internal/util/inspect.js +++ b/lib/internal/util/inspect.js @@ -60,6 +60,7 @@ const { kRejected, previewEntries, getConstructorName: internalGetConstructorName, + getExternalValue, propertyFilter: { ALL_PROPERTIES, ONLY_ENUMERABLE @@ -938,8 +939,10 @@ function formatRaw(ctx, value, recurseTimes, typedArray) { } } else { if (keys.length === 0 && protoProps === undefined) { - if (isExternal(value)) - return ctx.stylize('[External]', 'special'); + if (isExternal(value)) { + const address = getExternalValue(value).toString(16); + return ctx.stylize(`[External: ${address}]`, 'special'); + } return `${getCtxStyle(value, constructor, tag)}{}`; } braces[0] = `${getCtxStyle(value, constructor, tag)}{`; diff --git a/src/node_util.cc b/src/node_util.cc index eb956daac6c782..35bf5beb72c350 100644 --- a/src/node_util.cc +++ b/src/node_util.cc @@ -8,8 +8,10 @@ namespace util { using v8::ALL_PROPERTIES; using v8::Array; using v8::ArrayBufferView; +using v8::BigInt; using v8::Boolean; using v8::Context; +using v8::External; using v8::FunctionCallbackInfo; using v8::FunctionTemplate; using v8::Global; @@ -18,6 +20,7 @@ using v8::Integer; using v8::Isolate; using v8::KeyCollectionMode; using v8::Local; +using v8::MaybeLocal; using v8::Object; using v8::ONLY_CONFIGURABLE; using v8::ONLY_ENUMERABLE; @@ -67,6 +70,18 @@ static void GetConstructorName( args.GetReturnValue().Set(name); } +static void GetExternalValue( + const FunctionCallbackInfo& args) { + CHECK(args[0]->IsExternal()); + Isolate* isolate = args.GetIsolate(); + Local external = args[0].As(); + + void* ptr = external->Value(); + uint64_t value = reinterpret_cast(ptr); + Local ret = BigInt::NewFromUnsigned(isolate, value); + args.GetReturnValue().Set(ret); +} + static void GetPromiseDetails(const FunctionCallbackInfo& args) { // Return undefined if it's not a Promise. if (!args[0]->IsPromise()) @@ -296,6 +311,7 @@ void Initialize(Local target, env->SetMethodNoSideEffect(target, "getOwnNonIndexProperties", GetOwnNonIndexProperties); env->SetMethodNoSideEffect(target, "getConstructorName", GetConstructorName); + env->SetMethodNoSideEffect(target, "getExternalValue", GetExternalValue); env->SetMethod(target, "sleep", Sleep); env->SetMethod(target, "arrayBufferViewHasBuffer", ArrayBufferViewHasBuffer); diff --git a/test/parallel/test-util-inspect.js b/test/parallel/test-util-inspect.js index 2c7a3a96fa93d2..383f592bca4132 100644 --- a/test/parallel/test-util-inspect.js +++ b/test/parallel/test-util-inspect.js @@ -146,8 +146,8 @@ assert.strictEqual( "[String: 'hello'] { [length]: 5, [Symbol(foo)]: 123 }" ); -assert.strictEqual(util.inspect((new JSStream())._externalStream), - '[External]'); +assert.match(util.inspect((new JSStream())._externalStream), + /^\[External: [0-9a-f]+\]$/); { const regexp = /regexp/;