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