Skip to content

Commit

Permalink
util: print External address from inspect
Browse files Browse the repository at this point in the history
Fixes: #28250

PR-URL: #34398
Backport-PR-URL: #34583
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Gus Caplan <[email protected]>
Reviewed-By: Juan José Arboleda <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Tobias Nießen <[email protected]>
  • Loading branch information
rosaxxny authored and addaleax committed Sep 22, 2020
1 parent e7252df commit 9027a87
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
7 changes: 5 additions & 2 deletions lib/internal/util/inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ const {
kRejected,
previewEntries,
getConstructorName: internalGetConstructorName,
getExternalValue,
propertyFilter: {
ALL_PROPERTIES,
ONLY_ENUMERABLE
Expand Down Expand Up @@ -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)}{`;
Expand Down
16 changes: 16 additions & 0 deletions src/node_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -67,6 +70,18 @@ static void GetConstructorName(
args.GetReturnValue().Set(name);
}

static void GetExternalValue(
const FunctionCallbackInfo<Value>& args) {
CHECK(args[0]->IsExternal());
Isolate* isolate = args.GetIsolate();
Local<External> external = args[0].As<External>();

void* ptr = external->Value();
uint64_t value = reinterpret_cast<uint64_t>(ptr);
Local<BigInt> ret = BigInt::NewFromUnsigned(isolate, value);
args.GetReturnValue().Set(ret);
}

static void GetPromiseDetails(const FunctionCallbackInfo<Value>& args) {
// Return undefined if it's not a Promise.
if (!args[0]->IsPromise())
Expand Down Expand Up @@ -296,6 +311,7 @@ void Initialize(Local<Object> 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);
Expand Down
4 changes: 2 additions & 2 deletions test/parallel/test-util-inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -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/;
Expand Down

0 comments on commit 9027a87

Please sign in to comment.