]> git.saurik.com Git - cycript.git/blob - Handler.mm
Started working on variable name replacement strategies and generally improved compiler.
[cycript.git] / Handler.mm
1 /* Cycript - Inlining/Optimizing JavaScript Compiler
2 * Copyright (C) 2009 Jay Freeman (saurik)
3 */
4
5 /* Modified BSD License {{{ */
6 /*
7 * Redistribution and use in source and binary
8 * forms, with or without modification, are permitted
9 * provided that the following conditions are met:
10 *
11 * 1. Redistributions of source code must retain the
12 * above copyright notice, this list of conditions
13 * and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the
15 * above copyright notice, this list of conditions
16 * and the following disclaimer in the documentation
17 * and/or other materials provided with the
18 * distribution.
19 * 3. The name of the author may not be used to endorse
20 * or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS''
24 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
25 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
26 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
31 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
34 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
35 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
36 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 */
38 /* }}} */
39
40 #include "cycript.hpp"
41 #include "JavaScript.hpp"
42
43 #include "Pooling.hpp"
44 #include "Parser.hpp"
45 #include "Context.hpp"
46
47 #include "Cycript.tab.hh"
48
49 #include <Foundation/Foundation.h>
50 #include <apr_thread_proc.h>
51 #include <unistd.h>
52 #include <sstream>
53
54 #include <sys/types.h>
55 #include <sys/socket.h>
56 #include <netinet/in.h>
57 #include <sys/un.h>
58
59 struct CYExecute_ {
60 apr_pool_t *pool_;
61 const char * volatile data_;
62 };
63
64 // XXX: this is "tre lame"
65 @interface CYClient_ : NSObject {
66 }
67
68 - (void) execute:(NSValue *)value;
69
70 @end
71
72 @implementation CYClient_
73
74 - (void) execute:(NSValue *)value {
75 CYExecute_ *execute(reinterpret_cast<CYExecute_ *>([value pointerValue]));
76 const char *data(execute->data_);
77 execute->data_ = NULL;
78 execute->data_ = CYExecute(execute->pool_, data);
79 }
80
81 @end
82
83 struct CYClient :
84 CYData
85 {
86 int socket_;
87 apr_thread_t *thread_;
88
89 CYClient(int socket) :
90 socket_(socket)
91 {
92 }
93
94 ~CYClient() {
95 _syscall(close(socket_));
96 }
97
98 void Handle() {
99 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
100
101 CYClient_ *client = [[[CYClient_ alloc] init] autorelease];
102
103 bool dispatch;
104 if (CFStringRef mode = CFRunLoopCopyCurrentMode(CFRunLoopGetMain())) {
105 dispatch = true;
106 CFRelease(mode);
107 } else
108 dispatch = false;
109
110 for (;;) {
111 size_t size;
112 if (!CYRecvAll(socket_, &size, sizeof(size)))
113 return;
114
115 CYPool pool;
116 char *data(new(pool) char[size + 1]);
117 if (!CYRecvAll(socket_, data, size))
118 return;
119 data[size] = '\0';
120
121 CYDriver driver("");
122 cy::parser parser(driver);
123
124 driver.data_ = data;
125 driver.size_ = size;
126
127 const char *json;
128 if (parser.parse() != 0 || !driver.errors_.empty()) {
129 json = NULL;
130 size = _not(size_t);
131 } else {
132 NSAutoreleasePool *ar = [[NSAutoreleasePool alloc] init];
133
134 CYContext context(driver.pool_);
135 driver.program_->Replace(context);
136 std::ostringstream str;
137 CYOutput out(str);
138 out << *driver.program_;
139 std::string code(str.str());
140 CYExecute_ execute = {pool, code.c_str()};
141 NSValue *value([NSValue valueWithPointer:&execute]);
142 if (dispatch)
143 [client performSelectorOnMainThread:@selector(execute:) withObject:value waitUntilDone:YES];
144 else
145 [client execute:value];
146 json = execute.data_;
147 size = json == NULL ? _not(size_t) : strlen(json);
148
149 [ar release];
150 }
151
152 if (!CYSendAll(socket_, &size, sizeof(size)))
153 return;
154 if (json != NULL)
155 if (!CYSendAll(socket_, json, size))
156 return;
157 }
158
159 [pool release];
160 }
161 };
162
163 static void * APR_THREAD_FUNC OnClient(apr_thread_t *thread, void *data) {
164 CYClient *client(reinterpret_cast<CYClient *>(data));
165 client->Handle();
166 delete client;
167 return NULL;
168 }
169
170 extern "C" void CYHandleClient(apr_pool_t *pool, int socket) {
171 CYClient *client(new(pool) CYClient(socket));
172 apr_threadattr_t *attr;
173 _aprcall(apr_threadattr_create(&attr, client->pool_));
174 _aprcall(apr_thread_create(&client->thread_, attr, &OnClient, client, client->pool_));
175 }
176
177 extern "C" void CYHandleServer(pid_t pid) {
178 CYInitializeDynamic();
179
180 int socket(_syscall(::socket(PF_UNIX, SOCK_STREAM, 0))); try {
181 struct sockaddr_un address;
182 memset(&address, 0, sizeof(address));
183 address.sun_family = AF_UNIX;
184 sprintf(address.sun_path, "/tmp/.s.cy.%u", pid);
185
186 _syscall(connect(socket, reinterpret_cast<sockaddr *>(&address), SUN_LEN(&address)));
187
188 apr_pool_t *pool;
189 apr_pool_create(&pool, NULL);
190
191 CYHandleClient(pool, socket);
192 } catch (const CYException &error) {
193 CYPool pool;
194 fprintf(stderr, "%s\n", error.PoolCString(pool));
195 }
196 }