Skip to content

Commit b308a07

Browse files
devsnekMylesBorins
authored andcommitted
util: support inspecting namespaces of unevaluated modules
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]>
1 parent fb7a775 commit b308a07

File tree

2 files changed

+64
-11
lines changed

2 files changed

+64
-11
lines changed

lib/util.js

+42-11
Original file line numberDiff line numberDiff line change
@@ -629,6 +629,9 @@ function formatValue(ctx, value, recurseTimes, ln) {
629629
} else {
630630
extra = '[items unknown]';
631631
}
632+
} else if (types.isModuleNamespaceObject(value)) {
633+
braces[0] = `[${tag}] {`;
634+
formatter = formatNamespaceObject;
632635
} else {
633636
// Check boxed primitives other than string with valueOf()
634637
// NOTE: `Date` has to be checked first!
@@ -757,6 +760,15 @@ function formatObject(ctx, value, recurseTimes, keys) {
757760
return output;
758761
}
759762

763+
function formatNamespaceObject(ctx, value, recurseTimes, keys) {
764+
const len = keys.length;
765+
const output = new Array(len);
766+
for (var i = 0; i < len; i++) {
767+
output[i] = formatNamespaceProperty(ctx, value, recurseTimes, keys[i]);
768+
}
769+
return output;
770+
}
771+
760772
// The array is sparse and/or has extra keys
761773
function formatSpecialArray(ctx, value, recurseTimes, keys, maxLength, valLen) {
762774
const output = [];
@@ -980,8 +992,36 @@ function formatPromise(ctx, value, recurseTimes, keys) {
980992
return output;
981993
}
982994

995+
function formatKey(ctx, key, enumerable) {
996+
if (typeof key === 'symbol') {
997+
return `[${ctx.stylize(key.toString(), 'symbol')}]`;
998+
}
999+
if (enumerable === false) {
1000+
return `[${key}]`;
1001+
}
1002+
if (keyStrRegExp.test(key)) {
1003+
return ctx.stylize(key, 'name');
1004+
}
1005+
return ctx.stylize(strEscape(key), 'string');
1006+
}
1007+
1008+
function formatNamespaceProperty(ctx, ns, recurseTimes, key) {
1009+
let value;
1010+
try {
1011+
value = formatValue(ctx, ns[key], recurseTimes, true);
1012+
} catch (err) {
1013+
if (err instanceof ReferenceError) {
1014+
value = ctx.stylize('<uninitialized>', 'special');
1015+
} else {
1016+
throw err;
1017+
}
1018+
}
1019+
1020+
return `${formatKey(ctx, key)}: ${value}`;
1021+
}
1022+
9831023
function formatProperty(ctx, value, recurseTimes, key, array) {
984-
let name, str;
1024+
let str;
9851025
const desc = Object.getOwnPropertyDescriptor(value, key) ||
9861026
{ value: value[key], enumerable: true };
9871027
if (desc.value !== undefined) {
@@ -1003,17 +1043,8 @@ function formatProperty(ctx, value, recurseTimes, key, array) {
10031043
if (array === 1) {
10041044
return str;
10051045
}
1006-
if (typeof key === 'symbol') {
1007-
name = `[${ctx.stylize(key.toString(), 'symbol')}]`;
1008-
} else if (desc.enumerable === false) {
1009-
name = `[${key}]`;
1010-
} else if (keyStrRegExp.test(key)) {
1011-
name = ctx.stylize(key, 'name');
1012-
} else {
1013-
name = ctx.stylize(strEscape(key), 'string');
1014-
}
10151046

1016-
return `${name}: ${str}`;
1047+
return `${formatKey(ctx, key, desc.enumerable)}: ${str}`;
10171048
}
10181049

10191050
function reduceToSingleString(ctx, output, base, braces, addLn) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
'use strict';
2+
3+
// Flags: --experimental-vm-modules
4+
5+
const common = require('../common');
6+
const assert = require('assert');
7+
8+
common.crashOnUnhandledRejection();
9+
10+
const { Module } = require('vm');
11+
const { inspect } = require('util');
12+
13+
(async () => {
14+
const m = new Module('export const a = 1; export var b = 2');
15+
await m.link(() => 0);
16+
m.instantiate();
17+
assert.strictEqual(
18+
inspect(m.namespace),
19+
'[Module] { a: <uninitialized>, b: undefined }');
20+
await m.evaluate();
21+
assert.strictEqual(inspect(m.namespace), '[Module] { a: 1, b: 2 }');
22+
})();

0 commit comments

Comments
 (0)