1 /* Cycript - Optimizing JavaScript Compiler/Runtime
2 * Copyright (C) 2009-2013 Jay Freeman (saurik)
5 /* GNU General Public License, Version 3 {{{ */
7 * Cycript is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published
9 * by the Free Software Foundation, either version 3 of the License,
10 * or (at your option) any later version.
12 * Cycript is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Cycript. If not, see <http://www.gnu.org/licenses/>.
22 #include <Foundation/Foundation.h>
27 #include <sys/types.h>
28 #include <sys/socket.h>
29 #include <netinet/in.h>
32 #include "cycript.hpp"
34 #include "JavaScript.hpp"
36 #include "Pooling.hpp"
38 #include "Cycript.tab.hh"
43 const char * volatile data_;
46 // XXX: this is "tre lame"
47 @interface CYClient_ : NSObject {
50 - (void) execute:(NSValue *)value;
54 @implementation CYClient_
56 - (void) execute:(NSValue *)value {
57 CYExecute_ *execute(reinterpret_cast<CYExecute_ *>([value pointerValue]));
58 const char *data(execute->data_);
59 execute->data_ = NULL;
60 execute->data_ = CYExecute(execute->pool_, CYUTF8String(data));
71 CYClient(int socket) :
77 _syscall(close(socket_));
81 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
83 CYClient_ *client = [[[CYClient_ alloc] init] autorelease];
86 if (CFStringRef mode = CFRunLoopCopyCurrentMode(CFRunLoopGetMain())) {
94 if (!CYRecvAll(socket_, &size, sizeof(size)))
98 char *data(new(pool) char[size + 1]);
99 if (!CYRecvAll(socket_, data, size))
103 CYStream stream(data, data + size);
104 CYDriver driver(stream);
106 cy::parser parser(driver);
109 if (parser.parse() != 0 || !driver.errors_.empty()) {
111 size = _not(uint32_t);
113 NSAutoreleasePool *ar = [[NSAutoreleasePool alloc] init];
116 CYContext context(options);
117 driver.program_->Replace(context);
118 std::ostringstream str;
119 CYOutput out(str, options);
120 out << *driver.program_;
121 std::string code(str.str());
122 CYExecute_ execute = {pool, code.c_str()};
123 NSValue *value([NSValue valueWithPointer:&execute]);
125 [client performSelectorOnMainThread:@selector(execute:) withObject:value waitUntilDone:YES];
127 [client execute:value];
128 json = execute.data_;
129 size = json == NULL ? _not(uint32_t) : strlen(json);
134 if (!CYSendAll(socket_, &size, sizeof(size)))
137 if (!CYSendAll(socket_, json, size))
145 static void *OnClient(void *data) {
146 CYClient *client(reinterpret_cast<CYClient *>(data));
152 extern "C" void CYHandleClient(int socket) {
153 // XXX: this leaks memory... really?
154 CYPool *pool(new CYPool());
155 CYClient *client(new(*pool) CYClient(socket));
156 _assert(pthread_create(&client->thread_, NULL, &OnClient, client) == 0);
159 extern "C" void CYHandleServer(pid_t pid) {
160 CYInitializeDynamic();
162 int socket(_syscall(::socket(PF_UNIX, SOCK_STREAM, 0))); try {
163 struct sockaddr_un address;
164 memset(&address, 0, sizeof(address));
165 address.sun_family = AF_UNIX;
166 sprintf(address.sun_path, "/tmp/.s.cy.%u", pid);
168 _syscall(connect(socket, reinterpret_cast<sockaddr *>(&address), SUN_LEN(&address)));
169 CYHandleClient(socket);
170 } catch (const CYException &error) {
172 fprintf(stderr, "%s\n", error.PoolCString(pool));
181 CYServer(uint16_t port) :
189 _syscall(close(socket_));
193 socket_ = _syscall(::socket(PF_INET, SOCK_STREAM, 0)); try {
195 address.sin_family = AF_INET;
196 address.sin_addr.s_addr = INADDR_ANY;
197 address.sin_port = htons(port_);
198 _syscall(::bind(socket_, reinterpret_cast<sockaddr *>(&address), sizeof(address)));
200 _syscall(::listen(socket_, -1));
203 socklen_t length(sizeof(address));
204 int socket(_syscall(::accept(socket_, reinterpret_cast<sockaddr *>(&address), &length)));
205 CYHandleClient(socket);
207 } catch (const CYException &error) {
209 fprintf(stderr, "%s\n", error.PoolCString(pool));
214 static void *OnServer(void *data) {
215 CYServer *server(reinterpret_cast<CYServer *>(data));
221 extern "C" void CYListenServer(short port) {
222 CYInitializeDynamic();
224 CYServer *server(new CYServer(port));
225 _assert(pthread_create(&server->thread_, NULL, &OnServer, server) == 0);