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