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