]>
Commit | Line | Data |
---|---|---|
1 | #ifdef __APPLE__ | |
2 | #include <substrate.h> | |
3 | #endif | |
4 | ||
5 | #include "ObjectiveC/Internal.hpp" | |
6 | ||
7 | #ifdef __APPLE__ | |
8 | #include "Struct.hpp" | |
9 | #endif | |
10 | ||
11 | #include <Foundation/Foundation.h> | |
12 | ||
13 | #include <objc/Protocol.h> | |
14 | ||
15 | #include "cycript.hpp" | |
16 | ||
17 | #include "ObjectiveC/Internal.hpp" | |
18 | ||
19 | #ifdef __APPLE__ | |
20 | #include <CoreFoundation/CoreFoundation.h> | |
21 | #include <CoreFoundation/CFLogUtilities.h> | |
22 | #include <JavaScriptCore/JSStringRefCF.h> | |
23 | #include <WebKit/WebScriptObject.h> | |
24 | #endif | |
25 | ||
26 | #include "Error.hpp" | |
27 | #include "JavaScript.hpp" | |
28 | #include "String.hpp" | |
29 | ||
30 | #include <cmath> | |
31 | #include <map> | |
32 | ||
33 | #define CYObjectiveTry_(context) { \ | |
34 | JSContextRef context_(context); \ | |
35 | try | |
36 | #define CYObjectiveTry { \ | |
37 | try | |
38 | #define CYObjectiveCatch \ | |
39 | catch (const CYException &error) { \ | |
40 | @throw CYCastNSObject(NULL, context_, error.CastJSValue(context_)); \ | |
41 | } \ | |
42 | } | |
43 | ||
44 | #define CYPoolTry { \ | |
45 | id _saved(nil); \ | |
46 | NSAutoreleasePool *_pool([[NSAutoreleasePool alloc] init]); \ | |
47 | @try | |
48 | #define CYPoolCatch(value) \ | |
49 | @catch (NSException *error) { \ | |
50 | _saved = [error retain]; \ | |
51 | throw CYJSError(context, CYCastJSValue(context, error)); \ | |
52 | return value; \ | |
53 | } @finally { \ | |
54 | [_pool release]; \ | |
55 | if (_saved != nil) \ | |
56 | [_saved autorelease]; \ | |
57 | } \ | |
58 | } | |
59 | ||
60 | #ifndef __APPLE__ | |
61 | #define class_getSuperclass GSObjCSuper | |
62 | #define class_getInstanceVariable GSCGetInstanceVariableDefinition | |
63 | #define class_getName GSNameFromClass | |
64 | ||
65 | #define class_removeMethods(cls, list) GSRemoveMethodList(cls, list, YES) | |
66 | ||
67 | #define ivar_getName(ivar) ((ivar)->ivar_name) | |
68 | #define ivar_getOffset(ivar) ((ivar)->ivar_offset) | |
69 | #define ivar_getTypeEncoding(ivar) ((ivar)->ivar_type) | |
70 | ||
71 | #define method_getName(method) ((method)->method_name) | |
72 | #define method_getImplementation(method) ((method)->method_imp) | |
73 | #define method_getTypeEncoding(method) ((method)->method_types) | |
74 | #define method_setImplementation(method, imp) ((void) ((method)->method_imp = (imp))) | |
75 | ||
76 | #define objc_getProtocol GSProtocolFromName | |
77 | ||
78 | #define object_getClass GSObjCClass | |
79 | ||
80 | #define object_getInstanceVariable(object, name, value) ({ \ | |
81 | objc_ivar *ivar(class_getInstanceVariable(object_getClass(object), name)); \ | |
82 | GSObjCGetVariable(object, ivar_getOffset(ivar), sizeof(void *), value); \ | |
83 | ivar; \ | |
84 | }) | |
85 | ||
86 | #define object_setIvar(object, ivar, value) ({ \ | |
87 | void *data = (value); \ | |
88 | GSObjCSetVariable(object, ivar_getOffset(ivar), sizeof(void *), &data); \ | |
89 | }) | |
90 | ||
91 | #define protocol_getName(protocol) [(protocol) name] | |
92 | #endif | |
93 | ||
94 | JSValueRef CYSendMessage(apr_pool_t *pool, JSContextRef context, id self, Class super, SEL _cmd, size_t count, const JSValueRef arguments[], bool initialize, JSValueRef *exception); | |
95 | ||
96 | extern sqlite3 *Bridge_; | |
97 | ||
98 | /* Objective-C Pool Release {{{ */ | |
99 | apr_status_t CYPoolRelease_(void *data) { | |
100 | id object(reinterpret_cast<id>(data)); | |
101 | [object release]; | |
102 | return APR_SUCCESS; | |
103 | } | |
104 | ||
105 | id CYPoolRelease_(apr_pool_t *pool, id object) { | |
106 | if (object == nil) | |
107 | return nil; | |
108 | else if (pool == NULL) | |
109 | return [object autorelease]; | |
110 | else { | |
111 | apr_pool_cleanup_register(pool, object, &CYPoolRelease_, &apr_pool_cleanup_null); | |
112 | return object; | |
113 | } | |
114 | } | |
115 | ||
116 | template <typename Type_> | |
117 | Type_ CYPoolRelease(apr_pool_t *pool, Type_ object) { | |
118 | return (Type_) CYPoolRelease_(pool, (id) object); | |
119 | } | |
120 | /* }}} */ | |
121 | /* Objective-C Strings {{{ */ | |
122 | const char *CYPoolCString(apr_pool_t *pool, JSContextRef context, NSString *value) { | |
123 | if (pool == NULL) | |
124 | return [value UTF8String]; | |
125 | else { | |
126 | size_t size([value maximumLengthOfBytesUsingEncoding:NSUTF8StringEncoding] + 1); | |
127 | char *string(new(pool) char[size]); | |
128 | if (![value getCString:string maxLength:size encoding:NSUTF8StringEncoding]) | |
129 | throw CYJSError(context, "[NSString getCString:maxLength:encoding:] == NO"); | |
130 | return string; | |
131 | } | |
132 | } | |
133 | ||
134 | JSStringRef CYCopyJSString(JSContextRef context, NSString *value) { | |
135 | #ifdef __APPLE__ | |
136 | return JSStringCreateWithCFString(reinterpret_cast<CFStringRef>(value)); | |
137 | #else | |
138 | CYPool pool; | |
139 | return CYCopyJSString(CYPoolCString(pool, context, value)); | |
140 | #endif | |
141 | } | |
142 | ||
143 | JSStringRef CYCopyJSString(NSObject *value) { | |
144 | if (value == nil) | |
145 | return NULL; | |
146 | // XXX: this definition scares me; is anyone using this?! | |
147 | NSString *string([value description]); | |
148 | return CYCopyJSString(string); | |
149 | } | |
150 | ||
151 | NSString *CYCopyNSString(const CYUTF8String &value) { | |
152 | #ifdef __APPLE__ | |
153 | return (NSString *) CFStringCreateWithBytes(kCFAllocatorDefault, reinterpret_cast<const UInt8 *>(value.data), value.size, kCFStringEncodingUTF8, true); | |
154 | #else | |
155 | return [[NSString alloc] initWithBytes:value.data length:value.size encoding:NSUTF8StringEncoding]; | |
156 | #endif | |
157 | } | |
158 | ||
159 | NSString *CYCopyNSString(JSContextRef context, JSStringRef value) { | |
160 | #ifdef __APPLE__ | |
161 | return (NSString *) JSStringCopyCFString(kCFAllocatorDefault, value); | |
162 | #else | |
163 | CYPool pool; | |
164 | return CYCopyNSString(CYPoolUTF8String(pool, context, value)); | |
165 | #endif | |
166 | } | |
167 | ||
168 | NSString *CYCopyNSString(JSContextRef context, JSValueRef value) { | |
169 | return CYCopyNSString(context, CYJSString(context, value)); | |
170 | } | |
171 | ||
172 | NSString *CYCastNSString(apr_pool_t *pool, const CYUTF8String &value) { | |
173 | return CYPoolRelease(pool, CYCopyNSString(value)); | |
174 | } | |
175 | ||
176 | NSString *CYCastNSString(apr_pool_t *pool, SEL sel) { | |
177 | const char *name(sel_getName(sel)); | |
178 | return CYPoolRelease(pool, CYCopyNSString(CYUTF8String(name, strlen(name)))); | |
179 | } | |
180 | ||
181 | NSString *CYCastNSString(apr_pool_t *pool, JSContextRef context, JSStringRef value) { | |
182 | return CYPoolRelease(pool, CYCopyNSString(context, value)); | |
183 | } | |
184 | ||
185 | CYUTF8String CYCastUTF8String(NSString *value) { | |
186 | NSData *data([value dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:NO]); | |
187 | return CYUTF8String(reinterpret_cast<const char *>([data bytes]), [data length]); | |
188 | } | |
189 | /* }}} */ | |
190 | ||
191 | void CYThrow(JSContextRef context, NSException *error, JSValueRef *exception) { | |
192 | if (exception == NULL) | |
193 | throw error; | |
194 | *exception = CYCastJSValue(context, error); | |
195 | } | |
196 | ||
197 | size_t CYGetIndex(NSString *value) { | |
198 | return CYGetIndex(CYCastUTF8String(value)); | |
199 | } | |
200 | ||
201 | bool CYGetOffset(apr_pool_t *pool, JSContextRef context, NSString *value, ssize_t &index) { | |
202 | return CYGetOffset(CYPoolCString(pool, context, value), index); | |
203 | } | |
204 | ||
205 | static JSClassRef Instance_; | |
206 | static JSClassRef Internal_; | |
207 | static JSClassRef Message_; | |
208 | static JSClassRef Messages_; | |
209 | static JSClassRef Selector_; | |
210 | static JSClassRef Super_; | |
211 | ||
212 | static JSClassRef ObjectiveC_Classes_; | |
213 | static JSClassRef ObjectiveC_Protocols_; | |
214 | ||
215 | #ifdef __APPLE__ | |
216 | static JSClassRef ObjectiveC_Image_Classes_; | |
217 | static JSClassRef ObjectiveC_Images_; | |
218 | #endif | |
219 | ||
220 | static JSObjectRef Instance_prototype_; | |
221 | ||
222 | #ifdef __APPLE__ | |
223 | static Class NSCFBoolean_; | |
224 | static Class NSCFType_; | |
225 | static Class NSMessageBuilder_; | |
226 | static Class NSZombie_; | |
227 | #endif | |
228 | ||
229 | static Class NSArray_; | |
230 | static Class NSDictionary_; | |
231 | static Class Object_; | |
232 | ||
233 | static Type_privateData *Object_type; | |
234 | static Type_privateData *Selector_type; | |
235 | ||
236 | Type_privateData *Instance::GetType() const { | |
237 | return Object_type; | |
238 | } | |
239 | ||
240 | Type_privateData *Selector_privateData::GetType() const { | |
241 | return Selector_type; | |
242 | } | |
243 | ||
244 | // XXX: trick this out with associated objects! | |
245 | JSValueRef CYGetClassPrototype(JSContextRef context, id self) { | |
246 | if (self == nil) | |
247 | return Instance_prototype_; | |
248 | ||
249 | // XXX: I need to think through multi-context | |
250 | typedef std::map<id, JSValueRef> CacheMap; | |
251 | static CacheMap cache_; | |
252 | ||
253 | JSValueRef &value(cache_[self]); | |
254 | if (value != NULL) | |
255 | return value; | |
256 | ||
257 | JSClassRef _class(NULL); | |
258 | JSValueRef prototype; | |
259 | ||
260 | if (self == NSArray_) | |
261 | prototype = Array_prototype_; | |
262 | else if (self == NSDictionary_) | |
263 | prototype = Object_prototype_; | |
264 | else | |
265 | prototype = CYGetClassPrototype(context, class_getSuperclass(self)); | |
266 | ||
267 | JSObjectRef object(JSObjectMake(context, _class, NULL)); | |
268 | JSObjectSetPrototype(context, object, prototype); | |
269 | ||
270 | JSValueProtect(context, object); | |
271 | value = object; | |
272 | return object; | |
273 | } | |
274 | ||
275 | JSObjectRef Messages::Make(JSContextRef context, Class _class, bool array) { | |
276 | JSObjectRef value(JSObjectMake(context, Messages_, new Messages(_class))); | |
277 | if (_class == NSArray_) | |
278 | array = true; | |
279 | if (Class super = class_getSuperclass(_class)) | |
280 | JSObjectSetPrototype(context, value, Messages::Make(context, super, array)); | |
281 | /*else if (array) | |
282 | JSObjectSetPrototype(context, value, Array_prototype_);*/ | |
283 | return value; | |
284 | } | |
285 | ||
286 | JSObjectRef Internal::Make(JSContextRef context, id object, JSObjectRef owner) { | |
287 | return JSObjectMake(context, Internal_, new Internal(object, context, owner)); | |
288 | } | |
289 | ||
290 | namespace cy { | |
291 | JSObjectRef Super::Make(JSContextRef context, id object, Class _class) { | |
292 | JSObjectRef value(JSObjectMake(context, Super_, new Super(object, _class))); | |
293 | return value; | |
294 | } } | |
295 | ||
296 | JSObjectRef Instance::Make(JSContextRef context, id object, Flags flags) { | |
297 | JSObjectRef value(JSObjectMake(context, Instance_, new Instance(object, flags))); | |
298 | JSObjectSetPrototype(context, value, CYGetClassPrototype(context, object_getClass(object))); | |
299 | return value; | |
300 | } | |
301 | ||
302 | Instance::~Instance() { | |
303 | if ((flags_ & Transient) == 0) | |
304 | // XXX: does this handle background threads correctly? | |
305 | // XXX: this simply does not work on the console because I'm stupid | |
306 | [GetValue() performSelector:@selector(release) withObject:nil afterDelay:0]; | |
307 | } | |
308 | ||
309 | struct Message_privateData : | |
310 | cy::Functor | |
311 | { | |
312 | SEL sel_; | |
313 | ||
314 | Message_privateData(SEL sel, const char *type, IMP value = NULL) : | |
315 | cy::Functor(type, reinterpret_cast<void (*)()>(value)), | |
316 | sel_(sel) | |
317 | { | |
318 | } | |
319 | }; | |
320 | ||
321 | JSObjectRef CYMakeInstance(JSContextRef context, id object, bool transient) { | |
322 | Instance::Flags flags; | |
323 | ||
324 | if (transient) | |
325 | flags = Instance::Transient; | |
326 | else { | |
327 | flags = Instance::None; | |
328 | object = [object retain]; | |
329 | } | |
330 | ||
331 | return Instance::Make(context, object, flags); | |
332 | } | |
333 | ||
334 | @interface NSMethodSignature (Cycript) | |
335 | - (NSString *) _typeString; | |
336 | @end | |
337 | ||
338 | @interface NSObject (Cycript) | |
339 | ||
340 | - (JSValueRef) cy$JSValueInContext:(JSContextRef)context; | |
341 | - (JSType) cy$JSType; | |
342 | ||
343 | - (NSObject *) cy$toJSON:(NSString *)key; | |
344 | - (NSString *) cy$toCYON; | |
345 | - (NSString *) cy$toKey; | |
346 | ||
347 | - (bool) cy$hasProperty:(NSString *)name; | |
348 | - (NSObject *) cy$getProperty:(NSString *)name; | |
349 | - (bool) cy$setProperty:(NSString *)name to:(NSObject *)value; | |
350 | - (bool) cy$deleteProperty:(NSString *)name; | |
351 | - (void) cy$getPropertyNames:(JSPropertyNameAccumulatorRef)names; | |
352 | ||
353 | @end | |
354 | ||
355 | @protocol Cycript | |
356 | - (JSValueRef) cy$JSValueInContext:(JSContextRef)context; | |
357 | @end | |
358 | ||
359 | NSString *CYCastNSCYON(id value) { | |
360 | NSString *string; | |
361 | ||
362 | if (value == nil) | |
363 | string = @"nil"; | |
364 | else { | |
365 | Class _class(object_getClass(value)); | |
366 | SEL sel(@selector(cy$toCYON)); | |
367 | ||
368 | if (objc_method *toCYON = class_getInstanceMethod(_class, sel)) | |
369 | string = reinterpret_cast<NSString *(*)(id, SEL)>(method_getImplementation(toCYON))(value, sel); | |
370 | else if (objc_method *methodSignatureForSelector = class_getInstanceMethod(_class, @selector(methodSignatureForSelector:))) { | |
371 | if (reinterpret_cast<NSMethodSignature *(*)(id, SEL, SEL)>(method_getImplementation(methodSignatureForSelector))(value, @selector(methodSignatureForSelector:), sel) != nil) | |
372 | string = [value cy$toCYON]; | |
373 | else goto fail; | |
374 | } else fail: { | |
375 | if (false); | |
376 | #ifdef __APPLE__ | |
377 | else if (value == NSZombie_) | |
378 | string = @"_NSZombie_"; | |
379 | else if (_class == NSZombie_) | |
380 | string = [NSString stringWithFormat:@"<_NSZombie_: %p>", value]; | |
381 | // XXX: frowny /in/ the pants | |
382 | else if (value == NSMessageBuilder_ || value == Object_) | |
383 | string = nil; | |
384 | #endif | |
385 | else | |
386 | string = [NSString stringWithFormat:@"%@", value]; | |
387 | } | |
388 | ||
389 | // XXX: frowny pants | |
390 | if (string == nil) | |
391 | string = @"undefined"; | |
392 | } | |
393 | ||
394 | return string; | |
395 | } | |
396 | ||
397 | #ifdef __APPLE__ | |
398 | struct PropertyAttributes { | |
399 | CYPool pool_; | |
400 | ||
401 | const char *name; | |
402 | ||
403 | const char *variable; | |
404 | ||
405 | const char *getter_; | |
406 | const char *setter_; | |
407 | ||
408 | bool readonly; | |
409 | bool copy; | |
410 | bool retain; | |
411 | bool nonatomic; | |
412 | bool dynamic; | |
413 | bool weak; | |
414 | bool garbage; | |
415 | ||
416 | PropertyAttributes(objc_property_t property) : | |
417 | variable(NULL), | |
418 | getter_(NULL), | |
419 | setter_(NULL), | |
420 | readonly(false), | |
421 | copy(false), | |
422 | retain(false), | |
423 | nonatomic(false), | |
424 | dynamic(false), | |
425 | weak(false), | |
426 | garbage(false) | |
427 | { | |
428 | name = property_getName(property); | |
429 | const char *attributes(property_getAttributes(property)); | |
430 | ||
431 | for (char *state, *token(apr_strtok(apr_pstrdup(pool_, attributes), ",", &state)); token != NULL; token = apr_strtok(NULL, ",", &state)) { | |
432 | switch (*token) { | |
433 | case 'R': readonly = true; break; | |
434 | case 'C': copy = true; break; | |
435 | case '&': retain = true; break; | |
436 | case 'N': nonatomic = true; break; | |
437 | case 'G': getter_ = token + 1; break; | |
438 | case 'S': setter_ = token + 1; break; | |
439 | case 'V': variable = token + 1; break; | |
440 | } | |
441 | } | |
442 | ||
443 | /*if (variable == NULL) { | |
444 | variable = property_getName(property); | |
445 | size_t size(strlen(variable)); | |
446 | char *name(new(pool_) char[size + 2]); | |
447 | name[0] = '_'; | |
448 | memcpy(name + 1, variable, size); | |
449 | name[size + 1] = '\0'; | |
450 | variable = name; | |
451 | }*/ | |
452 | } | |
453 | ||
454 | const char *Getter() { | |
455 | if (getter_ == NULL) | |
456 | getter_ = apr_pstrdup(pool_, name); | |
457 | return getter_; | |
458 | } | |
459 | ||
460 | const char *Setter() { | |
461 | if (setter_ == NULL && !readonly) { | |
462 | size_t length(strlen(name)); | |
463 | ||
464 | char *temp(new(pool_) char[length + 5]); | |
465 | temp[0] = 's'; | |
466 | temp[1] = 'e'; | |
467 | temp[2] = 't'; | |
468 | ||
469 | if (length != 0) { | |
470 | temp[3] = toupper(name[0]); | |
471 | memcpy(temp + 4, name + 1, length - 1); | |
472 | } | |
473 | ||
474 | temp[length + 3] = ':'; | |
475 | temp[length + 4] = '\0'; | |
476 | setter_ = temp; | |
477 | } | |
478 | ||
479 | return setter_; | |
480 | } | |
481 | ||
482 | }; | |
483 | #endif | |
484 | ||
485 | #ifdef __APPLE__ | |
486 | NSObject *NSCFType$cy$toJSON(id self, SEL sel, NSString *key) { | |
487 | return [(NSString *) CFCopyDescription((CFTypeRef) self) autorelease]; | |
488 | } | |
489 | #endif | |
490 | ||
491 | #ifndef __APPLE__ | |
492 | @interface CYWebUndefined : NSObject { | |
493 | } | |
494 | ||
495 | + (CYWebUndefined *) undefined; | |
496 | ||
497 | @end | |
498 | ||
499 | @implementation CYWebUndefined | |
500 | ||
501 | + (CYWebUndefined *) undefined { | |
502 | static CYWebUndefined *instance_([[CYWebUndefined alloc] init]); | |
503 | return instance_; | |
504 | } | |
505 | ||
506 | @end | |
507 | ||
508 | #define WebUndefined CYWebUndefined | |
509 | #endif | |
510 | ||
511 | /* Bridge: CYJSObject {{{ */ | |
512 | @interface CYJSObject : NSMutableDictionary { | |
513 | JSObjectRef object_; | |
514 | JSContextRef context_; | |
515 | } | |
516 | ||
517 | - (id) initWithJSObject:(JSObjectRef)object inContext:(JSContextRef)context; | |
518 | ||
519 | - (NSObject *) cy$toJSON:(NSString *)key; | |
520 | ||
521 | - (NSUInteger) count; | |
522 | - (id) objectForKey:(id)key; | |
523 | - (NSEnumerator *) keyEnumerator; | |
524 | - (void) setObject:(id)object forKey:(id)key; | |
525 | - (void) removeObjectForKey:(id)key; | |
526 | ||
527 | @end | |
528 | /* }}} */ | |
529 | /* Bridge: CYJSArray {{{ */ | |
530 | @interface CYJSArray : NSMutableArray { | |
531 | JSObjectRef object_; | |
532 | JSContextRef context_; | |
533 | } | |
534 | ||
535 | - (id) initWithJSObject:(JSObjectRef)object inContext:(JSContextRef)context; | |
536 | ||
537 | - (NSUInteger) count; | |
538 | - (id) objectAtIndex:(NSUInteger)index; | |
539 | ||
540 | - (void) addObject:(id)anObject; | |
541 | - (void) insertObject:(id)anObject atIndex:(NSUInteger)index; | |
542 | - (void) removeLastObject; | |
543 | - (void) removeObjectAtIndex:(NSUInteger)index; | |
544 | - (void) replaceObjectAtIndex:(NSUInteger)index withObject:(id)anObject; | |
545 | ||
546 | @end | |
547 | /* }}} */ | |
548 | ||
549 | NSObject *CYCastNSObject_(apr_pool_t *pool, JSContextRef context, JSObjectRef object) { | |
550 | JSValueRef exception(NULL); | |
551 | bool array(JSValueIsInstanceOfConstructor(context, object, Array_, &exception)); | |
552 | CYThrow(context, exception); | |
553 | id value(array ? [CYJSArray alloc] : [CYJSObject alloc]); | |
554 | return CYPoolRelease(pool, [value initWithJSObject:object inContext:context]); | |
555 | } | |
556 | ||
557 | NSObject *CYCastNSObject(apr_pool_t *pool, JSContextRef context, JSObjectRef object) { | |
558 | if (!JSValueIsObjectOfClass(context, object, Instance_)) | |
559 | return CYCastNSObject_(pool, context, object); | |
560 | else { | |
561 | Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object))); | |
562 | return internal->GetValue(); | |
563 | } | |
564 | } | |
565 | ||
566 | NSNumber *CYCopyNSNumber(JSContextRef context, JSValueRef value) { | |
567 | return [[NSNumber alloc] initWithDouble:CYCastDouble(context, value)]; | |
568 | } | |
569 | ||
570 | id CYNSObject(apr_pool_t *pool, JSContextRef context, JSValueRef value, bool cast) { | |
571 | id object; | |
572 | bool copy; | |
573 | ||
574 | switch (JSType type = JSValueGetType(context, value)) { | |
575 | case kJSTypeUndefined: | |
576 | object = [WebUndefined undefined]; | |
577 | copy = false; | |
578 | break; | |
579 | ||
580 | case kJSTypeNull: | |
581 | return NULL; | |
582 | break; | |
583 | ||
584 | case kJSTypeBoolean: | |
585 | #ifdef __APPLE__ | |
586 | object = (id) (CYCastBool(context, value) ? kCFBooleanTrue : kCFBooleanFalse); | |
587 | copy = false; | |
588 | #else | |
589 | object = [[NSNumber alloc] initWithBool:CYCastBool(context, value)]; | |
590 | copy = true; | |
591 | #endif | |
592 | break; | |
593 | ||
594 | case kJSTypeNumber: | |
595 | object = CYCopyNSNumber(context, value); | |
596 | copy = true; | |
597 | break; | |
598 | ||
599 | case kJSTypeString: | |
600 | object = CYCopyNSString(context, value); | |
601 | copy = true; | |
602 | break; | |
603 | ||
604 | case kJSTypeObject: | |
605 | // XXX: this might could be more efficient | |
606 | object = CYCastNSObject(pool, context, (JSObjectRef) value); | |
607 | copy = false; | |
608 | break; | |
609 | ||
610 | default: | |
611 | throw CYJSError(context, "JSValueGetType() == 0x%x", type); | |
612 | break; | |
613 | } | |
614 | ||
615 | if (cast != copy) | |
616 | return object; | |
617 | else if (copy) | |
618 | return CYPoolRelease(pool, object); | |
619 | else | |
620 | return [object retain]; | |
621 | } | |
622 | ||
623 | NSObject *CYCastNSObject(apr_pool_t *pool, JSContextRef context, JSValueRef value) { | |
624 | return CYNSObject(pool, context, value, true); | |
625 | } | |
626 | ||
627 | NSObject *CYCopyNSObject(apr_pool_t *pool, JSContextRef context, JSValueRef value) { | |
628 | return CYNSObject(pool, context, value, false); | |
629 | } | |
630 | ||
631 | /* Bridge: NSArray {{{ */ | |
632 | @implementation NSArray (Cycript) | |
633 | ||
634 | - (NSString *) cy$toCYON { | |
635 | NSMutableString *json([[[NSMutableString alloc] init] autorelease]); | |
636 | [json appendString:@"["]; | |
637 | ||
638 | bool comma(false); | |
639 | #ifdef __APPLE__ | |
640 | for (id object in self) { | |
641 | #else | |
642 | for (size_t index(0), count([self count]); index != count; ++index) { | |
643 | id object([self objectAtIndex:index]); | |
644 | #endif | |
645 | if (comma) | |
646 | [json appendString:@","]; | |
647 | else | |
648 | comma = true; | |
649 | if (object == nil || [object cy$JSType] != kJSTypeUndefined) | |
650 | [json appendString:CYCastNSCYON(object)]; | |
651 | else { | |
652 | [json appendString:@","]; | |
653 | comma = false; | |
654 | } | |
655 | } | |
656 | ||
657 | [json appendString:@"]"]; | |
658 | return json; | |
659 | } | |
660 | ||
661 | - (bool) cy$hasProperty:(NSString *)name { | |
662 | if ([name isEqualToString:@"length"]) | |
663 | return true; | |
664 | ||
665 | size_t index(CYGetIndex(name)); | |
666 | if (index == _not(size_t) || index >= [self count]) | |
667 | return [super cy$hasProperty:name]; | |
668 | else | |
669 | return true; | |
670 | } | |
671 | ||
672 | - (NSObject *) cy$getProperty:(NSString *)name { | |
673 | if ([name isEqualToString:@"length"]) { | |
674 | NSUInteger count([self count]); | |
675 | #ifdef __APPLE__ | |
676 | return [NSNumber numberWithUnsignedInteger:count]; | |
677 | #else | |
678 | return [NSNumber numberWithUnsignedInt:count]; | |
679 | #endif | |
680 | } | |
681 | ||
682 | size_t index(CYGetIndex(name)); | |
683 | if (index == _not(size_t) || index >= [self count]) | |
684 | return [super cy$getProperty:name]; | |
685 | else | |
686 | return [self objectAtIndex:index]; | |
687 | } | |
688 | ||
689 | - (void) cy$getPropertyNames:(JSPropertyNameAccumulatorRef)names { | |
690 | [super cy$getPropertyNames:names]; | |
691 | ||
692 | for (size_t index(0), count([self count]); index != count; ++index) { | |
693 | id object([self objectAtIndex:index]); | |
694 | if (object == nil || [object cy$JSType] != kJSTypeUndefined) { | |
695 | char name[32]; | |
696 | sprintf(name, "%zu", index); | |
697 | JSPropertyNameAccumulatorAddName(names, CYJSString(name)); | |
698 | } | |
699 | } | |
700 | } | |
701 | ||
702 | @end | |
703 | /* }}} */ | |
704 | /* Bridge: NSDictionary {{{ */ | |
705 | @implementation NSDictionary (Cycript) | |
706 | ||
707 | - (NSString *) cy$toCYON { | |
708 | NSMutableString *json([[[NSMutableString alloc] init] autorelease]); | |
709 | [json appendString:@"{"]; | |
710 | ||
711 | bool comma(false); | |
712 | #ifdef __APPLE__ | |
713 | for (id key in self) { | |
714 | #else | |
715 | NSEnumerator *keys([self keyEnumerator]); | |
716 | while (id key = [keys nextObject]) { | |
717 | #endif | |
718 | if (comma) | |
719 | [json appendString:@","]; | |
720 | else | |
721 | comma = true; | |
722 | [json appendString:[key cy$toKey]]; | |
723 | [json appendString:@":"]; | |
724 | NSObject *object([self objectForKey:key]); | |
725 | [json appendString:CYCastNSCYON(object)]; | |
726 | } | |
727 | ||
728 | [json appendString:@"}"]; | |
729 | return json; | |
730 | } | |
731 | ||
732 | - (bool) cy$hasProperty:(NSString *)name { | |
733 | return [self objectForKey:name] != nil; | |
734 | } | |
735 | ||
736 | - (NSObject *) cy$getProperty:(NSString *)name { | |
737 | return [self objectForKey:name]; | |
738 | } | |
739 | ||
740 | - (void) cy$getPropertyNames:(JSPropertyNameAccumulatorRef)names { | |
741 | [super cy$getPropertyNames:names]; | |
742 | ||
743 | #ifdef __APPLE__ | |
744 | for (NSString *key in self) { | |
745 | #else | |
746 | NSEnumerator *keys([self keyEnumerator]); | |
747 | while (NSString *key = [keys nextObject]) { | |
748 | #endif | |
749 | JSPropertyNameAccumulatorAddName(names, CYJSString(key)); | |
750 | } | |
751 | } | |
752 | ||
753 | @end | |
754 | /* }}} */ | |
755 | /* Bridge: NSMutableArray {{{ */ | |
756 | @implementation NSMutableArray (Cycript) | |
757 | ||
758 | - (bool) cy$setProperty:(NSString *)name to:(NSObject *)value { | |
759 | if ([name isEqualToString:@"length"]) { | |
760 | // XXX: is this not intelligent? | |
761 | NSNumber *number(reinterpret_cast<NSNumber *>(value)); | |
762 | #ifdef __APPLE__ | |
763 | NSUInteger size([number unsignedIntegerValue]); | |
764 | #else | |
765 | NSUInteger size([number unsignedIntValue]); | |
766 | #endif | |
767 | NSUInteger count([self count]); | |
768 | if (size < count) | |
769 | [self removeObjectsInRange:NSMakeRange(size, count - size)]; | |
770 | else if (size != count) { | |
771 | WebUndefined *undefined([WebUndefined undefined]); | |
772 | for (size_t i(count); i != size; ++i) | |
773 | [self addObject:undefined]; | |
774 | } | |
775 | return true; | |
776 | } | |
777 | ||
778 | size_t index(CYGetIndex(name)); | |
779 | if (index == _not(size_t)) | |
780 | return [super cy$setProperty:name to:value]; | |
781 | ||
782 | id object(value ?: [NSNull null]); | |
783 | ||
784 | size_t count([self count]); | |
785 | if (index < count) | |
786 | [self replaceObjectAtIndex:index withObject:object]; | |
787 | else { | |
788 | if (index != count) { | |
789 | WebUndefined *undefined([WebUndefined undefined]); | |
790 | for (size_t i(count); i != index; ++i) | |
791 | [self addObject:undefined]; | |
792 | } | |
793 | ||
794 | [self addObject:object]; | |
795 | } | |
796 | ||
797 | return true; | |
798 | } | |
799 | ||
800 | - (bool) cy$deleteProperty:(NSString *)name { | |
801 | size_t index(CYGetIndex(name)); | |
802 | if (index == _not(size_t) || index >= [self count]) | |
803 | return [super cy$deleteProperty:name]; | |
804 | [self replaceObjectAtIndex:index withObject:[WebUndefined undefined]]; | |
805 | return true; | |
806 | } | |
807 | ||
808 | @end | |
809 | /* }}} */ | |
810 | /* Bridge: NSMutableDictionary {{{ */ | |
811 | @implementation NSMutableDictionary (Cycript) | |
812 | ||
813 | - (bool) cy$setProperty:(NSString *)name to:(NSObject *)value { | |
814 | [self setObject:(value ?: [NSNull null]) forKey:name]; | |
815 | return true; | |
816 | } | |
817 | ||
818 | - (bool) cy$deleteProperty:(NSString *)name { | |
819 | if ([self objectForKey:name] == nil) | |
820 | return false; | |
821 | else { | |
822 | [self removeObjectForKey:name]; | |
823 | return true; | |
824 | } | |
825 | } | |
826 | ||
827 | @end | |
828 | /* }}} */ | |
829 | /* Bridge: NSNumber {{{ */ | |
830 | @implementation NSNumber (Cycript) | |
831 | ||
832 | - (JSType) cy$JSType { | |
833 | #ifdef __APPLE__ | |
834 | // XXX: this just seems stupid | |
835 | if ([self class] == NSCFBoolean_) | |
836 | return kJSTypeBoolean; | |
837 | #endif | |
838 | return kJSTypeNumber; | |
839 | } | |
840 | ||
841 | - (NSObject *) cy$toJSON:(NSString *)key { | |
842 | return self; | |
843 | } | |
844 | ||
845 | - (NSString *) cy$toCYON { | |
846 | return [self cy$JSType] != kJSTypeBoolean ? [self stringValue] : [self boolValue] ? @"true" : @"false"; | |
847 | } | |
848 | ||
849 | - (JSValueRef) cy$JSValueInContext:(JSContextRef)context { CYObjectiveTry_(context) { | |
850 | return [self cy$JSType] != kJSTypeBoolean ? CYCastJSValue(context, [self doubleValue]) : CYCastJSValue(context, [self boolValue]); | |
851 | } CYObjectiveCatch } | |
852 | ||
853 | @end | |
854 | /* }}} */ | |
855 | /* Bridge: NSNull {{{ */ | |
856 | @implementation NSNull (Cycript) | |
857 | ||
858 | - (JSType) cy$JSType { | |
859 | return kJSTypeNull; | |
860 | } | |
861 | ||
862 | - (NSObject *) cy$toJSON:(NSString *)key { | |
863 | return self; | |
864 | } | |
865 | ||
866 | - (NSString *) cy$toCYON { | |
867 | return @"null"; | |
868 | } | |
869 | ||
870 | @end | |
871 | /* }}} */ | |
872 | /* Bridge: NSObject {{{ */ | |
873 | @implementation NSObject (Cycript) | |
874 | ||
875 | - (JSValueRef) cy$JSValueInContext:(JSContextRef)context { CYObjectiveTry_(context) { | |
876 | return CYMakeInstance(context, self, false); | |
877 | } CYObjectiveCatch } | |
878 | ||
879 | - (JSType) cy$JSType { | |
880 | return kJSTypeObject; | |
881 | } | |
882 | ||
883 | - (NSObject *) cy$toJSON:(NSString *)key { | |
884 | return [self description]; | |
885 | } | |
886 | ||
887 | - (NSString *) cy$toCYON { | |
888 | return [[self cy$toJSON:@""] cy$toCYON]; | |
889 | } | |
890 | ||
891 | - (NSString *) cy$toKey { | |
892 | return [self cy$toCYON]; | |
893 | } | |
894 | ||
895 | - (bool) cy$hasProperty:(NSString *)name { | |
896 | return false; | |
897 | } | |
898 | ||
899 | - (NSObject *) cy$getProperty:(NSString *)name { | |
900 | return nil; | |
901 | } | |
902 | ||
903 | - (bool) cy$setProperty:(NSString *)name to:(NSObject *)value { | |
904 | return false; | |
905 | } | |
906 | ||
907 | - (bool) cy$deleteProperty:(NSString *)name { | |
908 | return false; | |
909 | } | |
910 | ||
911 | - (void) cy$getPropertyNames:(JSPropertyNameAccumulatorRef)names { | |
912 | } | |
913 | ||
914 | @end | |
915 | /* }}} */ | |
916 | /* Bridge: NSProxy {{{ */ | |
917 | @implementation NSProxy (Cycript) | |
918 | ||
919 | - (NSObject *) cy$toJSON:(NSString *)key { | |
920 | return [self description]; | |
921 | } | |
922 | ||
923 | - (NSString *) cy$toCYON { | |
924 | return [[self cy$toJSON:@""] cy$toCYON]; | |
925 | } | |
926 | ||
927 | @end | |
928 | /* }}} */ | |
929 | /* Bridge: NSString {{{ */ | |
930 | @implementation NSString (Cycript) | |
931 | ||
932 | - (JSType) cy$JSType { | |
933 | return kJSTypeString; | |
934 | } | |
935 | ||
936 | - (NSObject *) cy$toJSON:(NSString *)key { | |
937 | return self; | |
938 | } | |
939 | ||
940 | - (NSString *) cy$toCYON { | |
941 | std::ostringstream str; | |
942 | CYUTF8String string(CYCastUTF8String(self)); | |
943 | CYStringify(str, string.data, string.size); | |
944 | std::string value(str.str()); | |
945 | return CYCastNSString(NULL, CYUTF8String(value.c_str(), value.size())); | |
946 | } | |
947 | ||
948 | - (NSString *) cy$toKey { | |
949 | if (CYIsKey(CYCastUTF8String(self))) | |
950 | return self; | |
951 | return [self cy$toCYON]; | |
952 | } | |
953 | ||
954 | @end | |
955 | /* }}} */ | |
956 | /* Bridge: WebUndefined {{{ */ | |
957 | @implementation WebUndefined (Cycript) | |
958 | ||
959 | - (JSType) cy$JSType { | |
960 | return kJSTypeUndefined; | |
961 | } | |
962 | ||
963 | - (NSObject *) cy$toJSON:(NSString *)key { | |
964 | return self; | |
965 | } | |
966 | ||
967 | - (NSString *) cy$toCYON { | |
968 | return @"undefined"; | |
969 | } | |
970 | ||
971 | - (JSValueRef) cy$JSValueInContext:(JSContextRef)context { CYObjectiveTry_(context) { | |
972 | return CYJSUndefined(context); | |
973 | } CYObjectiveCatch } | |
974 | ||
975 | @end | |
976 | /* }}} */ | |
977 | ||
978 | static bool CYIsClass(id self) { | |
979 | #ifdef __APPLE__ | |
980 | // XXX: this is a lame object_isClass | |
981 | return class_getInstanceMethod(object_getClass(self), @selector(alloc)) != NULL; | |
982 | #else | |
983 | return GSObjCIsClass(self); | |
984 | #endif | |
985 | } | |
986 | ||
987 | Class CYCastClass(apr_pool_t *pool, JSContextRef context, JSValueRef value) { | |
988 | id self(CYCastNSObject(pool, context, value)); | |
989 | if (CYIsClass(self)) | |
990 | return (Class) self; | |
991 | throw CYJSError(context, "got something that is not a Class"); | |
992 | return NULL; | |
993 | } | |
994 | ||
995 | NSArray *CYCastNSArray(JSContextRef context, JSPropertyNameArrayRef names) { | |
996 | CYPool pool; | |
997 | size_t size(JSPropertyNameArrayGetCount(names)); | |
998 | NSMutableArray *array([NSMutableArray arrayWithCapacity:size]); | |
999 | for (size_t index(0); index != size; ++index) | |
1000 | [array addObject:CYCastNSString(pool, context, JSPropertyNameArrayGetNameAtIndex(names, index))]; | |
1001 | return array; | |
1002 | } | |
1003 | ||
1004 | JSValueRef CYCastJSValue(JSContextRef context, id value) { CYPoolTry { | |
1005 | if (value == nil) | |
1006 | return CYJSNull(context); | |
1007 | else if ([value respondsToSelector:@selector(cy$JSValueInContext:)]) | |
1008 | return [value cy$JSValueInContext:context]; | |
1009 | else | |
1010 | return CYMakeInstance(context, value, false); | |
1011 | } CYPoolCatch(NULL) } | |
1012 | ||
1013 | @implementation CYJSObject | |
1014 | ||
1015 | - (id) initWithJSObject:(JSObjectRef)object inContext:(JSContextRef)context { CYObjectiveTry { | |
1016 | if ((self = [super init]) != nil) { | |
1017 | object_ = object; | |
1018 | context_ = context; | |
1019 | JSValueProtect(context_, object_); | |
1020 | } return self; | |
1021 | } CYObjectiveCatch } | |
1022 | ||
1023 | - (void) dealloc { CYObjectiveTry { | |
1024 | JSValueUnprotect(context_, object_); | |
1025 | [super dealloc]; | |
1026 | } CYObjectiveCatch } | |
1027 | ||
1028 | - (NSObject *) cy$toJSON:(NSString *)key { CYObjectiveTry { | |
1029 | JSValueRef toJSON(CYGetProperty(context_, object_, toJSON_)); | |
1030 | if (!CYIsCallable(context_, toJSON)) | |
1031 | return [super cy$toJSON:key]; | |
1032 | else { | |
1033 | JSValueRef arguments[1] = {CYCastJSValue(context_, key)}; | |
1034 | JSValueRef value(CYCallAsFunction(context_, (JSObjectRef) toJSON, object_, 1, arguments)); | |
1035 | // XXX: do I really want an NSNull here?! | |
1036 | return CYCastNSObject(NULL, context_, value) ?: [NSNull null]; | |
1037 | } | |
1038 | } CYObjectiveCatch } | |
1039 | ||
1040 | - (NSString *) cy$toCYON { CYObjectiveTry { | |
1041 | CYPool pool; | |
1042 | JSValueRef exception(NULL); | |
1043 | const char *cyon(CYPoolCCYON(pool, context_, object_)); | |
1044 | CYThrow(context_, exception); | |
1045 | if (cyon == NULL) | |
1046 | return [super cy$toCYON]; | |
1047 | else | |
1048 | return [NSString stringWithUTF8String:cyon]; | |
1049 | } CYObjectiveCatch } | |
1050 | ||
1051 | - (NSUInteger) count { CYObjectiveTry { | |
1052 | JSPropertyNameArrayRef names(JSObjectCopyPropertyNames(context_, object_)); | |
1053 | size_t size(JSPropertyNameArrayGetCount(names)); | |
1054 | JSPropertyNameArrayRelease(names); | |
1055 | return size; | |
1056 | } CYObjectiveCatch } | |
1057 | ||
1058 | - (id) objectForKey:(id)key { CYObjectiveTry { | |
1059 | // XXX: are NSDictionary keys always NSString *? | |
1060 | JSValueRef value(CYGetProperty(context_, object_, CYJSString((NSString *) key))); | |
1061 | if (JSValueIsUndefined(context_, value)) | |
1062 | return nil; | |
1063 | return CYCastNSObject(NULL, context_, value) ?: [NSNull null]; | |
1064 | } CYObjectiveCatch } | |
1065 | ||
1066 | - (NSEnumerator *) keyEnumerator { CYObjectiveTry { | |
1067 | JSPropertyNameArrayRef names(JSObjectCopyPropertyNames(context_, object_)); | |
1068 | NSEnumerator *enumerator([CYCastNSArray(context_, names) objectEnumerator]); | |
1069 | JSPropertyNameArrayRelease(names); | |
1070 | return enumerator; | |
1071 | } CYObjectiveCatch } | |
1072 | ||
1073 | - (void) setObject:(id)object forKey:(id)key { CYObjectiveTry { | |
1074 | // XXX: are NSDictionary keys always NSString *? | |
1075 | CYSetProperty(context_, object_, CYJSString((NSString *) key), CYCastJSValue(context_, object)); | |
1076 | } CYObjectiveCatch } | |
1077 | ||
1078 | - (void) removeObjectForKey:(id)key { CYObjectiveTry { | |
1079 | JSValueRef exception(NULL); | |
1080 | // XXX: are NSDictionary keys always NSString *? | |
1081 | (void) JSObjectDeleteProperty(context_, object_, CYJSString((NSString *) key), &exception); | |
1082 | CYThrow(context_, exception); | |
1083 | } CYObjectiveCatch } | |
1084 | ||
1085 | @end | |
1086 | ||
1087 | @implementation CYJSArray | |
1088 | ||
1089 | - (id) initWithJSObject:(JSObjectRef)object inContext:(JSContextRef)context { CYObjectiveTry { | |
1090 | if ((self = [super init]) != nil) { | |
1091 | object_ = object; | |
1092 | context_ = context; | |
1093 | JSValueProtect(context_, object_); | |
1094 | } return self; | |
1095 | } CYObjectiveCatch } | |
1096 | ||
1097 | - (void) dealloc { CYObjectiveTry { | |
1098 | JSValueUnprotect(context_, object_); | |
1099 | [super dealloc]; | |
1100 | } CYObjectiveCatch } | |
1101 | ||
1102 | - (NSUInteger) count { CYObjectiveTry { | |
1103 | return CYCastDouble(context_, CYGetProperty(context_, object_, length_)); | |
1104 | } CYObjectiveCatch } | |
1105 | ||
1106 | - (id) objectAtIndex:(NSUInteger)index { CYObjectiveTry { | |
1107 | size_t bounds([self count]); | |
1108 | if (index >= bounds) | |
1109 | @throw [NSException exceptionWithName:NSRangeException reason:[NSString stringWithFormat:@"*** -[CYJSArray objectAtIndex:]: index (%zu) beyond bounds (%zu)", index, bounds] userInfo:nil]; | |
1110 | JSValueRef exception(NULL); | |
1111 | JSValueRef value(JSObjectGetPropertyAtIndex(context_, object_, index, &exception)); | |
1112 | CYThrow(context_, exception); | |
1113 | return CYCastNSObject(NULL, context_, value) ?: [NSNull null]; | |
1114 | } CYObjectiveCatch } | |
1115 | ||
1116 | - (void) addObject:(id)object { CYObjectiveTry { | |
1117 | JSValueRef exception(NULL); | |
1118 | JSValueRef arguments[1]; | |
1119 | arguments[0] = CYCastJSValue(context_, object); | |
1120 | JSObjectCallAsFunction(context_, Array_push_, object_, 1, arguments, &exception); | |
1121 | CYThrow(context_, exception); | |
1122 | } CYObjectiveCatch } | |
1123 | ||
1124 | - (void) insertObject:(id)object atIndex:(NSUInteger)index { CYObjectiveTry { | |
1125 | size_t bounds([self count] + 1); | |
1126 | if (index >= bounds) | |
1127 | @throw [NSException exceptionWithName:NSRangeException reason:[NSString stringWithFormat:@"*** -[CYJSArray insertObject:atIndex:]: index (%zu) beyond bounds (%zu)", index, bounds] userInfo:nil]; | |
1128 | JSValueRef exception(NULL); | |
1129 | JSValueRef arguments[3]; | |
1130 | arguments[0] = CYCastJSValue(context_, index); | |
1131 | arguments[1] = CYCastJSValue(context_, 0); | |
1132 | arguments[2] = CYCastJSValue(context_, object); | |
1133 | JSObjectCallAsFunction(context_, Array_splice_, object_, 3, arguments, &exception); | |
1134 | CYThrow(context_, exception); | |
1135 | } CYObjectiveCatch } | |
1136 | ||
1137 | - (void) removeLastObject { CYObjectiveTry { | |
1138 | JSValueRef exception(NULL); | |
1139 | JSObjectCallAsFunction(context_, Array_pop_, object_, 0, NULL, &exception); | |
1140 | CYThrow(context_, exception); | |
1141 | } CYObjectiveCatch } | |
1142 | ||
1143 | - (void) removeObjectAtIndex:(NSUInteger)index { CYObjectiveTry { | |
1144 | size_t bounds([self count]); | |
1145 | if (index >= bounds) | |
1146 | @throw [NSException exceptionWithName:NSRangeException reason:[NSString stringWithFormat:@"*** -[CYJSArray removeObjectAtIndex:]: index (%zu) beyond bounds (%zu)", index, bounds] userInfo:nil]; | |
1147 | JSValueRef exception(NULL); | |
1148 | JSValueRef arguments[2]; | |
1149 | arguments[0] = CYCastJSValue(context_, index); | |
1150 | arguments[1] = CYCastJSValue(context_, 1); | |
1151 | JSObjectCallAsFunction(context_, Array_splice_, object_, 2, arguments, &exception); | |
1152 | CYThrow(context_, exception); | |
1153 | } CYObjectiveCatch } | |
1154 | ||
1155 | - (void) replaceObjectAtIndex:(NSUInteger)index withObject:(id)object { CYObjectiveTry { | |
1156 | size_t bounds([self count]); | |
1157 | if (index >= bounds) | |
1158 | @throw [NSException exceptionWithName:NSRangeException reason:[NSString stringWithFormat:@"*** -[CYJSArray replaceObjectAtIndex:withObject:]: index (%zu) beyond bounds (%zu)", index, bounds] userInfo:nil]; | |
1159 | CYSetProperty(context_, object_, index, CYCastJSValue(context_, object)); | |
1160 | } CYObjectiveCatch } | |
1161 | ||
1162 | @end | |
1163 | ||
1164 | // XXX: use objc_getAssociatedObject and objc_setAssociatedObject on 10.6 | |
1165 | struct CYInternal : | |
1166 | CYData | |
1167 | { | |
1168 | JSObjectRef object_; | |
1169 | ||
1170 | CYInternal() : | |
1171 | object_(NULL) | |
1172 | { | |
1173 | } | |
1174 | ||
1175 | ~CYInternal() { | |
1176 | // XXX: delete object_? ;( | |
1177 | } | |
1178 | ||
1179 | static CYInternal *Get(id self) { | |
1180 | CYInternal *internal(NULL); | |
1181 | if (object_getInstanceVariable(self, "cy$internal_", reinterpret_cast<void **>(&internal)) == NULL) { | |
1182 | // XXX: do something epic? ;P | |
1183 | } | |
1184 | ||
1185 | return internal; | |
1186 | } | |
1187 | ||
1188 | static CYInternal *Set(id self) { | |
1189 | CYInternal *internal(NULL); | |
1190 | if (objc_ivar *ivar = object_getInstanceVariable(self, "cy$internal_", reinterpret_cast<void **>(&internal))) { | |
1191 | if (internal == NULL) { | |
1192 | internal = new CYInternal(); | |
1193 | object_setIvar(self, ivar, reinterpret_cast<id>(internal)); | |
1194 | } | |
1195 | } else { | |
1196 | // XXX: do something epic? ;P | |
1197 | } | |
1198 | ||
1199 | return internal; | |
1200 | } | |
1201 | ||
1202 | bool HasProperty(JSContextRef context, JSStringRef name) { | |
1203 | if (object_ == NULL) | |
1204 | return false; | |
1205 | return JSObjectHasProperty(context, object_, name); | |
1206 | } | |
1207 | ||
1208 | JSValueRef GetProperty(JSContextRef context, JSStringRef name) { | |
1209 | if (object_ == NULL) | |
1210 | return NULL; | |
1211 | return CYGetProperty(context, object_, name); | |
1212 | } | |
1213 | ||
1214 | void SetProperty(JSContextRef context, JSStringRef name, JSValueRef value) { | |
1215 | if (object_ == NULL) | |
1216 | object_ = JSObjectMake(context, NULL, NULL); | |
1217 | CYSetProperty(context, object_, name, value); | |
1218 | } | |
1219 | }; | |
1220 | ||
1221 | static JSObjectRef CYMakeSelector(JSContextRef context, SEL sel) { | |
1222 | Selector_privateData *internal(new Selector_privateData(sel)); | |
1223 | return JSObjectMake(context, Selector_, internal); | |
1224 | } | |
1225 | ||
1226 | static SEL CYCastSEL(JSContextRef context, JSValueRef value) { | |
1227 | if (JSValueIsObjectOfClass(context, value, Selector_)) { | |
1228 | Selector_privateData *internal(reinterpret_cast<Selector_privateData *>(JSObjectGetPrivate((JSObjectRef) value))); | |
1229 | return reinterpret_cast<SEL>(internal->value_); | |
1230 | } else | |
1231 | return CYCastPointer<SEL>(context, value); | |
1232 | } | |
1233 | ||
1234 | void *CYObjectiveC_ExecuteStart(JSContextRef context) { | |
1235 | // XXX: deal with exceptions! | |
1236 | return (void *) [[NSAutoreleasePool alloc] init]; | |
1237 | } | |
1238 | ||
1239 | void CYObjectiveC_ExecuteEnd(JSContextRef context, void *handle) { | |
1240 | // XXX: deal with exceptions! | |
1241 | return [(NSAutoreleasePool *) handle release]; | |
1242 | } | |
1243 | ||
1244 | JSValueRef CYObjectiveC_RuntimeProperty(JSContextRef context, CYUTF8String name) { CYObjectiveTry_(context) { | |
1245 | if (name == "nil") | |
1246 | return Instance::Make(context, nil); | |
1247 | if (Class _class = objc_getClass(name.data)) | |
1248 | return CYMakeInstance(context, _class, true); | |
1249 | return NULL; | |
1250 | } CYObjectiveCatch } | |
1251 | ||
1252 | static void CYObjectiveC_CallFunction(JSContextRef context, ffi_cif *cif, void (*function)(), uint8_t *value, void **values) { CYObjectiveTry_(context) { | |
1253 | ffi_call(cif, function, value, values); | |
1254 | } CYObjectiveCatch } | |
1255 | ||
1256 | static bool CYObjectiveC_PoolFFI(apr_pool_t *pool, JSContextRef context, sig::Type *type, ffi_type *ffi, void *data, JSValueRef value) { CYObjectiveTry_(context) { | |
1257 | switch (type->primitive) { | |
1258 | case sig::object_P: | |
1259 | case sig::typename_P: | |
1260 | *reinterpret_cast<id *>(data) = CYCastNSObject(pool, context, value); | |
1261 | break; | |
1262 | ||
1263 | case sig::selector_P: | |
1264 | *reinterpret_cast<SEL *>(data) = CYCastSEL(context, value); | |
1265 | break; | |
1266 | ||
1267 | default: | |
1268 | return false; | |
1269 | } | |
1270 | ||
1271 | return true; | |
1272 | } CYObjectiveCatch } | |
1273 | ||
1274 | static JSValueRef CYObjectiveC_FromFFI(JSContextRef context, sig::Type *type, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) { CYObjectiveTry_(context) { | |
1275 | switch (type->primitive) { | |
1276 | case sig::object_P: | |
1277 | if (id object = *reinterpret_cast<id *>(data)) { | |
1278 | JSValueRef value(CYCastJSValue(context, object)); | |
1279 | if (initialize) | |
1280 | [object release]; | |
1281 | return value; | |
1282 | } else goto null; | |
1283 | ||
1284 | case sig::typename_P: | |
1285 | return CYMakeInstance(context, *reinterpret_cast<Class *>(data), true); | |
1286 | ||
1287 | case sig::selector_P: | |
1288 | if (SEL sel = *reinterpret_cast<SEL *>(data)) | |
1289 | return CYMakeSelector(context, sel); | |
1290 | else goto null; | |
1291 | ||
1292 | null: | |
1293 | return CYJSNull(context); | |
1294 | default: | |
1295 | return NULL; | |
1296 | } | |
1297 | } CYObjectiveCatch } | |
1298 | ||
1299 | static bool CYImplements(id object, Class _class, SEL selector, bool devoid) { | |
1300 | if (objc_method *method = class_getInstanceMethod(_class, selector)) { | |
1301 | if (!devoid) | |
1302 | return true; | |
1303 | #ifdef __OBJC2__ | |
1304 | char type[16]; | |
1305 | method_getReturnType(method, type, sizeof(type)); | |
1306 | #else | |
1307 | const char *type(method_getTypeEncoding(method)); | |
1308 | #endif | |
1309 | if (type[0] != 'v') | |
1310 | return true; | |
1311 | } | |
1312 | ||
1313 | // XXX: possibly use a more "awesome" check? | |
1314 | return false; | |
1315 | } | |
1316 | ||
1317 | static const char *CYPoolTypeEncoding(apr_pool_t *pool, JSContextRef context, SEL sel, objc_method *method) { | |
1318 | if (method != NULL) | |
1319 | return method_getTypeEncoding(method); | |
1320 | ||
1321 | const char *name(sel_getName(sel)); | |
1322 | ||
1323 | sqlite3_stmt *statement; | |
1324 | ||
1325 | _sqlcall(sqlite3_prepare(Bridge_, | |
1326 | "select " | |
1327 | "\"bridge\".\"value\" " | |
1328 | "from \"bridge\" " | |
1329 | "where" | |
1330 | " \"bridge\".\"mode\" = -1 and" | |
1331 | " \"bridge\".\"name\" = ?" | |
1332 | " limit 1" | |
1333 | , -1, &statement, NULL)); | |
1334 | ||
1335 | _trace(); | |
1336 | _sqlcall(sqlite3_bind_text(statement, 1, name, -1, SQLITE_STATIC)); | |
1337 | ||
1338 | const char *value; | |
1339 | if (_sqlcall(sqlite3_step(statement)) == SQLITE_DONE) { | |
1340 | _trace(); | |
1341 | value = NULL;} | |
1342 | else { | |
1343 | _trace(); | |
1344 | value = sqlite3_column_pooled(pool, statement, 0); | |
1345 | } | |
1346 | ||
1347 | _sqlcall(sqlite3_finalize(statement)); | |
1348 | ||
1349 | if (value != NULL) | |
1350 | return value; | |
1351 | _trace(); | |
1352 | return NULL; | |
1353 | } | |
1354 | ||
1355 | static void MessageClosure_(ffi_cif *cif, void *result, void **arguments, void *arg) { | |
1356 | Closure_privateData *internal(reinterpret_cast<Closure_privateData *>(arg)); | |
1357 | ||
1358 | JSContextRef context(internal->context_); | |
1359 | ||
1360 | size_t count(internal->cif_.nargs); | |
1361 | JSValueRef values[count]; | |
1362 | ||
1363 | for (size_t index(0); index != count; ++index) | |
1364 | values[index] = CYFromFFI(context, internal->signature_.elements[1 + index].type, internal->cif_.arg_types[index], arguments[index]); | |
1365 | ||
1366 | JSObjectRef _this(CYCastJSObject(context, values[0])); | |
1367 | ||
1368 | JSValueRef value(CYCallAsFunction(context, internal->function_, _this, count - 2, values + 2)); | |
1369 | CYPoolFFI(NULL, context, internal->signature_.elements[0].type, internal->cif_.rtype, result, value); | |
1370 | } | |
1371 | ||
1372 | static JSObjectRef CYMakeMessage(JSContextRef context, SEL sel, IMP imp, const char *type) { | |
1373 | Message_privateData *internal(new Message_privateData(sel, type, imp)); | |
1374 | return JSObjectMake(context, Message_, internal); | |
1375 | } | |
1376 | ||
1377 | static IMP CYMakeMessage(JSContextRef context, JSValueRef value, const char *type) { | |
1378 | JSObjectRef function(CYCastJSObject(context, value)); | |
1379 | Closure_privateData *internal(CYMakeFunctor_(context, function, type, &MessageClosure_)); | |
1380 | return reinterpret_cast<IMP>(internal->GetValue()); | |
1381 | } | |
1382 | ||
1383 | static bool Messages_hasProperty(JSContextRef context, JSObjectRef object, JSStringRef property) { | |
1384 | Messages *internal(reinterpret_cast<Messages *>(JSObjectGetPrivate(object))); | |
1385 | Class _class(internal->GetValue()); | |
1386 | ||
1387 | CYPool pool; | |
1388 | const char *name(CYPoolCString(pool, context, property)); | |
1389 | ||
1390 | if (SEL sel = sel_getUid(name)) | |
1391 | if (class_getInstanceMethod(_class, sel) != NULL) | |
1392 | return true; | |
1393 | ||
1394 | return false; | |
1395 | } | |
1396 | ||
1397 | static JSValueRef Messages_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { | |
1398 | Messages *internal(reinterpret_cast<Messages *>(JSObjectGetPrivate(object))); | |
1399 | Class _class(internal->GetValue()); | |
1400 | ||
1401 | CYPool pool; | |
1402 | const char *name(CYPoolCString(pool, context, property)); | |
1403 | ||
1404 | if (SEL sel = sel_getUid(name)) | |
1405 | if (objc_method *method = class_getInstanceMethod(_class, sel)) | |
1406 | return CYMakeMessage(context, sel, method_getImplementation(method), method_getTypeEncoding(method)); | |
1407 | ||
1408 | return NULL; | |
1409 | } | |
1410 | ||
1411 | static bool Messages_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) { | |
1412 | Messages *internal(reinterpret_cast<Messages *>(JSObjectGetPrivate(object))); | |
1413 | Class _class(internal->GetValue()); | |
1414 | ||
1415 | CYPool pool; | |
1416 | const char *name(CYPoolCString(pool, context, property)); | |
1417 | ||
1418 | SEL sel(sel_registerName(name)); | |
1419 | ||
1420 | objc_method *method(class_getInstanceMethod(_class, sel)); | |
1421 | ||
1422 | const char *type; | |
1423 | IMP imp; | |
1424 | ||
1425 | if (JSValueIsObjectOfClass(context, value, Message_)) { | |
1426 | Message_privateData *message(reinterpret_cast<Message_privateData *>(JSObjectGetPrivate((JSObjectRef) value))); | |
1427 | type = sig::Unparse(pool, &message->signature_); | |
1428 | imp = reinterpret_cast<IMP>(message->GetValue()); | |
1429 | } else { | |
1430 | type = CYPoolTypeEncoding(pool, context, sel, method); | |
1431 | imp = CYMakeMessage(context, value, type); | |
1432 | } | |
1433 | ||
1434 | if (method != NULL) | |
1435 | method_setImplementation(method, imp); | |
1436 | else { | |
1437 | #ifdef GNU_RUNTIME | |
1438 | GSMethodList list(GSAllocMethodList(1)); | |
1439 | GSAppendMethodToList(list, sel, type, imp, YES); | |
1440 | GSAddMethodList(_class, list, YES); | |
1441 | GSFlushMethodCacheForClass(_class); | |
1442 | #else | |
1443 | class_addMethod(_class, sel, imp, type); | |
1444 | #endif | |
1445 | } | |
1446 | ||
1447 | return true; | |
1448 | } | |
1449 | ||
1450 | #if 0 && !__OBJC2__ | |
1451 | static bool Messages_deleteProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { | |
1452 | Messages *internal(reinterpret_cast<Messages *>(JSObjectGetPrivate(object))); | |
1453 | Class _class(internal->GetValue()); | |
1454 | ||
1455 | CYPool pool; | |
1456 | const char *name(CYPoolCString(pool, context, property)); | |
1457 | ||
1458 | if (SEL sel = sel_getUid(name)) | |
1459 | if (objc_method *method = class_getInstanceMethod(_class, sel)) { | |
1460 | objc_method_list list = {NULL, 1, {method}}; | |
1461 | class_removeMethods(_class, &list); | |
1462 | return true; | |
1463 | } | |
1464 | ||
1465 | return false; | |
1466 | } | |
1467 | #endif | |
1468 | ||
1469 | static void Messages_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) { | |
1470 | Messages *internal(reinterpret_cast<Messages *>(JSObjectGetPrivate(object))); | |
1471 | Class _class(internal->GetValue()); | |
1472 | ||
1473 | #ifdef __OBJC2__ | |
1474 | unsigned int size; | |
1475 | objc_method **data(class_copyMethodList(_class, &size)); | |
1476 | for (size_t i(0); i != size; ++i) | |
1477 | JSPropertyNameAccumulatorAddName(names, CYJSString(sel_getName(method_getName(data[i])))); | |
1478 | free(data); | |
1479 | #else | |
1480 | for (objc_method_list *methods(_class->methods); methods != NULL; methods = methods->method_next) | |
1481 | for (int i(0); i != methods->method_count; ++i) | |
1482 | JSPropertyNameAccumulatorAddName(names, CYJSString(sel_getName(method_getName(&methods->method_list[i])))); | |
1483 | #endif | |
1484 | } | |
1485 | ||
1486 | static bool Instance_hasProperty(JSContextRef context, JSObjectRef object, JSStringRef property) { | |
1487 | Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object))); | |
1488 | id self(internal->GetValue()); | |
1489 | ||
1490 | if (JSStringIsEqualToUTF8CString(property, "$cyi")) | |
1491 | return true; | |
1492 | ||
1493 | CYPool pool; | |
1494 | NSString *name(CYCastNSString(pool, context, property)); | |
1495 | ||
1496 | if (CYInternal *internal = CYInternal::Get(self)) | |
1497 | if (internal->HasProperty(context, property)) | |
1498 | return true; | |
1499 | ||
1500 | Class _class(object_getClass(self)); | |
1501 | ||
1502 | CYPoolTry { | |
1503 | // XXX: this is an evil hack to deal with NSProxy; fix elsewhere | |
1504 | if (CYImplements(self, _class, @selector(cy$hasProperty:), false)) | |
1505 | if ([self cy$hasProperty:name]) | |
1506 | return true; | |
1507 | } CYPoolCatch(false) | |
1508 | ||
1509 | const char *string(CYPoolCString(pool, context, name)); | |
1510 | ||
1511 | #ifdef __APPLE__ | |
1512 | if (class_getProperty(_class, string) != NULL) | |
1513 | return true; | |
1514 | #endif | |
1515 | ||
1516 | if (SEL sel = sel_getUid(string)) | |
1517 | if (CYImplements(self, _class, sel, true)) | |
1518 | return true; | |
1519 | ||
1520 | return false; | |
1521 | } | |
1522 | ||
1523 | static JSValueRef Instance_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry { | |
1524 | Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object))); | |
1525 | id self(internal->GetValue()); | |
1526 | ||
1527 | if (JSStringIsEqualToUTF8CString(property, "$cyi")) | |
1528 | return Internal::Make(context, self, object); | |
1529 | ||
1530 | CYPool pool; | |
1531 | NSString *name(CYCastNSString(pool, context, property)); | |
1532 | ||
1533 | if (CYInternal *internal = CYInternal::Get(self)) | |
1534 | if (JSValueRef value = internal->GetProperty(context, property)) | |
1535 | return value; | |
1536 | ||
1537 | CYPoolTry { | |
1538 | if (NSObject *data = [self cy$getProperty:name]) | |
1539 | return CYCastJSValue(context, data); | |
1540 | } CYPoolCatch(NULL) | |
1541 | ||
1542 | const char *string(CYPoolCString(pool, context, name)); | |
1543 | Class _class(object_getClass(self)); | |
1544 | ||
1545 | #ifdef __APPLE__ | |
1546 | if (objc_property_t property = class_getProperty(_class, string)) { | |
1547 | PropertyAttributes attributes(property); | |
1548 | SEL sel(sel_registerName(attributes.Getter())); | |
1549 | return CYSendMessage(pool, context, self, NULL, sel, 0, NULL, false, exception); | |
1550 | } | |
1551 | #endif | |
1552 | ||
1553 | if (SEL sel = sel_getUid(string)) | |
1554 | if (CYImplements(self, _class, sel, true)) | |
1555 | return CYSendMessage(pool, context, self, NULL, sel, 0, NULL, false, exception); | |
1556 | ||
1557 | return NULL; | |
1558 | } CYCatch } | |
1559 | ||
1560 | static bool Instance_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) { CYTry { | |
1561 | Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object))); | |
1562 | id self(internal->GetValue()); | |
1563 | ||
1564 | CYPool pool; | |
1565 | ||
1566 | NSString *name(CYCastNSString(pool, context, property)); | |
1567 | NSObject *data(CYCastNSObject(pool, context, value)); | |
1568 | ||
1569 | CYPoolTry { | |
1570 | if ([self cy$setProperty:name to:data]) | |
1571 | return true; | |
1572 | } CYPoolCatch(NULL) | |
1573 | ||
1574 | const char *string(CYPoolCString(pool, context, name)); | |
1575 | Class _class(object_getClass(self)); | |
1576 | ||
1577 | #ifdef __APPLE__ | |
1578 | if (objc_property_t property = class_getProperty(_class, string)) { | |
1579 | PropertyAttributes attributes(property); | |
1580 | if (const char *setter = attributes.Setter()) { | |
1581 | SEL sel(sel_registerName(setter)); | |
1582 | JSValueRef arguments[1] = {value}; | |
1583 | CYSendMessage(pool, context, self, NULL, sel, 1, arguments, false, exception); | |
1584 | return true; | |
1585 | } | |
1586 | } | |
1587 | #endif | |
1588 | ||
1589 | size_t length(strlen(string)); | |
1590 | ||
1591 | char set[length + 5]; | |
1592 | ||
1593 | set[0] = 's'; | |
1594 | set[1] = 'e'; | |
1595 | set[2] = 't'; | |
1596 | ||
1597 | if (string[0] != '\0') { | |
1598 | set[3] = toupper(string[0]); | |
1599 | memcpy(set + 4, string + 1, length - 1); | |
1600 | } | |
1601 | ||
1602 | set[length + 3] = ':'; | |
1603 | set[length + 4] = '\0'; | |
1604 | ||
1605 | if (SEL sel = sel_getUid(set)) | |
1606 | if (CYImplements(self, _class, sel, false)) { | |
1607 | JSValueRef arguments[1] = {value}; | |
1608 | CYSendMessage(pool, context, self, NULL, sel, 1, arguments, false, exception); | |
1609 | } | |
1610 | ||
1611 | if (CYInternal *internal = CYInternal::Set(self)) { | |
1612 | internal->SetProperty(context, property, value); | |
1613 | return true; | |
1614 | } | |
1615 | ||
1616 | return false; | |
1617 | } CYCatch } | |
1618 | ||
1619 | static bool Instance_deleteProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry { | |
1620 | Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object))); | |
1621 | id self(internal->GetValue()); | |
1622 | ||
1623 | CYPoolTry { | |
1624 | NSString *name(CYCastNSString(NULL, context, property)); | |
1625 | return [self cy$deleteProperty:name]; | |
1626 | } CYPoolCatch(NULL) | |
1627 | } CYCatch } | |
1628 | ||
1629 | static void Instance_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) { | |
1630 | Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object))); | |
1631 | id self(internal->GetValue()); | |
1632 | ||
1633 | CYPool pool; | |
1634 | Class _class(object_getClass(self)); | |
1635 | ||
1636 | #ifdef __APPLE__ | |
1637 | { | |
1638 | unsigned int size; | |
1639 | objc_property_t *data(class_copyPropertyList(_class, &size)); | |
1640 | for (size_t i(0); i != size; ++i) | |
1641 | JSPropertyNameAccumulatorAddName(names, CYJSString(property_getName(data[i]))); | |
1642 | free(data); | |
1643 | } | |
1644 | #endif | |
1645 | ||
1646 | CYPoolTry { | |
1647 | // XXX: this is an evil hack to deal with NSProxy; fix elsewhere | |
1648 | if (CYImplements(self, _class, @selector(cy$getPropertyNames:), false)) | |
1649 | [self cy$getPropertyNames:names]; | |
1650 | } CYPoolCatch() | |
1651 | } | |
1652 | ||
1653 | static JSObjectRef Instance_callAsConstructor(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { | |
1654 | Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object))); | |
1655 | JSObjectRef value(Instance::Make(context, [internal->GetValue() alloc], Instance::Uninitialized)); | |
1656 | return value; | |
1657 | } CYCatch } | |
1658 | ||
1659 | static bool Instance_hasInstance(JSContextRef context, JSObjectRef constructor, JSValueRef instance, JSValueRef *exception) { CYTry { | |
1660 | Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate((JSObjectRef) constructor))); | |
1661 | Class _class(internal->GetValue()); | |
1662 | if (!CYIsClass(_class)) | |
1663 | return false; | |
1664 | ||
1665 | if (JSValueIsObjectOfClass(context, instance, Instance_)) { | |
1666 | Instance *linternal(reinterpret_cast<Instance *>(JSObjectGetPrivate((JSObjectRef) instance))); | |
1667 | // XXX: this isn't always safe | |
1668 | return [linternal->GetValue() isKindOfClass:_class]; | |
1669 | } | |
1670 | ||
1671 | return false; | |
1672 | } CYCatch } | |
1673 | ||
1674 | static bool Internal_hasProperty(JSContextRef context, JSObjectRef object, JSStringRef property) { | |
1675 | Internal *internal(reinterpret_cast<Internal *>(JSObjectGetPrivate(object))); | |
1676 | CYPool pool; | |
1677 | ||
1678 | id self(internal->GetValue()); | |
1679 | const char *name(CYPoolCString(pool, context, property)); | |
1680 | ||
1681 | if (object_getInstanceVariable(self, name, NULL) != NULL) | |
1682 | return true; | |
1683 | ||
1684 | return false; | |
1685 | } | |
1686 | ||
1687 | static JSValueRef Internal_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry { | |
1688 | Internal *internal(reinterpret_cast<Internal *>(JSObjectGetPrivate(object))); | |
1689 | CYPool pool; | |
1690 | ||
1691 | id self(internal->GetValue()); | |
1692 | const char *name(CYPoolCString(pool, context, property)); | |
1693 | ||
1694 | if (objc_ivar *ivar = object_getInstanceVariable(self, name, NULL)) { | |
1695 | Type_privateData type(pool, ivar_getTypeEncoding(ivar)); | |
1696 | return CYFromFFI(context, type.type_, type.GetFFI(), reinterpret_cast<uint8_t *>(self) + ivar_getOffset(ivar)); | |
1697 | } | |
1698 | ||
1699 | return NULL; | |
1700 | } CYCatch } | |
1701 | ||
1702 | static bool Internal_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) { CYTry { | |
1703 | Internal *internal(reinterpret_cast<Internal *>(JSObjectGetPrivate(object))); | |
1704 | CYPool pool; | |
1705 | ||
1706 | id self(internal->GetValue()); | |
1707 | const char *name(CYPoolCString(pool, context, property)); | |
1708 | ||
1709 | if (objc_ivar *ivar = object_getInstanceVariable(self, name, NULL)) { | |
1710 | Type_privateData type(pool, ivar_getTypeEncoding(ivar)); | |
1711 | CYPoolFFI(pool, context, type.type_, type.GetFFI(), reinterpret_cast<uint8_t *>(self) + ivar_getOffset(ivar), value); | |
1712 | return true; | |
1713 | } | |
1714 | ||
1715 | return false; | |
1716 | } CYCatch } | |
1717 | ||
1718 | static void Internal_getPropertyNames_(Class _class, JSPropertyNameAccumulatorRef names) { | |
1719 | if (Class super = class_getSuperclass(_class)) | |
1720 | Internal_getPropertyNames_(super, names); | |
1721 | ||
1722 | #ifdef __OBJC2__ | |
1723 | unsigned int size; | |
1724 | objc_ivar **data(class_copyIvarList(_class, &size)); | |
1725 | for (size_t i(0); i != size; ++i) | |
1726 | JSPropertyNameAccumulatorAddName(names, CYJSString(ivar_getName(data[i]))); | |
1727 | free(data); | |
1728 | #else | |
1729 | if (objc_ivar_list *ivars = _class->ivars) | |
1730 | for (int i(0); i != ivars->ivar_count; ++i) | |
1731 | JSPropertyNameAccumulatorAddName(names, CYJSString(ivar_getName(&ivars->ivar_list[i]))); | |
1732 | #endif | |
1733 | } | |
1734 | ||
1735 | static void Internal_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) { | |
1736 | Internal *internal(reinterpret_cast<Internal *>(JSObjectGetPrivate(object))); | |
1737 | CYPool pool; | |
1738 | ||
1739 | id self(internal->GetValue()); | |
1740 | Class _class(object_getClass(self)); | |
1741 | ||
1742 | Internal_getPropertyNames_(_class, names); | |
1743 | } | |
1744 | ||
1745 | static JSValueRef Internal_callAsFunction_$cya(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { | |
1746 | Internal *internal(reinterpret_cast<Internal *>(JSObjectGetPrivate(object))); | |
1747 | return internal->GetOwner(); | |
1748 | } | |
1749 | ||
1750 | static JSValueRef ObjectiveC_Classes_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry { | |
1751 | CYPool pool; | |
1752 | NSString *name(CYCastNSString(pool, context, property)); | |
1753 | if (Class _class = NSClassFromString(name)) | |
1754 | return CYMakeInstance(context, _class, true); | |
1755 | return NULL; | |
1756 | } CYCatch } | |
1757 | ||
1758 | static void ObjectiveC_Classes_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) { | |
1759 | #ifdef __APPLE__ | |
1760 | size_t size(objc_getClassList(NULL, 0)); | |
1761 | Class *data(reinterpret_cast<Class *>(malloc(sizeof(Class) * size))); | |
1762 | ||
1763 | get: | |
1764 | size_t writ(objc_getClassList(data, size)); | |
1765 | if (size < writ) { | |
1766 | size = writ; | |
1767 | if (Class *copy = reinterpret_cast<Class *>(realloc(data, sizeof(Class) * writ))) { | |
1768 | data = copy; | |
1769 | goto get; | |
1770 | } else goto done; | |
1771 | } | |
1772 | ||
1773 | for (size_t i(0); i != writ; ++i) | |
1774 | JSPropertyNameAccumulatorAddName(names, CYJSString(class_getName(data[i]))); | |
1775 | ||
1776 | done: | |
1777 | free(data); | |
1778 | #else | |
1779 | void *state(NULL); | |
1780 | while (Class _class = objc_next_class(&state)) | |
1781 | JSPropertyNameAccumulatorAddName(names, CYJSString(class_getName(_class))); | |
1782 | #endif | |
1783 | } | |
1784 | ||
1785 | #ifdef __OBJC2__ | |
1786 | static JSValueRef ObjectiveC_Image_Classes_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry { | |
1787 | const char *internal(reinterpret_cast<const char *>(JSObjectGetPrivate(object))); | |
1788 | ||
1789 | CYPool pool; | |
1790 | const char *name(CYPoolCString(pool, context, property)); | |
1791 | unsigned int size; | |
1792 | const char **data(objc_copyClassNamesForImage(internal, &size)); | |
1793 | JSValueRef value; | |
1794 | for (size_t i(0); i != size; ++i) | |
1795 | if (strcmp(name, data[i]) == 0) { | |
1796 | if (Class _class = objc_getClass(name)) { | |
1797 | value = CYMakeInstance(context, _class, true); | |
1798 | goto free; | |
1799 | } else | |
1800 | break; | |
1801 | } | |
1802 | value = NULL; | |
1803 | free: | |
1804 | free(data); | |
1805 | return value; | |
1806 | } CYCatch } | |
1807 | ||
1808 | static void ObjectiveC_Image_Classes_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) { | |
1809 | const char *internal(reinterpret_cast<const char *>(JSObjectGetPrivate(object))); | |
1810 | unsigned int size; | |
1811 | const char **data(objc_copyClassNamesForImage(internal, &size)); | |
1812 | for (size_t i(0); i != size; ++i) | |
1813 | JSPropertyNameAccumulatorAddName(names, CYJSString(data[i])); | |
1814 | free(data); | |
1815 | } | |
1816 | ||
1817 | static JSValueRef ObjectiveC_Images_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry { | |
1818 | CYPool pool; | |
1819 | const char *name(CYPoolCString(pool, context, property)); | |
1820 | unsigned int size; | |
1821 | const char **data(objc_copyImageNames(&size)); | |
1822 | for (size_t i(0); i != size; ++i) | |
1823 | if (strcmp(name, data[i]) == 0) { | |
1824 | name = data[i]; | |
1825 | goto free; | |
1826 | } | |
1827 | name = NULL; | |
1828 | free: | |
1829 | free(data); | |
1830 | if (name == NULL) | |
1831 | return NULL; | |
1832 | JSObjectRef value(JSObjectMake(context, NULL, NULL)); | |
1833 | CYSetProperty(context, value, CYJSString("classes"), JSObjectMake(context, ObjectiveC_Image_Classes_, const_cast<char *>(name))); | |
1834 | return value; | |
1835 | } CYCatch } | |
1836 | ||
1837 | static void ObjectiveC_Images_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) { | |
1838 | unsigned int size; | |
1839 | const char **data(objc_copyImageNames(&size)); | |
1840 | for (size_t i(0); i != size; ++i) | |
1841 | JSPropertyNameAccumulatorAddName(names, CYJSString(data[i])); | |
1842 | free(data); | |
1843 | } | |
1844 | #endif | |
1845 | ||
1846 | static JSValueRef ObjectiveC_Protocols_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry { | |
1847 | CYPool pool; | |
1848 | const char *name(CYPoolCString(pool, context, property)); | |
1849 | if (Protocol *protocol = objc_getProtocol(name)) | |
1850 | return CYMakeInstance(context, protocol, true); | |
1851 | return NULL; | |
1852 | } CYCatch } | |
1853 | ||
1854 | static void ObjectiveC_Protocols_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) { | |
1855 | #ifdef __OBJC2__ | |
1856 | unsigned int size; | |
1857 | Protocol **data(objc_copyProtocolList(&size)); | |
1858 | for (size_t i(0); i != size; ++i) | |
1859 | JSPropertyNameAccumulatorAddName(names, CYJSString(protocol_getName(data[i]))); | |
1860 | free(data); | |
1861 | #else | |
1862 | // XXX: fix this! | |
1863 | #endif | |
1864 | } | |
1865 | ||
1866 | #ifdef __APPLE__ | |
1867 | static bool stret(ffi_type *ffi_type) { | |
1868 | return ffi_type->type == FFI_TYPE_STRUCT && ( | |
1869 | ffi_type->size > OBJC_MAX_STRUCT_BY_VALUE || | |
1870 | struct_forward_array[ffi_type->size] != 0 | |
1871 | ); | |
1872 | } | |
1873 | #endif | |
1874 | ||
1875 | JSValueRef CYSendMessage(apr_pool_t *pool, JSContextRef context, id self, Class _class, SEL _cmd, size_t count, const JSValueRef arguments[], bool initialize, JSValueRef *exception) { CYTry { | |
1876 | const char *type; | |
1877 | ||
1878 | if (_class == NULL) | |
1879 | _class = object_getClass(self); | |
1880 | ||
1881 | IMP imp; | |
1882 | ||
1883 | if (objc_method *method = class_getInstanceMethod(_class, _cmd)) { | |
1884 | imp = method_getImplementation(method); | |
1885 | type = method_getTypeEncoding(method); | |
1886 | } else { | |
1887 | imp = NULL; | |
1888 | ||
1889 | CYPoolTry { | |
1890 | NSMethodSignature *method([self methodSignatureForSelector:_cmd]); | |
1891 | if (method == nil) | |
1892 | throw CYJSError(context, "unrecognized selector %s sent to object %p", sel_getName(_cmd), self); | |
1893 | type = CYPoolCString(pool, context, [method _typeString]); | |
1894 | } CYPoolCatch(NULL) | |
1895 | } | |
1896 | ||
1897 | objc_super super = {self, _class}; | |
1898 | void *arg0 = &super; | |
1899 | ||
1900 | void *setup[2]; | |
1901 | setup[0] = &arg0; | |
1902 | setup[1] = &_cmd; | |
1903 | ||
1904 | sig::Signature signature; | |
1905 | sig::Parse(pool, &signature, type, &Structor_); | |
1906 | ||
1907 | ffi_cif cif; | |
1908 | sig::sig_ffi_cif(pool, &sig::ObjectiveC, &signature, &cif); | |
1909 | ||
1910 | if (imp == NULL) | |
1911 | #ifdef __APPLE__ | |
1912 | if (stret(cif.rtype)) | |
1913 | imp = class_getMethodImplementation_stret(_class, _cmd); | |
1914 | else | |
1915 | imp = class_getMethodImplementation(_class, _cmd); | |
1916 | #else | |
1917 | imp = objc_msg_lookup_super(&super, _cmd); | |
1918 | #endif | |
1919 | ||
1920 | void (*function)() = reinterpret_cast<void (*)()>(imp); | |
1921 | return CYCallFunction(pool, context, 2, setup, count, arguments, initialize, exception, &signature, &cif, function); | |
1922 | } CYCatch } | |
1923 | ||
1924 | static JSValueRef $objc_msgSend(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { | |
1925 | if (count < 2) | |
1926 | throw CYJSError(context, "too few arguments to objc_msgSend"); | |
1927 | ||
1928 | CYPool pool; | |
1929 | ||
1930 | bool uninitialized; | |
1931 | ||
1932 | id self; | |
1933 | SEL _cmd; | |
1934 | Class _class; | |
1935 | ||
1936 | if (JSValueIsObjectOfClass(context, arguments[0], Super_)) { | |
1937 | cy::Super *internal(reinterpret_cast<cy::Super *>(JSObjectGetPrivate((JSObjectRef) arguments[0]))); | |
1938 | self = internal->GetValue(); | |
1939 | _class = internal->class_;; | |
1940 | uninitialized = false; | |
1941 | } else if (JSValueIsObjectOfClass(context, arguments[0], Instance_)) { | |
1942 | Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate((JSObjectRef) arguments[0]))); | |
1943 | self = internal->GetValue(); | |
1944 | _class = nil; | |
1945 | uninitialized = internal->IsUninitialized(); | |
1946 | if (uninitialized) | |
1947 | internal->value_ = nil; | |
1948 | } else { | |
1949 | self = CYCastNSObject(pool, context, arguments[0]); | |
1950 | _class = nil; | |
1951 | uninitialized = false; | |
1952 | } | |
1953 | ||
1954 | if (self == nil) | |
1955 | return CYJSNull(context); | |
1956 | ||
1957 | _cmd = CYCastSEL(context, arguments[1]); | |
1958 | ||
1959 | return CYSendMessage(pool, context, self, _class, _cmd, count - 2, arguments + 2, uninitialized, exception); | |
1960 | } CYCatch } | |
1961 | ||
1962 | /* Hook: objc_registerClassPair {{{ */ | |
1963 | #ifdef __OBJC2__ | |
1964 | // XXX: replace this with associated objects | |
1965 | ||
1966 | MSHook(void, CYDealloc, id self, SEL sel) { | |
1967 | CYInternal *internal; | |
1968 | object_getInstanceVariable(self, "cy$internal_", reinterpret_cast<void **>(&internal)); | |
1969 | if (internal != NULL) | |
1970 | delete internal; | |
1971 | _CYDealloc(self, sel); | |
1972 | } | |
1973 | ||
1974 | MSHook(void, objc_registerClassPair, Class _class) { | |
1975 | Class super(class_getSuperclass(_class)); | |
1976 | if (super == NULL || class_getInstanceVariable(super, "cy$internal_") == NULL) { | |
1977 | class_addIvar(_class, "cy$internal_", sizeof(CYInternal *), log2(sizeof(CYInternal *)), "^{CYInternal}"); | |
1978 | MSHookMessage(_class, @selector(dealloc), MSHake(CYDealloc)); | |
1979 | } | |
1980 | ||
1981 | _objc_registerClassPair(_class); | |
1982 | } | |
1983 | ||
1984 | static JSValueRef objc_registerClassPair_(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { | |
1985 | if (count != 1) | |
1986 | throw CYJSError(context, "incorrect number of arguments to objc_registerClassPair"); | |
1987 | CYPool pool; | |
1988 | NSObject *value(CYCastNSObject(pool, context, arguments[0])); | |
1989 | if (value == NULL || !CYIsClass(value)) | |
1990 | throw CYJSError(context, "incorrect number of arguments to objc_registerClassPair"); | |
1991 | Class _class((Class) value); | |
1992 | $objc_registerClassPair(_class); | |
1993 | return CYJSUndefined(context); | |
1994 | } CYCatch } | |
1995 | #endif | |
1996 | /* }}} */ | |
1997 | ||
1998 | static JSValueRef Selector_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { | |
1999 | JSValueRef setup[count + 2]; | |
2000 | setup[0] = _this; | |
2001 | setup[1] = object; | |
2002 | memcpy(setup + 2, arguments, sizeof(JSValueRef) * count); | |
2003 | return $objc_msgSend(context, NULL, NULL, count + 2, setup, exception); | |
2004 | } | |
2005 | ||
2006 | static JSValueRef Message_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { | |
2007 | CYPool pool; | |
2008 | Message_privateData *internal(reinterpret_cast<Message_privateData *>(JSObjectGetPrivate(object))); | |
2009 | ||
2010 | // XXX: handle Instance::Uninitialized? | |
2011 | id self(CYCastNSObject(pool, context, _this)); | |
2012 | ||
2013 | void *setup[2]; | |
2014 | setup[0] = &self; | |
2015 | setup[1] = &internal->sel_; | |
2016 | ||
2017 | return CYCallFunction(pool, context, 2, setup, count, arguments, false, exception, &internal->signature_, &internal->cif_, internal->GetValue()); | |
2018 | } | |
2019 | ||
2020 | static JSObjectRef Super_new(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { | |
2021 | if (count != 2) | |
2022 | throw CYJSError(context, "incorrect number of arguments to Super constructor"); | |
2023 | CYPool pool; | |
2024 | id self(CYCastNSObject(pool, context, arguments[0])); | |
2025 | Class _class(CYCastClass(pool, context, arguments[1])); | |
2026 | return cy::Super::Make(context, self, _class); | |
2027 | } CYCatch } | |
2028 | ||
2029 | static JSObjectRef Selector_new(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { | |
2030 | if (count != 1) | |
2031 | throw CYJSError(context, "incorrect number of arguments to Selector constructor"); | |
2032 | CYPool pool; | |
2033 | const char *name(CYPoolCString(pool, context, arguments[0])); | |
2034 | return CYMakeSelector(context, sel_registerName(name)); | |
2035 | } CYCatch } | |
2036 | ||
2037 | static JSObjectRef Instance_new(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { | |
2038 | if (count > 1) | |
2039 | throw CYJSError(context, "incorrect number of arguments to Instance constructor"); | |
2040 | id self(count == 0 ? nil : CYCastPointer<id>(context, arguments[0])); | |
2041 | return Instance::Make(context, self); | |
2042 | } CYCatch } | |
2043 | ||
2044 | static JSValueRef CYValue_getProperty_value(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { | |
2045 | CYValue *internal(reinterpret_cast<CYValue *>(JSObjectGetPrivate(object))); | |
2046 | return CYCastJSValue(context, reinterpret_cast<uintptr_t>(internal->value_)); | |
2047 | } | |
2048 | ||
2049 | static JSValueRef CYValue_callAsFunction_$cya(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { | |
2050 | CYValue *internal(reinterpret_cast<CYValue *>(JSObjectGetPrivate(_this))); | |
2051 | Type_privateData *typical(internal->GetType()); | |
2052 | ||
2053 | sig::Type *type; | |
2054 | ffi_type *ffi; | |
2055 | ||
2056 | if (typical == NULL) { | |
2057 | type = NULL; | |
2058 | ffi = NULL; | |
2059 | } else { | |
2060 | type = typical->type_; | |
2061 | ffi = typical->ffi_; | |
2062 | } | |
2063 | ||
2064 | return CYMakePointer(context, &internal->value_, type, ffi, object); | |
2065 | } | |
2066 | ||
2067 | static JSValueRef Instance_getProperty_constructor(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { | |
2068 | Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object))); | |
2069 | return Instance::Make(context, (id) object_getClass(internal->GetValue())); | |
2070 | } | |
2071 | ||
2072 | static JSValueRef Instance_getProperty_protocol(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry { | |
2073 | Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object))); | |
2074 | id self(internal->GetValue()); | |
2075 | if (!CYIsClass(self)) | |
2076 | return CYJSUndefined(context); | |
2077 | return CYGetClassPrototype(context, self); | |
2078 | } CYCatch } | |
2079 | ||
2080 | static JSValueRef Instance_getProperty_messages(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { | |
2081 | Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object))); | |
2082 | id self(internal->GetValue()); | |
2083 | if (!CYIsClass(self)) | |
2084 | return CYJSUndefined(context); | |
2085 | return Messages::Make(context, (Class) self); | |
2086 | } | |
2087 | ||
2088 | static JSValueRef Instance_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { | |
2089 | if (!JSValueIsObjectOfClass(context, _this, Instance_)) | |
2090 | return NULL; | |
2091 | ||
2092 | Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(_this))); | |
2093 | return CYCastJSValue(context, CYJSString(CYCastNSCYON(internal->GetValue()))); | |
2094 | } CYCatch } | |
2095 | ||
2096 | static JSValueRef Instance_callAsFunction_toJSON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { | |
2097 | if (!JSValueIsObjectOfClass(context, _this, Instance_)) | |
2098 | return NULL; | |
2099 | ||
2100 | Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(_this))); | |
2101 | ||
2102 | CYPoolTry { | |
2103 | NSString *key; | |
2104 | if (count == 0) | |
2105 | key = nil; | |
2106 | else | |
2107 | key = CYCastNSString(NULL, context, CYJSString(context, arguments[0])); | |
2108 | // XXX: check for support of cy$toJSON? | |
2109 | return CYCastJSValue(context, CYJSString([internal->GetValue() cy$toJSON:key])); | |
2110 | } CYPoolCatch(NULL) | |
2111 | } CYCatch } | |
2112 | ||
2113 | static JSValueRef Instance_callAsFunction_toString(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { | |
2114 | if (!JSValueIsObjectOfClass(context, _this, Instance_)) | |
2115 | return NULL; | |
2116 | ||
2117 | Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(_this))); | |
2118 | ||
2119 | CYPoolTry { | |
2120 | // XXX: this seems like a stupid implementation; what if it crashes? why not use the CYONifier backend? | |
2121 | return CYCastJSValue(context, CYJSString([internal->GetValue() description])); | |
2122 | } CYPoolCatch(NULL) | |
2123 | } CYCatch } | |
2124 | ||
2125 | static JSValueRef Selector_callAsFunction_toString(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { | |
2126 | Selector_privateData *internal(reinterpret_cast<Selector_privateData *>(JSObjectGetPrivate(_this))); | |
2127 | return CYCastJSValue(context, sel_getName(internal->GetValue())); | |
2128 | } CYCatch } | |
2129 | ||
2130 | static JSValueRef Selector_callAsFunction_toJSON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { | |
2131 | return Selector_callAsFunction_toString(context, object, _this, count, arguments, exception); | |
2132 | } | |
2133 | ||
2134 | static JSValueRef Selector_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { | |
2135 | Selector_privateData *internal(reinterpret_cast<Selector_privateData *>(JSObjectGetPrivate(_this))); | |
2136 | const char *name(sel_getName(internal->GetValue())); | |
2137 | ||
2138 | CYPoolTry { | |
2139 | return CYCastJSValue(context, CYJSString((NSString *) [NSString stringWithFormat:@"@selector(%s)", name])); | |
2140 | } CYPoolCatch(NULL) | |
2141 | } CYCatch } | |
2142 | ||
2143 | static JSValueRef Selector_callAsFunction_type(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { | |
2144 | if (count != 1) | |
2145 | throw CYJSError(context, "incorrect number of arguments to Selector.type"); | |
2146 | ||
2147 | CYPool pool; | |
2148 | Selector_privateData *internal(reinterpret_cast<Selector_privateData *>(JSObjectGetPrivate(_this))); | |
2149 | SEL sel(internal->GetValue()); | |
2150 | ||
2151 | objc_method *method; | |
2152 | if (Class _class = CYCastClass(pool, context, arguments[0])) | |
2153 | method = class_getInstanceMethod(_class, sel); | |
2154 | else | |
2155 | method = NULL; | |
2156 | ||
2157 | if (const char *type = CYPoolTypeEncoding(pool, context, sel, method)) | |
2158 | return CYCastJSValue(context, CYJSString(type)); | |
2159 | ||
2160 | return CYJSNull(context); | |
2161 | } CYCatch } | |
2162 | ||
2163 | static JSStaticValue Selector_staticValues[2] = { | |
2164 | {"value", &CYValue_getProperty_value, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete}, | |
2165 | {NULL, NULL, NULL, 0} | |
2166 | }; | |
2167 | ||
2168 | static JSStaticValue Instance_staticValues[5] = { | |
2169 | {"constructor", &Instance_getProperty_constructor, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
2170 | {"messages", &Instance_getProperty_messages, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
2171 | {"prototype", &Instance_getProperty_protocol, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
2172 | {"value", &CYValue_getProperty_value, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
2173 | {NULL, NULL, NULL, 0} | |
2174 | }; | |
2175 | ||
2176 | static JSStaticFunction Instance_staticFunctions[5] = { | |
2177 | {"$cya", &CYValue_callAsFunction_$cya, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
2178 | {"toCYON", &Instance_callAsFunction_toCYON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
2179 | {"toJSON", &Instance_callAsFunction_toJSON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
2180 | {"toString", &Instance_callAsFunction_toString, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
2181 | {NULL, NULL, 0} | |
2182 | }; | |
2183 | ||
2184 | static JSStaticFunction Internal_staticFunctions[2] = { | |
2185 | {"$cya", &Internal_callAsFunction_$cya, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
2186 | {NULL, NULL, 0} | |
2187 | }; | |
2188 | ||
2189 | static JSStaticFunction Selector_staticFunctions[5] = { | |
2190 | {"toCYON", &Selector_callAsFunction_toCYON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
2191 | {"toJSON", &Selector_callAsFunction_toJSON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
2192 | {"toString", &Selector_callAsFunction_toString, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
2193 | {"type", &Selector_callAsFunction_type, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
2194 | {NULL, NULL, 0} | |
2195 | }; | |
2196 | ||
2197 | void CYObjectiveC_SetupContext(JSContextRef context) { | |
2198 | JSObjectRef global(CYGetGlobalObject(context)); | |
2199 | apr_pool_t *pool(CYGetGlobalPool()); | |
2200 | ||
2201 | Object_type = new(pool) Type_privateData(pool, "@"); | |
2202 | Selector_type = new(pool) Type_privateData(pool, ":"); | |
2203 | ||
2204 | #ifdef __APPLE__ | |
2205 | NSCFBoolean_ = objc_getClass("NSCFBoolean"); | |
2206 | NSCFType_ = objc_getClass("NSCFType"); | |
2207 | NSMessageBuilder_ = objc_getClass("NSMessageBuilder"); | |
2208 | NSZombie_ = objc_getClass("_NSZombie_"); | |
2209 | #endif | |
2210 | ||
2211 | NSArray_ = objc_getClass("NSArray"); | |
2212 | NSDictionary_ = objc_getClass("NSDictionary"); | |
2213 | Object_ = objc_getClass("Object"); | |
2214 | ||
2215 | JSClassDefinition definition; | |
2216 | ||
2217 | definition = kJSClassDefinitionEmpty; | |
2218 | definition.className = "Instance"; | |
2219 | definition.staticValues = Instance_staticValues; | |
2220 | definition.staticFunctions = Instance_staticFunctions; | |
2221 | definition.hasProperty = &Instance_hasProperty; | |
2222 | definition.getProperty = &Instance_getProperty; | |
2223 | definition.setProperty = &Instance_setProperty; | |
2224 | definition.deleteProperty = &Instance_deleteProperty; | |
2225 | definition.getPropertyNames = &Instance_getPropertyNames; | |
2226 | definition.callAsConstructor = &Instance_callAsConstructor; | |
2227 | definition.hasInstance = &Instance_hasInstance; | |
2228 | definition.finalize = &CYFinalize; | |
2229 | Instance_ = JSClassCreate(&definition); | |
2230 | ||
2231 | definition = kJSClassDefinitionEmpty; | |
2232 | definition.className = "Internal"; | |
2233 | definition.staticFunctions = Internal_staticFunctions; | |
2234 | definition.hasProperty = &Internal_hasProperty; | |
2235 | definition.getProperty = &Internal_getProperty; | |
2236 | definition.setProperty = &Internal_setProperty; | |
2237 | definition.getPropertyNames = &Internal_getPropertyNames; | |
2238 | definition.finalize = &CYFinalize; | |
2239 | Internal_ = JSClassCreate(&definition); | |
2240 | ||
2241 | definition = kJSClassDefinitionEmpty; | |
2242 | definition.className = "Message"; | |
2243 | definition.staticFunctions = cy::Functor::StaticFunctions; | |
2244 | definition.callAsFunction = &Message_callAsFunction; | |
2245 | definition.finalize = &CYFinalize; | |
2246 | Message_ = JSClassCreate(&definition); | |
2247 | ||
2248 | definition = kJSClassDefinitionEmpty; | |
2249 | definition.className = "Messages"; | |
2250 | definition.hasProperty = &Messages_hasProperty; | |
2251 | definition.getProperty = &Messages_getProperty; | |
2252 | definition.setProperty = &Messages_setProperty; | |
2253 | #if 0 && !__OBJC2__ | |
2254 | definition.deleteProperty = &Messages_deleteProperty; | |
2255 | #endif | |
2256 | definition.getPropertyNames = &Messages_getPropertyNames; | |
2257 | definition.finalize = &CYFinalize; | |
2258 | Messages_ = JSClassCreate(&definition); | |
2259 | ||
2260 | definition = kJSClassDefinitionEmpty; | |
2261 | definition.className = "Selector"; | |
2262 | definition.staticValues = Selector_staticValues; | |
2263 | definition.staticFunctions = Selector_staticFunctions; | |
2264 | definition.callAsFunction = &Selector_callAsFunction; | |
2265 | definition.finalize = &CYFinalize; | |
2266 | Selector_ = JSClassCreate(&definition); | |
2267 | ||
2268 | definition = kJSClassDefinitionEmpty; | |
2269 | definition.className = "Super"; | |
2270 | definition.staticFunctions = Internal_staticFunctions; | |
2271 | definition.finalize = &CYFinalize; | |
2272 | Super_ = JSClassCreate(&definition); | |
2273 | ||
2274 | definition = kJSClassDefinitionEmpty; | |
2275 | definition.className = "ObjectiveC::Classes"; | |
2276 | definition.getProperty = &ObjectiveC_Classes_getProperty; | |
2277 | definition.getPropertyNames = &ObjectiveC_Classes_getPropertyNames; | |
2278 | ObjectiveC_Classes_ = JSClassCreate(&definition); | |
2279 | ||
2280 | definition = kJSClassDefinitionEmpty; | |
2281 | definition.className = "ObjectiveC::Protocols"; | |
2282 | definition.getProperty = &ObjectiveC_Protocols_getProperty; | |
2283 | definition.getPropertyNames = &ObjectiveC_Protocols_getPropertyNames; | |
2284 | ObjectiveC_Protocols_ = JSClassCreate(&definition); | |
2285 | ||
2286 | JSObjectRef ObjectiveC(JSObjectMake(context, NULL, NULL)); | |
2287 | CYSetProperty(context, global, CYJSString("ObjectiveC"), ObjectiveC); | |
2288 | ||
2289 | CYSetProperty(context, ObjectiveC, CYJSString("classes"), JSObjectMake(context, ObjectiveC_Classes_, NULL)); | |
2290 | CYSetProperty(context, ObjectiveC, CYJSString("protocols"), JSObjectMake(context, ObjectiveC_Protocols_, NULL)); | |
2291 | ||
2292 | #ifdef __OBJC2__ | |
2293 | definition = kJSClassDefinitionEmpty; | |
2294 | definition.className = "ObjectiveC::Images"; | |
2295 | definition.getProperty = &ObjectiveC_Images_getProperty; | |
2296 | definition.getPropertyNames = &ObjectiveC_Images_getPropertyNames; | |
2297 | ObjectiveC_Images_ = JSClassCreate(&definition); | |
2298 | ||
2299 | definition = kJSClassDefinitionEmpty; | |
2300 | definition.className = "ObjectiveC::Image::Classes"; | |
2301 | definition.getProperty = &ObjectiveC_Image_Classes_getProperty; | |
2302 | definition.getPropertyNames = &ObjectiveC_Image_Classes_getPropertyNames; | |
2303 | ObjectiveC_Image_Classes_ = JSClassCreate(&definition); | |
2304 | ||
2305 | CYSetProperty(context, ObjectiveC, CYJSString("images"), JSObjectMake(context, ObjectiveC_Images_, NULL)); | |
2306 | #endif | |
2307 | ||
2308 | JSObjectRef Instance(JSObjectMakeConstructor(context, Instance_, &Instance_new)); | |
2309 | JSObjectRef Message(JSObjectMakeConstructor(context, Message_, NULL)); | |
2310 | JSObjectRef Selector(JSObjectMakeConstructor(context, Selector_, &Selector_new)); | |
2311 | JSObjectRef Super(JSObjectMakeConstructor(context, Super_, &Super_new)); | |
2312 | ||
2313 | Instance_prototype_ = (JSObjectRef) CYGetProperty(context, Instance, prototype_); | |
2314 | JSValueProtect(context, Instance_prototype_); | |
2315 | ||
2316 | CYSetProperty(context, global, CYJSString("Instance"), Instance); | |
2317 | CYSetProperty(context, global, CYJSString("Selector"), Selector); | |
2318 | CYSetProperty(context, global, CYJSString("Super"), Super); | |
2319 | ||
2320 | #ifdef __OBJC2__ | |
2321 | CYSetProperty(context, global, CYJSString("objc_registerClassPair"), JSObjectMakeFunctionWithCallback(context, CYJSString("objc_registerClassPair"), &objc_registerClassPair_)); | |
2322 | MSHookFunction(&objc_registerClassPair, MSHake(objc_registerClassPair)); | |
2323 | #endif | |
2324 | ||
2325 | CYSetProperty(context, global, CYJSString("objc_msgSend"), JSObjectMakeFunctionWithCallback(context, CYJSString("objc_msgSend"), &$objc_msgSend)); | |
2326 | ||
2327 | JSObjectSetPrototype(context, (JSObjectRef) CYGetProperty(context, Message, prototype_), Function_prototype_); | |
2328 | JSObjectSetPrototype(context, (JSObjectRef) CYGetProperty(context, Selector, prototype_), Function_prototype_); | |
2329 | ||
2330 | #ifdef __APPLE__ | |
2331 | class_addMethod(NSCFType_, @selector(cy$toJSON:), reinterpret_cast<IMP>(&NSCFType$cy$toJSON), "@12@0:4@8"); | |
2332 | #endif | |
2333 | } | |
2334 | ||
2335 | static CYHooks CYObjectiveCHooks = { | |
2336 | &CYObjectiveC_ExecuteStart, | |
2337 | &CYObjectiveC_ExecuteEnd, | |
2338 | &CYObjectiveC_RuntimeProperty, | |
2339 | &CYObjectiveC_CallFunction, | |
2340 | &CYObjectiveC_SetupContext, | |
2341 | &CYObjectiveC_PoolFFI, | |
2342 | &CYObjectiveC_FromFFI, | |
2343 | }; | |
2344 | ||
2345 | struct CYObjectiveC { | |
2346 | CYObjectiveC() { | |
2347 | hooks_ = &CYObjectiveCHooks; | |
2348 | } | |
2349 | } CYObjectiveC; |