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