Skip to content
Newer
Older
100644 179 lines (143 sloc) 4.93 KB
c48417b Updated copyright (2010) and license (LGPLv3).
saurik authored
1 /* Cycript - Optimizing JavaScript Compiler/Runtime
2 * Copyright (C) 2009-2010 Jay Freeman (saurik)
ae34266 Mostly insulated Objective-C from Library.mm.
saurik authored
3 */
4
c48417b Updated copyright (2010) and license (LGPLv3).
saurik authored
5 /* GNU Lesser General Public License, Version 3 {{{ */
ae34266 Mostly insulated Objective-C from Library.mm.
saurik authored
6 /*
c48417b Updated copyright (2010) and license (LGPLv3).
saurik authored
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.
ae34266 Mostly insulated Objective-C from Library.mm.
saurik authored
11 *
c48417b Updated copyright (2010) and license (LGPLv3).
saurik authored
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.
ae34266 Mostly insulated Objective-C from Library.mm.
saurik authored
16 *
c48417b Updated copyright (2010) and license (LGPLv3).
saurik authored
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/>.
19 **/
ae34266 Mostly insulated Objective-C from Library.mm.
saurik authored
20 /* }}} */
21
22 #include "cycript.hpp"
35c178e Fixed some CYInitialize issues noticed back on the iPhone.
saurik authored
23 #include "JavaScript.hpp"
ae34266 Mostly insulated Objective-C from Library.mm.
saurik authored
24
25 #include "Pooling.hpp"
26 #include "Parser.hpp"
27
28 #include "Cycript.tab.hh"
29
30 #include <Foundation/Foundation.h>
31 #include <apr_thread_proc.h>
32 #include <unistd.h>
33 #include <sstream>
34
5cdd8e5 Added runloop detection support, ported Handler back to the iPhone, s…
saurik authored
35 #include <sys/types.h>
36 #include <sys/socket.h>
37 #include <netinet/in.h>
38 #include <sys/un.h>
39
ae34266 Mostly insulated Objective-C from Library.mm.
saurik authored
40 struct CYExecute_ {
41 apr_pool_t *pool_;
42 const char * volatile data_;
43 };
44
45 // XXX: this is "tre lame"
46 @interface CYClient_ : NSObject {
47 }
48
49 - (void) execute:(NSValue *)value;
50
51 @end
52
53 @implementation CYClient_
54
55 - (void) execute:(NSValue *)value {
56 CYExecute_ *execute(reinterpret_cast<CYExecute_ *>([value pointerValue]));
57 const char *data(execute->data_);
58 execute->data_ = NULL;
bd806a1 Make CYExecute take a CYUTF8String and fix the size shell game in Con…
saurik authored
59 execute->data_ = CYExecute(execute->pool_, CYUTF8String(data));
ae34266 Mostly insulated Objective-C from Library.mm.
saurik authored
60 }
61
62 @end
63
64 struct CYClient :
65 CYData
66 {
67 int socket_;
68 apr_thread_t *thread_;
69
70 CYClient(int socket) :
71 socket_(socket)
72 {
73 }
74
75 ~CYClient() {
76 _syscall(close(socket_));
77 }
78
8381ec1 Completed massive refactoring operation to completely isolate Objecti…
saurik authored
79 void Handle() {
140da19 Added getPropertyNames bridging (albeit lame) and fixed exception han…
saurik authored
80 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
81
82 CYClient_ *client = [[[CYClient_ alloc] init] autorelease];
ae34266 Mostly insulated Objective-C from Library.mm.
saurik authored
83
5cdd8e5 Added runloop detection support, ported Handler back to the iPhone, s…
saurik authored
84 bool dispatch;
85 if (CFStringRef mode = CFRunLoopCopyCurrentMode(CFRunLoopGetMain())) {
86 dispatch = true;
87 CFRelease(mode);
88 } else
89 dispatch = false;
90
ae34266 Mostly insulated Objective-C from Library.mm.
saurik authored
91 for (;;) {
92 size_t size;
93 if (!CYRecvAll(socket_, &size, sizeof(size)))
94 return;
95
f3998d0 Update injection handler for new local pool semantics.
saurik authored
96 CYLocalPool pool;
ae34266 Mostly insulated Objective-C from Library.mm.
saurik authored
97 char *data(new(pool) char[size + 1]);
98 if (!CYRecvAll(socket_, data, size))
99 return;
100 data[size] = '\0';
101
432213d Tab-complete FTW!
saurik authored
102 CYDriver driver;
ae34266 Mostly insulated Objective-C from Library.mm.
saurik authored
103 cy::parser parser(driver);
104
105 driver.data_ = data;
106 driver.size_ = size;
107
108 const char *json;
109 if (parser.parse() != 0 || !driver.errors_.empty()) {
110 json = NULL;
111 size = _not(size_t);
112 } else {
5cdd8e5 Added runloop detection support, ported Handler back to the iPhone, s…
saurik authored
113 NSAutoreleasePool *ar = [[NSAutoreleasePool alloc] init];
114
4c0c10b Ported Objective-C bridge to match up with the latest compiler changes.
saurik authored
115 CYOptions options;
f3998d0 Update injection handler for new local pool semantics.
saurik authored
116 CYContext context(options);
ae34266 Mostly insulated Objective-C from Library.mm.
saurik authored
117 driver.program_->Replace(context);
118 std::ostringstream str;
4c0c10b Ported Objective-C bridge to match up with the latest compiler changes.
saurik authored
119 CYOutput out(str, options);
ae34266 Mostly insulated Objective-C from Library.mm.
saurik authored
120 out << *driver.program_;
121 std::string code(str.str());
122 CYExecute_ execute = {pool, code.c_str()};
5cdd8e5 Added runloop detection support, ported Handler back to the iPhone, s…
saurik authored
123 NSValue *value([NSValue valueWithPointer:&execute]);
124 if (dispatch)
125 [client performSelectorOnMainThread:@selector(execute:) withObject:value waitUntilDone:YES];
126 else
127 [client execute:value];
ae34266 Mostly insulated Objective-C from Library.mm.
saurik authored
128 json = execute.data_;
129 size = json == NULL ? _not(size_t) : strlen(json);
5cdd8e5 Added runloop detection support, ported Handler back to the iPhone, s…
saurik authored
130
131 [ar release];
ae34266 Mostly insulated Objective-C from Library.mm.
saurik authored
132 }
133
134 if (!CYSendAll(socket_, &size, sizeof(size)))
135 return;
136 if (json != NULL)
137 if (!CYSendAll(socket_, json, size))
138 return;
139 }
8381ec1 Completed massive refactoring operation to completely isolate Objecti…
saurik authored
140
140da19 Added getPropertyNames bridging (albeit lame) and fixed exception han…
saurik authored
141 [pool release];
ae34266 Mostly insulated Objective-C from Library.mm.
saurik authored
142 }
143 };
144
145 static void * APR_THREAD_FUNC OnClient(apr_thread_t *thread, void *data) {
146 CYClient *client(reinterpret_cast<CYClient *>(data));
147 client->Handle();
148 delete client;
149 return NULL;
150 }
151
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_));
157 }
eed67d2 Fixed non-CY_ATTACH builds.
saurik authored
158
159 extern "C" void CYHandleServer(pid_t pid) {
35c178e Fixed some CYInitialize issues noticed back on the iPhone.
saurik authored
160 CYInitializeDynamic();
eed67d2 Fixed non-CY_ATTACH builds.
saurik authored
161
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);
167
168 _syscall(connect(socket, reinterpret_cast<sockaddr *>(&address), SUN_LEN(&address)));
169
170 apr_pool_t *pool;
171 apr_pool_create(&pool, NULL);
172
173 CYHandleClient(pool, socket);
174 } catch (const CYException &error) {
175 CYPool pool;
176 fprintf(stderr, "%s\n", error.PoolCString(pool));
177 }
178 }
Something went wrong with that request. Please try again.