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