Skip to content

Commit f46649b

Browse files
rosaxxnyDanielle Adams
authored and
Danielle Adams
committed
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 d910672 commit f46649b

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
@@ -63,6 +63,7 @@ const {
6363
kRejected,
6464
previewEntries,
6565
getConstructorName: internalGetConstructorName,
66+
getExternalValue,
6667
propertyFilter: {
6768
ALL_PROPERTIES,
6869
ONLY_ENUMERABLE
@@ -977,8 +978,10 @@ function formatRaw(ctx, value, recurseTimes, typedArray) {
977978
}
978979
} else {
979980
if (keys.length === 0 && protoProps === undefined) {
980-
if (isExternal(value))
981-
return ctx.stylize('[External]', 'special');
981+
if (isExternal(value)) {
982+
const address = getExternalValue(value).toString(16);
983+
return ctx.stylize(`[External: ${address}]`, 'special');
984+
}
982985
return `${getCtxStyle(value, constructor, tag)}{}`;
983986
}
984987
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
@@ -147,8 +147,8 @@ assert.strictEqual(
147147
"[String: 'hello'] { [length]: 5, [Symbol(foo)]: 123 }"
148148
);
149149

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

153153
{
154154
const regexp = /regexp/;

0 commit comments

Comments
 (0)