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