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