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