Skip to content

Commit d065be3

Browse files
committed
util: support inspecting namespaces of unevaluated modules
1 parent 1e98787 commit d065be3

File tree

2 files changed

+64
-11
lines changed

2 files changed

+64
-11
lines changed

lib/util.js

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -638,6 +638,9 @@ function formatValue(ctx, value, recurseTimes, ln) {
638638
} else {
639639
extra = '[items unknown]';
640640
}
641+
} else if (types.isModuleNamespaceObject(value)) {
642+
braces[0] = `[${tag}] {`;
643+
formatter = formatNamespaceObject;
641644
} else {
642645
// Check boxed primitives other than string with valueOf()
643646
// NOTE: `Date` has to be checked first!
@@ -766,6 +769,15 @@ function formatObject(ctx, value, recurseTimes, keys) {
766769
return output;
767770
}
768771

772+
function formatNamespaceObject(ctx, value, recurseTimes, keys) {
773+
const len = keys.length;
774+
const output = new Array(len);
775+
for (var i = 0; i < len; i++) {
776+
output[i] = formatNamespaceProperty(ctx, value, recurseTimes, keys[i]);
777+
}
778+
return output;
779+
}
780+
769781
// The array is sparse and/or has extra keys
770782
function formatSpecialArray(ctx, value, recurseTimes, keys, maxLength, valLen) {
771783
const output = [];
@@ -993,8 +1005,36 @@ function formatPromise(ctx, value, recurseTimes, keys) {
9931005
return output;
9941006
}
9951007

1008+
function formatKey(ctx, key, enumerable) {
1009+
if (typeof key === 'symbol') {
1010+
return `[${ctx.stylize(key.toString(), 'symbol')}]`;
1011+
}
1012+
if (enumerable === false) {
1013+
return `[${key}]`;
1014+
}
1015+
if (keyStrRegExp.test(key)) {
1016+
return ctx.stylize(key, 'name');
1017+
}
1018+
return ctx.stylize(strEscape(key), 'string');
1019+
}
1020+
1021+
function formatNamespaceProperty(ctx, ns, recurseTimes, key) {
1022+
let value;
1023+
try {
1024+
value = formatValue(ctx, ns[key], recurseTimes, true);
1025+
} catch (err) {
1026+
if (err instanceof ReferenceError) {
1027+
value = ctx.stylize('<uninitialized>', 'special');
1028+
} else {
1029+
throw err;
1030+
}
1031+
}
1032+
1033+
return `${formatKey(ctx, key)}: ${value}`;
1034+
}
1035+
9961036
function formatProperty(ctx, value, recurseTimes, key, array) {
997-
let name, str;
1037+
let str;
9981038
const desc = Object.getOwnPropertyDescriptor(value, key) ||
9991039
{ value: value[key], enumerable: true };
10001040
if (desc.value !== undefined) {
@@ -1016,17 +1056,8 @@ function formatProperty(ctx, value, recurseTimes, key, array) {
10161056
if (array === 1) {
10171057
return str;
10181058
}
1019-
if (typeof key === 'symbol') {
1020-
name = `[${ctx.stylize(key.toString(), 'symbol')}]`;
1021-
} else if (desc.enumerable === false) {
1022-
name = `[${key}]`;
1023-
} else if (keyStrRegExp.test(key)) {
1024-
name = ctx.stylize(key, 'name');
1025-
} else {
1026-
name = ctx.stylize(strEscape(key), 'string');
1027-
}
10281059

1029-
return `${name}: ${str}`;
1060+
return `${formatKey(ctx, key, desc.enumerable)}: ${str}`;
10301061
}
10311062

10321063
function reduceToSingleString(ctx, output, base, braces, addLn) {
Lines changed: 22 additions & 0 deletions
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)