]>
Commit | Line | Data |
---|---|---|
1 | /* Cycript - Remote 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 | #include <substrate.h> | |
41 | ||
42 | #include <dlfcn.h> | |
43 | #include <iconv.h> | |
44 | ||
45 | #include "cycript.hpp" | |
46 | ||
47 | #include "sig/parse.hpp" | |
48 | #include "sig/ffi_type.hpp" | |
49 | ||
50 | #include "Pooling.hpp" | |
51 | ||
52 | #ifdef __OBJC__ | |
53 | #include "Struct.hpp" | |
54 | #endif | |
55 | ||
56 | #ifdef __APPLE__ | |
57 | #include <CoreFoundation/CoreFoundation.h> | |
58 | #include <CoreFoundation/CFLogUtilities.h> | |
59 | #include <JavaScriptCore/JSStringRefCF.h> | |
60 | #endif | |
61 | ||
62 | #ifdef __OBJC__ | |
63 | #ifdef __APPLE__ | |
64 | #include <WebKit/WebScriptObject.h> | |
65 | #endif | |
66 | #include <Foundation/Foundation.h> | |
67 | #endif | |
68 | ||
69 | #include <sys/mman.h> | |
70 | ||
71 | #include <iostream> | |
72 | #include <ext/stdio_filebuf.h> | |
73 | #include <set> | |
74 | #include <map> | |
75 | #include <iomanip> | |
76 | #include <sstream> | |
77 | #include <cmath> | |
78 | ||
79 | #include "Parser.hpp" | |
80 | #include "Cycript.tab.hh" | |
81 | ||
82 | #undef _assert | |
83 | #undef _trace | |
84 | ||
85 | #ifdef __OBJC__ | |
86 | #define _throw(name, args...) \ | |
87 | @throw [NSException exceptionWithName:name reason:[NSString stringWithFormat:@args] userInfo:nil] | |
88 | #else | |
89 | #define _throw(name, args...) \ | |
90 | throw "_throw()" | |
91 | #endif | |
92 | ||
93 | #define _assert(test) do { \ | |
94 | if (!(test)) \ | |
95 | _throw(NSInternalInconsistencyException, "*** _assert(%s):%s(%u):%s [errno=%d]", #test, __FILE__, __LINE__, __FUNCTION__, errno); \ | |
96 | } while (false) | |
97 | ||
98 | #define _trace() do { \ | |
99 | fprintf(stderr, "_trace():%u\n", __LINE__); \ | |
100 | } while (false) | |
101 | ||
102 | #define CYTry \ | |
103 | try | |
104 | #define CYCatch \ | |
105 | catch (NSException *error) { \ | |
106 | CYThrow(context, error, exception); \ | |
107 | return NULL; \ | |
108 | } catch (...) { \ | |
109 | *exception = CYCastJSValue(context, "catch(...)"); \ | |
110 | return NULL; \ | |
111 | } | |
112 | ||
113 | #ifdef __OBJC__ | |
114 | #define CYPoolTry { \ | |
115 | id _saved(nil); \ | |
116 | NSAutoreleasePool *_pool([[NSAutoreleasePool alloc] init]); \ | |
117 | @try | |
118 | #define CYPoolCatch(value) \ | |
119 | @catch (NSException *error) { \ | |
120 | _saved = [error retain]; \ | |
121 | @throw; \ | |
122 | return value; \ | |
123 | } @finally { \ | |
124 | [_pool release]; \ | |
125 | if (_saved != nil) \ | |
126 | [_saved autorelease]; \ | |
127 | } \ | |
128 | } | |
129 | #else | |
130 | #define CYPoolTry { | |
131 | #define CYPoolCatch } | |
132 | #endif | |
133 | ||
134 | #ifndef __APPLE__ | |
135 | #define class_getSuperclass GSObjCSuper | |
136 | #define object_getClass GSObjCClass | |
137 | #endif | |
138 | ||
139 | void CYThrow(JSContextRef context, JSValueRef value); | |
140 | ||
141 | JSValueRef CYSendMessage(apr_pool_t *pool, JSContextRef context, id self, Class super, SEL _cmd, size_t count, const JSValueRef arguments[], bool initialize, JSValueRef *exception); | |
142 | ||
143 | struct CYUTF8String { | |
144 | const char *data; | |
145 | size_t size; | |
146 | ||
147 | CYUTF8String(const char *data, size_t size) : | |
148 | data(data), | |
149 | size(size) | |
150 | { | |
151 | } | |
152 | }; | |
153 | ||
154 | struct CYUTF16String { | |
155 | const uint16_t *data; | |
156 | size_t size; | |
157 | ||
158 | CYUTF16String(const uint16_t *data, size_t size) : | |
159 | data(data), | |
160 | size(size) | |
161 | { | |
162 | } | |
163 | }; | |
164 | ||
165 | /* JavaScript Properties {{{ */ | |
166 | JSValueRef CYGetProperty(JSContextRef context, JSObjectRef object, size_t index) { | |
167 | JSValueRef exception(NULL); | |
168 | JSValueRef value(JSObjectGetPropertyAtIndex(context, object, index, &exception)); | |
169 | CYThrow(context, exception); | |
170 | return value; | |
171 | } | |
172 | ||
173 | JSValueRef CYGetProperty(JSContextRef context, JSObjectRef object, JSStringRef name) { | |
174 | JSValueRef exception(NULL); | |
175 | JSValueRef value(JSObjectGetProperty(context, object, name, &exception)); | |
176 | CYThrow(context, exception); | |
177 | return value; | |
178 | } | |
179 | ||
180 | void CYSetProperty(JSContextRef context, JSObjectRef object, size_t index, JSValueRef value) { | |
181 | JSValueRef exception(NULL); | |
182 | JSObjectSetPropertyAtIndex(context, object, index, value, &exception); | |
183 | CYThrow(context, exception); | |
184 | } | |
185 | ||
186 | void CYSetProperty(JSContextRef context, JSObjectRef object, JSStringRef name, JSValueRef value) { | |
187 | JSValueRef exception(NULL); | |
188 | JSObjectSetProperty(context, object, name, value, kJSPropertyAttributeNone, &exception); | |
189 | CYThrow(context, exception); | |
190 | } | |
191 | /* }}} */ | |
192 | /* JavaScript Strings {{{ */ | |
193 | JSStringRef CYCopyJSString(const char *value) { | |
194 | return value == NULL ? NULL : JSStringCreateWithUTF8CString(value); | |
195 | } | |
196 | ||
197 | JSStringRef CYCopyJSString(JSStringRef value) { | |
198 | return value == NULL ? NULL : JSStringRetain(value); | |
199 | } | |
200 | ||
201 | JSStringRef CYCopyJSString(JSContextRef context, JSValueRef value) { | |
202 | if (JSValueIsNull(context, value)) | |
203 | return NULL; | |
204 | JSValueRef exception(NULL); | |
205 | JSStringRef string(JSValueToStringCopy(context, value, &exception)); | |
206 | CYThrow(context, exception); | |
207 | return string; | |
208 | } | |
209 | ||
210 | class CYJSString { | |
211 | private: | |
212 | JSStringRef string_; | |
213 | ||
214 | void Clear_() { | |
215 | if (string_ != NULL) | |
216 | JSStringRelease(string_); | |
217 | } | |
218 | ||
219 | public: | |
220 | CYJSString(const CYJSString &rhs) : | |
221 | string_(CYCopyJSString(rhs.string_)) | |
222 | { | |
223 | } | |
224 | ||
225 | template <typename Arg0_> | |
226 | CYJSString(Arg0_ arg0) : | |
227 | string_(CYCopyJSString(arg0)) | |
228 | { | |
229 | } | |
230 | ||
231 | template <typename Arg0_, typename Arg1_> | |
232 | CYJSString(Arg0_ arg0, Arg1_ arg1) : | |
233 | string_(CYCopyJSString(arg0, arg1)) | |
234 | { | |
235 | } | |
236 | ||
237 | CYJSString &operator =(const CYJSString &rhs) { | |
238 | Clear_(); | |
239 | string_ = CYCopyJSString(rhs.string_); | |
240 | return *this; | |
241 | } | |
242 | ||
243 | ~CYJSString() { | |
244 | Clear_(); | |
245 | } | |
246 | ||
247 | void Clear() { | |
248 | Clear_(); | |
249 | string_ = NULL; | |
250 | } | |
251 | ||
252 | operator JSStringRef() const { | |
253 | return string_; | |
254 | } | |
255 | }; | |
256 | /* }}} */ | |
257 | /* C Strings {{{ */ | |
258 | // XXX: this macro is unhygenic | |
259 | #define CYCastCString_(string) ({ \ | |
260 | char *utf8; \ | |
261 | if (string == NULL) \ | |
262 | utf8 = NULL; \ | |
263 | else { \ | |
264 | size_t size(JSStringGetMaximumUTF8CStringSize(string)); \ | |
265 | utf8 = reinterpret_cast<char *>(alloca(size)); \ | |
266 | JSStringGetUTF8CString(string, utf8, size); \ | |
267 | } \ | |
268 | utf8; \ | |
269 | }) | |
270 | ||
271 | // XXX: this macro is unhygenic | |
272 | #define CYCastCString(context, value) ({ \ | |
273 | char *utf8; \ | |
274 | if (value == NULL) \ | |
275 | utf8 = NULL; \ | |
276 | else if (JSStringRef string = CYCopyJSString(context, value)) { \ | |
277 | utf8 = CYCastCString_(string); \ | |
278 | JSStringRelease(string); \ | |
279 | } else \ | |
280 | utf8 = NULL; \ | |
281 | utf8; \ | |
282 | }) | |
283 | ||
284 | /* }}} */ | |
285 | ||
286 | #ifdef __OBJC__ | |
287 | /* Objective-C Pool Release {{{ */ | |
288 | apr_status_t CYPoolRelease_(void *data) { | |
289 | id object(reinterpret_cast<id>(data)); | |
290 | [object release]; | |
291 | return APR_SUCCESS; | |
292 | } | |
293 | ||
294 | id CYPoolRelease_(apr_pool_t *pool, id object) { | |
295 | if (object == nil) | |
296 | return nil; | |
297 | else if (pool == NULL) | |
298 | return [object autorelease]; | |
299 | else { | |
300 | apr_pool_cleanup_register(pool, object, &CYPoolRelease_, &apr_pool_cleanup_null); | |
301 | return object; | |
302 | } | |
303 | } | |
304 | ||
305 | template <typename Type_> | |
306 | Type_ CYPoolRelease(apr_pool_t *pool, Type_ object) { | |
307 | return (Type_) CYPoolRelease_(pool, (id) object); | |
308 | } | |
309 | /* }}} */ | |
310 | /* Objective-C Strings {{{ */ | |
311 | const char *CYPoolCString(apr_pool_t *pool, NSString *value) { | |
312 | if (pool == NULL) | |
313 | return [value UTF8String]; | |
314 | else { | |
315 | size_t size([value maximumLengthOfBytesUsingEncoding:NSUTF8StringEncoding] + 1); | |
316 | char *string(new(pool) char[size]); | |
317 | if (![value getCString:string maxLength:size encoding:NSUTF8StringEncoding]) | |
318 | _throw(NSInternalInconsistencyException, "[NSString getCString:maxLength:encoding:] == NO"); | |
319 | return string; | |
320 | } | |
321 | } | |
322 | ||
323 | JSStringRef CYCopyJSString_(NSString *value) { | |
324 | #ifdef __APPLE__ | |
325 | return JSStringCreateWithCFString(reinterpret_cast<CFStringRef>(value)); | |
326 | #else | |
327 | CYPool pool; | |
328 | return CYCopyJSString(CYPoolCString(pool, value)); | |
329 | #endif | |
330 | } | |
331 | ||
332 | JSStringRef CYCopyJSString(id value) { | |
333 | if (value == nil) | |
334 | return NULL; | |
335 | // XXX: this definition scares me; is anyone using this?! | |
336 | NSString *string([value description]); | |
337 | return CYCopyJSString_(string); | |
338 | } | |
339 | ||
340 | NSString *CYCopyNSString(const CYUTF8String &value) { | |
341 | #ifdef __APPLE__ | |
342 | return (NSString *) CFStringCreateWithBytes(kCFAllocatorDefault, reinterpret_cast<const UInt8 *>(value.data), value.size, kCFStringEncodingUTF8, true); | |
343 | #else | |
344 | return [[NSString alloc] initWithBytes:value.data length:value.size encoding:NSUTF8StringEncoding]; | |
345 | #endif | |
346 | } | |
347 | ||
348 | NSString *CYCopyNSString(JSStringRef value) { | |
349 | #ifdef __APPLE__ | |
350 | return (NSString *) JSStringCopyCFString(kCFAllocatorDefault, value); | |
351 | #else | |
352 | return CYCopyNSString(CYCastCString_(value)); | |
353 | #endif | |
354 | } | |
355 | ||
356 | NSString *CYCopyNSString(JSContextRef context, JSValueRef value) { | |
357 | return CYCopyNSString(CYJSString(context, value)); | |
358 | } | |
359 | ||
360 | NSString *CYCastNSString(apr_pool_t *pool, const CYUTF8String &value) { | |
361 | return CYPoolRelease(pool, CYCopyNSString(value)); | |
362 | } | |
363 | ||
364 | NSString *CYCastNSString(apr_pool_t *pool, SEL sel) { | |
365 | const char *name(sel_getName(sel)); | |
366 | return CYPoolRelease(pool, CYCopyNSString(CYUTF8String(name, strlen(name)))); | |
367 | } | |
368 | ||
369 | NSString *CYCastNSString(apr_pool_t *pool, JSStringRef value) { | |
370 | return CYPoolRelease(pool, CYCopyNSString(value)); | |
371 | } | |
372 | ||
373 | CYUTF8String CYCastUTF8String(NSString *value) { | |
374 | NSData *data([value dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:NO]); | |
375 | return CYUTF8String(reinterpret_cast<const char *>([data bytes]), [data length]); | |
376 | } | |
377 | /* }}} */ | |
378 | #endif | |
379 | ||
380 | /* JavaScript Stringify {{{ */ | |
381 | void CYStringify(std::ostringstream &str, const char *data, size_t size) { | |
382 | unsigned quot(0), apos(0); | |
383 | for (const char *value(data), *end(data + size); value != end; ++value) | |
384 | if (*value == '"') | |
385 | ++quot; | |
386 | else if (*value == '\'') | |
387 | ++apos; | |
388 | ||
389 | bool single(quot > apos); | |
390 | ||
391 | str << (single ? '\'' : '"'); | |
392 | ||
393 | for (const char *value(data), *end(data + size); value != end; ++value) | |
394 | switch (*value) { | |
395 | case '\\': str << "\\\\"; break; | |
396 | case '\b': str << "\\b"; break; | |
397 | case '\f': str << "\\f"; break; | |
398 | case '\n': str << "\\n"; break; | |
399 | case '\r': str << "\\r"; break; | |
400 | case '\t': str << "\\t"; break; | |
401 | case '\v': str << "\\v"; break; | |
402 | ||
403 | case '"': | |
404 | if (!single) | |
405 | str << "\\\""; | |
406 | else goto simple; | |
407 | break; | |
408 | ||
409 | case '\'': | |
410 | if (single) | |
411 | str << "\\'"; | |
412 | else goto simple; | |
413 | break; | |
414 | ||
415 | default: | |
416 | if (*value < 0x20 || *value >= 0x7f) | |
417 | str << "\\x" << std::setbase(16) << std::setw(2) << std::setfill('0') << unsigned(*value); | |
418 | else simple: | |
419 | str << *value; | |
420 | } | |
421 | ||
422 | str << (single ? '\'' : '"'); | |
423 | } | |
424 | /* }}} */ | |
425 | ||
426 | static JSGlobalContextRef Context_; | |
427 | static JSObjectRef System_; | |
428 | ||
429 | static JSClassRef Functor_; | |
430 | static JSClassRef Pointer_; | |
431 | static JSClassRef Runtime_; | |
432 | static JSClassRef Struct_; | |
433 | ||
434 | #ifdef __OBJC__ | |
435 | static JSClassRef Instance_; | |
436 | static JSClassRef Internal_; | |
437 | static JSClassRef Message_; | |
438 | static JSClassRef Messages_; | |
439 | static JSClassRef Selector_; | |
440 | static JSClassRef Super_; | |
441 | ||
442 | static JSClassRef ObjectiveC_Classes_; | |
443 | static JSClassRef ObjectiveC_Image_Classes_; | |
444 | static JSClassRef ObjectiveC_Images_; | |
445 | static JSClassRef ObjectiveC_Protocols_; | |
446 | ||
447 | static JSObjectRef Instance_prototype_; | |
448 | #endif | |
449 | ||
450 | static JSObjectRef Array_; | |
451 | static JSObjectRef Function_; | |
452 | static JSObjectRef String_; | |
453 | ||
454 | static JSStringRef Result_; | |
455 | ||
456 | static JSStringRef length_; | |
457 | static JSStringRef message_; | |
458 | static JSStringRef name_; | |
459 | static JSStringRef prototype_; | |
460 | static JSStringRef toCYON_; | |
461 | static JSStringRef toJSON_; | |
462 | ||
463 | static JSObjectRef Object_prototype_; | |
464 | ||
465 | static JSObjectRef Array_prototype_; | |
466 | static JSObjectRef Array_pop_; | |
467 | static JSObjectRef Array_push_; | |
468 | static JSObjectRef Array_splice_; | |
469 | ||
470 | #ifdef __OBJC__ | |
471 | #ifdef __APPLE__ | |
472 | static Class NSCFBoolean_; | |
473 | static Class NSCFType_; | |
474 | #endif | |
475 | ||
476 | static Class NSArray_; | |
477 | static Class NSDictionary_; | |
478 | static Class NSMessageBuilder_; | |
479 | static Class NSZombie_; | |
480 | static Class Object_; | |
481 | #endif | |
482 | ||
483 | static NSArray *Bridge_; | |
484 | ||
485 | static void Finalize(JSObjectRef object) { | |
486 | delete reinterpret_cast<CYData *>(JSObjectGetPrivate(object)); | |
487 | } | |
488 | ||
489 | class Type_privateData; | |
490 | ||
491 | struct CYValue : | |
492 | CYData | |
493 | { | |
494 | void *value_; | |
495 | ||
496 | CYValue() { | |
497 | } | |
498 | ||
499 | CYValue(const void *value) : | |
500 | value_(const_cast<void *>(value)) | |
501 | { | |
502 | } | |
503 | ||
504 | CYValue(const CYValue &rhs) : | |
505 | value_(rhs.value_) | |
506 | { | |
507 | } | |
508 | ||
509 | virtual Type_privateData *GetType() const { | |
510 | return NULL; | |
511 | } | |
512 | }; | |
513 | ||
514 | struct CYOwned : | |
515 | CYValue | |
516 | { | |
517 | private: | |
518 | JSContextRef context_; | |
519 | JSObjectRef owner_; | |
520 | ||
521 | public: | |
522 | CYOwned(void *value, JSContextRef context, JSObjectRef owner) : | |
523 | CYValue(value), | |
524 | context_(context), | |
525 | owner_(owner) | |
526 | { | |
527 | if (owner_ != NULL) | |
528 | JSValueProtect(context_, owner_); | |
529 | } | |
530 | ||
531 | virtual ~CYOwned() { | |
532 | if (owner_ != NULL) | |
533 | JSValueUnprotect(context_, owner_); | |
534 | } | |
535 | ||
536 | JSObjectRef GetOwner() const { | |
537 | return owner_; | |
538 | } | |
539 | }; | |
540 | ||
541 | #ifdef __OBJC__ | |
542 | struct Selector_privateData : | |
543 | CYValue | |
544 | { | |
545 | Selector_privateData(SEL value) : | |
546 | CYValue(value) | |
547 | { | |
548 | } | |
549 | ||
550 | SEL GetValue() const { | |
551 | return reinterpret_cast<SEL>(value_); | |
552 | } | |
553 | ||
554 | virtual Type_privateData *GetType() const; | |
555 | }; | |
556 | ||
557 | // XXX: trick this out with associated objects! | |
558 | JSValueRef CYGetClassPrototype(JSContextRef context, id self) { | |
559 | if (self == nil) | |
560 | return Instance_prototype_; | |
561 | ||
562 | // XXX: I need to think through multi-context | |
563 | typedef std::map<id, JSValueRef> CacheMap; | |
564 | static CacheMap cache_; | |
565 | ||
566 | JSValueRef &value(cache_[self]); | |
567 | if (value != NULL) | |
568 | return value; | |
569 | ||
570 | JSClassRef _class(NULL); | |
571 | JSValueRef prototype; | |
572 | ||
573 | if (self == NSArray_) | |
574 | prototype = Array_prototype_; | |
575 | else if (self == NSDictionary_) | |
576 | prototype = Object_prototype_; | |
577 | else | |
578 | prototype = CYGetClassPrototype(context, class_getSuperclass(self)); | |
579 | ||
580 | JSObjectRef object(JSObjectMake(context, _class, NULL)); | |
581 | JSObjectSetPrototype(context, object, prototype); | |
582 | ||
583 | JSValueProtect(context, object); | |
584 | value = object; | |
585 | return object; | |
586 | } | |
587 | ||
588 | struct Instance : | |
589 | CYValue | |
590 | { | |
591 | enum Flags { | |
592 | None = 0, | |
593 | Transient = (1 << 0), | |
594 | Uninitialized = (1 << 1), | |
595 | }; | |
596 | ||
597 | Flags flags_; | |
598 | ||
599 | Instance(id value, Flags flags) : | |
600 | CYValue(value), | |
601 | flags_(flags) | |
602 | { | |
603 | } | |
604 | ||
605 | virtual ~Instance() { | |
606 | if ((flags_ & Transient) == 0) | |
607 | // XXX: does this handle background threads correctly? | |
608 | // XXX: this simply does not work on the console because I'm stupid | |
609 | [GetValue() performSelector:@selector(release) withObject:nil afterDelay:0]; | |
610 | } | |
611 | ||
612 | static JSObjectRef Make(JSContextRef context, id object, Flags flags = None) { | |
613 | JSObjectRef value(JSObjectMake(context, Instance_, new Instance(object, flags))); | |
614 | JSObjectSetPrototype(context, value, CYGetClassPrototype(context, object_getClass(object))); | |
615 | return value; | |
616 | } | |
617 | ||
618 | id GetValue() const { | |
619 | return reinterpret_cast<id>(value_); | |
620 | } | |
621 | ||
622 | bool IsUninitialized() const { | |
623 | return (flags_ & Uninitialized) != 0; | |
624 | } | |
625 | ||
626 | virtual Type_privateData *GetType() const; | |
627 | }; | |
628 | ||
629 | struct Super : | |
630 | Instance | |
631 | { | |
632 | Class class_; | |
633 | ||
634 | Super(id value, Class _class) : | |
635 | Instance(value, Instance::Transient), | |
636 | class_(_class) | |
637 | { | |
638 | } | |
639 | ||
640 | static JSObjectRef Make(JSContextRef context, id object, Class _class) { | |
641 | JSObjectRef value(JSObjectMake(context, Super_, new Super(object, _class))); | |
642 | return value; | |
643 | } | |
644 | }; | |
645 | ||
646 | struct Messages : | |
647 | CYValue | |
648 | { | |
649 | Messages(Class value) : | |
650 | CYValue(value) | |
651 | { | |
652 | } | |
653 | ||
654 | static JSObjectRef Make(JSContextRef context, Class _class, bool array = false) { | |
655 | JSObjectRef value(JSObjectMake(context, Messages_, new Messages(_class))); | |
656 | if (_class == NSArray_) | |
657 | array = true; | |
658 | if (Class super = class_getSuperclass(_class)) | |
659 | JSObjectSetPrototype(context, value, Messages::Make(context, super, array)); | |
660 | /*else if (array) | |
661 | JSObjectSetPrototype(context, value, Array_prototype_);*/ | |
662 | return value; | |
663 | } | |
664 | ||
665 | Class GetValue() const { | |
666 | return reinterpret_cast<Class>(value_); | |
667 | } | |
668 | }; | |
669 | ||
670 | struct Internal : | |
671 | CYOwned | |
672 | { | |
673 | Internal(id value, JSContextRef context, JSObjectRef owner) : | |
674 | CYOwned(value, context, owner) | |
675 | { | |
676 | } | |
677 | ||
678 | static JSObjectRef Make(JSContextRef context, id object, JSObjectRef owner) { | |
679 | return JSObjectMake(context, Internal_, new Internal(object, context, owner)); | |
680 | } | |
681 | ||
682 | id GetValue() const { | |
683 | return reinterpret_cast<id>(value_); | |
684 | } | |
685 | }; | |
686 | #endif | |
687 | ||
688 | namespace sig { | |
689 | ||
690 | void Copy(apr_pool_t *pool, Type &lhs, Type &rhs); | |
691 | ||
692 | void Copy(apr_pool_t *pool, Element &lhs, Element &rhs) { | |
693 | lhs.name = apr_pstrdup(pool, rhs.name); | |
694 | if (rhs.type == NULL) | |
695 | lhs.type = NULL; | |
696 | else { | |
697 | lhs.type = new(pool) Type; | |
698 | Copy(pool, *lhs.type, *rhs.type); | |
699 | } | |
700 | lhs.offset = rhs.offset; | |
701 | } | |
702 | ||
703 | void Copy(apr_pool_t *pool, Signature &lhs, Signature &rhs) { | |
704 | size_t count(rhs.count); | |
705 | lhs.count = count; | |
706 | lhs.elements = new(pool) Element[count]; | |
707 | for (size_t index(0); index != count; ++index) | |
708 | Copy(pool, lhs.elements[index], rhs.elements[index]); | |
709 | } | |
710 | ||
711 | void Copy(apr_pool_t *pool, Type &lhs, Type &rhs) { | |
712 | lhs.primitive = rhs.primitive; | |
713 | lhs.name = apr_pstrdup(pool, rhs.name); | |
714 | lhs.flags = rhs.flags; | |
715 | ||
716 | if (sig::IsAggregate(rhs.primitive)) | |
717 | Copy(pool, lhs.data.signature, rhs.data.signature); | |
718 | else { | |
719 | sig::Type *&lht(lhs.data.data.type); | |
720 | sig::Type *&rht(rhs.data.data.type); | |
721 | ||
722 | if (rht == NULL) | |
723 | lht = NULL; | |
724 | else { | |
725 | lht = new(pool) Type; | |
726 | Copy(pool, *lht, *rht); | |
727 | } | |
728 | ||
729 | lhs.data.data.size = rhs.data.data.size; | |
730 | } | |
731 | } | |
732 | ||
733 | void Copy(apr_pool_t *pool, ffi_type &lhs, ffi_type &rhs) { | |
734 | lhs.size = rhs.size; | |
735 | lhs.alignment = rhs.alignment; | |
736 | lhs.type = rhs.type; | |
737 | if (rhs.elements == NULL) | |
738 | lhs.elements = NULL; | |
739 | else { | |
740 | size_t count(0); | |
741 | while (rhs.elements[count] != NULL) | |
742 | ++count; | |
743 | ||
744 | lhs.elements = new(pool) ffi_type *[count + 1]; | |
745 | lhs.elements[count] = NULL; | |
746 | ||
747 | for (size_t index(0); index != count; ++index) { | |
748 | // XXX: if these are libffi native then you can just take them | |
749 | ffi_type *ffi(new(pool) ffi_type); | |
750 | lhs.elements[index] = ffi; | |
751 | sig::Copy(pool, *ffi, *rhs.elements[index]); | |
752 | } | |
753 | } | |
754 | } | |
755 | ||
756 | } | |
757 | ||
758 | struct CStringMapLess : | |
759 | std::binary_function<const char *, const char *, bool> | |
760 | { | |
761 | _finline bool operator ()(const char *lhs, const char *rhs) const { | |
762 | return strcmp(lhs, rhs) < 0; | |
763 | } | |
764 | }; | |
765 | ||
766 | void Structor_(apr_pool_t *pool, const char *name, const char *types, sig::Type *&type) { | |
767 | if (name == NULL) | |
768 | return; | |
769 | ||
770 | CYPoolTry { | |
771 | if (NSMutableArray *entry = [[Bridge_ objectAtIndex:2] objectForKey:[NSString stringWithUTF8String:name]]) | |
772 | switch ([[entry objectAtIndex:0] intValue]) { | |
773 | case 0: { | |
774 | sig::Parse(pool, &type->data.signature, [[entry objectAtIndex:1] UTF8String], &Structor_); | |
775 | } break; | |
776 | ||
777 | case 1: { | |
778 | sig::Signature signature; | |
779 | sig::Parse(pool, &signature, [[entry objectAtIndex:1] UTF8String], &Structor_); | |
780 | type = signature.elements[0].type; | |
781 | } break; | |
782 | } | |
783 | } CYPoolCatch() | |
784 | } | |
785 | ||
786 | struct Type_privateData : | |
787 | CYData | |
788 | { | |
789 | #ifdef __OBJC__ | |
790 | static Type_privateData *Object; | |
791 | static Type_privateData *Selector; | |
792 | #endif | |
793 | ||
794 | static JSClassRef Class_; | |
795 | ||
796 | ffi_type *ffi_; | |
797 | sig::Type *type_; | |
798 | ||
799 | void Set(sig::Type *type) { | |
800 | type_ = new(pool_) sig::Type; | |
801 | sig::Copy(pool_, *type_, *type); | |
802 | } | |
803 | ||
804 | Type_privateData(apr_pool_t *pool, const char *type) : | |
805 | ffi_(NULL) | |
806 | { | |
807 | if (pool != NULL) | |
808 | pool_ = pool; | |
809 | ||
810 | sig::Signature signature; | |
811 | sig::Parse(pool_, &signature, type, &Structor_); | |
812 | type_ = signature.elements[0].type; | |
813 | } | |
814 | ||
815 | Type_privateData(sig::Type *type) : | |
816 | ffi_(NULL) | |
817 | { | |
818 | if (type != NULL) | |
819 | Set(type); | |
820 | } | |
821 | ||
822 | Type_privateData(sig::Type *type, ffi_type *ffi) { | |
823 | ffi_ = new(pool_) ffi_type; | |
824 | sig::Copy(pool_, *ffi_, *ffi); | |
825 | Set(type); | |
826 | } | |
827 | ||
828 | ffi_type *GetFFI() { | |
829 | if (ffi_ == NULL) { | |
830 | ffi_ = new(pool_) ffi_type; | |
831 | ||
832 | sig::Element element; | |
833 | element.name = NULL; | |
834 | element.type = type_; | |
835 | element.offset = 0; | |
836 | ||
837 | sig::Signature signature; | |
838 | signature.elements = &element; | |
839 | signature.count = 1; | |
840 | ||
841 | ffi_cif cif; | |
842 | sig::sig_ffi_cif(pool_, &sig::ObjectiveC, &signature, &cif); | |
843 | *ffi_ = *cif.rtype; | |
844 | } | |
845 | ||
846 | return ffi_; | |
847 | } | |
848 | }; | |
849 | ||
850 | JSClassRef Type_privateData::Class_; | |
851 | ||
852 | #ifdef __OBJC__ | |
853 | Type_privateData *Type_privateData::Object; | |
854 | Type_privateData *Type_privateData::Selector; | |
855 | ||
856 | Type_privateData *Instance::GetType() const { | |
857 | return Type_privateData::Object; | |
858 | } | |
859 | ||
860 | Type_privateData *Selector_privateData::GetType() const { | |
861 | return Type_privateData::Selector; | |
862 | } | |
863 | #endif | |
864 | ||
865 | struct Pointer : | |
866 | CYOwned | |
867 | { | |
868 | Type_privateData *type_; | |
869 | ||
870 | Pointer(void *value, JSContextRef context, JSObjectRef owner, sig::Type *type) : | |
871 | CYOwned(value, context, owner), | |
872 | type_(new(pool_) Type_privateData(type)) | |
873 | { | |
874 | } | |
875 | }; | |
876 | ||
877 | struct Struct_privateData : | |
878 | CYOwned | |
879 | { | |
880 | Type_privateData *type_; | |
881 | ||
882 | Struct_privateData(JSContextRef context, JSObjectRef owner) : | |
883 | CYOwned(NULL, context, owner) | |
884 | { | |
885 | } | |
886 | }; | |
887 | ||
888 | typedef std::map<const char *, Type_privateData *, CStringMapLess> TypeMap; | |
889 | static TypeMap Types_; | |
890 | ||
891 | JSObjectRef CYMakeStruct(JSContextRef context, void *data, sig::Type *type, ffi_type *ffi, JSObjectRef owner) { | |
892 | Struct_privateData *internal(new Struct_privateData(context, owner)); | |
893 | apr_pool_t *pool(internal->pool_); | |
894 | Type_privateData *typical(new(pool) Type_privateData(type, ffi)); | |
895 | internal->type_ = typical; | |
896 | ||
897 | if (owner != NULL) | |
898 | internal->value_ = data; | |
899 | else { | |
900 | size_t size(typical->GetFFI()->size); | |
901 | void *copy(apr_palloc(internal->pool_, size)); | |
902 | memcpy(copy, data, size); | |
903 | internal->value_ = copy; | |
904 | } | |
905 | ||
906 | return JSObjectMake(context, Struct_, internal); | |
907 | } | |
908 | ||
909 | struct Functor_privateData : | |
910 | CYValue | |
911 | { | |
912 | sig::Signature signature_; | |
913 | ffi_cif cif_; | |
914 | ||
915 | Functor_privateData(const char *type, void (*value)()) : | |
916 | CYValue(reinterpret_cast<void *>(value)) | |
917 | { | |
918 | sig::Parse(pool_, &signature_, type, &Structor_); | |
919 | sig::sig_ffi_cif(pool_, &sig::ObjectiveC, &signature_, &cif_); | |
920 | } | |
921 | ||
922 | void (*GetValue())() const { | |
923 | return reinterpret_cast<void (*)()>(value_); | |
924 | } | |
925 | }; | |
926 | ||
927 | struct Closure_privateData : | |
928 | Functor_privateData | |
929 | { | |
930 | JSContextRef context_; | |
931 | JSObjectRef function_; | |
932 | ||
933 | Closure_privateData(JSContextRef context, JSObjectRef function, const char *type) : | |
934 | Functor_privateData(type, NULL), | |
935 | context_(context), | |
936 | function_(function) | |
937 | { | |
938 | JSValueProtect(context_, function_); | |
939 | } | |
940 | ||
941 | virtual ~Closure_privateData() { | |
942 | JSValueUnprotect(context_, function_); | |
943 | } | |
944 | }; | |
945 | ||
946 | #ifdef __OBJC__ | |
947 | struct Message_privateData : | |
948 | Functor_privateData | |
949 | { | |
950 | SEL sel_; | |
951 | ||
952 | Message_privateData(SEL sel, const char *type, IMP value = NULL) : | |
953 | Functor_privateData(type, reinterpret_cast<void (*)()>(value)), | |
954 | sel_(sel) | |
955 | { | |
956 | } | |
957 | }; | |
958 | ||
959 | JSObjectRef CYMakeInstance(JSContextRef context, id object, bool transient) { | |
960 | Instance::Flags flags; | |
961 | ||
962 | if (transient) | |
963 | flags = Instance::Transient; | |
964 | else { | |
965 | flags = Instance::None; | |
966 | object = [object retain]; | |
967 | } | |
968 | ||
969 | return Instance::Make(context, object, flags); | |
970 | } | |
971 | #endif | |
972 | ||
973 | JSValueRef CYCastJSValue(JSContextRef context, bool value) { | |
974 | return JSValueMakeBoolean(context, value); | |
975 | } | |
976 | ||
977 | JSValueRef CYCastJSValue(JSContextRef context, double value) { | |
978 | return JSValueMakeNumber(context, value); | |
979 | } | |
980 | ||
981 | #define CYCastJSValue_(Type_) \ | |
982 | JSValueRef CYCastJSValue(JSContextRef context, Type_ value) { \ | |
983 | return JSValueMakeNumber(context, static_cast<double>(value)); \ | |
984 | } | |
985 | ||
986 | CYCastJSValue_(int) | |
987 | CYCastJSValue_(unsigned int) | |
988 | CYCastJSValue_(long int) | |
989 | CYCastJSValue_(long unsigned int) | |
990 | CYCastJSValue_(long long int) | |
991 | CYCastJSValue_(long long unsigned int) | |
992 | ||
993 | JSValueRef CYJSUndefined(JSContextRef context) { | |
994 | return JSValueMakeUndefined(context); | |
995 | } | |
996 | ||
997 | size_t CYGetIndex(const CYUTF8String &value) { | |
998 | if (value.data[0] != '0') { | |
999 | char *end; | |
1000 | size_t index(strtoul(value.data, &end, 10)); | |
1001 | if (value.data + value.size == end) | |
1002 | return index; | |
1003 | } else if (value.data[1] == '\0') | |
1004 | return 0; | |
1005 | return _not(size_t); | |
1006 | } | |
1007 | ||
1008 | // XXX: fix this | |
1009 | static CYUTF8String CYPoolUTF8String(apr_pool_t *pool, JSStringRef value); | |
1010 | ||
1011 | size_t CYGetIndex(apr_pool_t *pool, JSStringRef value) { | |
1012 | return CYGetIndex(CYPoolUTF8String(pool, value)); | |
1013 | } | |
1014 | ||
1015 | bool CYGetOffset(const char *value, ssize_t &index) { | |
1016 | if (value[0] != '0') { | |
1017 | char *end; | |
1018 | index = strtol(value, &end, 10); | |
1019 | if (value + strlen(value) == end) | |
1020 | return true; | |
1021 | } else if (value[1] == '\0') { | |
1022 | index = 0; | |
1023 | return true; | |
1024 | } | |
1025 | ||
1026 | return false; | |
1027 | } | |
1028 | ||
1029 | #ifdef __OBJC__ | |
1030 | size_t CYGetIndex(NSString *value) { | |
1031 | return CYGetIndex(CYCastUTF8String(value)); | |
1032 | } | |
1033 | ||
1034 | bool CYGetOffset(apr_pool_t *pool, NSString *value, ssize_t &index) { | |
1035 | return CYGetOffset(CYPoolCString(pool, value), index); | |
1036 | } | |
1037 | #endif | |
1038 | ||
1039 | #ifdef __OBJC__ | |
1040 | @interface NSMethodSignature (Cycript) | |
1041 | - (NSString *) _typeString; | |
1042 | @end | |
1043 | ||
1044 | @interface NSObject (Cycript) | |
1045 | ||
1046 | - (JSValueRef) cy$JSValueInContext:(JSContextRef)context; | |
1047 | - (JSType) cy$JSType; | |
1048 | ||
1049 | - (NSObject *) cy$toJSON:(NSString *)key; | |
1050 | - (NSString *) cy$toCYON; | |
1051 | - (NSString *) cy$toKey; | |
1052 | ||
1053 | - (bool) cy$hasProperty:(NSString *)name; | |
1054 | - (NSObject *) cy$getProperty:(NSString *)name; | |
1055 | - (bool) cy$setProperty:(NSString *)name to:(NSObject *)value; | |
1056 | - (bool) cy$deleteProperty:(NSString *)name; | |
1057 | ||
1058 | @end | |
1059 | ||
1060 | @protocol Cycript | |
1061 | - (JSValueRef) cy$JSValueInContext:(JSContextRef)context; | |
1062 | @end | |
1063 | ||
1064 | @interface NSString (Cycript) | |
1065 | - (void *) cy$symbol; | |
1066 | @end | |
1067 | #endif | |
1068 | ||
1069 | #ifdef __OBJC__ | |
1070 | NSString *CYCastNSCYON(id value) { | |
1071 | NSString *string; | |
1072 | ||
1073 | if (value == nil) | |
1074 | string = @"nil"; | |
1075 | else { | |
1076 | Class _class(object_getClass(value)); | |
1077 | SEL sel(@selector(cy$toCYON)); | |
1078 | ||
1079 | if (objc_method *toCYON = class_getInstanceMethod(_class, sel)) | |
1080 | string = reinterpret_cast<NSString *(*)(id, SEL)>(method_getImplementation(toCYON))(value, sel); | |
1081 | else if (objc_method *methodSignatureForSelector = class_getInstanceMethod(_class, @selector(methodSignatureForSelector:))) { | |
1082 | if (reinterpret_cast<NSMethodSignature *(*)(id, SEL, SEL)>(method_getImplementation(methodSignatureForSelector))(value, @selector(methodSignatureForSelector:), sel) != nil) | |
1083 | string = [value cy$toCYON]; | |
1084 | else goto fail; | |
1085 | } else fail: { | |
1086 | if (value == NSZombie_) | |
1087 | string = @"_NSZombie_"; | |
1088 | else if (_class == NSZombie_) | |
1089 | string = [NSString stringWithFormat:@"<_NSZombie_: %p>", value]; | |
1090 | // XXX: frowny /in/ the pants | |
1091 | else if (value == NSMessageBuilder_ || value == Object_) | |
1092 | string = nil; | |
1093 | else | |
1094 | string = [NSString stringWithFormat:@"%@", value]; | |
1095 | } | |
1096 | ||
1097 | // XXX: frowny pants | |
1098 | if (string == nil) | |
1099 | string = @"undefined"; | |
1100 | } | |
1101 | ||
1102 | return string; | |
1103 | } | |
1104 | #endif | |
1105 | ||
1106 | #ifdef __OBJC__ | |
1107 | #ifdef __APPLE__ | |
1108 | struct PropertyAttributes { | |
1109 | CYPool pool_; | |
1110 | ||
1111 | const char *name; | |
1112 | ||
1113 | const char *variable; | |
1114 | ||
1115 | const char *getter_; | |
1116 | const char *setter_; | |
1117 | ||
1118 | bool readonly; | |
1119 | bool copy; | |
1120 | bool retain; | |
1121 | bool nonatomic; | |
1122 | bool dynamic; | |
1123 | bool weak; | |
1124 | bool garbage; | |
1125 | ||
1126 | PropertyAttributes(objc_property_t property) : | |
1127 | variable(NULL), | |
1128 | getter_(NULL), | |
1129 | setter_(NULL), | |
1130 | readonly(false), | |
1131 | copy(false), | |
1132 | retain(false), | |
1133 | nonatomic(false), | |
1134 | dynamic(false), | |
1135 | weak(false), | |
1136 | garbage(false) | |
1137 | { | |
1138 | name = property_getName(property); | |
1139 | const char *attributes(property_getAttributes(property)); | |
1140 | ||
1141 | for (char *state, *token(apr_strtok(apr_pstrdup(pool_, attributes), ",", &state)); token != NULL; token = apr_strtok(NULL, ",", &state)) { | |
1142 | switch (*token) { | |
1143 | case 'R': readonly = true; break; | |
1144 | case 'C': copy = true; break; | |
1145 | case '&': retain = true; break; | |
1146 | case 'N': nonatomic = true; break; | |
1147 | case 'G': getter_ = token + 1; break; | |
1148 | case 'S': setter_ = token + 1; break; | |
1149 | case 'V': variable = token + 1; break; | |
1150 | } | |
1151 | } | |
1152 | ||
1153 | /*if (variable == NULL) { | |
1154 | variable = property_getName(property); | |
1155 | size_t size(strlen(variable)); | |
1156 | char *name(new(pool_) char[size + 2]); | |
1157 | name[0] = '_'; | |
1158 | memcpy(name + 1, variable, size); | |
1159 | name[size + 1] = '\0'; | |
1160 | variable = name; | |
1161 | }*/ | |
1162 | } | |
1163 | ||
1164 | const char *Getter() { | |
1165 | if (getter_ == NULL) | |
1166 | getter_ = apr_pstrdup(pool_, name); | |
1167 | return getter_; | |
1168 | } | |
1169 | ||
1170 | const char *Setter() { | |
1171 | if (setter_ == NULL && !readonly) { | |
1172 | size_t length(strlen(name)); | |
1173 | ||
1174 | char *temp(new(pool_) char[length + 5]); | |
1175 | temp[0] = 's'; | |
1176 | temp[1] = 'e'; | |
1177 | temp[2] = 't'; | |
1178 | ||
1179 | if (length != 0) { | |
1180 | temp[3] = toupper(name[0]); | |
1181 | memcpy(temp + 4, name + 1, length - 1); | |
1182 | } | |
1183 | ||
1184 | temp[length + 3] = ':'; | |
1185 | temp[length + 4] = '\0'; | |
1186 | setter_ = temp; | |
1187 | } | |
1188 | ||
1189 | return setter_; | |
1190 | } | |
1191 | ||
1192 | }; | |
1193 | #endif | |
1194 | #endif | |
1195 | ||
1196 | #ifdef __OBJC__ | |
1197 | #ifdef __APPLE__ | |
1198 | NSObject *NSCFType$cy$toJSON(id self, SEL sel, NSString *key) { | |
1199 | return [(NSString *) CFCopyDescription((CFTypeRef) self) autorelease]; | |
1200 | } | |
1201 | #endif | |
1202 | #endif | |
1203 | ||
1204 | #ifdef __OBJC__ | |
1205 | #ifndef __APPLE__ | |
1206 | @interface CYWebUndefined : NSObject { | |
1207 | } | |
1208 | ||
1209 | + (CYWebUndefined *) undefined; | |
1210 | ||
1211 | @end | |
1212 | ||
1213 | @implementation CYWebUndefined | |
1214 | ||
1215 | + (CYWebUndefined *) undefined { | |
1216 | static CYWebUndefined *instance_([[CYWebUndefined alloc] init]); | |
1217 | return instance_; | |
1218 | } | |
1219 | ||
1220 | @end | |
1221 | ||
1222 | #define WebUndefined CYWebUndefined | |
1223 | #endif | |
1224 | #endif | |
1225 | ||
1226 | #ifdef __OBJC__ | |
1227 | /* Bridge: NSArray {{{ */ | |
1228 | @implementation NSArray (Cycript) | |
1229 | ||
1230 | - (NSString *) cy$toCYON { | |
1231 | NSMutableString *json([[[NSMutableString alloc] init] autorelease]); | |
1232 | [json appendString:@"["]; | |
1233 | ||
1234 | bool comma(false); | |
1235 | #ifdef __APPLE__ | |
1236 | for (id object in self) { | |
1237 | #else | |
1238 | id object; | |
1239 | for (size_t index(0), count([self count]); index != count; ++index) { | |
1240 | object = [self objectAtIndex:index]; | |
1241 | #endif | |
1242 | if (comma) | |
1243 | [json appendString:@","]; | |
1244 | else | |
1245 | comma = true; | |
1246 | if (object == nil || [object cy$JSType] != kJSTypeUndefined) | |
1247 | [json appendString:CYCastNSCYON(object)]; | |
1248 | else { | |
1249 | [json appendString:@","]; | |
1250 | comma = false; | |
1251 | } | |
1252 | } | |
1253 | ||
1254 | [json appendString:@"]"]; | |
1255 | return json; | |
1256 | } | |
1257 | ||
1258 | - (bool) cy$hasProperty:(NSString *)name { | |
1259 | if ([name isEqualToString:@"length"]) | |
1260 | return true; | |
1261 | ||
1262 | size_t index(CYGetIndex(name)); | |
1263 | if (index == _not(size_t) || index >= [self count]) | |
1264 | return [super cy$hasProperty:name]; | |
1265 | else | |
1266 | return true; | |
1267 | } | |
1268 | ||
1269 | - (NSObject *) cy$getProperty:(NSString *)name { | |
1270 | if ([name isEqualToString:@"length"]) { | |
1271 | NSUInteger count([self count]); | |
1272 | #ifdef __APPLE__ | |
1273 | return [NSNumber numberWithUnsignedInteger:count]; | |
1274 | #else | |
1275 | return [NSNumber numberWithUnsignedInt:count]; | |
1276 | #endif | |
1277 | } | |
1278 | ||
1279 | size_t index(CYGetIndex(name)); | |
1280 | if (index == _not(size_t) || index >= [self count]) | |
1281 | return [super cy$getProperty:name]; | |
1282 | else | |
1283 | return [self objectAtIndex:index]; | |
1284 | } | |
1285 | ||
1286 | @end | |
1287 | /* }}} */ | |
1288 | /* Bridge: NSDictionary {{{ */ | |
1289 | @implementation NSDictionary (Cycript) | |
1290 | ||
1291 | - (NSString *) cy$toCYON { | |
1292 | NSMutableString *json([[[NSMutableString alloc] init] autorelease]); | |
1293 | [json appendString:@"{"]; | |
1294 | ||
1295 | bool comma(false); | |
1296 | #ifdef __APPLE__ | |
1297 | for (id key in self) { | |
1298 | #else | |
1299 | NSEnumerator *keys([self keyEnumerator]); | |
1300 | while (id key = [keys nextObject]) { | |
1301 | #endif | |
1302 | if (comma) | |
1303 | [json appendString:@","]; | |
1304 | else | |
1305 | comma = true; | |
1306 | [json appendString:[key cy$toKey]]; | |
1307 | [json appendString:@":"]; | |
1308 | NSObject *object([self objectForKey:key]); | |
1309 | [json appendString:CYCastNSCYON(object)]; | |
1310 | } | |
1311 | ||
1312 | [json appendString:@"}"]; | |
1313 | return json; | |
1314 | } | |
1315 | ||
1316 | - (bool) cy$hasProperty:(NSString *)name { | |
1317 | return [self objectForKey:name] != nil; | |
1318 | } | |
1319 | ||
1320 | - (NSObject *) cy$getProperty:(NSString *)name { | |
1321 | return [self objectForKey:name]; | |
1322 | } | |
1323 | ||
1324 | @end | |
1325 | /* }}} */ | |
1326 | /* Bridge: NSMutableArray {{{ */ | |
1327 | @implementation NSMutableArray (Cycript) | |
1328 | ||
1329 | - (bool) cy$setProperty:(NSString *)name to:(NSObject *)value { | |
1330 | if ([name isEqualToString:@"length"]) { | |
1331 | // XXX: is this not intelligent? | |
1332 | NSNumber *number(reinterpret_cast<NSNumber *>(value)); | |
1333 | #ifdef __APPLE__ | |
1334 | NSUInteger size([number unsignedIntegerValue]); | |
1335 | #else | |
1336 | NSUInteger size([number unsignedIntValue]); | |
1337 | #endif | |
1338 | NSUInteger count([self count]); | |
1339 | if (size < count) | |
1340 | [self removeObjectsInRange:NSMakeRange(size, count - size)]; | |
1341 | else if (size != count) { | |
1342 | WebUndefined *undefined([WebUndefined undefined]); | |
1343 | for (size_t i(count); i != size; ++i) | |
1344 | [self addObject:undefined]; | |
1345 | } | |
1346 | return true; | |
1347 | } | |
1348 | ||
1349 | size_t index(CYGetIndex(name)); | |
1350 | if (index == _not(size_t)) | |
1351 | return [super cy$setProperty:name to:value]; | |
1352 | ||
1353 | id object(value ?: [NSNull null]); | |
1354 | ||
1355 | size_t count([self count]); | |
1356 | if (index < count) | |
1357 | [self replaceObjectAtIndex:index withObject:object]; | |
1358 | else { | |
1359 | if (index != count) { | |
1360 | WebUndefined *undefined([WebUndefined undefined]); | |
1361 | for (size_t i(count); i != index; ++i) | |
1362 | [self addObject:undefined]; | |
1363 | } | |
1364 | ||
1365 | [self addObject:object]; | |
1366 | } | |
1367 | ||
1368 | return true; | |
1369 | } | |
1370 | ||
1371 | - (bool) cy$deleteProperty:(NSString *)name { | |
1372 | size_t index(CYGetIndex(name)); | |
1373 | if (index == _not(size_t) || index >= [self count]) | |
1374 | return [super cy$deleteProperty:name]; | |
1375 | [self replaceObjectAtIndex:index withObject:[WebUndefined undefined]]; | |
1376 | return true; | |
1377 | } | |
1378 | ||
1379 | @end | |
1380 | /* }}} */ | |
1381 | /* Bridge: NSMutableDictionary {{{ */ | |
1382 | @implementation NSMutableDictionary (Cycript) | |
1383 | ||
1384 | - (bool) cy$setProperty:(NSString *)name to:(NSObject *)value { | |
1385 | [self setObject:(value ?: [NSNull null]) forKey:name]; | |
1386 | return true; | |
1387 | } | |
1388 | ||
1389 | - (bool) cy$deleteProperty:(NSString *)name { | |
1390 | if ([self objectForKey:name] == nil) | |
1391 | return false; | |
1392 | else { | |
1393 | [self removeObjectForKey:name]; | |
1394 | return true; | |
1395 | } | |
1396 | } | |
1397 | ||
1398 | @end | |
1399 | /* }}} */ | |
1400 | /* Bridge: NSNumber {{{ */ | |
1401 | @implementation NSNumber (Cycript) | |
1402 | ||
1403 | - (JSType) cy$JSType { | |
1404 | #ifdef __APPLE__ | |
1405 | // XXX: this just seems stupid | |
1406 | if ([self class] == NSCFBoolean_) | |
1407 | return kJSTypeBoolean; | |
1408 | #endif | |
1409 | return kJSTypeNumber; | |
1410 | } | |
1411 | ||
1412 | - (NSObject *) cy$toJSON:(NSString *)key { | |
1413 | return self; | |
1414 | } | |
1415 | ||
1416 | - (NSString *) cy$toCYON { | |
1417 | return [self cy$JSType] != kJSTypeBoolean ? [self stringValue] : [self boolValue] ? @"true" : @"false"; | |
1418 | } | |
1419 | ||
1420 | - (JSValueRef) cy$JSValueInContext:(JSContextRef)context { | |
1421 | return [self cy$JSType] != kJSTypeBoolean ? CYCastJSValue(context, [self doubleValue]) : CYCastJSValue(context, [self boolValue]); | |
1422 | } | |
1423 | ||
1424 | @end | |
1425 | /* }}} */ | |
1426 | /* Bridge: NSNull {{{ */ | |
1427 | @implementation NSNull (Cycript) | |
1428 | ||
1429 | - (JSType) cy$JSType { | |
1430 | return kJSTypeNull; | |
1431 | } | |
1432 | ||
1433 | - (NSObject *) cy$toJSON:(NSString *)key { | |
1434 | return self; | |
1435 | } | |
1436 | ||
1437 | - (NSString *) cy$toCYON { | |
1438 | return @"null"; | |
1439 | } | |
1440 | ||
1441 | @end | |
1442 | /* }}} */ | |
1443 | /* Bridge: NSObject {{{ */ | |
1444 | @implementation NSObject (Cycript) | |
1445 | ||
1446 | - (JSValueRef) cy$JSValueInContext:(JSContextRef)context { | |
1447 | return CYMakeInstance(context, self, false); | |
1448 | } | |
1449 | ||
1450 | - (JSType) cy$JSType { | |
1451 | return kJSTypeObject; | |
1452 | } | |
1453 | ||
1454 | - (NSObject *) cy$toJSON:(NSString *)key { | |
1455 | return [self description]; | |
1456 | } | |
1457 | ||
1458 | - (NSString *) cy$toCYON { | |
1459 | return [[self cy$toJSON:@""] cy$toCYON]; | |
1460 | } | |
1461 | ||
1462 | - (NSString *) cy$toKey { | |
1463 | return [self cy$toCYON]; | |
1464 | } | |
1465 | ||
1466 | - (bool) cy$hasProperty:(NSString *)name { | |
1467 | return false; | |
1468 | } | |
1469 | ||
1470 | - (NSObject *) cy$getProperty:(NSString *)name { | |
1471 | return nil; | |
1472 | } | |
1473 | ||
1474 | - (bool) cy$setProperty:(NSString *)name to:(NSObject *)value { | |
1475 | return false; | |
1476 | } | |
1477 | ||
1478 | - (bool) cy$deleteProperty:(NSString *)name { | |
1479 | return false; | |
1480 | } | |
1481 | ||
1482 | @end | |
1483 | /* }}} */ | |
1484 | /* Bridge: NSProxy {{{ */ | |
1485 | @implementation NSProxy (Cycript) | |
1486 | ||
1487 | - (NSObject *) cy$toJSON:(NSString *)key { | |
1488 | return [self description]; | |
1489 | } | |
1490 | ||
1491 | - (NSString *) cy$toCYON { | |
1492 | return [[self cy$toJSON:@""] cy$toCYON]; | |
1493 | } | |
1494 | ||
1495 | @end | |
1496 | /* }}} */ | |
1497 | /* Bridge: NSString {{{ */ | |
1498 | @implementation NSString (Cycript) | |
1499 | ||
1500 | - (JSType) cy$JSType { | |
1501 | return kJSTypeString; | |
1502 | } | |
1503 | ||
1504 | - (NSObject *) cy$toJSON:(NSString *)key { | |
1505 | return self; | |
1506 | } | |
1507 | ||
1508 | - (NSString *) cy$toCYON { | |
1509 | std::ostringstream str; | |
1510 | CYUTF8String string(CYCastUTF8String(self)); | |
1511 | CYStringify(str, string.data, string.size); | |
1512 | std::string value(str.str()); | |
1513 | return CYCastNSString(NULL, CYUTF8String(value.c_str(), value.size())); | |
1514 | } | |
1515 | ||
1516 | - (NSString *) cy$toKey { | |
1517 | const char *value([self UTF8String]); | |
1518 | size_t size(strlen(value)); | |
1519 | ||
1520 | if (size == 0) | |
1521 | goto cyon; | |
1522 | ||
1523 | if (DigitRange_[value[0]]) { | |
1524 | size_t index(CYGetIndex(self)); | |
1525 | if (index == _not(size_t)) | |
1526 | goto cyon; | |
1527 | } else { | |
1528 | if (!WordStartRange_[value[0]]) | |
1529 | goto cyon; | |
1530 | for (size_t i(1); i != size; ++i) | |
1531 | if (!WordEndRange_[value[i]]) | |
1532 | goto cyon; | |
1533 | } | |
1534 | ||
1535 | return self; | |
1536 | ||
1537 | cyon: | |
1538 | return [self cy$toCYON]; | |
1539 | } | |
1540 | ||
1541 | - (void *) cy$symbol { | |
1542 | CYPool pool; | |
1543 | return dlsym(RTLD_DEFAULT, CYPoolCString(pool, self)); | |
1544 | } | |
1545 | ||
1546 | @end | |
1547 | /* }}} */ | |
1548 | /* Bridge: WebUndefined {{{ */ | |
1549 | @implementation WebUndefined (Cycript) | |
1550 | ||
1551 | - (JSType) cy$JSType { | |
1552 | return kJSTypeUndefined; | |
1553 | } | |
1554 | ||
1555 | - (NSObject *) cy$toJSON:(NSString *)key { | |
1556 | return self; | |
1557 | } | |
1558 | ||
1559 | - (NSString *) cy$toCYON { | |
1560 | return @"undefined"; | |
1561 | } | |
1562 | ||
1563 | - (JSValueRef) cy$JSValueInContext:(JSContextRef)context { | |
1564 | return CYJSUndefined(context); | |
1565 | } | |
1566 | ||
1567 | @end | |
1568 | /* }}} */ | |
1569 | ||
1570 | /* Bridge: CYJSObject {{{ */ | |
1571 | @interface CYJSObject : NSMutableDictionary { | |
1572 | JSObjectRef object_; | |
1573 | JSContextRef context_; | |
1574 | } | |
1575 | ||
1576 | - (id) initWithJSObject:(JSObjectRef)object inContext:(JSContextRef)context; | |
1577 | ||
1578 | - (NSObject *) cy$toJSON:(NSString *)key; | |
1579 | ||
1580 | - (NSUInteger) count; | |
1581 | - (id) objectForKey:(id)key; | |
1582 | - (NSEnumerator *) keyEnumerator; | |
1583 | - (void) setObject:(id)object forKey:(id)key; | |
1584 | - (void) removeObjectForKey:(id)key; | |
1585 | ||
1586 | @end | |
1587 | /* }}} */ | |
1588 | /* Bridge: CYJSArray {{{ */ | |
1589 | @interface CYJSArray : NSMutableArray { | |
1590 | JSObjectRef object_; | |
1591 | JSContextRef context_; | |
1592 | } | |
1593 | ||
1594 | - (id) initWithJSObject:(JSObjectRef)object inContext:(JSContextRef)context; | |
1595 | ||
1596 | - (NSUInteger) count; | |
1597 | - (id) objectAtIndex:(NSUInteger)index; | |
1598 | ||
1599 | - (void) addObject:(id)anObject; | |
1600 | - (void) insertObject:(id)anObject atIndex:(NSUInteger)index; | |
1601 | - (void) removeLastObject; | |
1602 | - (void) removeObjectAtIndex:(NSUInteger)index; | |
1603 | - (void) replaceObjectAtIndex:(NSUInteger)index withObject:(id)anObject; | |
1604 | ||
1605 | @end | |
1606 | /* }}} */ | |
1607 | #endif | |
1608 | ||
1609 | #ifdef __OBJC__ | |
1610 | NSObject *CYCastNSObject_(apr_pool_t *pool, JSContextRef context, JSObjectRef object) { | |
1611 | JSValueRef exception(NULL); | |
1612 | bool array(JSValueIsInstanceOfConstructor(context, object, Array_, &exception)); | |
1613 | CYThrow(context, exception); | |
1614 | id value(array ? [CYJSArray alloc] : [CYJSObject alloc]); | |
1615 | return CYPoolRelease(pool, [value initWithJSObject:object inContext:context]); | |
1616 | } | |
1617 | ||
1618 | NSObject *CYCastNSObject(apr_pool_t *pool, JSContextRef context, JSObjectRef object) { | |
1619 | if (!JSValueIsObjectOfClass(context, object, Instance_)) | |
1620 | return CYCastNSObject_(pool, context, object); | |
1621 | else { | |
1622 | Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object))); | |
1623 | return internal->GetValue(); | |
1624 | } | |
1625 | } | |
1626 | #endif | |
1627 | ||
1628 | double CYCastDouble(const char *value, size_t size) { | |
1629 | char *end; | |
1630 | double number(strtod(value, &end)); | |
1631 | if (end != value + size) | |
1632 | return NAN; | |
1633 | return number; | |
1634 | } | |
1635 | ||
1636 | double CYCastDouble(const char *value) { | |
1637 | return CYCastDouble(value, strlen(value)); | |
1638 | } | |
1639 | ||
1640 | double CYCastDouble(JSContextRef context, JSValueRef value) { | |
1641 | JSValueRef exception(NULL); | |
1642 | double number(JSValueToNumber(context, value, &exception)); | |
1643 | CYThrow(context, exception); | |
1644 | return number; | |
1645 | } | |
1646 | ||
1647 | #ifdef __OBJC__ | |
1648 | NSNumber *CYCopyNSNumber(JSContextRef context, JSValueRef value) { | |
1649 | return [[NSNumber alloc] initWithDouble:CYCastDouble(context, value)]; | |
1650 | } | |
1651 | #endif | |
1652 | ||
1653 | bool CYCastBool(JSContextRef context, JSValueRef value) { | |
1654 | return JSValueToBoolean(context, value); | |
1655 | } | |
1656 | ||
1657 | #ifdef __OBJC__ | |
1658 | id CYNSObject(apr_pool_t *pool, JSContextRef context, JSValueRef value, bool cast) { | |
1659 | id object; | |
1660 | bool copy; | |
1661 | ||
1662 | switch (JSType type = JSValueGetType(context, value)) { | |
1663 | case kJSTypeUndefined: | |
1664 | object = [WebUndefined undefined]; | |
1665 | copy = false; | |
1666 | break; | |
1667 | ||
1668 | case kJSTypeNull: | |
1669 | return NULL; | |
1670 | break; | |
1671 | ||
1672 | case kJSTypeBoolean: | |
1673 | #ifdef __APPLE__ | |
1674 | object = (id) (CYCastBool(context, value) ? kCFBooleanTrue : kCFBooleanFalse); | |
1675 | copy = false; | |
1676 | #else | |
1677 | object = [[NSNumber alloc] initWithBool:CYCastBool(context, value)]; | |
1678 | copy = true; | |
1679 | #endif | |
1680 | break; | |
1681 | ||
1682 | case kJSTypeNumber: | |
1683 | object = CYCopyNSNumber(context, value); | |
1684 | copy = true; | |
1685 | break; | |
1686 | ||
1687 | case kJSTypeString: | |
1688 | object = CYCopyNSString(context, value); | |
1689 | copy = true; | |
1690 | break; | |
1691 | ||
1692 | case kJSTypeObject: | |
1693 | // XXX: this might could be more efficient | |
1694 | object = CYCastNSObject(pool, context, (JSObjectRef) value); | |
1695 | copy = false; | |
1696 | break; | |
1697 | ||
1698 | default: | |
1699 | _throw(NSInternalInconsistencyException, "JSValueGetType() == 0x%x", type); | |
1700 | break; | |
1701 | } | |
1702 | ||
1703 | if (cast != copy) | |
1704 | return object; | |
1705 | else if (copy) | |
1706 | return CYPoolRelease(pool, object); | |
1707 | else | |
1708 | return [object retain]; | |
1709 | } | |
1710 | ||
1711 | NSObject *CYCastNSObject(apr_pool_t *pool, JSContextRef context, JSValueRef value) { | |
1712 | return CYNSObject(pool, context, value, true); | |
1713 | } | |
1714 | NSObject *CYCopyNSObject(apr_pool_t *pool, JSContextRef context, JSValueRef value) { | |
1715 | return CYNSObject(pool, context, value, false); | |
1716 | } | |
1717 | ||
1718 | static bool CYIsClass(id self) { | |
1719 | // XXX: this is a lame object_isClass | |
1720 | return class_getInstanceMethod(object_getClass(self), @selector(alloc)) != NULL; | |
1721 | } | |
1722 | ||
1723 | Class CYCastClass(apr_pool_t *pool, JSContextRef context, JSValueRef value) { | |
1724 | id self(CYCastNSObject(pool, context, value)); | |
1725 | if (CYIsClass(self)) | |
1726 | return (Class) self; | |
1727 | _throw(NSInvalidArgumentException, "got something that is not a Class"); | |
1728 | return NULL; | |
1729 | } | |
1730 | ||
1731 | NSArray *CYCastNSArray(JSPropertyNameArrayRef names) { | |
1732 | CYPool pool; | |
1733 | size_t size(JSPropertyNameArrayGetCount(names)); | |
1734 | NSMutableArray *array([NSMutableArray arrayWithCapacity:size]); | |
1735 | for (size_t index(0); index != size; ++index) | |
1736 | [array addObject:CYCastNSString(pool, JSPropertyNameArrayGetNameAtIndex(names, index))]; | |
1737 | return array; | |
1738 | } | |
1739 | #endif | |
1740 | ||
1741 | void CYThrow(JSContextRef context, JSValueRef value) { | |
1742 | if (value == NULL) | |
1743 | return; | |
1744 | #ifdef __OBJC__ | |
1745 | @throw CYCastNSObject(NULL, context, value); | |
1746 | #else | |
1747 | // XXX: why not? | |
1748 | throw value; | |
1749 | #endif | |
1750 | } | |
1751 | ||
1752 | JSValueRef CYJSNull(JSContextRef context) { | |
1753 | return JSValueMakeNull(context); | |
1754 | } | |
1755 | ||
1756 | JSValueRef CYCastJSValue(JSContextRef context, JSStringRef value) { | |
1757 | return value == NULL ? CYJSNull(context) : JSValueMakeString(context, value); | |
1758 | } | |
1759 | ||
1760 | JSValueRef CYCastJSValue(JSContextRef context, const char *value) { | |
1761 | return CYCastJSValue(context, CYJSString(value)); | |
1762 | } | |
1763 | ||
1764 | #ifdef __OBJC__ | |
1765 | JSValueRef CYCastJSValue(JSContextRef context, id value) { | |
1766 | if (value == nil) | |
1767 | return CYJSNull(context); | |
1768 | else if ([value respondsToSelector:@selector(cy$JSValueInContext:)]) | |
1769 | return [value cy$JSValueInContext:context]; | |
1770 | else | |
1771 | return CYMakeInstance(context, value, false); | |
1772 | } | |
1773 | #endif | |
1774 | ||
1775 | JSObjectRef CYCastJSObject(JSContextRef context, JSValueRef value) { | |
1776 | JSValueRef exception(NULL); | |
1777 | JSObjectRef object(JSValueToObject(context, value, &exception)); | |
1778 | CYThrow(context, exception); | |
1779 | return object; | |
1780 | } | |
1781 | ||
1782 | void CYThrow(JSContextRef context, id error, JSValueRef *exception) { | |
1783 | if (exception == NULL) | |
1784 | throw error; | |
1785 | *exception = CYCastJSValue(context, error); | |
1786 | } | |
1787 | ||
1788 | JSValueRef CYCallAsFunction(JSContextRef context, JSObjectRef function, JSObjectRef _this, size_t count, JSValueRef arguments[]) { | |
1789 | JSValueRef exception(NULL); | |
1790 | JSValueRef value(JSObjectCallAsFunction(context, function, _this, count, arguments, &exception)); | |
1791 | CYThrow(context, exception); | |
1792 | return value; | |
1793 | } | |
1794 | ||
1795 | bool CYIsCallable(JSContextRef context, JSValueRef value) { | |
1796 | return value != NULL && JSValueIsObject(context, value) && JSObjectIsFunction(context, (JSObjectRef) value); | |
1797 | } | |
1798 | ||
1799 | #ifdef __OBJC__ | |
1800 | @implementation CYJSObject | |
1801 | ||
1802 | - (id) initWithJSObject:(JSObjectRef)object inContext:(JSContextRef)context { | |
1803 | if ((self = [super init]) != nil) { | |
1804 | object_ = object; | |
1805 | context_ = context; | |
1806 | JSValueProtect(context_, object_); | |
1807 | } return self; | |
1808 | } | |
1809 | ||
1810 | - (void) dealloc { | |
1811 | JSValueUnprotect(context_, object_); | |
1812 | [super dealloc]; | |
1813 | } | |
1814 | ||
1815 | - (NSObject *) cy$toJSON:(NSString *)key { | |
1816 | JSValueRef toJSON(CYGetProperty(context_, object_, toJSON_)); | |
1817 | if (!CYIsCallable(context_, toJSON)) | |
1818 | return [super cy$toJSON:key]; | |
1819 | else { | |
1820 | JSValueRef arguments[1] = {CYCastJSValue(context_, key)}; | |
1821 | JSValueRef value(CYCallAsFunction(context_, (JSObjectRef) toJSON, object_, 1, arguments)); | |
1822 | // XXX: do I really want an NSNull here?! | |
1823 | return CYCastNSObject(NULL, context_, value) ?: [NSNull null]; | |
1824 | } | |
1825 | } | |
1826 | ||
1827 | - (NSString *) cy$toCYON { | |
1828 | JSValueRef toCYON(CYGetProperty(context_, object_, toCYON_)); | |
1829 | if (!CYIsCallable(context_, toCYON)) super: | |
1830 | return [super cy$toCYON]; | |
1831 | else if (JSValueRef value = CYCallAsFunction(context_, (JSObjectRef) toCYON, object_, 0, NULL)) | |
1832 | return CYCastNSString(NULL, CYJSString(context_, value)); | |
1833 | else goto super; | |
1834 | } | |
1835 | ||
1836 | - (NSUInteger) count { | |
1837 | JSPropertyNameArrayRef names(JSObjectCopyPropertyNames(context_, object_)); | |
1838 | size_t size(JSPropertyNameArrayGetCount(names)); | |
1839 | JSPropertyNameArrayRelease(names); | |
1840 | return size; | |
1841 | } | |
1842 | ||
1843 | - (id) objectForKey:(id)key { | |
1844 | JSValueRef value(CYGetProperty(context_, object_, CYJSString(key))); | |
1845 | if (JSValueIsUndefined(context_, value)) | |
1846 | return nil; | |
1847 | return CYCastNSObject(NULL, context_, value) ?: [NSNull null]; | |
1848 | } | |
1849 | ||
1850 | - (NSEnumerator *) keyEnumerator { | |
1851 | JSPropertyNameArrayRef names(JSObjectCopyPropertyNames(context_, object_)); | |
1852 | NSEnumerator *enumerator([CYCastNSArray(names) objectEnumerator]); | |
1853 | JSPropertyNameArrayRelease(names); | |
1854 | return enumerator; | |
1855 | } | |
1856 | ||
1857 | - (void) setObject:(id)object forKey:(id)key { | |
1858 | CYSetProperty(context_, object_, CYJSString(key), CYCastJSValue(context_, object)); | |
1859 | } | |
1860 | ||
1861 | - (void) removeObjectForKey:(id)key { | |
1862 | JSValueRef exception(NULL); | |
1863 | (void) JSObjectDeleteProperty(context_, object_, CYJSString(key), &exception); | |
1864 | CYThrow(context_, exception); | |
1865 | } | |
1866 | ||
1867 | @end | |
1868 | ||
1869 | @implementation CYJSArray | |
1870 | ||
1871 | - (id) initWithJSObject:(JSObjectRef)object inContext:(JSContextRef)context { | |
1872 | if ((self = [super init]) != nil) { | |
1873 | object_ = object; | |
1874 | context_ = context; | |
1875 | JSValueProtect(context_, object_); | |
1876 | } return self; | |
1877 | } | |
1878 | ||
1879 | - (void) dealloc { | |
1880 | JSValueUnprotect(context_, object_); | |
1881 | [super dealloc]; | |
1882 | } | |
1883 | ||
1884 | - (NSUInteger) count { | |
1885 | return CYCastDouble(context_, CYGetProperty(context_, object_, length_)); | |
1886 | } | |
1887 | ||
1888 | - (id) objectAtIndex:(NSUInteger)index { | |
1889 | size_t bounds([self count]); | |
1890 | if (index >= bounds) | |
1891 | @throw [NSException exceptionWithName:NSRangeException reason:[NSString stringWithFormat:@"*** -[CYJSArray objectAtIndex:]: index (%zu) beyond bounds (%zu)", index, bounds] userInfo:nil]; | |
1892 | JSValueRef exception(NULL); | |
1893 | JSValueRef value(JSObjectGetPropertyAtIndex(context_, object_, index, &exception)); | |
1894 | CYThrow(context_, exception); | |
1895 | return CYCastNSObject(NULL, context_, value) ?: [NSNull null]; | |
1896 | } | |
1897 | ||
1898 | - (void) addObject:(id)object { | |
1899 | JSValueRef exception(NULL); | |
1900 | JSValueRef arguments[1]; | |
1901 | arguments[0] = CYCastJSValue(context_, object); | |
1902 | JSObjectCallAsFunction(context_, Array_push_, object_, 1, arguments, &exception); | |
1903 | CYThrow(context_, exception); | |
1904 | } | |
1905 | ||
1906 | - (void) insertObject:(id)object atIndex:(NSUInteger)index { | |
1907 | size_t bounds([self count] + 1); | |
1908 | if (index >= bounds) | |
1909 | @throw [NSException exceptionWithName:NSRangeException reason:[NSString stringWithFormat:@"*** -[CYJSArray insertObject:atIndex:]: index (%zu) beyond bounds (%zu)", index, bounds] userInfo:nil]; | |
1910 | JSValueRef exception(NULL); | |
1911 | JSValueRef arguments[3]; | |
1912 | arguments[0] = CYCastJSValue(context_, index); | |
1913 | arguments[1] = CYCastJSValue(context_, 0); | |
1914 | arguments[2] = CYCastJSValue(context_, object); | |
1915 | JSObjectCallAsFunction(context_, Array_splice_, object_, 3, arguments, &exception); | |
1916 | CYThrow(context_, exception); | |
1917 | } | |
1918 | ||
1919 | - (void) removeLastObject { | |
1920 | JSValueRef exception(NULL); | |
1921 | JSObjectCallAsFunction(context_, Array_pop_, object_, 0, NULL, &exception); | |
1922 | CYThrow(context_, exception); | |
1923 | } | |
1924 | ||
1925 | - (void) removeObjectAtIndex:(NSUInteger)index { | |
1926 | size_t bounds([self count]); | |
1927 | if (index >= bounds) | |
1928 | @throw [NSException exceptionWithName:NSRangeException reason:[NSString stringWithFormat:@"*** -[CYJSArray removeObjectAtIndex:]: index (%zu) beyond bounds (%zu)", index, bounds] userInfo:nil]; | |
1929 | JSValueRef exception(NULL); | |
1930 | JSValueRef arguments[2]; | |
1931 | arguments[0] = CYCastJSValue(context_, index); | |
1932 | arguments[1] = CYCastJSValue(context_, 1); | |
1933 | JSObjectCallAsFunction(context_, Array_splice_, object_, 2, arguments, &exception); | |
1934 | CYThrow(context_, exception); | |
1935 | } | |
1936 | ||
1937 | - (void) replaceObjectAtIndex:(NSUInteger)index withObject:(id)object { | |
1938 | size_t bounds([self count]); | |
1939 | if (index >= bounds) | |
1940 | @throw [NSException exceptionWithName:NSRangeException reason:[NSString stringWithFormat:@"*** -[CYJSArray replaceObjectAtIndex:withObject:]: index (%zu) beyond bounds (%zu)", index, bounds] userInfo:nil]; | |
1941 | CYSetProperty(context_, object_, index, CYCastJSValue(context_, object)); | |
1942 | } | |
1943 | ||
1944 | @end | |
1945 | #endif | |
1946 | ||
1947 | NSString *CYCopyNSCYON(JSContextRef context, JSValueRef value, JSValueRef *exception) { | |
1948 | if (JSValueIsNull(context, value)) | |
1949 | return [@"null" retain]; | |
1950 | ||
1951 | CYTry { | |
1952 | CYPoolTry { | |
1953 | return [CYCastNSCYON(CYCastNSObject(NULL, context, value)) retain]; | |
1954 | } CYPoolCatch(NULL) | |
1955 | } CYCatch | |
1956 | } | |
1957 | ||
1958 | const char *CYPoolCCYON(apr_pool_t *pool, JSContextRef context, JSValueRef value, JSValueRef *exception) { | |
1959 | if (NSString *json = CYCopyNSCYON(context, value, exception)) { | |
1960 | const char *string(CYPoolCString(pool, json)); | |
1961 | [json release]; | |
1962 | return string; | |
1963 | } else return NULL; | |
1964 | } | |
1965 | ||
1966 | #ifdef __OBJC__ | |
1967 | // XXX: use objc_getAssociatedObject and objc_setAssociatedObject on 10.6 | |
1968 | struct CYInternal : | |
1969 | CYData | |
1970 | { | |
1971 | JSObjectRef object_; | |
1972 | ||
1973 | CYInternal() : | |
1974 | object_(NULL) | |
1975 | { | |
1976 | } | |
1977 | ||
1978 | ~CYInternal() { | |
1979 | // XXX: delete object_? ;( | |
1980 | } | |
1981 | ||
1982 | static CYInternal *Get(id self) { | |
1983 | CYInternal *internal(NULL); | |
1984 | if (object_getInstanceVariable(self, "cy$internal_", reinterpret_cast<void **>(&internal)) == NULL) { | |
1985 | // XXX: do something epic? ;P | |
1986 | } | |
1987 | ||
1988 | return internal; | |
1989 | } | |
1990 | ||
1991 | static CYInternal *Set(id self) { | |
1992 | CYInternal *internal(NULL); | |
1993 | if (Ivar ivar = object_getInstanceVariable(self, "cy$internal_", reinterpret_cast<void **>(&internal))) { | |
1994 | if (internal == NULL) { | |
1995 | internal = new CYInternal(); | |
1996 | object_setIvar(self, ivar, reinterpret_cast<id>(internal)); | |
1997 | } | |
1998 | } else { | |
1999 | // XXX: do something epic? ;P | |
2000 | } | |
2001 | ||
2002 | return internal; | |
2003 | } | |
2004 | ||
2005 | bool HasProperty(JSContextRef context, JSStringRef name) { | |
2006 | if (object_ == NULL) | |
2007 | return false; | |
2008 | return JSObjectHasProperty(context, object_, name); | |
2009 | } | |
2010 | ||
2011 | JSValueRef GetProperty(JSContextRef context, JSStringRef name) { | |
2012 | if (object_ == NULL) | |
2013 | return NULL; | |
2014 | return CYGetProperty(context, object_, name); | |
2015 | } | |
2016 | ||
2017 | void SetProperty(JSContextRef context, JSStringRef name, JSValueRef value) { | |
2018 | if (object_ == NULL) | |
2019 | object_ = JSObjectMake(context, NULL, NULL); | |
2020 | CYSetProperty(context, object_, name, value); | |
2021 | } | |
2022 | }; | |
2023 | #endif | |
2024 | ||
2025 | #ifdef __OBJC__ | |
2026 | static JSObjectRef CYMakeSelector(JSContextRef context, SEL sel) { | |
2027 | Selector_privateData *internal(new Selector_privateData(sel)); | |
2028 | return JSObjectMake(context, Selector_, internal); | |
2029 | } | |
2030 | #endif | |
2031 | ||
2032 | static JSObjectRef CYMakePointer(JSContextRef context, void *pointer, sig::Type *type, ffi_type *ffi, JSObjectRef owner) { | |
2033 | Pointer *internal(new Pointer(pointer, context, owner, type)); | |
2034 | return JSObjectMake(context, Pointer_, internal); | |
2035 | } | |
2036 | ||
2037 | static JSObjectRef CYMakeFunctor(JSContextRef context, void (*function)(), const char *type) { | |
2038 | Functor_privateData *internal(new Functor_privateData(type, function)); | |
2039 | return JSObjectMake(context, Functor_, internal); | |
2040 | } | |
2041 | ||
2042 | static CYUTF16String CYCastUTF16String(JSStringRef value) { | |
2043 | return CYUTF16String(JSStringGetCharactersPtr(value), JSStringGetLength(value)); | |
2044 | } | |
2045 | ||
2046 | // XXX: sometimes pool is null | |
2047 | static CYUTF8String CYPoolUTF8String(apr_pool_t *pool, JSStringRef value) { | |
2048 | CYUTF16String utf16(CYCastUTF16String(value)); | |
2049 | const char *in(reinterpret_cast<const char *>(utf16.data)); | |
2050 | ||
2051 | iconv_t conversion(_syscall(iconv_open("UTF-8", "UCS-2-INTERNAL"))); | |
2052 | ||
2053 | size_t size(JSStringGetMaximumUTF8CStringSize(value)); | |
2054 | char *out(new(pool) char[size]); | |
2055 | CYUTF8String utf8(out, size); | |
2056 | ||
2057 | size = utf16.size * 2; | |
2058 | _syscall(iconv(conversion, const_cast<char **>(&in), &size, &out, &utf8.size)); | |
2059 | ||
2060 | *out = '\0'; | |
2061 | utf8.size = out - utf8.data; | |
2062 | ||
2063 | _syscall(iconv_close(conversion)); | |
2064 | ||
2065 | return utf8; | |
2066 | } | |
2067 | ||
2068 | static const char *CYPoolCString(apr_pool_t *pool, JSStringRef value) { | |
2069 | CYUTF8String utf8(CYPoolUTF8String(pool, value)); | |
2070 | _assert(memchr(utf8.data, '\0', utf8.size) == NULL); | |
2071 | return utf8.data; | |
2072 | } | |
2073 | ||
2074 | static const char *CYPoolCString(apr_pool_t *pool, JSContextRef context, JSValueRef value) { | |
2075 | return JSValueIsNull(context, value) ? NULL : CYPoolCString(pool, CYJSString(context, value)); | |
2076 | } | |
2077 | ||
2078 | static bool CYGetOffset(apr_pool_t *pool, JSStringRef value, ssize_t &index) { | |
2079 | return CYGetOffset(CYPoolCString(pool, value), index); | |
2080 | } | |
2081 | ||
2082 | static void *CYCastPointer_(JSContextRef context, JSValueRef value) { | |
2083 | switch (JSValueGetType(context, value)) { | |
2084 | case kJSTypeNull: | |
2085 | return NULL; | |
2086 | /*case kJSTypeString: | |
2087 | return dlsym(RTLD_DEFAULT, CYCastCString(context, value)); | |
2088 | case kJSTypeObject: | |
2089 | if (JSValueIsObjectOfClass(context, value, Pointer_)) { | |
2090 | Pointer *internal(reinterpret_cast<Pointer *>(JSObjectGetPrivate((JSObjectRef) value))); | |
2091 | return internal->value_; | |
2092 | }*/ | |
2093 | default: | |
2094 | double number(CYCastDouble(context, value)); | |
2095 | if (std::isnan(number)) | |
2096 | _throw(NSInvalidArgumentException, "cannot convert value to pointer"); | |
2097 | return reinterpret_cast<void *>(static_cast<uintptr_t>(static_cast<long long>(number))); | |
2098 | } | |
2099 | } | |
2100 | ||
2101 | template <typename Type_> | |
2102 | static _finline Type_ CYCastPointer(JSContextRef context, JSValueRef value) { | |
2103 | return reinterpret_cast<Type_>(CYCastPointer_(context, value)); | |
2104 | } | |
2105 | ||
2106 | #ifdef __OBJC__ | |
2107 | static SEL CYCastSEL(JSContextRef context, JSValueRef value) { | |
2108 | if (JSValueIsObjectOfClass(context, value, Selector_)) { | |
2109 | Selector_privateData *internal(reinterpret_cast<Selector_privateData *>(JSObjectGetPrivate((JSObjectRef) value))); | |
2110 | return reinterpret_cast<SEL>(internal->value_); | |
2111 | } else | |
2112 | return CYCastPointer<SEL>(context, value); | |
2113 | } | |
2114 | #endif | |
2115 | ||
2116 | static void CYPoolFFI(apr_pool_t *pool, JSContextRef context, sig::Type *type, ffi_type *ffi, void *data, JSValueRef value) { | |
2117 | switch (type->primitive) { | |
2118 | case sig::boolean_P: | |
2119 | *reinterpret_cast<bool *>(data) = JSValueToBoolean(context, value); | |
2120 | break; | |
2121 | ||
2122 | #define CYPoolFFI_(primitive, native) \ | |
2123 | case sig::primitive ## _P: \ | |
2124 | *reinterpret_cast<native *>(data) = CYCastDouble(context, value); \ | |
2125 | break; | |
2126 | ||
2127 | CYPoolFFI_(uchar, unsigned char) | |
2128 | CYPoolFFI_(char, char) | |
2129 | CYPoolFFI_(ushort, unsigned short) | |
2130 | CYPoolFFI_(short, short) | |
2131 | CYPoolFFI_(ulong, unsigned long) | |
2132 | CYPoolFFI_(long, long) | |
2133 | CYPoolFFI_(uint, unsigned int) | |
2134 | CYPoolFFI_(int, int) | |
2135 | CYPoolFFI_(ulonglong, unsigned long long) | |
2136 | CYPoolFFI_(longlong, long long) | |
2137 | CYPoolFFI_(float, float) | |
2138 | CYPoolFFI_(double, double) | |
2139 | ||
2140 | #ifdef __OBJC__ | |
2141 | case sig::object_P: | |
2142 | case sig::typename_P: | |
2143 | *reinterpret_cast<id *>(data) = CYCastNSObject(pool, context, value); | |
2144 | break; | |
2145 | ||
2146 | case sig::selector_P: | |
2147 | *reinterpret_cast<SEL *>(data) = CYCastSEL(context, value); | |
2148 | break; | |
2149 | #endif | |
2150 | ||
2151 | case sig::pointer_P: | |
2152 | *reinterpret_cast<void **>(data) = CYCastPointer<void *>(context, value); | |
2153 | break; | |
2154 | ||
2155 | case sig::string_P: | |
2156 | *reinterpret_cast<const char **>(data) = CYPoolCString(pool, context, value); | |
2157 | break; | |
2158 | ||
2159 | case sig::struct_P: { | |
2160 | uint8_t *base(reinterpret_cast<uint8_t *>(data)); | |
2161 | JSObjectRef aggregate(JSValueIsObject(context, value) ? (JSObjectRef) value : NULL); | |
2162 | for (size_t index(0); index != type->data.signature.count; ++index) { | |
2163 | sig::Element *element(&type->data.signature.elements[index]); | |
2164 | ffi_type *field(ffi->elements[index]); | |
2165 | ||
2166 | JSValueRef rhs; | |
2167 | if (aggregate == NULL) | |
2168 | rhs = value; | |
2169 | else { | |
2170 | rhs = CYGetProperty(context, aggregate, index); | |
2171 | if (JSValueIsUndefined(context, rhs)) { | |
2172 | if (element->name != NULL) | |
2173 | rhs = CYGetProperty(context, aggregate, CYJSString(element->name)); | |
2174 | else | |
2175 | goto undefined; | |
2176 | if (JSValueIsUndefined(context, rhs)) undefined: | |
2177 | _throw(NSInvalidArgumentException, "unable to extract structure value"); | |
2178 | } | |
2179 | } | |
2180 | ||
2181 | CYPoolFFI(pool, context, element->type, field, base, rhs); | |
2182 | // XXX: alignment? | |
2183 | base += field->size; | |
2184 | } | |
2185 | } break; | |
2186 | ||
2187 | case sig::void_P: | |
2188 | break; | |
2189 | ||
2190 | default: | |
2191 | fprintf(stderr, "CYPoolFFI(%c)\n", type->primitive); | |
2192 | _assert(false); | |
2193 | } | |
2194 | } | |
2195 | ||
2196 | static JSValueRef CYFromFFI(JSContextRef context, sig::Type *type, ffi_type *ffi, void *data, bool initialize = false, JSObjectRef owner = NULL) { | |
2197 | JSValueRef value; | |
2198 | ||
2199 | switch (type->primitive) { | |
2200 | case sig::boolean_P: | |
2201 | value = CYCastJSValue(context, *reinterpret_cast<bool *>(data)); | |
2202 | break; | |
2203 | ||
2204 | #define CYFromFFI_(primitive, native) \ | |
2205 | case sig::primitive ## _P: \ | |
2206 | value = CYCastJSValue(context, *reinterpret_cast<native *>(data)); \ | |
2207 | break; | |
2208 | ||
2209 | CYFromFFI_(uchar, unsigned char) | |
2210 | CYFromFFI_(char, char) | |
2211 | CYFromFFI_(ushort, unsigned short) | |
2212 | CYFromFFI_(short, short) | |
2213 | CYFromFFI_(ulong, unsigned long) | |
2214 | CYFromFFI_(long, long) | |
2215 | CYFromFFI_(uint, unsigned int) | |
2216 | CYFromFFI_(int, int) | |
2217 | CYFromFFI_(ulonglong, unsigned long long) | |
2218 | CYFromFFI_(longlong, long long) | |
2219 | CYFromFFI_(float, float) | |
2220 | CYFromFFI_(double, double) | |
2221 | ||
2222 | #ifdef __OBJC__ | |
2223 | case sig::object_P: { | |
2224 | if (id object = *reinterpret_cast<id *>(data)) { | |
2225 | value = CYCastJSValue(context, object); | |
2226 | if (initialize) | |
2227 | [object release]; | |
2228 | } else goto null; | |
2229 | } break; | |
2230 | ||
2231 | case sig::typename_P: | |
2232 | value = CYMakeInstance(context, *reinterpret_cast<Class *>(data), true); | |
2233 | break; | |
2234 | ||
2235 | case sig::selector_P: | |
2236 | if (SEL sel = *reinterpret_cast<SEL *>(data)) | |
2237 | value = CYMakeSelector(context, sel); | |
2238 | else goto null; | |
2239 | break; | |
2240 | #endif | |
2241 | ||
2242 | case sig::pointer_P: | |
2243 | if (void *pointer = *reinterpret_cast<void **>(data)) | |
2244 | value = CYMakePointer(context, pointer, type->data.data.type, ffi, owner); | |
2245 | else goto null; | |
2246 | break; | |
2247 | ||
2248 | case sig::string_P: | |
2249 | if (char *utf8 = *reinterpret_cast<char **>(data)) | |
2250 | value = CYCastJSValue(context, utf8); | |
2251 | else goto null; | |
2252 | break; | |
2253 | ||
2254 | case sig::struct_P: | |
2255 | value = CYMakeStruct(context, data, type, ffi, owner); | |
2256 | break; | |
2257 | ||
2258 | case sig::void_P: | |
2259 | value = CYJSUndefined(context); | |
2260 | break; | |
2261 | ||
2262 | null: | |
2263 | value = CYJSNull(context); | |
2264 | break; | |
2265 | ||
2266 | default: | |
2267 | fprintf(stderr, "CYFromFFI(%c)\n", type->primitive); | |
2268 | _assert(false); | |
2269 | } | |
2270 | ||
2271 | return value; | |
2272 | } | |
2273 | ||
2274 | static bool CYImplements(id object, Class _class, SEL selector, bool devoid) { | |
2275 | if (objc_method *method = class_getInstanceMethod(_class, selector)) { | |
2276 | if (!devoid) | |
2277 | return true; | |
2278 | char type[16]; | |
2279 | method_getReturnType(method, type, sizeof(type)); | |
2280 | if (type[0] != 'v') | |
2281 | return true; | |
2282 | } | |
2283 | ||
2284 | // XXX: possibly use a more "awesome" check? | |
2285 | return false; | |
2286 | } | |
2287 | ||
2288 | static const char *CYPoolTypeEncoding(apr_pool_t *pool, Class _class, SEL sel, objc_method *method) { | |
2289 | if (method != NULL) | |
2290 | return method_getTypeEncoding(method); | |
2291 | else if (NSString *type = [[Bridge_ objectAtIndex:1] objectForKey:CYCastNSString(pool, sel)]) | |
2292 | return CYPoolCString(pool, type); | |
2293 | else | |
2294 | return NULL; | |
2295 | } | |
2296 | ||
2297 | static void FunctionClosure_(ffi_cif *cif, void *result, void **arguments, void *arg) { | |
2298 | Closure_privateData *internal(reinterpret_cast<Closure_privateData *>(arg)); | |
2299 | ||
2300 | JSContextRef context(internal->context_); | |
2301 | ||
2302 | size_t count(internal->cif_.nargs); | |
2303 | JSValueRef values[count]; | |
2304 | ||
2305 | for (size_t index(0); index != count; ++index) | |
2306 | values[index] = CYFromFFI(context, internal->signature_.elements[1 + index].type, internal->cif_.arg_types[index], arguments[index]); | |
2307 | ||
2308 | JSValueRef value(CYCallAsFunction(context, internal->function_, NULL, count, values)); | |
2309 | CYPoolFFI(NULL, context, internal->signature_.elements[0].type, internal->cif_.rtype, result, value); | |
2310 | } | |
2311 | ||
2312 | static void MessageClosure_(ffi_cif *cif, void *result, void **arguments, void *arg) { | |
2313 | Closure_privateData *internal(reinterpret_cast<Closure_privateData *>(arg)); | |
2314 | ||
2315 | JSContextRef context(internal->context_); | |
2316 | ||
2317 | size_t count(internal->cif_.nargs); | |
2318 | JSValueRef values[count]; | |
2319 | ||
2320 | for (size_t index(0); index != count; ++index) | |
2321 | values[index] = CYFromFFI(context, internal->signature_.elements[1 + index].type, internal->cif_.arg_types[index], arguments[index]); | |
2322 | ||
2323 | JSObjectRef _this(CYCastJSObject(context, values[0])); | |
2324 | ||
2325 | JSValueRef value(CYCallAsFunction(context, internal->function_, _this, count - 2, values + 2)); | |
2326 | CYPoolFFI(NULL, context, internal->signature_.elements[0].type, internal->cif_.rtype, result, value); | |
2327 | } | |
2328 | ||
2329 | static Closure_privateData *CYMakeFunctor_(JSContextRef context, JSObjectRef function, const char *type, void (*callback)(ffi_cif *, void *, void **, void *)) { | |
2330 | // XXX: in case of exceptions this will leak | |
2331 | // XXX: in point of fact, this may /need/ to leak :( | |
2332 | Closure_privateData *internal(new Closure_privateData(CYGetJSContext(), function, type)); | |
2333 | ||
2334 | ffi_closure *closure((ffi_closure *) _syscall(mmap( | |
2335 | NULL, sizeof(ffi_closure), | |
2336 | PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, | |
2337 | -1, 0 | |
2338 | ))); | |
2339 | ||
2340 | ffi_status status(ffi_prep_closure(closure, &internal->cif_, callback, internal)); | |
2341 | _assert(status == FFI_OK); | |
2342 | ||
2343 | _syscall(mprotect(closure, sizeof(*closure), PROT_READ | PROT_EXEC)); | |
2344 | ||
2345 | internal->value_ = closure; | |
2346 | ||
2347 | return internal; | |
2348 | } | |
2349 | ||
2350 | static JSObjectRef CYMakeFunctor(JSContextRef context, JSObjectRef function, const char *type) { | |
2351 | Closure_privateData *internal(CYMakeFunctor_(context, function, type, &FunctionClosure_)); | |
2352 | return JSObjectMake(context, Functor_, internal); | |
2353 | } | |
2354 | ||
2355 | static JSObjectRef CYMakeFunctor(JSContextRef context, JSValueRef value, const char *type) { | |
2356 | JSValueRef exception(NULL); | |
2357 | bool function(JSValueIsInstanceOfConstructor(context, value, Function_, &exception)); | |
2358 | CYThrow(context, exception); | |
2359 | ||
2360 | if (function) { | |
2361 | JSObjectRef function(CYCastJSObject(context, value)); | |
2362 | return CYMakeFunctor(context, function, type); | |
2363 | } else { | |
2364 | void (*function)()(CYCastPointer<void (*)()>(context, value)); | |
2365 | return CYMakeFunctor(context, function, type); | |
2366 | } | |
2367 | } | |
2368 | ||
2369 | #ifdef __OBJC__ | |
2370 | static JSObjectRef CYMakeMessage(JSContextRef context, SEL sel, IMP imp, const char *type) { | |
2371 | Message_privateData *internal(new Message_privateData(sel, type, imp)); | |
2372 | return JSObjectMake(context, Message_, internal); | |
2373 | } | |
2374 | ||
2375 | static IMP CYMakeMessage(JSContextRef context, JSValueRef value, const char *type) { | |
2376 | JSObjectRef function(CYCastJSObject(context, value)); | |
2377 | Closure_privateData *internal(CYMakeFunctor_(context, function, type, &MessageClosure_)); | |
2378 | return reinterpret_cast<IMP>(internal->GetValue()); | |
2379 | } | |
2380 | ||
2381 | static bool Messages_hasProperty(JSContextRef context, JSObjectRef object, JSStringRef property) { | |
2382 | Messages *internal(reinterpret_cast<Messages *>(JSObjectGetPrivate(object))); | |
2383 | Class _class(internal->GetValue()); | |
2384 | ||
2385 | CYPool pool; | |
2386 | const char *name(CYPoolCString(pool, property)); | |
2387 | ||
2388 | if (SEL sel = sel_getUid(name)) | |
2389 | if (class_getInstanceMethod(_class, sel) != NULL) | |
2390 | return true; | |
2391 | ||
2392 | return false; | |
2393 | } | |
2394 | ||
2395 | static JSValueRef Messages_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { | |
2396 | Messages *internal(reinterpret_cast<Messages *>(JSObjectGetPrivate(object))); | |
2397 | Class _class(internal->GetValue()); | |
2398 | ||
2399 | CYPool pool; | |
2400 | const char *name(CYPoolCString(pool, property)); | |
2401 | ||
2402 | if (SEL sel = sel_getUid(name)) | |
2403 | if (objc_method *method = class_getInstanceMethod(_class, sel)) | |
2404 | return CYMakeMessage(context, sel, method_getImplementation(method), method_getTypeEncoding(method)); | |
2405 | ||
2406 | return NULL; | |
2407 | } | |
2408 | ||
2409 | static bool Messages_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) { | |
2410 | Messages *internal(reinterpret_cast<Messages *>(JSObjectGetPrivate(object))); | |
2411 | Class _class(internal->GetValue()); | |
2412 | ||
2413 | CYPool pool; | |
2414 | const char *name(CYPoolCString(pool, property)); | |
2415 | ||
2416 | SEL sel(sel_registerName(name)); | |
2417 | ||
2418 | objc_method *method(class_getInstanceMethod(_class, sel)); | |
2419 | ||
2420 | const char *type; | |
2421 | IMP imp; | |
2422 | ||
2423 | if (JSValueIsObjectOfClass(context, value, Message_)) { | |
2424 | Message_privateData *message(reinterpret_cast<Message_privateData *>(JSObjectGetPrivate((JSObjectRef) value))); | |
2425 | type = sig::Unparse(pool, &message->signature_); | |
2426 | imp = reinterpret_cast<IMP>(message->GetValue()); | |
2427 | } else { | |
2428 | type = CYPoolTypeEncoding(pool, _class, sel, method); | |
2429 | imp = CYMakeMessage(context, value, type); | |
2430 | } | |
2431 | ||
2432 | if (method != NULL) | |
2433 | method_setImplementation(method, imp); | |
2434 | else | |
2435 | class_replaceMethod(_class, sel, imp, type); | |
2436 | ||
2437 | return true; | |
2438 | } | |
2439 | ||
2440 | #if !__OBJC2__ | |
2441 | static bool Messages_deleteProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { | |
2442 | Messages *internal(reinterpret_cast<Messages *>(JSObjectGetPrivate(object))); | |
2443 | Class _class(internal->GetValue()); | |
2444 | ||
2445 | CYPool pool; | |
2446 | const char *name(CYPoolCString(pool, property)); | |
2447 | ||
2448 | if (SEL sel = sel_getUid(name)) | |
2449 | if (objc_method *method = class_getInstanceMethod(_class, sel)) { | |
2450 | objc_method_list list = {NULL, 1, {method}}; | |
2451 | class_removeMethods(_class, &list); | |
2452 | return true; | |
2453 | } | |
2454 | ||
2455 | return false; | |
2456 | } | |
2457 | #endif | |
2458 | ||
2459 | static void Messages_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) { | |
2460 | Messages *internal(reinterpret_cast<Messages *>(JSObjectGetPrivate(object))); | |
2461 | Class _class(internal->GetValue()); | |
2462 | ||
2463 | unsigned int size; | |
2464 | objc_method **data(class_copyMethodList(_class, &size)); | |
2465 | for (size_t i(0); i != size; ++i) | |
2466 | JSPropertyNameAccumulatorAddName(names, CYJSString(sel_getName(method_getName(data[i])))); | |
2467 | free(data); | |
2468 | } | |
2469 | ||
2470 | static bool Instance_hasProperty(JSContextRef context, JSObjectRef object, JSStringRef property) { | |
2471 | Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object))); | |
2472 | id self(internal->GetValue()); | |
2473 | ||
2474 | if (JSStringIsEqualToUTF8CString(property, "$cyi")) | |
2475 | return true; | |
2476 | ||
2477 | CYPool pool; | |
2478 | NSString *name(CYCastNSString(pool, property)); | |
2479 | ||
2480 | if (CYInternal *internal = CYInternal::Get(self)) | |
2481 | if (internal->HasProperty(context, property)) | |
2482 | return true; | |
2483 | ||
2484 | Class _class(object_getClass(self)); | |
2485 | ||
2486 | CYPoolTry { | |
2487 | // XXX: this is an evil hack to deal with NSProxy; fix elsewhere | |
2488 | if (CYImplements(self, _class, @selector(cy$hasProperty:), false)) | |
2489 | if ([self cy$hasProperty:name]) | |
2490 | return true; | |
2491 | } CYPoolCatch(false) | |
2492 | ||
2493 | const char *string(CYPoolCString(pool, name)); | |
2494 | ||
2495 | if (class_getProperty(_class, string) != NULL) | |
2496 | return true; | |
2497 | ||
2498 | if (SEL sel = sel_getUid(string)) | |
2499 | if (CYImplements(self, _class, sel, true)) | |
2500 | return true; | |
2501 | ||
2502 | return false; | |
2503 | } | |
2504 | ||
2505 | static JSValueRef Instance_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { | |
2506 | Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object))); | |
2507 | id self(internal->GetValue()); | |
2508 | ||
2509 | if (JSStringIsEqualToUTF8CString(property, "$cyi")) | |
2510 | return Internal::Make(context, self, object); | |
2511 | ||
2512 | CYTry { | |
2513 | CYPool pool; | |
2514 | NSString *name(CYCastNSString(pool, property)); | |
2515 | ||
2516 | if (CYInternal *internal = CYInternal::Get(self)) | |
2517 | if (JSValueRef value = internal->GetProperty(context, property)) | |
2518 | return value; | |
2519 | ||
2520 | CYPoolTry { | |
2521 | if (NSObject *data = [self cy$getProperty:name]) | |
2522 | return CYCastJSValue(context, data); | |
2523 | } CYPoolCatch(NULL) | |
2524 | ||
2525 | const char *string(CYPoolCString(pool, name)); | |
2526 | Class _class(object_getClass(self)); | |
2527 | ||
2528 | #ifdef __APPLE__ | |
2529 | if (objc_property_t property = class_getProperty(_class, string)) { | |
2530 | PropertyAttributes attributes(property); | |
2531 | SEL sel(sel_registerName(attributes.Getter())); | |
2532 | return CYSendMessage(pool, context, self, NULL, sel, 0, NULL, false, exception); | |
2533 | } | |
2534 | #endif | |
2535 | ||
2536 | if (SEL sel = sel_getUid(string)) | |
2537 | if (CYImplements(self, _class, sel, true)) | |
2538 | return CYSendMessage(pool, context, self, NULL, sel, 0, NULL, false, exception); | |
2539 | ||
2540 | return NULL; | |
2541 | } CYCatch | |
2542 | } | |
2543 | ||
2544 | static bool Instance_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) { | |
2545 | Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object))); | |
2546 | id self(internal->GetValue()); | |
2547 | ||
2548 | CYPool pool; | |
2549 | ||
2550 | CYTry { | |
2551 | NSString *name(CYCastNSString(pool, property)); | |
2552 | NSObject *data(CYCastNSObject(pool, context, value)); | |
2553 | ||
2554 | CYPoolTry { | |
2555 | if ([self cy$setProperty:name to:data]) | |
2556 | return true; | |
2557 | } CYPoolCatch(NULL) | |
2558 | ||
2559 | const char *string(CYPoolCString(pool, name)); | |
2560 | Class _class(object_getClass(self)); | |
2561 | ||
2562 | #ifdef __APPLE__ | |
2563 | if (objc_property_t property = class_getProperty(_class, string)) { | |
2564 | PropertyAttributes attributes(property); | |
2565 | if (const char *setter = attributes.Setter()) { | |
2566 | SEL sel(sel_registerName(setter)); | |
2567 | JSValueRef arguments[1] = {value}; | |
2568 | CYSendMessage(pool, context, self, NULL, sel, 1, arguments, false, exception); | |
2569 | return true; | |
2570 | } | |
2571 | } | |
2572 | #endif | |
2573 | ||
2574 | size_t length(strlen(string)); | |
2575 | ||
2576 | char set[length + 5]; | |
2577 | ||
2578 | set[0] = 's'; | |
2579 | set[1] = 'e'; | |
2580 | set[2] = 't'; | |
2581 | ||
2582 | if (string[0] != '\0') { | |
2583 | set[3] = toupper(string[0]); | |
2584 | memcpy(set + 4, string + 1, length - 1); | |
2585 | } | |
2586 | ||
2587 | set[length + 3] = ':'; | |
2588 | set[length + 4] = '\0'; | |
2589 | ||
2590 | if (SEL sel = sel_getUid(set)) | |
2591 | if (CYImplements(self, _class, sel, false)) { | |
2592 | JSValueRef arguments[1] = {value}; | |
2593 | CYSendMessage(pool, context, self, NULL, sel, 1, arguments, false, exception); | |
2594 | } | |
2595 | ||
2596 | if (CYInternal *internal = CYInternal::Set(self)) { | |
2597 | internal->SetProperty(context, property, value); | |
2598 | return true; | |
2599 | } | |
2600 | ||
2601 | return false; | |
2602 | } CYCatch | |
2603 | } | |
2604 | ||
2605 | static bool Instance_deleteProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { | |
2606 | Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object))); | |
2607 | id self(internal->GetValue()); | |
2608 | ||
2609 | CYTry { | |
2610 | CYPoolTry { | |
2611 | NSString *name(CYCastNSString(NULL, property)); | |
2612 | return [self cy$deleteProperty:name]; | |
2613 | } CYPoolCatch(NULL) | |
2614 | } CYCatch | |
2615 | } | |
2616 | ||
2617 | static void Instance_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) { | |
2618 | Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object))); | |
2619 | id self(internal->GetValue()); | |
2620 | ||
2621 | CYPool pool; | |
2622 | Class _class(object_getClass(self)); | |
2623 | ||
2624 | { | |
2625 | unsigned int size; | |
2626 | objc_property_t *data(class_copyPropertyList(_class, &size)); | |
2627 | for (size_t i(0); i != size; ++i) | |
2628 | JSPropertyNameAccumulatorAddName(names, CYJSString(property_getName(data[i]))); | |
2629 | free(data); | |
2630 | } | |
2631 | } | |
2632 | ||
2633 | static JSObjectRef Instance_callAsConstructor(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { | |
2634 | CYTry { | |
2635 | Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object))); | |
2636 | JSObjectRef value(Instance::Make(context, [internal->GetValue() alloc], Instance::Uninitialized)); | |
2637 | return value; | |
2638 | } CYCatch | |
2639 | } | |
2640 | ||
2641 | static bool Instance_hasInstance(JSContextRef context, JSObjectRef constructor, JSValueRef instance, JSValueRef *exception) { | |
2642 | Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate((JSObjectRef) constructor))); | |
2643 | Class _class(internal->GetValue()); | |
2644 | if (!CYIsClass(_class)) | |
2645 | return false; | |
2646 | ||
2647 | if (JSValueIsObjectOfClass(context, instance, Instance_)) { | |
2648 | Instance *linternal(reinterpret_cast<Instance *>(JSObjectGetPrivate((JSObjectRef) instance))); | |
2649 | // XXX: this isn't always safe | |
2650 | CYTry { | |
2651 | return [linternal->GetValue() isKindOfClass:_class]; | |
2652 | } CYCatch | |
2653 | } | |
2654 | ||
2655 | return false; | |
2656 | } | |
2657 | ||
2658 | static bool Internal_hasProperty(JSContextRef context, JSObjectRef object, JSStringRef property) { | |
2659 | Internal *internal(reinterpret_cast<Internal *>(JSObjectGetPrivate(object))); | |
2660 | CYPool pool; | |
2661 | ||
2662 | id self(internal->GetValue()); | |
2663 | const char *name(CYPoolCString(pool, property)); | |
2664 | ||
2665 | if (object_getInstanceVariable(self, name, NULL) != NULL) | |
2666 | return true; | |
2667 | ||
2668 | return false; | |
2669 | } | |
2670 | ||
2671 | static JSValueRef Internal_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { | |
2672 | Internal *internal(reinterpret_cast<Internal *>(JSObjectGetPrivate(object))); | |
2673 | CYPool pool; | |
2674 | ||
2675 | CYTry { | |
2676 | id self(internal->GetValue()); | |
2677 | const char *name(CYPoolCString(pool, property)); | |
2678 | ||
2679 | if (Ivar ivar = object_getInstanceVariable(self, name, NULL)) { | |
2680 | Type_privateData type(pool, ivar_getTypeEncoding(ivar)); | |
2681 | return CYFromFFI(context, type.type_, type.GetFFI(), reinterpret_cast<uint8_t *>(self) + ivar_getOffset(ivar)); | |
2682 | } | |
2683 | ||
2684 | return NULL; | |
2685 | } CYCatch | |
2686 | } | |
2687 | ||
2688 | static bool Internal_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) { | |
2689 | Internal *internal(reinterpret_cast<Internal *>(JSObjectGetPrivate(object))); | |
2690 | CYPool pool; | |
2691 | ||
2692 | CYTry { | |
2693 | id self(internal->GetValue()); | |
2694 | const char *name(CYPoolCString(pool, property)); | |
2695 | ||
2696 | if (Ivar ivar = object_getInstanceVariable(self, name, NULL)) { | |
2697 | Type_privateData type(pool, ivar_getTypeEncoding(ivar)); | |
2698 | CYPoolFFI(pool, context, type.type_, type.GetFFI(), reinterpret_cast<uint8_t *>(self) + ivar_getOffset(ivar), value); | |
2699 | return true; | |
2700 | } | |
2701 | ||
2702 | return false; | |
2703 | } CYCatch | |
2704 | } | |
2705 | ||
2706 | static void Internal_getPropertyNames_(Class _class, JSPropertyNameAccumulatorRef names) { | |
2707 | if (Class super = class_getSuperclass(_class)) | |
2708 | Internal_getPropertyNames_(super, names); | |
2709 | ||
2710 | unsigned int size; | |
2711 | Ivar *data(class_copyIvarList(_class, &size)); | |
2712 | for (size_t i(0); i != size; ++i) | |
2713 | JSPropertyNameAccumulatorAddName(names, CYJSString(ivar_getName(data[i]))); | |
2714 | free(data); | |
2715 | } | |
2716 | ||
2717 | static void Internal_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) { | |
2718 | Internal *internal(reinterpret_cast<Internal *>(JSObjectGetPrivate(object))); | |
2719 | CYPool pool; | |
2720 | ||
2721 | id self(internal->GetValue()); | |
2722 | Class _class(object_getClass(self)); | |
2723 | ||
2724 | Internal_getPropertyNames_(_class, names); | |
2725 | } | |
2726 | ||
2727 | static JSValueRef Internal_callAsFunction_$cya(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { | |
2728 | Internal *internal(reinterpret_cast<Internal *>(JSObjectGetPrivate(object))); | |
2729 | return internal->GetOwner(); | |
2730 | } | |
2731 | #endif | |
2732 | ||
2733 | static bool Index_(apr_pool_t *pool, Struct_privateData *internal, JSStringRef property, ssize_t &index, uint8_t *&base) { | |
2734 | Type_privateData *typical(internal->type_); | |
2735 | sig::Type *type(typical->type_); | |
2736 | if (type == NULL) | |
2737 | return false; | |
2738 | ||
2739 | const char *name(CYPoolCString(pool, property)); | |
2740 | size_t length(strlen(name)); | |
2741 | double number(CYCastDouble(name, length)); | |
2742 | ||
2743 | size_t count(type->data.signature.count); | |
2744 | ||
2745 | if (std::isnan(number)) { | |
2746 | if (property == NULL) | |
2747 | return false; | |
2748 | ||
2749 | sig::Element *elements(type->data.signature.elements); | |
2750 | ||
2751 | for (size_t local(0); local != count; ++local) { | |
2752 | sig::Element *element(&elements[local]); | |
2753 | if (element->name != NULL && strcmp(name, element->name) == 0) { | |
2754 | index = local; | |
2755 | goto base; | |
2756 | } | |
2757 | } | |
2758 | ||
2759 | return false; | |
2760 | } else { | |
2761 | index = static_cast<ssize_t>(number); | |
2762 | if (index != number || index < 0 || static_cast<size_t>(index) >= count) | |
2763 | return false; | |
2764 | } | |
2765 | ||
2766 | base: | |
2767 | ffi_type **elements(typical->GetFFI()->elements); | |
2768 | ||
2769 | base = reinterpret_cast<uint8_t *>(internal->value_); | |
2770 | for (ssize_t local(0); local != index; ++local) | |
2771 | base += elements[local]->size; | |
2772 | ||
2773 | return true; | |
2774 | } | |
2775 | ||
2776 | static JSValueRef Pointer_getIndex(JSContextRef context, JSObjectRef object, size_t index, JSValueRef *exception) { | |
2777 | Pointer *internal(reinterpret_cast<Pointer *>(JSObjectGetPrivate(object))); | |
2778 | Type_privateData *typical(internal->type_); | |
2779 | ||
2780 | ffi_type *ffi(typical->GetFFI()); | |
2781 | ||
2782 | uint8_t *base(reinterpret_cast<uint8_t *>(internal->value_)); | |
2783 | base += ffi->size * index; | |
2784 | ||
2785 | JSObjectRef owner(internal->GetOwner() ?: object); | |
2786 | ||
2787 | CYTry { | |
2788 | return CYFromFFI(context, typical->type_, ffi, base, false, owner); | |
2789 | } CYCatch | |
2790 | } | |
2791 | ||
2792 | static JSValueRef Pointer_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { | |
2793 | CYPool pool; | |
2794 | Pointer *internal(reinterpret_cast<Pointer *>(JSObjectGetPrivate(object))); | |
2795 | Type_privateData *typical(internal->type_); | |
2796 | ||
2797 | if (typical->type_ == NULL) | |
2798 | return NULL; | |
2799 | ||
2800 | ssize_t offset; | |
2801 | if (!CYGetOffset(pool, property, offset)) | |
2802 | return NULL; | |
2803 | ||
2804 | return Pointer_getIndex(context, object, offset, exception); | |
2805 | } | |
2806 | ||
2807 | static JSValueRef Pointer_getProperty_$cyi(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { | |
2808 | return Pointer_getIndex(context, object, 0, exception); | |
2809 | } | |
2810 | ||
2811 | static bool Pointer_setIndex(JSContextRef context, JSObjectRef object, size_t index, JSValueRef value, JSValueRef *exception) { | |
2812 | Pointer *internal(reinterpret_cast<Pointer *>(JSObjectGetPrivate(object))); | |
2813 | Type_privateData *typical(internal->type_); | |
2814 | ||
2815 | ffi_type *ffi(typical->GetFFI()); | |
2816 | ||
2817 | uint8_t *base(reinterpret_cast<uint8_t *>(internal->value_)); | |
2818 | base += ffi->size * index; | |
2819 | ||
2820 | CYTry { | |
2821 | CYPoolFFI(NULL, context, typical->type_, ffi, base, value); | |
2822 | return true; | |
2823 | } CYCatch | |
2824 | } | |
2825 | ||
2826 | static bool Pointer_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) { | |
2827 | CYPool pool; | |
2828 | Pointer *internal(reinterpret_cast<Pointer *>(JSObjectGetPrivate(object))); | |
2829 | Type_privateData *typical(internal->type_); | |
2830 | ||
2831 | if (typical->type_ == NULL) | |
2832 | return NULL; | |
2833 | ||
2834 | ssize_t offset; | |
2835 | if (!CYGetOffset(pool, property, offset)) | |
2836 | return NULL; | |
2837 | ||
2838 | return Pointer_setIndex(context, object, offset, value, exception); | |
2839 | } | |
2840 | ||
2841 | static bool Pointer_setProperty_$cyi(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) { | |
2842 | return Pointer_setIndex(context, object, 0, value, exception); | |
2843 | } | |
2844 | ||
2845 | static JSValueRef Struct_callAsFunction_$cya(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { | |
2846 | Struct_privateData *internal(reinterpret_cast<Struct_privateData *>(JSObjectGetPrivate(_this))); | |
2847 | Type_privateData *typical(internal->type_); | |
2848 | return CYMakePointer(context, internal->value_, typical->type_, typical->ffi_, _this); | |
2849 | } | |
2850 | ||
2851 | static JSValueRef Struct_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { | |
2852 | CYPool pool; | |
2853 | Struct_privateData *internal(reinterpret_cast<Struct_privateData *>(JSObjectGetPrivate(object))); | |
2854 | Type_privateData *typical(internal->type_); | |
2855 | ||
2856 | ssize_t index; | |
2857 | uint8_t *base; | |
2858 | ||
2859 | CYTry { | |
2860 | if (!Index_(pool, internal, property, index, base)) | |
2861 | return NULL; | |
2862 | ||
2863 | JSObjectRef owner(internal->GetOwner() ?: object); | |
2864 | ||
2865 | return CYFromFFI(context, typical->type_->data.signature.elements[index].type, typical->GetFFI()->elements[index], base, false, owner); | |
2866 | } CYCatch | |
2867 | } | |
2868 | ||
2869 | static bool Struct_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) { | |
2870 | CYPool pool; | |
2871 | Struct_privateData *internal(reinterpret_cast<Struct_privateData *>(JSObjectGetPrivate(object))); | |
2872 | Type_privateData *typical(internal->type_); | |
2873 | ||
2874 | ssize_t index; | |
2875 | uint8_t *base; | |
2876 | ||
2877 | CYTry { | |
2878 | if (!Index_(pool, internal, property, index, base)) | |
2879 | return false; | |
2880 | ||
2881 | CYPoolFFI(NULL, context, typical->type_->data.signature.elements[index].type, typical->GetFFI()->elements[index], base, value); | |
2882 | return true; | |
2883 | } CYCatch | |
2884 | } | |
2885 | ||
2886 | static void Struct_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) { | |
2887 | Struct_privateData *internal(reinterpret_cast<Struct_privateData *>(JSObjectGetPrivate(object))); | |
2888 | Type_privateData *typical(internal->type_); | |
2889 | sig::Type *type(typical->type_); | |
2890 | ||
2891 | if (type == NULL) | |
2892 | return; | |
2893 | ||
2894 | size_t count(type->data.signature.count); | |
2895 | sig::Element *elements(type->data.signature.elements); | |
2896 | ||
2897 | char number[32]; | |
2898 | ||
2899 | for (size_t index(0); index != count; ++index) { | |
2900 | const char *name; | |
2901 | name = elements[index].name; | |
2902 | ||
2903 | if (name == NULL) { | |
2904 | sprintf(number, "%lu", index); | |
2905 | name = number; | |
2906 | } | |
2907 | ||
2908 | JSPropertyNameAccumulatorAddName(names, CYJSString(name)); | |
2909 | } | |
2910 | } | |
2911 | ||
2912 | 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)()) { | |
2913 | CYTry { | |
2914 | if (setups + count != signature->count - 1) | |
2915 | _throw(NSInvalidArgumentException, "incorrect number of arguments to ffi function"); | |
2916 | ||
2917 | size_t size(setups + count); | |
2918 | void *values[size]; | |
2919 | memcpy(values, setup, sizeof(void *) * setups); | |
2920 | ||
2921 | for (size_t index(setups); index != size; ++index) { | |
2922 | sig::Element *element(&signature->elements[index + 1]); | |
2923 | ffi_type *ffi(cif->arg_types[index]); | |
2924 | // XXX: alignment? | |
2925 | values[index] = new(pool) uint8_t[ffi->size]; | |
2926 | CYPoolFFI(pool, context, element->type, ffi, values[index], arguments[index - setups]); | |
2927 | } | |
2928 | ||
2929 | uint8_t value[cif->rtype->size]; | |
2930 | ffi_call(cif, function, value, values); | |
2931 | ||
2932 | return CYFromFFI(context, signature->elements[0].type, cif->rtype, value, initialize); | |
2933 | } CYCatch | |
2934 | } | |
2935 | ||
2936 | static JSValueRef ObjectiveC_Classes_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { | |
2937 | CYTry { | |
2938 | CYPool pool; | |
2939 | NSString *name(CYCastNSString(pool, property)); | |
2940 | if (Class _class = NSClassFromString(name)) | |
2941 | return CYMakeInstance(context, _class, true); | |
2942 | return NULL; | |
2943 | } CYCatch | |
2944 | } | |
2945 | ||
2946 | static void ObjectiveC_Classes_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) { | |
2947 | size_t size(objc_getClassList(NULL, 0)); | |
2948 | Class *data(reinterpret_cast<Class *>(malloc(sizeof(Class) * size))); | |
2949 | ||
2950 | get: | |
2951 | size_t writ(objc_getClassList(data, size)); | |
2952 | if (size < writ) { | |
2953 | size = writ; | |
2954 | if (Class *copy = reinterpret_cast<Class *>(realloc(data, sizeof(Class) * writ))) { | |
2955 | data = copy; | |
2956 | goto get; | |
2957 | } else goto done; | |
2958 | } | |
2959 | ||
2960 | for (size_t i(0); i != writ; ++i) | |
2961 | JSPropertyNameAccumulatorAddName(names, CYJSString(class_getName(data[i]))); | |
2962 | ||
2963 | done: | |
2964 | free(data); | |
2965 | } | |
2966 | ||
2967 | static JSValueRef ObjectiveC_Image_Classes_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { | |
2968 | const char *internal(reinterpret_cast<const char *>(JSObjectGetPrivate(object))); | |
2969 | ||
2970 | CYTry { | |
2971 | CYPool pool; | |
2972 | const char *name(CYPoolCString(pool, property)); | |
2973 | unsigned int size; | |
2974 | const char **data(objc_copyClassNamesForImage(internal, &size)); | |
2975 | JSValueRef value; | |
2976 | for (size_t i(0); i != size; ++i) | |
2977 | if (strcmp(name, data[i]) == 0) { | |
2978 | if (Class _class = objc_getClass(name)) { | |
2979 | value = CYMakeInstance(context, _class, true); | |
2980 | goto free; | |
2981 | } else | |
2982 | break; | |
2983 | } | |
2984 | value = NULL; | |
2985 | free: | |
2986 | free(data); | |
2987 | return value; | |
2988 | } CYCatch | |
2989 | } | |
2990 | ||
2991 | static void ObjectiveC_Image_Classes_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) { | |
2992 | const char *internal(reinterpret_cast<const char *>(JSObjectGetPrivate(object))); | |
2993 | unsigned int size; | |
2994 | const char **data(objc_copyClassNamesForImage(internal, &size)); | |
2995 | for (size_t i(0); i != size; ++i) | |
2996 | JSPropertyNameAccumulatorAddName(names, CYJSString(data[i])); | |
2997 | free(data); | |
2998 | } | |
2999 | ||
3000 | static JSValueRef ObjectiveC_Images_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { | |
3001 | CYTry { | |
3002 | CYPool pool; | |
3003 | const char *name(CYPoolCString(pool, property)); | |
3004 | unsigned int size; | |
3005 | const char **data(objc_copyImageNames(&size)); | |
3006 | for (size_t i(0); i != size; ++i) | |
3007 | if (strcmp(name, data[i]) == 0) { | |
3008 | name = data[i]; | |
3009 | goto free; | |
3010 | } | |
3011 | name = NULL; | |
3012 | free: | |
3013 | free(data); | |
3014 | if (name == NULL) | |
3015 | return NULL; | |
3016 | JSObjectRef value(JSObjectMake(context, NULL, NULL)); | |
3017 | CYSetProperty(context, value, CYJSString("classes"), JSObjectMake(context, ObjectiveC_Image_Classes_, const_cast<char *>(name))); | |
3018 | return value; | |
3019 | } CYCatch | |
3020 | } | |
3021 | ||
3022 | static void ObjectiveC_Images_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) { | |
3023 | unsigned int size; | |
3024 | const char **data(objc_copyImageNames(&size)); | |
3025 | for (size_t i(0); i != size; ++i) | |
3026 | JSPropertyNameAccumulatorAddName(names, CYJSString(data[i])); | |
3027 | free(data); | |
3028 | } | |
3029 | ||
3030 | static JSValueRef ObjectiveC_Protocols_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { | |
3031 | CYTry { | |
3032 | CYPool pool; | |
3033 | NSString *name(CYCastNSString(pool, property)); | |
3034 | if (Protocol *protocol = NSProtocolFromString(name)) | |
3035 | return CYMakeInstance(context, protocol, true); | |
3036 | return NULL; | |
3037 | } CYCatch | |
3038 | } | |
3039 | ||
3040 | static void ObjectiveC_Protocols_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) { | |
3041 | unsigned int size; | |
3042 | Protocol **data(objc_copyProtocolList(&size)); | |
3043 | for (size_t i(0); i != size; ++i) | |
3044 | JSPropertyNameAccumulatorAddName(names, CYJSString(protocol_getName(data[i]))); | |
3045 | free(data); | |
3046 | } | |
3047 | ||
3048 | static JSObjectRef CYMakeType(JSContextRef context, const char *type) { | |
3049 | Type_privateData *internal(new Type_privateData(NULL, type)); | |
3050 | return JSObjectMake(context, Type_privateData::Class_, internal); | |
3051 | } | |
3052 | ||
3053 | static JSObjectRef CYMakeType(JSContextRef context, sig::Type *type) { | |
3054 | Type_privateData *internal(new Type_privateData(type)); | |
3055 | return JSObjectMake(context, Type_privateData::Class_, internal); | |
3056 | } | |
3057 | ||
3058 | static JSValueRef Runtime_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { | |
3059 | if (JSStringIsEqualToUTF8CString(property, "nil")) | |
3060 | return Instance::Make(context, nil); | |
3061 | ||
3062 | CYTry { | |
3063 | CYPool pool; | |
3064 | NSString *name(CYCastNSString(pool, property)); | |
3065 | if (Class _class = NSClassFromString(name)) | |
3066 | return CYMakeInstance(context, _class, true); | |
3067 | if (NSMutableArray *entry = [[Bridge_ objectAtIndex:0] objectForKey:name]) | |
3068 | switch ([[entry objectAtIndex:0] intValue]) { | |
3069 | case 0: | |
3070 | return JSEvaluateScript(CYGetJSContext(), CYJSString([entry objectAtIndex:1]), NULL, NULL, 0, NULL); | |
3071 | case 1: | |
3072 | return CYMakeFunctor(context, reinterpret_cast<void (*)()>([name cy$symbol]), CYPoolCString(pool, [entry objectAtIndex:1])); | |
3073 | case 2: | |
3074 | // XXX: this is horrendously inefficient | |
3075 | sig::Signature signature; | |
3076 | sig::Parse(pool, &signature, CYPoolCString(pool, [entry objectAtIndex:1]), &Structor_); | |
3077 | ffi_cif cif; | |
3078 | sig::sig_ffi_cif(pool, &sig::ObjectiveC, &signature, &cif); | |
3079 | return CYFromFFI(context, signature.elements[0].type, cif.rtype, [name cy$symbol]); | |
3080 | } | |
3081 | if (NSMutableArray *entry = [[Bridge_ objectAtIndex:2] objectForKey:name]) | |
3082 | switch ([[entry objectAtIndex:0] intValue]) { | |
3083 | // XXX: implement case 0 | |
3084 | case 1: | |
3085 | return CYMakeType(context, CYPoolCString(pool, [entry objectAtIndex:1])); | |
3086 | } | |
3087 | return NULL; | |
3088 | } CYCatch | |
3089 | } | |
3090 | ||
3091 | #ifdef __OBJC__ | |
3092 | static bool stret(ffi_type *ffi_type) { | |
3093 | return ffi_type->type == FFI_TYPE_STRUCT && ( | |
3094 | ffi_type->size > OBJC_MAX_STRUCT_BY_VALUE || | |
3095 | struct_forward_array[ffi_type->size] != 0 | |
3096 | ); | |
3097 | } | |
3098 | #endif | |
3099 | ||
3100 | extern "C" { | |
3101 | int *_NSGetArgc(void); | |
3102 | char ***_NSGetArgv(void); | |
3103 | } | |
3104 | ||
3105 | static JSValueRef System_print(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { | |
3106 | CYTry { | |
3107 | if (count == 0) | |
3108 | printf("\n"); | |
3109 | else | |
3110 | printf("%s\n", CYCastCString(context, arguments[0])); | |
3111 | return CYJSUndefined(context); | |
3112 | } CYCatch | |
3113 | } | |
3114 | ||
3115 | JSValueRef CYSendMessage(apr_pool_t *pool, JSContextRef context, id self, Class _class, SEL _cmd, size_t count, const JSValueRef arguments[], bool initialize, JSValueRef *exception) { | |
3116 | const char *type; | |
3117 | ||
3118 | if (_class == NULL) | |
3119 | _class = object_getClass(self); | |
3120 | ||
3121 | if (objc_method *method = class_getInstanceMethod(_class, _cmd)) | |
3122 | type = method_getTypeEncoding(method); | |
3123 | else { | |
3124 | CYTry { | |
3125 | CYPoolTry { | |
3126 | NSMethodSignature *method([self methodSignatureForSelector:_cmd]); | |
3127 | if (method == nil) | |
3128 | _throw(NSInvalidArgumentException, "unrecognized selector %s sent to object %p", sel_getName(_cmd), self); | |
3129 | type = CYPoolCString(pool, [method _typeString]); | |
3130 | } CYPoolCatch(NULL) | |
3131 | } CYCatch | |
3132 | } | |
3133 | ||
3134 | objc_super super = {self, _class}; | |
3135 | void *arg0 = &super; | |
3136 | ||
3137 | void *setup[2]; | |
3138 | setup[0] = &arg0; | |
3139 | setup[1] = &_cmd; | |
3140 | ||
3141 | sig::Signature signature; | |
3142 | sig::Parse(pool, &signature, type, &Structor_); | |
3143 | ||
3144 | ffi_cif cif; | |
3145 | sig::sig_ffi_cif(pool, &sig::ObjectiveC, &signature, &cif); | |
3146 | ||
3147 | void (*function)() = stret(cif.rtype) ? reinterpret_cast<void (*)()>(&objc_msgSendSuper_stret) : reinterpret_cast<void (*)()>(&objc_msgSendSuper); | |
3148 | return CYCallFunction(pool, context, 2, setup, count, arguments, initialize, exception, &signature, &cif, function); | |
3149 | } | |
3150 | ||
3151 | static size_t Nonce_(0); | |
3152 | ||
3153 | static JSValueRef $cyq(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { | |
3154 | char name[16]; | |
3155 | sprintf(name, "%s%zu", CYCastCString(context, arguments[0]), Nonce_++); | |
3156 | return CYCastJSValue(context, name); | |
3157 | } | |
3158 | ||
3159 | static JSValueRef $objc_msgSend(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { | |
3160 | CYPool pool; | |
3161 | ||
3162 | bool uninitialized; | |
3163 | ||
3164 | id self; | |
3165 | SEL _cmd; | |
3166 | Class _class; | |
3167 | ||
3168 | CYTry { | |
3169 | if (count < 2) | |
3170 | _throw(NSInvalidArgumentException, "too few arguments to objc_msgSend"); | |
3171 | ||
3172 | if (JSValueIsObjectOfClass(context, arguments[0], Super_)) { | |
3173 | Super *internal(reinterpret_cast<Super *>(JSObjectGetPrivate((JSObjectRef) arguments[0]))); | |
3174 | self = internal->GetValue(); | |
3175 | _class = internal->class_;; | |
3176 | uninitialized = false; | |
3177 | } else if (JSValueIsObjectOfClass(context, arguments[0], Instance_)) { | |
3178 | Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate((JSObjectRef) arguments[0]))); | |
3179 | self = internal->GetValue(); | |
3180 | _class = nil; | |
3181 | uninitialized = internal->IsUninitialized(); | |
3182 | if (uninitialized) | |
3183 | internal->value_ = nil; | |
3184 | } else { | |
3185 | self = CYCastNSObject(pool, context, arguments[0]); | |
3186 | _class = nil; | |
3187 | uninitialized = false; | |
3188 | } | |
3189 | ||
3190 | if (self == nil) | |
3191 | return CYJSNull(context); | |
3192 | ||
3193 | _cmd = CYCastSEL(context, arguments[1]); | |
3194 | } CYCatch | |
3195 | ||
3196 | return CYSendMessage(pool, context, self, _class, _cmd, count - 2, arguments + 2, uninitialized, exception); | |
3197 | } | |
3198 | ||
3199 | #ifdef __OBJC__ | |
3200 | /* Hook: objc_registerClassPair {{{ */ | |
3201 | // XXX: replace this with associated objects | |
3202 | ||
3203 | MSHook(void, CYDealloc, id self, SEL sel) { | |
3204 | CYInternal *internal; | |
3205 | object_getInstanceVariable(self, "cy$internal_", reinterpret_cast<void **>(&internal)); | |
3206 | if (internal != NULL) | |
3207 | delete internal; | |
3208 | _CYDealloc(self, sel); | |
3209 | } | |
3210 | ||
3211 | MSHook(void, objc_registerClassPair, Class _class) { | |
3212 | Class super(class_getSuperclass(_class)); | |
3213 | if (super == NULL || class_getInstanceVariable(super, "cy$internal_") == NULL) { | |
3214 | class_addIvar(_class, "cy$internal_", sizeof(CYInternal *), log2(sizeof(CYInternal *)), "^{CYInternal}"); | |
3215 | MSHookMessage(_class, @selector(dealloc), MSHake(CYDealloc)); | |
3216 | } | |
3217 | ||
3218 | _objc_registerClassPair(_class); | |
3219 | } | |
3220 | ||
3221 | static JSValueRef objc_registerClassPair_(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { | |
3222 | CYTry { | |
3223 | if (count != 1) | |
3224 | _throw(NSInvalidArgumentException, "incorrect number of arguments to objc_registerClassPair"); | |
3225 | CYPool pool; | |
3226 | NSObject *value(CYCastNSObject(pool, context, arguments[0])); | |
3227 | if (value == NULL || !CYIsClass(value)) | |
3228 | _throw(NSInvalidArgumentException, "incorrect number of arguments to objc_registerClassPair"); | |
3229 | Class _class((Class) value); | |
3230 | $objc_registerClassPair(_class); | |
3231 | return CYJSUndefined(context); | |
3232 | } CYCatch | |
3233 | } | |
3234 | /* }}} */ | |
3235 | #endif | |
3236 | ||
3237 | static JSValueRef Cycript_gc_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { | |
3238 | JSGarbageCollect(context); | |
3239 | return CYJSUndefined(context); | |
3240 | } | |
3241 | ||
3242 | #ifdef __OBJC__ | |
3243 | static JSValueRef Selector_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { | |
3244 | JSValueRef setup[count + 2]; | |
3245 | setup[0] = _this; | |
3246 | setup[1] = object; | |
3247 | memcpy(setup + 2, arguments, sizeof(JSValueRef) * count); | |
3248 | return $objc_msgSend(context, NULL, NULL, count + 2, setup, exception); | |
3249 | } | |
3250 | ||
3251 | static JSValueRef Message_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { | |
3252 | CYPool pool; | |
3253 | Message_privateData *internal(reinterpret_cast<Message_privateData *>(JSObjectGetPrivate(object))); | |
3254 | ||
3255 | // XXX: handle Instance::Uninitialized? | |
3256 | id self(CYCastNSObject(pool, context, _this)); | |
3257 | ||
3258 | void *setup[2]; | |
3259 | setup[0] = &self; | |
3260 | setup[1] = &internal->sel_; | |
3261 | ||
3262 | return CYCallFunction(pool, context, 2, setup, count, arguments, false, exception, &internal->signature_, &internal->cif_, internal->GetValue()); | |
3263 | } | |
3264 | #endif | |
3265 | ||
3266 | static JSValueRef Functor_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { | |
3267 | CYPool pool; | |
3268 | Functor_privateData *internal(reinterpret_cast<Functor_privateData *>(JSObjectGetPrivate(object))); | |
3269 | return CYCallFunction(pool, context, 0, NULL, count, arguments, false, exception, &internal->signature_, &internal->cif_, internal->GetValue()); | |
3270 | } | |
3271 | ||
3272 | static JSObjectRef Super_new(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { | |
3273 | CYTry { | |
3274 | if (count != 2) | |
3275 | _throw(NSInvalidArgumentException, "incorrect number of arguments to Super constructor"); | |
3276 | CYPool pool; | |
3277 | NSObject *self(CYCastNSObject(pool, context, arguments[0])); | |
3278 | Class _class(CYCastClass(pool, context, arguments[1])); | |
3279 | return Super::Make(context, self, _class); | |
3280 | } CYCatch | |
3281 | } | |
3282 | ||
3283 | static JSObjectRef Selector_new(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { | |
3284 | CYTry { | |
3285 | if (count != 1) | |
3286 | _throw(NSInvalidArgumentException, "incorrect number of arguments to Selector constructor"); | |
3287 | const char *name(CYCastCString(context, arguments[0])); | |
3288 | return CYMakeSelector(context, sel_registerName(name)); | |
3289 | } CYCatch | |
3290 | } | |
3291 | ||
3292 | static JSObjectRef Pointer_new(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { | |
3293 | CYTry { | |
3294 | if (count != 2) | |
3295 | _throw(NSInvalidArgumentException, "incorrect number of arguments to Functor constructor"); | |
3296 | ||
3297 | void *value(CYCastPointer<void *>(context, arguments[0])); | |
3298 | const char *type(CYCastCString(context, arguments[1])); | |
3299 | ||
3300 | CYPool pool; | |
3301 | ||
3302 | sig::Signature signature; | |
3303 | sig::Parse(pool, &signature, type, &Structor_); | |
3304 | ||
3305 | return CYMakePointer(context, value, signature.elements[0].type, NULL, NULL); | |
3306 | } CYCatch | |
3307 | } | |
3308 | ||
3309 | static JSObjectRef Type_new(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { | |
3310 | CYTry { | |
3311 | if (count != 1) | |
3312 | _throw(NSInvalidArgumentException, "incorrect number of arguments to Type constructor"); | |
3313 | const char *type(CYCastCString(context, arguments[0])); | |
3314 | return CYMakeType(context, type); | |
3315 | } CYCatch | |
3316 | } | |
3317 | ||
3318 | static JSValueRef Type_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { | |
3319 | Type_privateData *internal(reinterpret_cast<Type_privateData *>(JSObjectGetPrivate(object))); | |
3320 | ||
3321 | CYTry { | |
3322 | sig::Type type; | |
3323 | ||
3324 | if (JSStringIsEqualToUTF8CString(property, "$cyi")) { | |
3325 | type.primitive = sig::pointer_P; | |
3326 | type.data.data.size = 0; | |
3327 | } else { | |
3328 | size_t index(CYGetIndex(NULL, property)); | |
3329 | if (index == _not(size_t)) | |
3330 | return NULL; | |
3331 | type.primitive = sig::array_P; | |
3332 | type.data.data.size = index; | |
3333 | } | |
3334 | ||
3335 | type.name = NULL; | |
3336 | type.flags = 0; | |
3337 | ||
3338 | type.data.data.type = internal->type_; | |
3339 | ||
3340 | return CYMakeType(context, &type); | |
3341 | } CYCatch | |
3342 | } | |
3343 | ||
3344 | static JSValueRef Type_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { | |
3345 | Type_privateData *internal(reinterpret_cast<Type_privateData *>(JSObjectGetPrivate(object))); | |
3346 | ||
3347 | CYTry { | |
3348 | if (count != 1) | |
3349 | _throw(NSInvalidArgumentException, "incorrect number of arguments to type cast function"); | |
3350 | sig::Type *type(internal->type_); | |
3351 | ffi_type *ffi(internal->GetFFI()); | |
3352 | // XXX: alignment? | |
3353 | uint8_t value[ffi->size]; | |
3354 | CYPool pool; | |
3355 | CYPoolFFI(pool, context, type, ffi, value, arguments[0]); | |
3356 | return CYFromFFI(context, type, ffi, value); | |
3357 | } CYCatch | |
3358 | } | |
3359 | ||
3360 | static JSObjectRef Type_callAsConstructor(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { | |
3361 | CYTry { | |
3362 | if (count != 0) | |
3363 | _throw(NSInvalidArgumentException, "incorrect number of arguments to type cast function"); | |
3364 | Type_privateData *internal(reinterpret_cast<Type_privateData *>(JSObjectGetPrivate(object))); | |
3365 | ||
3366 | sig::Type *type(internal->type_); | |
3367 | size_t size; | |
3368 | ||
3369 | if (type->primitive != sig::array_P) | |
3370 | size = 0; | |
3371 | else { | |
3372 | size = type->data.data.size; | |
3373 | type = type->data.data.type; | |
3374 | } | |
3375 | ||
3376 | void *value(malloc(internal->GetFFI()->size)); | |
3377 | return CYMakePointer(context, value, type, NULL, NULL); | |
3378 | } CYCatch | |
3379 | } | |
3380 | ||
3381 | #ifdef __OBJC__ | |
3382 | static JSObjectRef Instance_new(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { | |
3383 | CYTry { | |
3384 | if (count > 1) | |
3385 | _throw(NSInvalidArgumentException, "incorrect number of arguments to Instance constructor"); | |
3386 | id self(count == 0 ? nil : CYCastPointer<id>(context, arguments[0])); | |
3387 | return Instance::Make(context, self); | |
3388 | } CYCatch | |
3389 | } | |
3390 | #endif | |
3391 | ||
3392 | static JSObjectRef Functor_new(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { | |
3393 | CYTry { | |
3394 | if (count != 2) | |
3395 | _throw(NSInvalidArgumentException, "incorrect number of arguments to Functor constructor"); | |
3396 | const char *type(CYCastCString(context, arguments[1])); | |
3397 | return CYMakeFunctor(context, arguments[0], type); | |
3398 | } CYCatch | |
3399 | } | |
3400 | ||
3401 | static JSValueRef CYValue_getProperty_value(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { | |
3402 | CYValue *internal(reinterpret_cast<CYValue *>(JSObjectGetPrivate(object))); | |
3403 | return CYCastJSValue(context, reinterpret_cast<uintptr_t>(internal->value_)); | |
3404 | } | |
3405 | ||
3406 | static JSValueRef CYValue_callAsFunction_$cya(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { | |
3407 | CYValue *internal(reinterpret_cast<CYValue *>(JSObjectGetPrivate(_this))); | |
3408 | Type_privateData *typical(internal->GetType()); | |
3409 | ||
3410 | sig::Type *type; | |
3411 | ffi_type *ffi; | |
3412 | ||
3413 | if (typical == NULL) { | |
3414 | type = NULL; | |
3415 | ffi = NULL; | |
3416 | } else { | |
3417 | type = typical->type_; | |
3418 | ffi = typical->ffi_; | |
3419 | } | |
3420 | ||
3421 | return CYMakePointer(context, &internal->value_, type, ffi, object); | |
3422 | } | |
3423 | ||
3424 | static JSValueRef CYValue_callAsFunction_valueOf(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { | |
3425 | CYValue *internal(reinterpret_cast<CYValue *>(JSObjectGetPrivate(_this))); | |
3426 | ||
3427 | CYTry { | |
3428 | return CYCastJSValue(context, reinterpret_cast<uintptr_t>(internal->value_)); | |
3429 | } CYCatch | |
3430 | } | |
3431 | ||
3432 | static JSValueRef CYValue_callAsFunction_toJSON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { | |
3433 | return CYValue_callAsFunction_valueOf(context, object, _this, count, arguments, exception); | |
3434 | } | |
3435 | ||
3436 | static JSValueRef CYValue_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { | |
3437 | CYValue *internal(reinterpret_cast<CYValue *>(JSObjectGetPrivate(_this))); | |
3438 | char string[32]; | |
3439 | sprintf(string, "%p", internal->value_); | |
3440 | ||
3441 | CYTry { | |
3442 | return CYCastJSValue(context, string); | |
3443 | } CYCatch | |
3444 | } | |
3445 | ||
3446 | #ifdef __OBJC__ | |
3447 | static JSValueRef Instance_getProperty_constructor(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { | |
3448 | Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object))); | |
3449 | return Instance::Make(context, object_getClass(internal->GetValue())); | |
3450 | } | |
3451 | ||
3452 | static JSValueRef Instance_getProperty_protocol(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { | |
3453 | Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object))); | |
3454 | id self(internal->GetValue()); | |
3455 | if (!CYIsClass(self)) | |
3456 | return CYJSUndefined(context); | |
3457 | CYTry { | |
3458 | return CYGetClassPrototype(context, self); | |
3459 | } CYCatch | |
3460 | } | |
3461 | ||
3462 | static JSValueRef Instance_getProperty_messages(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { | |
3463 | Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object))); | |
3464 | id self(internal->GetValue()); | |
3465 | if (class_getInstanceMethod(object_getClass(self), @selector(alloc)) == NULL) | |
3466 | return CYJSUndefined(context); | |
3467 | return Messages::Make(context, self); | |
3468 | } | |
3469 | ||
3470 | static JSValueRef Instance_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { | |
3471 | if (!JSValueIsObjectOfClass(context, _this, Instance_)) | |
3472 | return NULL; | |
3473 | ||
3474 | Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(_this))); | |
3475 | ||
3476 | CYTry { | |
3477 | CYPoolTry { | |
3478 | return CYCastJSValue(context, CYJSString(CYCastNSCYON(internal->GetValue()))); | |
3479 | } CYPoolCatch(NULL) | |
3480 | } CYCatch | |
3481 | } | |
3482 | ||
3483 | static JSValueRef Instance_callAsFunction_toJSON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { | |
3484 | if (!JSValueIsObjectOfClass(context, _this, Instance_)) | |
3485 | return NULL; | |
3486 | ||
3487 | Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(_this))); | |
3488 | ||
3489 | CYTry { | |
3490 | CYPoolTry { | |
3491 | NSString *key(count == 0 ? nil : CYCastNSString(NULL, CYJSString(context, arguments[0]))); | |
3492 | // XXX: check for support of cy$toJSON? | |
3493 | return CYCastJSValue(context, CYJSString([internal->GetValue() cy$toJSON:key])); | |
3494 | } CYPoolCatch(NULL) | |
3495 | } CYCatch | |
3496 | } | |
3497 | ||
3498 | static JSValueRef Instance_callAsFunction_toString(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { | |
3499 | if (!JSValueIsObjectOfClass(context, _this, Instance_)) | |
3500 | return NULL; | |
3501 | ||
3502 | Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(_this))); | |
3503 | ||
3504 | CYTry { | |
3505 | CYPoolTry { | |
3506 | return CYCastJSValue(context, CYJSString([internal->GetValue() description])); | |
3507 | } CYPoolCatch(NULL) | |
3508 | } CYCatch | |
3509 | } | |
3510 | ||
3511 | static JSValueRef Selector_callAsFunction_toString(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { | |
3512 | Selector_privateData *internal(reinterpret_cast<Selector_privateData *>(JSObjectGetPrivate(_this))); | |
3513 | ||
3514 | CYTry { | |
3515 | return CYCastJSValue(context, sel_getName(internal->GetValue())); | |
3516 | } CYCatch | |
3517 | } | |
3518 | ||
3519 | static JSValueRef Selector_callAsFunction_toJSON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { | |
3520 | return Selector_callAsFunction_toString(context, object, _this, count, arguments, exception); | |
3521 | } | |
3522 | ||
3523 | static JSValueRef Selector_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { | |
3524 | Selector_privateData *internal(reinterpret_cast<Selector_privateData *>(JSObjectGetPrivate(_this))); | |
3525 | const char *name(sel_getName(internal->GetValue())); | |
3526 | ||
3527 | CYTry { | |
3528 | CYPoolTry { | |
3529 | return CYCastJSValue(context, CYJSString([NSString stringWithFormat:@"@selector(%s)", name])); | |
3530 | } CYPoolCatch(NULL) | |
3531 | } CYCatch | |
3532 | } | |
3533 | ||
3534 | static JSValueRef Selector_callAsFunction_type(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { | |
3535 | CYTry { | |
3536 | if (count != 1) | |
3537 | _throw(NSInvalidArgumentException, "incorrect number of arguments to Selector.type"); | |
3538 | CYPool pool; | |
3539 | Selector_privateData *internal(reinterpret_cast<Selector_privateData *>(JSObjectGetPrivate(_this))); | |
3540 | if (Class _class = CYCastClass(pool, context, arguments[0])) { | |
3541 | SEL sel(internal->GetValue()); | |
3542 | if (objc_method *method = class_getInstanceMethod(_class, sel)) | |
3543 | if (const char *type = CYPoolTypeEncoding(pool, _class, sel, method)) | |
3544 | return CYCastJSValue(context, CYJSString(type)); | |
3545 | } | |
3546 | ||
3547 | // XXX: do a lookup of some kind | |
3548 | return CYJSNull(context); | |
3549 | } CYCatch | |
3550 | } | |
3551 | #endif | |
3552 | ||
3553 | static JSValueRef Type_callAsFunction_toString(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { | |
3554 | CYTry { | |
3555 | Type_privateData *internal(reinterpret_cast<Type_privateData *>(JSObjectGetPrivate(_this))); | |
3556 | CYPool pool; | |
3557 | const char *type(sig::Unparse(pool, internal->type_)); | |
3558 | return CYCastJSValue(context, CYJSString(type)); | |
3559 | } CYCatch | |
3560 | } | |
3561 | ||
3562 | static JSValueRef Type_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { | |
3563 | CYTry { | |
3564 | Type_privateData *internal(reinterpret_cast<Type_privateData *>(JSObjectGetPrivate(_this))); | |
3565 | CYPool pool; | |
3566 | const char *type(sig::Unparse(pool, internal->type_)); | |
3567 | size_t size(strlen(type)); | |
3568 | char *cyon(new(pool) char[12 + size + 1]); | |
3569 | memcpy(cyon, "new Type(\"", 10); | |
3570 | cyon[12 + size] = '\0'; | |
3571 | cyon[12 + size - 2] = '"'; | |
3572 | cyon[12 + size - 1] = ')'; | |
3573 | memcpy(cyon + 10, type, size); | |
3574 | return CYCastJSValue(context, CYJSString(cyon)); | |
3575 | } CYCatch | |
3576 | } | |
3577 | ||
3578 | static JSValueRef Type_callAsFunction_toJSON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { | |
3579 | return Type_callAsFunction_toString(context, object, _this, count, arguments, exception); | |
3580 | } | |
3581 | ||
3582 | static JSStaticValue CYValue_staticValues[2] = { | |
3583 | {"value", &CYValue_getProperty_value, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete}, | |
3584 | {NULL, NULL, NULL, 0} | |
3585 | }; | |
3586 | ||
3587 | static JSStaticValue Pointer_staticValues[2] = { | |
3588 | {"$cyi", &Pointer_getProperty_$cyi, &Pointer_setProperty_$cyi, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
3589 | {NULL, NULL, NULL, 0} | |
3590 | }; | |
3591 | ||
3592 | static JSStaticFunction Pointer_staticFunctions[4] = { | |
3593 | {"toCYON", &CYValue_callAsFunction_toCYON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
3594 | {"toJSON", &CYValue_callAsFunction_toJSON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
3595 | {"valueOf", &CYValue_callAsFunction_valueOf, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
3596 | {NULL, NULL, 0} | |
3597 | }; | |
3598 | ||
3599 | static JSStaticFunction Struct_staticFunctions[2] = { | |
3600 | {"$cya", &Struct_callAsFunction_$cya, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
3601 | {NULL, NULL, 0} | |
3602 | }; | |
3603 | ||
3604 | static JSStaticFunction Functor_staticFunctions[4] = { | |
3605 | {"toCYON", &CYValue_callAsFunction_toCYON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
3606 | {"toJSON", &CYValue_callAsFunction_toJSON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
3607 | {"valueOf", &CYValue_callAsFunction_valueOf, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
3608 | {NULL, NULL, 0} | |
3609 | }; | |
3610 | ||
3611 | #ifdef __OBJC__ | |
3612 | static JSStaticValue Instance_staticValues[5] = { | |
3613 | {"constructor", &Instance_getProperty_constructor, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
3614 | {"messages", &Instance_getProperty_messages, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
3615 | {"prototype", &Instance_getProperty_protocol, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
3616 | {"value", &CYValue_getProperty_value, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
3617 | {NULL, NULL, NULL, 0} | |
3618 | }; | |
3619 | ||
3620 | static JSStaticFunction Instance_staticFunctions[5] = { | |
3621 | {"$cya", &CYValue_callAsFunction_$cya, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
3622 | {"toCYON", &Instance_callAsFunction_toCYON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
3623 | {"toJSON", &Instance_callAsFunction_toJSON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
3624 | {"toString", &Instance_callAsFunction_toString, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
3625 | {NULL, NULL, 0} | |
3626 | }; | |
3627 | ||
3628 | static JSStaticFunction Internal_staticFunctions[2] = { | |
3629 | {"$cya", &Internal_callAsFunction_$cya, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
3630 | {NULL, NULL, 0} | |
3631 | }; | |
3632 | ||
3633 | static JSStaticFunction Selector_staticFunctions[5] = { | |
3634 | {"toCYON", &Selector_callAsFunction_toCYON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
3635 | {"toJSON", &Selector_callAsFunction_toJSON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
3636 | {"toString", &Selector_callAsFunction_toString, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
3637 | {"type", &Selector_callAsFunction_type, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
3638 | {NULL, NULL, 0} | |
3639 | }; | |
3640 | #endif | |
3641 | ||
3642 | static JSStaticFunction Type_staticFunctions[4] = { | |
3643 | {"toCYON", &Type_callAsFunction_toCYON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
3644 | {"toJSON", &Type_callAsFunction_toJSON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
3645 | {"toString", &Type_callAsFunction_toString, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
3646 | {NULL, NULL, 0} | |
3647 | }; | |
3648 | ||
3649 | void CYSetArgs(int argc, const char *argv[]) { | |
3650 | JSContextRef context(CYGetJSContext()); | |
3651 | JSValueRef args[argc]; | |
3652 | for (int i(0); i != argc; ++i) | |
3653 | args[i] = CYCastJSValue(context, argv[i]); | |
3654 | JSValueRef exception(NULL); | |
3655 | JSObjectRef array(JSObjectMakeArray(context, argc, args, &exception)); | |
3656 | CYThrow(context, exception); | |
3657 | CYSetProperty(context, System_, CYJSString("args"), array); | |
3658 | } | |
3659 | ||
3660 | JSObjectRef CYGetGlobalObject(JSContextRef context) { | |
3661 | return JSContextGetGlobalObject(context); | |
3662 | } | |
3663 | ||
3664 | const char *CYExecute(apr_pool_t *pool, const char *code) { _pooled | |
3665 | JSContextRef context(CYGetJSContext()); | |
3666 | JSValueRef exception(NULL), result; | |
3667 | ||
3668 | try { | |
3669 | result = JSEvaluateScript(context, CYJSString(code), NULL, NULL, 0, &exception); | |
3670 | } catch (const char *error) { | |
3671 | return error; | |
3672 | } | |
3673 | ||
3674 | if (exception != NULL) { error: | |
3675 | result = exception; | |
3676 | exception = NULL; | |
3677 | } | |
3678 | ||
3679 | if (JSValueIsUndefined(context, result)) | |
3680 | return NULL; | |
3681 | ||
3682 | const char *json; | |
3683 | ||
3684 | try { | |
3685 | json = CYPoolCCYON(pool, context, result, &exception); | |
3686 | } catch (const char *error) { | |
3687 | return error; | |
3688 | } | |
3689 | ||
3690 | if (exception != NULL) | |
3691 | goto error; | |
3692 | ||
3693 | CYSetProperty(context, CYGetGlobalObject(context), Result_, result); | |
3694 | return json; | |
3695 | } | |
3696 | ||
3697 | static apr_pool_t *Pool_; | |
3698 | ||
3699 | apr_pool_t *CYGetGlobalPool() { | |
3700 | return Pool_; | |
3701 | } | |
3702 | ||
3703 | MSInitialize { _pooled | |
3704 | _aprcall(apr_initialize()); | |
3705 | _aprcall(apr_pool_create(&Pool_, NULL)); | |
3706 | ||
3707 | Bridge_ = [[NSMutableArray arrayWithContentsOfFile:@"/usr/lib/libcycript.plist"] retain]; | |
3708 | ||
3709 | #ifdef __OBJC__ | |
3710 | Type_privateData::Object = new(Pool_) Type_privateData(Pool_, "@"); | |
3711 | Type_privateData::Selector = new(Pool_) Type_privateData(Pool_, ":"); | |
3712 | ||
3713 | #ifdef __APPLE__ | |
3714 | NSCFBoolean_ = objc_getClass("NSCFBoolean"); | |
3715 | NSCFType_ = objc_getClass("NSCFType"); | |
3716 | #endif | |
3717 | ||
3718 | NSArray_ = objc_getClass("NSArray"); | |
3719 | NSDictionary_ = objc_getClass("NSDictonary"); | |
3720 | NSMessageBuilder_ = objc_getClass("NSMessageBuilder"); | |
3721 | NSZombie_ = objc_getClass("_NSZombie_"); | |
3722 | Object_ = objc_getClass("Object"); | |
3723 | #endif | |
3724 | } | |
3725 | ||
3726 | JSGlobalContextRef CYGetJSContext() { | |
3727 | if (Context_ == NULL) { | |
3728 | JSClassDefinition definition; | |
3729 | ||
3730 | definition = kJSClassDefinitionEmpty; | |
3731 | definition.className = "Functor"; | |
3732 | definition.staticFunctions = Functor_staticFunctions; | |
3733 | definition.callAsFunction = &Functor_callAsFunction; | |
3734 | definition.finalize = &Finalize; | |
3735 | Functor_ = JSClassCreate(&definition); | |
3736 | ||
3737 | definition = kJSClassDefinitionEmpty; | |
3738 | definition.className = "Pointer"; | |
3739 | definition.staticValues = Pointer_staticValues; | |
3740 | definition.staticFunctions = Pointer_staticFunctions; | |
3741 | definition.getProperty = &Pointer_getProperty; | |
3742 | definition.setProperty = &Pointer_setProperty; | |
3743 | definition.finalize = &Finalize; | |
3744 | Pointer_ = JSClassCreate(&definition); | |
3745 | ||
3746 | definition = kJSClassDefinitionEmpty; | |
3747 | definition.className = "Selector"; | |
3748 | definition.staticValues = CYValue_staticValues; | |
3749 | definition.staticFunctions = Selector_staticFunctions; | |
3750 | definition.callAsFunction = &Selector_callAsFunction; | |
3751 | definition.finalize = &Finalize; | |
3752 | Selector_ = JSClassCreate(&definition); | |
3753 | ||
3754 | definition = kJSClassDefinitionEmpty; | |
3755 | definition.className = "Struct"; | |
3756 | definition.staticFunctions = Struct_staticFunctions; | |
3757 | definition.getProperty = &Struct_getProperty; | |
3758 | definition.setProperty = &Struct_setProperty; | |
3759 | definition.getPropertyNames = &Struct_getPropertyNames; | |
3760 | definition.finalize = &Finalize; | |
3761 | Struct_ = JSClassCreate(&definition); | |
3762 | ||
3763 | definition = kJSClassDefinitionEmpty; | |
3764 | definition.className = "Type"; | |
3765 | definition.staticFunctions = Type_staticFunctions; | |
3766 | definition.getProperty = &Type_getProperty; | |
3767 | definition.callAsFunction = &Type_callAsFunction; | |
3768 | definition.callAsConstructor = &Type_callAsConstructor; | |
3769 | definition.finalize = &Finalize; | |
3770 | Type_privateData::Class_ = JSClassCreate(&definition); | |
3771 | ||
3772 | definition = kJSClassDefinitionEmpty; | |
3773 | definition.className = "Runtime"; | |
3774 | definition.getProperty = &Runtime_getProperty; | |
3775 | Runtime_ = JSClassCreate(&definition); | |
3776 | ||
3777 | definition = kJSClassDefinitionEmpty; | |
3778 | //definition.getProperty = &Global_getProperty; | |
3779 | JSClassRef Global(JSClassCreate(&definition)); | |
3780 | ||
3781 | JSGlobalContextRef context(JSGlobalContextCreate(Global)); | |
3782 | Context_ = context; | |
3783 | JSObjectRef global(CYGetGlobalObject(context)); | |
3784 | ||
3785 | JSObjectSetPrototype(context, global, JSObjectMake(context, Runtime_, NULL)); | |
3786 | ||
3787 | Array_ = CYCastJSObject(context, CYGetProperty(context, global, CYJSString("Array"))); | |
3788 | JSValueProtect(context, Array_); | |
3789 | ||
3790 | Function_ = CYCastJSObject(context, CYGetProperty(context, global, CYJSString("Function"))); | |
3791 | JSValueProtect(context, Function_); | |
3792 | ||
3793 | String_ = CYCastJSObject(context, CYGetProperty(context, global, CYJSString("String"))); | |
3794 | JSValueProtect(context, String_); | |
3795 | ||
3796 | length_ = JSStringCreateWithUTF8CString("length"); | |
3797 | message_ = JSStringCreateWithUTF8CString("message"); | |
3798 | name_ = JSStringCreateWithUTF8CString("name"); | |
3799 | prototype_ = JSStringCreateWithUTF8CString("prototype"); | |
3800 | toCYON_ = JSStringCreateWithUTF8CString("toCYON"); | |
3801 | toJSON_ = JSStringCreateWithUTF8CString("toJSON"); | |
3802 | ||
3803 | JSObjectRef Object(CYCastJSObject(context, CYGetProperty(context, global, CYJSString("Object")))); | |
3804 | Object_prototype_ = CYCastJSObject(context, CYGetProperty(context, Object, prototype_)); | |
3805 | JSValueProtect(context, Object_prototype_); | |
3806 | ||
3807 | Array_prototype_ = CYCastJSObject(context, CYGetProperty(context, Array_, prototype_)); | |
3808 | Array_pop_ = CYCastJSObject(context, CYGetProperty(context, Array_prototype_, CYJSString("pop"))); | |
3809 | Array_push_ = CYCastJSObject(context, CYGetProperty(context, Array_prototype_, CYJSString("push"))); | |
3810 | Array_splice_ = CYCastJSObject(context, CYGetProperty(context, Array_prototype_, CYJSString("splice"))); | |
3811 | ||
3812 | JSValueProtect(context, Array_prototype_); | |
3813 | JSValueProtect(context, Array_pop_); | |
3814 | JSValueProtect(context, Array_push_); | |
3815 | JSValueProtect(context, Array_splice_); | |
3816 | ||
3817 | JSObjectRef Functor(JSObjectMakeConstructor(context, Functor_, &Functor_new)); | |
3818 | ||
3819 | JSValueRef function(CYGetProperty(context, Function_, prototype_)); | |
3820 | ||
3821 | /* Objective-C Classes {{{ */ | |
3822 | #ifdef __OBJC__ | |
3823 | definition = kJSClassDefinitionEmpty; | |
3824 | definition.className = "Instance"; | |
3825 | definition.staticValues = Instance_staticValues; | |
3826 | definition.staticFunctions = Instance_staticFunctions; | |
3827 | definition.hasProperty = &Instance_hasProperty; | |
3828 | definition.getProperty = &Instance_getProperty; | |
3829 | definition.setProperty = &Instance_setProperty; | |
3830 | definition.deleteProperty = &Instance_deleteProperty; | |
3831 | definition.getPropertyNames = &Instance_getPropertyNames; | |
3832 | definition.callAsConstructor = &Instance_callAsConstructor; | |
3833 | definition.hasInstance = &Instance_hasInstance; | |
3834 | definition.finalize = &Finalize; | |
3835 | Instance_ = JSClassCreate(&definition); | |
3836 | ||
3837 | definition = kJSClassDefinitionEmpty; | |
3838 | definition.className = "Internal"; | |
3839 | definition.staticFunctions = Internal_staticFunctions; | |
3840 | definition.hasProperty = &Internal_hasProperty; | |
3841 | definition.getProperty = &Internal_getProperty; | |
3842 | definition.setProperty = &Internal_setProperty; | |
3843 | definition.getPropertyNames = &Internal_getPropertyNames; | |
3844 | definition.finalize = &Finalize; | |
3845 | Internal_ = JSClassCreate(&definition); | |
3846 | ||
3847 | definition = kJSClassDefinitionEmpty; | |
3848 | definition.className = "Message"; | |
3849 | definition.staticFunctions = Functor_staticFunctions; | |
3850 | definition.callAsFunction = &Message_callAsFunction; | |
3851 | definition.finalize = &Finalize; | |
3852 | Message_ = JSClassCreate(&definition); | |
3853 | ||
3854 | definition = kJSClassDefinitionEmpty; | |
3855 | definition.className = "Messages"; | |
3856 | definition.hasProperty = &Messages_hasProperty; | |
3857 | definition.getProperty = &Messages_getProperty; | |
3858 | definition.setProperty = &Messages_setProperty; | |
3859 | #if !__OBJC2__ | |
3860 | definition.deleteProperty = &Messages_deleteProperty; | |
3861 | #endif | |
3862 | definition.getPropertyNames = &Messages_getPropertyNames; | |
3863 | definition.finalize = &Finalize; | |
3864 | Messages_ = JSClassCreate(&definition); | |
3865 | ||
3866 | definition = kJSClassDefinitionEmpty; | |
3867 | definition.className = "Super"; | |
3868 | definition.staticFunctions = Internal_staticFunctions; | |
3869 | definition.finalize = &Finalize; | |
3870 | Super_ = JSClassCreate(&definition); | |
3871 | ||
3872 | definition = kJSClassDefinitionEmpty; | |
3873 | definition.className = "ObjectiveC::Classes"; | |
3874 | definition.getProperty = &ObjectiveC_Classes_getProperty; | |
3875 | definition.getPropertyNames = &ObjectiveC_Classes_getPropertyNames; | |
3876 | ObjectiveC_Classes_ = JSClassCreate(&definition); | |
3877 | ||
3878 | definition = kJSClassDefinitionEmpty; | |
3879 | definition.className = "ObjectiveC::Images"; | |
3880 | definition.getProperty = &ObjectiveC_Images_getProperty; | |
3881 | definition.getPropertyNames = &ObjectiveC_Images_getPropertyNames; | |
3882 | ObjectiveC_Images_ = JSClassCreate(&definition); | |
3883 | ||
3884 | definition = kJSClassDefinitionEmpty; | |
3885 | definition.className = "ObjectiveC::Image::Classes"; | |
3886 | definition.getProperty = &ObjectiveC_Image_Classes_getProperty; | |
3887 | definition.getPropertyNames = &ObjectiveC_Image_Classes_getPropertyNames; | |
3888 | ObjectiveC_Image_Classes_ = JSClassCreate(&definition); | |
3889 | ||
3890 | definition = kJSClassDefinitionEmpty; | |
3891 | definition.className = "ObjectiveC::Protocols"; | |
3892 | definition.getProperty = &ObjectiveC_Protocols_getProperty; | |
3893 | definition.getPropertyNames = &ObjectiveC_Protocols_getPropertyNames; | |
3894 | ObjectiveC_Protocols_ = JSClassCreate(&definition); | |
3895 | ||
3896 | JSObjectRef ObjectiveC(JSObjectMake(context, NULL, NULL)); | |
3897 | CYSetProperty(context, global, CYJSString("ObjectiveC"), ObjectiveC); | |
3898 | ||
3899 | CYSetProperty(context, ObjectiveC, CYJSString("classes"), JSObjectMake(context, ObjectiveC_Classes_, NULL)); | |
3900 | CYSetProperty(context, ObjectiveC, CYJSString("images"), JSObjectMake(context, ObjectiveC_Images_, NULL)); | |
3901 | CYSetProperty(context, ObjectiveC, CYJSString("protocols"), JSObjectMake(context, ObjectiveC_Protocols_, NULL)); | |
3902 | ||
3903 | JSObjectRef Instance(JSObjectMakeConstructor(context, Instance_, &Instance_new)); | |
3904 | JSObjectRef Message(JSObjectMakeConstructor(context, Message_, NULL)); | |
3905 | JSObjectRef Selector(JSObjectMakeConstructor(context, Selector_, &Selector_new)); | |
3906 | JSObjectRef Super(JSObjectMakeConstructor(context, Super_, &Super_new)); | |
3907 | ||
3908 | Instance_prototype_ = (JSObjectRef) CYGetProperty(context, Instance, prototype_); | |
3909 | JSValueProtect(context, Instance_prototype_); | |
3910 | ||
3911 | CYSetProperty(context, global, CYJSString("Instance"), Instance); | |
3912 | CYSetProperty(context, global, CYJSString("Selector"), Selector); | |
3913 | CYSetProperty(context, global, CYJSString("Super"), Super); | |
3914 | ||
3915 | CYSetProperty(context, global, CYJSString("objc_registerClassPair"), JSObjectMakeFunctionWithCallback(context, CYJSString("objc_registerClassPair"), &objc_registerClassPair_)); | |
3916 | CYSetProperty(context, global, CYJSString("objc_msgSend"), JSObjectMakeFunctionWithCallback(context, CYJSString("objc_msgSend"), &$objc_msgSend)); | |
3917 | ||
3918 | JSObjectSetPrototype(context, (JSObjectRef) CYGetProperty(context, Message, prototype_), function); | |
3919 | JSObjectSetPrototype(context, (JSObjectRef) CYGetProperty(context, Selector, prototype_), function); | |
3920 | #endif | |
3921 | /* }}} */ | |
3922 | ||
3923 | JSObjectSetPrototype(context, (JSObjectRef) CYGetProperty(context, Functor, prototype_), function); | |
3924 | ||
3925 | CYSetProperty(context, global, CYJSString("Functor"), Functor); | |
3926 | CYSetProperty(context, global, CYJSString("Pointer"), JSObjectMakeConstructor(context, Pointer_, &Pointer_new)); | |
3927 | CYSetProperty(context, global, CYJSString("Type"), JSObjectMakeConstructor(context, Type_privateData::Class_, &Type_new)); | |
3928 | ||
3929 | MSHookFunction(&objc_registerClassPair, MSHake(objc_registerClassPair)); | |
3930 | ||
3931 | #ifdef __OBJC__ | |
3932 | #ifdef __APPLE__ | |
3933 | class_addMethod(NSCFType_, @selector(cy$toJSON:), reinterpret_cast<IMP>(&NSCFType$cy$toJSON), "@12@0:4@8"); | |
3934 | #endif | |
3935 | #endif | |
3936 | ||
3937 | JSObjectRef cycript(JSObjectMake(context, NULL, NULL)); | |
3938 | CYSetProperty(context, global, CYJSString("Cycript"), cycript); | |
3939 | CYSetProperty(context, cycript, CYJSString("gc"), JSObjectMakeFunctionWithCallback(context, CYJSString("gc"), &Cycript_gc_callAsFunction)); | |
3940 | ||
3941 | CYSetProperty(context, global, CYJSString("$cyq"), JSObjectMakeFunctionWithCallback(context, CYJSString("$cyq"), &$cyq)); | |
3942 | ||
3943 | System_ = JSObjectMake(context, NULL, NULL); | |
3944 | JSValueProtect(context, System_); | |
3945 | ||
3946 | CYSetProperty(context, global, CYJSString("system"), System_); | |
3947 | CYSetProperty(context, System_, CYJSString("args"), CYJSNull(context)); | |
3948 | //CYSetProperty(context, System_, CYJSString("global"), global); | |
3949 | ||
3950 | CYSetProperty(context, System_, CYJSString("print"), JSObjectMakeFunctionWithCallback(context, CYJSString("print"), &System_print)); | |
3951 | ||
3952 | Result_ = JSStringCreateWithUTF8CString("_"); | |
3953 | } | |
3954 | ||
3955 | return Context_; | |
3956 | } |