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