1 /* Cycript - Optimizing JavaScript Compiler/Runtime
2 * Copyright (C) 2009-2012 Jay Freeman (saurik)
5 /* GNU Lesser General Public License, Version 3 {{{ */
7 * Cycript is free software: you can redistribute it and/or modify it under
8 * the terms of the GNU Lesser General Public License as published by the
9 * Free Software Foundation, either version 3 of the License, or (at your
10 * option) any later version.
12 * Cycript is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15 * License for more details.
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with Cycript. If not, see <http://www.gnu.org/licenses/>.
22 #include "cycript.hpp"
23 #include "JavaScript.hpp"
25 #include "Pooling.hpp"
28 #include "Cycript.tab.hh"
30 #include <Foundation/Foundation.h>
31 #include <apr_thread_proc.h>
35 #include <sys/types.h>
36 #include <sys/socket.h>
37 #include <netinet/in.h>
42 const char * volatile data_;
45 // XXX: this is "tre lame"
46 @interface CYClient_ : NSObject {
49 - (void) execute:(NSValue *)value;
53 @implementation CYClient_
55 - (void) execute:(NSValue *)value {
56 CYExecute_ *execute(reinterpret_cast<CYExecute_ *>([value pointerValue]));
57 const char *data(execute->data_);
58 execute->data_ = NULL;
59 execute->data_ = CYExecute(execute->pool_, CYUTF8String(data));
68 apr_thread_t *thread_;
70 CYClient(int socket) :
76 _syscall(close(socket_));
80 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
82 CYClient_ *client = [[[CYClient_ alloc] init] autorelease];
85 if (CFStringRef mode = CFRunLoopCopyCurrentMode(CFRunLoopGetMain())) {
93 if (!CYRecvAll(socket_, &size, sizeof(size)))
97 char *data(new(pool) char[size + 1]);
98 if (!CYRecvAll(socket_, data, size))
103 cy::parser parser(driver);
109 if (parser.parse() != 0 || !driver.errors_.empty()) {
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(size_t) : strlen(json);
134 if (!CYSendAll(socket_, &size, sizeof(size)))
137 if (!CYSendAll(socket_, json, size))
145 static void * APR_THREAD_FUNC OnClient(apr_thread_t *thread, void *data) {
146 CYClient *client(reinterpret_cast<CYClient *>(data));
152 extern "C" void CYHandleClient(apr_pool_t *pool, int socket) {
153 CYClient *client(new(pool) CYClient(socket));
154 apr_threadattr_t *attr;
155 _aprcall(apr_threadattr_create(&attr, client->pool_));
156 _aprcall(apr_thread_create(&client->thread_, attr, &OnClient, client, client->pool_));
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)));
171 apr_pool_create(&pool, NULL);
173 CYHandleClient(pool, socket);
174 } catch (const CYException &error) {
176 fprintf(stderr, "%s\n", error.PoolCString(pool));