]> git.saurik.com Git - cycript.git/blame - Handler.mm
Added Microsoft Ajax Minifier to local test output.
[cycript.git] / Handler.mm
CommitLineData
d15b59f5 1/* Cycript - Inlining/Optimizing JavaScript Compiler
b24eb750
JF
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
b24eb750 40#include "cycript.hpp"
09eee478 41#include "JavaScript.hpp"
b24eb750
JF
42
43#include "Pooling.hpp"
44#include "Parser.hpp"
029bc65b 45#include "Context.hpp"
b24eb750
JF
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
bb1c419c
JF
54#include <sys/types.h>
55#include <sys/socket.h>
56#include <netinet/in.h>
57#include <sys/un.h>
58
b24eb750
JF
59struct 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
83struct 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
37954781 98 void Handle() {
d3760804
JF
99 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
100
101 CYClient_ *client = [[[CYClient_ alloc] init] autorelease];
b24eb750 102
bb1c419c
JF
103 bool dispatch;
104 if (CFStringRef mode = CFRunLoopCopyCurrentMode(CFRunLoopGetMain())) {
105 dispatch = true;
106 CFRelease(mode);
107 } else
108 dispatch = false;
109
b24eb750
JF
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 {
bb1c419c
JF
132 NSAutoreleasePool *ar = [[NSAutoreleasePool alloc] init];
133
69688ca1
JF
134 CYOptions options;
135 CYContext context(driver.pool_, options);
b24eb750
JF
136 driver.program_->Replace(context);
137 std::ostringstream str;
69688ca1 138 CYOutput out(str, options);
b24eb750
JF
139 out << *driver.program_;
140 std::string code(str.str());
141 CYExecute_ execute = {pool, code.c_str()};
bb1c419c
JF
142 NSValue *value([NSValue valueWithPointer:&execute]);
143 if (dispatch)
144 [client performSelectorOnMainThread:@selector(execute:) withObject:value waitUntilDone:YES];
145 else
146 [client execute:value];
b24eb750
JF
147 json = execute.data_;
148 size = json == NULL ? _not(size_t) : strlen(json);
bb1c419c
JF
149
150 [ar release];
b24eb750
JF
151 }
152
153 if (!CYSendAll(socket_, &size, sizeof(size)))
154 return;
155 if (json != NULL)
156 if (!CYSendAll(socket_, json, size))
157 return;
158 }
37954781 159
d3760804 160 [pool release];
b24eb750
JF
161 }
162};
163
164static void * APR_THREAD_FUNC OnClient(apr_thread_t *thread, void *data) {
165 CYClient *client(reinterpret_cast<CYClient *>(data));
166 client->Handle();
167 delete client;
168 return NULL;
169}
170
171extern "C" void CYHandleClient(apr_pool_t *pool, int socket) {
172 CYClient *client(new(pool) CYClient(socket));
173 apr_threadattr_t *attr;
174 _aprcall(apr_threadattr_create(&attr, client->pool_));
175 _aprcall(apr_thread_create(&client->thread_, attr, &OnClient, client, client->pool_));
176}
994434e5
JF
177
178extern "C" void CYHandleServer(pid_t pid) {
09eee478 179 CYInitializeDynamic();
994434e5
JF
180
181 int socket(_syscall(::socket(PF_UNIX, SOCK_STREAM, 0))); try {
182 struct sockaddr_un address;
183 memset(&address, 0, sizeof(address));
184 address.sun_family = AF_UNIX;
185 sprintf(address.sun_path, "/tmp/.s.cy.%u", pid);
186
187 _syscall(connect(socket, reinterpret_cast<sockaddr *>(&address), SUN_LEN(&address)));
188
189 apr_pool_t *pool;
190 apr_pool_create(&pool, NULL);
191
192 CYHandleClient(pool, socket);
193 } catch (const CYException &error) {
194 CYPool pool;
195 fprintf(stderr, "%s\n", error.PoolCString(pool));
196 }
197}