]>
Commit | Line | Data |
---|---|---|
62ca2b82 | 1 | /* Cyrker - Remove Execution Server and Disassembler |
c1582939 JF |
2 | * Copyright (C) 2009 Jay Freeman (saurik) |
3 | */ | |
4 | ||
62ca2b82 | 5 | /* Modified BSD License {{{ */ |
c1582939 JF |
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 | */ | |
62ca2b82 | 38 | /* }}} */ |
c1582939 | 39 | |
8d9b5eed JF |
40 | #define _GNU_SOURCE |
41 | ||
c1582939 | 42 | #include <substrate.h> |
30ddc20c | 43 | #include "cycript.hpp" |
c1582939 | 44 | |
ea2d184c JF |
45 | #include "sig/parse.hpp" |
46 | #include "sig/ffi_type.hpp" | |
47 | ||
5999c315 | 48 | #include "Pooling.hpp" |
057f943f | 49 | #include "Struct.hpp" |
ea2d184c | 50 | |
c1582939 JF |
51 | #include <unistd.h> |
52 | ||
53 | #include <CoreFoundation/CoreFoundation.h> | |
54 | #include <CoreFoundation/CFLogUtilities.h> | |
55 | ||
56 | #include <CFNetwork/CFNetwork.h> | |
c1582939 JF |
57 | |
58 | #include <WebKit/WebScriptObject.h> | |
59 | ||
60 | #include <sys/types.h> | |
61 | #include <sys/socket.h> | |
62 | #include <netinet/in.h> | |
b09da87b | 63 | #include <sys/mman.h> |
c1582939 | 64 | |
8d9b5eed JF |
65 | #include <iostream> |
66 | #include <ext/stdio_filebuf.h> | |
a2d9403c JF |
67 | #include <set> |
68 | #include <map> | |
8d9b5eed | 69 | |
bd17e6f3 JF |
70 | #include <cmath> |
71 | ||
e5332278 | 72 | #include "Parser.hpp" |
63b4c5a8 | 73 | #include "Cycript.tab.hh" |
e5332278 | 74 | |
ea2d184c JF |
75 | #undef _assert |
76 | #undef _trace | |
77 | ||
c1582939 | 78 | #define _assert(test) do { \ |
f5e9be24 JF |
79 | if (!(test)) \ |
80 | @throw [NSException exceptionWithName:NSInternalInconsistencyException reason:[NSString stringWithFormat:@"_assert(%s):%s(%u):%s", #test, __FILE__, __LINE__, __FUNCTION__] userInfo:nil]; \ | |
c1582939 JF |
81 | } while (false) |
82 | ||
83 | #define _trace() do { \ | |
62ca2b82 | 84 | CFLog(kCFLogLevelNotice, CFSTR("_trace():%u"), __LINE__); \ |
c1582939 JF |
85 | } while (false) |
86 | ||
4cf49641 JF |
87 | #define CYPoolTry { \ |
88 | id _saved(nil); \ | |
89 | NSAutoreleasePool *_pool([[NSAutoreleasePool alloc] init]); \ | |
90 | @try | |
91 | #define CYPoolCatch(value) \ | |
92 | @catch (NSException *error) { \ | |
93 | _saved = [error retain]; \ | |
94 | @throw; \ | |
95 | return value; \ | |
96 | } @finally { \ | |
97 | [_pool release]; \ | |
98 | if (_saved != nil) \ | |
99 | [_saved autorelease]; \ | |
100 | } \ | |
101 | } | |
102 | ||
478d4ed0 | 103 | static JSGlobalContextRef Context_; |
b09da87b | 104 | static JSObjectRef System_; |
ea2d184c | 105 | |
88c977fa JF |
106 | static JSClassRef Functor_; |
107 | static JSClassRef Instance_; | |
108 | static JSClassRef Pointer_; | |
953647c1 | 109 | static JSClassRef Runtime_; |
88c977fa | 110 | static JSClassRef Selector_; |
953647c1 | 111 | static JSClassRef Struct_; |
ea2d184c | 112 | |
c1582939 | 113 | static JSObjectRef Array_; |
dea834b0 | 114 | static JSObjectRef Function_; |
ea2d184c | 115 | |
62ca2b82 JF |
116 | static JSStringRef name_; |
117 | static JSStringRef message_; | |
c1582939 | 118 | static JSStringRef length_; |
ea2d184c | 119 | |
c1582939 JF |
120 | static Class NSCFBoolean_; |
121 | ||
953647c1 | 122 | static NSArray *Bridge_; |
88c977fa | 123 | |
c1582939 JF |
124 | struct Client { |
125 | CFHTTPMessageRef message_; | |
126 | CFSocketRef socket_; | |
127 | }; | |
128 | ||
953647c1 | 129 | struct CYData { |
478d4ed0 | 130 | apr_pool_t *pool_; |
953647c1 JF |
131 | |
132 | virtual ~CYData() { | |
133 | } | |
478d4ed0 JF |
134 | |
135 | void *operator new(size_t size) { | |
136 | apr_pool_t *pool; | |
137 | apr_pool_create(&pool, NULL); | |
138 | void *data(apr_palloc(pool, size)); | |
953647c1 | 139 | reinterpret_cast<CYData *>(data)->pool_ = pool; |
478d4ed0 JF |
140 | return data;; |
141 | } | |
142 | ||
953647c1 JF |
143 | static void Finalize(JSObjectRef object) { |
144 | CYData *data(reinterpret_cast<CYData *>(JSObjectGetPrivate(object))); | |
145 | data->~CYData(); | |
146 | apr_pool_destroy(data->pool_); | |
478d4ed0 | 147 | } |
953647c1 JF |
148 | }; |
149 | ||
150 | struct Pointer_privateData : | |
151 | CYData | |
152 | { | |
153 | void *value_; | |
154 | sig::Type type_; | |
478d4ed0 | 155 | |
bd17e6f3 JF |
156 | Pointer_privateData() { |
157 | } | |
158 | ||
953647c1 JF |
159 | Pointer_privateData(void *value) : |
160 | value_(value) | |
161 | { | |
478d4ed0 JF |
162 | } |
163 | }; | |
164 | ||
bd17e6f3 JF |
165 | struct Functor_privateData : |
166 | Pointer_privateData | |
167 | { | |
478d4ed0 JF |
168 | sig::Signature signature_; |
169 | ffi_cif cif_; | |
170 | ||
953647c1 JF |
171 | Functor_privateData(const char *type, void (*value)()) : |
172 | Pointer_privateData(reinterpret_cast<void *>(value)) | |
478d4ed0 JF |
173 | { |
174 | sig::Parse(pool_, &signature_, type); | |
175 | sig::sig_ffi_cif(pool_, &sig::ObjectiveC, &signature_, &cif_); | |
176 | } | |
177 | }; | |
178 | ||
bd17e6f3 JF |
179 | struct ffoData : |
180 | Functor_privateData | |
181 | { | |
478d4ed0 JF |
182 | JSContextRef context_; |
183 | JSObjectRef function_; | |
184 | ||
185 | ffoData(const char *type) : | |
953647c1 | 186 | Functor_privateData(type, NULL) |
478d4ed0 JF |
187 | { |
188 | } | |
189 | }; | |
190 | ||
953647c1 JF |
191 | struct Selector_privateData : Pointer_privateData { |
192 | Selector_privateData(SEL value) : | |
193 | Pointer_privateData(value) | |
478d4ed0 JF |
194 | { |
195 | } | |
196 | ||
197 | SEL GetValue() const { | |
198 | return reinterpret_cast<SEL>(value_); | |
199 | } | |
200 | }; | |
201 | ||
bd17e6f3 JF |
202 | struct Instance_privateData : |
203 | Pointer_privateData | |
204 | { | |
478d4ed0 JF |
205 | bool transient_; |
206 | ||
953647c1 JF |
207 | Instance_privateData(id value, bool transient) : |
208 | Pointer_privateData(value) | |
478d4ed0 JF |
209 | { |
210 | } | |
211 | ||
953647c1 | 212 | virtual ~Instance_privateData() { |
478d4ed0 JF |
213 | if (!transient_) |
214 | [GetValue() release]; | |
215 | } | |
216 | ||
217 | id GetValue() const { | |
218 | return reinterpret_cast<id>(value_); | |
219 | } | |
220 | }; | |
221 | ||
bd17e6f3 JF |
222 | namespace sig { |
223 | ||
224 | void Copy(apr_pool_t *pool, Type &lhs, Type &rhs); | |
225 | ||
226 | void Copy(apr_pool_t *pool, Element &lhs, Element &rhs) { | |
227 | lhs.name = apr_pstrdup(pool, rhs.name); | |
228 | if (rhs.type == NULL) | |
229 | lhs.type = NULL; | |
230 | else { | |
231 | lhs.type = new(pool) Type; | |
232 | Copy(pool, *lhs.type, *rhs.type); | |
233 | } | |
234 | lhs.offset = rhs.offset; | |
235 | } | |
236 | ||
237 | void Copy(apr_pool_t *pool, Signature &lhs, Signature &rhs) { | |
238 | size_t count(rhs.count); | |
239 | lhs.count = count; | |
240 | lhs.elements = new(pool) Element[count]; | |
241 | for (size_t index(0); index != count; ++index) | |
242 | Copy(pool, lhs.elements[index], rhs.elements[index]); | |
243 | } | |
244 | ||
245 | void Copy(apr_pool_t *pool, Type &lhs, Type &rhs) { | |
246 | lhs.primitive = rhs.primitive; | |
247 | lhs.name = apr_pstrdup(pool, rhs.name); | |
248 | lhs.flags = rhs.flags; | |
249 | ||
250 | if (sig::IsAggregate(rhs.primitive)) | |
251 | Copy(pool, lhs.data.signature, rhs.data.signature); | |
252 | else { | |
253 | if (rhs.data.data.type != NULL) { | |
254 | lhs.data.data.type = new(pool) Type; | |
255 | Copy(pool, *lhs.data.data.type, *rhs.data.data.type); | |
256 | } | |
257 | ||
258 | lhs.data.data.size = rhs.data.data.size; | |
259 | } | |
260 | } | |
261 | ||
262 | void Copy(apr_pool_t *pool, ffi_type &lhs, ffi_type &rhs) { | |
263 | lhs.size = rhs.size; | |
264 | lhs.alignment = rhs.alignment; | |
265 | lhs.type = rhs.type; | |
266 | if (rhs.elements == NULL) | |
267 | lhs.elements = NULL; | |
268 | else { | |
269 | size_t count(0); | |
270 | while (rhs.elements[count] != NULL) | |
271 | ++count; | |
272 | ||
273 | lhs.elements = new(pool) ffi_type *[count + 1]; | |
274 | lhs.elements[count] = NULL; | |
275 | ||
276 | for (size_t index(0); index != count; ++index) { | |
277 | // XXX: if these are libffi native then you can just take them | |
278 | ffi_type *ffi(new(pool) ffi_type); | |
279 | lhs.elements[index] = ffi; | |
280 | sig::Copy(pool, *ffi, *rhs.elements[index]); | |
281 | } | |
282 | } | |
283 | } | |
284 | ||
285 | } | |
286 | ||
287 | struct Type_privateData { | |
288 | sig::Type type_; | |
289 | ffi_type ffi_; | |
290 | //size_t count_; | |
291 | ||
292 | Type_privateData(apr_pool_t *pool, sig::Type *type, ffi_type *ffi) { | |
293 | sig::Copy(pool, type_, *type); | |
294 | sig::Copy(pool, ffi_, *ffi); | |
295 | ||
296 | /*sig::Element element; | |
297 | element.name = NULL; | |
298 | element.type = type; | |
299 | element.offset = 0; | |
300 | ||
301 | sig::Signature signature; | |
302 | signature.elements = &element; | |
303 | signature.count = 1; | |
304 | ||
305 | ffi_cif cif; | |
306 | sig::sig_ffi_cif(pool, &sig::ObjectiveC, &signature, &cif); | |
307 | ffi_ = *cif.rtype;*/ | |
308 | ||
309 | /*if (type_->type != FFI_TYPE_STRUCT) | |
310 | count_ = 0; | |
311 | else { | |
312 | size_t count(0); | |
313 | while (type_->elements[count] != NULL) | |
314 | ++count; | |
315 | count_ = count; | |
316 | }*/ | |
317 | } | |
318 | }; | |
319 | ||
320 | struct Struct_privateData : | |
321 | Pointer_privateData | |
322 | { | |
323 | JSObjectRef owner_; | |
324 | Type_privateData *type_; | |
325 | ||
326 | Struct_privateData() { | |
327 | } | |
328 | }; | |
329 | ||
330 | struct CStringMapLess : | |
331 | std::binary_function<const char *, const char *, bool> | |
332 | { | |
333 | _finline bool operator ()(const char *lhs, const char *rhs) const { | |
334 | return strcmp(lhs, rhs) < 0; | |
335 | } | |
953647c1 JF |
336 | }; |
337 | ||
bd17e6f3 JF |
338 | typedef std::map<const char *, Type_privateData *, CStringMapLess> TypeMap; |
339 | static TypeMap Types_; | |
340 | ||
341 | JSObjectRef CYMakeStruct(JSContextRef context, void *data, sig::Type *type, ffi_type *ffi, JSObjectRef owner) { | |
342 | Struct_privateData *internal(new Struct_privateData()); | |
343 | apr_pool_t *pool(internal->pool_); | |
344 | Type_privateData *typical(new(pool) Type_privateData(pool, type, ffi)); | |
345 | internal->type_ = typical; | |
346 | ||
347 | if (owner != NULL) { | |
348 | internal->owner_ = owner; | |
349 | internal->value_ = data; | |
350 | } else { | |
351 | internal->owner_ = NULL; | |
352 | ||
353 | size_t size(typical->ffi_.size); | |
354 | void *copy(apr_palloc(internal->pool_, size)); | |
355 | memcpy(copy, data, size); | |
356 | internal->value_ = copy; | |
357 | } | |
358 | ||
bd17e6f3 JF |
359 | return JSObjectMake(context, Struct_, internal); |
360 | } | |
361 | ||
4cf49641 | 362 | JSObjectRef CYMakeInstance(JSContextRef context, id object, bool transient) { |
478d4ed0 JF |
363 | if (!transient) |
364 | object = [object retain]; | |
953647c1 | 365 | Instance_privateData *data(new Instance_privateData(object, transient)); |
478d4ed0 JF |
366 | return JSObjectMake(context, Instance_, data); |
367 | } | |
368 | ||
369 | const char *CYPoolCString(apr_pool_t *pool, NSString *value) { | |
370 | if (pool == NULL) | |
371 | return [value UTF8String]; | |
372 | else { | |
373 | size_t size([value maximumLengthOfBytesUsingEncoding:NSUTF8StringEncoding] + 1); | |
374 | char *string(new(pool) char[size]); | |
375 | if (![value getCString:string maxLength:size encoding:NSUTF8StringEncoding]) | |
376 | @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"[NSString getCString:maxLength:encoding:] == NO" userInfo:nil]; | |
377 | return string; | |
378 | } | |
b09da87b JF |
379 | } |
380 | ||
381 | JSValueRef CYCastJSValue(JSContextRef context, bool value) { | |
382 | return JSValueMakeBoolean(context, value); | |
383 | } | |
384 | ||
385 | JSValueRef CYCastJSValue(JSContextRef context, double value) { | |
386 | return JSValueMakeNumber(context, value); | |
387 | } | |
388 | ||
389 | #define CYCastJSValue_(Type_) \ | |
390 | JSValueRef CYCastJSValue(JSContextRef context, Type_ value) { \ | |
391 | return JSValueMakeNumber(context, static_cast<double>(value)); \ | |
392 | } | |
393 | ||
394 | CYCastJSValue_(int) | |
395 | CYCastJSValue_(unsigned int) | |
396 | CYCastJSValue_(long int) | |
397 | CYCastJSValue_(long unsigned int) | |
398 | CYCastJSValue_(long long int) | |
399 | CYCastJSValue_(long long unsigned int) | |
400 | ||
401 | JSValueRef CYJSUndefined(JSContextRef context) { | |
402 | return JSValueMakeUndefined(context); | |
0c862573 JF |
403 | } |
404 | ||
107e3ed0 | 405 | @interface NSMethodSignature (Cycript) |
7ba62cfd JF |
406 | - (NSString *) _typeString; |
407 | @end | |
408 | ||
107e3ed0 | 409 | @interface NSObject (Cycript) |
6b8a9500 | 410 | - (bool) cy$isUndefined; |
c1582939 | 411 | - (NSString *) cy$toJSON; |
4cf49641 | 412 | - (JSValueRef) cy$JSValueInContext:(JSContextRef)context transient:(bool)transient; |
cc103044 JF |
413 | - (NSObject *) cy$getProperty:(NSString *)name; |
414 | - (bool) cy$setProperty:(NSString *)name to:(NSObject *)value; | |
415 | - (bool) cy$deleteProperty:(NSString *)name; | |
c1582939 JF |
416 | @end |
417 | ||
107e3ed0 | 418 | @interface NSString (Cycript) |
88c977fa JF |
419 | - (void *) cy$symbol; |
420 | @end | |
421 | ||
107e3ed0 | 422 | @interface NSNumber (Cycript) |
88c977fa JF |
423 | - (void *) cy$symbol; |
424 | @end | |
425 | ||
107e3ed0 | 426 | @implementation NSObject (Cycript) |
62ca2b82 | 427 | |
6b8a9500 JF |
428 | - (bool) cy$isUndefined { |
429 | return false; | |
430 | } | |
431 | ||
c1582939 | 432 | - (NSString *) cy$toJSON { |
62ca2b82 JF |
433 | return [self description]; |
434 | } | |
435 | ||
4cf49641 JF |
436 | - (JSValueRef) cy$JSValueInContext:(JSContextRef)context transient:(bool)transient { |
437 | return CYMakeInstance(context, self, transient); | |
ea2d184c JF |
438 | } |
439 | ||
cc103044 | 440 | - (NSObject *) cy$getProperty:(NSString *)name { |
953647c1 JF |
441 | if (![name isEqualToString:@"prototype"]) |
442 | NSLog(@"get:%@", name); | |
cc103044 JF |
443 | return nil; |
444 | } | |
445 | ||
446 | - (bool) cy$setProperty:(NSString *)name to:(NSObject *)value { | |
447 | NSLog(@"set:%@", name); | |
448 | return false; | |
449 | } | |
450 | ||
451 | - (bool) cy$deleteProperty:(NSString *)name { | |
452 | NSLog(@"delete:%@", name); | |
453 | return false; | |
454 | } | |
455 | ||
62ca2b82 | 456 | @end |
c1582939 | 457 | |
107e3ed0 | 458 | @implementation WebUndefined (Cycript) |
62ca2b82 | 459 | |
6b8a9500 JF |
460 | - (bool) cy$isUndefined { |
461 | return true; | |
462 | } | |
463 | ||
c1582939 JF |
464 | - (NSString *) cy$toJSON { |
465 | return @"undefined"; | |
62ca2b82 JF |
466 | } |
467 | ||
4cf49641 | 468 | - (JSValueRef) cy$JSValueInContext:(JSContextRef)context transient:(bool)transient { |
b09da87b | 469 | return CYJSUndefined(context); |
62ca2b82 JF |
470 | } |
471 | ||
472 | @end | |
c1582939 | 473 | |
478d4ed0 JF |
474 | @implementation NSNull (Cycript) |
475 | ||
476 | - (NSString *) cy$toJSON { | |
477 | return @"null"; | |
478 | } | |
479 | ||
480 | @end | |
481 | ||
107e3ed0 | 482 | @implementation NSArray (Cycript) |
62ca2b82 | 483 | |
c1582939 JF |
484 | - (NSString *) cy$toJSON { |
485 | NSMutableString *json([[[NSMutableString alloc] init] autorelease]); | |
486 | [json appendString:@"["]; | |
487 | ||
488 | bool comma(false); | |
62ca2b82 | 489 | for (id object in self) { |
c1582939 JF |
490 | if (comma) |
491 | [json appendString:@","]; | |
492 | else | |
493 | comma = true; | |
6b8a9500 JF |
494 | if (![object cy$isUndefined]) |
495 | [json appendString:[object cy$toJSON]]; | |
496 | else { | |
497 | [json appendString:@","]; | |
498 | comma = false; | |
499 | } | |
c1582939 JF |
500 | } |
501 | ||
502 | [json appendString:@"]"]; | |
503 | return json; | |
62ca2b82 JF |
504 | } |
505 | ||
cc103044 JF |
506 | - (NSObject *) cy$getProperty:(NSString *)name { |
507 | int index([name intValue]); | |
508 | if (index < 0 || index >= static_cast<int>([self count])) | |
509 | return [super cy$getProperty:name]; | |
510 | else | |
511 | return [self objectAtIndex:index]; | |
512 | } | |
513 | ||
514 | @end | |
515 | ||
516 | @implementation NSMutableArray (Cycript) | |
517 | ||
518 | - (bool) cy$setProperty:(NSString *)name to:(NSObject *)value { | |
519 | int index([name intValue]); | |
520 | if (index < 0 || index >= static_cast<int>([self count])) | |
521 | return [super cy$setProperty:name to:value]; | |
522 | else { | |
b6ea08b6 | 523 | [self replaceObjectAtIndex:index withObject:(value ?: [NSNull null])]; |
cc103044 JF |
524 | return true; |
525 | } | |
526 | } | |
527 | ||
528 | - (bool) cy$deleteProperty:(NSString *)name { | |
529 | int index([name intValue]); | |
530 | if (index < 0 || index >= static_cast<int>([self count])) | |
531 | return [super cy$deleteProperty:name]; | |
532 | else { | |
533 | [self removeObjectAtIndex:index]; | |
534 | return true; | |
535 | } | |
536 | } | |
537 | ||
62ca2b82 JF |
538 | @end |
539 | ||
107e3ed0 | 540 | @implementation NSDictionary (Cycript) |
62ca2b82 JF |
541 | |
542 | - (NSString *) cy$toJSON { | |
543 | NSMutableString *json([[[NSMutableString alloc] init] autorelease]); | |
d35a3b07 | 544 | [json appendString:@"({"]; |
62ca2b82 JF |
545 | |
546 | bool comma(false); | |
547 | for (id key in self) { | |
548 | if (comma) | |
549 | [json appendString:@","]; | |
550 | else | |
551 | comma = true; | |
552 | [json appendString:[key cy$toJSON]]; | |
553 | [json appendString:@":"]; | |
554 | NSObject *object([self objectForKey:key]); | |
555 | [json appendString:[object cy$toJSON]]; | |
556 | } | |
557 | ||
558 | [json appendString:@"})"]; | |
559 | return json; | |
560 | } | |
561 | ||
cc103044 JF |
562 | - (NSObject *) cy$getProperty:(NSString *)name { |
563 | return [self objectForKey:name]; | |
564 | } | |
565 | ||
566 | @end | |
567 | ||
568 | @implementation NSMutableDictionary (Cycript) | |
569 | ||
570 | - (bool) cy$setProperty:(NSString *)name to:(NSObject *)value { | |
b6ea08b6 | 571 | [self setObject:(value ?: [NSNull null]) forKey:name]; |
cc103044 JF |
572 | return true; |
573 | } | |
574 | ||
575 | - (bool) cy$deleteProperty:(NSString *)name { | |
576 | if ([self objectForKey:name] == nil) | |
577 | return false; | |
578 | else { | |
579 | [self removeObjectForKey:name]; | |
580 | return true; | |
581 | } | |
582 | } | |
583 | ||
62ca2b82 | 584 | @end |
c1582939 | 585 | |
107e3ed0 | 586 | @implementation NSNumber (Cycript) |
62ca2b82 | 587 | |
c1582939 JF |
588 | - (NSString *) cy$toJSON { |
589 | return [self class] != NSCFBoolean_ ? [self stringValue] : [self boolValue] ? @"true" : @"false"; | |
62ca2b82 JF |
590 | } |
591 | ||
4cf49641 | 592 | - (JSValueRef) cy$JSValueInContext:(JSContextRef)context transient:(bool)transient { |
b09da87b | 593 | return [self class] != NSCFBoolean_ ? CYCastJSValue(context, [self doubleValue]) : CYCastJSValue(context, [self boolValue]); |
62ca2b82 JF |
594 | } |
595 | ||
88c977fa JF |
596 | - (void *) cy$symbol { |
597 | return [self pointerValue]; | |
598 | } | |
599 | ||
62ca2b82 | 600 | @end |
c1582939 | 601 | |
107e3ed0 | 602 | @implementation NSString (Cycript) |
62ca2b82 | 603 | |
c1582939 JF |
604 | - (NSString *) cy$toJSON { |
605 | CFMutableStringRef json(CFStringCreateMutableCopy(kCFAllocatorDefault, 0, (CFStringRef) self)); | |
606 | ||
c1582939 JF |
607 | CFStringFindAndReplace(json, CFSTR("\\"), CFSTR("\\\\"), CFRangeMake(0, CFStringGetLength(json)), 0); |
608 | CFStringFindAndReplace(json, CFSTR("\""), CFSTR("\\\""), CFRangeMake(0, CFStringGetLength(json)), 0); | |
609 | CFStringFindAndReplace(json, CFSTR("\t"), CFSTR("\\t"), CFRangeMake(0, CFStringGetLength(json)), 0); | |
610 | CFStringFindAndReplace(json, CFSTR("\r"), CFSTR("\\r"), CFRangeMake(0, CFStringGetLength(json)), 0); | |
611 | CFStringFindAndReplace(json, CFSTR("\n"), CFSTR("\\n"), CFRangeMake(0, CFStringGetLength(json)), 0); | |
612 | ||
613 | CFStringInsert(json, 0, CFSTR("\"")); | |
614 | CFStringAppend(json, CFSTR("\"")); | |
615 | ||
62ca2b82 JF |
616 | return [reinterpret_cast<const NSString *>(json) autorelease]; |
617 | } | |
618 | ||
88c977fa | 619 | - (void *) cy$symbol { |
478d4ed0 JF |
620 | CYPool pool; |
621 | return dlsym(RTLD_DEFAULT, CYPoolCString(pool, self)); | |
88c977fa JF |
622 | } |
623 | ||
62ca2b82 JF |
624 | @end |
625 | ||
b21525c7 | 626 | @interface CYJSObject : NSDictionary { |
62ca2b82 JF |
627 | JSObjectRef object_; |
628 | JSContextRef context_; | |
629 | } | |
630 | ||
631 | - (id) initWithJSObject:(JSObjectRef)object inContext:(JSContextRef)context; | |
632 | ||
633 | - (NSUInteger) count; | |
634 | - (id) objectForKey:(id)key; | |
635 | - (NSEnumerator *) keyEnumerator; | |
636 | - (void) setObject:(id)object forKey:(id)key; | |
637 | - (void) removeObjectForKey:(id)key; | |
638 | ||
639 | @end | |
c1582939 | 640 | |
b21525c7 | 641 | @interface CYJSArray : NSArray { |
c1582939 JF |
642 | JSObjectRef object_; |
643 | JSContextRef context_; | |
644 | } | |
645 | ||
646 | - (id) initWithJSObject:(JSObjectRef)object inContext:(JSContextRef)context; | |
647 | ||
648 | - (NSUInteger) count; | |
649 | - (id) objectAtIndex:(NSUInteger)index; | |
650 | ||
651 | @end | |
652 | ||
dea834b0 JF |
653 | CYRange WordStartRange_(0x1000000000LLU,0x7fffffe87fffffeLLU); // A-Za-z_$ |
654 | CYRange WordEndRange_(0x3ff001000000000LLU,0x7fffffe87fffffeLLU); // A-Za-z_$0-9 | |
655 | ||
478d4ed0 | 656 | JSGlobalContextRef CYGetJSContext() { |
ea2d184c | 657 | return Context_; |
62ca2b82 JF |
658 | } |
659 | ||
4cf49641 JF |
660 | #define CYTry \ |
661 | @try | |
0c862573 JF |
662 | #define CYCatch \ |
663 | @catch (id error) { \ | |
664 | CYThrow(context, error, exception); \ | |
665 | return NULL; \ | |
666 | } | |
667 | ||
ea2d184c JF |
668 | void CYThrow(JSContextRef context, JSValueRef value); |
669 | ||
b09da87b JF |
670 | apr_status_t CYPoolRelease_(void *data) { |
671 | id object(reinterpret_cast<id>(data)); | |
672 | [object release]; | |
673 | return APR_SUCCESS; | |
674 | } | |
675 | ||
676 | id CYPoolRelease(apr_pool_t *pool, id object) { | |
677 | if (pool == NULL) | |
678 | return [object autorelease]; | |
679 | else { | |
680 | apr_pool_cleanup_register(pool, object, &CYPoolRelease_, &apr_pool_cleanup_null); | |
681 | return object; | |
682 | } | |
683 | } | |
684 | ||
953647c1 JF |
685 | CFTypeRef CYPoolRelease(apr_pool_t *pool, CFTypeRef object) { |
686 | return (CFTypeRef) CYPoolRelease(pool, (id) object); | |
687 | } | |
688 | ||
b09da87b | 689 | id CYCastNSObject(apr_pool_t *pool, JSContextRef context, JSObjectRef object) { |
478d4ed0 | 690 | if (JSValueIsObjectOfClass(context, object, Instance_)) { |
953647c1 | 691 | Instance_privateData *data(reinterpret_cast<Instance_privateData *>(JSObjectGetPrivate(object))); |
478d4ed0 JF |
692 | return data->GetValue(); |
693 | } | |
694 | ||
ea2d184c JF |
695 | JSValueRef exception(NULL); |
696 | bool array(JSValueIsInstanceOfConstructor(context, object, Array_, &exception)); | |
697 | CYThrow(context, exception); | |
b09da87b JF |
698 | id value(array ? [CYJSArray alloc] : [CYJSObject alloc]); |
699 | return CYPoolRelease(pool, [value initWithJSObject:object inContext:context]); | |
62ca2b82 JF |
700 | } |
701 | ||
77e87a6c | 702 | JSStringRef CYCopyJSString(id value) { |
b09da87b | 703 | return value == NULL ? NULL : JSStringCreateWithCFString(reinterpret_cast<CFStringRef>([value description])); |
62ca2b82 JF |
704 | } |
705 | ||
77e87a6c | 706 | JSStringRef CYCopyJSString(const char *value) { |
b09da87b | 707 | return value == NULL ? NULL : JSStringCreateWithUTF8CString(value); |
77e87a6c JF |
708 | } |
709 | ||
710 | JSStringRef CYCopyJSString(JSStringRef value) { | |
b09da87b | 711 | return value == NULL ? NULL : JSStringRetain(value); |
77e87a6c JF |
712 | } |
713 | ||
714 | JSStringRef CYCopyJSString(JSContextRef context, JSValueRef value) { | |
b09da87b JF |
715 | if (JSValueIsNull(context, value)) |
716 | return NULL; | |
62ca2b82 | 717 | JSValueRef exception(NULL); |
ea2d184c JF |
718 | JSStringRef string(JSValueToStringCopy(context, value, &exception)); |
719 | CYThrow(context, exception); | |
77e87a6c JF |
720 | return string; |
721 | } | |
722 | ||
cf7d4c69 | 723 | class CYJSString { |
77e87a6c JF |
724 | private: |
725 | JSStringRef string_; | |
726 | ||
b09da87b JF |
727 | void Clear_() { |
728 | JSStringRelease(string_); | |
729 | } | |
730 | ||
77e87a6c | 731 | public: |
b09da87b JF |
732 | CYJSString(const CYJSString &rhs) : |
733 | string_(CYCopyJSString(rhs.string_)) | |
734 | { | |
735 | } | |
736 | ||
77e87a6c | 737 | template <typename Arg0_> |
b09da87b JF |
738 | CYJSString(Arg0_ arg0) : |
739 | string_(CYCopyJSString(arg0)) | |
740 | { | |
77e87a6c JF |
741 | } |
742 | ||
743 | template <typename Arg0_, typename Arg1_> | |
b09da87b JF |
744 | CYJSString(Arg0_ arg0, Arg1_ arg1) : |
745 | string_(CYCopyJSString(arg0, arg1)) | |
746 | { | |
747 | } | |
748 | ||
749 | CYJSString &operator =(const CYJSString &rhs) { | |
750 | Clear_(); | |
751 | string_ = CYCopyJSString(rhs.string_); | |
752 | return *this; | |
77e87a6c JF |
753 | } |
754 | ||
cf7d4c69 | 755 | ~CYJSString() { |
b09da87b JF |
756 | Clear_(); |
757 | } | |
758 | ||
759 | void Clear() { | |
760 | Clear_(); | |
761 | string_ = NULL; | |
77e87a6c JF |
762 | } |
763 | ||
764 | operator JSStringRef() const { | |
765 | return string_; | |
766 | } | |
767 | }; | |
768 | ||
769 | CFStringRef CYCopyCFString(JSStringRef value) { | |
770 | return JSStringCopyCFString(kCFAllocatorDefault, value); | |
771 | } | |
772 | ||
773 | CFStringRef CYCopyCFString(JSContextRef context, JSValueRef value) { | |
cf7d4c69 | 774 | return CYCopyCFString(CYJSString(context, value)); |
c1582939 JF |
775 | } |
776 | ||
bd17e6f3 JF |
777 | double CYCastDouble(const char *value, size_t size) { |
778 | char *end; | |
779 | double number(strtod(value, &end)); | |
780 | if (end != value + size) | |
781 | return NAN; | |
782 | return number; | |
783 | } | |
784 | ||
785 | double CYCastDouble(const char *value) { | |
786 | return CYCastDouble(value, strlen(value)); | |
787 | } | |
788 | ||
f610e1a0 | 789 | double CYCastDouble(JSContextRef context, JSValueRef value) { |
0c862573 JF |
790 | JSValueRef exception(NULL); |
791 | double number(JSValueToNumber(context, value, &exception)); | |
792 | CYThrow(context, exception); | |
f610e1a0 JF |
793 | return number; |
794 | } | |
795 | ||
796 | CFNumberRef CYCopyCFNumber(JSContextRef context, JSValueRef value) { | |
797 | double number(CYCastDouble(context, value)); | |
0c862573 JF |
798 | return CFNumberCreate(kCFAllocatorDefault, kCFNumberDoubleType, &number); |
799 | } | |
800 | ||
953647c1 JF |
801 | CFStringRef CYCopyCFString(const char *value) { |
802 | return CFStringCreateWithCString(kCFAllocatorDefault, value, kCFStringEncodingUTF8); | |
803 | } | |
804 | ||
805 | NSString *CYCastNSString(apr_pool_t *pool, const char *value) { | |
806 | return (NSString *) CYPoolRelease(pool, CYCopyCFString(value)); | |
807 | } | |
808 | ||
b09da87b | 809 | NSString *CYCastNSString(apr_pool_t *pool, JSStringRef value) { |
953647c1 | 810 | return (NSString *) CYPoolRelease(pool, CYCopyCFString(value)); |
b09da87b JF |
811 | } |
812 | ||
813 | bool CYCastBool(JSContextRef context, JSValueRef value) { | |
814 | return JSValueToBoolean(context, value); | |
62ca2b82 JF |
815 | } |
816 | ||
b09da87b JF |
817 | CFTypeRef CYCFType(apr_pool_t *pool, JSContextRef context, JSValueRef value, bool cast) { |
818 | CFTypeRef object; | |
819 | bool copy; | |
820 | ||
f610e1a0 | 821 | switch (JSType type = JSValueGetType(context, value)) { |
c1582939 | 822 | case kJSTypeUndefined: |
b09da87b JF |
823 | object = [WebUndefined undefined]; |
824 | copy = false; | |
825 | break; | |
826 | ||
c1582939 | 827 | case kJSTypeNull: |
b09da87b JF |
828 | return NULL; |
829 | break; | |
830 | ||
c1582939 | 831 | case kJSTypeBoolean: |
b09da87b JF |
832 | object = CYCastBool(context, value) ? kCFBooleanTrue : kCFBooleanFalse; |
833 | copy = false; | |
834 | break; | |
835 | ||
0c862573 | 836 | case kJSTypeNumber: |
b09da87b JF |
837 | object = CYCopyCFNumber(context, value); |
838 | copy = true; | |
839 | break; | |
840 | ||
62ca2b82 | 841 | case kJSTypeString: |
b09da87b JF |
842 | object = CYCopyCFString(context, value); |
843 | copy = true; | |
844 | break; | |
845 | ||
c1582939 | 846 | case kJSTypeObject: |
b09da87b JF |
847 | // XXX: this might could be more efficient |
848 | object = (CFTypeRef) CYCastNSObject(pool, context, (JSObjectRef) value); | |
849 | copy = false; | |
850 | break; | |
851 | ||
c1582939 | 852 | default: |
f5e9be24 | 853 | @throw [NSException exceptionWithName:NSInternalInconsistencyException reason:[NSString stringWithFormat:@"JSValueGetType() == 0x%x", type] userInfo:nil]; |
b09da87b | 854 | break; |
c1582939 | 855 | } |
b09da87b JF |
856 | |
857 | if (cast != copy) | |
858 | return object; | |
859 | else if (copy) | |
953647c1 | 860 | return CYPoolRelease(pool, object); |
b09da87b JF |
861 | else |
862 | return CFRetain(object); | |
863 | } | |
864 | ||
865 | CFTypeRef CYCastCFType(apr_pool_t *pool, JSContextRef context, JSValueRef value) { | |
866 | return CYCFType(pool, context, value, true); | |
867 | } | |
868 | ||
869 | CFTypeRef CYCopyCFType(apr_pool_t *pool, JSContextRef context, JSValueRef value) { | |
870 | return CYCFType(pool, context, value, false); | |
c1582939 JF |
871 | } |
872 | ||
62ca2b82 | 873 | NSArray *CYCastNSArray(JSPropertyNameArrayRef names) { |
b09da87b | 874 | CYPool pool; |
62ca2b82 JF |
875 | size_t size(JSPropertyNameArrayGetCount(names)); |
876 | NSMutableArray *array([NSMutableArray arrayWithCapacity:size]); | |
877 | for (size_t index(0); index != size; ++index) | |
b09da87b | 878 | [array addObject:CYCastNSString(pool, JSPropertyNameArrayGetNameAtIndex(names, index))]; |
62ca2b82 JF |
879 | return array; |
880 | } | |
881 | ||
b09da87b JF |
882 | id CYCastNSObject(apr_pool_t *pool, JSContextRef context, JSValueRef value) { |
883 | return reinterpret_cast<const NSObject *>(CYCastCFType(pool, context, value)); | |
c1582939 JF |
884 | } |
885 | ||
ea2d184c | 886 | void CYThrow(JSContextRef context, JSValueRef value) { |
62ca2b82 JF |
887 | if (value == NULL) |
888 | return; | |
b09da87b JF |
889 | @throw CYCastNSObject(NULL, context, value); |
890 | } | |
891 | ||
892 | JSValueRef CYJSNull(JSContextRef context) { | |
893 | return JSValueMakeNull(context); | |
894 | } | |
895 | ||
896 | JSValueRef CYCastJSValue(JSContextRef context, JSStringRef value) { | |
897 | return value == NULL ? CYJSNull(context) : JSValueMakeString(context, value); | |
898 | } | |
899 | ||
900 | JSValueRef CYCastJSValue(JSContextRef context, const char *value) { | |
901 | return CYCastJSValue(context, CYJSString(value)); | |
62ca2b82 JF |
902 | } |
903 | ||
4cf49641 JF |
904 | JSValueRef CYCastJSValue(JSContextRef context, id value, bool transient = true) { |
905 | return value == nil ? CYJSNull(context) : [value cy$JSValueInContext:context transient:transient]; | |
62ca2b82 JF |
906 | } |
907 | ||
dea834b0 JF |
908 | JSObjectRef CYCastJSObject(JSContextRef context, JSValueRef value) { |
909 | JSValueRef exception(NULL); | |
910 | JSObjectRef object(JSValueToObject(context, value, &exception)); | |
911 | CYThrow(context, exception); | |
912 | return object; | |
913 | } | |
914 | ||
bd17e6f3 JF |
915 | JSValueRef CYGetProperty(JSContextRef context, JSObjectRef object, size_t index) { |
916 | JSValueRef exception(NULL); | |
917 | JSValueRef value(JSObjectGetPropertyAtIndex(context, object, index, &exception)); | |
918 | CYThrow(context, exception); | |
919 | return value; | |
920 | } | |
921 | ||
dea834b0 JF |
922 | JSValueRef CYGetProperty(JSContextRef context, JSObjectRef object, JSStringRef name) { |
923 | JSValueRef exception(NULL); | |
924 | JSValueRef value(JSObjectGetProperty(context, object, name, &exception)); | |
925 | CYThrow(context, exception); | |
926 | return value; | |
927 | } | |
928 | ||
929 | void CYSetProperty(JSContextRef context, JSObjectRef object, JSStringRef name, JSValueRef value) { | |
930 | JSValueRef exception(NULL); | |
931 | JSObjectSetProperty(context, object, name, value, kJSPropertyAttributeNone, &exception); | |
932 | CYThrow(context, exception); | |
933 | } | |
934 | ||
30ddc20c | 935 | void CYThrow(JSContextRef context, id error, JSValueRef *exception) { |
4cf49641 JF |
936 | if (exception == NULL) |
937 | throw error; | |
7ba62cfd JF |
938 | *exception = CYCastJSValue(context, error); |
939 | } | |
940 | ||
b21525c7 | 941 | @implementation CYJSObject |
62ca2b82 JF |
942 | |
943 | - (id) initWithJSObject:(JSObjectRef)object inContext:(JSContextRef)context { | |
944 | if ((self = [super init]) != nil) { | |
945 | object_ = object; | |
946 | context_ = context; | |
947 | } return self; | |
948 | } | |
949 | ||
950 | - (NSUInteger) count { | |
951 | JSPropertyNameArrayRef names(JSObjectCopyPropertyNames(context_, object_)); | |
952 | size_t size(JSPropertyNameArrayGetCount(names)); | |
953 | JSPropertyNameArrayRelease(names); | |
954 | return size; | |
955 | } | |
956 | ||
957 | - (id) objectForKey:(id)key { | |
478d4ed0 | 958 | return CYCastNSObject(NULL, context_, CYGetProperty(context_, object_, CYJSString(key))) ?: [NSNull null]; |
62ca2b82 JF |
959 | } |
960 | ||
961 | - (NSEnumerator *) keyEnumerator { | |
962 | JSPropertyNameArrayRef names(JSObjectCopyPropertyNames(context_, object_)); | |
963 | NSEnumerator *enumerator([CYCastNSArray(names) objectEnumerator]); | |
964 | JSPropertyNameArrayRelease(names); | |
965 | return enumerator; | |
966 | } | |
967 | ||
968 | - (void) setObject:(id)object forKey:(id)key { | |
dea834b0 | 969 | CYSetProperty(context_, object_, CYJSString(key), CYCastJSValue(context_, object)); |
62ca2b82 JF |
970 | } |
971 | ||
972 | - (void) removeObjectForKey:(id)key { | |
973 | JSValueRef exception(NULL); | |
f610e1a0 | 974 | // XXX: this returns a bool... throw exception, or ignore? |
cf7d4c69 | 975 | JSObjectDeleteProperty(context_, object_, CYJSString(key), &exception); |
62ca2b82 JF |
976 | CYThrow(context_, exception); |
977 | } | |
978 | ||
979 | @end | |
980 | ||
b21525c7 | 981 | @implementation CYJSArray |
c1582939 JF |
982 | |
983 | - (id) initWithJSObject:(JSObjectRef)object inContext:(JSContextRef)context { | |
984 | if ((self = [super init]) != nil) { | |
985 | object_ = object; | |
986 | context_ = context; | |
987 | } return self; | |
988 | } | |
989 | ||
990 | - (NSUInteger) count { | |
dea834b0 | 991 | return CYCastDouble(context_, CYGetProperty(context_, object_, length_)); |
c1582939 JF |
992 | } |
993 | ||
994 | - (id) objectAtIndex:(NSUInteger)index { | |
62ca2b82 JF |
995 | JSValueRef exception(NULL); |
996 | JSValueRef value(JSObjectGetPropertyAtIndex(context_, object_, index, &exception)); | |
997 | CYThrow(context_, exception); | |
478d4ed0 | 998 | return CYCastNSObject(NULL, context_, value) ?: [NSNull null]; |
c1582939 JF |
999 | } |
1000 | ||
1001 | @end | |
1002 | ||
4cf49641 JF |
1003 | CFStringRef CYCopyJSONString(JSContextRef context, JSValueRef value, JSValueRef *exception) { |
1004 | CYTry { | |
1005 | CYPoolTry { | |
1006 | id object(CYCastNSObject(NULL, context, value)); | |
1007 | return reinterpret_cast<CFStringRef>([(object == nil ? @"null" : [object cy$toJSON]) retain]); | |
1008 | } CYPoolCatch(NULL) | |
1009 | } CYCatch | |
c1582939 JF |
1010 | } |
1011 | ||
4cf49641 JF |
1012 | const char *CYPoolJSONString(apr_pool_t *pool, JSContextRef context, JSValueRef value, JSValueRef *exception) { |
1013 | if (NSString *json = (NSString *) CYCopyJSONString(context, value, exception)) { | |
1014 | const char *string(CYPoolCString(pool, json)); | |
1015 | [json release]; | |
1016 | return string; | |
1017 | } else return NULL; | |
478d4ed0 JF |
1018 | } |
1019 | ||
c1582939 JF |
1020 | static void OnData(CFSocketRef socket, CFSocketCallBackType type, CFDataRef address, const void *value, void *info) { |
1021 | switch (type) { | |
1022 | case kCFSocketDataCallBack: | |
1023 | CFDataRef data(reinterpret_cast<CFDataRef>(value)); | |
1024 | Client *client(reinterpret_cast<Client *>(info)); | |
1025 | ||
1026 | if (client->message_ == NULL) | |
1027 | client->message_ = CFHTTPMessageCreateEmpty(kCFAllocatorDefault, TRUE); | |
1028 | ||
1029 | if (!CFHTTPMessageAppendBytes(client->message_, CFDataGetBytePtr(data), CFDataGetLength(data))) | |
1030 | CFLog(kCFLogLevelError, CFSTR("CFHTTPMessageAppendBytes()")); | |
1031 | else if (CFHTTPMessageIsHeaderComplete(client->message_)) { | |
1032 | CFURLRef url(CFHTTPMessageCopyRequestURL(client->message_)); | |
1033 | Boolean absolute; | |
1034 | CFStringRef path(CFURLCopyStrictPath(url, &absolute)); | |
1035 | CFRelease(client->message_); | |
1036 | ||
1037 | CFStringRef code(CFURLCreateStringByReplacingPercentEscapes(kCFAllocatorDefault, path, CFSTR(""))); | |
1038 | CFRelease(path); | |
1039 | ||
1040 | JSStringRef script(JSStringCreateWithCFString(code)); | |
1041 | CFRelease(code); | |
1042 | ||
057f943f | 1043 | JSValueRef result(JSEvaluateScript(CYGetJSContext(), script, NULL, NULL, 0, NULL)); |
c1582939 JF |
1044 | JSStringRelease(script); |
1045 | ||
1046 | CFHTTPMessageRef response(CFHTTPMessageCreateResponse(kCFAllocatorDefault, 200, NULL, kCFHTTPVersion1_1)); | |
1047 | CFHTTPMessageSetHeaderFieldValue(response, CFSTR("Content-Type"), CFSTR("application/json; charset=utf-8")); | |
1048 | ||
4cf49641 | 1049 | CFStringRef json(CYCopyJSONString(CYGetJSContext(), result, NULL)); |
c1582939 JF |
1050 | CFDataRef body(CFStringCreateExternalRepresentation(kCFAllocatorDefault, json, kCFStringEncodingUTF8, NULL)); |
1051 | CFRelease(json); | |
1052 | ||
1053 | CFStringRef length(CFStringCreateWithFormat(kCFAllocatorDefault, NULL, CFSTR("%u"), CFDataGetLength(body))); | |
1054 | CFHTTPMessageSetHeaderFieldValue(response, CFSTR("Content-Length"), length); | |
1055 | CFRelease(length); | |
1056 | ||
1057 | CFHTTPMessageSetBody(response, body); | |
1058 | CFRelease(body); | |
1059 | ||
1060 | CFDataRef serialized(CFHTTPMessageCopySerializedMessage(response)); | |
1061 | CFRelease(response); | |
1062 | ||
1063 | CFSocketSendData(socket, NULL, serialized, 0); | |
1064 | CFRelease(serialized); | |
1065 | ||
1066 | CFRelease(url); | |
1067 | } | |
1068 | break; | |
1069 | } | |
1070 | } | |
1071 | ||
1072 | static void OnAccept(CFSocketRef socket, CFSocketCallBackType type, CFDataRef address, const void *value, void *info) { | |
1073 | switch (type) { | |
1074 | case kCFSocketAcceptCallBack: | |
1075 | Client *client(new Client()); | |
1076 | ||
1077 | client->message_ = NULL; | |
1078 | ||
1079 | CFSocketContext context; | |
1080 | context.version = 0; | |
1081 | context.info = client; | |
1082 | context.retain = NULL; | |
1083 | context.release = NULL; | |
1084 | context.copyDescription = NULL; | |
1085 | ||
1086 | client->socket_ = CFSocketCreateWithNative(kCFAllocatorDefault, *reinterpret_cast<const CFSocketNativeHandle *>(value), kCFSocketDataCallBack, &OnData, &context); | |
1087 | ||
1088 | CFRunLoopAddSource(CFRunLoopGetCurrent(), CFSocketCreateRunLoopSource(kCFAllocatorDefault, client->socket_, 0), kCFRunLoopDefaultMode); | |
1089 | break; | |
1090 | } | |
1091 | } | |
1092 | ||
b09da87b | 1093 | static JSValueRef Instance_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { |
4cf49641 | 1094 | CYTry { |
b09da87b | 1095 | CYPool pool; |
cc103044 | 1096 | NSString *self(CYCastNSObject(pool, context, object)); |
b09da87b | 1097 | NSString *name(CYCastNSString(pool, property)); |
cc103044 JF |
1098 | NSObject *data([self cy$getProperty:name]); |
1099 | return data == nil ? NULL : CYCastJSValue(context, data); | |
f610e1a0 | 1100 | } CYCatch |
c1582939 JF |
1101 | } |
1102 | ||
b09da87b | 1103 | static bool Instance_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) { |
4cf49641 | 1104 | CYTry { |
b09da87b | 1105 | CYPool pool; |
cc103044 | 1106 | NSString *self(CYCastNSObject(pool, context, object)); |
b09da87b | 1107 | NSString *name(CYCastNSString(pool, property)); |
cc103044 JF |
1108 | NSString *data(CYCastNSObject(pool, context, value)); |
1109 | return [self cy$setProperty:name to:data]; | |
b09da87b JF |
1110 | } CYCatch |
1111 | } | |
1112 | ||
1113 | static bool Instance_deleteProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { | |
4cf49641 | 1114 | CYTry { |
b09da87b | 1115 | CYPool pool; |
cc103044 | 1116 | NSString *self(CYCastNSObject(pool, context, object)); |
b09da87b | 1117 | NSString *name(CYCastNSString(pool, property)); |
cc103044 | 1118 | return [self cy$deleteProperty:name]; |
b09da87b JF |
1119 | } CYCatch |
1120 | } | |
1121 | ||
b09da87b | 1122 | static JSObjectRef Instance_callAsConstructor(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { |
4cf49641 | 1123 | CYTry { |
953647c1 | 1124 | Instance_privateData *data(reinterpret_cast<Instance_privateData *>(JSObjectGetPrivate(object))); |
4cf49641 | 1125 | return CYMakeInstance(context, [data->GetValue() alloc], true); |
0c862573 JF |
1126 | } CYCatch |
1127 | } | |
1128 | ||
dea834b0 | 1129 | JSObjectRef CYMakeSelector(JSContextRef context, SEL sel) { |
953647c1 | 1130 | Selector_privateData *data(new Selector_privateData(sel)); |
dea834b0 JF |
1131 | return JSObjectMake(context, Selector_, data); |
1132 | } | |
1133 | ||
1134 | JSObjectRef CYMakePointer(JSContextRef context, void *pointer) { | |
953647c1 | 1135 | Pointer_privateData *data(new Pointer_privateData(pointer)); |
dea834b0 JF |
1136 | return JSObjectMake(context, Pointer_, data); |
1137 | } | |
1138 | ||
b09da87b | 1139 | JSObjectRef CYMakeFunctor(JSContextRef context, void (*function)(), const char *type) { |
953647c1 | 1140 | Functor_privateData *data(new Functor_privateData(type, function)); |
88c977fa JF |
1141 | return JSObjectMake(context, Functor_, data); |
1142 | } | |
1143 | ||
bd17e6f3 JF |
1144 | const char *CYPoolCString(apr_pool_t *pool, JSStringRef value, size_t *length = NULL) { |
1145 | if (pool == NULL) { | |
1146 | const char *string([CYCastNSString(NULL, value) UTF8String]); | |
1147 | if (length != NULL) | |
1148 | *length = strlen(string); | |
1149 | return string; | |
1150 | } else { | |
478d4ed0 JF |
1151 | size_t size(JSStringGetMaximumUTF8CStringSize(value)); |
1152 | char *string(new(pool) char[size]); | |
1153 | JSStringGetUTF8CString(value, string, size); | |
bd17e6f3 JF |
1154 | // XXX: this is ironic |
1155 | if (length != NULL) | |
1156 | *length = strlen(string); | |
478d4ed0 JF |
1157 | return string; |
1158 | } | |
7ba62cfd JF |
1159 | } |
1160 | ||
bd17e6f3 JF |
1161 | const char *CYPoolCString(apr_pool_t *pool, JSContextRef context, JSValueRef value, size_t *length = NULL) { |
1162 | if (!JSValueIsNull(context, value)) | |
1163 | return CYPoolCString(pool, CYJSString(context, value), length); | |
1164 | else { | |
1165 | if (length != NULL) | |
1166 | *length = 0; | |
b09da87b | 1167 | return NULL; |
bd17e6f3 | 1168 | } |
77e87a6c JF |
1169 | } |
1170 | ||
f610e1a0 | 1171 | // XXX: this macro is unhygenic |
b21525c7 | 1172 | #define CYCastCString(context, value) ({ \ |
b09da87b JF |
1173 | char *utf8; \ |
1174 | if (value == NULL) \ | |
1175 | utf8 = NULL; \ | |
478d4ed0 | 1176 | else if (JSStringRef string = CYCopyJSString(context, value)) { \ |
b09da87b JF |
1177 | size_t size(JSStringGetMaximumUTF8CStringSize(string)); \ |
1178 | utf8 = reinterpret_cast<char *>(alloca(size)); \ | |
1179 | JSStringGetUTF8CString(string, utf8, size); \ | |
1180 | JSStringRelease(string); \ | |
478d4ed0 JF |
1181 | } else \ |
1182 | utf8 = NULL; \ | |
b21525c7 JF |
1183 | utf8; \ |
1184 | }) | |
1185 | ||
7ba62cfd JF |
1186 | SEL CYCastSEL(JSContextRef context, JSValueRef value) { |
1187 | if (JSValueIsNull(context, value)) | |
1188 | return NULL; | |
88c977fa | 1189 | else if (JSValueIsObjectOfClass(context, value, Selector_)) { |
953647c1 | 1190 | Selector_privateData *data(reinterpret_cast<Selector_privateData *>(JSObjectGetPrivate((JSObjectRef) value))); |
77e87a6c JF |
1191 | return reinterpret_cast<SEL>(data->value_); |
1192 | } else | |
b21525c7 JF |
1193 | return sel_registerName(CYCastCString(context, value)); |
1194 | } | |
1195 | ||
b09da87b | 1196 | void *CYCastPointer_(JSContextRef context, JSValueRef value) { |
b21525c7 JF |
1197 | switch (JSValueGetType(context, value)) { |
1198 | case kJSTypeNull: | |
1199 | return NULL; | |
953647c1 | 1200 | /*case kJSTypeString: |
b21525c7 | 1201 | return dlsym(RTLD_DEFAULT, CYCastCString(context, value)); |
b21525c7 | 1202 | case kJSTypeObject: |
88c977fa | 1203 | if (JSValueIsObjectOfClass(context, value, Pointer_)) { |
953647c1 | 1204 | Pointer_privateData *data(reinterpret_cast<Pointer_privateData *>(JSObjectGetPrivate((JSObjectRef) value))); |
b21525c7 | 1205 | return data->value_; |
953647c1 | 1206 | }*/ |
b21525c7 | 1207 | default: |
953647c1 | 1208 | double number(CYCastDouble(context, value)); |
bd17e6f3 | 1209 | if (std::isnan(number)) |
953647c1 JF |
1210 | @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"cannot convert value to pointer" userInfo:nil]; |
1211 | return reinterpret_cast<void *>(static_cast<uintptr_t>(static_cast<long long>(number))); | |
7ba62cfd JF |
1212 | } |
1213 | } | |
1214 | ||
b09da87b JF |
1215 | template <typename Type_> |
1216 | _finline Type_ CYCastPointer(JSContextRef context, JSValueRef value) { | |
1217 | return reinterpret_cast<Type_>(CYCastPointer_(context, value)); | |
1218 | } | |
1219 | ||
bd17e6f3 | 1220 | void CYPoolFFI(apr_pool_t *pool, JSContextRef context, sig::Type *type, ffi_type *ffi, void *data, JSValueRef value) { |
ea2d184c JF |
1221 | switch (type->primitive) { |
1222 | case sig::boolean_P: | |
1223 | *reinterpret_cast<bool *>(data) = JSValueToBoolean(context, value); | |
1224 | break; | |
1225 | ||
43cb3d68 | 1226 | #define CYPoolFFI_(primitive, native) \ |
f610e1a0 JF |
1227 | case sig::primitive ## _P: \ |
1228 | *reinterpret_cast<native *>(data) = CYCastDouble(context, value); \ | |
1229 | break; | |
ea2d184c | 1230 | |
43cb3d68 JF |
1231 | CYPoolFFI_(uchar, unsigned char) |
1232 | CYPoolFFI_(char, char) | |
1233 | CYPoolFFI_(ushort, unsigned short) | |
1234 | CYPoolFFI_(short, short) | |
1235 | CYPoolFFI_(ulong, unsigned long) | |
1236 | CYPoolFFI_(long, long) | |
1237 | CYPoolFFI_(uint, unsigned int) | |
1238 | CYPoolFFI_(int, int) | |
1239 | CYPoolFFI_(ulonglong, unsigned long long) | |
1240 | CYPoolFFI_(longlong, long long) | |
1241 | CYPoolFFI_(float, float) | |
1242 | CYPoolFFI_(double, double) | |
ea2d184c JF |
1243 | |
1244 | case sig::object_P: | |
1245 | case sig::typename_P: | |
b09da87b | 1246 | *reinterpret_cast<id *>(data) = CYCastNSObject(pool, context, value); |
7ba62cfd JF |
1247 | break; |
1248 | ||
ea2d184c | 1249 | case sig::selector_P: |
7ba62cfd JF |
1250 | *reinterpret_cast<SEL *>(data) = CYCastSEL(context, value); |
1251 | break; | |
ea2d184c | 1252 | |
b21525c7 | 1253 | case sig::pointer_P: |
b09da87b | 1254 | *reinterpret_cast<void **>(data) = CYCastPointer<void *>(context, value); |
b21525c7 | 1255 | break; |
ea2d184c | 1256 | |
77e87a6c | 1257 | case sig::string_P: |
478d4ed0 | 1258 | *reinterpret_cast<const char **>(data) = CYPoolCString(pool, context, value); |
77e87a6c | 1259 | break; |
7ba62cfd | 1260 | |
bd17e6f3 JF |
1261 | case sig::struct_P: { |
1262 | uint8_t *base(reinterpret_cast<uint8_t *>(data)); | |
1263 | bool aggregate(JSValueIsObject(context, value)); | |
1264 | for (size_t index(0); index != type->data.signature.count; ++index) { | |
4e8c99fb | 1265 | ffi_type *element(ffi->elements[index]); |
bd17e6f3 | 1266 | JSValueRef rhs(aggregate ? CYGetProperty(context, (JSObjectRef) value, index) : value); |
4e8c99fb JF |
1267 | CYPoolFFI(pool, context, type->data.signature.elements[index].type, element, base, rhs); |
1268 | // XXX: alignment? | |
1269 | base += element->size; | |
bd17e6f3 JF |
1270 | } |
1271 | } break; | |
ea2d184c JF |
1272 | |
1273 | case sig::void_P: | |
1274 | break; | |
1275 | ||
bd17e6f3 | 1276 | default: |
43cb3d68 | 1277 | NSLog(@"CYPoolFFI(%c)\n", type->primitive); |
ea2d184c JF |
1278 | _assert(false); |
1279 | } | |
1280 | } | |
1281 | ||
bd17e6f3 | 1282 | JSValueRef CYFromFFI(JSContextRef context, sig::Type *type, ffi_type *ffi, void *data, JSObjectRef owner = NULL) { |
ea2d184c JF |
1283 | JSValueRef value; |
1284 | ||
1285 | switch (type->primitive) { | |
1286 | case sig::boolean_P: | |
b09da87b | 1287 | value = CYCastJSValue(context, *reinterpret_cast<bool *>(data)); |
ea2d184c JF |
1288 | break; |
1289 | ||
1290 | #define CYFromFFI_(primitive, native) \ | |
1291 | case sig::primitive ## _P: \ | |
b09da87b | 1292 | value = CYCastJSValue(context, *reinterpret_cast<native *>(data)); \ |
ea2d184c JF |
1293 | break; |
1294 | ||
1295 | CYFromFFI_(uchar, unsigned char) | |
1296 | CYFromFFI_(char, char) | |
1297 | CYFromFFI_(ushort, unsigned short) | |
1298 | CYFromFFI_(short, short) | |
1299 | CYFromFFI_(ulong, unsigned long) | |
1300 | CYFromFFI_(long, long) | |
1301 | CYFromFFI_(uint, unsigned int) | |
1302 | CYFromFFI_(int, int) | |
1303 | CYFromFFI_(ulonglong, unsigned long long) | |
1304 | CYFromFFI_(longlong, long long) | |
1305 | CYFromFFI_(float, float) | |
1306 | CYFromFFI_(double, double) | |
1307 | ||
1308 | case sig::object_P: | |
ea2d184c | 1309 | value = CYCastJSValue(context, *reinterpret_cast<id *>(data)); |
dea834b0 JF |
1310 | break; |
1311 | ||
b09da87b | 1312 | case sig::typename_P: |
4cf49641 | 1313 | value = CYMakeInstance(context, *reinterpret_cast<Class *>(data), true); |
b09da87b JF |
1314 | break; |
1315 | ||
dea834b0 JF |
1316 | case sig::selector_P: |
1317 | if (SEL sel = *reinterpret_cast<SEL *>(data)) | |
1318 | value = CYMakeSelector(context, sel); | |
1319 | else goto null; | |
1320 | break; | |
1321 | ||
1322 | case sig::pointer_P: | |
1323 | if (void *pointer = *reinterpret_cast<void **>(data)) | |
1324 | value = CYMakePointer(context, pointer); | |
1325 | else goto null; | |
1326 | break; | |
1327 | ||
1328 | case sig::string_P: | |
f610e1a0 | 1329 | if (char *utf8 = *reinterpret_cast<char **>(data)) |
b09da87b | 1330 | value = CYCastJSValue(context, utf8); |
f610e1a0 | 1331 | else goto null; |
dea834b0 | 1332 | break; |
ea2d184c JF |
1333 | |
1334 | case sig::struct_P: | |
bd17e6f3 JF |
1335 | value = CYMakeStruct(context, data, type, ffi, owner); |
1336 | break; | |
ea2d184c JF |
1337 | |
1338 | case sig::void_P: | |
b09da87b | 1339 | value = CYJSUndefined(context); |
f610e1a0 JF |
1340 | break; |
1341 | ||
1342 | null: | |
b09da87b | 1343 | value = CYJSNull(context); |
ea2d184c JF |
1344 | break; |
1345 | ||
bd17e6f3 | 1346 | default: |
ea2d184c JF |
1347 | NSLog(@"CYFromFFI(%c)\n", type->primitive); |
1348 | _assert(false); | |
1349 | } | |
1350 | ||
1351 | return value; | |
1352 | } | |
1353 | ||
bd17e6f3 JF |
1354 | void Index_(Struct_privateData *internal, double number, ssize_t &index, uint8_t *&base) { |
1355 | Type_privateData *typical(internal->type_); | |
1356 | ||
1357 | index = static_cast<ssize_t>(number); | |
1358 | if (index != number) | |
1359 | @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"struct index non-integral" userInfo:nil]; | |
1360 | if (index < 0) | |
1361 | @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"struct index negative" userInfo:nil]; | |
1362 | ||
1363 | base = reinterpret_cast<uint8_t *>(internal->value_); | |
1364 | for (ssize_t local(0); local != index; ++local) | |
1365 | if (ffi_type *element = typical->ffi_.elements[local]) | |
1366 | base += element->size; | |
1367 | else | |
1368 | @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"struct index out-of-range" userInfo:nil]; | |
1369 | } | |
1370 | ||
1371 | static JSValueRef Struct_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { | |
1372 | CYTry { | |
1373 | CYPool pool; | |
1374 | Struct_privateData *internal(reinterpret_cast<Struct_privateData *>(JSObjectGetPrivate(object))); | |
1375 | Type_privateData *typical(internal->type_); | |
1376 | ||
1377 | size_t length; | |
1378 | const char *name(CYPoolCString(pool, property, &length)); | |
1379 | double number(CYCastDouble(name, length)); | |
1380 | ||
1381 | if (std::isnan(number)) { | |
1382 | // XXX: implement! | |
1383 | return NULL; | |
1384 | } | |
1385 | ||
1386 | ssize_t index; | |
1387 | uint8_t *base; | |
1388 | ||
1389 | Index_(internal, number, index, base); | |
1390 | ||
1391 | return CYFromFFI(context, typical->type_.data.signature.elements[index].type, typical->ffi_.elements[index], base, object); | |
1392 | } CYCatch | |
1393 | } | |
1394 | ||
1395 | static bool Struct_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) { | |
1396 | CYTry { | |
1397 | CYPool pool; | |
1398 | Struct_privateData *internal(reinterpret_cast<Struct_privateData *>(JSObjectGetPrivate(object))); | |
1399 | Type_privateData *typical(internal->type_); | |
1400 | ||
1401 | size_t length; | |
1402 | const char *name(CYPoolCString(pool, property, &length)); | |
1403 | double number(CYCastDouble(name, length)); | |
1404 | ||
1405 | if (std::isnan(number)) { | |
1406 | // XXX: implement! | |
1407 | return false; | |
1408 | } | |
1409 | ||
1410 | ssize_t index; | |
1411 | uint8_t *base; | |
1412 | ||
1413 | Index_(internal, number, index, base); | |
1414 | ||
1415 | CYPoolFFI(NULL, context, typical->type_.data.signature.elements[index].type, typical->ffi_.elements[index], base, value); | |
1416 | return true; | |
1417 | } CYCatch | |
1418 | } | |
1419 | ||
b09da87b | 1420 | static JSValueRef CYCallFunction(JSContextRef context, size_t count, const JSValueRef *arguments, JSValueRef *exception, sig::Signature *signature, ffi_cif *cif, void (*function)()) { |
4cf49641 | 1421 | CYTry { |
85a33bf5 | 1422 | if (count != signature->count - 1) |
f5e9be24 | 1423 | @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"incorrect number of arguments to ffi function" userInfo:nil]; |
85a33bf5 | 1424 | |
7ba62cfd JF |
1425 | CYPool pool; |
1426 | void *values[count]; | |
ea2d184c | 1427 | |
7ba62cfd JF |
1428 | for (unsigned index(0); index != count; ++index) { |
1429 | sig::Element *element(&signature->elements[index + 1]); | |
bd17e6f3 | 1430 | ffi_type *ffi(cif->arg_types[index]); |
77e87a6c | 1431 | // XXX: alignment? |
bd17e6f3 JF |
1432 | values[index] = new(pool) uint8_t[ffi->size]; |
1433 | CYPoolFFI(pool, context, element->type, ffi, values[index], arguments[index]); | |
7ba62cfd | 1434 | } |
ea2d184c | 1435 | |
7ba62cfd JF |
1436 | uint8_t value[cif->rtype->size]; |
1437 | ffi_call(cif, function, value, values); | |
1438 | ||
bd17e6f3 | 1439 | return CYFromFFI(context, signature->elements[0].type, cif->rtype, value); |
0c862573 | 1440 | } CYCatch |
7ba62cfd | 1441 | } |
ea2d184c | 1442 | |
478d4ed0 | 1443 | void Closure_(ffi_cif *cif, void *result, void **arguments, void *arg) { |
478d4ed0 JF |
1444 | ffoData *data(reinterpret_cast<ffoData *>(arg)); |
1445 | ||
1446 | JSContextRef context(data->context_); | |
1447 | ||
1448 | size_t count(data->cif_.nargs); | |
1449 | JSValueRef values[count]; | |
1450 | ||
1451 | for (size_t index(0); index != count; ++index) | |
bd17e6f3 | 1452 | values[index] = CYFromFFI(context, data->signature_.elements[1 + index].type, data->cif_.arg_types[index], arguments[index]); |
478d4ed0 JF |
1453 | |
1454 | JSValueRef exception(NULL); | |
1455 | JSValueRef value(JSObjectCallAsFunction(context, data->function_, NULL, count, values, &exception)); | |
1456 | CYThrow(context, exception); | |
1457 | ||
bd17e6f3 | 1458 | CYPoolFFI(NULL, context, data->signature_.elements[0].type, data->cif_.rtype, result, value); |
478d4ed0 JF |
1459 | } |
1460 | ||
1461 | JSObjectRef CYMakeFunctor(JSContextRef context, JSObjectRef function, const char *type) { | |
1462 | // XXX: in case of exceptions this will leak | |
1463 | ffoData *data(new ffoData(type)); | |
1464 | ||
1465 | ffi_closure *closure; | |
1466 | _syscall(closure = (ffi_closure *) mmap( | |
1467 | NULL, sizeof(ffi_closure), | |
1468 | PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, | |
1469 | -1, 0 | |
1470 | )); | |
1471 | ||
1472 | ffi_status status(ffi_prep_closure(closure, &data->cif_, &Closure_, data)); | |
1473 | _assert(status == FFI_OK); | |
1474 | ||
1475 | _syscall(mprotect(closure, sizeof(*closure), PROT_READ | PROT_EXEC)); | |
1476 | ||
1477 | data->value_ = closure; | |
1478 | ||
1479 | data->context_ = CYGetJSContext(); | |
1480 | data->function_ = function; | |
1481 | ||
1482 | return JSObjectMake(context, Functor_, data); | |
1483 | } | |
1484 | ||
953647c1 | 1485 | static JSValueRef Runtime_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { |
4cf49641 | 1486 | CYTry { |
b09da87b JF |
1487 | CYPool pool; |
1488 | NSString *name(CYCastNSString(pool, property)); | |
f610e1a0 | 1489 | if (Class _class = NSClassFromString(name)) |
4cf49641 | 1490 | return CYMakeInstance(context, _class, true); |
953647c1 | 1491 | if (NSMutableArray *entry = [[Bridge_ objectAtIndex:0] objectForKey:name]) |
707bcb93 JF |
1492 | switch ([[entry objectAtIndex:0] intValue]) { |
1493 | case 0: | |
057f943f | 1494 | return JSEvaluateScript(CYGetJSContext(), CYJSString([entry objectAtIndex:1]), NULL, NULL, 0, NULL); |
707bcb93 | 1495 | case 1: |
478d4ed0 | 1496 | return CYMakeFunctor(context, reinterpret_cast<void (*)()>([name cy$symbol]), CYPoolCString(pool, [entry objectAtIndex:1])); |
707bcb93 | 1497 | case 2: |
bd17e6f3 | 1498 | // XXX: this is horrendously inefficient |
707bcb93 | 1499 | sig::Signature signature; |
478d4ed0 | 1500 | sig::Parse(pool, &signature, CYPoolCString(pool, [entry objectAtIndex:1])); |
bd17e6f3 JF |
1501 | ffi_cif cif; |
1502 | sig::sig_ffi_cif(pool, &sig::ObjectiveC, &signature, &cif); | |
1503 | return CYFromFFI(context, signature.elements[0].type, cif.rtype, [name cy$symbol]); | |
707bcb93 JF |
1504 | } |
1505 | return NULL; | |
1506 | } CYCatch | |
1507 | } | |
1508 | ||
04450da0 JF |
1509 | bool stret(ffi_type *ffi_type) { |
1510 | return ffi_type->type == FFI_TYPE_STRUCT && ( | |
1511 | ffi_type->size > OBJC_MAX_STRUCT_BY_VALUE || | |
1512 | struct_forward_array[ffi_type->size] != 0 | |
1513 | ); | |
1514 | } | |
1515 | ||
b09da87b JF |
1516 | extern "C" { |
1517 | int *_NSGetArgc(void); | |
1518 | char ***_NSGetArgv(void); | |
1519 | int UIApplicationMain(int argc, char *argv[], NSString *principalClassName, NSString *delegateClassName); | |
1520 | } | |
1521 | ||
1522 | static JSValueRef System_print(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { | |
4cf49641 | 1523 | CYTry { |
b09da87b JF |
1524 | NSLog(@"%s", CYCastCString(context, arguments[0])); |
1525 | return CYJSUndefined(context); | |
1526 | } CYCatch | |
1527 | } | |
1528 | ||
1529 | static JSValueRef CYApplicationMain(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { | |
4cf49641 | 1530 | CYTry { |
b09da87b JF |
1531 | CYPool pool; |
1532 | NSString *name(CYCastNSObject(pool, context, arguments[0])); | |
478d4ed0 JF |
1533 | int argc(*_NSGetArgc()); |
1534 | char **argv(*_NSGetArgv()); | |
b09da87b JF |
1535 | for (int i(0); i != argc; ++i) |
1536 | NSLog(@"argv[%i]=%s", i, argv[i]); | |
478d4ed0 | 1537 | _pooled |
b09da87b JF |
1538 | return CYCastJSValue(context, UIApplicationMain(argc, argv, name, name)); |
1539 | } CYCatch | |
1540 | } | |
1541 | ||
1542 | static JSValueRef $objc_msgSend(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { | |
7ba62cfd | 1543 | const char *type; |
ea2d184c | 1544 | |
478d4ed0 JF |
1545 | CYPool pool; |
1546 | ||
4cf49641 | 1547 | CYTry { |
85a33bf5 | 1548 | if (count < 2) |
f5e9be24 | 1549 | @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"too few arguments to objc_msgSend" userInfo:nil]; |
85a33bf5 | 1550 | |
b09da87b | 1551 | id self(CYCastNSObject(pool, context, arguments[0])); |
7ba62cfd | 1552 | if (self == nil) |
b09da87b | 1553 | return CYJSNull(context); |
7ba62cfd | 1554 | |
85a33bf5 | 1555 | SEL _cmd(CYCastSEL(context, arguments[1])); |
7ba62cfd | 1556 | |
478d4ed0 JF |
1557 | Class _class(object_getClass(self)); |
1558 | if (Method method = class_getInstanceMethod(_class, _cmd)) | |
1559 | type = method_getTypeEncoding(method); | |
4cf49641 JF |
1560 | else { |
1561 | CYPoolTry { | |
1562 | NSMethodSignature *method([self methodSignatureForSelector:_cmd]); | |
1563 | if (method == nil) | |
1564 | @throw [NSException exceptionWithName:NSInvalidArgumentException reason:[NSString stringWithFormat:@"unrecognized selector %s sent to object %p", sel_getName(_cmd), self] userInfo:nil]; | |
1565 | type = CYPoolCString(pool, [method _typeString]); | |
1566 | } CYPoolCatch(NULL) | |
478d4ed0 | 1567 | } |
0c862573 | 1568 | } CYCatch |
ea2d184c | 1569 | |
7ba62cfd JF |
1570 | sig::Signature signature; |
1571 | sig::Parse(pool, &signature, type); | |
ea2d184c | 1572 | |
7ba62cfd | 1573 | ffi_cif cif; |
b21525c7 | 1574 | sig::sig_ffi_cif(pool, &sig::ObjectiveC, &signature, &cif); |
7ba62cfd | 1575 | |
04450da0 | 1576 | void (*function)() = stret(cif.rtype) ? reinterpret_cast<void (*)()>(&objc_msgSend_stret) : reinterpret_cast<void (*)()>(&objc_msgSend); |
7ba62cfd JF |
1577 | return CYCallFunction(context, count, arguments, exception, &signature, &cif, function); |
1578 | } | |
1579 | ||
dea834b0 JF |
1580 | static JSValueRef Selector_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { |
1581 | JSValueRef setup[count + 2]; | |
1582 | setup[0] = _this; | |
1583 | setup[1] = object; | |
1584 | memmove(setup + 2, arguments, sizeof(JSValueRef) * count); | |
1585 | return $objc_msgSend(context, NULL, NULL, count + 2, setup, exception); | |
1586 | } | |
1587 | ||
1588 | static JSValueRef Functor_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { | |
953647c1 | 1589 | Functor_privateData *data(reinterpret_cast<Functor_privateData *>(JSObjectGetPrivate(object))); |
77e87a6c | 1590 | return CYCallFunction(context, count, arguments, exception, &data->signature_, &data->cif_, reinterpret_cast<void (*)()>(data->value_)); |
ea2d184c JF |
1591 | } |
1592 | ||
b09da87b | 1593 | JSObjectRef Selector_new(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { |
4cf49641 | 1594 | CYTry { |
dea834b0 JF |
1595 | if (count != 1) |
1596 | @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"incorrect number of arguments to Selector constructor" userInfo:nil]; | |
1597 | const char *name(CYCastCString(context, arguments[0])); | |
1598 | return CYMakeSelector(context, sel_registerName(name)); | |
1599 | } CYCatch | |
1600 | } | |
1601 | ||
b09da87b | 1602 | JSObjectRef Functor_new(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { |
4cf49641 | 1603 | CYTry { |
b21525c7 | 1604 | if (count != 2) |
dea834b0 | 1605 | @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"incorrect number of arguments to Functor constructor" userInfo:nil]; |
b21525c7 | 1606 | const char *type(CYCastCString(context, arguments[1])); |
b09da87b JF |
1607 | JSValueRef exception(NULL); |
1608 | if (JSValueIsInstanceOfConstructor(context, arguments[0], Function_, &exception)) { | |
1609 | JSObjectRef function(CYCastJSObject(context, arguments[0])); | |
1610 | return CYMakeFunctor(context, function, type); | |
1611 | } else if (exception != NULL) { | |
1612 | return NULL; | |
1613 | } else { | |
1614 | void (*function)()(CYCastPointer<void (*)()>(context, arguments[0])); | |
1615 | return CYMakeFunctor(context, function, type); | |
1616 | } | |
0c862573 | 1617 | } CYCatch |
ea2d184c JF |
1618 | } |
1619 | ||
f610e1a0 | 1620 | JSValueRef Pointer_getProperty_value(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { |
953647c1 | 1621 | Pointer_privateData *data(reinterpret_cast<Pointer_privateData *>(JSObjectGetPrivate(object))); |
b09da87b | 1622 | return CYCastJSValue(context, reinterpret_cast<uintptr_t>(data->value_)); |
04450da0 JF |
1623 | } |
1624 | ||
dea834b0 JF |
1625 | JSValueRef Selector_getProperty_prototype(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { |
1626 | return Function_; | |
1627 | } | |
1628 | ||
953647c1 JF |
1629 | static JSValueRef Pointer_callAsFunction_valueOf(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { |
1630 | CYTry { | |
1631 | Pointer_privateData *data(reinterpret_cast<Pointer_privateData *>(JSObjectGetPrivate(_this))); | |
1632 | return CYCastJSValue(context, reinterpret_cast<uintptr_t>(data->value_)); | |
1633 | } CYCatch | |
1634 | } | |
1635 | ||
4cf49641 JF |
1636 | static JSValueRef Instance_callAsFunction_toString(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { |
1637 | CYTry { | |
953647c1 | 1638 | Instance_privateData *data(reinterpret_cast<Instance_privateData *>(JSObjectGetPrivate(_this))); |
4e8c99fb JF |
1639 | CYPoolTry { |
1640 | return CYCastJSValue(context, CYJSString([data->GetValue() description])); | |
4cf49641 | 1641 | } CYPoolCatch(NULL) |
478d4ed0 JF |
1642 | } CYCatch |
1643 | } | |
1644 | ||
1645 | static JSValueRef Selector_callAsFunction_toString(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { | |
4cf49641 | 1646 | CYTry { |
953647c1 | 1647 | Selector_privateData *data(reinterpret_cast<Selector_privateData *>(JSObjectGetPrivate(_this))); |
478d4ed0 JF |
1648 | return CYCastJSValue(context, sel_getName(data->GetValue())); |
1649 | } CYCatch | |
1650 | } | |
1651 | ||
b09da87b | 1652 | static JSValueRef Selector_callAsFunction_type(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { |
4cf49641 | 1653 | CYTry { |
b09da87b JF |
1654 | if (count != 2) |
1655 | @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"incorrect number of arguments to Selector.type" userInfo:nil]; | |
1656 | CYPool pool; | |
953647c1 | 1657 | Selector_privateData *data(reinterpret_cast<Selector_privateData *>(JSObjectGetPrivate(_this))); |
b09da87b JF |
1658 | Class _class(CYCastNSObject(pool, context, arguments[0])); |
1659 | bool instance(CYCastBool(context, arguments[1])); | |
1660 | SEL sel(data->GetValue()); | |
1661 | if (Method method = (*(instance ? &class_getInstanceMethod : class_getClassMethod))(_class, sel)) | |
1662 | return CYCastJSValue(context, method_getTypeEncoding(method)); | |
953647c1 | 1663 | else if (NSString *type = [[Bridge_ objectAtIndex:1] objectForKey:CYCastNSString(pool, sel_getName(sel))]) |
b09da87b JF |
1664 | return CYCastJSValue(context, CYJSString(type)); |
1665 | else | |
1666 | return CYJSNull(context); | |
1667 | } CYCatch | |
1668 | } | |
1669 | ||
88c977fa JF |
1670 | static JSStaticValue Pointer_staticValues[2] = { |
1671 | {"value", &Pointer_getProperty_value, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete}, | |
04450da0 JF |
1672 | {NULL, NULL, NULL, 0} |
1673 | }; | |
1674 | ||
953647c1 JF |
1675 | static JSStaticFunction Pointer_staticFunctions[2] = { |
1676 | {"valueOf", &Pointer_callAsFunction_valueOf, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
1677 | {NULL, NULL, 0} | |
1678 | }; | |
1679 | ||
dea834b0 JF |
1680 | /*static JSStaticValue Selector_staticValues[2] = { |
1681 | {"prototype", &Selector_getProperty_prototype, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete}, | |
1682 | {NULL, NULL, NULL, 0} | |
1683 | };*/ | |
1684 | ||
478d4ed0 JF |
1685 | static JSStaticFunction Instance_staticFunctions[2] = { |
1686 | {"toString", &Instance_callAsFunction_toString, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
1687 | {NULL, NULL, 0} | |
1688 | }; | |
1689 | ||
1690 | static JSStaticFunction Selector_staticFunctions[3] = { | |
1691 | {"toString", &Selector_callAsFunction_toString, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
b09da87b JF |
1692 | {"type", &Selector_callAsFunction_type, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, |
1693 | {NULL, NULL, 0} | |
1694 | }; | |
1695 | ||
5999c315 | 1696 | CYDriver::CYDriver(const std::string &filename) : |
db5e2840 | 1697 | state_(CYClear), |
e7ed5354 JF |
1698 | data_(NULL), |
1699 | size_(0), | |
5999c315 JF |
1700 | filename_(filename), |
1701 | source_(NULL) | |
1702 | { | |
924f67b2 JF |
1703 | ScannerInit(); |
1704 | } | |
1705 | ||
5999c315 | 1706 | CYDriver::~CYDriver() { |
924f67b2 JF |
1707 | ScannerDestroy(); |
1708 | } | |
1709 | ||
5befe15e JF |
1710 | void cy::parser::error(const cy::parser::location_type &location, const std::string &message) { |
1711 | CYDriver::Error error; | |
1712 | error.location_ = location; | |
1713 | error.message_ = message; | |
1714 | driver.errors_.push_back(error); | |
63b4c5a8 JF |
1715 | } |
1716 | ||
b09da87b JF |
1717 | void CYSetArgs(int argc, const char *argv[]) { |
1718 | JSContextRef context(CYGetJSContext()); | |
1719 | JSValueRef args[argc]; | |
1720 | for (int i(0); i != argc; ++i) | |
1721 | args[i] = CYCastJSValue(context, argv[i]); | |
1722 | JSValueRef exception(NULL); | |
1723 | JSObjectRef array(JSObjectMakeArray(context, argc, args, &exception)); | |
1724 | CYThrow(context, exception); | |
1725 | CYSetProperty(context, System_, CYJSString("args"), array); | |
1726 | } | |
1727 | ||
579ed526 JF |
1728 | JSObjectRef CYGetGlobalObject(JSContextRef context) { |
1729 | return JSContextGetGlobalObject(context); | |
1730 | } | |
1731 | ||
7ba62cfd | 1732 | MSInitialize { _pooled |
ea2d184c JF |
1733 | apr_initialize(); |
1734 | ||
953647c1 JF |
1735 | Bridge_ = [[NSMutableArray arrayWithContentsOfFile:@"/usr/lib/libcycript.plist"] retain]; |
1736 | ||
c1582939 JF |
1737 | NSCFBoolean_ = objc_getClass("NSCFBoolean"); |
1738 | ||
1739 | pid_t pid(getpid()); | |
1740 | ||
1741 | struct sockaddr_in address; | |
1742 | address.sin_len = sizeof(address); | |
1743 | address.sin_family = AF_INET; | |
1744 | address.sin_addr.s_addr = INADDR_ANY; | |
1745 | address.sin_port = htons(10000 + pid); | |
1746 | ||
1747 | CFDataRef data(CFDataCreate(kCFAllocatorDefault, reinterpret_cast<UInt8 *>(&address), sizeof(address))); | |
1748 | ||
1749 | CFSocketSignature signature; | |
1750 | signature.protocolFamily = AF_INET; | |
1751 | signature.socketType = SOCK_STREAM; | |
1752 | signature.protocol = IPPROTO_TCP; | |
1753 | signature.address = data; | |
1754 | ||
1755 | CFSocketRef socket(CFSocketCreateWithSocketSignature(kCFAllocatorDefault, &signature, kCFSocketAcceptCallBack, &OnAccept, NULL)); | |
1756 | CFRunLoopAddSource(CFRunLoopGetCurrent(), CFSocketCreateRunLoopSource(kCFAllocatorDefault, socket, 0), kCFRunLoopDefaultMode); | |
1757 | ||
1758 | JSClassDefinition definition; | |
1759 | ||
1760 | definition = kJSClassDefinitionEmpty; | |
88c977fa JF |
1761 | definition.className = "Pointer"; |
1762 | definition.staticValues = Pointer_staticValues; | |
953647c1 JF |
1763 | definition.staticFunctions = Pointer_staticFunctions; |
1764 | definition.finalize = &CYData::Finalize; | |
88c977fa | 1765 | Pointer_ = JSClassCreate(&definition); |
c1582939 JF |
1766 | |
1767 | definition = kJSClassDefinitionEmpty; | |
88c977fa | 1768 | definition.className = "Functor"; |
953647c1 JF |
1769 | definition.staticValues = Pointer_staticValues; |
1770 | definition.staticFunctions = Pointer_staticFunctions; | |
dea834b0 | 1771 | definition.callAsFunction = &Functor_callAsFunction; |
953647c1 | 1772 | definition.finalize = &CYData::Finalize; |
88c977fa | 1773 | Functor_ = JSClassCreate(&definition); |
7ba62cfd | 1774 | |
953647c1 JF |
1775 | definition = kJSClassDefinitionEmpty; |
1776 | definition.className = "Struct"; | |
bd17e6f3 JF |
1777 | definition.getProperty = &Struct_getProperty; |
1778 | definition.setProperty = &Struct_setProperty; | |
953647c1 JF |
1779 | definition.finalize = &CYData::Finalize; |
1780 | Struct_ = JSClassCreate(&definition); | |
1781 | ||
77e87a6c | 1782 | definition = kJSClassDefinitionEmpty; |
88c977fa | 1783 | definition.className = "Selector"; |
953647c1 | 1784 | definition.staticValues = Pointer_staticValues; |
dea834b0 | 1785 | //definition.staticValues = Selector_staticValues; |
b09da87b | 1786 | definition.staticFunctions = Selector_staticFunctions; |
dea834b0 | 1787 | definition.callAsFunction = &Selector_callAsFunction; |
953647c1 | 1788 | definition.finalize = &CYData::Finalize; |
88c977fa | 1789 | Selector_ = JSClassCreate(&definition); |
77e87a6c | 1790 | |
7ba62cfd | 1791 | definition = kJSClassDefinitionEmpty; |
b09da87b | 1792 | definition.className = "Instance"; |
953647c1 | 1793 | definition.staticValues = Pointer_staticValues; |
478d4ed0 | 1794 | definition.staticFunctions = Instance_staticFunctions; |
88c977fa | 1795 | definition.getProperty = &Instance_getProperty; |
b09da87b JF |
1796 | definition.setProperty = &Instance_setProperty; |
1797 | definition.deleteProperty = &Instance_deleteProperty; | |
88c977fa | 1798 | definition.callAsConstructor = &Instance_callAsConstructor; |
953647c1 | 1799 | definition.finalize = &CYData::Finalize; |
88c977fa | 1800 | Instance_ = JSClassCreate(&definition); |
7ba62cfd JF |
1801 | |
1802 | definition = kJSClassDefinitionEmpty; | |
953647c1 JF |
1803 | definition.className = "Runtime"; |
1804 | definition.getProperty = &Runtime_getProperty; | |
1805 | Runtime_ = JSClassCreate(&definition); | |
1806 | ||
1807 | definition = kJSClassDefinitionEmpty; | |
1808 | //definition.getProperty = &Global_getProperty; | |
88c977fa | 1809 | JSClassRef Global(JSClassCreate(&definition)); |
c1582939 | 1810 | |
478d4ed0 | 1811 | JSGlobalContextRef context(JSGlobalContextCreate(Global)); |
ea2d184c JF |
1812 | Context_ = context; |
1813 | ||
579ed526 | 1814 | JSObjectRef global(CYGetGlobalObject(context)); |
c1582939 | 1815 | |
953647c1 | 1816 | JSObjectSetPrototype(context, global, JSObjectMake(context, Runtime_, NULL)); |
579ed526 | 1817 | CYSetProperty(context, global, CYJSString("ObjectiveC"), JSObjectMake(context, Runtime_, NULL)); |
953647c1 | 1818 | |
b09da87b JF |
1819 | CYSetProperty(context, global, CYJSString("Selector"), JSObjectMakeConstructor(context, Selector_, &Selector_new)); |
1820 | CYSetProperty(context, global, CYJSString("Functor"), JSObjectMakeConstructor(context, Functor_, &Functor_new)); | |
7ba62cfd | 1821 | |
b09da87b | 1822 | CYSetProperty(context, global, CYJSString("CYApplicationMain"), JSObjectMakeFunctionWithCallback(context, CYJSString("CYApplicationMain"), &CYApplicationMain)); |
dea834b0 | 1823 | CYSetProperty(context, global, CYJSString("objc_msgSend"), JSObjectMakeFunctionWithCallback(context, CYJSString("objc_msgSend"), &$objc_msgSend)); |
7ba62cfd | 1824 | |
b09da87b JF |
1825 | System_ = JSObjectMake(context, NULL, NULL); |
1826 | CYSetProperty(context, global, CYJSString("system"), System_); | |
1827 | CYSetProperty(context, System_, CYJSString("args"), CYJSNull(context)); | |
579ed526 | 1828 | //CYSetProperty(context, System_, CYJSString("global"), global); |
b09da87b JF |
1829 | |
1830 | CYSetProperty(context, System_, CYJSString("print"), JSObjectMakeFunctionWithCallback(context, CYJSString("print"), &System_print)); | |
1831 | ||
62ca2b82 JF |
1832 | name_ = JSStringCreateWithUTF8CString("name"); |
1833 | message_ = JSStringCreateWithUTF8CString("message"); | |
c1582939 JF |
1834 | length_ = JSStringCreateWithUTF8CString("length"); |
1835 | ||
dea834b0 JF |
1836 | Array_ = CYCastJSObject(context, CYGetProperty(context, global, CYJSString("Array"))); |
1837 | Function_ = CYCastJSObject(context, CYGetProperty(context, global, CYJSString("Function"))); | |
c1582939 | 1838 | } |