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