Skip to content

Commit

Permalink
inspector: move options parsing
Browse files Browse the repository at this point in the history
As inspector functionality expands, more options will need to be added.
Currently this requires changing adding function arguments, etc. This
change packs the veriables into a single class that can be extended
without changing APIs.

PR-URL: nodejs#9691
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
  • Loading branch information
Eugene Ostroukhov authored and italoacasas committed Jan 30, 2017
1 parent 33af09f commit e30e307
Show file tree
Hide file tree
Showing 8 changed files with 260 additions and 154 deletions.
2 changes: 2 additions & 0 deletions node.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@
'src/node_config.cc',
'src/node_constants.cc',
'src/node_contextify.cc',
'src/node_debug_options.cc',
'src/node_file.cc',
'src/node_http_parser.cc',
'src/node_javascript.cc',
Expand Down Expand Up @@ -201,6 +202,7 @@
'src/node.h',
'src/node_buffer.h',
'src/node_constants.h',
'src/node_debug_options.h',
'src/node_file.h',
'src/node_http_parser.h',
'src/node_internals.h',
Expand Down
16 changes: 8 additions & 8 deletions src/debug-agent.cc
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ using v8::Value;


Agent::Agent(Environment* env) : state_(kNone),
port_(5858),
wait_(false),
parent_env_(env),
child_env_(nullptr),
Expand All @@ -69,7 +68,7 @@ Agent::~Agent() {
}


bool Agent::Start(const char* host, int port, bool wait) {
bool Agent::Start(const DebugOptions& options) {
int err;

if (state_ == kRunning)
Expand All @@ -85,9 +84,8 @@ bool Agent::Start(const char* host, int port, bool wait) {
goto async_init_failed;
uv_unref(reinterpret_cast<uv_handle_t*>(&child_signal_));

host_ = host;
port_ = port;
wait_ = wait;
options_ = options;
wait_ = options_.wait_for_connect();

err = uv_thread_create(&thread_,
reinterpret_cast<uv_thread_cb>(ThreadCb),
Expand Down Expand Up @@ -210,9 +208,11 @@ void Agent::InitAdaptor(Environment* env) {

api->Set(String::NewFromUtf8(isolate, "host",
NewStringType::kNormal).ToLocalChecked(),
String::NewFromUtf8(isolate, host_.data(), NewStringType::kNormal,
host_.size()).ToLocalChecked());
api->Set(String::NewFromUtf8(isolate, "port"), Integer::New(isolate, port_));
String::NewFromUtf8(isolate, options_.host_name().data(),
NewStringType::kNormal,
options_.host_name().size()).ToLocalChecked());
api->Set(String::NewFromUtf8(isolate, "port"),
Integer::New(isolate, options_.port()));

env->process_object()->Set(String::NewFromUtf8(isolate, "_debugAPI"), api);
api_.Reset(env->isolate(), api);
Expand Down
6 changes: 3 additions & 3 deletions src/debug-agent.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS

#include "node_mutex.h"
#include "node_debug_options.h"
#include "util.h"
#include "util-inl.h"
#include "uv.h"
Expand Down Expand Up @@ -76,7 +77,7 @@ class Agent {
typedef void (*DispatchHandler)(node::Environment* env);

// Start the debugger agent thread
bool Start(const char* host, int port, bool wait);
bool Start(const DebugOptions& options);
// Listen for debug events
void Enable();
// Stop the debugger agent
Expand Down Expand Up @@ -114,9 +115,8 @@ class Agent {
};

State state_;
DebugOptions options_;

std::string host_;
int port_;
bool wait_;

uv_sem_t start_sem_;
Expand Down
25 changes: 13 additions & 12 deletions src/inspector_agent.cc
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,8 @@ class AgentImpl {
explicit AgentImpl(node::Environment* env);

// Start the inspector agent thread
bool Start(v8::Platform* platform, const char* path, int port, bool wait);
bool Start(v8::Platform* platform, const char* path,
const DebugOptions& options);
// Stop the inspector agent
void Stop();

Expand Down Expand Up @@ -168,15 +169,14 @@ class AgentImpl {
void NotifyMessageReceived();
State ToState(State state);

DebugOptions options_;
uv_sem_t start_sem_;
ConditionVariable incoming_message_cond_;
Mutex state_lock_;
uv_thread_t thread_;
uv_loop_t child_loop_;

InspectorAgentDelegate* delegate_;

int port_;
bool wait_;
bool shutting_down_;
State state_;
Expand All @@ -192,6 +192,8 @@ class AgentImpl {
InspectorSocketServer* server_;

std::string script_name_;
std::string script_path_;
const std::string id_;

friend class ChannelImpl;
friend class DispatchOnInspectorBackendTask;
Expand Down Expand Up @@ -316,7 +318,6 @@ class V8NodeInspector : public v8_inspector::V8InspectorClient {
};

AgentImpl::AgentImpl(Environment* env) : delegate_(nullptr),
port_(0),
wait_(false),
shutting_down_(false),
state_(State::kNew),
Expand Down Expand Up @@ -396,7 +397,10 @@ void InspectorWrapConsoleCall(const v8::FunctionCallbackInfo<v8::Value>& args) {
}

bool AgentImpl::Start(v8::Platform* platform, const char* path,
int port, bool wait) {
const DebugOptions& options) {
options_ = options;
wait_ = options.wait_for_connect();

auto env = parent_env_;
inspector_ = new V8NodeInspector(this, env, platform);
platform_ = platform;
Expand All @@ -408,9 +412,6 @@ bool AgentImpl::Start(v8::Platform* platform, const char* path,
int err = uv_loop_init(&child_loop_);
CHECK_EQ(err, 0);

port_ = port;
wait_ = wait;

err = uv_thread_create(&thread_, AgentImpl::ThreadCbIO, this);
CHECK_EQ(err, 0);
uv_sem_wait(&start_sem_);
Expand All @@ -420,7 +421,7 @@ bool AgentImpl::Start(v8::Platform* platform, const char* path,
return false;
}
state_ = State::kAccepting;
if (wait) {
if (options_.wait_for_connect()) {
DispatchMessages();
}
return true;
Expand Down Expand Up @@ -548,7 +549,7 @@ void AgentImpl::WorkerRunIO() {
}
InspectorAgentDelegate delegate(this, script_path, script_name_, wait_);
delegate_ = &delegate;
InspectorSocketServer server(&delegate, port_);
InspectorSocketServer server(&delegate, options_.port());
if (!server.Start(&child_loop_)) {
fprintf(stderr, "Unable to open devtools socket: %s\n", uv_strerror(err));
state_ = State::kError; // Safe, main thread is waiting on semaphore
Expand Down Expand Up @@ -666,8 +667,8 @@ Agent::~Agent() {
}

bool Agent::Start(v8::Platform* platform, const char* path,
int port, bool wait) {
return impl->Start(platform, path, port, wait);
const DebugOptions& options) {
return impl->Start(platform, path, options);
}

void Agent::Stop() {
Expand Down
5 changes: 4 additions & 1 deletion src/inspector_agent.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#error("This header can only be used when inspector is enabled")
#endif

#include "node_debug_options.h"

// Forward declaration to break recursive dependency chain with src/env.h.
namespace node {
class Environment;
Expand All @@ -31,7 +33,8 @@ class Agent {
~Agent();

// Start the inspector agent thread
bool Start(v8::Platform* platform, const char* path, int port, bool wait);
bool Start(v8::Platform* platform, const char* path,
const DebugOptions& options);
// Stop the inspector agent
void Stop();

Expand Down
Loading

0 comments on commit e30e307

Please sign in to comment.