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