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