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