Skip to content

Commit 6412d06

Browse files
Matheus Marchinimmarchini
Matheus Marchini
authored andcommittedDec 19, 2018
src: teach llnode how to print Symbol properties names
Closes: nodejs#156
1 parent dbd95d7 commit 6412d06

8 files changed

+57
-2
lines changed
 

‎src/llv8-constants.cc

+6
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,11 @@ void Frame::Load() {
482482
}
483483

484484

485+
void Symbol::Load() {
486+
kNameOffset = LoadConstant("class_Symbol__name__Object");
487+
}
488+
489+
485490
void Types::Load() {
486491
kFirstNonstringType = LoadConstant("FirstNonstringType");
487492
kFirstJSObjectType =
@@ -513,6 +518,7 @@ void Types::Load() {
513518
LoadConstant("type_SharedFunctionInfo__SHARED_FUNCTION_INFO_TYPE");
514519
kScriptType = LoadConstant("type_Script__SCRIPT_TYPE");
515520
kScopeInfoType = LoadConstant("type_ScopeInfo__SCOPE_INFO_TYPE");
521+
kSymbolType = LoadConstant("type_Symbol__SYMBOL_TYPE");
516522

517523
if (kJSAPIObjectType == -1) {
518524
common_->Load();

‎src/llv8-constants.h

+13
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,18 @@ class Frame : public Module {
468468
void Load();
469469
};
470470

471+
472+
class Symbol : public Module {
473+
public:
474+
CONSTANTS_DEFAULT_METHODS(Symbol);
475+
476+
int64_t kNameOffset;
477+
478+
protected:
479+
void Load();
480+
};
481+
482+
471483
class Types : public Module {
472484
public:
473485
CONSTANTS_DEFAULT_METHODS(Types);
@@ -498,6 +510,7 @@ class Types : public Module {
498510
int64_t kSharedFunctionInfoType;
499511
int64_t kScriptType;
500512
int64_t kScopeInfoType;
513+
int64_t kSymbolType;
501514

502515
protected:
503516
void Load();

‎src/llv8-inl.h

+2
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,8 @@ ACCESSOR(Map, MaybeConstructor, map()->kMaybeConstructorOffset, HeapObject)
171171
ACCESSOR(Map, InstanceDescriptors, map()->kInstanceDescriptorsOffset,
172172
HeapObject)
173173

174+
ACCESSOR(Symbol, Name, symbol()->kNameOffset, HeapObject)
175+
174176
inline int64_t Map::BitField3(Error& err) {
175177
return v8()->LoadUnsigned(LeaField(v8()->map()->kBitField3Offset), 4, err);
176178
}

‎src/llv8.cc

+15
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ void LLV8::Load(SBTarget target) {
6060
descriptor_array.Assign(target, &common);
6161
name_dictionary.Assign(target, &common);
6262
frame.Assign(target, &common);
63+
symbol.Assign(target, &common);
6364
types.Assign(target, &common);
6465
}
6566

@@ -566,6 +567,11 @@ std::string HeapObject::ToString(Error& err) {
566567
return str.ToString(err);
567568
}
568569

570+
if (type == v8()->types()->kSymbolType) {
571+
Symbol symbol(this);
572+
return symbol.ToString(err);
573+
}
574+
569575
return "<non-string>";
570576
}
571577

@@ -674,6 +680,15 @@ std::string HeapNumber::ToString(bool whole, Error& err) {
674680
}
675681

676682

683+
std::string Symbol::ToString(Error& err) {
684+
if (!String::IsString(v8(), Name(err), err)) {
685+
return "Symbol()";
686+
}
687+
HeapObject name = Name(err);
688+
return "Symbol('" + String(name).ToString(err) + "')";
689+
}
690+
691+
677692
std::string String::ToString(Error& err) {
678693
int64_t repr = Representation(err);
679694
if (err.Fail()) return std::string();

‎src/llv8.h

+11
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,15 @@ class Map : public HeapObject {
112112
HeapObject Constructor(Error& err);
113113
};
114114

115+
class Symbol : public HeapObject {
116+
public:
117+
V8_VALUE_DEFAULT_METHODS(Symbol, HeapObject)
118+
119+
HeapObject Name(Error& err);
120+
121+
std::string ToString(Error& err);
122+
};
123+
115124
class String : public HeapObject {
116125
public:
117126
V8_VALUE_DEFAULT_METHODS(String, HeapObject)
@@ -513,6 +522,7 @@ class LLV8 {
513522
constants::DescriptorArray descriptor_array;
514523
constants::NameDictionary name_dictionary;
515524
constants::Frame frame;
525+
constants::Symbol symbol;
516526
constants::Types types;
517527

518528
friend class Value;
@@ -547,6 +557,7 @@ class LLV8 {
547557
friend class JSRegExp;
548558
friend class JSDate;
549559
friend class CodeMap;
560+
friend class Symbol;
550561
friend class llnode::Printer;
551562
friend class llnode::FindJSObjectsVisitor;
552563
friend class llnode::FindObjectsCmd;

‎src/printer.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -567,9 +567,9 @@ std::string Printer::Stringify(v8::JSError js_error, Error& err) {
567567
// In the future we can add postmortem metadata on V8 regarding existing
568568
// symbols, but for now we'll use an heuristic to find the stack in the
569569
// error object.
570-
v8::Value maybe_stack = js_error.GetProperty("<non-string>", err);
570+
v8::Value maybe_stack = js_error.GetProperty("Symbol()", err);
571571

572-
if (err.Fail()) {
572+
if (err.Fail() || maybe_stack.raw() == -1) {
573573
Error::PrintInDebugMode(
574574
"Couldn't find a symbol property in the Error object.");
575575
output << rang::fg::yellow << ">" << rang::fg::reset;

‎test/fixtures/inspect-scenario.js

+3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ const zlib = require('zlib');
66

77
let outerVar = 'outer variable';
88

9+
const oneSymbol = Symbol("oneSymbol");
10+
911
exports.holder = {};
1012

1113
function makeThin(a, b) {
@@ -66,6 +68,7 @@ function closure() {
6668
c.hashmap[4] = undefined;
6769
c.hashmap[23] = /regexp/;
6870
c.hashmap[25] = (a,b)=>{a+b};
71+
c.hashmap[oneSymbol] = 42;
6972

7073
let scopedVar = 'scoped value';
7174
let scopedAPI = zlib.createDeflate()._handle;

‎test/plugin/inspect-test.js

+5
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,11 @@ const hashMapTests = {
328328
cb(null);
329329
});
330330
}]
331+
},
332+
// .@@oneSymbol=<Smi: 42>
333+
'symbol': {
334+
re: /.Symbol\('oneSymbol'\)=<Smi: 42>/,
335+
desc: ".Symbol('oneSymbol') Symbol property",
331336
}
332337
};
333338

0 commit comments

Comments
 (0)
Please sign in to comment.