Skip to content

Commit 9871d26

Browse files
authored
Merge pull request #196 from ptomato/dev-inspector
Restore inspector functionality
2 parents fa44007 + 995ab17 commit 9871d26

File tree

176 files changed

+27708
-115
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

176 files changed

+27708
-115
lines changed

NativeScript/NativeScript.mm

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <Foundation/Foundation.h>
22
#include "NativeScript.h"
3-
// #include "inspector/JsV8InspectorClient.h"
3+
#include "inspector/JsV8InspectorClient.h"
4+
#include "runtime/Console.h"
45
#include "runtime/RuntimeConfig.h"
56
#include "runtime/Helpers.h"
67
#include "runtime/Runtime.h"
@@ -47,14 +48,15 @@ - (instancetype)initWithConfig:(Config*)config {
4748
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(t2 - t1).count();
4849
printf("Runtime initialization took %llims\n", duration);
4950

50-
// if (config.IsDebug) {
51-
// Isolate::Scope isolate_scope(isolate);
52-
// HandleScope handle_scope(isolate);
53-
// v8_inspector::JsV8InspectorClient* inspectorClient = new v8_inspector::JsV8InspectorClient(runtime_.get());
54-
// inspectorClient->init();
55-
// inspectorClient->registerModules();
56-
// inspectorClient->connect([config ArgumentsCount], [config Arguments]);
57-
// }
51+
if (config.IsDebug) {
52+
Isolate::Scope isolate_scope(isolate);
53+
HandleScope handle_scope(isolate);
54+
v8_inspector::JsV8InspectorClient* inspectorClient = new v8_inspector::JsV8InspectorClient(runtime_.get());
55+
inspectorClient->init();
56+
inspectorClient->registerModules();
57+
inspectorClient->connect([config ArgumentsCount], [config Arguments]);
58+
Console::AttachInspectorClient(inspectorClient);
59+
}
5860
}
5961

6062
return self;
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// This file is generated by Exported_h.template.
2+
3+
// Copyright (c) 2016 The Chromium Authors. All rights reserved.
4+
// Use of this source code is governed by a BSD-style license that can be
5+
// found in the LICENSE file.
6+
7+
#ifndef v8_inspector_protocol_Debugger_api_h
8+
#define v8_inspector_protocol_Debugger_api_h
9+
10+
#include "v8-inspector.h"
11+
12+
namespace v8_inspector {
13+
namespace protocol {
14+
15+
#ifndef v8_inspector_protocol_exported_api_h
16+
#define v8_inspector_protocol_exported_api_h
17+
class V8_EXPORT Exported {
18+
public:
19+
virtual void AppendSerialized(std::vector<uint8_t>* out) const = 0;
20+
21+
virtual ~Exported() { }
22+
};
23+
#endif // !defined(v8_inspector_protocol_exported_api_h)
24+
25+
namespace Debugger {
26+
namespace API {
27+
28+
// ------------- Enums.
29+
30+
namespace Paused {
31+
namespace ReasonEnum {
32+
V8_EXPORT extern const char* Ambiguous;
33+
V8_EXPORT extern const char* Assert;
34+
V8_EXPORT extern const char* CSPViolation;
35+
V8_EXPORT extern const char* DebugCommand;
36+
V8_EXPORT extern const char* DOM;
37+
V8_EXPORT extern const char* EventListener;
38+
V8_EXPORT extern const char* Exception;
39+
V8_EXPORT extern const char* Instrumentation;
40+
V8_EXPORT extern const char* OOM;
41+
V8_EXPORT extern const char* Other;
42+
V8_EXPORT extern const char* PromiseRejection;
43+
V8_EXPORT extern const char* XHR;
44+
} // ReasonEnum
45+
} // Paused
46+
47+
// ------------- Types.
48+
49+
class V8_EXPORT SearchMatch : public Exported {
50+
public:
51+
static std::unique_ptr<protocol::Debugger::API::SearchMatch> fromBinary(const uint8_t* data, size_t length);
52+
};
53+
54+
} // namespace API
55+
} // namespace Debugger
56+
} // namespace v8_inspector
57+
} // namespace protocol
58+
59+
#endif // !defined(v8_inspector_protocol_Debugger_api_h)
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// This file is generated by Exported_h.template.
2+
3+
// Copyright (c) 2016 The Chromium Authors. All rights reserved.
4+
// Use of this source code is governed by a BSD-style license that can be
5+
// found in the LICENSE file.
6+
7+
#ifndef v8_inspector_protocol_Runtime_api_h
8+
#define v8_inspector_protocol_Runtime_api_h
9+
10+
#include "v8-inspector.h"
11+
12+
namespace v8_inspector {
13+
namespace protocol {
14+
15+
#ifndef v8_inspector_protocol_exported_api_h
16+
#define v8_inspector_protocol_exported_api_h
17+
class V8_EXPORT Exported {
18+
public:
19+
virtual void AppendSerialized(std::vector<uint8_t>* out) const = 0;
20+
21+
virtual ~Exported() { }
22+
};
23+
#endif // !defined(v8_inspector_protocol_exported_api_h)
24+
25+
namespace Runtime {
26+
namespace API {
27+
28+
// ------------- Enums.
29+
30+
// ------------- Types.
31+
32+
class V8_EXPORT RemoteObject : public Exported {
33+
public:
34+
static std::unique_ptr<protocol::Runtime::API::RemoteObject> fromBinary(const uint8_t* data, size_t length);
35+
};
36+
37+
class V8_EXPORT StackTrace : public Exported {
38+
public:
39+
static std::unique_ptr<protocol::Runtime::API::StackTrace> fromBinary(const uint8_t* data, size_t length);
40+
};
41+
42+
class V8_EXPORT StackTraceId : public Exported {
43+
public:
44+
static std::unique_ptr<protocol::Runtime::API::StackTraceId> fromBinary(const uint8_t* data, size_t length);
45+
};
46+
47+
} // namespace API
48+
} // namespace Runtime
49+
} // namespace v8_inspector
50+
} // namespace protocol
51+
52+
#endif // !defined(v8_inspector_protocol_Runtime_api_h)
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// This file is generated by Exported_h.template.
2+
3+
// Copyright (c) 2016 The Chromium Authors. All rights reserved.
4+
// Use of this source code is governed by a BSD-style license that can be
5+
// found in the LICENSE file.
6+
7+
#ifndef v8_inspector_protocol_Schema_api_h
8+
#define v8_inspector_protocol_Schema_api_h
9+
10+
#include "v8-inspector.h"
11+
12+
namespace v8_inspector {
13+
namespace protocol {
14+
15+
#ifndef v8_inspector_protocol_exported_api_h
16+
#define v8_inspector_protocol_exported_api_h
17+
class V8_EXPORT Exported {
18+
public:
19+
virtual void AppendSerialized(std::vector<uint8_t>* out) const = 0;
20+
21+
virtual ~Exported() { }
22+
};
23+
#endif // !defined(v8_inspector_protocol_exported_api_h)
24+
25+
namespace Schema {
26+
namespace API {
27+
28+
// ------------- Enums.
29+
30+
// ------------- Types.
31+
32+
class V8_EXPORT Domain : public Exported {
33+
public:
34+
static std::unique_ptr<protocol::Schema::API::Domain> fromBinary(const uint8_t* data, size_t length);
35+
};
36+
37+
} // namespace API
38+
} // namespace Schema
39+
} // namespace v8_inspector
40+
} // namespace protocol
41+
42+
#endif // !defined(v8_inspector_protocol_Schema_api_h)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#ifndef InspectorServer_h
2+
#define InspectorServer_h
3+
4+
#include <functional>
5+
#include <string>
6+
#include <sys/types.h>
7+
#include <dispatch/dispatch.h>
8+
9+
namespace v8_inspector {
10+
11+
class InspectorServer {
12+
public:
13+
static in_port_t Init(std::function<void (std::function<void (std::string)>)> onClientConnected, std::function<void (std::string)> onMessage);
14+
private:
15+
static void Send(dispatch_io_t channel, dispatch_queue_t queue, std::string message);
16+
};
17+
18+
}
19+
20+
#endif /* InspectorServer_h */
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
#include "InspectorServer.h"
2+
#include <Foundation/Foundation.h>
3+
#include <netinet/in.h>
4+
#include <sys/socket.h>
5+
6+
namespace v8_inspector {
7+
8+
in_port_t InspectorServer::Init(std::function<void (std::function<void (std::string)>)> onClientConnected, std::function<void (std::string)> onMessage) {
9+
in_port_t listenPort = 18183;
10+
11+
int serverSocket = -1;
12+
if ((serverSocket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
13+
assert(false);
14+
}
15+
16+
struct sockaddr_in serverAddress;
17+
memset(&serverAddress, 0, sizeof(serverAddress));
18+
serverAddress.sin_family = AF_INET;
19+
serverAddress.sin_addr.s_addr = htonl(INADDR_ANY);
20+
do {
21+
serverAddress.sin_port = htons(listenPort);
22+
} while (bind(serverSocket, (struct sockaddr *) &serverAddress, sizeof(serverAddress)) < 0 && ++listenPort);
23+
24+
// Make the socket non-blocking
25+
if (fcntl(serverSocket, F_SETFL, O_NONBLOCK) < 0) {
26+
shutdown(serverSocket, SHUT_RDWR);
27+
close(serverSocket);
28+
assert(false);
29+
}
30+
31+
// Set up the dispatch source that will alert us to new incoming connections
32+
dispatch_queue_t q = dispatch_queue_create("server_queue", DISPATCH_QUEUE_CONCURRENT);
33+
dispatch_source_t acceptSource = dispatch_source_create(DISPATCH_SOURCE_TYPE_READ, serverSocket, 0, q);
34+
dispatch_source_set_event_handler(acceptSource, ^{
35+
const unsigned long numPendingConnections = dispatch_source_get_data(acceptSource);
36+
for (unsigned long i = 0; i < numPendingConnections; i++) {
37+
int clientSocket = -1;
38+
struct sockaddr_in echoClntAddr;
39+
unsigned int clntLen = sizeof(echoClntAddr);
40+
41+
// Wait for a client to connect
42+
if ((clientSocket = accept(serverSocket, (struct sockaddr *) &echoClntAddr, &clntLen)) >= 0) {
43+
dispatch_io_t channel = dispatch_io_create(DISPATCH_IO_STREAM, clientSocket, q, ^(int error) {
44+
if (error) {
45+
NSLog(@"Error: %s", strerror(error));
46+
}
47+
close(clientSocket);
48+
});
49+
50+
onClientConnected([channel, q](std::string message) {
51+
Send(channel, q, message);
52+
});
53+
54+
__block dispatch_io_handler_t receiver = ^(bool done, dispatch_data_t data, int error) {
55+
if (error) {
56+
NSLog(@"Error: %s", strerror(error));
57+
}
58+
59+
const void* bytes = [(NSData*)data bytes];
60+
if (!bytes) {
61+
return;
62+
}
63+
64+
uint32_t length = ntohl(*(uint32_t*)bytes);
65+
66+
// Configure the channel...
67+
dispatch_io_set_low_water(channel, length);
68+
69+
// Setup read handler
70+
dispatch_io_read(channel, 0, length, q, ^(bool done, dispatch_data_t data, int error) {
71+
BOOL close = NO;
72+
if (error) {
73+
NSLog(@"Error: %s", strerror(error));
74+
close = YES;
75+
}
76+
77+
const size_t size = data ? dispatch_data_get_size(data) : 0;
78+
if (size) {
79+
NSString* payload = [[NSString alloc] initWithData:(NSData*)data encoding:NSUTF16LittleEndianStringEncoding];
80+
81+
onMessage([payload UTF8String]);
82+
83+
#pragma clang diagnostic push
84+
#pragma clang diagnostic ignored "-Warc-retain-cycles"
85+
dispatch_io_read(channel, 0, 4, q, receiver);
86+
#pragma clang diagnostic pop
87+
} else {
88+
close = YES;
89+
}
90+
91+
if (close) {
92+
dispatch_io_close(channel, DISPATCH_IO_STOP);
93+
}
94+
});
95+
};
96+
97+
if (channel) {
98+
dispatch_io_read(channel, 0, 4, q, receiver);
99+
}
100+
}
101+
else {
102+
NSLog(@"accept() failed;\n");
103+
}
104+
}
105+
});
106+
107+
// Resume the source so we're ready to accept once we listen()
108+
dispatch_resume(acceptSource);
109+
110+
// Listen() on the socket
111+
if (listen(serverSocket, SOMAXCONN) < 0) {
112+
shutdown(serverSocket, SHUT_RDWR);
113+
close(serverSocket);
114+
assert(false);
115+
}
116+
117+
return listenPort;
118+
}
119+
120+
void InspectorServer::Send(dispatch_io_t channel, dispatch_queue_t queue, std::string message) {
121+
NSString* str = [NSString stringWithUTF8String:message.c_str()];
122+
NSUInteger length = [str lengthOfBytesUsingEncoding:NSUTF16LittleEndianStringEncoding];
123+
124+
uint8_t* buffer = (uint8_t*)malloc(length + sizeof(uint32_t));
125+
126+
*(uint32_t*)buffer = htonl(length);
127+
128+
[str getBytes:&buffer[sizeof(uint32_t)]
129+
maxLength:length
130+
usedLength:NULL
131+
encoding:NSUTF16LittleEndianStringEncoding
132+
options:0
133+
range:NSMakeRange(0, str.length)
134+
remainingRange:NULL];
135+
136+
dispatch_data_t data = dispatch_data_create(buffer, length + sizeof(uint32_t), queue, ^{
137+
free(buffer);
138+
});
139+
140+
dispatch_io_write(channel, 0, data, queue, ^(bool done, dispatch_data_t data, int error) {
141+
if (error) {
142+
NSLog(@"Error: %s", strerror(error));
143+
}
144+
});
145+
}
146+
147+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include <stdio.h>
2+
#include "JsV8InspectorClient.h"
3+
//#include "src/inspector/v8-inspector-session-impl.h"
4+
#include "Helpers.h"
5+
6+
using namespace v8;
7+
8+
namespace v8_inspector {
9+
10+
void JsV8InspectorClient::inspectorSendEventCallback(const FunctionCallbackInfo<Value>& args) {
11+
Local<External> data = args.Data().As<External>();
12+
v8_inspector::JsV8InspectorClient* client = static_cast<v8_inspector::JsV8InspectorClient*>(data->Value());
13+
Isolate* isolate = args.GetIsolate();
14+
Local<v8::String> arg = args[0].As<v8::String>();
15+
std::string message = tns::ToString(isolate, arg);
16+
17+
// if (message.find("\"Network.") != std::string::npos) {
18+
// // The Network domain is handled directly by the corresponding backend
19+
// V8InspectorSessionImpl* session = (V8InspectorSessionImpl*)client->session_.get();
20+
// session->networkArgent()->dispatch(message);
21+
// return;
22+
// }
23+
//
24+
// if (message.find("\"DOM.") != std::string::npos) {
25+
// // The DOM domain is handled directly by the corresponding backend
26+
// V8InspectorSessionImpl* session = (V8InspectorSessionImpl*)client->session_.get();
27+
// session->domArgent()->dispatch(message);
28+
// return;
29+
// }
30+
31+
client->dispatchMessage(message);
32+
}
33+
34+
}

0 commit comments

Comments
 (0)