Skip to content
Prev Previous commit
Next Next commit
contextAttached
  • Loading branch information
TimothyGu committed Jul 17, 2017
commit 0352a48f8f8e8e6cb5bf158694b4ed9dc3c2b560
9 changes: 8 additions & 1 deletion lib/inspector.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const {
connect,
open,
url,
contextAttached: _contextAttached,
attachContext: _attachContext,
detachContext: _detachContext
} = process.binding('inspector');
Expand Down Expand Up @@ -126,11 +127,17 @@ function detachContext(sandbox) {
_detachContext(sandbox);
}

function contextAttached(sandbox) {
checkSandbox(sandbox);
return _contextAttached(sandbox);
}

module.exports = {
open: (port, host, wait) => open(port, host, !!wait),
close: process._debugEnd,
url: url,
Session,
attachContext,
detachContext
detachContext,
contextAttached
};
37 changes: 32 additions & 5 deletions src/inspector_agent.cc
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,28 @@ void AttachContext(const v8::FunctionCallbackInfo<v8::Value>& args) {
env->inspector_agent()->ContextCreated(info);
}

void ContextAttached(const v8::FunctionCallbackInfo<v8::Value>& args) {
Environment* env = Environment::GetCurrent(args);
if (!args[0]->IsObject()) {
env->ThrowTypeError("sandbox must be an object");
return;
}
Local<Object> sandbox = args[0].As<Object>();
ContextifyContext* contextify_context =
ContextifyContext::ContextFromContextifiedSandbox(env, sandbox);
if (contextify_context == nullptr) {
return env->ThrowTypeError(
"sandbox argument must have been converted to a context.");
}

if (contextify_context->context().IsEmpty())
return;

args.GetReturnValue().Set(
env->inspector_agent()->ContextRegistered(
contextify_context->context()));
}

void DetachContext(const v8::FunctionCallbackInfo<v8::Value>& args) {
Environment* env = Environment::GetCurrent(args);
if (!args[0]->IsObject()) {
Expand Down Expand Up @@ -621,14 +643,18 @@ Agent::Agent(Environment* env) : parent_env_(env),
Agent::~Agent() {
}

bool Agent::ContextCreated(const node::inspector::ContextInfo* info) {
auto isolate = parent_env_->isolate();
bool Agent::ContextRegistered(Local<Context> context) {
auto it = std::find_if(
contexts_.begin(), contexts_.end(),
[&] (const node::inspector::ContextInfo*& cur) {
return cur->context(isolate) == info->context(isolate);
[&] (const node::inspector::ContextInfo*& info) {
return info->context(parent_env_->isolate()) == context;
});
if (it != contexts_.end()) {
return it != contexts_.end();
}

bool Agent::ContextCreated(const node::inspector::ContextInfo* info) {
auto isolate = parent_env_->isolate();
if (ContextRegistered(info->context(isolate))) {
return false;
}
contexts_.push_back(info);
Expand Down Expand Up @@ -822,6 +848,7 @@ void Agent::InitInspector(Local<Object> target, Local<Value> unused,
Environment* env = Environment::GetCurrent(context);
Agent* agent = env->inspector_agent();
env->SetMethod(target, "consoleCall", InspectorConsoleCall);
env->SetMethod(target, "contextAttached", ContextAttached);
env->SetMethod(target, "attachContext", AttachContext);
env->SetMethod(target, "detachContext", DetachContext);
if (agent->debug_options_.wait_for_connect())
Expand Down
1 change: 1 addition & 0 deletions src/inspector_agent.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ class Agent {
// Stop and destroy io_
void Stop();

bool ContextRegistered(v8::Local<v8::Context> context);
bool ContextCreated(const node::inspector::ContextInfo* info);
void ContextDestroyed(v8::Local<v8::Context> context);

Expand Down