Skip to content

Commit 9027a87

Browse files
rosaxxnyaddaleax
authored andcommitted
util: print External address from inspect
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]>
1 parent e7252df commit 9027a87

File tree

3 files changed

+23
-4
lines changed

3 files changed

+23
-4
lines changed

lib/internal/util/inspect.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ const {
6060
kRejected,
6161
previewEntries,
6262
getConstructorName: internalGetConstructorName,
63+
getExternalValue,
6364
propertyFilter: {
6465
ALL_PROPERTIES,
6566
ONLY_ENUMERABLE
@@ -938,8 +939,10 @@ function formatRaw(ctx, value, recurseTimes, typedArray) {
938939
}
939940
} else {
940941
if (keys.length === 0 && protoProps === undefined) {
941-
if (isExternal(value))
942-
return ctx.stylize('[External]', 'special');
942+
if (isExternal(value)) {
943+
const address = getExternalValue(value).toString(16);
944+
return ctx.stylize(`[External: ${address}]`, 'special');
945+
}
943946
return `${getCtxStyle(value, constructor, tag)}{}`;
944947
}
945948
braces[0] = `${getCtxStyle(value, constructor, tag)}{`;

src/node_util.cc

+16
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ namespace util {
88
using v8::ALL_PROPERTIES;
99
using v8::Array;
1010
using v8::ArrayBufferView;
11+
using v8::BigInt;
1112
using v8::Boolean;
1213
using v8::Context;
14+
using v8::External;
1315
using v8::FunctionCallbackInfo;
1416
using v8::FunctionTemplate;
1517
using v8::Global;
@@ -18,6 +20,7 @@ using v8::Integer;
1820
using v8::Isolate;
1921
using v8::KeyCollectionMode;
2022
using v8::Local;
23+
using v8::MaybeLocal;
2124
using v8::Object;
2225
using v8::ONLY_CONFIGURABLE;
2326
using v8::ONLY_ENUMERABLE;
@@ -67,6 +70,18 @@ static void GetConstructorName(
6770
args.GetReturnValue().Set(name);
6871
}
6972

73+
static void GetExternalValue(
74+
const FunctionCallbackInfo<Value>& args) {
75+
CHECK(args[0]->IsExternal());
76+
Isolate* isolate = args.GetIsolate();
77+
Local<External> external = args[0].As<External>();
78+
79+
void* ptr = external->Value();
80+
uint64_t value = reinterpret_cast<uint64_t>(ptr);
81+
Local<BigInt> ret = BigInt::NewFromUnsigned(isolate, value);
82+
args.GetReturnValue().Set(ret);
83+
}
84+
7085
static void GetPromiseDetails(const FunctionCallbackInfo<Value>& args) {
7186
// Return undefined if it's not a Promise.
7287
if (!args[0]->IsPromise())
@@ -296,6 +311,7 @@ void Initialize(Local<Object> target,
296311
env->SetMethodNoSideEffect(target, "getOwnNonIndexProperties",
297312
GetOwnNonIndexProperties);
298313
env->SetMethodNoSideEffect(target, "getConstructorName", GetConstructorName);
314+
env->SetMethodNoSideEffect(target, "getExternalValue", GetExternalValue);
299315
env->SetMethod(target, "sleep", Sleep);
300316

301317
env->SetMethod(target, "arrayBufferViewHasBuffer", ArrayBufferViewHasBuffer);

test/parallel/test-util-inspect.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,8 @@ assert.strictEqual(
146146
"[String: 'hello'] { [length]: 5, [Symbol(foo)]: 123 }"
147147
);
148148

149-
assert.strictEqual(util.inspect((new JSStream())._externalStream),
150-
'[External]');
149+
assert.match(util.inspect((new JSStream())._externalStream),
150+
/^\[External: [0-9a-f]+\]$/);
151151

152152
{
153153
const regexp = /regexp/;

0 commit comments

Comments
 (0)