]>
Commit | Line | Data |
---|---|---|
1 | /* Cycript - Inlining/Optimizing JavaScript Compiler | |
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 | #if defined(__APPLE__) && defined(__arm__) | |
41 | #include <substrate.h> | |
42 | #else | |
43 | #include <objc/objc-api.h> | |
44 | #endif | |
45 | ||
46 | #ifdef __APPLE__ | |
47 | #include "Struct.hpp" | |
48 | #endif | |
49 | ||
50 | #include <Foundation/Foundation.h> | |
51 | ||
52 | #include "ObjectiveC/Internal.hpp" | |
53 | ||
54 | #include <objc/Protocol.h> | |
55 | ||
56 | #include "cycript.hpp" | |
57 | ||
58 | #include "ObjectiveC/Internal.hpp" | |
59 | ||
60 | #ifdef __APPLE__ | |
61 | #include <CoreFoundation/CoreFoundation.h> | |
62 | #include <JavaScriptCore/JSStringRefCF.h> | |
63 | #include <WebKit/WebScriptObject.h> | |
64 | #endif | |
65 | ||
66 | #include "Error.hpp" | |
67 | #include "JavaScript.hpp" | |
68 | #include "String.hpp" | |
69 | #include "Execute.hpp" | |
70 | ||
71 | #include <cmath> | |
72 | #include <map> | |
73 | ||
74 | #define CYObjectiveTry_(context) { \ | |
75 | JSContextRef context_(context); \ | |
76 | try | |
77 | #define CYObjectiveTry { \ | |
78 | try | |
79 | #define CYObjectiveCatch \ | |
80 | catch (const CYException &error) { \ | |
81 | @throw CYCastNSObject(NULL, context_, error.CastJSValue(context_)); \ | |
82 | } \ | |
83 | } | |
84 | ||
85 | #define CYPoolTry { \ | |
86 | id _saved(nil); \ | |
87 | NSAutoreleasePool *_pool([[NSAutoreleasePool alloc] init]); \ | |
88 | @try | |
89 | #define CYPoolCatch(value) \ | |
90 | @catch (NSException *error) { \ | |
91 | _saved = [error retain]; \ | |
92 | throw CYJSError(context, CYCastJSValue(context, error)); \ | |
93 | return value; \ | |
94 | } @finally { \ | |
95 | [_pool release]; \ | |
96 | if (_saved != nil) \ | |
97 | [_saved autorelease]; \ | |
98 | } \ | |
99 | } | |
100 | ||
101 | #define CYSadTry { \ | |
102 | @try | |
103 | #define CYSadCatch(value) \ | |
104 | @catch (NSException *error ) { \ | |
105 | throw CYJSError(context, CYCastJSValue(context, error)); \ | |
106 | } return value; \ | |
107 | } | |
108 | ||
109 | #ifndef __APPLE__ | |
110 | #define class_getSuperclass GSObjCSuper | |
111 | #define class_getInstanceVariable GSCGetInstanceVariableDefinition | |
112 | #define class_getName GSNameFromClass | |
113 | ||
114 | #define class_removeMethods(cls, list) GSRemoveMethodList(cls, list, YES) | |
115 | ||
116 | #define ivar_getName(ivar) ((ivar)->ivar_name) | |
117 | #define ivar_getOffset(ivar) ((ivar)->ivar_offset) | |
118 | #define ivar_getTypeEncoding(ivar) ((ivar)->ivar_type) | |
119 | ||
120 | #define method_getName(method) ((method)->method_name) | |
121 | #define method_getImplementation(method) ((method)->method_imp) | |
122 | #define method_getTypeEncoding(method) ((method)->method_types) | |
123 | #define method_setImplementation(method, imp) ((void) ((method)->method_imp = (imp))) | |
124 | ||
125 | #undef objc_getClass | |
126 | #define objc_getClass GSClassFromName | |
127 | ||
128 | #define objc_getProtocol GSProtocolFromName | |
129 | ||
130 | #define object_getClass GSObjCClass | |
131 | ||
132 | #define object_getInstanceVariable(object, name, value) ({ \ | |
133 | objc_ivar *ivar(class_getInstanceVariable(object_getClass(object), name)); \ | |
134 | if (ivar != NULL) \ | |
135 | GSObjCGetVariable(object, ivar_getOffset(ivar), sizeof(void *), value); \ | |
136 | ivar; \ | |
137 | }) | |
138 | ||
139 | #define object_setIvar(object, ivar, value) ({ \ | |
140 | void *data = (value); \ | |
141 | GSObjCSetVariable(object, ivar_getOffset(ivar), sizeof(void *), &data); \ | |
142 | }) | |
143 | ||
144 | #define protocol_getName(protocol) [(protocol) name] | |
145 | #endif | |
146 | ||
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 | /* Objective-C Pool Release {{{ */ | |
150 | apr_status_t CYPoolRelease_(void *data) { | |
151 | id object(reinterpret_cast<id>(data)); | |
152 | [object release]; | |
153 | return APR_SUCCESS; | |
154 | } | |
155 | ||
156 | id CYPoolRelease_(apr_pool_t *pool, id object) { | |
157 | if (object == nil) | |
158 | return nil; | |
159 | else if (pool == NULL) | |
160 | return [object autorelease]; | |
161 | else { | |
162 | apr_pool_cleanup_register(pool, object, &CYPoolRelease_, &apr_pool_cleanup_null); | |
163 | return object; | |
164 | } | |
165 | } | |
166 | ||
167 | template <typename Type_> | |
168 | Type_ CYPoolRelease(apr_pool_t *pool, Type_ object) { | |
169 | return (Type_) CYPoolRelease_(pool, (id) object); | |
170 | } | |
171 | /* }}} */ | |
172 | /* Objective-C Strings {{{ */ | |
173 | const char *CYPoolCString(apr_pool_t *pool, JSContextRef context, NSString *value) { | |
174 | if (pool == NULL) | |
175 | return [value UTF8String]; | |
176 | else { | |
177 | size_t size([value maximumLengthOfBytesUsingEncoding:NSUTF8StringEncoding] + 1); | |
178 | char *string(new(pool) char[size]); | |
179 | if (![value getCString:string maxLength:size encoding:NSUTF8StringEncoding]) | |
180 | throw CYJSError(context, "[NSString getCString:maxLength:encoding:] == NO"); | |
181 | return string; | |
182 | } | |
183 | } | |
184 | ||
185 | JSStringRef CYCopyJSString(JSContextRef context, NSString *value) { | |
186 | #ifdef __APPLE__ | |
187 | return JSStringCreateWithCFString(reinterpret_cast<CFStringRef>(value)); | |
188 | #else | |
189 | CYPool pool; | |
190 | return CYCopyJSString(CYPoolCString(pool, context, value)); | |
191 | #endif | |
192 | } | |
193 | ||
194 | JSStringRef CYCopyJSString(JSContextRef context, NSObject *value) { | |
195 | if (value == nil) | |
196 | return NULL; | |
197 | // XXX: this definition scares me; is anyone using this?! | |
198 | NSString *string([value description]); | |
199 | return CYCopyJSString(context, string); | |
200 | } | |
201 | ||
202 | NSString *CYCopyNSString(const CYUTF8String &value) { | |
203 | #ifdef __APPLE__ | |
204 | return (NSString *) CFStringCreateWithBytes(kCFAllocatorDefault, reinterpret_cast<const UInt8 *>(value.data), value.size, kCFStringEncodingUTF8, true); | |
205 | #else | |
206 | return [[NSString alloc] initWithBytes:value.data length:value.size encoding:NSUTF8StringEncoding]; | |
207 | #endif | |
208 | } | |
209 | ||
210 | NSString *CYCopyNSString(JSContextRef context, JSStringRef value) { | |
211 | #ifdef __APPLE__ | |
212 | return (NSString *) JSStringCopyCFString(kCFAllocatorDefault, value); | |
213 | #else | |
214 | CYPool pool; | |
215 | return CYCopyNSString(CYPoolUTF8String(pool, context, value)); | |
216 | #endif | |
217 | } | |
218 | ||
219 | NSString *CYCopyNSString(JSContextRef context, JSValueRef value) { | |
220 | return CYCopyNSString(context, CYJSString(context, value)); | |
221 | } | |
222 | ||
223 | NSString *CYCastNSString(apr_pool_t *pool, const CYUTF8String &value) { | |
224 | return CYPoolRelease(pool, CYCopyNSString(value)); | |
225 | } | |
226 | ||
227 | NSString *CYCastNSString(apr_pool_t *pool, SEL sel) { | |
228 | const char *name(sel_getName(sel)); | |
229 | return CYPoolRelease(pool, CYCopyNSString(CYUTF8String(name, strlen(name)))); | |
230 | } | |
231 | ||
232 | NSString *CYCastNSString(apr_pool_t *pool, JSContextRef context, JSStringRef value) { | |
233 | return CYPoolRelease(pool, CYCopyNSString(context, value)); | |
234 | } | |
235 | ||
236 | CYUTF8String CYCastUTF8String(NSString *value) { | |
237 | NSData *data([value dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:NO]); | |
238 | return CYUTF8String(reinterpret_cast<const char *>([data bytes]), [data length]); | |
239 | } | |
240 | /* }}} */ | |
241 | ||
242 | JSValueRef CYCastJSValue(JSContextRef context, NSObject *value); | |
243 | ||
244 | void CYThrow(JSContextRef context, NSException *error, JSValueRef *exception) { | |
245 | if (exception == NULL) | |
246 | throw error; | |
247 | *exception = CYCastJSValue(context, error); | |
248 | } | |
249 | ||
250 | size_t CYGetIndex(NSString *value) { | |
251 | return CYGetIndex(CYCastUTF8String(value)); | |
252 | } | |
253 | ||
254 | bool CYGetOffset(apr_pool_t *pool, JSContextRef context, NSString *value, ssize_t &index) { | |
255 | return CYGetOffset(CYPoolCString(pool, context, value), index); | |
256 | } | |
257 | ||
258 | static JSClassRef Instance_; | |
259 | static JSClassRef Internal_; | |
260 | static JSClassRef Message_; | |
261 | static JSClassRef Messages_; | |
262 | static JSClassRef Selector_; | |
263 | static JSClassRef StringInstance_; | |
264 | static JSClassRef Super_; | |
265 | ||
266 | static JSClassRef ObjectiveC_Classes_; | |
267 | static JSClassRef ObjectiveC_Protocols_; | |
268 | ||
269 | #ifdef __APPLE__ | |
270 | static JSClassRef ObjectiveC_Image_Classes_; | |
271 | static JSClassRef ObjectiveC_Images_; | |
272 | #endif | |
273 | ||
274 | #ifdef __APPLE__ | |
275 | static Class NSCFBoolean_; | |
276 | static Class NSCFType_; | |
277 | static Class NSMessageBuilder_; | |
278 | static Class NSZombie_; | |
279 | #else | |
280 | static Class NSBoolNumber_; | |
281 | #endif | |
282 | ||
283 | static Class NSArray_; | |
284 | static Class NSDictionary_; | |
285 | static Class NSString_; | |
286 | static Class Object_; | |
287 | ||
288 | static Type_privateData *Object_type; | |
289 | static Type_privateData *Selector_type; | |
290 | ||
291 | Type_privateData *Instance::GetType() const { | |
292 | return Object_type; | |
293 | } | |
294 | ||
295 | Type_privateData *Selector_privateData::GetType() const { | |
296 | return Selector_type; | |
297 | } | |
298 | ||
299 | static JSValueRef Instance_callAsFunction_toString(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception); | |
300 | ||
301 | JSValueRef CYGetClassPrototype(JSContextRef context, id self) { | |
302 | if (self == nil) | |
303 | return CYGetCachedObject(context, CYJSString("Instance_prototype")); | |
304 | ||
305 | JSObjectRef global(CYGetGlobalObject(context)); | |
306 | JSObjectRef cy(CYCastJSObject(context, CYGetProperty(context, global, cy_s))); | |
307 | ||
308 | char label[32]; | |
309 | sprintf(label, "i%p", self); | |
310 | CYJSString name(label); | |
311 | ||
312 | JSValueRef value(CYGetProperty(context, cy, name)); | |
313 | if (!JSValueIsUndefined(context, value)) | |
314 | return value; | |
315 | ||
316 | JSClassRef _class(NULL); | |
317 | JSValueRef prototype; | |
318 | ||
319 | if (self == NSArray_) | |
320 | prototype = CYGetCachedObject(context, CYJSString("Array_prototype")); | |
321 | else if (self == NSDictionary_) | |
322 | prototype = CYGetCachedObject(context, CYJSString("Object_prototype")); | |
323 | else if (self == NSString_) | |
324 | prototype = CYGetCachedObject(context, CYJSString("StringInstance_prototype")); | |
325 | else | |
326 | prototype = CYGetClassPrototype(context, class_getSuperclass(self)); | |
327 | ||
328 | JSObjectRef object(JSObjectMake(context, _class, NULL)); | |
329 | JSObjectSetPrototype(context, object, prototype); | |
330 | CYSetProperty(context, cy, name, object); | |
331 | ||
332 | return object; | |
333 | } | |
334 | ||
335 | JSObjectRef Messages::Make(JSContextRef context, Class _class, bool array) { | |
336 | JSObjectRef value(JSObjectMake(context, Messages_, new Messages(_class))); | |
337 | if (_class == NSArray_) | |
338 | array = true; | |
339 | if (Class super = class_getSuperclass(_class)) | |
340 | JSObjectSetPrototype(context, value, Messages::Make(context, super, array)); | |
341 | /*else if (array) | |
342 | JSObjectSetPrototype(context, value, Array_prototype_);*/ | |
343 | return value; | |
344 | } | |
345 | ||
346 | JSObjectRef Internal::Make(JSContextRef context, id object, JSObjectRef owner) { | |
347 | return JSObjectMake(context, Internal_, new Internal(object, context, owner)); | |
348 | } | |
349 | ||
350 | namespace cy { | |
351 | JSObjectRef Super::Make(JSContextRef context, id object, Class _class) { | |
352 | JSObjectRef value(JSObjectMake(context, Super_, new Super(object, _class))); | |
353 | return value; | |
354 | } } | |
355 | ||
356 | JSObjectRef Instance::Make(JSContextRef context, id object, Flags flags) { | |
357 | JSObjectRef value(JSObjectMake(context, Instance_, new Instance(object, flags))); | |
358 | JSObjectSetPrototype(context, value, CYGetClassPrototype(context, object_getClass(object))); | |
359 | return value; | |
360 | } | |
361 | ||
362 | Instance::~Instance() { | |
363 | if ((flags_ & Transient) == 0) | |
364 | // XXX: does this handle background threads correctly? | |
365 | // XXX: this simply does not work on the console because I'm stupid | |
366 | [GetValue() performSelector:@selector(release) withObject:nil afterDelay:0]; | |
367 | } | |
368 | ||
369 | struct Message_privateData : | |
370 | cy::Functor | |
371 | { | |
372 | SEL sel_; | |
373 | ||
374 | Message_privateData(SEL sel, const char *type, IMP value = NULL) : | |
375 | cy::Functor(type, reinterpret_cast<void (*)()>(value)), | |
376 | sel_(sel) | |
377 | { | |
378 | } | |
379 | }; | |
380 | ||
381 | JSObjectRef CYMakeInstance(JSContextRef context, id object, bool transient) { | |
382 | Instance::Flags flags; | |
383 | ||
384 | if (transient) | |
385 | flags = Instance::Transient; | |
386 | else { | |
387 | flags = Instance::None; | |
388 | object = [object retain]; | |
389 | } | |
390 | ||
391 | return Instance::Make(context, object, flags); | |
392 | } | |
393 | ||
394 | @interface NSMethodSignature (Cycript) | |
395 | - (NSString *) _typeString; | |
396 | @end | |
397 | ||
398 | @interface NSObject (Cycript) | |
399 | ||
400 | - (JSValueRef) cy$JSValueInContext:(JSContextRef)context; | |
401 | - (JSType) cy$JSType; | |
402 | ||
403 | - (NSObject *) cy$toJSON:(NSString *)key; | |
404 | - (NSString *) cy$toCYON; | |
405 | - (NSString *) cy$toKey; | |
406 | ||
407 | - (bool) cy$hasProperty:(NSString *)name; | |
408 | - (NSObject *) cy$getProperty:(NSString *)name; | |
409 | - (bool) cy$setProperty:(NSString *)name to:(NSObject *)value; | |
410 | - (bool) cy$deleteProperty:(NSString *)name; | |
411 | - (void) cy$getPropertyNames:(JSPropertyNameAccumulatorRef)names inContext:(JSContextRef)context; | |
412 | ||
413 | + (bool) cy$hasImplicitProperties; | |
414 | ||
415 | @end | |
416 | ||
417 | @protocol Cycript | |
418 | - (JSValueRef) cy$JSValueInContext:(JSContextRef)context; | |
419 | @end | |
420 | ||
421 | NSString *CYCastNSCYON(id value) { | |
422 | NSString *string; | |
423 | ||
424 | if (value == nil) | |
425 | string = @"nil"; | |
426 | else { | |
427 | Class _class(object_getClass(value)); | |
428 | SEL sel(@selector(cy$toCYON)); | |
429 | ||
430 | if (objc_method *toCYON = class_getInstanceMethod(_class, sel)) | |
431 | string = reinterpret_cast<NSString *(*)(id, SEL)>(method_getImplementation(toCYON))(value, sel); | |
432 | else if (objc_method *methodSignatureForSelector = class_getInstanceMethod(_class, @selector(methodSignatureForSelector:))) { | |
433 | if (reinterpret_cast<NSMethodSignature *(*)(id, SEL, SEL)>(method_getImplementation(methodSignatureForSelector))(value, @selector(methodSignatureForSelector:), sel) != nil) | |
434 | string = [value cy$toCYON]; | |
435 | else goto fail; | |
436 | } else fail: { | |
437 | if (false); | |
438 | #ifdef __APPLE__ | |
439 | else if (value == NSZombie_) | |
440 | string = @"_NSZombie_"; | |
441 | else if (_class == NSZombie_) | |
442 | string = [NSString stringWithFormat:@"<_NSZombie_: %p>", value]; | |
443 | // XXX: frowny /in/ the pants | |
444 | else if (value == NSMessageBuilder_ || value == Object_) | |
445 | string = nil; | |
446 | #endif | |
447 | else | |
448 | string = [NSString stringWithFormat:@"%@", value]; | |
449 | } | |
450 | ||
451 | // XXX: frowny pants | |
452 | if (string == nil) | |
453 | string = @"undefined"; | |
454 | } | |
455 | ||
456 | return string; | |
457 | } | |
458 | ||
459 | #ifdef __APPLE__ | |
460 | struct PropertyAttributes { | |
461 | CYPool pool_; | |
462 | ||
463 | const char *name; | |
464 | ||
465 | const char *variable; | |
466 | ||
467 | const char *getter_; | |
468 | const char *setter_; | |
469 | ||
470 | bool readonly; | |
471 | bool copy; | |
472 | bool retain; | |
473 | bool nonatomic; | |
474 | bool dynamic; | |
475 | bool weak; | |
476 | bool garbage; | |
477 | ||
478 | PropertyAttributes(objc_property_t property) : | |
479 | variable(NULL), | |
480 | getter_(NULL), | |
481 | setter_(NULL), | |
482 | readonly(false), | |
483 | copy(false), | |
484 | retain(false), | |
485 | nonatomic(false), | |
486 | dynamic(false), | |
487 | weak(false), | |
488 | garbage(false) | |
489 | { | |
490 | name = property_getName(property); | |
491 | const char *attributes(property_getAttributes(property)); | |
492 | ||
493 | for (char *state, *token(apr_strtok(apr_pstrdup(pool_, attributes), ",", &state)); token != NULL; token = apr_strtok(NULL, ",", &state)) { | |
494 | switch (*token) { | |
495 | case 'R': readonly = true; break; | |
496 | case 'C': copy = true; break; | |
497 | case '&': retain = true; break; | |
498 | case 'N': nonatomic = true; break; | |
499 | case 'G': getter_ = token + 1; break; | |
500 | case 'S': setter_ = token + 1; break; | |
501 | case 'V': variable = token + 1; break; | |
502 | } | |
503 | } | |
504 | ||
505 | /*if (variable == NULL) { | |
506 | variable = property_getName(property); | |
507 | size_t size(strlen(variable)); | |
508 | char *name(new(pool_) char[size + 2]); | |
509 | name[0] = '_'; | |
510 | memcpy(name + 1, variable, size); | |
511 | name[size + 1] = '\0'; | |
512 | variable = name; | |
513 | }*/ | |
514 | } | |
515 | ||
516 | const char *Getter() { | |
517 | if (getter_ == NULL) | |
518 | getter_ = apr_pstrdup(pool_, name); | |
519 | return getter_; | |
520 | } | |
521 | ||
522 | const char *Setter() { | |
523 | if (setter_ == NULL && !readonly) { | |
524 | size_t length(strlen(name)); | |
525 | ||
526 | char *temp(new(pool_) char[length + 5]); | |
527 | temp[0] = 's'; | |
528 | temp[1] = 'e'; | |
529 | temp[2] = 't'; | |
530 | ||
531 | if (length != 0) { | |
532 | temp[3] = toupper(name[0]); | |
533 | memcpy(temp + 4, name + 1, length - 1); | |
534 | } | |
535 | ||
536 | temp[length + 3] = ':'; | |
537 | temp[length + 4] = '\0'; | |
538 | setter_ = temp; | |
539 | } | |
540 | ||
541 | return setter_; | |
542 | } | |
543 | ||
544 | }; | |
545 | #endif | |
546 | ||
547 | #ifdef __APPLE__ | |
548 | NSObject *NSCFType$cy$toJSON(id self, SEL sel, NSString *key) { | |
549 | return [(NSString *) CFCopyDescription((CFTypeRef) self) autorelease]; | |
550 | } | |
551 | #endif | |
552 | ||
553 | #ifndef __APPLE__ | |
554 | @interface CYWebUndefined : NSObject { | |
555 | } | |
556 | ||
557 | + (CYWebUndefined *) undefined; | |
558 | ||
559 | @end | |
560 | ||
561 | @implementation CYWebUndefined | |
562 | ||
563 | + (CYWebUndefined *) undefined { | |
564 | static CYWebUndefined *instance_([[CYWebUndefined alloc] init]); | |
565 | return instance_; | |
566 | } | |
567 | ||
568 | @end | |
569 | ||
570 | #define WebUndefined CYWebUndefined | |
571 | #endif | |
572 | ||
573 | /* Bridge: CYJSObject {{{ */ | |
574 | @interface CYJSObject : NSMutableDictionary { | |
575 | JSObjectRef object_; | |
576 | JSContextRef context_; | |
577 | } | |
578 | ||
579 | - (id) initWithJSObject:(JSObjectRef)object inContext:(JSContextRef)context; | |
580 | ||
581 | - (NSObject *) cy$toJSON:(NSString *)key; | |
582 | ||
583 | - (NSUInteger) count; | |
584 | - (id) objectForKey:(id)key; | |
585 | - (NSEnumerator *) keyEnumerator; | |
586 | - (void) setObject:(id)object forKey:(id)key; | |
587 | - (void) removeObjectForKey:(id)key; | |
588 | ||
589 | @end | |
590 | /* }}} */ | |
591 | /* Bridge: CYJSArray {{{ */ | |
592 | @interface CYJSArray : NSMutableArray { | |
593 | JSObjectRef object_; | |
594 | JSContextRef context_; | |
595 | } | |
596 | ||
597 | - (id) initWithJSObject:(JSObjectRef)object inContext:(JSContextRef)context; | |
598 | ||
599 | - (NSUInteger) count; | |
600 | - (id) objectAtIndex:(NSUInteger)index; | |
601 | ||
602 | - (void) addObject:(id)anObject; | |
603 | - (void) insertObject:(id)anObject atIndex:(NSUInteger)index; | |
604 | - (void) removeLastObject; | |
605 | - (void) removeObjectAtIndex:(NSUInteger)index; | |
606 | - (void) replaceObjectAtIndex:(NSUInteger)index withObject:(id)anObject; | |
607 | ||
608 | @end | |
609 | /* }}} */ | |
610 | ||
611 | NSObject *CYCastNSObject_(apr_pool_t *pool, JSContextRef context, JSObjectRef object) { | |
612 | JSObjectRef Array(CYGetCachedObject(context, Array_s)); | |
613 | JSValueRef exception(NULL); | |
614 | bool array(JSValueIsInstanceOfConstructor(context, object, Array, &exception)); | |
615 | CYThrow(context, exception); | |
616 | id value(array ? [CYJSArray alloc] : [CYJSObject alloc]); | |
617 | return CYPoolRelease(pool, [value initWithJSObject:object inContext:context]); | |
618 | } | |
619 | ||
620 | NSObject *CYCastNSObject(apr_pool_t *pool, JSContextRef context, JSObjectRef object) { | |
621 | if (!JSValueIsObjectOfClass(context, object, Instance_)) | |
622 | return CYCastNSObject_(pool, context, object); | |
623 | else { | |
624 | Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object))); | |
625 | return internal->GetValue(); | |
626 | } | |
627 | } | |
628 | ||
629 | NSNumber *CYCopyNSNumber(JSContextRef context, JSValueRef value) { | |
630 | return [[NSNumber alloc] initWithDouble:CYCastDouble(context, value)]; | |
631 | } | |
632 | ||
633 | #ifndef __APPLE__ | |
634 | @interface NSBoolNumber : NSNumber { | |
635 | } | |
636 | @end | |
637 | #endif | |
638 | ||
639 | id CYNSObject(apr_pool_t *pool, JSContextRef context, JSValueRef value, bool cast) { | |
640 | id object; | |
641 | bool copy; | |
642 | ||
643 | switch (JSType type = JSValueGetType(context, value)) { | |
644 | case kJSTypeUndefined: | |
645 | object = [WebUndefined undefined]; | |
646 | copy = false; | |
647 | break; | |
648 | ||
649 | case kJSTypeNull: | |
650 | return NULL; | |
651 | break; | |
652 | ||
653 | case kJSTypeBoolean: | |
654 | #ifdef __APPLE__ | |
655 | object = (id) (CYCastBool(context, value) ? kCFBooleanTrue : kCFBooleanFalse); | |
656 | copy = false; | |
657 | #else | |
658 | object = [[NSBoolNumber alloc] initWithBool:CYCastBool(context, value)]; | |
659 | copy = true; | |
660 | #endif | |
661 | break; | |
662 | ||
663 | case kJSTypeNumber: | |
664 | object = CYCopyNSNumber(context, value); | |
665 | copy = true; | |
666 | break; | |
667 | ||
668 | case kJSTypeString: | |
669 | object = CYCopyNSString(context, value); | |
670 | copy = true; | |
671 | break; | |
672 | ||
673 | case kJSTypeObject: | |
674 | // XXX: this might could be more efficient | |
675 | object = CYCastNSObject(pool, context, (JSObjectRef) value); | |
676 | copy = false; | |
677 | break; | |
678 | ||
679 | default: | |
680 | throw CYJSError(context, "JSValueGetType() == 0x%x", type); | |
681 | break; | |
682 | } | |
683 | ||
684 | if (cast != copy) | |
685 | return object; | |
686 | else if (copy) | |
687 | return CYPoolRelease(pool, object); | |
688 | else | |
689 | return [object retain]; | |
690 | } | |
691 | ||
692 | NSObject *CYCastNSObject(apr_pool_t *pool, JSContextRef context, JSValueRef value) { | |
693 | return CYNSObject(pool, context, value, true); | |
694 | } | |
695 | ||
696 | NSObject *CYCopyNSObject(apr_pool_t *pool, JSContextRef context, JSValueRef value) { | |
697 | return CYNSObject(pool, context, value, false); | |
698 | } | |
699 | ||
700 | /* Bridge: NSArray {{{ */ | |
701 | @implementation NSArray (Cycript) | |
702 | ||
703 | - (NSString *) cy$toCYON { | |
704 | NSMutableString *json([[[NSMutableString alloc] init] autorelease]); | |
705 | [json appendString:@"["]; | |
706 | ||
707 | bool comma(false); | |
708 | #ifdef __APPLE__ | |
709 | for (id object in self) { | |
710 | #else | |
711 | for (size_t index(0), count([self count]); index != count; ++index) { | |
712 | id object([self objectAtIndex:index]); | |
713 | #endif | |
714 | if (comma) | |
715 | [json appendString:@","]; | |
716 | else | |
717 | comma = true; | |
718 | if (object == nil || [object cy$JSType] != kJSTypeUndefined) | |
719 | [json appendString:CYCastNSCYON(object)]; | |
720 | else { | |
721 | [json appendString:@","]; | |
722 | comma = false; | |
723 | } | |
724 | } | |
725 | ||
726 | [json appendString:@"]"]; | |
727 | return json; | |
728 | } | |
729 | ||
730 | - (bool) cy$hasProperty:(NSString *)name { | |
731 | if ([name isEqualToString:@"length"]) | |
732 | return true; | |
733 | ||
734 | size_t index(CYGetIndex(name)); | |
735 | if (index == _not(size_t) || index >= [self count]) | |
736 | return [super cy$hasProperty:name]; | |
737 | else | |
738 | return true; | |
739 | } | |
740 | ||
741 | - (NSObject *) cy$getProperty:(NSString *)name { | |
742 | if ([name isEqualToString:@"length"]) { | |
743 | NSUInteger count([self count]); | |
744 | #ifdef __APPLE__ | |
745 | return [NSNumber numberWithUnsignedInteger:count]; | |
746 | #else | |
747 | return [NSNumber numberWithUnsignedInt:count]; | |
748 | #endif | |
749 | } | |
750 | ||
751 | size_t index(CYGetIndex(name)); | |
752 | if (index == _not(size_t) || index >= [self count]) | |
753 | return [super cy$getProperty:name]; | |
754 | else | |
755 | return [self objectAtIndex:index]; | |
756 | } | |
757 | ||
758 | - (void) cy$getPropertyNames:(JSPropertyNameAccumulatorRef)names inContext:(JSContextRef)context { | |
759 | [super cy$getPropertyNames:names inContext:context]; | |
760 | ||
761 | for (size_t index(0), count([self count]); index != count; ++index) { | |
762 | id object([self objectAtIndex:index]); | |
763 | if (object == nil || [object cy$JSType] != kJSTypeUndefined) { | |
764 | char name[32]; | |
765 | sprintf(name, "%zu", index); | |
766 | JSPropertyNameAccumulatorAddName(names, CYJSString(name)); | |
767 | } | |
768 | } | |
769 | } | |
770 | ||
771 | + (bool) cy$hasImplicitProperties { | |
772 | return false; | |
773 | } | |
774 | ||
775 | @end | |
776 | /* }}} */ | |
777 | /* Bridge: NSBoolNumber {{{ */ | |
778 | #ifndef __APPLE__ | |
779 | @implementation NSBoolNumber (Cycript) | |
780 | ||
781 | - (JSType) cy$JSType { | |
782 | return kJSTypeBoolean; | |
783 | } | |
784 | ||
785 | - (NSObject *) cy$toJSON:(NSString *)key { | |
786 | return self; | |
787 | } | |
788 | ||
789 | - (NSString *) cy$toCYON { | |
790 | return [self boolValue] ? @"true" : @"false"; | |
791 | } | |
792 | ||
793 | - (JSValueRef) cy$JSValueInContext:(JSContextRef)context { CYObjectiveTry_(context) { | |
794 | return CYCastJSValue(context, (bool) [self boolValue]); | |
795 | } CYObjectiveCatch } | |
796 | ||
797 | @end | |
798 | #endif | |
799 | /* }}} */ | |
800 | /* Bridge: NSDictionary {{{ */ | |
801 | @implementation NSDictionary (Cycript) | |
802 | ||
803 | - (NSString *) cy$toCYON { | |
804 | NSMutableString *json([[[NSMutableString alloc] init] autorelease]); | |
805 | [json appendString:@"{"]; | |
806 | ||
807 | bool comma(false); | |
808 | #ifdef __APPLE__ | |
809 | for (NSObject *key in self) { | |
810 | #else | |
811 | NSEnumerator *keys([self keyEnumerator]); | |
812 | while (NSObject *key = [keys nextObject]) { | |
813 | #endif | |
814 | if (comma) | |
815 | [json appendString:@","]; | |
816 | else | |
817 | comma = true; | |
818 | [json appendString:[key cy$toKey]]; | |
819 | [json appendString:@":"]; | |
820 | NSObject *object([self objectForKey:key]); | |
821 | [json appendString:CYCastNSCYON(object)]; | |
822 | } | |
823 | ||
824 | [json appendString:@"}"]; | |
825 | return json; | |
826 | } | |
827 | ||
828 | - (bool) cy$hasProperty:(NSString *)name { | |
829 | return [self objectForKey:name] != nil; | |
830 | } | |
831 | ||
832 | - (NSObject *) cy$getProperty:(NSString *)name { | |
833 | return [self objectForKey:name]; | |
834 | } | |
835 | ||
836 | - (void) cy$getPropertyNames:(JSPropertyNameAccumulatorRef)names inContext:(JSContextRef)context { | |
837 | [super cy$getPropertyNames:names inContext:context]; | |
838 | ||
839 | #ifdef __APPLE__ | |
840 | for (NSObject *key in self) { | |
841 | #else | |
842 | NSEnumerator *keys([self keyEnumerator]); | |
843 | while (NSObject *key = [keys nextObject]) { | |
844 | #endif | |
845 | JSPropertyNameAccumulatorAddName(names, CYJSString(context, key)); | |
846 | } | |
847 | } | |
848 | ||
849 | + (bool) cy$hasImplicitProperties { | |
850 | return false; | |
851 | } | |
852 | ||
853 | @end | |
854 | /* }}} */ | |
855 | /* Bridge: NSMutableArray {{{ */ | |
856 | @implementation NSMutableArray (Cycript) | |
857 | ||
858 | - (bool) cy$setProperty:(NSString *)name to:(NSObject *)value { | |
859 | if ([name isEqualToString:@"length"]) { | |
860 | // XXX: is this not intelligent? | |
861 | NSNumber *number(reinterpret_cast<NSNumber *>(value)); | |
862 | #ifdef __APPLE__ | |
863 | NSUInteger size([number unsignedIntegerValue]); | |
864 | #else | |
865 | NSUInteger size([number unsignedIntValue]); | |
866 | #endif | |
867 | NSUInteger count([self count]); | |
868 | if (size < count) | |
869 | [self removeObjectsInRange:NSMakeRange(size, count - size)]; | |
870 | else if (size != count) { | |
871 | WebUndefined *undefined([WebUndefined undefined]); | |
872 | for (size_t i(count); i != size; ++i) | |
873 | [self addObject:undefined]; | |
874 | } | |
875 | return true; | |
876 | } | |
877 | ||
878 | size_t index(CYGetIndex(name)); | |
879 | if (index == _not(size_t)) | |
880 | return [super cy$setProperty:name to:value]; | |
881 | ||
882 | id object(value ?: [NSNull null]); | |
883 | ||
884 | size_t count([self count]); | |
885 | if (index < count) | |
886 | [self replaceObjectAtIndex:index withObject:object]; | |
887 | else { | |
888 | if (index != count) { | |
889 | WebUndefined *undefined([WebUndefined undefined]); | |
890 | for (size_t i(count); i != index; ++i) | |
891 | [self addObject:undefined]; | |
892 | } | |
893 | ||
894 | [self addObject:object]; | |
895 | } | |
896 | ||
897 | return true; | |
898 | } | |
899 | ||
900 | - (bool) cy$deleteProperty:(NSString *)name { | |
901 | size_t index(CYGetIndex(name)); | |
902 | if (index == _not(size_t) || index >= [self count]) | |
903 | return [super cy$deleteProperty:name]; | |
904 | [self replaceObjectAtIndex:index withObject:[WebUndefined undefined]]; | |
905 | return true; | |
906 | } | |
907 | ||
908 | @end | |
909 | /* }}} */ | |
910 | /* Bridge: NSMutableDictionary {{{ */ | |
911 | @implementation NSMutableDictionary (Cycript) | |
912 | ||
913 | - (bool) cy$setProperty:(NSString *)name to:(NSObject *)value { | |
914 | [self setObject:(value ?: [NSNull null]) forKey:name]; | |
915 | return true; | |
916 | } | |
917 | ||
918 | - (bool) cy$deleteProperty:(NSString *)name { | |
919 | if ([self objectForKey:name] == nil) | |
920 | return false; | |
921 | else { | |
922 | [self removeObjectForKey:name]; | |
923 | return true; | |
924 | } | |
925 | } | |
926 | ||
927 | @end | |
928 | /* }}} */ | |
929 | /* Bridge: NSNumber {{{ */ | |
930 | @implementation NSNumber (Cycript) | |
931 | ||
932 | - (JSType) cy$JSType { | |
933 | #ifdef __APPLE__ | |
934 | // XXX: this just seems stupid | |
935 | if ([self class] == NSCFBoolean_) | |
936 | return kJSTypeBoolean; | |
937 | #endif | |
938 | return kJSTypeNumber; | |
939 | } | |
940 | ||
941 | - (NSObject *) cy$toJSON:(NSString *)key { | |
942 | return self; | |
943 | } | |
944 | ||
945 | - (NSString *) cy$toCYON { | |
946 | return [self cy$JSType] != kJSTypeBoolean ? [self stringValue] : [self boolValue] ? @"true" : @"false"; | |
947 | } | |
948 | ||
949 | - (JSValueRef) cy$JSValueInContext:(JSContextRef)context { CYObjectiveTry_(context) { | |
950 | return [self cy$JSType] != kJSTypeBoolean ? CYCastJSValue(context, [self doubleValue]) : CYCastJSValue(context, [self boolValue]); | |
951 | } CYObjectiveCatch } | |
952 | ||
953 | @end | |
954 | /* }}} */ | |
955 | /* Bridge: NSNull {{{ */ | |
956 | @implementation NSNull (Cycript) | |
957 | ||
958 | - (JSType) cy$JSType { | |
959 | return kJSTypeNull; | |
960 | } | |
961 | ||
962 | - (NSObject *) cy$toJSON:(NSString *)key { | |
963 | return self; | |
964 | } | |
965 | ||
966 | - (NSString *) cy$toCYON { | |
967 | return @"null"; | |
968 | } | |
969 | ||
970 | @end | |
971 | /* }}} */ | |
972 | /* Bridge: NSObject {{{ */ | |
973 | @implementation NSObject (Cycript) | |
974 | ||
975 | - (JSValueRef) cy$JSValueInContext:(JSContextRef)context { CYObjectiveTry_(context) { | |
976 | return CYMakeInstance(context, self, false); | |
977 | } CYObjectiveCatch } | |
978 | ||
979 | - (JSType) cy$JSType { | |
980 | return kJSTypeObject; | |
981 | } | |
982 | ||
983 | - (NSObject *) cy$toJSON:(NSString *)key { | |
984 | return [self description]; | |
985 | } | |
986 | ||
987 | - (NSString *) cy$toCYON { | |
988 | return [[self cy$toJSON:@""] cy$toCYON]; | |
989 | } | |
990 | ||
991 | - (NSString *) cy$toKey { | |
992 | return [self cy$toCYON]; | |
993 | } | |
994 | ||
995 | - (bool) cy$hasProperty:(NSString *)name { | |
996 | return false; | |
997 | } | |
998 | ||
999 | - (NSObject *) cy$getProperty:(NSString *)name { | |
1000 | return nil; | |
1001 | } | |
1002 | ||
1003 | - (bool) cy$setProperty:(NSString *)name to:(NSObject *)value { | |
1004 | return false; | |
1005 | } | |
1006 | ||
1007 | - (bool) cy$deleteProperty:(NSString *)name { | |
1008 | return false; | |
1009 | } | |
1010 | ||
1011 | - (void) cy$getPropertyNames:(JSPropertyNameAccumulatorRef)names inContext:(JSContextRef)context { | |
1012 | } | |
1013 | ||
1014 | + (bool) cy$hasImplicitProperties { | |
1015 | return true; | |
1016 | } | |
1017 | ||
1018 | @end | |
1019 | /* }}} */ | |
1020 | /* Bridge: NSProxy {{{ */ | |
1021 | @implementation NSProxy (Cycript) | |
1022 | ||
1023 | - (NSObject *) cy$toJSON:(NSString *)key { | |
1024 | return [self description]; | |
1025 | } | |
1026 | ||
1027 | - (NSString *) cy$toCYON { | |
1028 | return [[self cy$toJSON:@""] cy$toCYON]; | |
1029 | } | |
1030 | ||
1031 | @end | |
1032 | /* }}} */ | |
1033 | /* Bridge: NSString {{{ */ | |
1034 | @implementation NSString (Cycript) | |
1035 | ||
1036 | - (JSType) cy$JSType { | |
1037 | return kJSTypeString; | |
1038 | } | |
1039 | ||
1040 | - (NSObject *) cy$toJSON:(NSString *)key { | |
1041 | return self; | |
1042 | } | |
1043 | ||
1044 | - (NSString *) cy$toCYON { | |
1045 | std::ostringstream str; | |
1046 | CYUTF8String string(CYCastUTF8String(self)); | |
1047 | CYStringify(str, string.data, string.size); | |
1048 | std::string value(str.str()); | |
1049 | return CYCastNSString(NULL, CYUTF8String(value.c_str(), value.size())); | |
1050 | } | |
1051 | ||
1052 | - (NSString *) cy$toKey { | |
1053 | if (CYIsKey(CYCastUTF8String(self))) | |
1054 | return self; | |
1055 | return [self cy$toCYON]; | |
1056 | } | |
1057 | ||
1058 | - (bool) cy$hasProperty:(NSString *)name { | |
1059 | if ([name isEqualToString:@"length"]) | |
1060 | return true; | |
1061 | ||
1062 | size_t index(CYGetIndex(name)); | |
1063 | if (index == _not(size_t) || index >= [self length]) | |
1064 | return [super cy$hasProperty:name]; | |
1065 | else | |
1066 | return true; | |
1067 | } | |
1068 | ||
1069 | - (NSObject *) cy$getProperty:(NSString *)name { | |
1070 | if ([name isEqualToString:@"length"]) { | |
1071 | NSUInteger count([self length]); | |
1072 | #ifdef __APPLE__ | |
1073 | return [NSNumber numberWithUnsignedInteger:count]; | |
1074 | #else | |
1075 | return [NSNumber numberWithUnsignedInt:count]; | |
1076 | #endif | |
1077 | } | |
1078 | ||
1079 | size_t index(CYGetIndex(name)); | |
1080 | if (index == _not(size_t) || index >= [self length]) | |
1081 | return [super cy$getProperty:name]; | |
1082 | else | |
1083 | return [self substringWithRange:NSMakeRange(index, 1)]; | |
1084 | } | |
1085 | ||
1086 | - (void) cy$getPropertyNames:(JSPropertyNameAccumulatorRef)names inContext:(JSContextRef)context { | |
1087 | [super cy$getPropertyNames:names inContext:context]; | |
1088 | ||
1089 | for (size_t index(0), length([self length]); index != length; ++index) { | |
1090 | char name[32]; | |
1091 | sprintf(name, "%zu", index); | |
1092 | JSPropertyNameAccumulatorAddName(names, CYJSString(name)); | |
1093 | } | |
1094 | } | |
1095 | ||
1096 | // XXX: this might be overly restrictive for NSString; I think I need a half-way between /injecting/ implicit properties and /accepting/ implicit properties | |
1097 | + (bool) cy$hasImplicitProperties { | |
1098 | return false; | |
1099 | } | |
1100 | ||
1101 | @end | |
1102 | /* }}} */ | |
1103 | /* Bridge: WebUndefined {{{ */ | |
1104 | @implementation WebUndefined (Cycript) | |
1105 | ||
1106 | - (JSType) cy$JSType { | |
1107 | return kJSTypeUndefined; | |
1108 | } | |
1109 | ||
1110 | - (NSObject *) cy$toJSON:(NSString *)key { | |
1111 | return self; | |
1112 | } | |
1113 | ||
1114 | - (NSString *) cy$toCYON { | |
1115 | return @"undefined"; | |
1116 | } | |
1117 | ||
1118 | - (JSValueRef) cy$JSValueInContext:(JSContextRef)context { CYObjectiveTry_(context) { | |
1119 | return CYJSUndefined(context); | |
1120 | } CYObjectiveCatch } | |
1121 | ||
1122 | @end | |
1123 | /* }}} */ | |
1124 | ||
1125 | static bool CYIsClass(id self) { | |
1126 | #ifdef __APPLE__ | |
1127 | // XXX: this is a lame object_isClass | |
1128 | return class_getInstanceMethod(object_getClass(self), @selector(alloc)) != NULL; | |
1129 | #else | |
1130 | return GSObjCIsClass(self); | |
1131 | #endif | |
1132 | } | |
1133 | ||
1134 | Class CYCastClass(apr_pool_t *pool, JSContextRef context, JSValueRef value) { | |
1135 | id self(CYCastNSObject(pool, context, value)); | |
1136 | if (CYIsClass(self)) | |
1137 | return (Class) self; | |
1138 | throw CYJSError(context, "got something that is not a Class"); | |
1139 | return NULL; | |
1140 | } | |
1141 | ||
1142 | NSArray *CYCastNSArray(JSContextRef context, JSPropertyNameArrayRef names) { | |
1143 | CYPool pool; | |
1144 | size_t size(JSPropertyNameArrayGetCount(names)); | |
1145 | NSMutableArray *array([NSMutableArray arrayWithCapacity:size]); | |
1146 | for (size_t index(0); index != size; ++index) | |
1147 | [array addObject:CYCastNSString(pool, context, JSPropertyNameArrayGetNameAtIndex(names, index))]; | |
1148 | return array; | |
1149 | } | |
1150 | ||
1151 | JSValueRef CYCastJSValue(JSContextRef context, NSObject *value) { CYPoolTry { | |
1152 | if (value == nil) | |
1153 | return CYJSNull(context); | |
1154 | else if ([value respondsToSelector:@selector(cy$JSValueInContext:)]) | |
1155 | return [value cy$JSValueInContext:context]; | |
1156 | else | |
1157 | return CYMakeInstance(context, value, false); | |
1158 | } CYPoolCatch(NULL) return /*XXX*/ NULL; } | |
1159 | ||
1160 | @implementation CYJSObject | |
1161 | ||
1162 | - (id) initWithJSObject:(JSObjectRef)object inContext:(JSContextRef)context { CYObjectiveTry { | |
1163 | if ((self = [super init]) != nil) { | |
1164 | object_ = object; | |
1165 | context_ = CYGetJSContext(context); | |
1166 | //XXX:JSGlobalContextRetain(context_); | |
1167 | JSValueProtect(context_, object_); | |
1168 | } return self; | |
1169 | } CYObjectiveCatch } | |
1170 | ||
1171 | - (void) dealloc { CYObjectiveTry { | |
1172 | JSValueUnprotect(context_, object_); | |
1173 | //XXX:JSGlobalContextRelease(context_); | |
1174 | [super dealloc]; | |
1175 | } CYObjectiveCatch } | |
1176 | ||
1177 | - (NSObject *) cy$toJSON:(NSString *)key { CYObjectiveTry { | |
1178 | JSValueRef toJSON(CYGetProperty(context_, object_, toJSON_s)); | |
1179 | if (!CYIsCallable(context_, toJSON)) | |
1180 | return [super cy$toJSON:key]; | |
1181 | else { | |
1182 | JSValueRef arguments[1] = {CYCastJSValue(context_, key)}; | |
1183 | JSValueRef value(CYCallAsFunction(context_, (JSObjectRef) toJSON, object_, 1, arguments)); | |
1184 | // XXX: do I really want an NSNull here?! | |
1185 | return CYCastNSObject(NULL, context_, value) ?: [NSNull null]; | |
1186 | } | |
1187 | } CYObjectiveCatch } | |
1188 | ||
1189 | - (NSString *) cy$toCYON { CYObjectiveTry { | |
1190 | CYPool pool; | |
1191 | JSValueRef exception(NULL); | |
1192 | const char *cyon(CYPoolCCYON(pool, context_, object_)); | |
1193 | CYThrow(context_, exception); | |
1194 | if (cyon == NULL) | |
1195 | return [super cy$toCYON]; | |
1196 | else | |
1197 | return [NSString stringWithUTF8String:cyon]; | |
1198 | } CYObjectiveCatch } | |
1199 | ||
1200 | - (NSUInteger) count { CYObjectiveTry { | |
1201 | JSPropertyNameArrayRef names(JSObjectCopyPropertyNames(context_, object_)); | |
1202 | size_t size(JSPropertyNameArrayGetCount(names)); | |
1203 | JSPropertyNameArrayRelease(names); | |
1204 | return size; | |
1205 | } CYObjectiveCatch } | |
1206 | ||
1207 | - (id) objectForKey:(id)key { CYObjectiveTry { | |
1208 | JSValueRef value(CYGetProperty(context_, object_, CYJSString(context_, (NSObject *) key))); | |
1209 | if (JSValueIsUndefined(context_, value)) | |
1210 | return nil; | |
1211 | return CYCastNSObject(NULL, context_, value) ?: [NSNull null]; | |
1212 | } CYObjectiveCatch } | |
1213 | ||
1214 | - (NSEnumerator *) keyEnumerator { CYObjectiveTry { | |
1215 | JSPropertyNameArrayRef names(JSObjectCopyPropertyNames(context_, object_)); | |
1216 | NSEnumerator *enumerator([CYCastNSArray(context_, names) objectEnumerator]); | |
1217 | JSPropertyNameArrayRelease(names); | |
1218 | return enumerator; | |
1219 | } CYObjectiveCatch } | |
1220 | ||
1221 | - (void) setObject:(id)object forKey:(id)key { CYObjectiveTry { | |
1222 | CYSetProperty(context_, object_, CYJSString(context_, (NSObject *) key), CYCastJSValue(context_, (NSString *) object)); | |
1223 | } CYObjectiveCatch } | |
1224 | ||
1225 | - (void) removeObjectForKey:(id)key { CYObjectiveTry { | |
1226 | JSValueRef exception(NULL); | |
1227 | (void) JSObjectDeleteProperty(context_, object_, CYJSString(context_, (NSObject *) key), &exception); | |
1228 | CYThrow(context_, exception); | |
1229 | } CYObjectiveCatch } | |
1230 | ||
1231 | @end | |
1232 | ||
1233 | @implementation CYJSArray | |
1234 | ||
1235 | - (id) initWithJSObject:(JSObjectRef)object inContext:(JSContextRef)context { CYObjectiveTry { | |
1236 | if ((self = [super init]) != nil) { | |
1237 | object_ = object; | |
1238 | context_ = CYGetJSContext(context); | |
1239 | //XXX:JSGlobalContextRetain(context_); | |
1240 | JSValueProtect(context_, object_); | |
1241 | } return self; | |
1242 | } CYObjectiveCatch } | |
1243 | ||
1244 | - (void) dealloc { CYObjectiveTry { | |
1245 | JSValueUnprotect(context_, object_); | |
1246 | //XXX:JSGlobalContextRelease(context_); | |
1247 | [super dealloc]; | |
1248 | } CYObjectiveCatch } | |
1249 | ||
1250 | - (NSUInteger) count { CYObjectiveTry { | |
1251 | return CYCastDouble(context_, CYGetProperty(context_, object_, length_s)); | |
1252 | } CYObjectiveCatch } | |
1253 | ||
1254 | - (id) objectAtIndex:(NSUInteger)index { CYObjectiveTry { | |
1255 | size_t bounds([self count]); | |
1256 | if (index >= bounds) | |
1257 | @throw [NSException exceptionWithName:NSRangeException reason:[NSString stringWithFormat:@"*** -[CYJSArray objectAtIndex:]: index (%zu) beyond bounds (%zu)", index, bounds] userInfo:nil]; | |
1258 | JSValueRef exception(NULL); | |
1259 | JSValueRef value(JSObjectGetPropertyAtIndex(context_, object_, index, &exception)); | |
1260 | CYThrow(context_, exception); | |
1261 | return CYCastNSObject(NULL, context_, value) ?: [NSNull null]; | |
1262 | } CYObjectiveCatch } | |
1263 | ||
1264 | - (void) addObject:(id)object { CYObjectiveTry { | |
1265 | JSValueRef exception(NULL); | |
1266 | JSValueRef arguments[1]; | |
1267 | arguments[0] = CYCastJSValue(context_, (NSObject *) object); | |
1268 | JSObjectRef Array(CYGetCachedObject(context_, Array_s)); | |
1269 | JSObjectCallAsFunction(context_, CYCastJSObject(context_, CYGetProperty(context_, Array, push_s)), object_, 1, arguments, &exception); | |
1270 | CYThrow(context_, exception); | |
1271 | } CYObjectiveCatch } | |
1272 | ||
1273 | - (void) insertObject:(id)object atIndex:(NSUInteger)index { CYObjectiveTry { | |
1274 | size_t bounds([self count] + 1); | |
1275 | if (index >= bounds) | |
1276 | @throw [NSException exceptionWithName:NSRangeException reason:[NSString stringWithFormat:@"*** -[CYJSArray insertObject:atIndex:]: index (%zu) beyond bounds (%zu)", index, bounds] userInfo:nil]; | |
1277 | JSValueRef exception(NULL); | |
1278 | JSValueRef arguments[3]; | |
1279 | arguments[0] = CYCastJSValue(context_, index); | |
1280 | arguments[1] = CYCastJSValue(context_, 0); | |
1281 | arguments[2] = CYCastJSValue(context_, (NSObject *) object); | |
1282 | JSObjectRef Array(CYGetCachedObject(context_, Array_s)); | |
1283 | JSObjectCallAsFunction(context_, CYCastJSObject(context_, CYGetProperty(context_, Array, splice_s)), object_, 3, arguments, &exception); | |
1284 | CYThrow(context_, exception); | |
1285 | } CYObjectiveCatch } | |
1286 | ||
1287 | - (void) removeLastObject { CYObjectiveTry { | |
1288 | JSValueRef exception(NULL); | |
1289 | JSObjectRef Array(CYGetCachedObject(context_, Array_s)); | |
1290 | JSObjectCallAsFunction(context_, CYCastJSObject(context_, CYGetProperty(context_, Array, pop_s)), object_, 0, NULL, &exception); | |
1291 | CYThrow(context_, exception); | |
1292 | } CYObjectiveCatch } | |
1293 | ||
1294 | - (void) removeObjectAtIndex:(NSUInteger)index { CYObjectiveTry { | |
1295 | size_t bounds([self count]); | |
1296 | if (index >= bounds) | |
1297 | @throw [NSException exceptionWithName:NSRangeException reason:[NSString stringWithFormat:@"*** -[CYJSArray removeObjectAtIndex:]: index (%zu) beyond bounds (%zu)", index, bounds] userInfo:nil]; | |
1298 | JSValueRef exception(NULL); | |
1299 | JSValueRef arguments[2]; | |
1300 | arguments[0] = CYCastJSValue(context_, index); | |
1301 | arguments[1] = CYCastJSValue(context_, 1); | |
1302 | JSObjectRef Array(CYGetCachedObject(context_, Array_s)); | |
1303 | JSObjectCallAsFunction(context_, CYCastJSObject(context_, CYGetProperty(context_, Array, splice_s)), object_, 2, arguments, &exception); | |
1304 | CYThrow(context_, exception); | |
1305 | } CYObjectiveCatch } | |
1306 | ||
1307 | - (void) replaceObjectAtIndex:(NSUInteger)index withObject:(id)object { CYObjectiveTry { | |
1308 | size_t bounds([self count]); | |
1309 | if (index >= bounds) | |
1310 | @throw [NSException exceptionWithName:NSRangeException reason:[NSString stringWithFormat:@"*** -[CYJSArray replaceObjectAtIndex:withObject:]: index (%zu) beyond bounds (%zu)", index, bounds] userInfo:nil]; | |
1311 | CYSetProperty(context_, object_, index, CYCastJSValue(context_, (NSObject *) object)); | |
1312 | } CYObjectiveCatch } | |
1313 | ||
1314 | @end | |
1315 | ||
1316 | // XXX: use objc_getAssociatedObject and objc_setAssociatedObject on 10.6 | |
1317 | struct CYInternal : | |
1318 | CYData | |
1319 | { | |
1320 | JSObjectRef object_; | |
1321 | ||
1322 | CYInternal() : | |
1323 | object_(NULL) | |
1324 | { | |
1325 | } | |
1326 | ||
1327 | ~CYInternal() { | |
1328 | // XXX: delete object_? ;( | |
1329 | } | |
1330 | ||
1331 | static CYInternal *Get(id self) { | |
1332 | CYInternal *internal(NULL); | |
1333 | if (object_getInstanceVariable(self, "cy$internal_", reinterpret_cast<void **>(&internal)) == NULL) { | |
1334 | // XXX: do something epic? ;P | |
1335 | } | |
1336 | ||
1337 | return internal; | |
1338 | } | |
1339 | ||
1340 | static CYInternal *Set(id self) { | |
1341 | CYInternal *internal(NULL); | |
1342 | if (objc_ivar *ivar = object_getInstanceVariable(self, "cy$internal_", reinterpret_cast<void **>(&internal))) { | |
1343 | if (internal == NULL) { | |
1344 | internal = new CYInternal(); | |
1345 | object_setIvar(self, ivar, reinterpret_cast<id>(internal)); | |
1346 | } | |
1347 | } else { | |
1348 | // XXX: do something epic? ;P | |
1349 | } | |
1350 | ||
1351 | return internal; | |
1352 | } | |
1353 | ||
1354 | bool HasProperty(JSContextRef context, JSStringRef name) { | |
1355 | if (object_ == NULL) | |
1356 | return false; | |
1357 | return JSObjectHasProperty(context, object_, name); | |
1358 | } | |
1359 | ||
1360 | JSValueRef GetProperty(JSContextRef context, JSStringRef name) { | |
1361 | if (object_ == NULL) | |
1362 | return NULL; | |
1363 | return CYGetProperty(context, object_, name); | |
1364 | } | |
1365 | ||
1366 | void SetProperty(JSContextRef context, JSStringRef name, JSValueRef value) { | |
1367 | if (object_ == NULL) | |
1368 | object_ = JSObjectMake(context, NULL, NULL); | |
1369 | CYSetProperty(context, object_, name, value); | |
1370 | } | |
1371 | }; | |
1372 | ||
1373 | static JSObjectRef CYMakeSelector(JSContextRef context, SEL sel) { | |
1374 | Selector_privateData *internal(new Selector_privateData(sel)); | |
1375 | return JSObjectMake(context, Selector_, internal); | |
1376 | } | |
1377 | ||
1378 | static SEL CYCastSEL(JSContextRef context, JSValueRef value) { | |
1379 | if (JSValueIsObjectOfClass(context, value, Selector_)) { | |
1380 | Selector_privateData *internal(reinterpret_cast<Selector_privateData *>(JSObjectGetPrivate((JSObjectRef) value))); | |
1381 | return reinterpret_cast<SEL>(internal->value_); | |
1382 | } else | |
1383 | return CYCastPointer<SEL>(context, value); | |
1384 | } | |
1385 | ||
1386 | void *CYObjectiveC_ExecuteStart(JSContextRef context) { CYSadTry { | |
1387 | return (void *) [[NSAutoreleasePool alloc] init]; | |
1388 | } CYSadCatch(NULL) } | |
1389 | ||
1390 | void CYObjectiveC_ExecuteEnd(JSContextRef context, void *handle) { CYSadTry { | |
1391 | return [(NSAutoreleasePool *) handle release]; | |
1392 | } CYSadCatch() } | |
1393 | ||
1394 | JSValueRef CYObjectiveC_RuntimeProperty(JSContextRef context, CYUTF8String name) { CYPoolTry { | |
1395 | if (name == "nil") | |
1396 | return Instance::Make(context, nil); | |
1397 | if (Class _class = objc_getClass(name.data)) | |
1398 | return CYMakeInstance(context, _class, true); | |
1399 | if (Protocol *protocol = objc_getProtocol(name.data)) | |
1400 | return CYMakeInstance(context, protocol, true); | |
1401 | return NULL; | |
1402 | } CYPoolCatch(NULL) return /*XXX*/ NULL; } | |
1403 | ||
1404 | static void CYObjectiveC_CallFunction(JSContextRef context, ffi_cif *cif, void (*function)(), uint8_t *value, void **values) { CYSadTry { | |
1405 | ffi_call(cif, function, value, values); | |
1406 | } CYSadCatch() } | |
1407 | ||
1408 | static bool CYObjectiveC_PoolFFI(apr_pool_t *pool, JSContextRef context, sig::Type *type, ffi_type *ffi, void *data, JSValueRef value) { CYSadTry { | |
1409 | switch (type->primitive) { | |
1410 | // XXX: do something epic about blocks | |
1411 | case sig::block_P: | |
1412 | case sig::object_P: | |
1413 | case sig::typename_P: | |
1414 | *reinterpret_cast<id *>(data) = CYCastNSObject(pool, context, value); | |
1415 | break; | |
1416 | ||
1417 | case sig::selector_P: | |
1418 | *reinterpret_cast<SEL *>(data) = CYCastSEL(context, value); | |
1419 | break; | |
1420 | ||
1421 | default: | |
1422 | return false; | |
1423 | } | |
1424 | ||
1425 | return true; | |
1426 | } CYSadCatch(false) } | |
1427 | ||
1428 | static JSValueRef CYObjectiveC_FromFFI(JSContextRef context, sig::Type *type, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) { CYPoolTry { | |
1429 | switch (type->primitive) { | |
1430 | // XXX: do something epic about blocks | |
1431 | case sig::block_P: | |
1432 | case sig::object_P: | |
1433 | if (NSObject *object = *reinterpret_cast<NSObject **>(data)) { | |
1434 | JSValueRef value(CYCastJSValue(context, object)); | |
1435 | if (initialize) | |
1436 | [object release]; | |
1437 | return value; | |
1438 | } else goto null; | |
1439 | ||
1440 | case sig::typename_P: | |
1441 | return CYMakeInstance(context, *reinterpret_cast<Class *>(data), true); | |
1442 | ||
1443 | case sig::selector_P: | |
1444 | if (SEL sel = *reinterpret_cast<SEL *>(data)) | |
1445 | return CYMakeSelector(context, sel); | |
1446 | else goto null; | |
1447 | ||
1448 | null: | |
1449 | return CYJSNull(context); | |
1450 | default: | |
1451 | return NULL; | |
1452 | } | |
1453 | } CYPoolCatch(NULL) return /*XXX*/ NULL; } | |
1454 | ||
1455 | static bool CYImplements(id object, Class _class, SEL selector, bool devoid) { | |
1456 | if (objc_method *method = class_getInstanceMethod(_class, selector)) { | |
1457 | if (!devoid) | |
1458 | return true; | |
1459 | #if OBJC_API_VERSION >= 2 | |
1460 | char type[16]; | |
1461 | method_getReturnType(method, type, sizeof(type)); | |
1462 | #else | |
1463 | const char *type(method_getTypeEncoding(method)); | |
1464 | #endif | |
1465 | if (type[0] != 'v') | |
1466 | return true; | |
1467 | } | |
1468 | ||
1469 | // XXX: possibly use a more "awesome" check? | |
1470 | return false; | |
1471 | } | |
1472 | ||
1473 | static const char *CYPoolTypeEncoding(apr_pool_t *pool, JSContextRef context, SEL sel, objc_method *method) { | |
1474 | if (method != NULL) | |
1475 | return method_getTypeEncoding(method); | |
1476 | ||
1477 | const char *name(sel_getName(sel)); | |
1478 | size_t length(strlen(name)); | |
1479 | ||
1480 | char keyed[length + 2]; | |
1481 | keyed[0] = '6'; | |
1482 | keyed[length + 1] = '\0'; | |
1483 | memcpy(keyed + 1, name, length); | |
1484 | ||
1485 | if (CYBridgeEntry *entry = CYBridgeHash(keyed, length + 1)) | |
1486 | return entry->value_; | |
1487 | ||
1488 | return NULL; | |
1489 | } | |
1490 | ||
1491 | static void MessageClosure_(ffi_cif *cif, void *result, void **arguments, void *arg) { | |
1492 | Closure_privateData *internal(reinterpret_cast<Closure_privateData *>(arg)); | |
1493 | ||
1494 | JSContextRef context(internal->context_); | |
1495 | ||
1496 | size_t count(internal->cif_.nargs); | |
1497 | JSValueRef values[count]; | |
1498 | ||
1499 | for (size_t index(0); index != count; ++index) | |
1500 | values[index] = CYFromFFI(context, internal->signature_.elements[1 + index].type, internal->cif_.arg_types[index], arguments[index]); | |
1501 | ||
1502 | JSObjectRef _this(CYCastJSObject(context, values[0])); | |
1503 | ||
1504 | JSValueRef value(CYCallAsFunction(context, internal->function_, _this, count - 2, values + 2)); | |
1505 | CYPoolFFI(NULL, context, internal->signature_.elements[0].type, internal->cif_.rtype, result, value); | |
1506 | } | |
1507 | ||
1508 | static JSObjectRef CYMakeMessage(JSContextRef context, SEL sel, IMP imp, const char *type) { | |
1509 | Message_privateData *internal(new Message_privateData(sel, type, imp)); | |
1510 | return JSObjectMake(context, Message_, internal); | |
1511 | } | |
1512 | ||
1513 | static IMP CYMakeMessage(JSContextRef context, JSValueRef value, const char *type) { | |
1514 | JSObjectRef function(CYCastJSObject(context, value)); | |
1515 | Closure_privateData *internal(CYMakeFunctor_(context, function, type, &MessageClosure_)); | |
1516 | // XXX: see notes in Library.cpp about needing to leak | |
1517 | return reinterpret_cast<IMP>(internal->GetValue()); | |
1518 | } | |
1519 | ||
1520 | static bool Messages_hasProperty(JSContextRef context, JSObjectRef object, JSStringRef property) { | |
1521 | Messages *internal(reinterpret_cast<Messages *>(JSObjectGetPrivate(object))); | |
1522 | Class _class(internal->GetValue()); | |
1523 | ||
1524 | CYPool pool; | |
1525 | const char *name(CYPoolCString(pool, context, property)); | |
1526 | ||
1527 | if (SEL sel = sel_getUid(name)) | |
1528 | if (class_getInstanceMethod(_class, sel) != NULL) | |
1529 | return true; | |
1530 | ||
1531 | return false; | |
1532 | } | |
1533 | ||
1534 | static JSValueRef Messages_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { | |
1535 | Messages *internal(reinterpret_cast<Messages *>(JSObjectGetPrivate(object))); | |
1536 | Class _class(internal->GetValue()); | |
1537 | ||
1538 | CYPool pool; | |
1539 | const char *name(CYPoolCString(pool, context, property)); | |
1540 | ||
1541 | if (SEL sel = sel_getUid(name)) | |
1542 | if (objc_method *method = class_getInstanceMethod(_class, sel)) | |
1543 | return CYMakeMessage(context, sel, method_getImplementation(method), method_getTypeEncoding(method)); | |
1544 | ||
1545 | return NULL; | |
1546 | } | |
1547 | ||
1548 | static bool Messages_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) { | |
1549 | Messages *internal(reinterpret_cast<Messages *>(JSObjectGetPrivate(object))); | |
1550 | Class _class(internal->GetValue()); | |
1551 | ||
1552 | CYPool pool; | |
1553 | const char *name(CYPoolCString(pool, context, property)); | |
1554 | ||
1555 | SEL sel(sel_registerName(name)); | |
1556 | ||
1557 | objc_method *method(class_getInstanceMethod(_class, sel)); | |
1558 | ||
1559 | const char *type; | |
1560 | IMP imp; | |
1561 | ||
1562 | if (JSValueIsObjectOfClass(context, value, Message_)) { | |
1563 | Message_privateData *message(reinterpret_cast<Message_privateData *>(JSObjectGetPrivate((JSObjectRef) value))); | |
1564 | type = sig::Unparse(pool, &message->signature_); | |
1565 | imp = reinterpret_cast<IMP>(message->GetValue()); | |
1566 | } else { | |
1567 | type = CYPoolTypeEncoding(pool, context, sel, method); | |
1568 | imp = CYMakeMessage(context, value, type); | |
1569 | } | |
1570 | ||
1571 | if (method != NULL) | |
1572 | method_setImplementation(method, imp); | |
1573 | else { | |
1574 | #ifdef GNU_RUNTIME | |
1575 | GSMethodList list(GSAllocMethodList(1)); | |
1576 | GSAppendMethodToList(list, sel, type, imp, YES); | |
1577 | GSAddMethodList(_class, list, YES); | |
1578 | GSFlushMethodCacheForClass(_class); | |
1579 | #else | |
1580 | class_addMethod(_class, sel, imp, type); | |
1581 | #endif | |
1582 | } | |
1583 | ||
1584 | return true; | |
1585 | } | |
1586 | ||
1587 | #if 0 && OBJC_API_VERSION < 2 | |
1588 | static bool Messages_deleteProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { | |
1589 | Messages *internal(reinterpret_cast<Messages *>(JSObjectGetPrivate(object))); | |
1590 | Class _class(internal->GetValue()); | |
1591 | ||
1592 | CYPool pool; | |
1593 | const char *name(CYPoolCString(pool, context, property)); | |
1594 | ||
1595 | if (SEL sel = sel_getUid(name)) | |
1596 | if (objc_method *method = class_getInstanceMethod(_class, sel)) { | |
1597 | objc_method_list list = {NULL, 1, {method}}; | |
1598 | class_removeMethods(_class, &list); | |
1599 | return true; | |
1600 | } | |
1601 | ||
1602 | return false; | |
1603 | } | |
1604 | #endif | |
1605 | ||
1606 | static void Messages_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) { | |
1607 | Messages *internal(reinterpret_cast<Messages *>(JSObjectGetPrivate(object))); | |
1608 | Class _class(internal->GetValue()); | |
1609 | ||
1610 | #if OBJC_API_VERSION >= 2 | |
1611 | unsigned int size; | |
1612 | objc_method **data(class_copyMethodList(_class, &size)); | |
1613 | for (size_t i(0); i != size; ++i) | |
1614 | JSPropertyNameAccumulatorAddName(names, CYJSString(sel_getName(method_getName(data[i])))); | |
1615 | free(data); | |
1616 | #else | |
1617 | for (objc_method_list *methods(_class->methods); methods != NULL; methods = methods->method_next) | |
1618 | for (int i(0); i != methods->method_count; ++i) | |
1619 | JSPropertyNameAccumulatorAddName(names, CYJSString(sel_getName(method_getName(&methods->method_list[i])))); | |
1620 | #endif | |
1621 | } | |
1622 | ||
1623 | static bool CYHasImplicitProperties(Class _class) { | |
1624 | // XXX: this is an evil hack to deal with NSProxy; fix elsewhere | |
1625 | if (!CYImplements(_class, object_getClass(_class), @selector(cy$hasImplicitProperties), false)) | |
1626 | return true; | |
1627 | return [_class cy$hasImplicitProperties]; | |
1628 | } | |
1629 | ||
1630 | static bool Instance_hasProperty(JSContextRef context, JSObjectRef object, JSStringRef property) { | |
1631 | Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object))); | |
1632 | id self(internal->GetValue()); | |
1633 | ||
1634 | if (JSStringIsEqualToUTF8CString(property, "$cyi")) | |
1635 | return true; | |
1636 | ||
1637 | CYPool pool; | |
1638 | NSString *name(CYCastNSString(pool, context, property)); | |
1639 | ||
1640 | if (CYInternal *internal = CYInternal::Get(self)) | |
1641 | if (internal->HasProperty(context, property)) | |
1642 | return true; | |
1643 | ||
1644 | Class _class(object_getClass(self)); | |
1645 | ||
1646 | CYPoolTry { | |
1647 | // XXX: this is an evil hack to deal with NSProxy; fix elsewhere | |
1648 | if (CYImplements(self, _class, @selector(cy$hasProperty:), false)) | |
1649 | if ([self cy$hasProperty:name]) | |
1650 | return true; | |
1651 | } CYPoolCatch(false) | |
1652 | ||
1653 | const char *string(CYPoolCString(pool, context, name)); | |
1654 | ||
1655 | #ifdef __APPLE__ | |
1656 | if (class_getProperty(_class, string) != NULL) | |
1657 | return true; | |
1658 | #endif | |
1659 | ||
1660 | if (CYHasImplicitProperties(_class)) | |
1661 | if (SEL sel = sel_getUid(string)) | |
1662 | if (CYImplements(self, _class, sel, true)) | |
1663 | return true; | |
1664 | ||
1665 | return false; | |
1666 | } | |
1667 | ||
1668 | static JSValueRef Instance_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry { | |
1669 | Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object))); | |
1670 | id self(internal->GetValue()); | |
1671 | ||
1672 | if (JSStringIsEqualToUTF8CString(property, "$cyi")) | |
1673 | return Internal::Make(context, self, object); | |
1674 | ||
1675 | CYPool pool; | |
1676 | NSString *name(CYCastNSString(pool, context, property)); | |
1677 | ||
1678 | if (CYInternal *internal = CYInternal::Get(self)) | |
1679 | if (JSValueRef value = internal->GetProperty(context, property)) | |
1680 | return value; | |
1681 | ||
1682 | CYPoolTry { | |
1683 | if (NSObject *data = [self cy$getProperty:name]) | |
1684 | return CYCastJSValue(context, data); | |
1685 | } CYPoolCatch(NULL) | |
1686 | ||
1687 | const char *string(CYPoolCString(pool, context, name)); | |
1688 | Class _class(object_getClass(self)); | |
1689 | ||
1690 | #ifdef __APPLE__ | |
1691 | if (objc_property_t property = class_getProperty(_class, string)) { | |
1692 | PropertyAttributes attributes(property); | |
1693 | SEL sel(sel_registerName(attributes.Getter())); | |
1694 | return CYSendMessage(pool, context, self, NULL, sel, 0, NULL, false, exception); | |
1695 | } | |
1696 | #endif | |
1697 | ||
1698 | if (CYHasImplicitProperties(_class)) | |
1699 | if (SEL sel = sel_getUid(string)) | |
1700 | if (CYImplements(self, _class, sel, true)) | |
1701 | return CYSendMessage(pool, context, self, NULL, sel, 0, NULL, false, exception); | |
1702 | ||
1703 | return NULL; | |
1704 | } CYCatch } | |
1705 | ||
1706 | static bool Instance_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) { CYTry { | |
1707 | Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object))); | |
1708 | id self(internal->GetValue()); | |
1709 | ||
1710 | CYPool pool; | |
1711 | ||
1712 | NSString *name(CYCastNSString(pool, context, property)); | |
1713 | NSObject *data(CYCastNSObject(pool, context, value)); | |
1714 | ||
1715 | CYPoolTry { | |
1716 | if ([self cy$setProperty:name to:data]) | |
1717 | return true; | |
1718 | } CYPoolCatch(NULL) | |
1719 | ||
1720 | const char *string(CYPoolCString(pool, context, name)); | |
1721 | Class _class(object_getClass(self)); | |
1722 | ||
1723 | #ifdef __APPLE__ | |
1724 | if (objc_property_t property = class_getProperty(_class, string)) { | |
1725 | PropertyAttributes attributes(property); | |
1726 | if (const char *setter = attributes.Setter()) { | |
1727 | SEL sel(sel_registerName(setter)); | |
1728 | JSValueRef arguments[1] = {value}; | |
1729 | CYSendMessage(pool, context, self, NULL, sel, 1, arguments, false, exception); | |
1730 | return true; | |
1731 | } | |
1732 | } | |
1733 | #endif | |
1734 | ||
1735 | size_t length(strlen(string)); | |
1736 | ||
1737 | char set[length + 5]; | |
1738 | ||
1739 | set[0] = 's'; | |
1740 | set[1] = 'e'; | |
1741 | set[2] = 't'; | |
1742 | ||
1743 | if (string[0] != '\0') { | |
1744 | set[3] = toupper(string[0]); | |
1745 | memcpy(set + 4, string + 1, length - 1); | |
1746 | } | |
1747 | ||
1748 | set[length + 3] = ':'; | |
1749 | set[length + 4] = '\0'; | |
1750 | ||
1751 | if (SEL sel = sel_getUid(set)) | |
1752 | if (CYImplements(self, _class, sel, false)) { | |
1753 | JSValueRef arguments[1] = {value}; | |
1754 | CYSendMessage(pool, context, self, NULL, sel, 1, arguments, false, exception); | |
1755 | } | |
1756 | ||
1757 | if (CYInternal *internal = CYInternal::Set(self)) { | |
1758 | internal->SetProperty(context, property, value); | |
1759 | return true; | |
1760 | } | |
1761 | ||
1762 | return false; | |
1763 | } CYCatch } | |
1764 | ||
1765 | static bool Instance_deleteProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry { | |
1766 | Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object))); | |
1767 | id self(internal->GetValue()); | |
1768 | ||
1769 | CYPoolTry { | |
1770 | NSString *name(CYCastNSString(NULL, context, property)); | |
1771 | return [self cy$deleteProperty:name]; | |
1772 | } CYPoolCatch(NULL) | |
1773 | } CYCatch return /*XXX*/ NULL; } | |
1774 | ||
1775 | static void Instance_getPropertyNames_message(JSPropertyNameAccumulatorRef names, objc_method *method) { | |
1776 | const char *name(sel_getName(method_getName(method))); | |
1777 | if (strchr(name, ':') != NULL) | |
1778 | return; | |
1779 | ||
1780 | const char *type(method_getTypeEncoding(method)); | |
1781 | if (type == NULL || *type == '\0' || *type == 'v') | |
1782 | return; | |
1783 | ||
1784 | JSPropertyNameAccumulatorAddName(names, CYJSString(name)); | |
1785 | } | |
1786 | ||
1787 | static void Instance_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) { | |
1788 | Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object))); | |
1789 | id self(internal->GetValue()); | |
1790 | ||
1791 | CYPool pool; | |
1792 | Class _class(object_getClass(self)); | |
1793 | ||
1794 | #ifdef __APPLE__ | |
1795 | { | |
1796 | unsigned int size; | |
1797 | objc_property_t *data(class_copyPropertyList(_class, &size)); | |
1798 | for (size_t i(0); i != size; ++i) | |
1799 | JSPropertyNameAccumulatorAddName(names, CYJSString(property_getName(data[i]))); | |
1800 | free(data); | |
1801 | } | |
1802 | #endif | |
1803 | ||
1804 | if (CYHasImplicitProperties(_class)) | |
1805 | for (Class current(_class); current != nil; current = class_getSuperclass(current)) { | |
1806 | #if OBJC_API_VERSION >= 2 | |
1807 | unsigned int size; | |
1808 | objc_method **data(class_copyMethodList(current, &size)); | |
1809 | for (size_t i(0); i != size; ++i) | |
1810 | Instance_getPropertyNames_message(names, data[i]); | |
1811 | free(data); | |
1812 | #else | |
1813 | for (objc_method_list *methods(current->methods); methods != NULL; methods = methods->method_next) | |
1814 | for (int i(0); i != methods->method_count; ++i) | |
1815 | Instance_getPropertyNames_message(names, &methods->method_list[i]); | |
1816 | #endif | |
1817 | } | |
1818 | ||
1819 | CYPoolTry { | |
1820 | // XXX: this is an evil hack to deal with NSProxy; fix elsewhere | |
1821 | if (CYImplements(self, _class, @selector(cy$getPropertyNames:inContext:), false)) | |
1822 | [self cy$getPropertyNames:names inContext:context]; | |
1823 | } CYPoolCatch() | |
1824 | } | |
1825 | ||
1826 | static JSObjectRef Instance_callAsConstructor(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { | |
1827 | Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object))); | |
1828 | JSObjectRef value(Instance::Make(context, [internal->GetValue() alloc], Instance::Uninitialized)); | |
1829 | return value; | |
1830 | } CYCatch } | |
1831 | ||
1832 | static bool Instance_hasInstance(JSContextRef context, JSObjectRef constructor, JSValueRef instance, JSValueRef *exception) { CYTry { | |
1833 | Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate((JSObjectRef) constructor))); | |
1834 | Class _class(internal->GetValue()); | |
1835 | if (!CYIsClass(_class)) | |
1836 | return false; | |
1837 | ||
1838 | if (JSValueIsObjectOfClass(context, instance, Instance_)) { | |
1839 | Instance *linternal(reinterpret_cast<Instance *>(JSObjectGetPrivate((JSObjectRef) instance))); | |
1840 | // XXX: this isn't always safe | |
1841 | return [linternal->GetValue() isKindOfClass:_class]; | |
1842 | } | |
1843 | ||
1844 | return false; | |
1845 | } CYCatch } | |
1846 | ||
1847 | static bool Internal_hasProperty(JSContextRef context, JSObjectRef object, JSStringRef property) { | |
1848 | Internal *internal(reinterpret_cast<Internal *>(JSObjectGetPrivate(object))); | |
1849 | CYPool pool; | |
1850 | ||
1851 | id self(internal->GetValue()); | |
1852 | const char *name(CYPoolCString(pool, context, property)); | |
1853 | ||
1854 | if (object_getInstanceVariable(self, name, NULL) != NULL) | |
1855 | return true; | |
1856 | ||
1857 | return false; | |
1858 | } | |
1859 | ||
1860 | static JSValueRef Internal_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry { | |
1861 | Internal *internal(reinterpret_cast<Internal *>(JSObjectGetPrivate(object))); | |
1862 | CYPool pool; | |
1863 | ||
1864 | id self(internal->GetValue()); | |
1865 | const char *name(CYPoolCString(pool, context, property)); | |
1866 | ||
1867 | if (objc_ivar *ivar = object_getInstanceVariable(self, name, NULL)) { | |
1868 | Type_privateData type(pool, ivar_getTypeEncoding(ivar)); | |
1869 | // XXX: if this fails and throws an exception the person we are throwing it to gets the wrong exception | |
1870 | return CYFromFFI(context, type.type_, type.GetFFI(), reinterpret_cast<uint8_t *>(self) + ivar_getOffset(ivar)); | |
1871 | } | |
1872 | ||
1873 | return NULL; | |
1874 | } CYCatch } | |
1875 | ||
1876 | static bool Internal_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) { CYTry { | |
1877 | Internal *internal(reinterpret_cast<Internal *>(JSObjectGetPrivate(object))); | |
1878 | CYPool pool; | |
1879 | ||
1880 | id self(internal->GetValue()); | |
1881 | const char *name(CYPoolCString(pool, context, property)); | |
1882 | ||
1883 | if (objc_ivar *ivar = object_getInstanceVariable(self, name, NULL)) { | |
1884 | Type_privateData type(pool, ivar_getTypeEncoding(ivar)); | |
1885 | CYPoolFFI(pool, context, type.type_, type.GetFFI(), reinterpret_cast<uint8_t *>(self) + ivar_getOffset(ivar), value); | |
1886 | return true; | |
1887 | } | |
1888 | ||
1889 | return false; | |
1890 | } CYCatch } | |
1891 | ||
1892 | static void Internal_getPropertyNames_(Class _class, JSPropertyNameAccumulatorRef names) { | |
1893 | if (Class super = class_getSuperclass(_class)) | |
1894 | Internal_getPropertyNames_(super, names); | |
1895 | ||
1896 | #if OBJC_API_VERSION >= 2 | |
1897 | unsigned int size; | |
1898 | objc_ivar **data(class_copyIvarList(_class, &size)); | |
1899 | for (size_t i(0); i != size; ++i) | |
1900 | JSPropertyNameAccumulatorAddName(names, CYJSString(ivar_getName(data[i]))); | |
1901 | free(data); | |
1902 | #else | |
1903 | if (objc_ivar_list *ivars = _class->ivars) | |
1904 | for (int i(0); i != ivars->ivar_count; ++i) | |
1905 | JSPropertyNameAccumulatorAddName(names, CYJSString(ivar_getName(&ivars->ivar_list[i]))); | |
1906 | #endif | |
1907 | } | |
1908 | ||
1909 | static void Internal_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) { | |
1910 | Internal *internal(reinterpret_cast<Internal *>(JSObjectGetPrivate(object))); | |
1911 | CYPool pool; | |
1912 | ||
1913 | id self(internal->GetValue()); | |
1914 | Class _class(object_getClass(self)); | |
1915 | ||
1916 | Internal_getPropertyNames_(_class, names); | |
1917 | } | |
1918 | ||
1919 | static JSValueRef Internal_callAsFunction_$cya(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { | |
1920 | Internal *internal(reinterpret_cast<Internal *>(JSObjectGetPrivate(object))); | |
1921 | return internal->GetOwner(); | |
1922 | } | |
1923 | ||
1924 | static JSValueRef ObjectiveC_Classes_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry { | |
1925 | CYPool pool; | |
1926 | NSString *name(CYCastNSString(pool, context, property)); | |
1927 | if (Class _class = NSClassFromString(name)) | |
1928 | return CYMakeInstance(context, _class, true); | |
1929 | return NULL; | |
1930 | } CYCatch } | |
1931 | ||
1932 | static void ObjectiveC_Classes_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) { | |
1933 | #ifdef __APPLE__ | |
1934 | size_t size(objc_getClassList(NULL, 0)); | |
1935 | Class *data(reinterpret_cast<Class *>(malloc(sizeof(Class) * size))); | |
1936 | ||
1937 | get: | |
1938 | size_t writ(objc_getClassList(data, size)); | |
1939 | if (size < writ) { | |
1940 | size = writ; | |
1941 | if (Class *copy = reinterpret_cast<Class *>(realloc(data, sizeof(Class) * writ))) { | |
1942 | data = copy; | |
1943 | goto get; | |
1944 | } else goto done; | |
1945 | } | |
1946 | ||
1947 | for (size_t i(0); i != writ; ++i) | |
1948 | JSPropertyNameAccumulatorAddName(names, CYJSString(class_getName(data[i]))); | |
1949 | ||
1950 | done: | |
1951 | free(data); | |
1952 | #else | |
1953 | void *state(NULL); | |
1954 | while (Class _class = objc_next_class(&state)) | |
1955 | JSPropertyNameAccumulatorAddName(names, CYJSString(class_getName(_class))); | |
1956 | #endif | |
1957 | } | |
1958 | ||
1959 | #if OBJC_API_VERSION >= 2 | |
1960 | static JSValueRef ObjectiveC_Image_Classes_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry { | |
1961 | const char *internal(reinterpret_cast<const char *>(JSObjectGetPrivate(object))); | |
1962 | ||
1963 | CYPool pool; | |
1964 | const char *name(CYPoolCString(pool, context, property)); | |
1965 | unsigned int size; | |
1966 | const char **data(objc_copyClassNamesForImage(internal, &size)); | |
1967 | JSValueRef value; | |
1968 | for (size_t i(0); i != size; ++i) | |
1969 | if (strcmp(name, data[i]) == 0) { | |
1970 | if (Class _class = objc_getClass(name)) { | |
1971 | value = CYMakeInstance(context, _class, true); | |
1972 | goto free; | |
1973 | } else | |
1974 | break; | |
1975 | } | |
1976 | value = NULL; | |
1977 | free: | |
1978 | free(data); | |
1979 | return value; | |
1980 | } CYCatch } | |
1981 | ||
1982 | static void ObjectiveC_Image_Classes_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) { | |
1983 | const char *internal(reinterpret_cast<const char *>(JSObjectGetPrivate(object))); | |
1984 | unsigned int size; | |
1985 | const char **data(objc_copyClassNamesForImage(internal, &size)); | |
1986 | for (size_t i(0); i != size; ++i) | |
1987 | JSPropertyNameAccumulatorAddName(names, CYJSString(data[i])); | |
1988 | free(data); | |
1989 | } | |
1990 | ||
1991 | static JSValueRef ObjectiveC_Images_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry { | |
1992 | CYPool pool; | |
1993 | const char *name(CYPoolCString(pool, context, property)); | |
1994 | unsigned int size; | |
1995 | const char **data(objc_copyImageNames(&size)); | |
1996 | for (size_t i(0); i != size; ++i) | |
1997 | if (strcmp(name, data[i]) == 0) { | |
1998 | name = data[i]; | |
1999 | goto free; | |
2000 | } | |
2001 | name = NULL; | |
2002 | free: | |
2003 | free(data); | |
2004 | if (name == NULL) | |
2005 | return NULL; | |
2006 | JSObjectRef value(JSObjectMake(context, NULL, NULL)); | |
2007 | CYSetProperty(context, value, CYJSString("classes"), JSObjectMake(context, ObjectiveC_Image_Classes_, const_cast<char *>(name))); | |
2008 | return value; | |
2009 | } CYCatch } | |
2010 | ||
2011 | static void ObjectiveC_Images_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) { | |
2012 | unsigned int size; | |
2013 | const char **data(objc_copyImageNames(&size)); | |
2014 | for (size_t i(0); i != size; ++i) | |
2015 | JSPropertyNameAccumulatorAddName(names, CYJSString(data[i])); | |
2016 | free(data); | |
2017 | } | |
2018 | #endif | |
2019 | ||
2020 | static JSValueRef ObjectiveC_Protocols_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry { | |
2021 | CYPool pool; | |
2022 | const char *name(CYPoolCString(pool, context, property)); | |
2023 | if (Protocol *protocol = objc_getProtocol(name)) | |
2024 | return CYMakeInstance(context, protocol, true); | |
2025 | return NULL; | |
2026 | } CYCatch } | |
2027 | ||
2028 | static void ObjectiveC_Protocols_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) { | |
2029 | #if OBJC_API_VERSION >= 2 | |
2030 | unsigned int size; | |
2031 | Protocol **data(objc_copyProtocolList(&size)); | |
2032 | for (size_t i(0); i != size; ++i) | |
2033 | JSPropertyNameAccumulatorAddName(names, CYJSString(protocol_getName(data[i]))); | |
2034 | free(data); | |
2035 | #else | |
2036 | // XXX: fix this! | |
2037 | #endif | |
2038 | } | |
2039 | ||
2040 | #ifdef __APPLE__ | |
2041 | static bool stret(ffi_type *ffi_type) { | |
2042 | return ffi_type->type == FFI_TYPE_STRUCT && ( | |
2043 | ffi_type->size > OBJC_MAX_STRUCT_BY_VALUE || | |
2044 | struct_forward_array[ffi_type->size] != 0 | |
2045 | ); | |
2046 | } | |
2047 | #endif | |
2048 | ||
2049 | JSValueRef CYSendMessage(apr_pool_t *pool, JSContextRef context, id self, Class _class, SEL _cmd, size_t count, const JSValueRef arguments[], bool initialize, JSValueRef *exception) { CYTry { | |
2050 | const char *type; | |
2051 | ||
2052 | if (_class == NULL) | |
2053 | _class = object_getClass(self); | |
2054 | ||
2055 | IMP imp; | |
2056 | ||
2057 | if (objc_method *method = class_getInstanceMethod(_class, _cmd)) { | |
2058 | imp = method_getImplementation(method); | |
2059 | type = method_getTypeEncoding(method); | |
2060 | } else { | |
2061 | imp = NULL; | |
2062 | ||
2063 | CYPoolTry { | |
2064 | NSMethodSignature *method([self methodSignatureForSelector:_cmd]); | |
2065 | if (method == nil) | |
2066 | throw CYJSError(context, "unrecognized selector %s sent to object %p", sel_getName(_cmd), self); | |
2067 | type = CYPoolCString(pool, context, [method _typeString]); | |
2068 | } CYPoolCatch(NULL) | |
2069 | } | |
2070 | ||
2071 | void *setup[2]; | |
2072 | setup[0] = &self; | |
2073 | setup[1] = &_cmd; | |
2074 | ||
2075 | sig::Signature signature; | |
2076 | sig::Parse(pool, &signature, type, &Structor_); | |
2077 | ||
2078 | ffi_cif cif; | |
2079 | sig::sig_ffi_cif(pool, &sig::ObjectiveC, &signature, &cif); | |
2080 | ||
2081 | if (imp == NULL) { | |
2082 | #ifdef __APPLE__ | |
2083 | if (stret(cif.rtype)) | |
2084 | imp = class_getMethodImplementation_stret(_class, _cmd); | |
2085 | else | |
2086 | imp = class_getMethodImplementation(_class, _cmd); | |
2087 | #else | |
2088 | objc_super super = {self, _class}; | |
2089 | imp = objc_msg_lookup_super(&super, _cmd); | |
2090 | #endif | |
2091 | } | |
2092 | ||
2093 | void (*function)() = reinterpret_cast<void (*)()>(imp); | |
2094 | return CYCallFunction(pool, context, 2, setup, count, arguments, initialize, exception, &signature, &cif, function); | |
2095 | } CYCatch } | |
2096 | ||
2097 | static JSValueRef $objc_msgSend(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { | |
2098 | if (count < 2) | |
2099 | throw CYJSError(context, "too few arguments to objc_msgSend"); | |
2100 | ||
2101 | CYPool pool; | |
2102 | ||
2103 | bool uninitialized; | |
2104 | ||
2105 | id self; | |
2106 | SEL _cmd; | |
2107 | Class _class; | |
2108 | ||
2109 | if (JSValueIsObjectOfClass(context, arguments[0], Super_)) { | |
2110 | cy::Super *internal(reinterpret_cast<cy::Super *>(JSObjectGetPrivate((JSObjectRef) arguments[0]))); | |
2111 | self = internal->GetValue(); | |
2112 | _class = internal->class_;; | |
2113 | uninitialized = false; | |
2114 | } else if (JSValueIsObjectOfClass(context, arguments[0], Instance_)) { | |
2115 | Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate((JSObjectRef) arguments[0]))); | |
2116 | self = internal->GetValue(); | |
2117 | _class = nil; | |
2118 | uninitialized = internal->IsUninitialized(); | |
2119 | if (uninitialized) | |
2120 | internal->value_ = nil; | |
2121 | } else { | |
2122 | self = CYCastNSObject(pool, context, arguments[0]); | |
2123 | _class = nil; | |
2124 | uninitialized = false; | |
2125 | } | |
2126 | ||
2127 | if (self == nil) | |
2128 | return CYJSNull(context); | |
2129 | ||
2130 | _cmd = CYCastSEL(context, arguments[1]); | |
2131 | ||
2132 | return CYSendMessage(pool, context, self, _class, _cmd, count - 2, arguments + 2, uninitialized, exception); | |
2133 | } CYCatch } | |
2134 | ||
2135 | /* Hook: objc_registerClassPair {{{ */ | |
2136 | #if defined(__APPLE__) && defined(__arm__) | |
2137 | // XXX: replace this with associated objects | |
2138 | ||
2139 | MSHook(void, CYDealloc, id self, SEL sel) { | |
2140 | CYInternal *internal; | |
2141 | object_getInstanceVariable(self, "cy$internal_", reinterpret_cast<void **>(&internal)); | |
2142 | if (internal != NULL) | |
2143 | delete internal; | |
2144 | _CYDealloc(self, sel); | |
2145 | } | |
2146 | ||
2147 | MSHook(void, objc_registerClassPair, Class _class) { | |
2148 | Class super(class_getSuperclass(_class)); | |
2149 | if (super == NULL || class_getInstanceVariable(super, "cy$internal_") == NULL) { | |
2150 | class_addIvar(_class, "cy$internal_", sizeof(CYInternal *), log2(sizeof(CYInternal *)), "^{CYInternal}"); | |
2151 | MSHookMessage(_class, @selector(dealloc), MSHake(CYDealloc)); | |
2152 | } | |
2153 | ||
2154 | _objc_registerClassPair(_class); | |
2155 | } | |
2156 | ||
2157 | static JSValueRef objc_registerClassPair_(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { | |
2158 | if (count != 1) | |
2159 | throw CYJSError(context, "incorrect number of arguments to objc_registerClassPair"); | |
2160 | CYPool pool; | |
2161 | NSObject *value(CYCastNSObject(pool, context, arguments[0])); | |
2162 | if (value == NULL || !CYIsClass(value)) | |
2163 | throw CYJSError(context, "incorrect number of arguments to objc_registerClassPair"); | |
2164 | Class _class((Class) value); | |
2165 | $objc_registerClassPair(_class); | |
2166 | return CYJSUndefined(context); | |
2167 | } CYCatch } | |
2168 | #endif | |
2169 | /* }}} */ | |
2170 | ||
2171 | static JSValueRef Selector_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { | |
2172 | JSValueRef setup[count + 2]; | |
2173 | setup[0] = _this; | |
2174 | setup[1] = object; | |
2175 | memcpy(setup + 2, arguments, sizeof(JSValueRef) * count); | |
2176 | return $objc_msgSend(context, NULL, NULL, count + 2, setup, exception); | |
2177 | } | |
2178 | ||
2179 | static JSValueRef Message_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { | |
2180 | CYPool pool; | |
2181 | Message_privateData *internal(reinterpret_cast<Message_privateData *>(JSObjectGetPrivate(object))); | |
2182 | ||
2183 | // XXX: handle Instance::Uninitialized? | |
2184 | id self(CYCastNSObject(pool, context, _this)); | |
2185 | ||
2186 | void *setup[2]; | |
2187 | setup[0] = &self; | |
2188 | setup[1] = &internal->sel_; | |
2189 | ||
2190 | return CYCallFunction(pool, context, 2, setup, count, arguments, false, exception, &internal->signature_, &internal->cif_, internal->GetValue()); | |
2191 | } | |
2192 | ||
2193 | static JSObjectRef Super_new(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { | |
2194 | if (count != 2) | |
2195 | throw CYJSError(context, "incorrect number of arguments to Super constructor"); | |
2196 | CYPool pool; | |
2197 | id self(CYCastNSObject(pool, context, arguments[0])); | |
2198 | Class _class(CYCastClass(pool, context, arguments[1])); | |
2199 | return cy::Super::Make(context, self, _class); | |
2200 | } CYCatch } | |
2201 | ||
2202 | static JSObjectRef Selector_new(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { | |
2203 | if (count != 1) | |
2204 | throw CYJSError(context, "incorrect number of arguments to Selector constructor"); | |
2205 | CYPool pool; | |
2206 | const char *name(CYPoolCString(pool, context, arguments[0])); | |
2207 | return CYMakeSelector(context, sel_registerName(name)); | |
2208 | } CYCatch } | |
2209 | ||
2210 | static JSObjectRef Instance_new(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { | |
2211 | if (count > 1) | |
2212 | throw CYJSError(context, "incorrect number of arguments to Instance constructor"); | |
2213 | id self(count == 0 ? nil : CYCastPointer<id>(context, arguments[0])); | |
2214 | return CYMakeInstance(context, self, false); | |
2215 | } CYCatch } | |
2216 | ||
2217 | static JSValueRef CYValue_getProperty_value(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { | |
2218 | CYValue *internal(reinterpret_cast<CYValue *>(JSObjectGetPrivate(object))); | |
2219 | return CYCastJSValue(context, reinterpret_cast<uintptr_t>(internal->value_)); | |
2220 | } | |
2221 | ||
2222 | static JSValueRef CYValue_callAsFunction_$cya(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { | |
2223 | CYValue *internal(reinterpret_cast<CYValue *>(JSObjectGetPrivate(_this))); | |
2224 | Type_privateData *typical(internal->GetType()); | |
2225 | ||
2226 | sig::Type *type; | |
2227 | ffi_type *ffi; | |
2228 | ||
2229 | if (typical == NULL) { | |
2230 | type = NULL; | |
2231 | ffi = NULL; | |
2232 | } else { | |
2233 | type = typical->type_; | |
2234 | ffi = typical->ffi_; | |
2235 | } | |
2236 | ||
2237 | return CYMakePointer(context, &internal->value_, _not(size_t), type, ffi, object); | |
2238 | } | |
2239 | ||
2240 | static JSValueRef Instance_getProperty_constructor(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { | |
2241 | Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object))); | |
2242 | return Instance::Make(context, (id) object_getClass(internal->GetValue())); | |
2243 | } | |
2244 | ||
2245 | static JSValueRef Instance_getProperty_protocol(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry { | |
2246 | Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object))); | |
2247 | id self(internal->GetValue()); | |
2248 | if (!CYIsClass(self)) | |
2249 | return CYJSUndefined(context); | |
2250 | return CYGetClassPrototype(context, self); | |
2251 | } CYCatch } | |
2252 | ||
2253 | static JSValueRef Instance_getProperty_messages(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { | |
2254 | Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object))); | |
2255 | id self(internal->GetValue()); | |
2256 | if (!CYIsClass(self)) | |
2257 | return CYJSUndefined(context); | |
2258 | return Messages::Make(context, (Class) self); | |
2259 | } | |
2260 | ||
2261 | static JSValueRef Instance_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { | |
2262 | if (!JSValueIsObjectOfClass(context, _this, Instance_)) | |
2263 | return NULL; | |
2264 | ||
2265 | Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(_this))); | |
2266 | return CYCastJSValue(context, CYJSString(context, CYCastNSCYON(internal->GetValue()))); | |
2267 | } CYCatch } | |
2268 | ||
2269 | static JSValueRef Instance_callAsFunction_toJSON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { | |
2270 | if (!JSValueIsObjectOfClass(context, _this, Instance_)) | |
2271 | return NULL; | |
2272 | ||
2273 | Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(_this))); | |
2274 | ||
2275 | CYPoolTry { | |
2276 | NSString *key; | |
2277 | if (count == 0) | |
2278 | key = nil; | |
2279 | else | |
2280 | key = CYCastNSString(NULL, context, CYJSString(context, arguments[0])); | |
2281 | // XXX: check for support of cy$toJSON? | |
2282 | return CYCastJSValue(context, CYJSString(context, [internal->GetValue() cy$toJSON:key])); | |
2283 | } CYPoolCatch(NULL) | |
2284 | } CYCatch return /*XXX*/ NULL; } | |
2285 | ||
2286 | #if 0 | |
2287 | static JSValueRef Instance_callAsFunction_valueOf(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { | |
2288 | if (!JSValueIsObjectOfClass(context, _this, Instance_)) | |
2289 | return NULL; | |
2290 | ||
2291 | Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(_this))); | |
2292 | return CYCastJSValue(context, reinterpret_cast<uintptr_t>(internal->GetValue())); | |
2293 | } CYCatch return /*XXX*/ NULL; } | |
2294 | #endif | |
2295 | ||
2296 | static JSValueRef Instance_callAsFunction_toPointer(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { | |
2297 | if (!JSValueIsObjectOfClass(context, _this, Instance_)) | |
2298 | return NULL; | |
2299 | ||
2300 | Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(_this))); | |
2301 | // XXX: but... but... THIS ISN'T A POINTER! :( | |
2302 | return CYCastJSValue(context, reinterpret_cast<uintptr_t>(internal->GetValue())); | |
2303 | } CYCatch return /*XXX*/ NULL; } | |
2304 | ||
2305 | static JSValueRef Instance_callAsFunction_toString(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { | |
2306 | if (!JSValueIsObjectOfClass(context, _this, Instance_)) | |
2307 | return NULL; | |
2308 | ||
2309 | Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(_this))); | |
2310 | ||
2311 | id value(internal->GetValue()); | |
2312 | if (value == nil) | |
2313 | return CYCastJSValue(context, "nil"); | |
2314 | ||
2315 | CYPoolTry { | |
2316 | // XXX: this seems like a stupid implementation; what if it crashes? why not use the CYONifier backend? | |
2317 | return CYCastJSValue(context, CYJSString(context, [internal->GetValue() description])); | |
2318 | } CYPoolCatch(NULL) | |
2319 | } CYCatch return /*XXX*/ NULL; } | |
2320 | ||
2321 | static JSValueRef Selector_callAsFunction_toString(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { | |
2322 | Selector_privateData *internal(reinterpret_cast<Selector_privateData *>(JSObjectGetPrivate(_this))); | |
2323 | return CYCastJSValue(context, sel_getName(internal->GetValue())); | |
2324 | } CYCatch } | |
2325 | ||
2326 | static JSValueRef Selector_callAsFunction_toJSON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { | |
2327 | return Selector_callAsFunction_toString(context, object, _this, count, arguments, exception); | |
2328 | } | |
2329 | ||
2330 | static JSValueRef Selector_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { | |
2331 | Selector_privateData *internal(reinterpret_cast<Selector_privateData *>(JSObjectGetPrivate(_this))); | |
2332 | const char *name(sel_getName(internal->GetValue())); | |
2333 | ||
2334 | CYPoolTry { | |
2335 | NSString *string([NSString stringWithFormat:@"@selector(%s)", name]); | |
2336 | return CYCastJSValue(context, CYJSString(context, string)); | |
2337 | } CYPoolCatch(NULL) | |
2338 | } CYCatch return /*XXX*/ NULL; } | |
2339 | ||
2340 | static JSValueRef Selector_callAsFunction_type(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { | |
2341 | if (count != 1) | |
2342 | throw CYJSError(context, "incorrect number of arguments to Selector.type"); | |
2343 | ||
2344 | CYPool pool; | |
2345 | Selector_privateData *internal(reinterpret_cast<Selector_privateData *>(JSObjectGetPrivate(_this))); | |
2346 | SEL sel(internal->GetValue()); | |
2347 | ||
2348 | objc_method *method; | |
2349 | if (Class _class = CYCastClass(pool, context, arguments[0])) | |
2350 | method = class_getInstanceMethod(_class, sel); | |
2351 | else | |
2352 | method = NULL; | |
2353 | ||
2354 | if (const char *type = CYPoolTypeEncoding(pool, context, sel, method)) | |
2355 | return CYCastJSValue(context, CYJSString(type)); | |
2356 | ||
2357 | return CYJSNull(context); | |
2358 | } CYCatch } | |
2359 | ||
2360 | static JSStaticValue Selector_staticValues[2] = { | |
2361 | {"value", &CYValue_getProperty_value, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete}, | |
2362 | {NULL, NULL, NULL, 0} | |
2363 | }; | |
2364 | ||
2365 | static JSStaticValue Instance_staticValues[5] = { | |
2366 | {"constructor", &Instance_getProperty_constructor, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
2367 | {"messages", &Instance_getProperty_messages, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
2368 | {"prototype", &Instance_getProperty_protocol, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
2369 | {"value", &CYValue_getProperty_value, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
2370 | {NULL, NULL, NULL, 0} | |
2371 | }; | |
2372 | ||
2373 | static JSStaticFunction Instance_staticFunctions[6] = { | |
2374 | {"$cya", &CYValue_callAsFunction_$cya, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
2375 | {"toCYON", &Instance_callAsFunction_toCYON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
2376 | {"toJSON", &Instance_callAsFunction_toJSON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
2377 | //{"valueOf", &Instance_callAsFunction_valueOf, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
2378 | {"toPointer", &Instance_callAsFunction_toPointer, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
2379 | {"toString", &Instance_callAsFunction_toString, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
2380 | {NULL, NULL, 0} | |
2381 | }; | |
2382 | ||
2383 | static JSStaticFunction Internal_staticFunctions[2] = { | |
2384 | {"$cya", &Internal_callAsFunction_$cya, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
2385 | {NULL, NULL, 0} | |
2386 | }; | |
2387 | ||
2388 | static JSStaticFunction Selector_staticFunctions[5] = { | |
2389 | {"toCYON", &Selector_callAsFunction_toCYON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
2390 | {"toJSON", &Selector_callAsFunction_toJSON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
2391 | {"toString", &Selector_callAsFunction_toString, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
2392 | {"type", &Selector_callAsFunction_type, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
2393 | {NULL, NULL, 0} | |
2394 | }; | |
2395 | ||
2396 | static JSStaticFunction StringInstance_staticFunctions[2] = { | |
2397 | //{"valueOf", &Instance_callAsFunction_valueOf, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
2398 | {"toString", &Instance_callAsFunction_toString, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
2399 | {NULL, NULL, 0} | |
2400 | }; | |
2401 | ||
2402 | void CYObjectiveC_Initialize() { /*XXX*/ JSContextRef context(NULL); CYPoolTry { | |
2403 | apr_pool_t *pool(CYGetGlobalPool()); | |
2404 | ||
2405 | Object_type = new(pool) Type_privateData("@"); | |
2406 | Selector_type = new(pool) Type_privateData(":"); | |
2407 | ||
2408 | #ifdef __APPLE__ | |
2409 | NSCFBoolean_ = objc_getClass("NSCFBoolean"); | |
2410 | NSCFType_ = objc_getClass("NSCFType"); | |
2411 | NSMessageBuilder_ = objc_getClass("NSMessageBuilder"); | |
2412 | NSZombie_ = objc_getClass("_NSZombie_"); | |
2413 | #else | |
2414 | NSBoolNumber_ = objc_getClass("NSBoolNumber"); | |
2415 | #endif | |
2416 | ||
2417 | NSArray_ = objc_getClass("NSArray"); | |
2418 | NSDictionary_ = objc_getClass("NSDictionary"); | |
2419 | NSString_ = objc_getClass("NSString"); | |
2420 | Object_ = objc_getClass("Object"); | |
2421 | ||
2422 | JSClassDefinition definition; | |
2423 | ||
2424 | definition = kJSClassDefinitionEmpty; | |
2425 | definition.className = "Instance"; | |
2426 | definition.staticValues = Instance_staticValues; | |
2427 | definition.staticFunctions = Instance_staticFunctions; | |
2428 | definition.hasProperty = &Instance_hasProperty; | |
2429 | definition.getProperty = &Instance_getProperty; | |
2430 | definition.setProperty = &Instance_setProperty; | |
2431 | definition.deleteProperty = &Instance_deleteProperty; | |
2432 | definition.getPropertyNames = &Instance_getPropertyNames; | |
2433 | definition.callAsConstructor = &Instance_callAsConstructor; | |
2434 | definition.hasInstance = &Instance_hasInstance; | |
2435 | definition.finalize = &CYFinalize; | |
2436 | Instance_ = JSClassCreate(&definition); | |
2437 | ||
2438 | definition = kJSClassDefinitionEmpty; | |
2439 | definition.className = "Internal"; | |
2440 | definition.staticFunctions = Internal_staticFunctions; | |
2441 | definition.hasProperty = &Internal_hasProperty; | |
2442 | definition.getProperty = &Internal_getProperty; | |
2443 | definition.setProperty = &Internal_setProperty; | |
2444 | definition.getPropertyNames = &Internal_getPropertyNames; | |
2445 | definition.finalize = &CYFinalize; | |
2446 | Internal_ = JSClassCreate(&definition); | |
2447 | ||
2448 | definition = kJSClassDefinitionEmpty; | |
2449 | definition.className = "Message"; | |
2450 | definition.staticFunctions = cy::Functor::StaticFunctions; | |
2451 | definition.callAsFunction = &Message_callAsFunction; | |
2452 | definition.finalize = &CYFinalize; | |
2453 | Message_ = JSClassCreate(&definition); | |
2454 | ||
2455 | definition = kJSClassDefinitionEmpty; | |
2456 | definition.className = "Messages"; | |
2457 | definition.hasProperty = &Messages_hasProperty; | |
2458 | definition.getProperty = &Messages_getProperty; | |
2459 | definition.setProperty = &Messages_setProperty; | |
2460 | #if 0 && OBJC_API_VERSION < 2 | |
2461 | definition.deleteProperty = &Messages_deleteProperty; | |
2462 | #endif | |
2463 | definition.getPropertyNames = &Messages_getPropertyNames; | |
2464 | definition.finalize = &CYFinalize; | |
2465 | Messages_ = JSClassCreate(&definition); | |
2466 | ||
2467 | definition = kJSClassDefinitionEmpty; | |
2468 | definition.className = "Selector"; | |
2469 | definition.staticValues = Selector_staticValues; | |
2470 | definition.staticFunctions = Selector_staticFunctions; | |
2471 | definition.callAsFunction = &Selector_callAsFunction; | |
2472 | definition.finalize = &CYFinalize; | |
2473 | Selector_ = JSClassCreate(&definition); | |
2474 | ||
2475 | definition = kJSClassDefinitionEmpty; | |
2476 | definition.className = "StringInstance"; | |
2477 | definition.staticFunctions = StringInstance_staticFunctions; | |
2478 | StringInstance_ = JSClassCreate(&definition); | |
2479 | ||
2480 | definition = kJSClassDefinitionEmpty; | |
2481 | definition.className = "Super"; | |
2482 | definition.staticFunctions = Internal_staticFunctions; | |
2483 | definition.finalize = &CYFinalize; | |
2484 | Super_ = JSClassCreate(&definition); | |
2485 | ||
2486 | definition = kJSClassDefinitionEmpty; | |
2487 | definition.className = "ObjectiveC::Classes"; | |
2488 | definition.getProperty = &ObjectiveC_Classes_getProperty; | |
2489 | definition.getPropertyNames = &ObjectiveC_Classes_getPropertyNames; | |
2490 | ObjectiveC_Classes_ = JSClassCreate(&definition); | |
2491 | ||
2492 | #if OBJC_API_VERSION >= 2 | |
2493 | definition = kJSClassDefinitionEmpty; | |
2494 | definition.className = "ObjectiveC::Images"; | |
2495 | definition.getProperty = &ObjectiveC_Images_getProperty; | |
2496 | definition.getPropertyNames = &ObjectiveC_Images_getPropertyNames; | |
2497 | ObjectiveC_Images_ = JSClassCreate(&definition); | |
2498 | ||
2499 | definition = kJSClassDefinitionEmpty; | |
2500 | definition.className = "ObjectiveC::Image::Classes"; | |
2501 | definition.getProperty = &ObjectiveC_Image_Classes_getProperty; | |
2502 | definition.getPropertyNames = &ObjectiveC_Image_Classes_getPropertyNames; | |
2503 | ObjectiveC_Image_Classes_ = JSClassCreate(&definition); | |
2504 | #endif | |
2505 | ||
2506 | definition = kJSClassDefinitionEmpty; | |
2507 | definition.className = "ObjectiveC::Protocols"; | |
2508 | definition.getProperty = &ObjectiveC_Protocols_getProperty; | |
2509 | definition.getPropertyNames = &ObjectiveC_Protocols_getPropertyNames; | |
2510 | ObjectiveC_Protocols_ = JSClassCreate(&definition); | |
2511 | ||
2512 | #if defined(__APPLE__) && defined(__arm__) | |
2513 | MSHookFunction(&objc_registerClassPair, MSHake(objc_registerClassPair)); | |
2514 | #endif | |
2515 | ||
2516 | #ifdef __APPLE__ | |
2517 | class_addMethod(NSCFType_, @selector(cy$toJSON:), reinterpret_cast<IMP>(&NSCFType$cy$toJSON), "@12@0:4@8"); | |
2518 | #endif | |
2519 | } CYPoolCatch() } | |
2520 | ||
2521 | void CYObjectiveC_SetupContext(JSContextRef context) { CYPoolTry { | |
2522 | JSObjectRef global(CYGetGlobalObject(context)); | |
2523 | JSObjectRef cy(CYCastJSObject(context, CYGetProperty(context, global, cy_s))); | |
2524 | JSObjectRef cycript(CYCastJSObject(context, CYGetProperty(context, global, CYJSString("Cycript")))); | |
2525 | JSObjectRef all(CYCastJSObject(context, CYGetProperty(context, cycript, CYJSString("all")))); | |
2526 | ||
2527 | JSObjectRef ObjectiveC(JSObjectMake(context, NULL, NULL)); | |
2528 | CYSetProperty(context, cycript, CYJSString("ObjectiveC"), ObjectiveC); | |
2529 | ||
2530 | CYSetProperty(context, ObjectiveC, CYJSString("classes"), JSObjectMake(context, ObjectiveC_Classes_, NULL)); | |
2531 | CYSetProperty(context, ObjectiveC, CYJSString("protocols"), JSObjectMake(context, ObjectiveC_Protocols_, NULL)); | |
2532 | ||
2533 | #if OBJC_API_VERSION >= 2 | |
2534 | CYSetProperty(context, ObjectiveC, CYJSString("images"), JSObjectMake(context, ObjectiveC_Images_, NULL)); | |
2535 | #endif | |
2536 | ||
2537 | JSObjectRef Instance(JSObjectMakeConstructor(context, Instance_, &Instance_new)); | |
2538 | JSObjectRef Message(JSObjectMakeConstructor(context, Message_, NULL)); | |
2539 | JSObjectRef Selector(JSObjectMakeConstructor(context, Selector_, &Selector_new)); | |
2540 | JSObjectRef StringInstance(JSObjectMakeConstructor(context, StringInstance_, NULL)); | |
2541 | JSObjectRef Super(JSObjectMakeConstructor(context, Super_, &Super_new)); | |
2542 | ||
2543 | JSObjectRef Instance_prototype(CYCastJSObject(context, CYGetProperty(context, Instance, prototype_s))); | |
2544 | CYSetProperty(context, cy, CYJSString("Instance_prototype"), Instance_prototype); | |
2545 | ||
2546 | JSObjectRef StringInstance_prototype(CYCastJSObject(context, CYGetProperty(context, StringInstance, prototype_s))); | |
2547 | CYSetProperty(context, cy, CYJSString("StringInstance_prototype"), StringInstance_prototype); | |
2548 | ||
2549 | JSObjectRef String_prototype(CYGetCachedObject(context, CYJSString("String_prototype"))); | |
2550 | JSObjectSetPrototype(context, StringInstance_prototype, String_prototype); | |
2551 | ||
2552 | CYSetProperty(context, cycript, CYJSString("Instance"), Instance); | |
2553 | CYSetProperty(context, cycript, CYJSString("Selector"), Selector); | |
2554 | CYSetProperty(context, cycript, CYJSString("Super"), Super); | |
2555 | ||
2556 | #if defined(__APPLE__) && defined(__arm__) | |
2557 | CYSetProperty(context, all, CYJSString("objc_registerClassPair"), &objc_registerClassPair_, kJSPropertyAttributeDontEnum); | |
2558 | #endif | |
2559 | ||
2560 | CYSetProperty(context, all, CYJSString("objc_msgSend"), &$objc_msgSend, kJSPropertyAttributeDontEnum); | |
2561 | ||
2562 | JSObjectRef Function_prototype(CYGetCachedObject(context, CYJSString("Function_prototype"))); | |
2563 | JSObjectSetPrototype(context, CYCastJSObject(context, CYGetProperty(context, Message, prototype_s)), Function_prototype); | |
2564 | JSObjectSetPrototype(context, CYCastJSObject(context, CYGetProperty(context, Selector, prototype_s)), Function_prototype); | |
2565 | } CYPoolCatch() } | |
2566 | ||
2567 | static CYHooks CYObjectiveCHooks = { | |
2568 | &CYObjectiveC_ExecuteStart, | |
2569 | &CYObjectiveC_ExecuteEnd, | |
2570 | &CYObjectiveC_RuntimeProperty, | |
2571 | &CYObjectiveC_CallFunction, | |
2572 | &CYObjectiveC_Initialize, | |
2573 | &CYObjectiveC_SetupContext, | |
2574 | &CYObjectiveC_PoolFFI, | |
2575 | &CYObjectiveC_FromFFI, | |
2576 | }; | |
2577 | ||
2578 | struct CYObjectiveC { | |
2579 | CYObjectiveC() { | |
2580 | hooks_ = &CYObjectiveCHooks; | |
2581 | // XXX: evil magic juju to make this actually take effect on a Mac when compiled with autoconf/libtool doom! | |
2582 | _assert(hooks_ != NULL); | |
2583 | } | |
2584 | } CYObjectiveC; |