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