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