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