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