Skip to content

Commit

Permalink
src: fix context inpection for V8 6.8
Browse files Browse the repository at this point in the history
V8 6.8 replaces Closure inside Context with ScopeInfo. Those are minimal
changes to adopt llnode to the new behavior.

Ref: https://chromium-review.googlesource.com/#/c/785151/
Fixes: nodejs#193

PR-URL: nodejs#201
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
  • Loading branch information
Matheus Marchini authored and hyj1991 committed Sep 3, 2018
1 parent 5001f99 commit fa74421
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 19 deletions.
5 changes: 3 additions & 2 deletions src/llv8-constants.cc
Original file line number Diff line number Diff line change
Expand Up @@ -244,8 +244,9 @@ void ScopeInfo::Load() {


void Context::Load() {
kClosureIndex =
LoadConstant("class_Context__closure_index__int", "context_idx_closure");
kClosureIndex = LoadConstant("class_Context__closure_index__int",
"context_idx_closure", -1);
kScopeInfoIndex = LoadConstant("context_idx_scope_info", -1);
kPreviousIndex =
LoadConstant("class_Context__previous_index__int", "context_idx_prev");
// TODO (mmarchini) change LoadConstant to accept variable arguments, a list
Expand Down
10 changes: 10 additions & 0 deletions src/llv8-constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -199,12 +199,22 @@ class Context : public Module {
CONSTANTS_DEFAULT_METHODS(Context);

int64_t kClosureIndex;
int64_t kScopeInfoIndex;
int64_t kGlobalObjectIndex;
int64_t kPreviousIndex;
int64_t kNativeIndex;
int64_t kEmbedderDataIndex;
int64_t kMinContextSlots;

inline bool hasClosure() {
// NOTE (mmarchini): V8 6.8 replaced the closure field (which was a
// JSFunction) with a scope_info field (which is a ScopeInfo). The change
// made it easier to get the scope info for a context, but removed our
// ability to get the outer function for a given context. We can still get
// the outer context through the previous field though.
return kClosureIndex != -1;
}

protected:
void Load();
};
Expand Down
51 changes: 38 additions & 13 deletions src/llv8.cc
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,7 @@ std::string JSFunction::Inspect(InspectOptions* options, Error& err) {
std::string context_str = context.Inspect(err);
if (err.Fail()) return std::string();

if (!context_str.empty()) res += "{\n" + context_str + "}";
if (!context_str.empty()) res += ":" + context_str;

if (options->print_source) {
SharedFunctionInfo info = Info(err);
Expand Down Expand Up @@ -1752,24 +1752,31 @@ std::string FixedArray::InspectContents(int length, Error& err) {
return res;
}

HeapObject Context::GetScopeInfo(Error& err) {
if (v8()->context()->kScopeInfoIndex != -1) {
return FixedArray::Get<HeapObject>(v8()->context()->kScopeInfoIndex, err);
}
JSFunction closure = Closure(err);
if (err.Fail()) return HeapObject();

SharedFunctionInfo info = closure.Info(err);
if (err.Fail()) return HeapObject();

return info.GetScopeInfo(err);
}

std::string Context::Inspect(Error& err) {
std::string res;
// Not enough postmortem information, return bare minimum
if (v8()->shared_info()->kScopeInfoOffset == -1 &&
v8()->shared_info()->kNameOrScopeInfoOffset == -1)
return res;
return std::string();

Value previous = Previous(err);
if (err.Fail()) return std::string();
std::string res = "<Context: {\n";

JSFunction closure = Closure(err);
if (err.Fail()) return std::string();

SharedFunctionInfo info = closure.Info(err);
Value previous = Previous(err);
if (err.Fail()) return std::string();

HeapObject scope_obj = info.GetScopeInfo(err);
HeapObject scope_obj = GetScopeInfo(err);
if (err.Fail()) return std::string();

ScopeInfo scope(scope_obj);
Expand All @@ -1787,11 +1794,14 @@ std::string Context::Inspect(Error& err) {
if (heap_previous.Check()) {
char tmp[128];
snprintf(tmp, sizeof(tmp), " (previous)=0x%016" PRIx64, previous.raw());
res += tmp;
res += std::string(tmp) + ":<Context>,";
}

if (!res.empty()) res += "\n";
{

if (v8()->context()->hasClosure()) {
JSFunction closure = Closure(err);
if (err.Fail()) return std::string();
char tmp[128];
snprintf(tmp, sizeof(tmp), " (closure)=0x%016" PRIx64 " {",
closure.raw());
Expand All @@ -1800,6 +1810,21 @@ std::string Context::Inspect(Error& err) {
InspectOptions options;
res += closure.Inspect(&options, err) + "}";
if (err.Fail()) return std::string();
} else {
char tmp[128];
snprintf(tmp, sizeof(tmp), " (scope_info)=0x%016" PRIx64,
scope.raw());

res += std::string(tmp) + ":<ScopeInfo";

Error function_name_error;
HeapObject maybe_function_name = scope.MaybeFunctionName(function_name_error);

if (function_name_error.Success()) {
res += ": for function " + String(maybe_function_name).ToString(err);
}

res += ">";
}

int param_count = param_count_smi.GetValue();
Expand All @@ -1821,7 +1846,7 @@ std::string Context::Inspect(Error& err) {
if (err.Fail()) return std::string();
}

return res;
return res + " }>";
}

context_t* Context::InspectX(Error& err) {
Expand Down
5 changes: 4 additions & 1 deletion src/llv8.h
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ class Context : public FixedArray {
public:
V8_VALUE_DEFAULT_METHODS(Context, FixedArray)

inline JSFunction Closure(Error& err);
inline HeapObject GetScopeInfo(Error& err);
inline Value Previous(Error& err);
inline Value Native(Error& err);
inline bool IsNative(Error& err);
Expand All @@ -447,6 +447,9 @@ class Context : public FixedArray {

std::string Inspect(Error& err);
context_t* InspectX(Error& err);

private:
inline JSFunction Closure(Error& err);
};

class ScopeInfo : public FixedArray {
Expand Down
14 changes: 11 additions & 3 deletions test/plugin/inspect-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -308,14 +308,22 @@ const hashMapTests = {

const contextTests = {
'previous': {
re: /\(previous\)/,
re: /\(previous\)=(0x[0-9a-f]+)[^\n]+/,
desc: '.(previous)'
},
'closure': {
re: /\(closure\)=(0x[0-9a-f]+)[^\n]+function: closure/i,
re: /(\((?:closure|scope_info)\)=0x[0-9a-f]+)[^\n]+/i,
desc: '.(closure)',
validator(t, sess, addresses, name, cb) {
const address = addresses[name];
const type = addresses[name].split("=")[0];
let address = undefined;
if (type === "(closure)") {
address = addresses[name].split("=")[1];
} else if (type === "(scope_info)") {
address = addresses["previous"];
} else {
return cb(new Error("unknown field"));
}
sess.send(`v8 inspect ${address}`);
sess.linesUntil(/}>/, (err, lines) => {
if (err) return cb(err);
Expand Down

0 comments on commit fa74421

Please sign in to comment.