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