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