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