Skip to content

Commit

Permalink
src: teach llnode how to print Symbol properties names
Browse files Browse the repository at this point in the history
Closes: #156
  • Loading branch information
Matheus Marchini committed Jul 12, 2018
1 parent baf4cae commit 9d031f3
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/llv8-constants.cc
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,11 @@ void Frame::Load() {
}


void Symbol::Load() {
kNameOffset = LoadConstant("class_Symbol__name__Object");
}


void Types::Load() {
kFirstNonstringType = LoadConstant("FirstNonstringType");
kFirstJSObjectType =
Expand All @@ -507,6 +512,7 @@ void Types::Load() {
kSharedFunctionInfoType =
LoadConstant("type_SharedFunctionInfo__SHARED_FUNCTION_INFO_TYPE");
kScriptType = LoadConstant("type_Script__SCRIPT_TYPE");
kSymbolType = LoadConstant("type_Symbol__SYMBOL_TYPE");

if (kJSAPIObjectType == -1) {
common_->Load();
Expand Down
13 changes: 13 additions & 0 deletions src/llv8-constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,18 @@ class Frame : public Module {
void Load();
};


class Symbol : public Module {
public:
CONSTANTS_DEFAULT_METHODS(Symbol);

int64_t kNameOffset;

protected:
void Load();
};


class Types : public Module {
public:
CONSTANTS_DEFAULT_METHODS(Types);
Expand All @@ -483,6 +495,7 @@ class Types : public Module {
int64_t kJSDateType;
int64_t kSharedFunctionInfoType;
int64_t kScriptType;
int64_t kSymbolType;

protected:
void Load();
Expand Down
2 changes: 2 additions & 0 deletions src/llv8-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ ACCESSOR(Map, MaybeConstructor, map()->kMaybeConstructorOffset, HeapObject)
ACCESSOR(Map, InstanceDescriptors, map()->kInstanceDescriptorsOffset,
HeapObject)

ACCESSOR(Symbol, Name, symbol()->kNameOffset, HeapObject)

inline int64_t Map::BitField3(Error& err) {
return v8()->LoadUnsigned(LeaField(v8()->map()->kBitField3Offset), 4, err);
}
Expand Down
15 changes: 15 additions & 0 deletions src/llv8.cc
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ void LLV8::Load(SBTarget target) {
descriptor_array.Assign(target, &common);
name_dictionary.Assign(target, &common);
frame.Assign(target, &common);
symbol.Assign(target, &common);
types.Assign(target, &common);
}

Expand Down Expand Up @@ -757,6 +758,11 @@ std::string HeapObject::ToString(Error& err) {
return str.ToString(err);
}

if (type == v8()->types()->kSymbolType) {
Symbol symbol(this);
return symbol.ToString(err);
}

return "<non-string>";
}

Expand Down Expand Up @@ -955,6 +961,15 @@ std::string HeapNumber::Inspect(Error& err) {
}


std::string Symbol::ToString(Error& err) {
if (!String::IsString(v8(), Name(err), err)) {
return "<unnamed-symbol>";
}
HeapObject name = Name(err);
return "@@" + String(name).ToString(err);
}


std::string String::ToString(Error& err) {
int64_t repr = Representation(err);
if (err.Fail()) return std::string();
Expand Down
11 changes: 11 additions & 0 deletions src/llv8.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,15 @@ class Map : public HeapObject {
HeapObject Constructor(Error& err);
};

class Symbol : public HeapObject {
public:
V8_VALUE_DEFAULT_METHODS(Symbol, HeapObject)

HeapObject Name(Error& err);

std::string ToString(Error& err);
};

class String : public HeapObject {
public:
V8_VALUE_DEFAULT_METHODS(String, HeapObject)
Expand Down Expand Up @@ -511,6 +520,7 @@ class LLV8 {
constants::DescriptorArray descriptor_array;
constants::NameDictionary name_dictionary;
constants::Frame frame;
constants::Symbol symbol;
constants::Types types;

friend class Value;
Expand Down Expand Up @@ -544,6 +554,7 @@ class LLV8 {
friend class JSRegExp;
friend class JSDate;
friend class CodeMap;
friend class Symbol;
friend class llnode::FindJSObjectsVisitor;
friend class llnode::FindObjectsCmd;
friend class llnode::FindReferencesCmd;
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/inspect-scenario.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ const zlib = require('zlib');

let outerVar = 'outer variable';

const oneSymbol = Symbol("oneSymbol");

exports.holder = {};

function makeThin(a, b) {
Expand Down Expand Up @@ -61,6 +63,7 @@ function closure() {
c.hashmap[4] = undefined;
c.hashmap[23] = /regexp/;
c.hashmap[25] = (a,b)=>{a+b};
c.hashmap[oneSymbol] = 42;

let scopedVar = 'scoped value';
let scopedAPI = zlib.createDeflate()._handle;
Expand Down
7 changes: 6 additions & 1 deletion test/plugin/inspect-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ const hashMapTests = {
const arrowSource = 'source:\n' +
'function c.hashmap.(anonymous function)(a,b)=>{a+b}\n' +
'>';

t.ok(lines.includes(arrowSource),
'hashmap[25] should have the correct function source');
cb(null);
Expand Down Expand Up @@ -300,6 +300,11 @@ const hashMapTests = {
cb(null);
});
}]
},
// .@@oneSymbol=<Smi: 42>
'symbol': {
re: /.@@oneSymbol=<Smi: 42>/,
desc: '.@@oneSymbol Symbol property',
}
};

Expand Down

0 comments on commit 9d031f3

Please sign in to comment.