Skip to content

Commit d5e0ada

Browse files
author
Matheus Marchini
committed
src: inspect context objects (Node.js v10.x+)
This patch teaches llnode how to inspect Context objects. This is useful to inspect context variables. PR-URL: #201 Fixes: #211 Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
1 parent 50b3357 commit d5e0ada

File tree

4 files changed

+47
-17
lines changed

4 files changed

+47
-17
lines changed

src/llv8-constants.cc

+4
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,9 @@ void Types::Load() {
487487
kFirstJSObjectType =
488488
LoadConstant("type_JSGlobalObject__JS_GLOBAL_OBJECT_TYPE");
489489

490+
kFirstContextType = LoadConstant("FirstContextType");
491+
kLastContextType = LoadConstant("LastContextType");
492+
490493
kHeapNumberType = LoadConstant("type_HeapNumber__HEAP_NUMBER_TYPE");
491494
kMapType = LoadConstant("type_Map__MAP_TYPE");
492495
kGlobalObjectType =
@@ -508,6 +511,7 @@ void Types::Load() {
508511
kSharedFunctionInfoType =
509512
LoadConstant("type_SharedFunctionInfo__SHARED_FUNCTION_INFO_TYPE");
510513
kScriptType = LoadConstant("type_Script__SCRIPT_TYPE");
514+
kScopeInfoType = LoadConstant("type_ScopeInfo__SCOPE_INFO_TYPE");
511515

512516
if (kJSAPIObjectType == -1) {
513517
common_->Load();

src/llv8-constants.h

+4
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,9 @@ class Types : public Module {
475475
int64_t kFirstNonstringType;
476476
int64_t kFirstJSObjectType;
477477

478+
int64_t kFirstContextType;
479+
int64_t kLastContextType;
480+
478481
int64_t kHeapNumberType;
479482
int64_t kMapType;
480483
int64_t kGlobalObjectType;
@@ -493,6 +496,7 @@ class Types : public Module {
493496
int64_t kJSDateType;
494497
int64_t kSharedFunctionInfoType;
495498
int64_t kScriptType;
499+
int64_t kScopeInfoType;
496500

497501
protected:
498502
void Load();

src/llv8.cc

+31-15
Original file line numberDiff line numberDiff line change
@@ -389,10 +389,15 @@ std::string JSFunction::Inspect(InspectOptions* options, Error& err) {
389389
snprintf(tmp, sizeof(tmp), "\n context=0x%016" PRIx64, context.raw());
390390
res += tmp;
391391

392-
std::string context_str = context.Inspect(err);
393-
if (err.Fail()) return std::string();
392+
{
393+
InspectOptions ctx_options;
394+
ctx_options.detailed = true;
395+
ctx_options.indent_depth = options->indent_depth + 1;
396+
std::string context_str = context.Inspect(&ctx_options, err);
397+
if (err.Fail()) return std::string();
394398

395-
if (!context_str.empty()) res += ":" + context_str;
399+
if (!context_str.empty()) res += ":" + context_str;
400+
}
396401

397402
if (options->print_source) {
398403
SharedFunctionInfo info = Info(err);
@@ -821,6 +826,12 @@ std::string HeapObject::Inspect(InspectOptions* options, Error& err) {
821826
return pre + str.Inspect(options, err);
822827
}
823828

829+
if (type >= v8()->types()->kFirstContextType &&
830+
type <= v8()->types()->kLastContextType) {
831+
Context ctx(this);
832+
return pre + ctx.Inspect(options, err);
833+
}
834+
824835
if (type == v8()->types()->kFixedArrayType) {
825836
FixedArray arr(this);
826837
return pre + arr.Inspect(options, err);
@@ -1060,13 +1071,19 @@ HeapObject Context::GetScopeInfo(Error& err) {
10601071
return info.GetScopeInfo(err);
10611072
}
10621073

1063-
std::string Context::Inspect(Error& err) {
1074+
std::string Context::Inspect(InspectOptions* options, Error& err) {
10641075
// Not enough postmortem information, return bare minimum
10651076
if (v8()->shared_info()->kScopeInfoOffset == -1 &&
10661077
v8()->shared_info()->kNameOrScopeInfoOffset == -1)
10671078
return std::string();
10681079

1069-
std::string res = "<Context: {\n";
1080+
std::string res = "<Context";
1081+
1082+
if (!options->detailed) {
1083+
return res + ">";
1084+
}
1085+
1086+
res += ": {\n";
10701087

10711088
Value previous = Previous(err);
10721089
if (err.Fail()) return std::string();
@@ -1083,12 +1100,10 @@ std::string Context::Inspect(Error& err) {
10831100
Smi local_count_smi = scope.ContextLocalCount(err);
10841101
if (err.Fail()) return std::string();
10851102

1086-
InspectOptions options;
1087-
10881103
HeapObject heap_previous = HeapObject(previous);
10891104
if (heap_previous.Check()) {
10901105
char tmp[128];
1091-
snprintf(tmp, sizeof(tmp), " (previous)=0x%016" PRIx64, previous.raw());
1106+
snprintf(tmp, sizeof(tmp), (options->get_indent_spaces() + "(previous)=0x%016" PRIx64).c_str(), previous.raw());
10921107
res += std::string(tmp) + ":<Context>,";
10931108
}
10941109

@@ -1098,16 +1113,16 @@ std::string Context::Inspect(Error& err) {
10981113
JSFunction closure = Closure(err);
10991114
if (err.Fail()) return std::string();
11001115
char tmp[128];
1101-
snprintf(tmp, sizeof(tmp), " (closure)=0x%016" PRIx64 " {",
1116+
snprintf(tmp, sizeof(tmp), (options->get_indent_spaces() + "(closure)=0x%016" PRIx64 " {").c_str(),
11021117
closure.raw());
11031118
res += tmp;
11041119

1105-
InspectOptions options;
1106-
res += closure.Inspect(&options, err) + "}";
1120+
InspectOptions closure_options;
1121+
res += closure.Inspect(&closure_options, err) + "}";
11071122
if (err.Fail()) return std::string();
11081123
} else {
11091124
char tmp[128];
1110-
snprintf(tmp, sizeof(tmp), " (scope_info)=0x%016" PRIx64,
1125+
snprintf(tmp, sizeof(tmp), (options->get_indent_spaces() + "(scope_info)=0x%016" PRIx64).c_str(),
11111126
scope.raw());
11121127

11131128
res += std::string(tmp) + ":<ScopeInfo";
@@ -1131,17 +1146,18 @@ std::string Context::Inspect(Error& err) {
11311146

11321147
if (!res.empty()) res += ",\n";
11331148

1134-
res += " " + name.ToString(err) + "=";
1149+
res += options->get_indent_spaces() + name.ToString(err) + "=";
11351150
if (err.Fail()) return std::string();
11361151

11371152
Value value = ContextSlot(i, err);
11381153
if (err.Fail()) return std::string();
11391154

1140-
res += value.Inspect(&options, err);
1155+
InspectOptions val_options;
1156+
res += value.Inspect(&val_options, err);
11411157
if (err.Fail()) return std::string();
11421158
}
11431159

1144-
return res + " }>";
1160+
return res + "}>";
11451161
}
11461162

11471163

src/llv8.h

+8-2
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,20 @@ class Value {
4444
: detailed(false),
4545
print_map(false),
4646
print_source(false),
47-
length(kLength) {}
47+
length(kLength),
48+
indent_depth(1) {}
4849

4950
static const unsigned int kLength = 16;
51+
static const unsigned int kIndentSize = 2;
52+
inline std::string get_indent_spaces() {
53+
return std::string(indent_depth * kIndentSize, ' ');
54+
}
5055

5156
bool detailed;
5257
bool print_map;
5358
bool print_source;
5459
unsigned int length;
60+
unsigned int indent_depth;
5561
};
5662

5763
Value(const Value& v) = default;
@@ -390,7 +396,7 @@ class Context : public FixedArray {
390396
inline T GetEmbedderData(int64_t index, Error& err);
391397
inline Value ContextSlot(int index, Error& err);
392398

393-
std::string Inspect(Error& err);
399+
std::string Inspect(InspectOptions *options, Error& err);
394400

395401
private:
396402
inline JSFunction Closure(Error& err);

0 commit comments

Comments
 (0)