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