]>
Commit | Line | Data |
---|---|---|
1 | /* Cycript - Remove Execution Server and Disassembler | |
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 | #define _GNU_SOURCE | |
41 | ||
42 | #include <substrate.h> | |
43 | #include "cycript.hpp" | |
44 | ||
45 | #include "sig/parse.hpp" | |
46 | #include "sig/ffi_type.hpp" | |
47 | ||
48 | #include "Pooling.hpp" | |
49 | #include "Struct.hpp" | |
50 | ||
51 | #include <unistd.h> | |
52 | ||
53 | #include <CoreFoundation/CoreFoundation.h> | |
54 | #include <CoreFoundation/CFLogUtilities.h> | |
55 | ||
56 | #include <WebKit/WebScriptObject.h> | |
57 | ||
58 | #include <sys/types.h> | |
59 | #include <sys/socket.h> | |
60 | #include <netinet/in.h> | |
61 | #include <sys/un.h> | |
62 | ||
63 | #include <sys/mman.h> | |
64 | ||
65 | #include <iostream> | |
66 | #include <ext/stdio_filebuf.h> | |
67 | #include <set> | |
68 | #include <map> | |
69 | ||
70 | #include <sstream> | |
71 | #include <cmath> | |
72 | ||
73 | #include "Parser.hpp" | |
74 | #include "Cycript.tab.hh" | |
75 | ||
76 | #include <fcntl.h> | |
77 | ||
78 | #include <apr-1/apr_thread_proc.h> | |
79 | ||
80 | #undef _assert | |
81 | #undef _trace | |
82 | ||
83 | #define _assert(test) do { \ | |
84 | if (!(test)) \ | |
85 | @throw [NSException exceptionWithName:NSInternalInconsistencyException reason:[NSString stringWithFormat:@"_assert(%s):%s(%u):%s", #test, __FILE__, __LINE__, __FUNCTION__] userInfo:nil]; \ | |
86 | } while (false) | |
87 | ||
88 | #define _trace() do { \ | |
89 | CFLog(kCFLogLevelNotice, CFSTR("_trace():%u"), __LINE__); \ | |
90 | } while (false) | |
91 | ||
92 | #define CYPoolTry { \ | |
93 | id _saved(nil); \ | |
94 | NSAutoreleasePool *_pool([[NSAutoreleasePool alloc] init]); \ | |
95 | @try | |
96 | #define CYPoolCatch(value) \ | |
97 | @catch (NSException *error) { \ | |
98 | _saved = [error retain]; \ | |
99 | @throw; \ | |
100 | return value; \ | |
101 | } @finally { \ | |
102 | [_pool release]; \ | |
103 | if (_saved != nil) \ | |
104 | [_saved autorelease]; \ | |
105 | } \ | |
106 | } | |
107 | ||
108 | static JSGlobalContextRef Context_; | |
109 | static JSObjectRef System_; | |
110 | static JSObjectRef ObjectiveC_; | |
111 | ||
112 | static JSClassRef Functor_; | |
113 | static JSClassRef Instance_; | |
114 | static JSClassRef Internal_; | |
115 | static JSClassRef Pointer_; | |
116 | static JSClassRef Runtime_; | |
117 | static JSClassRef Selector_; | |
118 | static JSClassRef Struct_; | |
119 | static JSClassRef Type_; | |
120 | ||
121 | static JSClassRef ObjectiveC_Classes_; | |
122 | static JSClassRef ObjectiveC_Image_Classes_; | |
123 | static JSClassRef ObjectiveC_Images_; | |
124 | static JSClassRef ObjectiveC_Protocols_; | |
125 | ||
126 | static JSObjectRef Array_; | |
127 | static JSObjectRef Function_; | |
128 | ||
129 | static JSStringRef Result_; | |
130 | ||
131 | static JSStringRef length_; | |
132 | static JSStringRef message_; | |
133 | static JSStringRef name_; | |
134 | static JSStringRef toCYON_; | |
135 | static JSStringRef toJSON_; | |
136 | ||
137 | static Class NSCFBoolean_; | |
138 | static Class NSCFType_; | |
139 | static Class NSMessageBuilder_; | |
140 | static Class NSZombie_; | |
141 | static Class Object_; | |
142 | ||
143 | static NSArray *Bridge_; | |
144 | ||
145 | struct CYData { | |
146 | apr_pool_t *pool_; | |
147 | ||
148 | virtual ~CYData() { | |
149 | } | |
150 | ||
151 | static void *operator new(size_t size, apr_pool_t *pool) { | |
152 | void *data(apr_palloc(pool, size)); | |
153 | reinterpret_cast<CYData *>(data)->pool_ = pool; | |
154 | return data; | |
155 | } | |
156 | ||
157 | static void *operator new(size_t size) { | |
158 | apr_pool_t *pool; | |
159 | apr_pool_create(&pool, NULL); | |
160 | return operator new(size, pool); | |
161 | } | |
162 | ||
163 | static void operator delete(void *data) { | |
164 | apr_pool_destroy(reinterpret_cast<CYData *>(data)->pool_); | |
165 | } | |
166 | ||
167 | static void Finalize(JSObjectRef object) { | |
168 | delete reinterpret_cast<CYData *>(JSObjectGetPrivate(object)); | |
169 | } | |
170 | }; | |
171 | ||
172 | class Type_privateData; | |
173 | ||
174 | struct CYValue : | |
175 | CYData | |
176 | { | |
177 | void *value_; | |
178 | ||
179 | CYValue() { | |
180 | } | |
181 | ||
182 | CYValue(void *value) : | |
183 | value_(value) | |
184 | { | |
185 | } | |
186 | ||
187 | CYValue(const CYValue &rhs) : | |
188 | value_(rhs.value_) | |
189 | { | |
190 | } | |
191 | ||
192 | virtual Type_privateData *GetType() const { | |
193 | return NULL; | |
194 | } | |
195 | }; | |
196 | ||
197 | struct Selector_privateData : | |
198 | CYValue | |
199 | { | |
200 | Selector_privateData(SEL value) : | |
201 | CYValue(value) | |
202 | { | |
203 | } | |
204 | ||
205 | SEL GetValue() const { | |
206 | return reinterpret_cast<SEL>(value_); | |
207 | } | |
208 | ||
209 | virtual Type_privateData *GetType() const; | |
210 | }; | |
211 | ||
212 | struct Instance : | |
213 | CYValue | |
214 | { | |
215 | enum Flags { | |
216 | None = 0, | |
217 | Transient = (1 << 0), | |
218 | Uninitialized = (1 << 1), | |
219 | }; | |
220 | ||
221 | Flags flags_; | |
222 | ||
223 | Instance(id value, Flags flags) : | |
224 | CYValue(value), | |
225 | flags_(flags) | |
226 | { | |
227 | } | |
228 | ||
229 | virtual ~Instance() { | |
230 | if ((flags_ & Transient) == 0) | |
231 | // XXX: does this handle background threads correctly? | |
232 | [GetValue() performSelector:@selector(release) withObject:nil afterDelay:0]; | |
233 | } | |
234 | ||
235 | static JSObjectRef Make(JSContextRef context, id object, Flags flags) { | |
236 | return JSObjectMake(context, Instance_, new Instance(object, flags)); | |
237 | } | |
238 | ||
239 | id GetValue() const { | |
240 | return reinterpret_cast<id>(value_); | |
241 | } | |
242 | ||
243 | bool IsUninitialized() const { | |
244 | return (flags_ & Uninitialized) != 0; | |
245 | } | |
246 | ||
247 | virtual Type_privateData *GetType() const; | |
248 | }; | |
249 | ||
250 | struct Internal : | |
251 | CYValue | |
252 | { | |
253 | JSObjectRef owner_; | |
254 | ||
255 | Internal(id value, JSObjectRef owner) : | |
256 | CYValue(value), | |
257 | owner_(owner) | |
258 | { | |
259 | } | |
260 | ||
261 | static JSObjectRef Make(JSContextRef context, id object, JSObjectRef owner) { | |
262 | return JSObjectMake(context, Internal_, new Internal(object, owner)); | |
263 | } | |
264 | ||
265 | id GetValue() const { | |
266 | return reinterpret_cast<id>(value_); | |
267 | } | |
268 | }; | |
269 | ||
270 | namespace sig { | |
271 | ||
272 | void Copy(apr_pool_t *pool, Type &lhs, Type &rhs); | |
273 | ||
274 | void Copy(apr_pool_t *pool, Element &lhs, Element &rhs) { | |
275 | lhs.name = apr_pstrdup(pool, rhs.name); | |
276 | if (rhs.type == NULL) | |
277 | lhs.type = NULL; | |
278 | else { | |
279 | lhs.type = new(pool) Type; | |
280 | Copy(pool, *lhs.type, *rhs.type); | |
281 | } | |
282 | lhs.offset = rhs.offset; | |
283 | } | |
284 | ||
285 | void Copy(apr_pool_t *pool, Signature &lhs, Signature &rhs) { | |
286 | size_t count(rhs.count); | |
287 | lhs.count = count; | |
288 | lhs.elements = new(pool) Element[count]; | |
289 | for (size_t index(0); index != count; ++index) | |
290 | Copy(pool, lhs.elements[index], rhs.elements[index]); | |
291 | } | |
292 | ||
293 | void Copy(apr_pool_t *pool, Type &lhs, Type &rhs) { | |
294 | lhs.primitive = rhs.primitive; | |
295 | lhs.name = apr_pstrdup(pool, rhs.name); | |
296 | lhs.flags = rhs.flags; | |
297 | ||
298 | if (sig::IsAggregate(rhs.primitive)) | |
299 | Copy(pool, lhs.data.signature, rhs.data.signature); | |
300 | else { | |
301 | if (rhs.data.data.type != NULL) { | |
302 | lhs.data.data.type = new(pool) Type; | |
303 | Copy(pool, *lhs.data.data.type, *rhs.data.data.type); | |
304 | } | |
305 | ||
306 | lhs.data.data.size = rhs.data.data.size; | |
307 | } | |
308 | } | |
309 | ||
310 | void Copy(apr_pool_t *pool, ffi_type &lhs, ffi_type &rhs) { | |
311 | lhs.size = rhs.size; | |
312 | lhs.alignment = rhs.alignment; | |
313 | lhs.type = rhs.type; | |
314 | if (rhs.elements == NULL) | |
315 | lhs.elements = NULL; | |
316 | else { | |
317 | size_t count(0); | |
318 | while (rhs.elements[count] != NULL) | |
319 | ++count; | |
320 | ||
321 | lhs.elements = new(pool) ffi_type *[count + 1]; | |
322 | lhs.elements[count] = NULL; | |
323 | ||
324 | for (size_t index(0); index != count; ++index) { | |
325 | // XXX: if these are libffi native then you can just take them | |
326 | ffi_type *ffi(new(pool) ffi_type); | |
327 | lhs.elements[index] = ffi; | |
328 | sig::Copy(pool, *ffi, *rhs.elements[index]); | |
329 | } | |
330 | } | |
331 | } | |
332 | ||
333 | } | |
334 | ||
335 | struct CStringMapLess : | |
336 | std::binary_function<const char *, const char *, bool> | |
337 | { | |
338 | _finline bool operator ()(const char *lhs, const char *rhs) const { | |
339 | return strcmp(lhs, rhs) < 0; | |
340 | } | |
341 | }; | |
342 | ||
343 | void Structor_(apr_pool_t *pool, const char *name, const char *types, sig::Type *&type) { | |
344 | if (name == NULL) | |
345 | return; | |
346 | ||
347 | CYPoolTry { | |
348 | if (NSMutableArray *entry = [[Bridge_ objectAtIndex:2] objectForKey:[NSString stringWithUTF8String:name]]) | |
349 | switch ([[entry objectAtIndex:0] intValue]) { | |
350 | case 0: { | |
351 | sig::Parse(pool, &type->data.signature, [[entry objectAtIndex:1] UTF8String], &Structor_); | |
352 | } break; | |
353 | ||
354 | case 1: { | |
355 | sig::Signature signature; | |
356 | sig::Parse(pool, &signature, [[entry objectAtIndex:1] UTF8String], &Structor_); | |
357 | type = signature.elements[0].type; | |
358 | } break; | |
359 | } | |
360 | } CYPoolCatch() | |
361 | } | |
362 | ||
363 | struct Type_privateData : | |
364 | CYData | |
365 | { | |
366 | static Type_privateData *Object; | |
367 | static Type_privateData *Selector; | |
368 | ||
369 | ffi_type *ffi_; | |
370 | sig::Type *type_; | |
371 | ||
372 | void Set(sig::Type *type) { | |
373 | type_ = new(pool_) sig::Type; | |
374 | sig::Copy(pool_, *type_, *type); | |
375 | } | |
376 | ||
377 | Type_privateData(apr_pool_t *pool, const char *type) : | |
378 | ffi_(NULL) | |
379 | { | |
380 | if (pool != NULL) | |
381 | pool_ = pool; | |
382 | ||
383 | sig::Signature signature; | |
384 | sig::Parse(pool_, &signature, type, &Structor_); | |
385 | type_ = signature.elements[0].type; | |
386 | } | |
387 | ||
388 | Type_privateData(sig::Type *type) : | |
389 | ffi_(NULL) | |
390 | { | |
391 | if (type != NULL) | |
392 | Set(type); | |
393 | } | |
394 | ||
395 | Type_privateData(sig::Type *type, ffi_type *ffi) { | |
396 | ffi_ = new(pool_) ffi_type; | |
397 | sig::Copy(pool_, *ffi_, *ffi); | |
398 | Set(type); | |
399 | } | |
400 | ||
401 | ffi_type *GetFFI() { | |
402 | if (ffi_ == NULL) { | |
403 | ffi_ = new(pool_) ffi_type; | |
404 | ||
405 | sig::Element element; | |
406 | element.name = NULL; | |
407 | element.type = type_; | |
408 | element.offset = 0; | |
409 | ||
410 | sig::Signature signature; | |
411 | signature.elements = &element; | |
412 | signature.count = 1; | |
413 | ||
414 | ffi_cif cif; | |
415 | sig::sig_ffi_cif(pool_, &sig::ObjectiveC, &signature, &cif); | |
416 | *ffi_ = *cif.rtype; | |
417 | } | |
418 | ||
419 | return ffi_; | |
420 | } | |
421 | }; | |
422 | ||
423 | Type_privateData *Type_privateData::Object; | |
424 | Type_privateData *Type_privateData::Selector; | |
425 | ||
426 | Type_privateData *Instance::GetType() const { | |
427 | return Type_privateData::Object; | |
428 | } | |
429 | ||
430 | Type_privateData *Selector_privateData::GetType() const { | |
431 | return Type_privateData::Selector; | |
432 | } | |
433 | ||
434 | struct Pointer : | |
435 | CYValue | |
436 | { | |
437 | JSObjectRef owner_; | |
438 | Type_privateData *type_; | |
439 | ||
440 | Pointer(void *value, sig::Type *type, JSObjectRef owner) : | |
441 | CYValue(value), | |
442 | owner_(owner), | |
443 | type_(new(pool_) Type_privateData(type)) | |
444 | { | |
445 | } | |
446 | }; | |
447 | ||
448 | struct Struct_privateData : | |
449 | CYValue | |
450 | { | |
451 | JSObjectRef owner_; | |
452 | Type_privateData *type_; | |
453 | ||
454 | Struct_privateData(JSObjectRef owner) : | |
455 | owner_(owner) | |
456 | { | |
457 | } | |
458 | }; | |
459 | ||
460 | typedef std::map<const char *, Type_privateData *, CStringMapLess> TypeMap; | |
461 | static TypeMap Types_; | |
462 | ||
463 | JSObjectRef CYMakeStruct(JSContextRef context, void *data, sig::Type *type, ffi_type *ffi, JSObjectRef owner) { | |
464 | Struct_privateData *internal(new Struct_privateData(owner)); | |
465 | apr_pool_t *pool(internal->pool_); | |
466 | Type_privateData *typical(new(pool) Type_privateData(type, ffi)); | |
467 | internal->type_ = typical; | |
468 | ||
469 | if (owner != NULL) | |
470 | internal->value_ = data; | |
471 | else { | |
472 | size_t size(typical->GetFFI()->size); | |
473 | void *copy(apr_palloc(internal->pool_, size)); | |
474 | memcpy(copy, data, size); | |
475 | internal->value_ = copy; | |
476 | } | |
477 | ||
478 | return JSObjectMake(context, Struct_, internal); | |
479 | } | |
480 | ||
481 | struct Functor_privateData : | |
482 | CYValue | |
483 | { | |
484 | sig::Signature signature_; | |
485 | ffi_cif cif_; | |
486 | ||
487 | Functor_privateData(const char *type, void (*value)()) : | |
488 | CYValue(reinterpret_cast<void *>(value)) | |
489 | { | |
490 | sig::Parse(pool_, &signature_, type, &Structor_); | |
491 | sig::sig_ffi_cif(pool_, &sig::ObjectiveC, &signature_, &cif_); | |
492 | } | |
493 | }; | |
494 | ||
495 | struct ffoData : | |
496 | Functor_privateData | |
497 | { | |
498 | JSContextRef context_; | |
499 | JSObjectRef function_; | |
500 | ||
501 | ffoData(const char *type) : | |
502 | Functor_privateData(type, NULL) | |
503 | { | |
504 | } | |
505 | }; | |
506 | ||
507 | JSObjectRef CYMakeInstance(JSContextRef context, id object, bool transient) { | |
508 | Instance::Flags flags; | |
509 | ||
510 | if (transient) | |
511 | flags = Instance::Transient; | |
512 | else { | |
513 | flags = Instance::None; | |
514 | object = [object retain]; | |
515 | } | |
516 | ||
517 | return Instance::Make(context, object, flags); | |
518 | } | |
519 | ||
520 | const char *CYPoolCString(apr_pool_t *pool, NSString *value) { | |
521 | if (pool == NULL) | |
522 | return [value UTF8String]; | |
523 | else { | |
524 | size_t size([value maximumLengthOfBytesUsingEncoding:NSUTF8StringEncoding] + 1); | |
525 | char *string(new(pool) char[size]); | |
526 | if (![value getCString:string maxLength:size encoding:NSUTF8StringEncoding]) | |
527 | @throw [NSException exceptionWithName:NSInternalInconsistencyException reason:@"[NSString getCString:maxLength:encoding:] == NO" userInfo:nil]; | |
528 | return string; | |
529 | } | |
530 | } | |
531 | ||
532 | JSValueRef CYCastJSValue(JSContextRef context, bool value) { | |
533 | return JSValueMakeBoolean(context, value); | |
534 | } | |
535 | ||
536 | JSValueRef CYCastJSValue(JSContextRef context, double value) { | |
537 | return JSValueMakeNumber(context, value); | |
538 | } | |
539 | ||
540 | #define CYCastJSValue_(Type_) \ | |
541 | JSValueRef CYCastJSValue(JSContextRef context, Type_ value) { \ | |
542 | return JSValueMakeNumber(context, static_cast<double>(value)); \ | |
543 | } | |
544 | ||
545 | CYCastJSValue_(int) | |
546 | CYCastJSValue_(unsigned int) | |
547 | CYCastJSValue_(long int) | |
548 | CYCastJSValue_(long unsigned int) | |
549 | CYCastJSValue_(long long int) | |
550 | CYCastJSValue_(long long unsigned int) | |
551 | ||
552 | JSValueRef CYJSUndefined(JSContextRef context) { | |
553 | return JSValueMakeUndefined(context); | |
554 | } | |
555 | ||
556 | bool CYGetIndex(const char *value, ssize_t &index) { | |
557 | if (value[0] != '0') { | |
558 | char *end; | |
559 | index = strtol(value, &end, 10); | |
560 | if (value + strlen(value) == end) | |
561 | return true; | |
562 | } else if (value[1] == '\0') { | |
563 | index = 0; | |
564 | return true; | |
565 | } | |
566 | ||
567 | return false; | |
568 | } | |
569 | ||
570 | bool CYGetIndex(apr_pool_t *pool, NSString *value, ssize_t &index) { | |
571 | return CYGetIndex(CYPoolCString(pool, value), index); | |
572 | } | |
573 | ||
574 | NSString *CYPoolNSCYON(apr_pool_t *pool, id value); | |
575 | ||
576 | @interface NSMethodSignature (Cycript) | |
577 | - (NSString *) _typeString; | |
578 | @end | |
579 | ||
580 | @interface NSObject (Cycript) | |
581 | ||
582 | - (JSValueRef) cy$JSValueInContext:(JSContextRef)context; | |
583 | - (JSType) cy$JSType; | |
584 | ||
585 | - (NSObject *) cy$toJSON:(NSString *)key; | |
586 | - (NSString *) cy$toCYON; | |
587 | - (NSString *) cy$toKey; | |
588 | ||
589 | - (bool) cy$hasProperty:(NSString *)name; | |
590 | - (NSObject *) cy$getProperty:(NSString *)name; | |
591 | - (bool) cy$setProperty:(NSString *)name to:(NSObject *)value; | |
592 | - (bool) cy$deleteProperty:(NSString *)name; | |
593 | ||
594 | @end | |
595 | ||
596 | @protocol Cycript | |
597 | - (JSValueRef) cy$JSValueInContext:(JSContextRef)context; | |
598 | @end | |
599 | ||
600 | @interface NSString (Cycript) | |
601 | - (void *) cy$symbol; | |
602 | @end | |
603 | ||
604 | struct PropertyAttributes { | |
605 | CYPool pool_; | |
606 | ||
607 | const char *name; | |
608 | ||
609 | const char *variable; | |
610 | ||
611 | const char *getter_; | |
612 | const char *setter_; | |
613 | ||
614 | bool readonly; | |
615 | bool copy; | |
616 | bool retain; | |
617 | bool nonatomic; | |
618 | bool dynamic; | |
619 | bool weak; | |
620 | bool garbage; | |
621 | ||
622 | PropertyAttributes(objc_property_t property) : | |
623 | variable(NULL), | |
624 | getter_(NULL), | |
625 | setter_(NULL), | |
626 | readonly(false), | |
627 | copy(false), | |
628 | retain(false), | |
629 | nonatomic(false), | |
630 | dynamic(false), | |
631 | weak(false), | |
632 | garbage(false) | |
633 | { | |
634 | name = property_getName(property); | |
635 | const char *attributes(property_getAttributes(property)); | |
636 | ||
637 | for (char *state, *token(apr_strtok(apr_pstrdup(pool_, attributes), ",", &state)); token != NULL; token = apr_strtok(NULL, ",", &state)) { | |
638 | switch (*token) { | |
639 | case 'R': readonly = true; break; | |
640 | case 'C': copy = true; break; | |
641 | case '&': retain = true; break; | |
642 | case 'N': nonatomic = true; break; | |
643 | case 'G': getter_ = token + 1; break; | |
644 | case 'S': setter_ = token + 1; break; | |
645 | case 'V': variable = token + 1; break; | |
646 | } | |
647 | } | |
648 | ||
649 | /*if (variable == NULL) { | |
650 | variable = property_getName(property); | |
651 | size_t size(strlen(variable)); | |
652 | char *name(new(pool_) char[size + 2]); | |
653 | name[0] = '_'; | |
654 | memcpy(name + 1, variable, size); | |
655 | name[size + 1] = '\0'; | |
656 | variable = name; | |
657 | }*/ | |
658 | } | |
659 | ||
660 | const char *Getter() { | |
661 | if (getter_ == NULL) | |
662 | getter_ = apr_pstrdup(pool_, name); | |
663 | return getter_; | |
664 | } | |
665 | ||
666 | const char *Setter() { | |
667 | if (setter_ == NULL && !readonly) { | |
668 | size_t length(strlen(name)); | |
669 | ||
670 | char *temp(new(pool_) char[length + 5]); | |
671 | temp[0] = 's'; | |
672 | temp[1] = 'e'; | |
673 | temp[2] = 't'; | |
674 | ||
675 | if (length != 0) { | |
676 | temp[3] = toupper(name[0]); | |
677 | memcpy(temp + 4, name + 1, length - 1); | |
678 | } | |
679 | ||
680 | temp[length + 3] = ':'; | |
681 | temp[length + 4] = '\0'; | |
682 | setter_ = temp; | |
683 | } | |
684 | ||
685 | return setter_; | |
686 | } | |
687 | ||
688 | }; | |
689 | ||
690 | @implementation NSProxy (Cycript) | |
691 | ||
692 | - (NSObject *) cy$toJSON:(NSString *)key { | |
693 | return [self description]; | |
694 | } | |
695 | ||
696 | - (NSString *) cy$toCYON { | |
697 | return [[self cy$toJSON:@""] cy$toCYON]; | |
698 | } | |
699 | ||
700 | @end | |
701 | ||
702 | @implementation NSObject (Cycript) | |
703 | ||
704 | - (JSValueRef) cy$JSValueInContext:(JSContextRef)context { | |
705 | return CYMakeInstance(context, self, false); | |
706 | } | |
707 | ||
708 | - (JSType) cy$JSType { | |
709 | return kJSTypeObject; | |
710 | } | |
711 | ||
712 | - (NSObject *) cy$toJSON:(NSString *)key { | |
713 | return [self description]; | |
714 | } | |
715 | ||
716 | - (NSString *) cy$toCYON { | |
717 | return [[self cy$toJSON:@""] cy$toCYON]; | |
718 | } | |
719 | ||
720 | - (NSString *) cy$toKey { | |
721 | return [self cy$toCYON]; | |
722 | } | |
723 | ||
724 | - (bool) cy$hasProperty:(NSString *)name { | |
725 | return false; | |
726 | } | |
727 | ||
728 | - (NSObject *) cy$getProperty:(NSString *)name { | |
729 | return nil; | |
730 | } | |
731 | ||
732 | - (bool) cy$setProperty:(NSString *)name to:(NSObject *)value { | |
733 | return false; | |
734 | } | |
735 | ||
736 | - (bool) cy$deleteProperty:(NSString *)name { | |
737 | return false; | |
738 | } | |
739 | ||
740 | @end | |
741 | ||
742 | NSString *NSCFType$cy$toJSON(id self, SEL sel, NSString *key) { | |
743 | return [(NSString *) CFCopyDescription((CFTypeRef) self) autorelease]; | |
744 | } | |
745 | ||
746 | @implementation WebUndefined (Cycript) | |
747 | ||
748 | - (JSType) cy$JSType { | |
749 | return kJSTypeUndefined; | |
750 | } | |
751 | ||
752 | - (NSObject *) cy$toJSON:(NSString *)key { | |
753 | return self; | |
754 | } | |
755 | ||
756 | - (NSString *) cy$toCYON { | |
757 | return @"undefined"; | |
758 | } | |
759 | ||
760 | - (JSValueRef) cy$JSValueInContext:(JSContextRef)context { | |
761 | return CYJSUndefined(context); | |
762 | } | |
763 | ||
764 | @end | |
765 | ||
766 | @implementation NSNull (Cycript) | |
767 | ||
768 | - (JSType) cy$JSType { | |
769 | return kJSTypeNull; | |
770 | } | |
771 | ||
772 | - (NSObject *) cy$toJSON:(NSString *)key { | |
773 | return self; | |
774 | } | |
775 | ||
776 | - (NSString *) cy$toCYON { | |
777 | return @"null"; | |
778 | } | |
779 | ||
780 | @end | |
781 | ||
782 | @implementation NSArray (Cycript) | |
783 | ||
784 | - (NSString *) cy$toCYON { | |
785 | NSMutableString *json([[[NSMutableString alloc] init] autorelease]); | |
786 | [json appendString:@"["]; | |
787 | ||
788 | bool comma(false); | |
789 | for (id object in self) { | |
790 | if (comma) | |
791 | [json appendString:@","]; | |
792 | else | |
793 | comma = true; | |
794 | if (object == nil || [object cy$JSType] != kJSTypeUndefined) | |
795 | [json appendString:CYPoolNSCYON(NULL, object)]; | |
796 | else { | |
797 | [json appendString:@","]; | |
798 | comma = false; | |
799 | } | |
800 | } | |
801 | ||
802 | [json appendString:@"]"]; | |
803 | return json; | |
804 | } | |
805 | ||
806 | - (bool) cy$hasProperty:(NSString *)name { | |
807 | if ([name isEqualToString:@"length"]) | |
808 | return true; | |
809 | ||
810 | ssize_t index; | |
811 | if (!CYGetIndex(NULL, name, index) || index < 0 || index >= static_cast<ssize_t>([self count])) | |
812 | return [super cy$hasProperty:name]; | |
813 | else | |
814 | return true; | |
815 | } | |
816 | ||
817 | - (NSObject *) cy$getProperty:(NSString *)name { | |
818 | if ([name isEqualToString:@"length"]) | |
819 | return [NSNumber numberWithUnsignedInteger:[self count]]; | |
820 | ||
821 | ssize_t index; | |
822 | if (!CYGetIndex(NULL, name, index) || index < 0 || index >= static_cast<ssize_t>([self count])) | |
823 | return [super cy$getProperty:name]; | |
824 | else | |
825 | return [self objectAtIndex:index]; | |
826 | } | |
827 | ||
828 | @end | |
829 | ||
830 | @implementation NSMutableArray (Cycript) | |
831 | ||
832 | - (bool) cy$setProperty:(NSString *)name to:(NSObject *)value { | |
833 | ssize_t index; | |
834 | if (!CYGetIndex(NULL, name, index) || index < 0 || index >= static_cast<ssize_t>([self count])) | |
835 | return [super cy$setProperty:name to:value]; | |
836 | else { | |
837 | [self replaceObjectAtIndex:index withObject:(value ?: [NSNull null])]; | |
838 | return true; | |
839 | } | |
840 | } | |
841 | ||
842 | - (bool) cy$deleteProperty:(NSString *)name { | |
843 | ssize_t index; | |
844 | if (!CYGetIndex(NULL, name, index) || index < 0 || index >= static_cast<ssize_t>([self count])) | |
845 | return [super cy$deleteProperty:name]; | |
846 | else { | |
847 | [self removeObjectAtIndex:index]; | |
848 | return true; | |
849 | } | |
850 | } | |
851 | ||
852 | @end | |
853 | ||
854 | @implementation NSDictionary (Cycript) | |
855 | ||
856 | - (NSString *) cy$toCYON { | |
857 | NSMutableString *json([[[NSMutableString alloc] init] autorelease]); | |
858 | [json appendString:@"{"]; | |
859 | ||
860 | bool comma(false); | |
861 | for (id key in self) { | |
862 | if (comma) | |
863 | [json appendString:@","]; | |
864 | else | |
865 | comma = true; | |
866 | [json appendString:[key cy$toKey]]; | |
867 | [json appendString:@":"]; | |
868 | NSObject *object([self objectForKey:key]); | |
869 | [json appendString:CYPoolNSCYON(NULL, object)]; | |
870 | } | |
871 | ||
872 | [json appendString:@"}"]; | |
873 | return json; | |
874 | } | |
875 | ||
876 | - (bool) cy$hasProperty:(NSString *)name { | |
877 | return [self objectForKey:name] != nil; | |
878 | } | |
879 | ||
880 | - (NSObject *) cy$getProperty:(NSString *)name { | |
881 | return [self objectForKey:name]; | |
882 | } | |
883 | ||
884 | @end | |
885 | ||
886 | @implementation NSMutableDictionary (Cycript) | |
887 | ||
888 | - (bool) cy$setProperty:(NSString *)name to:(NSObject *)value { | |
889 | [self setObject:(value ?: [NSNull null]) forKey:name]; | |
890 | return true; | |
891 | } | |
892 | ||
893 | - (bool) cy$deleteProperty:(NSString *)name { | |
894 | if ([self objectForKey:name] == nil) | |
895 | return false; | |
896 | else { | |
897 | [self removeObjectForKey:name]; | |
898 | return true; | |
899 | } | |
900 | } | |
901 | ||
902 | @end | |
903 | ||
904 | @implementation NSNumber (Cycript) | |
905 | ||
906 | - (JSType) cy$JSType { | |
907 | // XXX: this just seems stupid | |
908 | return [self class] == NSCFBoolean_ ? kJSTypeBoolean : kJSTypeNumber; | |
909 | } | |
910 | ||
911 | - (NSObject *) cy$toJSON:(NSString *)key { | |
912 | return self; | |
913 | } | |
914 | ||
915 | - (NSString *) cy$toCYON { | |
916 | return [self cy$JSType] != kJSTypeBoolean ? [self stringValue] : [self boolValue] ? @"true" : @"false"; | |
917 | } | |
918 | ||
919 | - (JSValueRef) cy$JSValueInContext:(JSContextRef)context { | |
920 | return [self cy$JSType] != kJSTypeBoolean ? CYCastJSValue(context, [self doubleValue]) : CYCastJSValue(context, [self boolValue]); | |
921 | } | |
922 | ||
923 | @end | |
924 | ||
925 | @implementation NSString (Cycript) | |
926 | ||
927 | - (JSType) cy$JSType { | |
928 | return kJSTypeString; | |
929 | } | |
930 | ||
931 | - (NSObject *) cy$toJSON:(NSString *)key { | |
932 | return self; | |
933 | } | |
934 | ||
935 | - (NSString *) cy$toCYON { | |
936 | // XXX: this should use the better code from Output.cpp | |
937 | CFMutableStringRef json(CFStringCreateMutableCopy(kCFAllocatorDefault, 0, (CFStringRef) self)); | |
938 | ||
939 | CFStringFindAndReplace(json, CFSTR("\\"), CFSTR("\\\\"), CFRangeMake(0, CFStringGetLength(json)), 0); | |
940 | CFStringFindAndReplace(json, CFSTR("\""), CFSTR("\\\""), CFRangeMake(0, CFStringGetLength(json)), 0); | |
941 | CFStringFindAndReplace(json, CFSTR("\t"), CFSTR("\\t"), CFRangeMake(0, CFStringGetLength(json)), 0); | |
942 | CFStringFindAndReplace(json, CFSTR("\r"), CFSTR("\\r"), CFRangeMake(0, CFStringGetLength(json)), 0); | |
943 | CFStringFindAndReplace(json, CFSTR("\n"), CFSTR("\\n"), CFRangeMake(0, CFStringGetLength(json)), 0); | |
944 | ||
945 | CFStringInsert(json, 0, CFSTR("\"")); | |
946 | CFStringAppend(json, CFSTR("\"")); | |
947 | ||
948 | return [reinterpret_cast<const NSString *>(json) autorelease]; | |
949 | } | |
950 | ||
951 | - (NSString *) cy$toKey { | |
952 | const char *value([self UTF8String]); | |
953 | size_t size(strlen(value)); | |
954 | ||
955 | if (size == 0) | |
956 | goto cyon; | |
957 | ||
958 | if (DigitRange_[value[0]]) { | |
959 | ssize_t index; | |
960 | if (!CYGetIndex(NULL, self, index) || index < 0) | |
961 | goto cyon; | |
962 | } else { | |
963 | if (!WordStartRange_[value[0]]) | |
964 | goto cyon; | |
965 | for (size_t i(1); i != size; ++i) | |
966 | if (!WordEndRange_[value[i]]) | |
967 | goto cyon; | |
968 | } | |
969 | ||
970 | return self; | |
971 | ||
972 | cyon: | |
973 | return [self cy$toCYON]; | |
974 | } | |
975 | ||
976 | - (void *) cy$symbol { | |
977 | CYPool pool; | |
978 | return dlsym(RTLD_DEFAULT, CYPoolCString(pool, self)); | |
979 | } | |
980 | ||
981 | @end | |
982 | ||
983 | @interface CYJSObject : NSDictionary { | |
984 | JSObjectRef object_; | |
985 | JSContextRef context_; | |
986 | } | |
987 | ||
988 | - (id) initWithJSObject:(JSObjectRef)object inContext:(JSContextRef)context; | |
989 | ||
990 | - (NSString *) cy$toJSON:(NSString *)key; | |
991 | ||
992 | - (NSUInteger) count; | |
993 | - (id) objectForKey:(id)key; | |
994 | - (NSEnumerator *) keyEnumerator; | |
995 | - (void) setObject:(id)object forKey:(id)key; | |
996 | - (void) removeObjectForKey:(id)key; | |
997 | ||
998 | @end | |
999 | ||
1000 | @interface CYJSArray : NSArray { | |
1001 | JSObjectRef object_; | |
1002 | JSContextRef context_; | |
1003 | } | |
1004 | ||
1005 | - (id) initWithJSObject:(JSObjectRef)object inContext:(JSContextRef)context; | |
1006 | ||
1007 | - (NSUInteger) count; | |
1008 | - (id) objectAtIndex:(NSUInteger)index; | |
1009 | ||
1010 | @end | |
1011 | ||
1012 | CYRange DigitRange_ (0x3ff000000000000LLU, 0x000000000000000LLU); // 0-9 | |
1013 | CYRange WordStartRange_(0x000001000000000LLU, 0x7fffffe87fffffeLLU); // A-Za-z_$ | |
1014 | CYRange WordEndRange_ (0x3ff001000000000LLU, 0x7fffffe87fffffeLLU); // A-Za-z_$0-9 | |
1015 | ||
1016 | #define CYTry \ | |
1017 | @try | |
1018 | #define CYCatch \ | |
1019 | @catch (id error) { \ | |
1020 | CYThrow(context, error, exception); \ | |
1021 | return NULL; \ | |
1022 | } | |
1023 | ||
1024 | void CYThrow(JSContextRef context, JSValueRef value); | |
1025 | ||
1026 | apr_status_t CYPoolRelease_(void *data) { | |
1027 | id object(reinterpret_cast<id>(data)); | |
1028 | [object release]; | |
1029 | return APR_SUCCESS; | |
1030 | } | |
1031 | ||
1032 | id CYPoolRelease(apr_pool_t *pool, id object) { | |
1033 | if (object == nil) | |
1034 | return nil; | |
1035 | else if (pool == NULL) | |
1036 | return [object autorelease]; | |
1037 | else { | |
1038 | apr_pool_cleanup_register(pool, object, &CYPoolRelease_, &apr_pool_cleanup_null); | |
1039 | return object; | |
1040 | } | |
1041 | } | |
1042 | ||
1043 | CFTypeRef CYPoolRelease(apr_pool_t *pool, CFTypeRef object) { | |
1044 | return (CFTypeRef) CYPoolRelease(pool, (id) object); | |
1045 | } | |
1046 | ||
1047 | id CYCastNSObject_(apr_pool_t *pool, JSContextRef context, JSObjectRef object) { | |
1048 | JSValueRef exception(NULL); | |
1049 | bool array(JSValueIsInstanceOfConstructor(context, object, Array_, &exception)); | |
1050 | CYThrow(context, exception); | |
1051 | id value(array ? [CYJSArray alloc] : [CYJSObject alloc]); | |
1052 | return CYPoolRelease(pool, [value initWithJSObject:object inContext:context]); | |
1053 | } | |
1054 | ||
1055 | id CYCastNSObject(apr_pool_t *pool, JSContextRef context, JSObjectRef object) { | |
1056 | if (!JSValueIsObjectOfClass(context, object, Instance_)) | |
1057 | return CYCastNSObject_(pool, context, object); | |
1058 | else { | |
1059 | Instance *data(reinterpret_cast<Instance *>(JSObjectGetPrivate(object))); | |
1060 | return data->GetValue(); | |
1061 | } | |
1062 | } | |
1063 | ||
1064 | JSStringRef CYCopyJSString(id value) { | |
1065 | return value == NULL ? NULL : JSStringCreateWithCFString(reinterpret_cast<CFStringRef>([value description])); | |
1066 | } | |
1067 | ||
1068 | JSStringRef CYCopyJSString(const char *value) { | |
1069 | return value == NULL ? NULL : JSStringCreateWithUTF8CString(value); | |
1070 | } | |
1071 | ||
1072 | JSStringRef CYCopyJSString(JSStringRef value) { | |
1073 | return value == NULL ? NULL : JSStringRetain(value); | |
1074 | } | |
1075 | ||
1076 | JSStringRef CYCopyJSString(JSContextRef context, JSValueRef value) { | |
1077 | if (JSValueIsNull(context, value)) | |
1078 | return NULL; | |
1079 | JSValueRef exception(NULL); | |
1080 | JSStringRef string(JSValueToStringCopy(context, value, &exception)); | |
1081 | CYThrow(context, exception); | |
1082 | return string; | |
1083 | } | |
1084 | ||
1085 | class CYJSString { | |
1086 | private: | |
1087 | JSStringRef string_; | |
1088 | ||
1089 | void Clear_() { | |
1090 | if (string_ != NULL) | |
1091 | JSStringRelease(string_); | |
1092 | } | |
1093 | ||
1094 | public: | |
1095 | CYJSString(const CYJSString &rhs) : | |
1096 | string_(CYCopyJSString(rhs.string_)) | |
1097 | { | |
1098 | } | |
1099 | ||
1100 | template <typename Arg0_> | |
1101 | CYJSString(Arg0_ arg0) : | |
1102 | string_(CYCopyJSString(arg0)) | |
1103 | { | |
1104 | } | |
1105 | ||
1106 | template <typename Arg0_, typename Arg1_> | |
1107 | CYJSString(Arg0_ arg0, Arg1_ arg1) : | |
1108 | string_(CYCopyJSString(arg0, arg1)) | |
1109 | { | |
1110 | } | |
1111 | ||
1112 | CYJSString &operator =(const CYJSString &rhs) { | |
1113 | Clear_(); | |
1114 | string_ = CYCopyJSString(rhs.string_); | |
1115 | return *this; | |
1116 | } | |
1117 | ||
1118 | ~CYJSString() { | |
1119 | Clear_(); | |
1120 | } | |
1121 | ||
1122 | void Clear() { | |
1123 | Clear_(); | |
1124 | string_ = NULL; | |
1125 | } | |
1126 | ||
1127 | operator JSStringRef() const { | |
1128 | return string_; | |
1129 | } | |
1130 | }; | |
1131 | ||
1132 | CFStringRef CYCopyCFString(JSStringRef value) { | |
1133 | return JSStringCopyCFString(kCFAllocatorDefault, value); | |
1134 | } | |
1135 | ||
1136 | CFStringRef CYCopyCFString(JSContextRef context, JSValueRef value) { | |
1137 | return CYCopyCFString(CYJSString(context, value)); | |
1138 | } | |
1139 | ||
1140 | double CYCastDouble(const char *value, size_t size) { | |
1141 | char *end; | |
1142 | double number(strtod(value, &end)); | |
1143 | if (end != value + size) | |
1144 | return NAN; | |
1145 | return number; | |
1146 | } | |
1147 | ||
1148 | double CYCastDouble(const char *value) { | |
1149 | return CYCastDouble(value, strlen(value)); | |
1150 | } | |
1151 | ||
1152 | double CYCastDouble(JSContextRef context, JSValueRef value) { | |
1153 | JSValueRef exception(NULL); | |
1154 | double number(JSValueToNumber(context, value, &exception)); | |
1155 | CYThrow(context, exception); | |
1156 | return number; | |
1157 | } | |
1158 | ||
1159 | CFNumberRef CYCopyCFNumber(JSContextRef context, JSValueRef value) { | |
1160 | double number(CYCastDouble(context, value)); | |
1161 | return CFNumberCreate(kCFAllocatorDefault, kCFNumberDoubleType, &number); | |
1162 | } | |
1163 | ||
1164 | CFStringRef CYCopyCFString(const char *value) { | |
1165 | return CFStringCreateWithCString(kCFAllocatorDefault, value, kCFStringEncodingUTF8); | |
1166 | } | |
1167 | ||
1168 | NSString *CYCastNSString(apr_pool_t *pool, const char *value) { | |
1169 | return (NSString *) CYPoolRelease(pool, CYCopyCFString(value)); | |
1170 | } | |
1171 | ||
1172 | NSString *CYCastNSString(apr_pool_t *pool, JSStringRef value) { | |
1173 | return (NSString *) CYPoolRelease(pool, CYCopyCFString(value)); | |
1174 | } | |
1175 | ||
1176 | bool CYCastBool(JSContextRef context, JSValueRef value) { | |
1177 | return JSValueToBoolean(context, value); | |
1178 | } | |
1179 | ||
1180 | CFTypeRef CYCFType(apr_pool_t *pool, JSContextRef context, JSValueRef value, bool cast) { | |
1181 | CFTypeRef object; | |
1182 | bool copy; | |
1183 | ||
1184 | switch (JSType type = JSValueGetType(context, value)) { | |
1185 | case kJSTypeUndefined: | |
1186 | object = [WebUndefined undefined]; | |
1187 | copy = false; | |
1188 | break; | |
1189 | ||
1190 | case kJSTypeNull: | |
1191 | return NULL; | |
1192 | break; | |
1193 | ||
1194 | case kJSTypeBoolean: | |
1195 | object = CYCastBool(context, value) ? kCFBooleanTrue : kCFBooleanFalse; | |
1196 | copy = false; | |
1197 | break; | |
1198 | ||
1199 | case kJSTypeNumber: | |
1200 | object = CYCopyCFNumber(context, value); | |
1201 | copy = true; | |
1202 | break; | |
1203 | ||
1204 | case kJSTypeString: | |
1205 | object = CYCopyCFString(context, value); | |
1206 | copy = true; | |
1207 | break; | |
1208 | ||
1209 | case kJSTypeObject: | |
1210 | // XXX: this might could be more efficient | |
1211 | object = (CFTypeRef) CYCastNSObject(pool, context, (JSObjectRef) value); | |
1212 | copy = false; | |
1213 | break; | |
1214 | ||
1215 | default: | |
1216 | @throw [NSException exceptionWithName:NSInternalInconsistencyException reason:[NSString stringWithFormat:@"JSValueGetType() == 0x%x", type] userInfo:nil]; | |
1217 | break; | |
1218 | } | |
1219 | ||
1220 | if (cast != copy) | |
1221 | return object; | |
1222 | else if (copy) | |
1223 | return CYPoolRelease(pool, object); | |
1224 | else | |
1225 | return CFRetain(object); | |
1226 | } | |
1227 | ||
1228 | CFTypeRef CYCastCFType(apr_pool_t *pool, JSContextRef context, JSValueRef value) { | |
1229 | return CYCFType(pool, context, value, true); | |
1230 | } | |
1231 | ||
1232 | CFTypeRef CYCopyCFType(apr_pool_t *pool, JSContextRef context, JSValueRef value) { | |
1233 | return CYCFType(pool, context, value, false); | |
1234 | } | |
1235 | ||
1236 | NSArray *CYCastNSArray(JSPropertyNameArrayRef names) { | |
1237 | CYPool pool; | |
1238 | size_t size(JSPropertyNameArrayGetCount(names)); | |
1239 | NSMutableArray *array([NSMutableArray arrayWithCapacity:size]); | |
1240 | for (size_t index(0); index != size; ++index) | |
1241 | [array addObject:CYCastNSString(pool, JSPropertyNameArrayGetNameAtIndex(names, index))]; | |
1242 | return array; | |
1243 | } | |
1244 | ||
1245 | id CYCastNSObject(apr_pool_t *pool, JSContextRef context, JSValueRef value) { | |
1246 | return reinterpret_cast<const NSObject *>(CYCastCFType(pool, context, value)); | |
1247 | } | |
1248 | ||
1249 | void CYThrow(JSContextRef context, JSValueRef value) { | |
1250 | if (value == NULL) | |
1251 | return; | |
1252 | @throw CYCastNSObject(NULL, context, value); | |
1253 | } | |
1254 | ||
1255 | JSValueRef CYJSNull(JSContextRef context) { | |
1256 | return JSValueMakeNull(context); | |
1257 | } | |
1258 | ||
1259 | JSValueRef CYCastJSValue(JSContextRef context, JSStringRef value) { | |
1260 | return value == NULL ? CYJSNull(context) : JSValueMakeString(context, value); | |
1261 | } | |
1262 | ||
1263 | JSValueRef CYCastJSValue(JSContextRef context, const char *value) { | |
1264 | return CYCastJSValue(context, CYJSString(value)); | |
1265 | } | |
1266 | ||
1267 | JSValueRef CYCastJSValue(JSContextRef context, id value) { | |
1268 | if (value == nil) | |
1269 | return CYJSNull(context); | |
1270 | else if ([value respondsToSelector:@selector(cy$JSValueInContext:)]) | |
1271 | return [value cy$JSValueInContext:context]; | |
1272 | else | |
1273 | return CYMakeInstance(context, value, false); | |
1274 | } | |
1275 | ||
1276 | JSObjectRef CYCastJSObject(JSContextRef context, JSValueRef value) { | |
1277 | JSValueRef exception(NULL); | |
1278 | JSObjectRef object(JSValueToObject(context, value, &exception)); | |
1279 | CYThrow(context, exception); | |
1280 | return object; | |
1281 | } | |
1282 | ||
1283 | JSValueRef CYGetProperty(JSContextRef context, JSObjectRef object, size_t index) { | |
1284 | JSValueRef exception(NULL); | |
1285 | JSValueRef value(JSObjectGetPropertyAtIndex(context, object, index, &exception)); | |
1286 | CYThrow(context, exception); | |
1287 | return value; | |
1288 | } | |
1289 | ||
1290 | JSValueRef CYGetProperty(JSContextRef context, JSObjectRef object, JSStringRef name) { | |
1291 | JSValueRef exception(NULL); | |
1292 | JSValueRef value(JSObjectGetProperty(context, object, name, &exception)); | |
1293 | CYThrow(context, exception); | |
1294 | return value; | |
1295 | } | |
1296 | ||
1297 | void CYSetProperty(JSContextRef context, JSObjectRef object, JSStringRef name, JSValueRef value) { | |
1298 | JSValueRef exception(NULL); | |
1299 | JSObjectSetProperty(context, object, name, value, kJSPropertyAttributeNone, &exception); | |
1300 | CYThrow(context, exception); | |
1301 | } | |
1302 | ||
1303 | void CYThrow(JSContextRef context, id error, JSValueRef *exception) { | |
1304 | if (exception == NULL) | |
1305 | throw error; | |
1306 | *exception = CYCastJSValue(context, error); | |
1307 | } | |
1308 | ||
1309 | JSValueRef CYCallAsFunction(JSContextRef context, JSObjectRef function, JSObjectRef _this, size_t count, JSValueRef arguments[]) { | |
1310 | JSValueRef exception(NULL); | |
1311 | JSValueRef value(JSObjectCallAsFunction(context, function, _this, count, arguments, &exception)); | |
1312 | CYThrow(context, exception); | |
1313 | return value; | |
1314 | } | |
1315 | ||
1316 | bool CYIsCallable(JSContextRef context, JSValueRef value) { | |
1317 | // XXX: this isn't actually correct | |
1318 | return value != NULL && JSValueIsObject(context, value); | |
1319 | } | |
1320 | ||
1321 | @implementation CYJSObject | |
1322 | ||
1323 | - (id) initWithJSObject:(JSObjectRef)object inContext:(JSContextRef)context { | |
1324 | if ((self = [super init]) != nil) { | |
1325 | object_ = object; | |
1326 | context_ = context; | |
1327 | } return self; | |
1328 | } | |
1329 | ||
1330 | - (NSObject *) cy$toJSON:(NSString *)key { | |
1331 | JSValueRef toJSON(CYGetProperty(context_, object_, toJSON_)); | |
1332 | if (!CYIsCallable(context_, toJSON)) | |
1333 | return [super cy$toJSON:key]; | |
1334 | else { | |
1335 | JSValueRef arguments[1] = {CYCastJSValue(context_, key)}; | |
1336 | JSValueRef value(CYCallAsFunction(context_, (JSObjectRef) toJSON, object_, 1, arguments)); | |
1337 | // XXX: do I really want an NSNull here?! | |
1338 | return CYCastNSObject(NULL, context_, value) ?: [NSNull null]; | |
1339 | } | |
1340 | } | |
1341 | ||
1342 | - (NSString *) cy$toCYON { | |
1343 | JSValueRef toCYON(CYGetProperty(context_, object_, toCYON_)); | |
1344 | if (!CYIsCallable(context_, toCYON)) | |
1345 | return [super cy$toCYON]; | |
1346 | else { | |
1347 | JSValueRef value(CYCallAsFunction(context_, (JSObjectRef) toCYON, object_, 0, NULL)); | |
1348 | return CYCastNSString(NULL, CYJSString(context_, value)); | |
1349 | } | |
1350 | } | |
1351 | ||
1352 | - (NSUInteger) count { | |
1353 | JSPropertyNameArrayRef names(JSObjectCopyPropertyNames(context_, object_)); | |
1354 | size_t size(JSPropertyNameArrayGetCount(names)); | |
1355 | JSPropertyNameArrayRelease(names); | |
1356 | return size; | |
1357 | } | |
1358 | ||
1359 | - (id) objectForKey:(id)key { | |
1360 | return CYCastNSObject(NULL, context_, CYGetProperty(context_, object_, CYJSString(key))) ?: [NSNull null]; | |
1361 | } | |
1362 | ||
1363 | - (NSEnumerator *) keyEnumerator { | |
1364 | JSPropertyNameArrayRef names(JSObjectCopyPropertyNames(context_, object_)); | |
1365 | NSEnumerator *enumerator([CYCastNSArray(names) objectEnumerator]); | |
1366 | JSPropertyNameArrayRelease(names); | |
1367 | return enumerator; | |
1368 | } | |
1369 | ||
1370 | - (void) setObject:(id)object forKey:(id)key { | |
1371 | CYSetProperty(context_, object_, CYJSString(key), CYCastJSValue(context_, object)); | |
1372 | } | |
1373 | ||
1374 | - (void) removeObjectForKey:(id)key { | |
1375 | JSValueRef exception(NULL); | |
1376 | (void) JSObjectDeleteProperty(context_, object_, CYJSString(key), &exception); | |
1377 | CYThrow(context_, exception); | |
1378 | } | |
1379 | ||
1380 | @end | |
1381 | ||
1382 | @implementation CYJSArray | |
1383 | ||
1384 | - (id) initWithJSObject:(JSObjectRef)object inContext:(JSContextRef)context { | |
1385 | if ((self = [super init]) != nil) { | |
1386 | object_ = object; | |
1387 | context_ = context; | |
1388 | } return self; | |
1389 | } | |
1390 | ||
1391 | - (NSUInteger) count { | |
1392 | return CYCastDouble(context_, CYGetProperty(context_, object_, length_)); | |
1393 | } | |
1394 | ||
1395 | - (id) objectAtIndex:(NSUInteger)index { | |
1396 | JSValueRef exception(NULL); | |
1397 | JSValueRef value(JSObjectGetPropertyAtIndex(context_, object_, index, &exception)); | |
1398 | CYThrow(context_, exception); | |
1399 | return CYCastNSObject(NULL, context_, value) ?: [NSNull null]; | |
1400 | } | |
1401 | ||
1402 | @end | |
1403 | ||
1404 | NSString *CYCopyNSCYON(id value) { | |
1405 | NSString *string; | |
1406 | ||
1407 | if (value == nil) | |
1408 | string = @"nil"; | |
1409 | else { | |
1410 | Class _class(object_getClass(value)); | |
1411 | SEL sel(@selector(cy$toCYON)); | |
1412 | ||
1413 | if (Method toCYON = class_getInstanceMethod(_class, sel)) | |
1414 | string = reinterpret_cast<NSString *(*)(id, SEL)>(method_getImplementation(toCYON))(value, sel); | |
1415 | else if (Method methodSignatureForSelector = class_getInstanceMethod(_class, @selector(methodSignatureForSelector:))) { | |
1416 | if (reinterpret_cast<NSMethodSignature *(*)(id, SEL, SEL)>(method_getImplementation(methodSignatureForSelector))(value, @selector(methodSignatureForSelector:), sel) != nil) | |
1417 | string = [value cy$toCYON]; | |
1418 | else goto fail; | |
1419 | } else fail: { | |
1420 | if (value == NSZombie_) | |
1421 | string = @"_NSZombie_"; | |
1422 | else if (_class == NSZombie_) | |
1423 | string = [NSString stringWithFormat:@"<_NSZombie_: %p>", value]; | |
1424 | // XXX: frowny /in/ the pants | |
1425 | else if (value == NSMessageBuilder_ || value == Object_) | |
1426 | string = nil; | |
1427 | else | |
1428 | string = [NSString stringWithFormat:@"%@", value]; | |
1429 | } | |
1430 | ||
1431 | // XXX: frowny pants | |
1432 | if (string == nil) | |
1433 | string = @"undefined"; | |
1434 | } | |
1435 | ||
1436 | return [string retain]; | |
1437 | } | |
1438 | ||
1439 | NSString *CYCopyNSCYON(JSContextRef context, JSValueRef value, JSValueRef *exception) { | |
1440 | CYTry { | |
1441 | CYPoolTry { | |
1442 | return CYCopyNSCYON(CYCastNSObject(NULL, context, value)); | |
1443 | } CYPoolCatch(NULL) | |
1444 | } CYCatch | |
1445 | } | |
1446 | ||
1447 | NSString *CYPoolNSCYON(apr_pool_t *pool, id value) { | |
1448 | return CYPoolRelease(pool, static_cast<id>(CYCopyNSCYON(value))); | |
1449 | } | |
1450 | ||
1451 | const char *CYPoolCCYON(apr_pool_t *pool, JSContextRef context, JSValueRef value, JSValueRef *exception) { | |
1452 | if (NSString *json = CYCopyNSCYON(context, value, exception)) { | |
1453 | const char *string(CYPoolCString(pool, json)); | |
1454 | [json release]; | |
1455 | return string; | |
1456 | } else return NULL; | |
1457 | } | |
1458 | ||
1459 | // XXX: use objc_getAssociatedObject and objc_setAssociatedObject on 10.6 | |
1460 | struct CYInternal : | |
1461 | CYData | |
1462 | { | |
1463 | JSObjectRef object_; | |
1464 | ||
1465 | CYInternal() : | |
1466 | object_(NULL) | |
1467 | { | |
1468 | } | |
1469 | ||
1470 | ~CYInternal() { | |
1471 | // XXX: delete object_? ;( | |
1472 | } | |
1473 | ||
1474 | static CYInternal *Get(id self) { | |
1475 | CYInternal *internal(NULL); | |
1476 | if (object_getInstanceVariable(self, "cy$internal_", reinterpret_cast<void **>(&internal)) == NULL) { | |
1477 | // XXX: do something epic? ;P | |
1478 | } | |
1479 | ||
1480 | return internal; | |
1481 | } | |
1482 | ||
1483 | static CYInternal *Set(id self) { | |
1484 | CYInternal *internal(NULL); | |
1485 | if (Ivar ivar = object_getInstanceVariable(self, "cy$internal_", reinterpret_cast<void **>(&internal))) { | |
1486 | if (internal == NULL) { | |
1487 | internal = new CYInternal(); | |
1488 | object_setIvar(self, ivar, reinterpret_cast<id>(internal)); | |
1489 | } | |
1490 | } else { | |
1491 | // XXX: do something epic? ;P | |
1492 | } | |
1493 | ||
1494 | return internal; | |
1495 | } | |
1496 | ||
1497 | bool HasProperty(JSContextRef context, JSStringRef name) { | |
1498 | if (object_ == NULL) | |
1499 | return false; | |
1500 | return JSObjectHasProperty(context, object_, name); | |
1501 | } | |
1502 | ||
1503 | JSValueRef GetProperty(JSContextRef context, JSStringRef name) { | |
1504 | if (object_ == NULL) | |
1505 | return NULL; | |
1506 | return CYGetProperty(context, object_, name); | |
1507 | } | |
1508 | ||
1509 | void SetProperty(JSContextRef context, JSStringRef name, JSValueRef value) { | |
1510 | if (object_ == NULL) | |
1511 | object_ = JSObjectMake(context, NULL, NULL); | |
1512 | CYSetProperty(context, object_, name, value); | |
1513 | } | |
1514 | }; | |
1515 | ||
1516 | JSObjectRef CYMakeSelector(JSContextRef context, SEL sel) { | |
1517 | Selector_privateData *data(new Selector_privateData(sel)); | |
1518 | return JSObjectMake(context, Selector_, data); | |
1519 | } | |
1520 | ||
1521 | JSObjectRef CYMakePointer(JSContextRef context, void *pointer, sig::Type *type, ffi_type *ffi, JSObjectRef owner) { | |
1522 | Pointer *data(new Pointer(pointer, type, owner)); | |
1523 | return JSObjectMake(context, Pointer_, data); | |
1524 | } | |
1525 | ||
1526 | JSObjectRef CYMakeFunctor(JSContextRef context, void (*function)(), const char *type) { | |
1527 | Functor_privateData *data(new Functor_privateData(type, function)); | |
1528 | return JSObjectMake(context, Functor_, data); | |
1529 | } | |
1530 | ||
1531 | const char *CYPoolCString(apr_pool_t *pool, JSStringRef value) { | |
1532 | if (pool == NULL) { | |
1533 | const char *string([CYCastNSString(NULL, value) UTF8String]); | |
1534 | return string; | |
1535 | } else { | |
1536 | size_t size(JSStringGetMaximumUTF8CStringSize(value)); | |
1537 | char *string(new(pool) char[size]); | |
1538 | JSStringGetUTF8CString(value, string, size); | |
1539 | return string; | |
1540 | } | |
1541 | } | |
1542 | ||
1543 | const char *CYPoolCString(apr_pool_t *pool, JSContextRef context, JSValueRef value) { | |
1544 | return JSValueIsNull(context, value) ? NULL : CYPoolCString(pool, CYJSString(context, value)); | |
1545 | } | |
1546 | ||
1547 | bool CYGetIndex(apr_pool_t *pool, JSStringRef value, ssize_t &index) { | |
1548 | return CYGetIndex(CYPoolCString(pool, value), index); | |
1549 | } | |
1550 | ||
1551 | // XXX: this macro is unhygenic | |
1552 | #define CYCastCString(context, value) ({ \ | |
1553 | char *utf8; \ | |
1554 | if (value == NULL) \ | |
1555 | utf8 = NULL; \ | |
1556 | else if (JSStringRef string = CYCopyJSString(context, value)) { \ | |
1557 | size_t size(JSStringGetMaximumUTF8CStringSize(string)); \ | |
1558 | utf8 = reinterpret_cast<char *>(alloca(size)); \ | |
1559 | JSStringGetUTF8CString(string, utf8, size); \ | |
1560 | JSStringRelease(string); \ | |
1561 | } else \ | |
1562 | utf8 = NULL; \ | |
1563 | utf8; \ | |
1564 | }) | |
1565 | ||
1566 | void *CYCastPointer_(JSContextRef context, JSValueRef value) { | |
1567 | switch (JSValueGetType(context, value)) { | |
1568 | case kJSTypeNull: | |
1569 | return NULL; | |
1570 | /*case kJSTypeString: | |
1571 | return dlsym(RTLD_DEFAULT, CYCastCString(context, value)); | |
1572 | case kJSTypeObject: | |
1573 | if (JSValueIsObjectOfClass(context, value, Pointer_)) { | |
1574 | Pointer *data(reinterpret_cast<Pointer *>(JSObjectGetPrivate((JSObjectRef) value))); | |
1575 | return data->value_; | |
1576 | }*/ | |
1577 | default: | |
1578 | double number(CYCastDouble(context, value)); | |
1579 | if (std::isnan(number)) | |
1580 | @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"cannot convert value to pointer" userInfo:nil]; | |
1581 | return reinterpret_cast<void *>(static_cast<uintptr_t>(static_cast<long long>(number))); | |
1582 | } | |
1583 | } | |
1584 | ||
1585 | template <typename Type_> | |
1586 | _finline Type_ CYCastPointer(JSContextRef context, JSValueRef value) { | |
1587 | return reinterpret_cast<Type_>(CYCastPointer_(context, value)); | |
1588 | } | |
1589 | ||
1590 | SEL CYCastSEL(JSContextRef context, JSValueRef value) { | |
1591 | if (JSValueIsObjectOfClass(context, value, Selector_)) { | |
1592 | Selector_privateData *data(reinterpret_cast<Selector_privateData *>(JSObjectGetPrivate((JSObjectRef) value))); | |
1593 | return reinterpret_cast<SEL>(data->value_); | |
1594 | } else | |
1595 | return CYCastPointer<SEL>(context, value); | |
1596 | } | |
1597 | ||
1598 | void CYPoolFFI(apr_pool_t *pool, JSContextRef context, sig::Type *type, ffi_type *ffi, void *data, JSValueRef value) { | |
1599 | switch (type->primitive) { | |
1600 | case sig::boolean_P: | |
1601 | *reinterpret_cast<bool *>(data) = JSValueToBoolean(context, value); | |
1602 | break; | |
1603 | ||
1604 | #define CYPoolFFI_(primitive, native) \ | |
1605 | case sig::primitive ## _P: \ | |
1606 | *reinterpret_cast<native *>(data) = CYCastDouble(context, value); \ | |
1607 | break; | |
1608 | ||
1609 | CYPoolFFI_(uchar, unsigned char) | |
1610 | CYPoolFFI_(char, char) | |
1611 | CYPoolFFI_(ushort, unsigned short) | |
1612 | CYPoolFFI_(short, short) | |
1613 | CYPoolFFI_(ulong, unsigned long) | |
1614 | CYPoolFFI_(long, long) | |
1615 | CYPoolFFI_(uint, unsigned int) | |
1616 | CYPoolFFI_(int, int) | |
1617 | CYPoolFFI_(ulonglong, unsigned long long) | |
1618 | CYPoolFFI_(longlong, long long) | |
1619 | CYPoolFFI_(float, float) | |
1620 | CYPoolFFI_(double, double) | |
1621 | ||
1622 | case sig::object_P: | |
1623 | case sig::typename_P: | |
1624 | *reinterpret_cast<id *>(data) = CYCastNSObject(pool, context, value); | |
1625 | break; | |
1626 | ||
1627 | case sig::selector_P: | |
1628 | *reinterpret_cast<SEL *>(data) = CYCastSEL(context, value); | |
1629 | break; | |
1630 | ||
1631 | case sig::pointer_P: | |
1632 | *reinterpret_cast<void **>(data) = CYCastPointer<void *>(context, value); | |
1633 | break; | |
1634 | ||
1635 | case sig::string_P: | |
1636 | *reinterpret_cast<const char **>(data) = CYPoolCString(pool, context, value); | |
1637 | break; | |
1638 | ||
1639 | case sig::struct_P: { | |
1640 | uint8_t *base(reinterpret_cast<uint8_t *>(data)); | |
1641 | JSObjectRef aggregate(JSValueIsObject(context, value) ? (JSObjectRef) value : NULL); | |
1642 | for (size_t index(0); index != type->data.signature.count; ++index) { | |
1643 | sig::Element *element(&type->data.signature.elements[index]); | |
1644 | ffi_type *field(ffi->elements[index]); | |
1645 | ||
1646 | JSValueRef rhs; | |
1647 | if (aggregate == NULL) | |
1648 | rhs = value; | |
1649 | else { | |
1650 | rhs = CYGetProperty(context, aggregate, index); | |
1651 | if (JSValueIsUndefined(context, rhs)) { | |
1652 | if (element->name != NULL) | |
1653 | rhs = CYGetProperty(context, aggregate, CYJSString(element->name)); | |
1654 | else | |
1655 | goto undefined; | |
1656 | if (JSValueIsUndefined(context, rhs)) undefined: | |
1657 | @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"unable to extract structure value" userInfo:nil]; | |
1658 | } | |
1659 | } | |
1660 | ||
1661 | CYPoolFFI(pool, context, element->type, field, base, rhs); | |
1662 | // XXX: alignment? | |
1663 | base += field->size; | |
1664 | } | |
1665 | } break; | |
1666 | ||
1667 | case sig::void_P: | |
1668 | break; | |
1669 | ||
1670 | default: | |
1671 | NSLog(@"CYPoolFFI(%c)\n", type->primitive); | |
1672 | _assert(false); | |
1673 | } | |
1674 | } | |
1675 | ||
1676 | JSValueRef CYFromFFI(JSContextRef context, sig::Type *type, ffi_type *ffi, void *data, bool initialize = false, JSObjectRef owner = NULL) { | |
1677 | JSValueRef value; | |
1678 | ||
1679 | switch (type->primitive) { | |
1680 | case sig::boolean_P: | |
1681 | value = CYCastJSValue(context, *reinterpret_cast<bool *>(data)); | |
1682 | break; | |
1683 | ||
1684 | #define CYFromFFI_(primitive, native) \ | |
1685 | case sig::primitive ## _P: \ | |
1686 | value = CYCastJSValue(context, *reinterpret_cast<native *>(data)); \ | |
1687 | break; | |
1688 | ||
1689 | CYFromFFI_(uchar, unsigned char) | |
1690 | CYFromFFI_(char, char) | |
1691 | CYFromFFI_(ushort, unsigned short) | |
1692 | CYFromFFI_(short, short) | |
1693 | CYFromFFI_(ulong, unsigned long) | |
1694 | CYFromFFI_(long, long) | |
1695 | CYFromFFI_(uint, unsigned int) | |
1696 | CYFromFFI_(int, int) | |
1697 | CYFromFFI_(ulonglong, unsigned long long) | |
1698 | CYFromFFI_(longlong, long long) | |
1699 | CYFromFFI_(float, float) | |
1700 | CYFromFFI_(double, double) | |
1701 | ||
1702 | case sig::object_P: { | |
1703 | if (id object = *reinterpret_cast<id *>(data)) { | |
1704 | value = CYCastJSValue(context, object); | |
1705 | if (initialize) | |
1706 | [object release]; | |
1707 | } else goto null; | |
1708 | } break; | |
1709 | ||
1710 | case sig::typename_P: | |
1711 | value = CYMakeInstance(context, *reinterpret_cast<Class *>(data), true); | |
1712 | break; | |
1713 | ||
1714 | case sig::selector_P: | |
1715 | if (SEL sel = *reinterpret_cast<SEL *>(data)) | |
1716 | value = CYMakeSelector(context, sel); | |
1717 | else goto null; | |
1718 | break; | |
1719 | ||
1720 | case sig::pointer_P: | |
1721 | if (void *pointer = *reinterpret_cast<void **>(data)) | |
1722 | value = CYMakePointer(context, pointer, type->data.data.type, ffi, owner); | |
1723 | else goto null; | |
1724 | break; | |
1725 | ||
1726 | case sig::string_P: | |
1727 | if (char *utf8 = *reinterpret_cast<char **>(data)) | |
1728 | value = CYCastJSValue(context, utf8); | |
1729 | else goto null; | |
1730 | break; | |
1731 | ||
1732 | case sig::struct_P: | |
1733 | value = CYMakeStruct(context, data, type, ffi, owner); | |
1734 | break; | |
1735 | ||
1736 | case sig::void_P: | |
1737 | value = CYJSUndefined(context); | |
1738 | break; | |
1739 | ||
1740 | null: | |
1741 | value = CYJSNull(context); | |
1742 | break; | |
1743 | ||
1744 | default: | |
1745 | NSLog(@"CYFromFFI(%c)\n", type->primitive); | |
1746 | _assert(false); | |
1747 | } | |
1748 | ||
1749 | return value; | |
1750 | } | |
1751 | ||
1752 | static bool CYImplements(id object, Class _class, SEL selector) { | |
1753 | // XXX: possibly use a more "awesome" check? | |
1754 | return class_getInstanceMethod(_class, selector) != NULL; | |
1755 | } | |
1756 | ||
1757 | static bool Instance_hasProperty(JSContextRef context, JSObjectRef object, JSStringRef property) { | |
1758 | Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object))); | |
1759 | id self(internal->GetValue()); | |
1760 | ||
1761 | if (JSStringIsEqualToUTF8CString(property, "$cyi")) | |
1762 | return true; | |
1763 | ||
1764 | CYPool pool; | |
1765 | NSString *name(CYCastNSString(pool, property)); | |
1766 | ||
1767 | if (CYInternal *internal = CYInternal::Get(self)) | |
1768 | if (internal->HasProperty(context, property)) | |
1769 | return true; | |
1770 | ||
1771 | CYPoolTry { | |
1772 | if ([self cy$hasProperty:name]) | |
1773 | return true; | |
1774 | } CYPoolCatch(false) | |
1775 | ||
1776 | const char *string(CYPoolCString(pool, name)); | |
1777 | Class _class(object_getClass(self)); | |
1778 | ||
1779 | if (class_getProperty(_class, string) != NULL) | |
1780 | return true; | |
1781 | ||
1782 | if (SEL sel = sel_getUid(string)) | |
1783 | if (CYImplements(self, _class, sel)) | |
1784 | return true; | |
1785 | ||
1786 | return false; | |
1787 | } | |
1788 | ||
1789 | static JSValueRef Instance_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { | |
1790 | Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object))); | |
1791 | id self(internal->GetValue()); | |
1792 | ||
1793 | if (JSStringIsEqualToUTF8CString(property, "$cyi")) | |
1794 | return Internal::Make(context, self, object); | |
1795 | ||
1796 | CYTry { | |
1797 | CYPool pool; | |
1798 | NSString *name(CYCastNSString(pool, property)); | |
1799 | ||
1800 | if (CYInternal *internal = CYInternal::Get(self)) | |
1801 | if (JSValueRef value = internal->GetProperty(context, property)) | |
1802 | return value; | |
1803 | ||
1804 | CYPoolTry { | |
1805 | if (NSObject *data = [self cy$getProperty:name]) | |
1806 | return CYCastJSValue(context, data); | |
1807 | } CYPoolCatch(NULL) | |
1808 | ||
1809 | const char *string(CYPoolCString(pool, name)); | |
1810 | Class _class(object_getClass(self)); | |
1811 | ||
1812 | if (objc_property_t property = class_getProperty(_class, string)) { | |
1813 | PropertyAttributes attributes(property); | |
1814 | SEL sel(sel_registerName(attributes.Getter())); | |
1815 | return CYSendMessage(pool, context, self, sel, 0, NULL, false, exception); | |
1816 | } | |
1817 | ||
1818 | if (SEL sel = sel_getUid(string)) | |
1819 | if (CYImplements(self, _class, sel)) | |
1820 | return CYSendMessage(pool, context, self, sel, 0, NULL, false, exception); | |
1821 | ||
1822 | return NULL; | |
1823 | } CYCatch | |
1824 | } | |
1825 | ||
1826 | static bool Instance_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) { | |
1827 | CYPool pool; | |
1828 | ||
1829 | CYTry { | |
1830 | id self(CYCastNSObject(pool, context, object)); | |
1831 | NSString *name(CYCastNSString(pool, property)); | |
1832 | NSString *data(CYCastNSObject(pool, context, value)); | |
1833 | ||
1834 | CYPoolTry { | |
1835 | if ([self cy$setProperty:name to:data]) | |
1836 | return true; | |
1837 | } CYPoolCatch(NULL) | |
1838 | ||
1839 | const char *string(CYPoolCString(pool, name)); | |
1840 | Class _class(object_getClass(self)); | |
1841 | ||
1842 | if (objc_property_t property = class_getProperty(_class, string)) { | |
1843 | PropertyAttributes attributes(property); | |
1844 | if (const char *setter = attributes.Setter()) { | |
1845 | SEL sel(sel_registerName(setter)); | |
1846 | JSValueRef arguments[1] = {value}; | |
1847 | CYSendMessage(pool, context, self, sel, 1, arguments, false, exception); | |
1848 | return true; | |
1849 | } | |
1850 | } | |
1851 | ||
1852 | size_t length(strlen(string)); | |
1853 | ||
1854 | char set[length + 5]; | |
1855 | ||
1856 | set[0] = 's'; | |
1857 | set[1] = 'e'; | |
1858 | set[2] = 't'; | |
1859 | ||
1860 | if (string[0] != '\0') { | |
1861 | set[3] = toupper(string[0]); | |
1862 | memcpy(set + 4, string + 1, length - 1); | |
1863 | } | |
1864 | ||
1865 | set[length + 3] = ':'; | |
1866 | set[length + 4] = '\0'; | |
1867 | ||
1868 | if (SEL sel = sel_getUid(set)) | |
1869 | if (CYImplements(self, _class, sel)) { | |
1870 | JSValueRef arguments[1] = {value}; | |
1871 | CYSendMessage(pool, context, self, sel, 1, arguments, false, exception); | |
1872 | } | |
1873 | ||
1874 | if (CYInternal *internal = CYInternal::Set(self)) { | |
1875 | internal->SetProperty(context, property, value); | |
1876 | return true; | |
1877 | } | |
1878 | ||
1879 | return false; | |
1880 | } CYCatch | |
1881 | } | |
1882 | ||
1883 | static bool Instance_deleteProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { | |
1884 | CYTry { | |
1885 | CYPoolTry { | |
1886 | id self(CYCastNSObject(NULL, context, object)); | |
1887 | NSString *name(CYCastNSString(NULL, property)); | |
1888 | return [self cy$deleteProperty:name]; | |
1889 | } CYPoolCatch(NULL) | |
1890 | } CYCatch | |
1891 | } | |
1892 | ||
1893 | static void Instance_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) { | |
1894 | CYPool pool; | |
1895 | NSString *self(CYCastNSObject(pool, context, object)); | |
1896 | Class _class(object_getClass(self)); | |
1897 | ||
1898 | { | |
1899 | unsigned int size; | |
1900 | objc_property_t *data(class_copyPropertyList(_class, &size)); | |
1901 | for (size_t i(0); i != size; ++i) | |
1902 | JSPropertyNameAccumulatorAddName(names, CYJSString(property_getName(data[i]))); | |
1903 | free(data); | |
1904 | } | |
1905 | } | |
1906 | ||
1907 | static JSObjectRef Instance_callAsConstructor(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { | |
1908 | CYTry { | |
1909 | Instance *data(reinterpret_cast<Instance *>(JSObjectGetPrivate(object))); | |
1910 | JSObjectRef value(Instance::Make(context, [data->GetValue() alloc], Instance::Uninitialized)); | |
1911 | return value; | |
1912 | } CYCatch | |
1913 | } | |
1914 | ||
1915 | static bool Internal_hasProperty(JSContextRef context, JSObjectRef object, JSStringRef property) { | |
1916 | Internal *internal(reinterpret_cast<Internal *>(JSObjectGetPrivate(object))); | |
1917 | CYPool pool; | |
1918 | ||
1919 | id self(internal->GetValue()); | |
1920 | const char *name(CYPoolCString(pool, property)); | |
1921 | ||
1922 | if (object_getInstanceVariable(self, name, NULL) != NULL) | |
1923 | return true; | |
1924 | ||
1925 | return false; | |
1926 | } | |
1927 | ||
1928 | static JSValueRef Internal_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { | |
1929 | Internal *internal(reinterpret_cast<Internal *>(JSObjectGetPrivate(object))); | |
1930 | CYPool pool; | |
1931 | ||
1932 | CYTry { | |
1933 | id self(internal->GetValue()); | |
1934 | const char *name(CYPoolCString(pool, property)); | |
1935 | ||
1936 | if (Ivar ivar = object_getInstanceVariable(self, name, NULL)) { | |
1937 | Type_privateData type(pool, ivar_getTypeEncoding(ivar)); | |
1938 | return CYFromFFI(context, type.type_, type.GetFFI(), reinterpret_cast<uint8_t *>(self) + ivar_getOffset(ivar)); | |
1939 | } | |
1940 | ||
1941 | return NULL; | |
1942 | } CYCatch | |
1943 | } | |
1944 | ||
1945 | static bool Internal_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) { | |
1946 | Internal *internal(reinterpret_cast<Internal *>(JSObjectGetPrivate(object))); | |
1947 | CYPool pool; | |
1948 | ||
1949 | CYTry { | |
1950 | id self(internal->GetValue()); | |
1951 | const char *name(CYPoolCString(pool, property)); | |
1952 | ||
1953 | if (Ivar ivar = object_getInstanceVariable(self, name, NULL)) { | |
1954 | Type_privateData type(pool, ivar_getTypeEncoding(ivar)); | |
1955 | CYPoolFFI(pool, context, type.type_, type.GetFFI(), reinterpret_cast<uint8_t *>(self) + ivar_getOffset(ivar), value); | |
1956 | return true; | |
1957 | } | |
1958 | ||
1959 | return false; | |
1960 | } CYCatch | |
1961 | } | |
1962 | ||
1963 | static void Internal_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) { | |
1964 | Internal *internal(reinterpret_cast<Internal *>(JSObjectGetPrivate(object))); | |
1965 | CYPool pool; | |
1966 | ||
1967 | id self(internal->GetValue()); | |
1968 | Class _class(object_getClass(self)); | |
1969 | ||
1970 | for (Class super(_class); super != NULL; super = class_getSuperclass(super)) { | |
1971 | unsigned int size; | |
1972 | Ivar *data(class_copyIvarList(super, &size)); | |
1973 | for (size_t i(0); i != size; ++i) | |
1974 | JSPropertyNameAccumulatorAddName(names, CYJSString(ivar_getName(data[i]))); | |
1975 | free(data); | |
1976 | } | |
1977 | } | |
1978 | ||
1979 | static JSValueRef Internal_callAsFunction_$cya(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { | |
1980 | Internal *internal(reinterpret_cast<Internal *>(JSObjectGetPrivate(object))); | |
1981 | return internal->owner_; | |
1982 | } | |
1983 | ||
1984 | bool Index_(apr_pool_t *pool, Struct_privateData *internal, JSStringRef property, ssize_t &index, uint8_t *&base) { | |
1985 | Type_privateData *typical(internal->type_); | |
1986 | sig::Type *type(typical->type_); | |
1987 | if (type == NULL) | |
1988 | return false; | |
1989 | ||
1990 | const char *name(CYPoolCString(pool, property)); | |
1991 | size_t length(strlen(name)); | |
1992 | double number(CYCastDouble(name, length)); | |
1993 | ||
1994 | size_t count(type->data.signature.count); | |
1995 | ||
1996 | if (std::isnan(number)) { | |
1997 | if (property == NULL) | |
1998 | return false; | |
1999 | ||
2000 | sig::Element *elements(type->data.signature.elements); | |
2001 | ||
2002 | for (size_t local(0); local != count; ++local) { | |
2003 | sig::Element *element(&elements[local]); | |
2004 | if (element->name != NULL && strcmp(name, element->name) == 0) { | |
2005 | index = local; | |
2006 | goto base; | |
2007 | } | |
2008 | } | |
2009 | ||
2010 | return false; | |
2011 | } else { | |
2012 | index = static_cast<ssize_t>(number); | |
2013 | if (index != number || index < 0 || static_cast<size_t>(index) >= count) | |
2014 | return false; | |
2015 | } | |
2016 | ||
2017 | base: | |
2018 | ffi_type **elements(typical->GetFFI()->elements); | |
2019 | ||
2020 | base = reinterpret_cast<uint8_t *>(internal->value_); | |
2021 | for (ssize_t local(0); local != index; ++local) | |
2022 | base += elements[local]->size; | |
2023 | ||
2024 | return true; | |
2025 | } | |
2026 | ||
2027 | static JSValueRef Pointer_getIndex(JSContextRef context, JSObjectRef object, size_t index, JSValueRef *exception) { | |
2028 | Pointer *internal(reinterpret_cast<Pointer *>(JSObjectGetPrivate(object))); | |
2029 | Type_privateData *typical(internal->type_); | |
2030 | ||
2031 | ffi_type *ffi(typical->GetFFI()); | |
2032 | ||
2033 | uint8_t *base(reinterpret_cast<uint8_t *>(internal->value_)); | |
2034 | base += ffi->size * index; | |
2035 | ||
2036 | JSObjectRef owner(internal->owner_ ?: object); | |
2037 | ||
2038 | CYTry { | |
2039 | return CYFromFFI(context, typical->type_, ffi, base, false, owner); | |
2040 | } CYCatch | |
2041 | } | |
2042 | ||
2043 | static JSValueRef Pointer_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { | |
2044 | CYPool pool; | |
2045 | Pointer *internal(reinterpret_cast<Pointer *>(JSObjectGetPrivate(object))); | |
2046 | Type_privateData *typical(internal->type_); | |
2047 | ||
2048 | if (typical->type_ == NULL) | |
2049 | return NULL; | |
2050 | ||
2051 | ssize_t index; | |
2052 | if (!CYGetIndex(pool, property, index)) | |
2053 | return NULL; | |
2054 | ||
2055 | return Pointer_getIndex(context, object, index, exception); | |
2056 | } | |
2057 | ||
2058 | static JSValueRef Pointer_getProperty_$cyi(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { | |
2059 | return Pointer_getIndex(context, object, 0, exception); | |
2060 | } | |
2061 | ||
2062 | static bool Pointer_setIndex(JSContextRef context, JSObjectRef object, size_t index, JSValueRef value, JSValueRef *exception) { | |
2063 | Pointer *internal(reinterpret_cast<Pointer *>(JSObjectGetPrivate(object))); | |
2064 | Type_privateData *typical(internal->type_); | |
2065 | ||
2066 | ffi_type *ffi(typical->GetFFI()); | |
2067 | ||
2068 | uint8_t *base(reinterpret_cast<uint8_t *>(internal->value_)); | |
2069 | base += ffi->size * index; | |
2070 | ||
2071 | CYTry { | |
2072 | CYPoolFFI(NULL, context, typical->type_, ffi, base, value); | |
2073 | return true; | |
2074 | } CYCatch | |
2075 | } | |
2076 | ||
2077 | static bool Pointer_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) { | |
2078 | CYPool pool; | |
2079 | Pointer *internal(reinterpret_cast<Pointer *>(JSObjectGetPrivate(object))); | |
2080 | Type_privateData *typical(internal->type_); | |
2081 | ||
2082 | if (typical->type_ == NULL) | |
2083 | return NULL; | |
2084 | ||
2085 | ssize_t index; | |
2086 | if (!CYGetIndex(pool, property, index)) | |
2087 | return NULL; | |
2088 | ||
2089 | return Pointer_setIndex(context, object, 0, value, exception); | |
2090 | } | |
2091 | ||
2092 | static bool Pointer_setProperty_$cyi(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) { | |
2093 | return Pointer_setIndex(context, object, 0, value, exception); | |
2094 | } | |
2095 | ||
2096 | static JSValueRef Struct_callAsFunction_$cya(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { | |
2097 | Struct_privateData *internal(reinterpret_cast<Struct_privateData *>(JSObjectGetPrivate(_this))); | |
2098 | Type_privateData *typical(internal->type_); | |
2099 | return CYMakePointer(context, internal->value_, typical->type_, typical->ffi_, _this); | |
2100 | } | |
2101 | ||
2102 | static JSValueRef Struct_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { | |
2103 | CYPool pool; | |
2104 | Struct_privateData *internal(reinterpret_cast<Struct_privateData *>(JSObjectGetPrivate(object))); | |
2105 | Type_privateData *typical(internal->type_); | |
2106 | ||
2107 | ssize_t index; | |
2108 | uint8_t *base; | |
2109 | ||
2110 | if (!Index_(pool, internal, property, index, base)) | |
2111 | return NULL; | |
2112 | ||
2113 | JSObjectRef owner(internal->owner_ ?: object); | |
2114 | ||
2115 | CYTry { | |
2116 | return CYFromFFI(context, typical->type_->data.signature.elements[index].type, typical->GetFFI()->elements[index], base, false, owner); | |
2117 | } CYCatch | |
2118 | } | |
2119 | ||
2120 | static bool Struct_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) { | |
2121 | CYPool pool; | |
2122 | Struct_privateData *internal(reinterpret_cast<Struct_privateData *>(JSObjectGetPrivate(object))); | |
2123 | Type_privateData *typical(internal->type_); | |
2124 | ||
2125 | ssize_t index; | |
2126 | uint8_t *base; | |
2127 | ||
2128 | if (!Index_(pool, internal, property, index, base)) | |
2129 | return false; | |
2130 | ||
2131 | CYTry { | |
2132 | CYPoolFFI(NULL, context, typical->type_->data.signature.elements[index].type, typical->GetFFI()->elements[index], base, value); | |
2133 | return true; | |
2134 | } CYCatch | |
2135 | } | |
2136 | ||
2137 | static void Struct_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) { | |
2138 | Struct_privateData *internal(reinterpret_cast<Struct_privateData *>(JSObjectGetPrivate(object))); | |
2139 | Type_privateData *typical(internal->type_); | |
2140 | sig::Type *type(typical->type_); | |
2141 | ||
2142 | if (type == NULL) | |
2143 | return; | |
2144 | ||
2145 | size_t count(type->data.signature.count); | |
2146 | sig::Element *elements(type->data.signature.elements); | |
2147 | ||
2148 | char number[32]; | |
2149 | ||
2150 | for (size_t index(0); index != count; ++index) { | |
2151 | const char *name; | |
2152 | name = elements[index].name; | |
2153 | ||
2154 | if (name == NULL) { | |
2155 | sprintf(number, "%lu", index); | |
2156 | name = number; | |
2157 | } | |
2158 | ||
2159 | JSPropertyNameAccumulatorAddName(names, CYJSString(name)); | |
2160 | } | |
2161 | } | |
2162 | ||
2163 | JSValueRef CYCallFunction(apr_pool_t *pool, JSContextRef context, size_t setups, void *setup[], size_t count, const JSValueRef arguments[], bool initialize, JSValueRef *exception, sig::Signature *signature, ffi_cif *cif, void (*function)()) { | |
2164 | CYTry { | |
2165 | if (setups + count != signature->count - 1) | |
2166 | @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"incorrect number of arguments to ffi function" userInfo:nil]; | |
2167 | ||
2168 | size_t size(setups + count); | |
2169 | void *values[size]; | |
2170 | memcpy(values, setup, sizeof(void *) * setups); | |
2171 | ||
2172 | for (size_t index(setups); index != size; ++index) { | |
2173 | sig::Element *element(&signature->elements[index + 1]); | |
2174 | ffi_type *ffi(cif->arg_types[index]); | |
2175 | // XXX: alignment? | |
2176 | values[index] = new(pool) uint8_t[ffi->size]; | |
2177 | CYPoolFFI(pool, context, element->type, ffi, values[index], arguments[index - setups]); | |
2178 | } | |
2179 | ||
2180 | uint8_t value[cif->rtype->size]; | |
2181 | ffi_call(cif, function, value, values); | |
2182 | ||
2183 | return CYFromFFI(context, signature->elements[0].type, cif->rtype, value, initialize); | |
2184 | } CYCatch | |
2185 | } | |
2186 | ||
2187 | void Closure_(ffi_cif *cif, void *result, void **arguments, void *arg) { | |
2188 | ffoData *data(reinterpret_cast<ffoData *>(arg)); | |
2189 | ||
2190 | JSContextRef context(data->context_); | |
2191 | ||
2192 | size_t count(data->cif_.nargs); | |
2193 | JSValueRef values[count]; | |
2194 | ||
2195 | for (size_t index(0); index != count; ++index) | |
2196 | values[index] = CYFromFFI(context, data->signature_.elements[1 + index].type, data->cif_.arg_types[index], arguments[index]); | |
2197 | ||
2198 | JSValueRef value(CYCallAsFunction(context, data->function_, NULL, count, values)); | |
2199 | CYPoolFFI(NULL, context, data->signature_.elements[0].type, data->cif_.rtype, result, value); | |
2200 | } | |
2201 | ||
2202 | JSObjectRef CYMakeFunctor(JSContextRef context, JSObjectRef function, const char *type) { | |
2203 | // XXX: in case of exceptions this will leak | |
2204 | ffoData *data(new ffoData(type)); | |
2205 | ||
2206 | ffi_closure *closure((ffi_closure *) _syscall(mmap( | |
2207 | NULL, sizeof(ffi_closure), | |
2208 | PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, | |
2209 | -1, 0 | |
2210 | ))); | |
2211 | ||
2212 | ffi_status status(ffi_prep_closure(closure, &data->cif_, &Closure_, data)); | |
2213 | _assert(status == FFI_OK); | |
2214 | ||
2215 | _syscall(mprotect(closure, sizeof(*closure), PROT_READ | PROT_EXEC)); | |
2216 | ||
2217 | data->value_ = closure; | |
2218 | ||
2219 | data->context_ = CYGetJSContext(); | |
2220 | data->function_ = function; | |
2221 | ||
2222 | return JSObjectMake(context, Functor_, data); | |
2223 | } | |
2224 | ||
2225 | static JSValueRef ObjectiveC_Classes_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { | |
2226 | CYTry { | |
2227 | CYPool pool; | |
2228 | NSString *name(CYCastNSString(pool, property)); | |
2229 | if (Class _class = NSClassFromString(name)) | |
2230 | return CYMakeInstance(context, _class, true); | |
2231 | return NULL; | |
2232 | } CYCatch | |
2233 | } | |
2234 | ||
2235 | static void ObjectiveC_Classes_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) { | |
2236 | size_t size(objc_getClassList(NULL, 0)); | |
2237 | Class *data(reinterpret_cast<Class *>(malloc(sizeof(Class) * size))); | |
2238 | ||
2239 | get: | |
2240 | size_t writ(objc_getClassList(data, size)); | |
2241 | if (size < writ) { | |
2242 | size = writ; | |
2243 | if (Class *copy = reinterpret_cast<Class *>(realloc(data, sizeof(Class) * writ))) { | |
2244 | data = copy; | |
2245 | goto get; | |
2246 | } else goto done; | |
2247 | } | |
2248 | ||
2249 | for (size_t i(0); i != writ; ++i) | |
2250 | JSPropertyNameAccumulatorAddName(names, CYJSString(class_getName(data[i]))); | |
2251 | ||
2252 | done: | |
2253 | free(data); | |
2254 | } | |
2255 | ||
2256 | static JSValueRef ObjectiveC_Image_Classes_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { | |
2257 | const char *internal(reinterpret_cast<const char *>(JSObjectGetPrivate(object))); | |
2258 | ||
2259 | CYTry { | |
2260 | CYPool pool; | |
2261 | const char *name(CYPoolCString(pool, property)); | |
2262 | unsigned int size; | |
2263 | const char **data(objc_copyClassNamesForImage(internal, &size)); | |
2264 | JSValueRef value; | |
2265 | for (size_t i(0); i != size; ++i) | |
2266 | if (strcmp(name, data[i]) == 0) { | |
2267 | if (Class _class = objc_getClass(name)) { | |
2268 | value = CYMakeInstance(context, _class, true); | |
2269 | goto free; | |
2270 | } else | |
2271 | break; | |
2272 | } | |
2273 | value = NULL; | |
2274 | free: | |
2275 | free(data); | |
2276 | return value; | |
2277 | } CYCatch | |
2278 | } | |
2279 | ||
2280 | static void ObjectiveC_Image_Classes_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) { | |
2281 | const char *internal(reinterpret_cast<const char *>(JSObjectGetPrivate(object))); | |
2282 | unsigned int size; | |
2283 | const char **data(objc_copyClassNamesForImage(internal, &size)); | |
2284 | for (size_t i(0); i != size; ++i) | |
2285 | JSPropertyNameAccumulatorAddName(names, CYJSString(data[i])); | |
2286 | free(data); | |
2287 | } | |
2288 | ||
2289 | static JSValueRef ObjectiveC_Images_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { | |
2290 | CYTry { | |
2291 | CYPool pool; | |
2292 | const char *name(CYPoolCString(pool, property)); | |
2293 | unsigned int size; | |
2294 | const char **data(objc_copyImageNames(&size)); | |
2295 | for (size_t i(0); i != size; ++i) | |
2296 | if (strcmp(name, data[i]) == 0) { | |
2297 | name = data[i]; | |
2298 | goto free; | |
2299 | } | |
2300 | name = NULL; | |
2301 | free: | |
2302 | free(data); | |
2303 | if (name == NULL) | |
2304 | return NULL; | |
2305 | JSObjectRef value(JSObjectMake(context, NULL, NULL)); | |
2306 | CYSetProperty(context, value, CYJSString("classes"), JSObjectMake(context, ObjectiveC_Image_Classes_, const_cast<char *>(name))); | |
2307 | return value; | |
2308 | } CYCatch | |
2309 | } | |
2310 | ||
2311 | static void ObjectiveC_Images_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) { | |
2312 | unsigned int size; | |
2313 | const char **data(objc_copyImageNames(&size)); | |
2314 | for (size_t i(0); i != size; ++i) | |
2315 | JSPropertyNameAccumulatorAddName(names, CYJSString(data[i])); | |
2316 | free(data); | |
2317 | } | |
2318 | ||
2319 | static JSValueRef ObjectiveC_Protocols_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { | |
2320 | CYTry { | |
2321 | CYPool pool; | |
2322 | NSString *name(CYCastNSString(pool, property)); | |
2323 | if (Protocol *protocol = NSProtocolFromString(name)) | |
2324 | return CYMakeInstance(context, protocol, true); | |
2325 | return NULL; | |
2326 | } CYCatch | |
2327 | } | |
2328 | ||
2329 | static void ObjectiveC_Protocols_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) { | |
2330 | unsigned int size; | |
2331 | Protocol **data(objc_copyProtocolList(&size)); | |
2332 | for (size_t i(0); i != size; ++i) | |
2333 | JSPropertyNameAccumulatorAddName(names, CYJSString(protocol_getName(data[i]))); | |
2334 | free(data); | |
2335 | } | |
2336 | ||
2337 | static JSValueRef Runtime_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { | |
2338 | if (JSStringIsEqualToUTF8CString(property, "nil")) | |
2339 | return Instance::Make(context, nil, Instance::None); | |
2340 | ||
2341 | CYTry { | |
2342 | CYPool pool; | |
2343 | NSString *name(CYCastNSString(pool, property)); | |
2344 | if (Class _class = NSClassFromString(name)) | |
2345 | return CYMakeInstance(context, _class, true); | |
2346 | if (NSMutableArray *entry = [[Bridge_ objectAtIndex:0] objectForKey:name]) | |
2347 | switch ([[entry objectAtIndex:0] intValue]) { | |
2348 | case 0: | |
2349 | return JSEvaluateScript(CYGetJSContext(), CYJSString([entry objectAtIndex:1]), NULL, NULL, 0, NULL); | |
2350 | case 1: | |
2351 | return CYMakeFunctor(context, reinterpret_cast<void (*)()>([name cy$symbol]), CYPoolCString(pool, [entry objectAtIndex:1])); | |
2352 | case 2: | |
2353 | // XXX: this is horrendously inefficient | |
2354 | sig::Signature signature; | |
2355 | sig::Parse(pool, &signature, CYPoolCString(pool, [entry objectAtIndex:1]), &Structor_); | |
2356 | ffi_cif cif; | |
2357 | sig::sig_ffi_cif(pool, &sig::ObjectiveC, &signature, &cif); | |
2358 | return CYFromFFI(context, signature.elements[0].type, cif.rtype, [name cy$symbol]); | |
2359 | } | |
2360 | return NULL; | |
2361 | } CYCatch | |
2362 | } | |
2363 | ||
2364 | bool stret(ffi_type *ffi_type) { | |
2365 | return ffi_type->type == FFI_TYPE_STRUCT && ( | |
2366 | ffi_type->size > OBJC_MAX_STRUCT_BY_VALUE || | |
2367 | struct_forward_array[ffi_type->size] != 0 | |
2368 | ); | |
2369 | } | |
2370 | ||
2371 | extern "C" { | |
2372 | int *_NSGetArgc(void); | |
2373 | char ***_NSGetArgv(void); | |
2374 | int UIApplicationMain(int argc, char *argv[], NSString *principalClassName, NSString *delegateClassName); | |
2375 | } | |
2376 | ||
2377 | static JSValueRef System_print(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { | |
2378 | CYTry { | |
2379 | NSLog(@"%s", CYCastCString(context, arguments[0])); | |
2380 | return CYJSUndefined(context); | |
2381 | } CYCatch | |
2382 | } | |
2383 | ||
2384 | JSValueRef CYSendMessage(apr_pool_t *pool, JSContextRef context, id self, SEL _cmd, size_t count, const JSValueRef arguments[], bool initialize, JSValueRef *exception) { | |
2385 | const char *type; | |
2386 | ||
2387 | Class _class(object_getClass(self)); | |
2388 | if (Method method = class_getInstanceMethod(_class, _cmd)) | |
2389 | type = method_getTypeEncoding(method); | |
2390 | else { | |
2391 | CYTry { | |
2392 | CYPoolTry { | |
2393 | NSMethodSignature *method([self methodSignatureForSelector:_cmd]); | |
2394 | if (method == nil) | |
2395 | @throw [NSException exceptionWithName:NSInvalidArgumentException reason:[NSString stringWithFormat:@"unrecognized selector %s sent to object %p", sel_getName(_cmd), self] userInfo:nil]; | |
2396 | type = CYPoolCString(pool, [method _typeString]); | |
2397 | } CYPoolCatch(NULL) | |
2398 | } CYCatch | |
2399 | } | |
2400 | ||
2401 | void *setup[2]; | |
2402 | setup[0] = &self; | |
2403 | setup[1] = &_cmd; | |
2404 | ||
2405 | sig::Signature signature; | |
2406 | sig::Parse(pool, &signature, type, &Structor_); | |
2407 | ||
2408 | ffi_cif cif; | |
2409 | sig::sig_ffi_cif(pool, &sig::ObjectiveC, &signature, &cif); | |
2410 | ||
2411 | void (*function)() = stret(cif.rtype) ? reinterpret_cast<void (*)()>(&objc_msgSend_stret) : reinterpret_cast<void (*)()>(&objc_msgSend); | |
2412 | return CYCallFunction(pool, context, 2, setup, count, arguments, initialize, exception, &signature, &cif, function); | |
2413 | } | |
2414 | ||
2415 | static JSValueRef $objc_msgSend(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { | |
2416 | CYPool pool; | |
2417 | ||
2418 | bool uninitialized; | |
2419 | ||
2420 | id self; | |
2421 | SEL _cmd; | |
2422 | ||
2423 | CYTry { | |
2424 | if (count < 2) | |
2425 | @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"too few arguments to objc_msgSend" userInfo:nil]; | |
2426 | ||
2427 | if (JSValueIsObjectOfClass(context, arguments[0], Instance_)) { | |
2428 | Instance *data(reinterpret_cast<Instance *>(JSObjectGetPrivate((JSObjectRef) arguments[0]))); | |
2429 | self = data->GetValue(); | |
2430 | uninitialized = data->IsUninitialized(); | |
2431 | if (uninitialized) | |
2432 | data->value_ = nil; | |
2433 | } else { | |
2434 | self = CYCastNSObject(pool, context, arguments[0]); | |
2435 | uninitialized = false; | |
2436 | } | |
2437 | ||
2438 | if (self == nil) | |
2439 | return CYJSNull(context); | |
2440 | ||
2441 | _cmd = CYCastSEL(context, arguments[1]); | |
2442 | } CYCatch | |
2443 | ||
2444 | return CYSendMessage(pool, context, self, _cmd, count - 2, arguments + 2, uninitialized, exception); | |
2445 | } | |
2446 | ||
2447 | MSHook(void, CYDealloc, id self, SEL sel) { | |
2448 | CYInternal *internal; | |
2449 | object_getInstanceVariable(self, "cy$internal_", reinterpret_cast<void **>(&internal)); | |
2450 | if (internal != NULL) | |
2451 | delete internal; | |
2452 | _CYDealloc(self, sel); | |
2453 | } | |
2454 | ||
2455 | MSHook(void, objc_registerClassPair, Class _class) { | |
2456 | Class super(class_getSuperclass(_class)); | |
2457 | if (super == NULL || class_getInstanceVariable(super, "cy$internal_") == NULL) { | |
2458 | class_addIvar(_class, "cy$internal_", sizeof(CYInternal *), log2(sizeof(CYInternal *)), "^{CYInternal}"); | |
2459 | MSHookMessage(_class, @selector(dealloc), MSHake(CYDealloc)); | |
2460 | } | |
2461 | ||
2462 | _objc_registerClassPair(_class); | |
2463 | } | |
2464 | ||
2465 | static JSValueRef objc_registerClassPair_(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { | |
2466 | CYTry { | |
2467 | if (count != 1) | |
2468 | @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"incorrect number of arguments to objc_registerClassPair" userInfo:nil]; | |
2469 | CYPool pool; | |
2470 | Class _class(CYCastNSObject(pool, context, arguments[0])); | |
2471 | $objc_registerClassPair(_class); | |
2472 | return CYJSUndefined(context); | |
2473 | } CYCatch | |
2474 | } | |
2475 | ||
2476 | static JSValueRef Selector_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { | |
2477 | JSValueRef setup[count + 2]; | |
2478 | setup[0] = _this; | |
2479 | setup[1] = object; | |
2480 | memcpy(setup + 2, arguments, sizeof(JSValueRef) * count); | |
2481 | return $objc_msgSend(context, NULL, NULL, count + 2, setup, exception); | |
2482 | } | |
2483 | ||
2484 | static JSValueRef Functor_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { | |
2485 | CYPool pool; | |
2486 | Functor_privateData *data(reinterpret_cast<Functor_privateData *>(JSObjectGetPrivate(object))); | |
2487 | return CYCallFunction(pool, context, 0, NULL, count, arguments, false, exception, &data->signature_, &data->cif_, reinterpret_cast<void (*)()>(data->value_)); | |
2488 | } | |
2489 | ||
2490 | JSObjectRef Selector_new(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { | |
2491 | CYTry { | |
2492 | if (count != 1) | |
2493 | @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"incorrect number of arguments to Selector constructor" userInfo:nil]; | |
2494 | const char *name(CYCastCString(context, arguments[0])); | |
2495 | return CYMakeSelector(context, sel_registerName(name)); | |
2496 | } CYCatch | |
2497 | } | |
2498 | ||
2499 | JSObjectRef Pointer_new(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { | |
2500 | CYTry { | |
2501 | if (count != 2) | |
2502 | @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"incorrect number of arguments to Functor constructor" userInfo:nil]; | |
2503 | ||
2504 | void *value(CYCastPointer<void *>(context, arguments[0])); | |
2505 | const char *type(CYCastCString(context, arguments[1])); | |
2506 | ||
2507 | CYPool pool; | |
2508 | ||
2509 | sig::Signature signature; | |
2510 | sig::Parse(pool, &signature, type, &Structor_); | |
2511 | ||
2512 | return CYMakePointer(context, value, signature.elements[0].type, NULL, NULL); | |
2513 | } CYCatch | |
2514 | } | |
2515 | ||
2516 | JSObjectRef CYMakeType(JSContextRef context, JSObjectRef object, const char *type) { | |
2517 | Type_privateData *internal(new Type_privateData(NULL, type)); | |
2518 | return JSObjectMake(context, Type_, internal); | |
2519 | } | |
2520 | ||
2521 | JSObjectRef Type_new(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { | |
2522 | CYTry { | |
2523 | if (count != 1) | |
2524 | @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"incorrect number of arguments to Type constructor" userInfo:nil]; | |
2525 | const char *type(CYCastCString(context, arguments[0])); | |
2526 | return CYMakeType(context, object, type); | |
2527 | } CYCatch | |
2528 | } | |
2529 | ||
2530 | static JSValueRef Type_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { | |
2531 | CYTry { | |
2532 | if (count != 1) | |
2533 | @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"incorrect number of arguments to type cast function" userInfo:nil]; | |
2534 | Type_privateData *internal(reinterpret_cast<Type_privateData *>(JSObjectGetPrivate(object))); | |
2535 | sig::Type *type(internal->type_); | |
2536 | ffi_type *ffi(internal->GetFFI()); | |
2537 | // XXX: alignment? | |
2538 | uint8_t value[ffi->size]; | |
2539 | CYPool pool; | |
2540 | CYPoolFFI(pool, context, type, ffi, value, arguments[0]); | |
2541 | return CYFromFFI(context, type, ffi, value); | |
2542 | } CYCatch | |
2543 | } | |
2544 | ||
2545 | static JSObjectRef Type_callAsConstructor(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { | |
2546 | CYTry { | |
2547 | if (count > 1) | |
2548 | @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"incorrect number of arguments to type cast function" userInfo:nil]; | |
2549 | Type_privateData *internal(reinterpret_cast<Type_privateData *>(JSObjectGetPrivate(object))); | |
2550 | size_t size(count == 0 ? 0 : CYCastDouble(context, arguments[0])); | |
2551 | // XXX: alignment? | |
2552 | void *value(malloc(internal->GetFFI()->size * size)); | |
2553 | return CYMakePointer(context, value, internal->type_, internal->ffi_, NULL); | |
2554 | } CYCatch | |
2555 | } | |
2556 | ||
2557 | JSObjectRef Instance_new(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { | |
2558 | CYTry { | |
2559 | if (count > 1) | |
2560 | @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"incorrect number of arguments to Instance constructor" userInfo:nil]; | |
2561 | id self(count == 0 ? nil : CYCastPointer<id>(context, arguments[0])); | |
2562 | return Instance::Make(context, self, Instance::None); | |
2563 | } CYCatch | |
2564 | } | |
2565 | ||
2566 | JSObjectRef Functor_new(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { | |
2567 | CYTry { | |
2568 | if (count != 2) | |
2569 | @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"incorrect number of arguments to Functor constructor" userInfo:nil]; | |
2570 | const char *type(CYCastCString(context, arguments[1])); | |
2571 | JSValueRef exception(NULL); | |
2572 | if (JSValueIsInstanceOfConstructor(context, arguments[0], Function_, &exception)) { | |
2573 | JSObjectRef function(CYCastJSObject(context, arguments[0])); | |
2574 | return CYMakeFunctor(context, function, type); | |
2575 | } else if (exception != NULL) { | |
2576 | return NULL; | |
2577 | } else { | |
2578 | void (*function)()(CYCastPointer<void (*)()>(context, arguments[0])); | |
2579 | return CYMakeFunctor(context, function, type); | |
2580 | } | |
2581 | } CYCatch | |
2582 | } | |
2583 | ||
2584 | JSValueRef CYValue_getProperty_value(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { | |
2585 | CYValue *internal(reinterpret_cast<CYValue *>(JSObjectGetPrivate(object))); | |
2586 | return CYCastJSValue(context, reinterpret_cast<uintptr_t>(internal->value_)); | |
2587 | } | |
2588 | ||
2589 | JSValueRef Selector_getProperty_prototype(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { | |
2590 | return Function_; | |
2591 | } | |
2592 | ||
2593 | static JSValueRef CYValue_callAsFunction_$cya(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { | |
2594 | CYValue *internal(reinterpret_cast<CYValue *>(JSObjectGetPrivate(_this))); | |
2595 | Type_privateData *typical(internal->GetType()); | |
2596 | ||
2597 | sig::Type *type; | |
2598 | ffi_type *ffi; | |
2599 | ||
2600 | if (typical == NULL) { | |
2601 | type = NULL; | |
2602 | ffi = NULL; | |
2603 | } else { | |
2604 | type = typical->type_; | |
2605 | ffi = typical->ffi_; | |
2606 | } | |
2607 | ||
2608 | return CYMakePointer(context, &internal->value_, type, ffi, object); | |
2609 | } | |
2610 | ||
2611 | static JSValueRef CYValue_callAsFunction_valueOf(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { | |
2612 | CYValue *internal(reinterpret_cast<CYValue *>(JSObjectGetPrivate(_this))); | |
2613 | ||
2614 | CYTry { | |
2615 | return CYCastJSValue(context, reinterpret_cast<uintptr_t>(internal->value_)); | |
2616 | } CYCatch | |
2617 | } | |
2618 | ||
2619 | static JSValueRef CYValue_callAsFunction_toJSON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { | |
2620 | return CYValue_callAsFunction_valueOf(context, object, _this, count, arguments, exception); | |
2621 | } | |
2622 | ||
2623 | static JSValueRef CYValue_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { | |
2624 | CYValue *internal(reinterpret_cast<CYValue *>(JSObjectGetPrivate(_this))); | |
2625 | char string[32]; | |
2626 | sprintf(string, "%p", internal->value_); | |
2627 | ||
2628 | CYTry { | |
2629 | return CYCastJSValue(context, string); | |
2630 | } CYCatch | |
2631 | } | |
2632 | ||
2633 | static JSValueRef Instance_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { | |
2634 | Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(_this))); | |
2635 | ||
2636 | CYTry { | |
2637 | CYPoolTry { | |
2638 | return CYCastJSValue(context, CYJSString(CYPoolNSCYON(NULL, internal->GetValue()))); | |
2639 | } CYPoolCatch(NULL) | |
2640 | } CYCatch | |
2641 | } | |
2642 | ||
2643 | static JSValueRef Instance_callAsFunction_toJSON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { | |
2644 | Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(_this))); | |
2645 | ||
2646 | CYTry { | |
2647 | CYPoolTry { | |
2648 | NSString *key(count == 0 ? nil : CYCastNSString(NULL, CYJSString(context, arguments[0]))); | |
2649 | return CYCastJSValue(context, CYJSString([internal->GetValue() cy$toJSON:key])); | |
2650 | } CYPoolCatch(NULL) | |
2651 | } CYCatch | |
2652 | } | |
2653 | ||
2654 | static JSValueRef Instance_callAsFunction_toString(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { | |
2655 | Instance *data(reinterpret_cast<Instance *>(JSObjectGetPrivate(_this))); | |
2656 | ||
2657 | CYTry { | |
2658 | CYPoolTry { | |
2659 | return CYCastJSValue(context, CYJSString([data->GetValue() description])); | |
2660 | } CYPoolCatch(NULL) | |
2661 | } CYCatch | |
2662 | } | |
2663 | ||
2664 | static JSValueRef Selector_callAsFunction_toString(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { | |
2665 | Selector_privateData *data(reinterpret_cast<Selector_privateData *>(JSObjectGetPrivate(_this))); | |
2666 | ||
2667 | CYTry { | |
2668 | return CYCastJSValue(context, sel_getName(data->GetValue())); | |
2669 | } CYCatch | |
2670 | } | |
2671 | ||
2672 | static JSValueRef Selector_callAsFunction_toJSON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { | |
2673 | return Selector_callAsFunction_toString(context, object, _this, count, arguments, exception); | |
2674 | } | |
2675 | ||
2676 | static JSValueRef Selector_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { | |
2677 | Selector_privateData *internal(reinterpret_cast<Selector_privateData *>(JSObjectGetPrivate(_this))); | |
2678 | const char *name(sel_getName(internal->GetValue())); | |
2679 | ||
2680 | CYTry { | |
2681 | CYPoolTry { | |
2682 | return CYCastJSValue(context, CYJSString([NSString stringWithFormat:@"@selector(%s)", name])); | |
2683 | } CYPoolCatch(NULL) | |
2684 | } CYCatch | |
2685 | } | |
2686 | ||
2687 | static JSValueRef Selector_callAsFunction_type(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { | |
2688 | CYTry { | |
2689 | if (count != 1) | |
2690 | @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"incorrect number of arguments to Selector.type" userInfo:nil]; | |
2691 | CYPool pool; | |
2692 | Selector_privateData *internal(reinterpret_cast<Selector_privateData *>(JSObjectGetPrivate(_this))); | |
2693 | Class _class(CYCastNSObject(pool, context, arguments[0])); | |
2694 | SEL sel(internal->GetValue()); | |
2695 | if (Method method = class_getInstanceMethod(_class, sel)) | |
2696 | return CYCastJSValue(context, method_getTypeEncoding(method)); | |
2697 | else if (NSString *type = [[Bridge_ objectAtIndex:1] objectForKey:CYCastNSString(pool, sel_getName(sel))]) | |
2698 | return CYCastJSValue(context, CYJSString(type)); | |
2699 | else | |
2700 | return CYJSNull(context); | |
2701 | } CYCatch | |
2702 | } | |
2703 | ||
2704 | static JSValueRef Type_callAsFunction_toString(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { | |
2705 | CYTry { | |
2706 | Type_privateData *internal(reinterpret_cast<Type_privateData *>(JSObjectGetPrivate(_this))); | |
2707 | CYPool pool; | |
2708 | const char *type(sig::Unparse(pool, internal->type_)); | |
2709 | CYPoolTry { | |
2710 | return CYCastJSValue(context, CYJSString(type)); | |
2711 | } CYPoolCatch(NULL) | |
2712 | } CYCatch | |
2713 | } | |
2714 | ||
2715 | static JSValueRef Type_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { | |
2716 | CYTry { | |
2717 | Type_privateData *internal(reinterpret_cast<Type_privateData *>(JSObjectGetPrivate(_this))); | |
2718 | CYPool pool; | |
2719 | const char *type(sig::Unparse(pool, internal->type_)); | |
2720 | CYPoolTry { | |
2721 | return CYCastJSValue(context, CYJSString([NSString stringWithFormat:@"new Type(%@)", [[NSString stringWithUTF8String:type] cy$toCYON]])); | |
2722 | } CYPoolCatch(NULL) | |
2723 | } CYCatch | |
2724 | } | |
2725 | ||
2726 | static JSValueRef Type_callAsFunction_toJSON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { | |
2727 | return Type_callAsFunction_toString(context, object, _this, count, arguments, exception); | |
2728 | } | |
2729 | ||
2730 | static JSStaticValue CYValue_staticValues[2] = { | |
2731 | {"value", &CYValue_getProperty_value, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete}, | |
2732 | {NULL, NULL, NULL, 0} | |
2733 | }; | |
2734 | ||
2735 | static JSStaticValue Pointer_staticValues[2] = { | |
2736 | {"$cyi", &Pointer_getProperty_$cyi, &Pointer_setProperty_$cyi, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
2737 | {NULL, NULL, NULL, 0} | |
2738 | }; | |
2739 | ||
2740 | static JSStaticFunction Pointer_staticFunctions[4] = { | |
2741 | {"toCYON", &CYValue_callAsFunction_toCYON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
2742 | {"toJSON", &CYValue_callAsFunction_toJSON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
2743 | {"valueOf", &CYValue_callAsFunction_valueOf, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
2744 | {NULL, NULL, 0} | |
2745 | }; | |
2746 | ||
2747 | static JSStaticFunction Struct_staticFunctions[2] = { | |
2748 | {"$cya", &Struct_callAsFunction_$cya, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
2749 | {NULL, NULL, 0} | |
2750 | }; | |
2751 | ||
2752 | static JSStaticFunction Functor_staticFunctions[4] = { | |
2753 | {"toCYON", &CYValue_callAsFunction_toCYON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
2754 | {"toJSON", &CYValue_callAsFunction_toJSON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
2755 | {"valueOf", &CYValue_callAsFunction_valueOf, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
2756 | {NULL, NULL, 0} | |
2757 | }; | |
2758 | ||
2759 | /*static JSStaticValue Selector_staticValues[2] = { | |
2760 | {"prototype", &Selector_getProperty_prototype, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete}, | |
2761 | {NULL, NULL, NULL, 0} | |
2762 | };*/ | |
2763 | ||
2764 | static JSStaticValue Instance_staticValues[2] = { | |
2765 | {"value", &CYValue_getProperty_value, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete}, | |
2766 | {NULL, NULL, NULL, 0} | |
2767 | }; | |
2768 | ||
2769 | static JSStaticFunction Instance_staticFunctions[5] = { | |
2770 | {"$cya", &CYValue_callAsFunction_$cya, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
2771 | {"toCYON", &Instance_callAsFunction_toCYON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
2772 | {"toJSON", &Instance_callAsFunction_toJSON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
2773 | {"toString", &Instance_callAsFunction_toString, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
2774 | {NULL, NULL, 0} | |
2775 | }; | |
2776 | ||
2777 | static JSStaticFunction Internal_staticFunctions[2] = { | |
2778 | {"$cya", &Internal_callAsFunction_$cya, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
2779 | {NULL, NULL, 0} | |
2780 | }; | |
2781 | ||
2782 | static JSStaticFunction Selector_staticFunctions[5] = { | |
2783 | {"toCYON", &Selector_callAsFunction_toCYON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
2784 | {"toJSON", &Selector_callAsFunction_toJSON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
2785 | {"toString", &Selector_callAsFunction_toString, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
2786 | {"type", &Selector_callAsFunction_type, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
2787 | {NULL, NULL, 0} | |
2788 | }; | |
2789 | ||
2790 | static JSStaticFunction Type_staticFunctions[4] = { | |
2791 | {"toCYON", &Type_callAsFunction_toCYON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
2792 | {"toJSON", &Type_callAsFunction_toJSON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
2793 | {"toString", &Type_callAsFunction_toString, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
2794 | {NULL, NULL, 0} | |
2795 | }; | |
2796 | ||
2797 | CYDriver::CYDriver(const std::string &filename) : | |
2798 | state_(CYClear), | |
2799 | data_(NULL), | |
2800 | size_(0), | |
2801 | filename_(filename), | |
2802 | source_(NULL) | |
2803 | { | |
2804 | ScannerInit(); | |
2805 | } | |
2806 | ||
2807 | CYDriver::~CYDriver() { | |
2808 | ScannerDestroy(); | |
2809 | } | |
2810 | ||
2811 | void cy::parser::error(const cy::parser::location_type &location, const std::string &message) { | |
2812 | CYDriver::Error error; | |
2813 | error.location_ = location; | |
2814 | error.message_ = message; | |
2815 | driver.errors_.push_back(error); | |
2816 | } | |
2817 | ||
2818 | void CYSetArgs(int argc, const char *argv[]) { | |
2819 | JSContextRef context(CYGetJSContext()); | |
2820 | JSValueRef args[argc]; | |
2821 | for (int i(0); i != argc; ++i) | |
2822 | args[i] = CYCastJSValue(context, argv[i]); | |
2823 | JSValueRef exception(NULL); | |
2824 | JSObjectRef array(JSObjectMakeArray(context, argc, args, &exception)); | |
2825 | CYThrow(context, exception); | |
2826 | CYSetProperty(context, System_, CYJSString("args"), array); | |
2827 | } | |
2828 | ||
2829 | JSObjectRef CYGetGlobalObject(JSContextRef context) { | |
2830 | return JSContextGetGlobalObject(context); | |
2831 | } | |
2832 | ||
2833 | const char *CYExecute(apr_pool_t *pool, const char *code) { _pooled | |
2834 | JSContextRef context(CYGetJSContext()); | |
2835 | JSValueRef exception(NULL), result; | |
2836 | ||
2837 | try { | |
2838 | result = JSEvaluateScript(context, CYJSString(code), NULL, NULL, 0, &exception); | |
2839 | } catch (const char *error) { | |
2840 | return error; | |
2841 | } | |
2842 | ||
2843 | if (exception != NULL) { error: | |
2844 | result = exception; | |
2845 | exception = NULL; | |
2846 | } | |
2847 | ||
2848 | if (JSValueIsUndefined(context, result)) | |
2849 | return NULL; | |
2850 | ||
2851 | const char *json(CYPoolCCYON(pool, context, result, &exception)); | |
2852 | if (exception != NULL) | |
2853 | goto error; | |
2854 | ||
2855 | CYSetProperty(context, CYGetGlobalObject(context), Result_, result); | |
2856 | return json; | |
2857 | } | |
2858 | ||
2859 | bool CYRecvAll_(int socket, uint8_t *data, size_t size) { | |
2860 | while (size != 0) if (size_t writ = _syscall(recv(socket, data, size, 0))) { | |
2861 | data += writ; | |
2862 | size -= writ; | |
2863 | } else | |
2864 | return false; | |
2865 | return true; | |
2866 | } | |
2867 | ||
2868 | bool CYSendAll_(int socket, const uint8_t *data, size_t size) { | |
2869 | while (size != 0) if (size_t writ = _syscall(send(socket, data, size, 0))) { | |
2870 | data += writ; | |
2871 | size -= writ; | |
2872 | } else | |
2873 | return false; | |
2874 | return true; | |
2875 | } | |
2876 | ||
2877 | static int Socket_; | |
2878 | apr_pool_t *Pool_; | |
2879 | ||
2880 | struct CYExecute_ { | |
2881 | apr_pool_t *pool_; | |
2882 | const char * volatile data_; | |
2883 | }; | |
2884 | ||
2885 | // XXX: this is "tre lame" | |
2886 | @interface CYClient_ : NSObject { | |
2887 | } | |
2888 | ||
2889 | - (void) execute:(NSValue *)value; | |
2890 | ||
2891 | @end | |
2892 | ||
2893 | @implementation CYClient_ | |
2894 | ||
2895 | - (void) execute:(NSValue *)value { | |
2896 | CYExecute_ *execute(reinterpret_cast<CYExecute_ *>([value pointerValue])); | |
2897 | const char *data(execute->data_); | |
2898 | execute->data_ = NULL; | |
2899 | execute->data_ = CYExecute(execute->pool_, data); | |
2900 | } | |
2901 | ||
2902 | @end | |
2903 | ||
2904 | struct CYClient : | |
2905 | CYData | |
2906 | { | |
2907 | int socket_; | |
2908 | apr_thread_t *thread_; | |
2909 | ||
2910 | CYClient(int socket) : | |
2911 | socket_(socket) | |
2912 | { | |
2913 | } | |
2914 | ||
2915 | ~CYClient() { | |
2916 | _syscall(close(socket_)); | |
2917 | } | |
2918 | ||
2919 | void Handle() { _pooled | |
2920 | CYClient_ *client = [[[CYClient_ alloc] init] autorelease]; | |
2921 | ||
2922 | for (;;) { | |
2923 | size_t size; | |
2924 | if (!CYRecvAll(socket_, &size, sizeof(size))) | |
2925 | return; | |
2926 | ||
2927 | CYPool pool; | |
2928 | char *data(new(pool) char[size + 1]); | |
2929 | if (!CYRecvAll(socket_, data, size)) | |
2930 | return; | |
2931 | data[size] = '\0'; | |
2932 | ||
2933 | CYDriver driver(""); | |
2934 | cy::parser parser(driver); | |
2935 | ||
2936 | driver.data_ = data; | |
2937 | driver.size_ = size; | |
2938 | ||
2939 | const char *json; | |
2940 | if (parser.parse() != 0 || !driver.errors_.empty()) { | |
2941 | json = NULL; | |
2942 | size = _not(size_t); | |
2943 | } else { | |
2944 | std::ostringstream str; | |
2945 | driver.source_->Show(str); | |
2946 | std::string code(str.str()); | |
2947 | CYExecute_ execute = {pool, code.c_str()}; | |
2948 | [client performSelectorOnMainThread:@selector(execute:) withObject:[NSValue valueWithPointer:&execute] waitUntilDone:YES]; | |
2949 | json = execute.data_; | |
2950 | size = json == NULL ? _not(size_t) : strlen(json); | |
2951 | } | |
2952 | ||
2953 | if (!CYSendAll(socket_, &size, sizeof(size))) | |
2954 | return; | |
2955 | if (json != NULL) | |
2956 | if (!CYSendAll(socket_, json, size)) | |
2957 | return; | |
2958 | } | |
2959 | } | |
2960 | }; | |
2961 | ||
2962 | static void * APR_THREAD_FUNC OnClient(apr_thread_t *thread, void *data) { | |
2963 | CYClient *client(reinterpret_cast<CYClient *>(data)); | |
2964 | client->Handle(); | |
2965 | delete client; | |
2966 | return NULL; | |
2967 | } | |
2968 | ||
2969 | static void * APR_THREAD_FUNC Cyrver(apr_thread_t *thread, void *data) { | |
2970 | for (;;) { | |
2971 | int socket(_syscall(accept(Socket_, NULL, NULL))); | |
2972 | CYClient *client(new CYClient(socket)); | |
2973 | apr_threadattr_t *attr; | |
2974 | _aprcall(apr_threadattr_create(&attr, Pool_)); | |
2975 | _aprcall(apr_thread_create(&client->thread_, attr, &OnClient, client, client->pool_)); | |
2976 | } | |
2977 | ||
2978 | return NULL; | |
2979 | } | |
2980 | ||
2981 | void Unlink() { | |
2982 | pid_t pid(getpid()); | |
2983 | char path[104]; | |
2984 | sprintf(path, "/tmp/.s.cy.%u", pid); | |
2985 | unlink(path); | |
2986 | } | |
2987 | ||
2988 | MSInitialize { _pooled | |
2989 | _aprcall(apr_initialize()); | |
2990 | _aprcall(apr_pool_create(&Pool_, NULL)); | |
2991 | ||
2992 | Type_privateData::Object = new(Pool_) Type_privateData(Pool_, "@"); | |
2993 | Type_privateData::Selector = new(Pool_) Type_privateData(Pool_, ":"); | |
2994 | ||
2995 | Bridge_ = [[NSMutableArray arrayWithContentsOfFile:@"/usr/lib/libcycript.plist"] retain]; | |
2996 | ||
2997 | NSCFBoolean_ = objc_getClass("NSCFBoolean"); | |
2998 | NSCFType_ = objc_getClass("NSCFType"); | |
2999 | NSMessageBuilder_ = objc_getClass("NSMessageBuilder"); | |
3000 | NSZombie_ = objc_getClass("_NSZombie_"); | |
3001 | Object_ = objc_getClass("Object"); | |
3002 | ||
3003 | Socket_ = _syscall(socket(PF_UNIX, SOCK_STREAM, 0)); | |
3004 | ||
3005 | struct sockaddr_un address; | |
3006 | memset(&address, 0, sizeof(address)); | |
3007 | address.sun_family = AF_UNIX; | |
3008 | ||
3009 | pid_t pid(getpid()); | |
3010 | sprintf(address.sun_path, "/tmp/.s.cy.%u", pid); | |
3011 | ||
3012 | try { | |
3013 | _syscall(bind(Socket_, reinterpret_cast<sockaddr *>(&address), SUN_LEN(&address))); | |
3014 | atexit(&Unlink); | |
3015 | _syscall(listen(Socket_, 0)); | |
3016 | ||
3017 | apr_threadattr_t *attr; | |
3018 | _aprcall(apr_threadattr_create(&attr, Pool_)); | |
3019 | ||
3020 | apr_thread_t *thread; | |
3021 | _aprcall(apr_thread_create(&thread, attr, &Cyrver, NULL, Pool_)); | |
3022 | } catch (...) { | |
3023 | NSLog(@"failed to setup Cyrver"); | |
3024 | } | |
3025 | } | |
3026 | ||
3027 | JSGlobalContextRef CYGetJSContext() { | |
3028 | if (Context_ == NULL) { | |
3029 | JSClassDefinition definition; | |
3030 | ||
3031 | definition = kJSClassDefinitionEmpty; | |
3032 | definition.className = "Functor"; | |
3033 | definition.staticFunctions = Functor_staticFunctions; | |
3034 | definition.callAsFunction = &Functor_callAsFunction; | |
3035 | definition.finalize = &CYData::Finalize; | |
3036 | Functor_ = JSClassCreate(&definition); | |
3037 | ||
3038 | definition = kJSClassDefinitionEmpty; | |
3039 | definition.className = "Instance"; | |
3040 | definition.staticValues = Instance_staticValues; | |
3041 | definition.staticFunctions = Instance_staticFunctions; | |
3042 | definition.hasProperty = &Instance_hasProperty; | |
3043 | definition.getProperty = &Instance_getProperty; | |
3044 | definition.setProperty = &Instance_setProperty; | |
3045 | definition.deleteProperty = &Instance_deleteProperty; | |
3046 | definition.getPropertyNames = &Instance_getPropertyNames; | |
3047 | definition.callAsConstructor = &Instance_callAsConstructor; | |
3048 | definition.finalize = &CYData::Finalize; | |
3049 | Instance_ = JSClassCreate(&definition); | |
3050 | ||
3051 | definition = kJSClassDefinitionEmpty; | |
3052 | definition.className = "Internal"; | |
3053 | definition.staticFunctions = Internal_staticFunctions; | |
3054 | definition.hasProperty = &Internal_hasProperty; | |
3055 | definition.getProperty = &Internal_getProperty; | |
3056 | definition.setProperty = &Internal_setProperty; | |
3057 | definition.getPropertyNames = &Internal_getPropertyNames; | |
3058 | definition.finalize = &CYData::Finalize; | |
3059 | Internal_ = JSClassCreate(&definition); | |
3060 | ||
3061 | definition = kJSClassDefinitionEmpty; | |
3062 | definition.className = "Pointer"; | |
3063 | definition.staticValues = Pointer_staticValues; | |
3064 | definition.staticFunctions = Pointer_staticFunctions; | |
3065 | definition.getProperty = &Pointer_getProperty; | |
3066 | definition.setProperty = &Pointer_setProperty; | |
3067 | definition.finalize = &CYData::Finalize; | |
3068 | Pointer_ = JSClassCreate(&definition); | |
3069 | ||
3070 | definition = kJSClassDefinitionEmpty; | |
3071 | definition.className = "Selector"; | |
3072 | definition.staticValues = CYValue_staticValues; | |
3073 | //definition.staticValues = Selector_staticValues; | |
3074 | definition.staticFunctions = Selector_staticFunctions; | |
3075 | definition.callAsFunction = &Selector_callAsFunction; | |
3076 | definition.finalize = &CYData::Finalize; | |
3077 | Selector_ = JSClassCreate(&definition); | |
3078 | ||
3079 | definition = kJSClassDefinitionEmpty; | |
3080 | definition.className = "Struct"; | |
3081 | definition.staticFunctions = Struct_staticFunctions; | |
3082 | definition.getProperty = &Struct_getProperty; | |
3083 | definition.setProperty = &Struct_setProperty; | |
3084 | definition.getPropertyNames = &Struct_getPropertyNames; | |
3085 | definition.finalize = &CYData::Finalize; | |
3086 | Struct_ = JSClassCreate(&definition); | |
3087 | ||
3088 | definition = kJSClassDefinitionEmpty; | |
3089 | definition.className = "Type"; | |
3090 | definition.staticFunctions = Type_staticFunctions; | |
3091 | //definition.getProperty = &Type_getProperty; | |
3092 | definition.callAsFunction = &Type_callAsFunction; | |
3093 | definition.callAsConstructor = &Type_callAsConstructor; | |
3094 | definition.finalize = &CYData::Finalize; | |
3095 | Type_ = JSClassCreate(&definition); | |
3096 | ||
3097 | definition = kJSClassDefinitionEmpty; | |
3098 | definition.className = "Runtime"; | |
3099 | definition.getProperty = &Runtime_getProperty; | |
3100 | Runtime_ = JSClassCreate(&definition); | |
3101 | ||
3102 | definition = kJSClassDefinitionEmpty; | |
3103 | definition.className = "ObjectiveC::Classes"; | |
3104 | definition.getProperty = &ObjectiveC_Classes_getProperty; | |
3105 | definition.getPropertyNames = &ObjectiveC_Classes_getPropertyNames; | |
3106 | ObjectiveC_Classes_ = JSClassCreate(&definition); | |
3107 | ||
3108 | definition = kJSClassDefinitionEmpty; | |
3109 | definition.className = "ObjectiveC::Images"; | |
3110 | definition.getProperty = &ObjectiveC_Images_getProperty; | |
3111 | definition.getPropertyNames = &ObjectiveC_Images_getPropertyNames; | |
3112 | ObjectiveC_Images_ = JSClassCreate(&definition); | |
3113 | ||
3114 | definition = kJSClassDefinitionEmpty; | |
3115 | definition.className = "ObjectiveC::Image::Classes"; | |
3116 | definition.getProperty = &ObjectiveC_Image_Classes_getProperty; | |
3117 | definition.getPropertyNames = &ObjectiveC_Image_Classes_getPropertyNames; | |
3118 | definition.finalize = &CYData::Finalize; | |
3119 | ObjectiveC_Image_Classes_ = JSClassCreate(&definition); | |
3120 | ||
3121 | definition = kJSClassDefinitionEmpty; | |
3122 | definition.className = "ObjectiveC::Protocols"; | |
3123 | definition.getProperty = &ObjectiveC_Protocols_getProperty; | |
3124 | definition.getPropertyNames = &ObjectiveC_Protocols_getPropertyNames; | |
3125 | ObjectiveC_Protocols_ = JSClassCreate(&definition); | |
3126 | ||
3127 | definition = kJSClassDefinitionEmpty; | |
3128 | //definition.getProperty = &Global_getProperty; | |
3129 | JSClassRef Global(JSClassCreate(&definition)); | |
3130 | ||
3131 | JSGlobalContextRef context(JSGlobalContextCreate(Global)); | |
3132 | Context_ = context; | |
3133 | ||
3134 | JSObjectRef global(CYGetGlobalObject(context)); | |
3135 | ||
3136 | JSObjectSetPrototype(context, global, JSObjectMake(context, Runtime_, NULL)); | |
3137 | ObjectiveC_ = JSObjectMake(context, NULL, NULL); | |
3138 | CYSetProperty(context, global, CYJSString("ObjectiveC"), ObjectiveC_); | |
3139 | ||
3140 | CYSetProperty(context, ObjectiveC_, CYJSString("classes"), JSObjectMake(context, ObjectiveC_Classes_, NULL)); | |
3141 | CYSetProperty(context, ObjectiveC_, CYJSString("images"), JSObjectMake(context, ObjectiveC_Images_, NULL)); | |
3142 | CYSetProperty(context, ObjectiveC_, CYJSString("protocols"), JSObjectMake(context, ObjectiveC_Protocols_, NULL)); | |
3143 | ||
3144 | CYSetProperty(context, global, CYJSString("Functor"), JSObjectMakeConstructor(context, Functor_, &Functor_new)); | |
3145 | CYSetProperty(context, global, CYJSString("Instance"), JSObjectMakeConstructor(context, Instance_, &Instance_new)); | |
3146 | CYSetProperty(context, global, CYJSString("Pointer"), JSObjectMakeConstructor(context, Pointer_, &Pointer_new)); | |
3147 | CYSetProperty(context, global, CYJSString("Selector"), JSObjectMakeConstructor(context, Selector_, &Selector_new)); | |
3148 | CYSetProperty(context, global, CYJSString("Type"), JSObjectMakeConstructor(context, Type_, &Type_new)); | |
3149 | ||
3150 | MSHookFunction(&objc_registerClassPair, MSHake(objc_registerClassPair)); | |
3151 | ||
3152 | class_addMethod(NSCFType_, @selector(cy$toJSON:), reinterpret_cast<IMP>(&NSCFType$cy$toJSON), "@12@0:4@8"); | |
3153 | ||
3154 | CYSetProperty(context, global, CYJSString("objc_registerClassPair"), JSObjectMakeFunctionWithCallback(context, CYJSString("objc_registerClassPair"), &objc_registerClassPair_)); | |
3155 | CYSetProperty(context, global, CYJSString("objc_msgSend"), JSObjectMakeFunctionWithCallback(context, CYJSString("objc_msgSend"), &$objc_msgSend)); | |
3156 | ||
3157 | System_ = JSObjectMake(context, NULL, NULL); | |
3158 | CYSetProperty(context, global, CYJSString("system"), System_); | |
3159 | CYSetProperty(context, System_, CYJSString("args"), CYJSNull(context)); | |
3160 | //CYSetProperty(context, System_, CYJSString("global"), global); | |
3161 | ||
3162 | CYSetProperty(context, System_, CYJSString("print"), JSObjectMakeFunctionWithCallback(context, CYJSString("print"), &System_print)); | |
3163 | ||
3164 | Result_ = JSStringCreateWithUTF8CString("_"); | |
3165 | ||
3166 | length_ = JSStringCreateWithUTF8CString("length"); | |
3167 | message_ = JSStringCreateWithUTF8CString("message"); | |
3168 | name_ = JSStringCreateWithUTF8CString("name"); | |
3169 | toCYON_ = JSStringCreateWithUTF8CString("toCYON"); | |
3170 | toJSON_ = JSStringCreateWithUTF8CString("toJSON"); | |
3171 | ||
3172 | Array_ = CYCastJSObject(context, CYGetProperty(context, global, CYJSString("Array"))); | |
3173 | Function_ = CYCastJSObject(context, CYGetProperty(context, global, CYJSString("Function"))); | |
3174 | } | |
3175 | ||
3176 | return Context_; | |
3177 | } |