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