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