Skip to content

Commit f9aadfb

Browse files
author
Eugene Ostroukhov
committed
inspector: move options parsing
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: #9691 Reviewed-By: Ben Noordhuis <[email protected]>
1 parent bc335c0 commit f9aadfb

File tree

8 files changed

+260
-154
lines changed

8 files changed

+260
-154
lines changed

node.gyp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@
154154
'src/node_config.cc',
155155
'src/node_constants.cc',
156156
'src/node_contextify.cc',
157+
'src/node_debug_options.cc',
157158
'src/node_file.cc',
158159
'src/node_http_parser.cc',
159160
'src/node_javascript.cc',
@@ -194,6 +195,7 @@
194195
'src/node.h',
195196
'src/node_buffer.h',
196197
'src/node_constants.h',
198+
'src/node_debug_options.h',
197199
'src/node_file.h',
198200
'src/node_http_parser.h',
199201
'src/node_internals.h',

src/debug-agent.cc

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ using v8::Value;
5050

5151

5252
Agent::Agent(Environment* env) : state_(kNone),
53-
port_(5858),
5453
wait_(false),
5554
parent_env_(env),
5655
child_env_(nullptr),
@@ -69,7 +68,7 @@ Agent::~Agent() {
6968
}
7069

7170

72-
bool Agent::Start(const char* host, int port, bool wait) {
71+
bool Agent::Start(const DebugOptions& options) {
7372
int err;
7473

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

88-
host_ = host;
89-
port_ = port;
90-
wait_ = wait;
87+
options_ = options;
88+
wait_ = options_.wait_for_connect();
9189

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

211209
api->Set(String::NewFromUtf8(isolate, "host",
212210
NewStringType::kNormal).ToLocalChecked(),
213-
String::NewFromUtf8(isolate, host_.data(), NewStringType::kNormal,
214-
host_.size()).ToLocalChecked());
215-
api->Set(String::NewFromUtf8(isolate, "port"), Integer::New(isolate, port_));
211+
String::NewFromUtf8(isolate, options_.host_name().data(),
212+
NewStringType::kNormal,
213+
options_.host_name().size()).ToLocalChecked());
214+
api->Set(String::NewFromUtf8(isolate, "port"),
215+
Integer::New(isolate, options_.port()));
216216

217217
env->process_object()->Set(String::NewFromUtf8(isolate, "_debugAPI"), api);
218218
api_.Reset(env->isolate(), api);

src/debug-agent.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
2626

2727
#include "node_mutex.h"
28+
#include "node_debug_options.h"
2829
#include "util.h"
2930
#include "util-inl.h"
3031
#include "uv.h"
@@ -76,7 +77,7 @@ class Agent {
7677
typedef void (*DispatchHandler)(node::Environment* env);
7778

7879
// Start the debugger agent thread
79-
bool Start(const char* host, int port, bool wait);
80+
bool Start(const DebugOptions& options);
8081
// Listen for debug events
8182
void Enable();
8283
// Stop the debugger agent
@@ -114,9 +115,8 @@ class Agent {
114115
};
115116

116117
State state_;
118+
DebugOptions options_;
117119

118-
std::string host_;
119-
int port_;
120120
bool wait_;
121121

122122
uv_sem_t start_sem_;

src/inspector_agent.cc

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,8 @@ class AgentImpl {
132132
~AgentImpl();
133133

134134
// Start the inspector agent thread
135-
bool Start(v8::Platform* platform, const char* path, int port, bool wait);
135+
bool Start(v8::Platform* platform, const char* path,
136+
const DebugOptions& options);
136137
// Stop the inspector agent
137138
void Stop();
138139

@@ -169,15 +170,14 @@ class AgentImpl {
169170
void NotifyMessageReceived();
170171
State ToState(State state);
171172

173+
DebugOptions options_;
172174
uv_sem_t start_sem_;
173175
ConditionVariable incoming_message_cond_;
174176
Mutex state_lock_;
175177
uv_thread_t thread_;
176178
uv_loop_t child_loop_;
177179

178180
InspectorAgentDelegate* delegate_;
179-
180-
int port_;
181181
bool wait_;
182182
bool shutting_down_;
183183
State state_;
@@ -194,6 +194,8 @@ class AgentImpl {
194194
InspectorSocketServer* server_;
195195

196196
std::string script_name_;
197+
std::string script_path_;
198+
const std::string id_;
197199

198200
friend class ChannelImpl;
199201
friend class DispatchOnInspectorBackendTask;
@@ -318,7 +320,6 @@ class V8NodeInspector : public v8_inspector::V8InspectorClient {
318320
};
319321

320322
AgentImpl::AgentImpl(Environment* env) : delegate_(nullptr),
321-
port_(0),
322323
wait_(false),
323324
shutting_down_(false),
324325
state_(State::kNew),
@@ -409,7 +410,10 @@ void InspectorWrapConsoleCall(const v8::FunctionCallbackInfo<v8::Value>& args) {
409410
}
410411

411412
bool AgentImpl::Start(v8::Platform* platform, const char* path,
412-
int port, bool wait) {
413+
const DebugOptions& options) {
414+
options_ = options;
415+
wait_ = options.wait_for_connect();
416+
413417
auto env = parent_env_;
414418
inspector_ = new V8NodeInspector(this, env, platform);
415419
platform_ = platform;
@@ -421,9 +425,6 @@ bool AgentImpl::Start(v8::Platform* platform, const char* path,
421425
int err = uv_loop_init(&child_loop_);
422426
CHECK_EQ(err, 0);
423427

424-
port_ = port;
425-
wait_ = wait;
426-
427428
err = uv_thread_create(&thread_, AgentImpl::ThreadCbIO, this);
428429
CHECK_EQ(err, 0);
429430
uv_sem_wait(&start_sem_);
@@ -433,7 +434,7 @@ bool AgentImpl::Start(v8::Platform* platform, const char* path,
433434
return false;
434435
}
435436
state_ = State::kAccepting;
436-
if (wait) {
437+
if (options_.wait_for_connect()) {
437438
DispatchMessages();
438439
}
439440
return true;
@@ -561,7 +562,7 @@ void AgentImpl::WorkerRunIO() {
561562
}
562563
InspectorAgentDelegate delegate(this, script_path, script_name_, wait_);
563564
delegate_ = &delegate;
564-
InspectorSocketServer server(&delegate, port_);
565+
InspectorSocketServer server(&delegate, options_.port());
565566
if (!server.Start(&child_loop_)) {
566567
fprintf(stderr, "Unable to open devtools socket: %s\n", uv_strerror(err));
567568
state_ = State::kError; // Safe, main thread is waiting on semaphore
@@ -681,8 +682,8 @@ Agent::~Agent() {
681682
}
682683

683684
bool Agent::Start(v8::Platform* platform, const char* path,
684-
int port, bool wait) {
685-
return impl->Start(platform, path, port, wait);
685+
const DebugOptions& options) {
686+
return impl->Start(platform, path, options);
686687
}
687688

688689
void Agent::Stop() {

src/inspector_agent.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
#error("This header can only be used when inspector is enabled")
88
#endif
99

10+
#include "node_debug_options.h"
11+
1012
// Forward declaration to break recursive dependency chain with src/env.h.
1113
namespace node {
1214
class Environment;
@@ -31,7 +33,8 @@ class Agent {
3133
~Agent();
3234

3335
// Start the inspector agent thread
34-
bool Start(v8::Platform* platform, const char* path, int port, bool wait);
36+
bool Start(v8::Platform* platform, const char* path,
37+
const DebugOptions& options);
3538
// Stop the inspector agent
3639
void Stop();
3740

0 commit comments

Comments
 (0)