Skip to content

Commit b308a07

Browse files
devsnekMylesBorins
authored andcommitted
util: support inspecting namespaces of unevaluated modules
PR-URL: #20782 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Guy Bedford <guybedford@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent fb7a775 commit b308a07

2 files changed

Lines changed: 64 additions & 11 deletions

File tree

‎lib/util.js‎

Lines changed: 42 additions & 11 deletions
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+
}elseif(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
returnoutput;
758761
}
759762

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

995+
functionformatKey(ctx,key,enumerable){
996+
if(typeofkey==='symbol'){
997+
return`[${ctx.stylize(key.toString(),'symbol')}]`;
998+
}
999+
if(enumerable===false){
1000+
return`[${key}]`;
1001+
}
1002+
if(keyStrRegExp.test(key)){
1003+
returnctx.stylize(key,'name');
1004+
}
1005+
returnctx.stylize(strEscape(key),'string');
1006+
}
1007+
1008+
functionformatNamespaceProperty(ctx,ns,recurseTimes,key){
1009+
letvalue;
1010+
try{
1011+
value=formatValue(ctx,ns[key],recurseTimes,true);
1012+
}catch(err){
1013+
if(errinstanceofReferenceError){
1014+
value=ctx.stylize('<uninitialized>','special');
1015+
}else{
1016+
throwerr;
1017+
}
1018+
}
1019+
1020+
return`${formatKey(ctx,key)}: ${value}`;
1021+
}
1022+
9831023
functionformatProperty(ctx,value,recurseTimes,key,array){
984-
letname,str;
1024+
letstr;
9851025
constdesc=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
returnstr;
10051045
}
1006-
if(typeofkey==='symbol'){
1007-
name=`[${ctx.stylize(key.toString(),'symbol')}]`;
1008-
}elseif(desc.enumerable===false){
1009-
name=`[${key}]`;
1010-
}elseif(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
functionreduceToSingleString(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+
constcommon=require('../common');
6+
constassert=require('assert');
7+
8+
common.crashOnUnhandledRejection();
9+
10+
const{ Module }=require('vm');
11+
const{ inspect }=require('util');
12+
13+
(async()=>{
14+
constm=newModule('export const a = 1; export var b = 2');
15+
awaitm.link(()=>0);
16+
m.instantiate();
17+
assert.strictEqual(
18+
inspect(m.namespace),
19+
'[Module] { a: <uninitialized>, b: undefined }');
20+
awaitm.evaluate();
21+
assert.strictEqual(inspect(m.namespace),'[Module] { a: 1, b: 2 }');
22+
})();

0 commit comments

Comments
 (0)