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