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