From 9d071d96b26654b08b402a3db8e781cef95f1313 Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 16 Jul 2020 12:28:09 -0400 Subject: [PATCH 1/3] util: print External address from inspect Fixes: https://github.com/nodejs/node/issues/28250 --- lib/internal/util/inspect.js | 7 +++++-- src/node_util.cc | 17 +++++++++++++++++ test/parallel/test-util-inspect.js | 4 ++-- 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/lib/internal/util/inspect.js b/lib/internal/util/inspect.js index 13ec60f7773381..b7621dc5d71ac3 100644 --- a/lib/internal/util/inspect.js +++ b/lib/internal/util/inspect.js @@ -63,6 +63,7 @@ const { kRejected, previewEntries, getConstructorName: internalGetConstructorName, + getExternalValue, propertyFilter: { ALL_PROPERTIES, ONLY_ENUMERABLE @@ -977,8 +978,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); + 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 44148ba2b0958a..3571dff5fd671b 100644 --- a/src/node_util.cc +++ b/src/node_util.cc @@ -11,6 +11,7 @@ using v8::Array; using v8::ArrayBufferView; using v8::Boolean; using v8::Context; +using v8::External; using v8::FunctionCallbackInfo; using v8::FunctionTemplate; using v8::Global; @@ -19,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; @@ -68,6 +70,20 @@ static void GetConstructorName( args.GetReturnValue().Set(name); } +static void GetExternalValue( + const FunctionCallbackInfo& args) { + CHECK(args[0]->IsExternal()); + auto isolate = args.GetIsolate(); + Local external = args[0].As(); + + void* ptr = external->Value(); + char val[20]; + snprintf(val, sizeof(val), "%p", ptr); + + MaybeLocal ret = String::NewFromUtf8(isolate, val); + args.GetReturnValue().Set(ret.ToLocalChecked()); +} + static void GetPromiseDetails(const FunctionCallbackInfo& args) { // Return undefined if it's not a Promise. if (!args[0]->IsPromise()) @@ -314,6 +330,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 78184d85f6e9f3..7a8fb32bc1ef9b 100644 --- a/test/parallel/test-util-inspect.js +++ b/test/parallel/test-util-inspect.js @@ -147,8 +147,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: .*?\]$/); { const regexp = /regexp/; From b0f6baacfa619e9f104b0e75f21f6b79ee52cd85 Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 23 Jul 2020 11:07:30 -0400 Subject: [PATCH 2/3] fixup! util: print External address from inspect --- lib/internal/util/inspect.js | 2 +- src/node_util.cc | 11 +++++------ test/parallel/test-util-inspect.js | 2 +- 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/lib/internal/util/inspect.js b/lib/internal/util/inspect.js index b7621dc5d71ac3..102d6f0f153d6a 100644 --- a/lib/internal/util/inspect.js +++ b/lib/internal/util/inspect.js @@ -979,7 +979,7 @@ function formatRaw(ctx, value, recurseTimes, typedArray) { } else { if (keys.length === 0 && protoProps === undefined) { if (isExternal(value)) { - const address = getExternalValue(value); + const address = getExternalValue(value).toString(16); return ctx.stylize(`[External: ${address}]`, 'special'); } return `${getCtxStyle(value, constructor, tag)}{}`; diff --git a/src/node_util.cc b/src/node_util.cc index 3571dff5fd671b..10015a00153a6d 100644 --- a/src/node_util.cc +++ b/src/node_util.cc @@ -9,6 +9,7 @@ namespace util { using v8::ALL_PROPERTIES; using v8::Array; using v8::ArrayBufferView; +using v8::BigInt; using v8::Boolean; using v8::Context; using v8::External; @@ -73,15 +74,13 @@ static void GetConstructorName( static void GetExternalValue( const FunctionCallbackInfo& args) { CHECK(args[0]->IsExternal()); - auto isolate = args.GetIsolate(); + Isolate* isolate = args.GetIsolate(); Local external = args[0].As(); void* ptr = external->Value(); - char val[20]; - snprintf(val, sizeof(val), "%p", ptr); - - MaybeLocal ret = String::NewFromUtf8(isolate, val); - args.GetReturnValue().Set(ret.ToLocalChecked()); + uint64_t value = reinterpret_cast(ptr); + Local ret = BigInt::NewFromUnsigned(isolate, value); + args.GetReturnValue().Set(ret); } static void GetPromiseDetails(const FunctionCallbackInfo& args) { diff --git a/test/parallel/test-util-inspect.js b/test/parallel/test-util-inspect.js index 7a8fb32bc1ef9b..3b0fc186160f24 100644 --- a/test/parallel/test-util-inspect.js +++ b/test/parallel/test-util-inspect.js @@ -148,7 +148,7 @@ assert.strictEqual( ); assert.match(util.inspect((new JSStream())._externalStream), - /^\[External: .*?\]$/); + /^\[External: [0-9a-f]+\]$/); { const regexp = /regexp/; From 880c5b124bd40a7aab17cbe92a5129ca169c92c6 Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 23 Jul 2020 12:11:13 -0400 Subject: [PATCH 3/3] fixup! fixup! util: print External address from inspect --- src/node_util.cc | 1 + 1 file changed, 1 insertion(+) diff --git a/src/node_util.cc b/src/node_util.cc index 10015a00153a6d..3b571180ca9d02 100644 --- a/src/node_util.cc +++ b/src/node_util.cc @@ -286,6 +286,7 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) { registry->Register(PreviewEntries); registry->Register(GetOwnNonIndexProperties); registry->Register(GetConstructorName); + registry->Register(GetExternalValue); registry->Register(Sleep); registry->Register(ArrayBufferViewHasBuffer); registry->Register(WeakReference::New);