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
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 Jul 28, 2020
1 parent 53870dd commit fe2a7f0
Show file tree
Hide file tree
Showing 3 changed files with 24 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 @@ -63,6 +63,7 @@ const {
kRejected,
previewEntries,
getConstructorName: internalGetConstructorName,
getExternalValue,
propertyFilter: {
ALL_PROPERTIES,
ONLY_ENUMERABLE
Expand Down Expand Up @@ -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).toString(16);
return ctx.stylize(`[External: ${address}]`, 'special');
}
return `${getCtxStyle(value, constructor, tag)}{}`;
}
braces[0] = `${getCtxStyle(value, constructor, tag)}{`;
Expand Down
17 changes: 17 additions & 0 deletions src/node_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,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 @@ -19,6 +21,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 @@ -68,6 +71,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 @@ -271,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);
Expand Down Expand Up @@ -314,6 +330,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 @@ -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: [0-9a-f]+\]$/);

{
const regexp = /regexp/;
Expand Down

0 comments on commit fe2a7f0

Please sign in to comment.