Skip to content

Commit

Permalink
fixup! src: add node's internals constants
Browse files Browse the repository at this point in the history
  • Loading branch information
Matheus Marchini committed Jun 14, 2018
1 parent b814884 commit b58b51d
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 28 deletions.
2 changes: 1 addition & 1 deletion src/llnode.cc
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ bool BacktraceCmd::DoExecute(SBDebugger d, char** cmd,
res.c_str());
continue;
} else {
v8::Error::PrintInDebugMode("%s", err.GetMessage());
Error::PrintInDebugMode("%s", err.GetMessage());
}
}

Expand Down
18 changes: 18 additions & 0 deletions src/llv8-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,24 @@ inline Value Context::Native(Error& err) {
return FixedArray::Get<Value>(v8()->context()->kNativeIndex, err);
}

inline bool Context::IsNative(Error& err) {
Value native = Native(err);
if (err.Fail()) {
return false;
}
return native.raw() == raw();
}

template <class T>
inline T Context::GetEmbedderData(int64_t index, Error& err) {
FixedArray embedder_data = FixedArray(*this).Get<FixedArray>(
v8()->context()->kEmbedderDataIndex, err);
if (err.Fail()) {
return T();
}
return embedder_data.Get<T>(index, err);
}

inline Value Context::ContextSlot(int index, Error& err) {
return FixedArray::Get<Value>(v8()->context()->kMinContextSlots + index, err);
}
Expand Down
3 changes: 3 additions & 0 deletions src/llv8.h
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,9 @@ class Context : public FixedArray {
inline JSFunction Closure(Error& err);
inline Value Previous(Error& err);
inline Value Native(Error& err);
inline bool IsNative(Error& err);
template <class T>
inline T GetEmbedderData(int64_t index, Error& err);
inline Value ContextSlot(int index, Error& err);

std::string Inspect(Error& err);
Expand Down
37 changes: 11 additions & 26 deletions src/node-constants.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ addr_t Environment::LoadCurrentEnvironment(Error& err) {
err = Error::Failure("Missing Node's embedder data index");
return 0;
}
addr_t currentEnvironment = 0;
addr_t current_environment = 0;
SBProcess process = target_.GetProcess();
SBThread thread = process.GetSelectedThread();
if (!thread.IsValid()) {
Expand All @@ -40,13 +40,6 @@ addr_t Environment::LoadCurrentEnvironment(Error& err) {

llv8()->Load(target_);

SBStream desc;
if (!thread.GetDescription(desc)) {
err = Error::Failure("Couldn't get thread description");
return 0;
}
SBFrame selected_frame = thread.GetSelectedFrame();

uint32_t num_frames = thread.GetNumFrames();

// Heuristically finds the native context and gets the Environment from its
Expand Down Expand Up @@ -75,13 +68,10 @@ addr_t Environment::LoadCurrentEnvironment(Error& err) {
}
visited_contexts.insert(val.raw());
v8::Context context(val);
v8::Value native = context.Native(v8_err);
if (v8_err.Success()) {
if (native.raw() == context.raw()) {
found = true;
currentEnvironment = CurrentEnvironmentFromContext(native, err);
break;
}
if (context.IsNative(err)) {
found = true;
current_environment = CurrentEnvironmentFromContext(context, err);
break;
}

val = context.Previous(v8_err);
Expand All @@ -95,29 +85,24 @@ addr_t Environment::LoadCurrentEnvironment(Error& err) {
}
}

if (!currentEnvironment) {
if (!current_environment) {
err =
Error::Failure("Couldn't find the Environemnt from the native context");
}

return currentEnvironment;
return current_environment;
}

addr_t Environment::CurrentEnvironmentFromContext(v8::Value context,
addr_t Environment::CurrentEnvironmentFromContext(v8::Context context,
Error& err) {
llv8()->Load(target_);

v8::FixedArray contextArray = v8::FixedArray(context);
v8::FixedArray embed = contextArray.Get<v8::FixedArray>(
llv8()->context()->kEmbedderDataIndex, err);
if (err.Fail()) {
return 0;
}
v8::Smi encodedEnv = embed.Get<v8::Smi>(kEnvContextEmbedderDataIndex, err);
v8::Smi environment =
context.GetEmbedderData<v8::Smi>(kEnvContextEmbedderDataIndex, err);
if (err.Fail()) {
return 0;
}
return encodedEnv.raw();
return environment.raw();
}


Expand Down
2 changes: 1 addition & 1 deletion src/node-constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class Environment : public Module {

private:
addr_t LoadCurrentEnvironment(Error& err);
addr_t CurrentEnvironmentFromContext(v8::Value context, Error& err);
addr_t CurrentEnvironmentFromContext(v8::Context context, Error& err);
};

class ReqWrapQueue : public Module {
Expand Down

0 comments on commit b58b51d

Please sign in to comment.