Skip to content

Commit

Permalink
util: support inspecting namespaces of unevaluated modules
Browse files Browse the repository at this point in the history
PR-URL: #20782
Reviewed-By: Ben Noordhuis <[email protected]>
Reviewed-By: Guy Bedford <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
Reviewed-By: James M Snell <[email protected]>
  • Loading branch information
devsnek committed May 19, 2018
1 parent 70cc5da commit 064057b
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 11 deletions.
53 changes: 42 additions & 11 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,9 @@ function formatValue(ctx, value, recurseTimes, ln) {
} else {
extra = '[items unknown]';
}
} else if (types.isModuleNamespaceObject(value)) {
braces[0] = `[${tag}] {`;
formatter = formatNamespaceObject;
} else {
// Check boxed primitives other than string with valueOf()
// NOTE: `Date` has to be checked first!
Expand Down Expand Up @@ -757,6 +760,15 @@ function formatObject(ctx, value, recurseTimes, keys) {
return output;
}

function formatNamespaceObject(ctx, value, recurseTimes, keys) {
const len = keys.length;
const output = new Array(len);
for (var i = 0; i < len; i++) {
output[i] = formatNamespaceProperty(ctx, value, recurseTimes, keys[i]);
}
return output;
}

// The array is sparse and/or has extra keys
function formatSpecialArray(ctx, value, recurseTimes, keys, maxLength, valLen) {
const output = [];
Expand Down Expand Up @@ -980,8 +992,36 @@ function formatPromise(ctx, value, recurseTimes, keys) {
return output;
}

function formatKey(ctx, key, enumerable) {
if (typeof key === 'symbol') {
return `[${ctx.stylize(key.toString(), 'symbol')}]`;
}
if (enumerable === false) {
return `[${key}]`;
}
if (keyStrRegExp.test(key)) {
return ctx.stylize(key, 'name');
}
return ctx.stylize(strEscape(key), 'string');
}

function formatNamespaceProperty(ctx, ns, recurseTimes, key) {
let value;
try {
value = formatValue(ctx, ns[key], recurseTimes, true);
} catch (err) {
if (err instanceof ReferenceError) {
value = ctx.stylize('<uninitialized>', 'special');
} else {
throw err;
}
}

return `${formatKey(ctx, key)}: ${value}`;
}

function formatProperty(ctx, value, recurseTimes, key, array) {
let name, str;
let str;
const desc = Object.getOwnPropertyDescriptor(value, key) ||
{ value: value[key], enumerable: true };
if (desc.value !== undefined) {
Expand All @@ -1003,17 +1043,8 @@ function formatProperty(ctx, value, recurseTimes, key, array) {
if (array === 1) {
return str;
}
if (typeof key === 'symbol') {
name = `[${ctx.stylize(key.toString(), 'symbol')}]`;
} else if (desc.enumerable === false) {
name = `[${key}]`;
} else if (keyStrRegExp.test(key)) {
name = ctx.stylize(key, 'name');
} else {
name = ctx.stylize(strEscape(key), 'string');
}

return `${name}: ${str}`;
return `${formatKey(ctx, key, desc.enumerable)}: ${str}`;
}

function reduceToSingleString(ctx, output, base, braces, addLn) {
Expand Down
22 changes: 22 additions & 0 deletions test/parallel/test-util-inspect-namespace.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict';

// Flags: --experimental-vm-modules

const common = require('../common');
const assert = require('assert');

common.crashOnUnhandledRejection();

const { Module } = require('vm');
const { inspect } = require('util');

(async () => {
const m = new Module('export const a = 1; export var b = 2');
await m.link(() => 0);
m.instantiate();
assert.strictEqual(
inspect(m.namespace),
'[Module] { a: <uninitialized>, b: undefined }');
await m.evaluate();
assert.strictEqual(inspect(m.namespace), '[Module] { a: 1, b: 2 }');
})();

0 comments on commit 064057b

Please sign in to comment.