1 /* Cycript - Remove Execution Server and Disassembler
2 * Copyright (C) 2009 Jay Freeman (saurik)
5 /* Modified BSD License {{{ */
7 * Redistribution and use in source and binary
8 * forms, with or without modification, are permitted
9 * provided that the following conditions are met:
11 * 1. Redistributions of source code must retain the
12 * above copyright notice, this list of conditions
13 * and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the
15 * above copyright notice, this list of conditions
16 * and the following disclaimer in the documentation
17 * and/or other materials provided with the
19 * 3. The name of the author may not be used to endorse
20 * or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS''
24 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
25 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
26 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
31 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
34 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
35 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
36 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42 #include <substrate.h>
43 #include "cycript.hpp"
45 #include "sig/parse.hpp"
46 #include "sig/ffi_type.hpp"
48 #include "Pooling.hpp"
51 #include <CoreFoundation/CoreFoundation.h>
52 #include <CoreFoundation/CFLogUtilities.h>
54 #include <WebKit/WebScriptObject.h>
59 #include <ext/stdio_filebuf.h>
67 #include "Cycript.tab.hh"
69 #include <apr-1/apr_thread_proc.h>
74 #define _assert(test) do { \
76 @throw [NSException exceptionWithName:NSInternalInconsistencyException reason:[NSString stringWithFormat:@"_assert(%s):%s(%u):%s", #test, __FILE__, __LINE__, __FUNCTION__] userInfo:nil]; \
79 #define _trace() do { \
80 CFLog(kCFLogLevelNotice, CFSTR("_trace():%u"), __LINE__); \
85 NSAutoreleasePool *_pool([[NSAutoreleasePool alloc] init]); \
87 #define CYPoolCatch(value) \
88 @catch (NSException *error) { \
89 _saved = [error retain]; \
95 [_saved autorelease]; \
99 static JSGlobalContextRef Context_;
100 static JSObjectRef System_;
101 static JSObjectRef ObjectiveC_;
103 static JSClassRef Functor_;
104 static JSClassRef Instance_;
105 static JSClassRef Internal_;
106 static JSClassRef Pointer_;
107 static JSClassRef Runtime_;
108 static JSClassRef Selector_;
109 static JSClassRef Struct_;
110 static JSClassRef Type_;
112 static JSClassRef ObjectiveC_Classes_;
113 static JSClassRef ObjectiveC_Image_Classes_;
114 static JSClassRef ObjectiveC_Images_;
115 static JSClassRef ObjectiveC_Protocols_;
117 static JSObjectRef Array_;
118 static JSObjectRef Function_;
120 static JSStringRef Result_;
122 static JSStringRef length_;
123 static JSStringRef message_;
124 static JSStringRef name_;
125 static JSStringRef toCYON_;
126 static JSStringRef toJSON_;
128 static Class NSCFBoolean_;
129 static Class NSCFType_;
130 static Class NSMessageBuilder_;
131 static Class NSZombie_;
132 static Class Object_;
134 static NSArray *Bridge_;
136 static void Finalize(JSObjectRef object) {
137 delete reinterpret_cast<CYData *>(JSObjectGetPrivate(object));
140 class Type_privateData;
150 CYValue(void *value) :
155 CYValue(const CYValue &rhs) :
160 virtual Type_privateData *GetType() const {
165 struct Selector_privateData :
168 Selector_privateData(SEL value) :
173 SEL GetValue() const {
174 return reinterpret_cast<SEL>(value_);
177 virtual Type_privateData *GetType() const;
185 Transient = (1 << 0),
186 Uninitialized = (1 << 1),
191 Instance(id value, Flags flags) :
197 virtual ~Instance() {
198 if ((flags_ & Transient) == 0)
199 // XXX: does this handle background threads correctly?
200 [GetValue() performSelector:@selector(release) withObject:nil afterDelay:0];
203 static JSObjectRef Make(JSContextRef context, id object, Flags flags) {
204 return JSObjectMake(context, Instance_, new Instance(object, flags));
207 id GetValue() const {
208 return reinterpret_cast<id>(value_);
211 bool IsUninitialized() const {
212 return (flags_ & Uninitialized) != 0;
215 virtual Type_privateData *GetType() const;
223 Internal(id value, JSObjectRef owner) :
229 static JSObjectRef Make(JSContextRef context, id object, JSObjectRef owner) {
230 return JSObjectMake(context, Internal_, new Internal(object, owner));
233 id GetValue() const {
234 return reinterpret_cast<id>(value_);
240 void Copy(apr_pool_t *pool, Type &lhs, Type &rhs);
242 void Copy(apr_pool_t *pool, Element &lhs, Element &rhs) {
243 lhs.name = apr_pstrdup(pool, rhs.name);
244 if (rhs.type == NULL)
247 lhs.type = new(pool) Type;
248 Copy(pool, *lhs.type, *rhs.type);
250 lhs.offset = rhs.offset;
253 void Copy(apr_pool_t *pool, Signature &lhs, Signature &rhs) {
254 size_t count(rhs.count);
256 lhs.elements = new(pool) Element[count];
257 for (size_t index(0); index != count; ++index)
258 Copy(pool, lhs.elements[index], rhs.elements[index]);
261 void Copy(apr_pool_t *pool, Type &lhs, Type &rhs) {
262 lhs.primitive = rhs.primitive;
263 lhs.name = apr_pstrdup(pool, rhs.name);
264 lhs.flags = rhs.flags;
266 if (sig::IsAggregate(rhs.primitive))
267 Copy(pool, lhs.data.signature, rhs.data.signature);
269 if (rhs.data.data.type != NULL) {
270 lhs.data.data.type = new(pool) Type;
271 Copy(pool, *lhs.data.data.type, *rhs.data.data.type);
274 lhs.data.data.size = rhs.data.data.size;
278 void Copy(apr_pool_t *pool, ffi_type &lhs, ffi_type &rhs) {
280 lhs.alignment = rhs.alignment;
282 if (rhs.elements == NULL)
286 while (rhs.elements[count] != NULL)
289 lhs.elements = new(pool) ffi_type *[count + 1];
290 lhs.elements[count] = NULL;
292 for (size_t index(0); index != count; ++index) {
293 // XXX: if these are libffi native then you can just take them
294 ffi_type *ffi(new(pool) ffi_type);
295 lhs.elements[index] = ffi;
296 sig::Copy(pool, *ffi, *rhs.elements[index]);
303 struct CStringMapLess :
304 std::binary_function<const char *, const char *, bool>
306 _finline bool operator ()(const char *lhs, const char *rhs) const {
307 return strcmp(lhs, rhs) < 0;
311 void Structor_(apr_pool_t *pool, const char *name, const char *types, sig::Type *&type) {
316 if (NSMutableArray *entry = [[Bridge_ objectAtIndex:2] objectForKey:[NSString stringWithUTF8String:name]])
317 switch ([[entry objectAtIndex:0] intValue]) {
319 sig::Parse(pool, &type->data.signature, [[entry objectAtIndex:1] UTF8String], &Structor_);
323 sig::Signature signature;
324 sig::Parse(pool, &signature, [[entry objectAtIndex:1] UTF8String], &Structor_);
325 type = signature.elements[0].type;
331 struct Type_privateData :
334 static Type_privateData *Object;
335 static Type_privateData *Selector;
340 void Set(sig::Type *type) {
341 type_ = new(pool_) sig::Type;
342 sig::Copy(pool_, *type_, *type);
345 Type_privateData(apr_pool_t *pool, const char *type) :
351 sig::Signature signature;
352 sig::Parse(pool_, &signature, type, &Structor_);
353 type_ = signature.elements[0].type;
356 Type_privateData(sig::Type *type) :
363 Type_privateData(sig::Type *type, ffi_type *ffi) {
364 ffi_ = new(pool_) ffi_type;
365 sig::Copy(pool_, *ffi_, *ffi);
371 ffi_ = new(pool_) ffi_type;
373 sig::Element element;
375 element.type = type_;
378 sig::Signature signature;
379 signature.elements = &element;
383 sig::sig_ffi_cif(pool_, &sig::ObjectiveC, &signature, &cif);
391 Type_privateData *Type_privateData::Object;
392 Type_privateData *Type_privateData::Selector;
394 Type_privateData *Instance::GetType() const {
395 return Type_privateData::Object;
398 Type_privateData *Selector_privateData::GetType() const {
399 return Type_privateData::Selector;
406 Type_privateData *type_;
408 Pointer(void *value, sig::Type *type, JSObjectRef owner) :
411 type_(new(pool_) Type_privateData(type))
416 struct Struct_privateData :
420 Type_privateData *type_;
422 Struct_privateData(JSObjectRef owner) :
428 typedef std::map<const char *, Type_privateData *, CStringMapLess> TypeMap;
429 static TypeMap Types_;
431 JSObjectRef CYMakeStruct(JSContextRef context, void *data, sig::Type *type, ffi_type *ffi, JSObjectRef owner) {
432 Struct_privateData *internal(new Struct_privateData(owner));
433 apr_pool_t *pool(internal->pool_);
434 Type_privateData *typical(new(pool) Type_privateData(type, ffi));
435 internal->type_ = typical;
438 internal->value_ = data;
440 size_t size(typical->GetFFI()->size);
441 void *copy(apr_palloc(internal->pool_, size));
442 memcpy(copy, data, size);
443 internal->value_ = copy;
446 return JSObjectMake(context, Struct_, internal);
449 struct Functor_privateData :
452 sig::Signature signature_;
455 Functor_privateData(const char *type, void (*value)()) :
456 CYValue(reinterpret_cast<void *>(value))
458 sig::Parse(pool_, &signature_, type, &Structor_);
459 sig::sig_ffi_cif(pool_, &sig::ObjectiveC, &signature_, &cif_);
466 JSContextRef context_;
467 JSObjectRef function_;
469 ffoData(const char *type) :
470 Functor_privateData(type, NULL)
475 JSObjectRef CYMakeInstance(JSContextRef context, id object, bool transient) {
476 Instance::Flags flags;
479 flags = Instance::Transient;
481 flags = Instance::None;
482 object = [object retain];
485 return Instance::Make(context, object, flags);
488 const char *CYPoolCString(apr_pool_t *pool, NSString *value) {
490 return [value UTF8String];
492 size_t size([value maximumLengthOfBytesUsingEncoding:NSUTF8StringEncoding] + 1);
493 char *string(new(pool) char[size]);
494 if (![value getCString:string maxLength:size encoding:NSUTF8StringEncoding])
495 @throw [NSException exceptionWithName:NSInternalInconsistencyException reason:@"[NSString getCString:maxLength:encoding:] == NO" userInfo:nil];
500 JSValueRef CYCastJSValue(JSContextRef context, bool value) {
501 return JSValueMakeBoolean(context, value);
504 JSValueRef CYCastJSValue(JSContextRef context, double value) {
505 return JSValueMakeNumber(context, value);
508 #define CYCastJSValue_(Type_) \
509 JSValueRef CYCastJSValue(JSContextRef context, Type_ value) { \
510 return JSValueMakeNumber(context, static_cast<double>(value)); \
514 CYCastJSValue_(unsigned int)
515 CYCastJSValue_(long int)
516 CYCastJSValue_(long unsigned int)
517 CYCastJSValue_(long long int)
518 CYCastJSValue_(long long unsigned int)
520 JSValueRef CYJSUndefined(JSContextRef context) {
521 return JSValueMakeUndefined(context);
524 bool CYGetIndex(const char *value, ssize_t &index) {
525 if (value[0] != '0') {
527 index = strtol(value, &end, 10);
528 if (value + strlen(value) == end)
530 } else if (value[1] == '\0') {
538 bool CYGetIndex(apr_pool_t *pool, NSString *value, ssize_t &index) {
539 return CYGetIndex(CYPoolCString(pool, value), index);
542 NSString *CYPoolNSCYON(apr_pool_t *pool, id value);
544 @interface NSMethodSignature (Cycript)
545 - (NSString *) _typeString;
548 @interface NSObject (Cycript)
550 - (JSValueRef) cy$JSValueInContext:(JSContextRef)context;
551 - (JSType) cy$JSType;
553 - (NSObject *) cy$toJSON:(NSString *)key;
554 - (NSString *) cy$toCYON;
555 - (NSString *) cy$toKey;
557 - (bool) cy$hasProperty:(NSString *)name;
558 - (NSObject *) cy$getProperty:(NSString *)name;
559 - (bool) cy$setProperty:(NSString *)name to:(NSObject *)value;
560 - (bool) cy$deleteProperty:(NSString *)name;
565 - (JSValueRef) cy$JSValueInContext:(JSContextRef)context;
568 @interface NSString (Cycript)
569 - (void *) cy$symbol;
572 struct PropertyAttributes {
577 const char *variable;
590 PropertyAttributes(objc_property_t property) :
602 name = property_getName(property);
603 const char *attributes(property_getAttributes(property));
605 for (char *state, *token(apr_strtok(apr_pstrdup(pool_, attributes), ",", &state)); token != NULL; token = apr_strtok(NULL, ",", &state)) {
607 case 'R': readonly = true; break;
608 case 'C': copy = true; break;
609 case '&': retain = true; break;
610 case 'N': nonatomic = true; break;
611 case 'G': getter_ = token + 1; break;
612 case 'S': setter_ = token + 1; break;
613 case 'V': variable = token + 1; break;
617 /*if (variable == NULL) {
618 variable = property_getName(property);
619 size_t size(strlen(variable));
620 char *name(new(pool_) char[size + 2]);
622 memcpy(name + 1, variable, size);
623 name[size + 1] = '\0';
628 const char *Getter() {
630 getter_ = apr_pstrdup(pool_, name);
634 const char *Setter() {
635 if (setter_ == NULL && !readonly) {
636 size_t length(strlen(name));
638 char *temp(new(pool_) char[length + 5]);
644 temp[3] = toupper(name[0]);
645 memcpy(temp + 4, name + 1, length - 1);
648 temp[length + 3] = ':';
649 temp[length + 4] = '\0';
658 @implementation NSProxy (Cycript)
660 - (NSObject *) cy$toJSON:(NSString *)key {
661 return [self description];
664 - (NSString *) cy$toCYON {
665 return [[self cy$toJSON:@""] cy$toCYON];
670 @implementation NSObject (Cycript)
672 - (JSValueRef) cy$JSValueInContext:(JSContextRef)context {
673 return CYMakeInstance(context, self, false);
676 - (JSType) cy$JSType {
677 return kJSTypeObject;
680 - (NSObject *) cy$toJSON:(NSString *)key {
681 return [self description];
684 - (NSString *) cy$toCYON {
685 return [[self cy$toJSON:@""] cy$toCYON];
688 - (NSString *) cy$toKey {
689 return [self cy$toCYON];
692 - (bool) cy$hasProperty:(NSString *)name {
696 - (NSObject *) cy$getProperty:(NSString *)name {
700 - (bool) cy$setProperty:(NSString *)name to:(NSObject *)value {
704 - (bool) cy$deleteProperty:(NSString *)name {
710 NSString *NSCFType$cy$toJSON(id self, SEL sel, NSString *key) {
711 return [(NSString *) CFCopyDescription((CFTypeRef) self) autorelease];
714 @implementation WebUndefined (Cycript)
716 - (JSType) cy$JSType {
717 return kJSTypeUndefined;
720 - (NSObject *) cy$toJSON:(NSString *)key {
724 - (NSString *) cy$toCYON {
728 - (JSValueRef) cy$JSValueInContext:(JSContextRef)context {
729 return CYJSUndefined(context);
734 @implementation NSNull (Cycript)
736 - (JSType) cy$JSType {
740 - (NSObject *) cy$toJSON:(NSString *)key {
744 - (NSString *) cy$toCYON {
750 @implementation NSArray (Cycript)
752 - (NSString *) cy$toCYON {
753 NSMutableString *json([[[NSMutableString alloc] init] autorelease]);
754 [json appendString:@"["];
757 for (id object in self) {
759 [json appendString:@","];
762 if (object == nil || [object cy$JSType] != kJSTypeUndefined)
763 [json appendString:CYPoolNSCYON(NULL, object)];
765 [json appendString:@","];
770 [json appendString:@"]"];
774 - (bool) cy$hasProperty:(NSString *)name {
775 if ([name isEqualToString:@"length"])
779 if (!CYGetIndex(NULL, name, index) || index < 0 || index >= static_cast<ssize_t>([self count]))
780 return [super cy$hasProperty:name];
785 - (NSObject *) cy$getProperty:(NSString *)name {
786 if ([name isEqualToString:@"length"])
787 return [NSNumber numberWithUnsignedInteger:[self count]];
790 if (!CYGetIndex(NULL, name, index) || index < 0 || index >= static_cast<ssize_t>([self count]))
791 return [super cy$getProperty:name];
793 return [self objectAtIndex:index];
798 @implementation NSMutableArray (Cycript)
800 - (bool) cy$setProperty:(NSString *)name to:(NSObject *)value {
802 if (!CYGetIndex(NULL, name, index) || index < 0 || index >= static_cast<ssize_t>([self count]))
803 return [super cy$setProperty:name to:value];
805 [self replaceObjectAtIndex:index withObject:(value ?: [NSNull null])];
810 - (bool) cy$deleteProperty:(NSString *)name {
812 if (!CYGetIndex(NULL, name, index) || index < 0 || index >= static_cast<ssize_t>([self count]))
813 return [super cy$deleteProperty:name];
815 [self removeObjectAtIndex:index];
822 @implementation NSDictionary (Cycript)
824 - (NSString *) cy$toCYON {
825 NSMutableString *json([[[NSMutableString alloc] init] autorelease]);
826 [json appendString:@"{"];
829 for (id key in self) {
831 [json appendString:@","];
834 [json appendString:[key cy$toKey]];
835 [json appendString:@":"];
836 NSObject *object([self objectForKey:key]);
837 [json appendString:CYPoolNSCYON(NULL, object)];
840 [json appendString:@"}"];
844 - (bool) cy$hasProperty:(NSString *)name {
845 return [self objectForKey:name] != nil;
848 - (NSObject *) cy$getProperty:(NSString *)name {
849 return [self objectForKey:name];
854 @implementation NSMutableDictionary (Cycript)
856 - (bool) cy$setProperty:(NSString *)name to:(NSObject *)value {
857 [self setObject:(value ?: [NSNull null]) forKey:name];
861 - (bool) cy$deleteProperty:(NSString *)name {
862 if ([self objectForKey:name] == nil)
865 [self removeObjectForKey:name];
872 @implementation NSNumber (Cycript)
874 - (JSType) cy$JSType {
875 // XXX: this just seems stupid
876 return [self class] == NSCFBoolean_ ? kJSTypeBoolean : kJSTypeNumber;
879 - (NSObject *) cy$toJSON:(NSString *)key {
883 - (NSString *) cy$toCYON {
884 return [self cy$JSType] != kJSTypeBoolean ? [self stringValue] : [self boolValue] ? @"true" : @"false";
887 - (JSValueRef) cy$JSValueInContext:(JSContextRef)context {
888 return [self cy$JSType] != kJSTypeBoolean ? CYCastJSValue(context, [self doubleValue]) : CYCastJSValue(context, [self boolValue]);
893 @implementation NSString (Cycript)
895 - (JSType) cy$JSType {
896 return kJSTypeString;
899 - (NSObject *) cy$toJSON:(NSString *)key {
903 - (NSString *) cy$toCYON {
904 // XXX: this should use the better code from Output.cpp
905 CFMutableStringRef json(CFStringCreateMutableCopy(kCFAllocatorDefault, 0, (CFStringRef) self));
907 CFStringFindAndReplace(json, CFSTR("\\"), CFSTR("\\\\"), CFRangeMake(0, CFStringGetLength(json)), 0);
908 CFStringFindAndReplace(json, CFSTR("\""), CFSTR("\\\""), CFRangeMake(0, CFStringGetLength(json)), 0);
909 CFStringFindAndReplace(json, CFSTR("\t"), CFSTR("\\t"), CFRangeMake(0, CFStringGetLength(json)), 0);
910 CFStringFindAndReplace(json, CFSTR("\r"), CFSTR("\\r"), CFRangeMake(0, CFStringGetLength(json)), 0);
911 CFStringFindAndReplace(json, CFSTR("\n"), CFSTR("\\n"), CFRangeMake(0, CFStringGetLength(json)), 0);
913 CFStringInsert(json, 0, CFSTR("\""));
914 CFStringAppend(json, CFSTR("\""));
916 return [reinterpret_cast<const NSString *>(json) autorelease];
919 - (NSString *) cy$toKey {
920 const char *value([self UTF8String]);
921 size_t size(strlen(value));
926 if (DigitRange_[value[0]]) {
928 if (!CYGetIndex(NULL, self, index) || index < 0)
931 if (!WordStartRange_[value[0]])
933 for (size_t i(1); i != size; ++i)
934 if (!WordEndRange_[value[i]])
941 return [self cy$toCYON];
944 - (void *) cy$symbol {
946 return dlsym(RTLD_DEFAULT, CYPoolCString(pool, self));
951 @interface CYJSObject : NSDictionary {
953 JSContextRef context_;
956 - (id) initWithJSObject:(JSObjectRef)object inContext:(JSContextRef)context;
958 - (NSString *) cy$toJSON:(NSString *)key;
960 - (NSUInteger) count;
961 - (id) objectForKey:(id)key;
962 - (NSEnumerator *) keyEnumerator;
963 - (void) setObject:(id)object forKey:(id)key;
964 - (void) removeObjectForKey:(id)key;
968 @interface CYJSArray : NSArray {
970 JSContextRef context_;
973 - (id) initWithJSObject:(JSObjectRef)object inContext:(JSContextRef)context;
975 - (NSUInteger) count;
976 - (id) objectAtIndex:(NSUInteger)index;
980 CYRange DigitRange_ (0x3ff000000000000LLU, 0x000000000000000LLU); // 0-9
981 CYRange WordStartRange_(0x000001000000000LLU, 0x7fffffe87fffffeLLU); // A-Za-z_$
982 CYRange WordEndRange_ (0x3ff001000000000LLU, 0x7fffffe87fffffeLLU); // A-Za-z_$0-9
987 @catch (id error) { \
988 CYThrow(context, error, exception); \
992 void CYThrow(JSContextRef context, JSValueRef value);
994 apr_status_t CYPoolRelease_(void *data) {
995 id object(reinterpret_cast<id>(data));
1000 id CYPoolRelease(apr_pool_t *pool, id object) {
1003 else if (pool == NULL)
1004 return [object autorelease];
1006 apr_pool_cleanup_register(pool, object, &CYPoolRelease_, &apr_pool_cleanup_null);
1011 CFTypeRef CYPoolRelease(apr_pool_t *pool, CFTypeRef object) {
1012 return (CFTypeRef) CYPoolRelease(pool, (id) object);
1015 id CYCastNSObject_(apr_pool_t *pool, JSContextRef context, JSObjectRef object) {
1016 JSValueRef exception(NULL);
1017 bool array(JSValueIsInstanceOfConstructor(context, object, Array_, &exception));
1018 CYThrow(context, exception);
1019 id value(array ? [CYJSArray alloc] : [CYJSObject alloc]);
1020 return CYPoolRelease(pool, [value initWithJSObject:object inContext:context]);
1023 id CYCastNSObject(apr_pool_t *pool, JSContextRef context, JSObjectRef object) {
1024 if (!JSValueIsObjectOfClass(context, object, Instance_))
1025 return CYCastNSObject_(pool, context, object);
1027 Instance *data(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
1028 return data->GetValue();
1032 JSStringRef CYCopyJSString(id value) {
1033 return value == NULL ? NULL : JSStringCreateWithCFString(reinterpret_cast<CFStringRef>([value description]));
1036 JSStringRef CYCopyJSString(const char *value) {
1037 return value == NULL ? NULL : JSStringCreateWithUTF8CString(value);
1040 JSStringRef CYCopyJSString(JSStringRef value) {
1041 return value == NULL ? NULL : JSStringRetain(value);
1044 JSStringRef CYCopyJSString(JSContextRef context, JSValueRef value) {
1045 if (JSValueIsNull(context, value))
1047 JSValueRef exception(NULL);
1048 JSStringRef string(JSValueToStringCopy(context, value, &exception));
1049 CYThrow(context, exception);
1055 JSStringRef string_;
1058 if (string_ != NULL)
1059 JSStringRelease(string_);
1063 CYJSString(const CYJSString &rhs) :
1064 string_(CYCopyJSString(rhs.string_))
1068 template <typename Arg0_>
1069 CYJSString(Arg0_ arg0) :
1070 string_(CYCopyJSString(arg0))
1074 template <typename Arg0_, typename Arg1_>
1075 CYJSString(Arg0_ arg0, Arg1_ arg1) :
1076 string_(CYCopyJSString(arg0, arg1))
1080 CYJSString &operator =(const CYJSString &rhs) {
1082 string_ = CYCopyJSString(rhs.string_);
1095 operator JSStringRef() const {
1100 CFStringRef CYCopyCFString(JSStringRef value) {
1101 return JSStringCopyCFString(kCFAllocatorDefault, value);
1104 CFStringRef CYCopyCFString(JSContextRef context, JSValueRef value) {
1105 return CYCopyCFString(CYJSString(context, value));
1108 double CYCastDouble(const char *value, size_t size) {
1110 double number(strtod(value, &end));
1111 if (end != value + size)
1116 double CYCastDouble(const char *value) {
1117 return CYCastDouble(value, strlen(value));
1120 double CYCastDouble(JSContextRef context, JSValueRef value) {
1121 JSValueRef exception(NULL);
1122 double number(JSValueToNumber(context, value, &exception));
1123 CYThrow(context, exception);
1127 CFNumberRef CYCopyCFNumber(JSContextRef context, JSValueRef value) {
1128 double number(CYCastDouble(context, value));
1129 return CFNumberCreate(kCFAllocatorDefault, kCFNumberDoubleType, &number);
1132 CFStringRef CYCopyCFString(const char *value) {
1133 return CFStringCreateWithCString(kCFAllocatorDefault, value, kCFStringEncodingUTF8);
1136 NSString *CYCastNSString(apr_pool_t *pool, const char *value) {
1137 return (NSString *) CYPoolRelease(pool, CYCopyCFString(value));
1140 NSString *CYCastNSString(apr_pool_t *pool, JSStringRef value) {
1141 return (NSString *) CYPoolRelease(pool, CYCopyCFString(value));
1144 bool CYCastBool(JSContextRef context, JSValueRef value) {
1145 return JSValueToBoolean(context, value);
1148 CFTypeRef CYCFType(apr_pool_t *pool, JSContextRef context, JSValueRef value, bool cast) {
1152 switch (JSType type = JSValueGetType(context, value)) {
1153 case kJSTypeUndefined:
1154 object = [WebUndefined undefined];
1162 case kJSTypeBoolean:
1163 object = CYCastBool(context, value) ? kCFBooleanTrue : kCFBooleanFalse;
1168 object = CYCopyCFNumber(context, value);
1173 object = CYCopyCFString(context, value);
1178 // XXX: this might could be more efficient
1179 object = (CFTypeRef) CYCastNSObject(pool, context, (JSObjectRef) value);
1184 @throw [NSException exceptionWithName:NSInternalInconsistencyException reason:[NSString stringWithFormat:@"JSValueGetType() == 0x%x", type] userInfo:nil];
1191 return CYPoolRelease(pool, object);
1193 return CFRetain(object);
1196 CFTypeRef CYCastCFType(apr_pool_t *pool, JSContextRef context, JSValueRef value) {
1197 return CYCFType(pool, context, value, true);
1200 CFTypeRef CYCopyCFType(apr_pool_t *pool, JSContextRef context, JSValueRef value) {
1201 return CYCFType(pool, context, value, false);
1204 NSArray *CYCastNSArray(JSPropertyNameArrayRef names) {
1206 size_t size(JSPropertyNameArrayGetCount(names));
1207 NSMutableArray *array([NSMutableArray arrayWithCapacity:size]);
1208 for (size_t index(0); index != size; ++index)
1209 [array addObject:CYCastNSString(pool, JSPropertyNameArrayGetNameAtIndex(names, index))];
1213 id CYCastNSObject(apr_pool_t *pool, JSContextRef context, JSValueRef value) {
1214 return reinterpret_cast<const NSObject *>(CYCastCFType(pool, context, value));
1217 void CYThrow(JSContextRef context, JSValueRef value) {
1220 @throw CYCastNSObject(NULL, context, value);
1223 JSValueRef CYJSNull(JSContextRef context) {
1224 return JSValueMakeNull(context);
1227 JSValueRef CYCastJSValue(JSContextRef context, JSStringRef value) {
1228 return value == NULL ? CYJSNull(context) : JSValueMakeString(context, value);
1231 JSValueRef CYCastJSValue(JSContextRef context, const char *value) {
1232 return CYCastJSValue(context, CYJSString(value));
1235 JSValueRef CYCastJSValue(JSContextRef context, id value) {
1237 return CYJSNull(context);
1238 else if ([value respondsToSelector:@selector(cy$JSValueInContext:)])
1239 return [value cy$JSValueInContext:context];
1241 return CYMakeInstance(context, value, false);
1244 JSObjectRef CYCastJSObject(JSContextRef context, JSValueRef value) {
1245 JSValueRef exception(NULL);
1246 JSObjectRef object(JSValueToObject(context, value, &exception));
1247 CYThrow(context, exception);
1251 JSValueRef CYGetProperty(JSContextRef context, JSObjectRef object, size_t index) {
1252 JSValueRef exception(NULL);
1253 JSValueRef value(JSObjectGetPropertyAtIndex(context, object, index, &exception));
1254 CYThrow(context, exception);
1258 JSValueRef CYGetProperty(JSContextRef context, JSObjectRef object, JSStringRef name) {
1259 JSValueRef exception(NULL);
1260 JSValueRef value(JSObjectGetProperty(context, object, name, &exception));
1261 CYThrow(context, exception);
1265 void CYSetProperty(JSContextRef context, JSObjectRef object, JSStringRef name, JSValueRef value) {
1266 JSValueRef exception(NULL);
1267 JSObjectSetProperty(context, object, name, value, kJSPropertyAttributeNone, &exception);
1268 CYThrow(context, exception);
1271 void CYThrow(JSContextRef context, id error, JSValueRef *exception) {
1272 if (exception == NULL)
1274 *exception = CYCastJSValue(context, error);
1277 JSValueRef CYCallAsFunction(JSContextRef context, JSObjectRef function, JSObjectRef _this, size_t count, JSValueRef arguments[]) {
1278 JSValueRef exception(NULL);
1279 JSValueRef value(JSObjectCallAsFunction(context, function, _this, count, arguments, &exception));
1280 CYThrow(context, exception);
1284 bool CYIsCallable(JSContextRef context, JSValueRef value) {
1285 // XXX: this isn't actually correct
1286 return value != NULL && JSValueIsObject(context, value);
1289 @implementation CYJSObject
1291 - (id) initWithJSObject:(JSObjectRef)object inContext:(JSContextRef)context {
1292 if ((self = [super init]) != nil) {
1298 - (NSObject *) cy$toJSON:(NSString *)key {
1299 JSValueRef toJSON(CYGetProperty(context_, object_, toJSON_));
1300 if (!CYIsCallable(context_, toJSON))
1301 return [super cy$toJSON:key];
1303 JSValueRef arguments[1] = {CYCastJSValue(context_, key)};
1304 JSValueRef value(CYCallAsFunction(context_, (JSObjectRef) toJSON, object_, 1, arguments));
1305 // XXX: do I really want an NSNull here?!
1306 return CYCastNSObject(NULL, context_, value) ?: [NSNull null];
1310 - (NSString *) cy$toCYON {
1311 JSValueRef toCYON(CYGetProperty(context_, object_, toCYON_));
1312 if (!CYIsCallable(context_, toCYON))
1313 return [super cy$toCYON];
1315 JSValueRef value(CYCallAsFunction(context_, (JSObjectRef) toCYON, object_, 0, NULL));
1316 return CYCastNSString(NULL, CYJSString(context_, value));
1320 - (NSUInteger) count {
1321 JSPropertyNameArrayRef names(JSObjectCopyPropertyNames(context_, object_));
1322 size_t size(JSPropertyNameArrayGetCount(names));
1323 JSPropertyNameArrayRelease(names);
1327 - (id) objectForKey:(id)key {
1328 return CYCastNSObject(NULL, context_, CYGetProperty(context_, object_, CYJSString(key))) ?: [NSNull null];
1331 - (NSEnumerator *) keyEnumerator {
1332 JSPropertyNameArrayRef names(JSObjectCopyPropertyNames(context_, object_));
1333 NSEnumerator *enumerator([CYCastNSArray(names) objectEnumerator]);
1334 JSPropertyNameArrayRelease(names);
1338 - (void) setObject:(id)object forKey:(id)key {
1339 CYSetProperty(context_, object_, CYJSString(key), CYCastJSValue(context_, object));
1342 - (void) removeObjectForKey:(id)key {
1343 JSValueRef exception(NULL);
1344 (void) JSObjectDeleteProperty(context_, object_, CYJSString(key), &exception);
1345 CYThrow(context_, exception);
1350 @implementation CYJSArray
1352 - (id) initWithJSObject:(JSObjectRef)object inContext:(JSContextRef)context {
1353 if ((self = [super init]) != nil) {
1359 - (NSUInteger) count {
1360 return CYCastDouble(context_, CYGetProperty(context_, object_, length_));
1363 - (id) objectAtIndex:(NSUInteger)index {
1364 JSValueRef exception(NULL);
1365 JSValueRef value(JSObjectGetPropertyAtIndex(context_, object_, index, &exception));
1366 CYThrow(context_, exception);
1367 return CYCastNSObject(NULL, context_, value) ?: [NSNull null];
1372 NSString *CYCopyNSCYON(id value) {
1378 Class _class(object_getClass(value));
1379 SEL sel(@selector(cy$toCYON));
1381 if (Method toCYON = class_getInstanceMethod(_class, sel))
1382 string = reinterpret_cast<NSString *(*)(id, SEL)>(method_getImplementation(toCYON))(value, sel);
1383 else if (Method methodSignatureForSelector = class_getInstanceMethod(_class, @selector(methodSignatureForSelector:))) {
1384 if (reinterpret_cast<NSMethodSignature *(*)(id, SEL, SEL)>(method_getImplementation(methodSignatureForSelector))(value, @selector(methodSignatureForSelector:), sel) != nil)
1385 string = [value cy$toCYON];
1388 if (value == NSZombie_)
1389 string = @"_NSZombie_";
1390 else if (_class == NSZombie_)
1391 string = [NSString stringWithFormat:@"<_NSZombie_: %p>", value];
1392 // XXX: frowny /in/ the pants
1393 else if (value == NSMessageBuilder_ || value == Object_)
1396 string = [NSString stringWithFormat:@"%@", value];
1399 // XXX: frowny pants
1401 string = @"undefined";
1404 return [string retain];
1407 NSString *CYCopyNSCYON(JSContextRef context, JSValueRef value, JSValueRef *exception) {
1408 if (JSValueIsNull(context, value))
1409 return [@"null" retain];
1413 return CYCopyNSCYON(CYCastNSObject(NULL, context, value));
1418 NSString *CYPoolNSCYON(apr_pool_t *pool, id value) {
1419 return CYPoolRelease(pool, static_cast<id>(CYCopyNSCYON(value)));
1422 const char *CYPoolCCYON(apr_pool_t *pool, JSContextRef context, JSValueRef value, JSValueRef *exception) {
1423 if (NSString *json = CYCopyNSCYON(context, value, exception)) {
1424 const char *string(CYPoolCString(pool, json));
1430 // XXX: use objc_getAssociatedObject and objc_setAssociatedObject on 10.6
1434 JSObjectRef object_;
1442 // XXX: delete object_? ;(
1445 static CYInternal *Get(id self) {
1446 CYInternal *internal(NULL);
1447 if (object_getInstanceVariable(self, "cy$internal_", reinterpret_cast<void **>(&internal)) == NULL) {
1448 // XXX: do something epic? ;P
1454 static CYInternal *Set(id self) {
1455 CYInternal *internal(NULL);
1456 if (Ivar ivar = object_getInstanceVariable(self, "cy$internal_", reinterpret_cast<void **>(&internal))) {
1457 if (internal == NULL) {
1458 internal = new CYInternal();
1459 object_setIvar(self, ivar, reinterpret_cast<id>(internal));
1462 // XXX: do something epic? ;P
1468 bool HasProperty(JSContextRef context, JSStringRef name) {
1469 if (object_ == NULL)
1471 return JSObjectHasProperty(context, object_, name);
1474 JSValueRef GetProperty(JSContextRef context, JSStringRef name) {
1475 if (object_ == NULL)
1477 return CYGetProperty(context, object_, name);
1480 void SetProperty(JSContextRef context, JSStringRef name, JSValueRef value) {
1481 if (object_ == NULL)
1482 object_ = JSObjectMake(context, NULL, NULL);
1483 CYSetProperty(context, object_, name, value);
1487 JSObjectRef CYMakeSelector(JSContextRef context, SEL sel) {
1488 Selector_privateData *data(new Selector_privateData(sel));
1489 return JSObjectMake(context, Selector_, data);
1492 JSObjectRef CYMakePointer(JSContextRef context, void *pointer, sig::Type *type, ffi_type *ffi, JSObjectRef owner) {
1493 Pointer *data(new Pointer(pointer, type, owner));
1494 return JSObjectMake(context, Pointer_, data);
1497 JSObjectRef CYMakeFunctor(JSContextRef context, void (*function)(), const char *type) {
1498 Functor_privateData *data(new Functor_privateData(type, function));
1499 return JSObjectMake(context, Functor_, data);
1502 const char *CYPoolCString(apr_pool_t *pool, JSStringRef value) {
1504 const char *string([CYCastNSString(NULL, value) UTF8String]);
1507 size_t size(JSStringGetMaximumUTF8CStringSize(value));
1508 char *string(new(pool) char[size]);
1509 JSStringGetUTF8CString(value, string, size);
1514 const char *CYPoolCString(apr_pool_t *pool, JSContextRef context, JSValueRef value) {
1515 return JSValueIsNull(context, value) ? NULL : CYPoolCString(pool, CYJSString(context, value));
1518 bool CYGetIndex(apr_pool_t *pool, JSStringRef value, ssize_t &index) {
1519 return CYGetIndex(CYPoolCString(pool, value), index);
1522 // XXX: this macro is unhygenic
1523 #define CYCastCString(context, value) ({ \
1525 if (value == NULL) \
1527 else if (JSStringRef string = CYCopyJSString(context, value)) { \
1528 size_t size(JSStringGetMaximumUTF8CStringSize(string)); \
1529 utf8 = reinterpret_cast<char *>(alloca(size)); \
1530 JSStringGetUTF8CString(string, utf8, size); \
1531 JSStringRelease(string); \
1537 void *CYCastPointer_(JSContextRef context, JSValueRef value) {
1538 switch (JSValueGetType(context, value)) {
1541 /*case kJSTypeString:
1542 return dlsym(RTLD_DEFAULT, CYCastCString(context, value));
1544 if (JSValueIsObjectOfClass(context, value, Pointer_)) {
1545 Pointer *data(reinterpret_cast<Pointer *>(JSObjectGetPrivate((JSObjectRef) value)));
1546 return data->value_;
1549 double number(CYCastDouble(context, value));
1550 if (std::isnan(number))
1551 @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"cannot convert value to pointer" userInfo:nil];
1552 return reinterpret_cast<void *>(static_cast<uintptr_t>(static_cast<long long>(number)));
1556 template <typename Type_>
1557 _finline Type_ CYCastPointer(JSContextRef context, JSValueRef value) {
1558 return reinterpret_cast<Type_>(CYCastPointer_(context, value));
1561 SEL CYCastSEL(JSContextRef context, JSValueRef value) {
1562 if (JSValueIsObjectOfClass(context, value, Selector_)) {
1563 Selector_privateData *data(reinterpret_cast<Selector_privateData *>(JSObjectGetPrivate((JSObjectRef) value)));
1564 return reinterpret_cast<SEL>(data->value_);
1566 return CYCastPointer<SEL>(context, value);
1569 void CYPoolFFI(apr_pool_t *pool, JSContextRef context, sig::Type *type, ffi_type *ffi, void *data, JSValueRef value) {
1570 switch (type->primitive) {
1571 case sig::boolean_P:
1572 *reinterpret_cast<bool *>(data) = JSValueToBoolean(context, value);
1575 #define CYPoolFFI_(primitive, native) \
1576 case sig::primitive ## _P: \
1577 *reinterpret_cast<native *>(data) = CYCastDouble(context, value); \
1580 CYPoolFFI_(uchar, unsigned char)
1581 CYPoolFFI_(char, char)
1582 CYPoolFFI_(ushort, unsigned short)
1583 CYPoolFFI_(short, short)
1584 CYPoolFFI_(ulong, unsigned long)
1585 CYPoolFFI_(long, long)
1586 CYPoolFFI_(uint, unsigned int)
1587 CYPoolFFI_(int, int)
1588 CYPoolFFI_(ulonglong, unsigned long long)
1589 CYPoolFFI_(longlong, long long)
1590 CYPoolFFI_(float, float)
1591 CYPoolFFI_(double, double)
1594 case sig::typename_P:
1595 *reinterpret_cast<id *>(data) = CYCastNSObject(pool, context, value);
1598 case sig::selector_P:
1599 *reinterpret_cast<SEL *>(data) = CYCastSEL(context, value);
1602 case sig::pointer_P:
1603 *reinterpret_cast<void **>(data) = CYCastPointer<void *>(context, value);
1607 *reinterpret_cast<const char **>(data) = CYPoolCString(pool, context, value);
1610 case sig::struct_P: {
1611 uint8_t *base(reinterpret_cast<uint8_t *>(data));
1612 JSObjectRef aggregate(JSValueIsObject(context, value) ? (JSObjectRef) value : NULL);
1613 for (size_t index(0); index != type->data.signature.count; ++index) {
1614 sig::Element *element(&type->data.signature.elements[index]);
1615 ffi_type *field(ffi->elements[index]);
1618 if (aggregate == NULL)
1621 rhs = CYGetProperty(context, aggregate, index);
1622 if (JSValueIsUndefined(context, rhs)) {
1623 if (element->name != NULL)
1624 rhs = CYGetProperty(context, aggregate, CYJSString(element->name));
1627 if (JSValueIsUndefined(context, rhs)) undefined:
1628 @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"unable to extract structure value" userInfo:nil];
1632 CYPoolFFI(pool, context, element->type, field, base, rhs);
1634 base += field->size;
1642 NSLog(@"CYPoolFFI(%c)\n", type->primitive);
1647 JSValueRef CYFromFFI(JSContextRef context, sig::Type *type, ffi_type *ffi, void *data, bool initialize = false, JSObjectRef owner = NULL) {
1650 switch (type->primitive) {
1651 case sig::boolean_P:
1652 value = CYCastJSValue(context, *reinterpret_cast<bool *>(data));
1655 #define CYFromFFI_(primitive, native) \
1656 case sig::primitive ## _P: \
1657 value = CYCastJSValue(context, *reinterpret_cast<native *>(data)); \
1660 CYFromFFI_(uchar, unsigned char)
1661 CYFromFFI_(char, char)
1662 CYFromFFI_(ushort, unsigned short)
1663 CYFromFFI_(short, short)
1664 CYFromFFI_(ulong, unsigned long)
1665 CYFromFFI_(long, long)
1666 CYFromFFI_(uint, unsigned int)
1667 CYFromFFI_(int, int)
1668 CYFromFFI_(ulonglong, unsigned long long)
1669 CYFromFFI_(longlong, long long)
1670 CYFromFFI_(float, float)
1671 CYFromFFI_(double, double)
1673 case sig::object_P: {
1674 if (id object = *reinterpret_cast<id *>(data)) {
1675 value = CYCastJSValue(context, object);
1681 case sig::typename_P:
1682 value = CYMakeInstance(context, *reinterpret_cast<Class *>(data), true);
1685 case sig::selector_P:
1686 if (SEL sel = *reinterpret_cast<SEL *>(data))
1687 value = CYMakeSelector(context, sel);
1691 case sig::pointer_P:
1692 if (void *pointer = *reinterpret_cast<void **>(data))
1693 value = CYMakePointer(context, pointer, type->data.data.type, ffi, owner);
1698 if (char *utf8 = *reinterpret_cast<char **>(data))
1699 value = CYCastJSValue(context, utf8);
1704 value = CYMakeStruct(context, data, type, ffi, owner);
1708 value = CYJSUndefined(context);
1712 value = CYJSNull(context);
1716 NSLog(@"CYFromFFI(%c)\n", type->primitive);
1723 static bool CYImplements(id object, Class _class, SEL selector) {
1724 // XXX: possibly use a more "awesome" check?
1725 return class_getInstanceMethod(_class, selector) != NULL;
1728 static bool Instance_hasProperty(JSContextRef context, JSObjectRef object, JSStringRef property) {
1729 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
1730 id self(internal->GetValue());
1732 if (JSStringIsEqualToUTF8CString(property, "$cyi"))
1736 NSString *name(CYCastNSString(pool, property));
1738 if (CYInternal *internal = CYInternal::Get(self))
1739 if (internal->HasProperty(context, property))
1743 if ([self cy$hasProperty:name])
1745 } CYPoolCatch(false)
1747 const char *string(CYPoolCString(pool, name));
1748 Class _class(object_getClass(self));
1750 if (class_getProperty(_class, string) != NULL)
1753 if (SEL sel = sel_getUid(string))
1754 if (CYImplements(self, _class, sel))
1760 static JSValueRef Instance_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) {
1761 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
1762 id self(internal->GetValue());
1764 if (JSStringIsEqualToUTF8CString(property, "$cyi"))
1765 return Internal::Make(context, self, object);
1769 NSString *name(CYCastNSString(pool, property));
1771 if (CYInternal *internal = CYInternal::Get(self))
1772 if (JSValueRef value = internal->GetProperty(context, property))
1776 if (NSObject *data = [self cy$getProperty:name])
1777 return CYCastJSValue(context, data);
1780 const char *string(CYPoolCString(pool, name));
1781 Class _class(object_getClass(self));
1783 if (objc_property_t property = class_getProperty(_class, string)) {
1784 PropertyAttributes attributes(property);
1785 SEL sel(sel_registerName(attributes.Getter()));
1786 return CYSendMessage(pool, context, self, sel, 0, NULL, false, exception);
1789 if (SEL sel = sel_getUid(string))
1790 if (CYImplements(self, _class, sel))
1791 return CYSendMessage(pool, context, self, sel, 0, NULL, false, exception);
1797 static bool Instance_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) {
1801 id self(CYCastNSObject(pool, context, object));
1802 NSString *name(CYCastNSString(pool, property));
1803 NSString *data(CYCastNSObject(pool, context, value));
1806 if ([self cy$setProperty:name to:data])
1810 const char *string(CYPoolCString(pool, name));
1811 Class _class(object_getClass(self));
1813 if (objc_property_t property = class_getProperty(_class, string)) {
1814 PropertyAttributes attributes(property);
1815 if (const char *setter = attributes.Setter()) {
1816 SEL sel(sel_registerName(setter));
1817 JSValueRef arguments[1] = {value};
1818 CYSendMessage(pool, context, self, sel, 1, arguments, false, exception);
1823 size_t length(strlen(string));
1825 char set[length + 5];
1831 if (string[0] != '\0') {
1832 set[3] = toupper(string[0]);
1833 memcpy(set + 4, string + 1, length - 1);
1836 set[length + 3] = ':';
1837 set[length + 4] = '\0';
1839 if (SEL sel = sel_getUid(set))
1840 if (CYImplements(self, _class, sel)) {
1841 JSValueRef arguments[1] = {value};
1842 CYSendMessage(pool, context, self, sel, 1, arguments, false, exception);
1845 if (CYInternal *internal = CYInternal::Set(self)) {
1846 internal->SetProperty(context, property, value);
1854 static bool Instance_deleteProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) {
1857 id self(CYCastNSObject(NULL, context, object));
1858 NSString *name(CYCastNSString(NULL, property));
1859 return [self cy$deleteProperty:name];
1864 static void Instance_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) {
1866 NSString *self(CYCastNSObject(pool, context, object));
1867 Class _class(object_getClass(self));
1871 objc_property_t *data(class_copyPropertyList(_class, &size));
1872 for (size_t i(0); i != size; ++i)
1873 JSPropertyNameAccumulatorAddName(names, CYJSString(property_getName(data[i])));
1878 static JSObjectRef Instance_callAsConstructor(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
1880 Instance *data(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
1881 JSObjectRef value(Instance::Make(context, [data->GetValue() alloc], Instance::Uninitialized));
1886 static bool Internal_hasProperty(JSContextRef context, JSObjectRef object, JSStringRef property) {
1887 Internal *internal(reinterpret_cast<Internal *>(JSObjectGetPrivate(object)));
1890 id self(internal->GetValue());
1891 const char *name(CYPoolCString(pool, property));
1893 if (object_getInstanceVariable(self, name, NULL) != NULL)
1899 static JSValueRef Internal_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) {
1900 Internal *internal(reinterpret_cast<Internal *>(JSObjectGetPrivate(object)));
1904 id self(internal->GetValue());
1905 const char *name(CYPoolCString(pool, property));
1907 if (Ivar ivar = object_getInstanceVariable(self, name, NULL)) {
1908 Type_privateData type(pool, ivar_getTypeEncoding(ivar));
1909 return CYFromFFI(context, type.type_, type.GetFFI(), reinterpret_cast<uint8_t *>(self) + ivar_getOffset(ivar));
1916 static bool Internal_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) {
1917 Internal *internal(reinterpret_cast<Internal *>(JSObjectGetPrivate(object)));
1921 id self(internal->GetValue());
1922 const char *name(CYPoolCString(pool, property));
1924 if (Ivar ivar = object_getInstanceVariable(self, name, NULL)) {
1925 Type_privateData type(pool, ivar_getTypeEncoding(ivar));
1926 CYPoolFFI(pool, context, type.type_, type.GetFFI(), reinterpret_cast<uint8_t *>(self) + ivar_getOffset(ivar), value);
1934 static void Internal_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) {
1935 Internal *internal(reinterpret_cast<Internal *>(JSObjectGetPrivate(object)));
1938 id self(internal->GetValue());
1939 Class _class(object_getClass(self));
1941 for (Class super(_class); super != NULL; super = class_getSuperclass(super)) {
1943 Ivar *data(class_copyIvarList(super, &size));
1944 for (size_t i(0); i != size; ++i)
1945 JSPropertyNameAccumulatorAddName(names, CYJSString(ivar_getName(data[i])));
1950 static JSValueRef Internal_callAsFunction_$cya(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
1951 Internal *internal(reinterpret_cast<Internal *>(JSObjectGetPrivate(object)));
1952 return internal->owner_;
1955 bool Index_(apr_pool_t *pool, Struct_privateData *internal, JSStringRef property, ssize_t &index, uint8_t *&base) {
1956 Type_privateData *typical(internal->type_);
1957 sig::Type *type(typical->type_);
1961 const char *name(CYPoolCString(pool, property));
1962 size_t length(strlen(name));
1963 double number(CYCastDouble(name, length));
1965 size_t count(type->data.signature.count);
1967 if (std::isnan(number)) {
1968 if (property == NULL)
1971 sig::Element *elements(type->data.signature.elements);
1973 for (size_t local(0); local != count; ++local) {
1974 sig::Element *element(&elements[local]);
1975 if (element->name != NULL && strcmp(name, element->name) == 0) {
1983 index = static_cast<ssize_t>(number);
1984 if (index != number || index < 0 || static_cast<size_t>(index) >= count)
1989 ffi_type **elements(typical->GetFFI()->elements);
1991 base = reinterpret_cast<uint8_t *>(internal->value_);
1992 for (ssize_t local(0); local != index; ++local)
1993 base += elements[local]->size;
1998 static JSValueRef Pointer_getIndex(JSContextRef context, JSObjectRef object, size_t index, JSValueRef *exception) {
1999 Pointer *internal(reinterpret_cast<Pointer *>(JSObjectGetPrivate(object)));
2000 Type_privateData *typical(internal->type_);
2002 ffi_type *ffi(typical->GetFFI());
2004 uint8_t *base(reinterpret_cast<uint8_t *>(internal->value_));
2005 base += ffi->size * index;
2007 JSObjectRef owner(internal->owner_ ?: object);
2010 return CYFromFFI(context, typical->type_, ffi, base, false, owner);
2014 static JSValueRef Pointer_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) {
2016 Pointer *internal(reinterpret_cast<Pointer *>(JSObjectGetPrivate(object)));
2017 Type_privateData *typical(internal->type_);
2019 if (typical->type_ == NULL)
2023 if (!CYGetIndex(pool, property, index))
2026 return Pointer_getIndex(context, object, index, exception);
2029 static JSValueRef Pointer_getProperty_$cyi(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) {
2030 return Pointer_getIndex(context, object, 0, exception);
2033 static bool Pointer_setIndex(JSContextRef context, JSObjectRef object, size_t index, JSValueRef value, JSValueRef *exception) {
2034 Pointer *internal(reinterpret_cast<Pointer *>(JSObjectGetPrivate(object)));
2035 Type_privateData *typical(internal->type_);
2037 ffi_type *ffi(typical->GetFFI());
2039 uint8_t *base(reinterpret_cast<uint8_t *>(internal->value_));
2040 base += ffi->size * index;
2043 CYPoolFFI(NULL, context, typical->type_, ffi, base, value);
2048 static bool Pointer_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) {
2050 Pointer *internal(reinterpret_cast<Pointer *>(JSObjectGetPrivate(object)));
2051 Type_privateData *typical(internal->type_);
2053 if (typical->type_ == NULL)
2057 if (!CYGetIndex(pool, property, index))
2060 return Pointer_setIndex(context, object, 0, value, exception);
2063 static bool Pointer_setProperty_$cyi(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) {
2064 return Pointer_setIndex(context, object, 0, value, exception);
2067 static JSValueRef Struct_callAsFunction_$cya(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
2068 Struct_privateData *internal(reinterpret_cast<Struct_privateData *>(JSObjectGetPrivate(_this)));
2069 Type_privateData *typical(internal->type_);
2070 return CYMakePointer(context, internal->value_, typical->type_, typical->ffi_, _this);
2073 static JSValueRef Struct_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) {
2075 Struct_privateData *internal(reinterpret_cast<Struct_privateData *>(JSObjectGetPrivate(object)));
2076 Type_privateData *typical(internal->type_);
2081 if (!Index_(pool, internal, property, index, base))
2084 JSObjectRef owner(internal->owner_ ?: object);
2087 return CYFromFFI(context, typical->type_->data.signature.elements[index].type, typical->GetFFI()->elements[index], base, false, owner);
2091 static bool Struct_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) {
2093 Struct_privateData *internal(reinterpret_cast<Struct_privateData *>(JSObjectGetPrivate(object)));
2094 Type_privateData *typical(internal->type_);
2099 if (!Index_(pool, internal, property, index, base))
2103 CYPoolFFI(NULL, context, typical->type_->data.signature.elements[index].type, typical->GetFFI()->elements[index], base, value);
2108 static void Struct_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) {
2109 Struct_privateData *internal(reinterpret_cast<Struct_privateData *>(JSObjectGetPrivate(object)));
2110 Type_privateData *typical(internal->type_);
2111 sig::Type *type(typical->type_);
2116 size_t count(type->data.signature.count);
2117 sig::Element *elements(type->data.signature.elements);
2121 for (size_t index(0); index != count; ++index) {
2123 name = elements[index].name;
2126 sprintf(number, "%lu", index);
2130 JSPropertyNameAccumulatorAddName(names, CYJSString(name));
2134 JSValueRef CYCallFunction(apr_pool_t *pool, JSContextRef context, size_t setups, void *setup[], size_t count, const JSValueRef arguments[], bool initialize, JSValueRef *exception, sig::Signature *signature, ffi_cif *cif, void (*function)()) {
2136 if (setups + count != signature->count - 1)
2137 @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"incorrect number of arguments to ffi function" userInfo:nil];
2139 size_t size(setups + count);
2141 memcpy(values, setup, sizeof(void *) * setups);
2143 for (size_t index(setups); index != size; ++index) {
2144 sig::Element *element(&signature->elements[index + 1]);
2145 ffi_type *ffi(cif->arg_types[index]);
2147 values[index] = new(pool) uint8_t[ffi->size];
2148 CYPoolFFI(pool, context, element->type, ffi, values[index], arguments[index - setups]);
2151 uint8_t value[cif->rtype->size];
2152 ffi_call(cif, function, value, values);
2154 return CYFromFFI(context, signature->elements[0].type, cif->rtype, value, initialize);
2158 void Closure_(ffi_cif *cif, void *result, void **arguments, void *arg) {
2159 ffoData *data(reinterpret_cast<ffoData *>(arg));
2161 JSContextRef context(data->context_);
2163 size_t count(data->cif_.nargs);
2164 JSValueRef values[count];
2166 for (size_t index(0); index != count; ++index)
2167 values[index] = CYFromFFI(context, data->signature_.elements[1 + index].type, data->cif_.arg_types[index], arguments[index]);
2169 JSValueRef value(CYCallAsFunction(context, data->function_, NULL, count, values));
2170 CYPoolFFI(NULL, context, data->signature_.elements[0].type, data->cif_.rtype, result, value);
2173 JSObjectRef CYMakeFunctor(JSContextRef context, JSObjectRef function, const char *type) {
2174 // XXX: in case of exceptions this will leak
2175 ffoData *data(new ffoData(type));
2177 ffi_closure *closure((ffi_closure *) _syscall(mmap(
2178 NULL, sizeof(ffi_closure),
2179 PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE,
2183 ffi_status status(ffi_prep_closure(closure, &data->cif_, &Closure_, data));
2184 _assert(status == FFI_OK);
2186 _syscall(mprotect(closure, sizeof(*closure), PROT_READ | PROT_EXEC));
2188 data->value_ = closure;
2190 data->context_ = CYGetJSContext();
2191 data->function_ = function;
2193 return JSObjectMake(context, Functor_, data);
2196 static JSValueRef ObjectiveC_Classes_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) {
2199 NSString *name(CYCastNSString(pool, property));
2200 if (Class _class = NSClassFromString(name))
2201 return CYMakeInstance(context, _class, true);
2206 static void ObjectiveC_Classes_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) {
2207 size_t size(objc_getClassList(NULL, 0));
2208 Class *data(reinterpret_cast<Class *>(malloc(sizeof(Class) * size)));
2211 size_t writ(objc_getClassList(data, size));
2214 if (Class *copy = reinterpret_cast<Class *>(realloc(data, sizeof(Class) * writ))) {
2220 for (size_t i(0); i != writ; ++i)
2221 JSPropertyNameAccumulatorAddName(names, CYJSString(class_getName(data[i])));
2227 static JSValueRef ObjectiveC_Image_Classes_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) {
2228 const char *internal(reinterpret_cast<const char *>(JSObjectGetPrivate(object)));
2232 const char *name(CYPoolCString(pool, property));
2234 const char **data(objc_copyClassNamesForImage(internal, &size));
2236 for (size_t i(0); i != size; ++i)
2237 if (strcmp(name, data[i]) == 0) {
2238 if (Class _class = objc_getClass(name)) {
2239 value = CYMakeInstance(context, _class, true);
2251 static void ObjectiveC_Image_Classes_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) {
2252 const char *internal(reinterpret_cast<const char *>(JSObjectGetPrivate(object)));
2254 const char **data(objc_copyClassNamesForImage(internal, &size));
2255 for (size_t i(0); i != size; ++i)
2256 JSPropertyNameAccumulatorAddName(names, CYJSString(data[i]));
2260 static JSValueRef ObjectiveC_Images_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) {
2263 const char *name(CYPoolCString(pool, property));
2265 const char **data(objc_copyImageNames(&size));
2266 for (size_t i(0); i != size; ++i)
2267 if (strcmp(name, data[i]) == 0) {
2276 JSObjectRef value(JSObjectMake(context, NULL, NULL));
2277 CYSetProperty(context, value, CYJSString("classes"), JSObjectMake(context, ObjectiveC_Image_Classes_, const_cast<char *>(name)));
2282 static void ObjectiveC_Images_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) {
2284 const char **data(objc_copyImageNames(&size));
2285 for (size_t i(0); i != size; ++i)
2286 JSPropertyNameAccumulatorAddName(names, CYJSString(data[i]));
2290 static JSValueRef ObjectiveC_Protocols_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) {
2293 NSString *name(CYCastNSString(pool, property));
2294 if (Protocol *protocol = NSProtocolFromString(name))
2295 return CYMakeInstance(context, protocol, true);
2300 static void ObjectiveC_Protocols_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) {
2302 Protocol **data(objc_copyProtocolList(&size));
2303 for (size_t i(0); i != size; ++i)
2304 JSPropertyNameAccumulatorAddName(names, CYJSString(protocol_getName(data[i])));
2308 static JSValueRef Runtime_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) {
2309 if (JSStringIsEqualToUTF8CString(property, "nil"))
2310 return Instance::Make(context, nil, Instance::None);
2314 NSString *name(CYCastNSString(pool, property));
2315 if (Class _class = NSClassFromString(name))
2316 return CYMakeInstance(context, _class, true);
2317 if (NSMutableArray *entry = [[Bridge_ objectAtIndex:0] objectForKey:name])
2318 switch ([[entry objectAtIndex:0] intValue]) {
2320 return JSEvaluateScript(CYGetJSContext(), CYJSString([entry objectAtIndex:1]), NULL, NULL, 0, NULL);
2322 return CYMakeFunctor(context, reinterpret_cast<void (*)()>([name cy$symbol]), CYPoolCString(pool, [entry objectAtIndex:1]));
2324 // XXX: this is horrendously inefficient
2325 sig::Signature signature;
2326 sig::Parse(pool, &signature, CYPoolCString(pool, [entry objectAtIndex:1]), &Structor_);
2328 sig::sig_ffi_cif(pool, &sig::ObjectiveC, &signature, &cif);
2329 return CYFromFFI(context, signature.elements[0].type, cif.rtype, [name cy$symbol]);
2335 bool stret(ffi_type *ffi_type) {
2336 return ffi_type->type == FFI_TYPE_STRUCT && (
2337 ffi_type->size > OBJC_MAX_STRUCT_BY_VALUE ||
2338 struct_forward_array[ffi_type->size] != 0
2343 int *_NSGetArgc(void);
2344 char ***_NSGetArgv(void);
2345 int UIApplicationMain(int argc, char *argv[], NSString *principalClassName, NSString *delegateClassName);
2348 static JSValueRef System_print(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
2350 NSLog(@"%s", CYCastCString(context, arguments[0]));
2351 return CYJSUndefined(context);
2355 JSValueRef CYSendMessage(apr_pool_t *pool, JSContextRef context, id self, SEL _cmd, size_t count, const JSValueRef arguments[], bool initialize, JSValueRef *exception) {
2358 Class _class(object_getClass(self));
2359 if (Method method = class_getInstanceMethod(_class, _cmd))
2360 type = method_getTypeEncoding(method);
2364 NSMethodSignature *method([self methodSignatureForSelector:_cmd]);
2366 @throw [NSException exceptionWithName:NSInvalidArgumentException reason:[NSString stringWithFormat:@"unrecognized selector %s sent to object %p", sel_getName(_cmd), self] userInfo:nil];
2367 type = CYPoolCString(pool, [method _typeString]);
2376 sig::Signature signature;
2377 sig::Parse(pool, &signature, type, &Structor_);
2380 sig::sig_ffi_cif(pool, &sig::ObjectiveC, &signature, &cif);
2382 void (*function)() = stret(cif.rtype) ? reinterpret_cast<void (*)()>(&objc_msgSend_stret) : reinterpret_cast<void (*)()>(&objc_msgSend);
2383 return CYCallFunction(pool, context, 2, setup, count, arguments, initialize, exception, &signature, &cif, function);
2386 static JSValueRef $objc_msgSend(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
2396 @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"too few arguments to objc_msgSend" userInfo:nil];
2398 if (JSValueIsObjectOfClass(context, arguments[0], Instance_)) {
2399 Instance *data(reinterpret_cast<Instance *>(JSObjectGetPrivate((JSObjectRef) arguments[0])));
2400 self = data->GetValue();
2401 uninitialized = data->IsUninitialized();
2405 self = CYCastNSObject(pool, context, arguments[0]);
2406 uninitialized = false;
2410 return CYJSNull(context);
2412 _cmd = CYCastSEL(context, arguments[1]);
2415 return CYSendMessage(pool, context, self, _cmd, count - 2, arguments + 2, uninitialized, exception);
2418 MSHook(void, CYDealloc, id self, SEL sel) {
2419 CYInternal *internal;
2420 object_getInstanceVariable(self, "cy$internal_", reinterpret_cast<void **>(&internal));
2421 if (internal != NULL)
2423 _CYDealloc(self, sel);
2426 MSHook(void, objc_registerClassPair, Class _class) {
2427 Class super(class_getSuperclass(_class));
2428 if (super == NULL || class_getInstanceVariable(super, "cy$internal_") == NULL) {
2429 class_addIvar(_class, "cy$internal_", sizeof(CYInternal *), log2(sizeof(CYInternal *)), "^{CYInternal}");
2430 MSHookMessage(_class, @selector(dealloc), MSHake(CYDealloc));
2433 _objc_registerClassPair(_class);
2436 static JSValueRef objc_registerClassPair_(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
2439 @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"incorrect number of arguments to objc_registerClassPair" userInfo:nil];
2441 Class _class(CYCastNSObject(pool, context, arguments[0]));
2442 $objc_registerClassPair(_class);
2443 return CYJSUndefined(context);
2447 static JSValueRef Selector_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
2448 JSValueRef setup[count + 2];
2451 memcpy(setup + 2, arguments, sizeof(JSValueRef) * count);
2452 return $objc_msgSend(context, NULL, NULL, count + 2, setup, exception);
2455 static JSValueRef Functor_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
2457 Functor_privateData *data(reinterpret_cast<Functor_privateData *>(JSObjectGetPrivate(object)));
2458 return CYCallFunction(pool, context, 0, NULL, count, arguments, false, exception, &data->signature_, &data->cif_, reinterpret_cast<void (*)()>(data->value_));
2461 JSObjectRef Selector_new(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
2464 @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"incorrect number of arguments to Selector constructor" userInfo:nil];
2465 const char *name(CYCastCString(context, arguments[0]));
2466 return CYMakeSelector(context, sel_registerName(name));
2470 JSObjectRef Pointer_new(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
2473 @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"incorrect number of arguments to Functor constructor" userInfo:nil];
2475 void *value(CYCastPointer<void *>(context, arguments[0]));
2476 const char *type(CYCastCString(context, arguments[1]));
2480 sig::Signature signature;
2481 sig::Parse(pool, &signature, type, &Structor_);
2483 return CYMakePointer(context, value, signature.elements[0].type, NULL, NULL);
2487 JSObjectRef CYMakeType(JSContextRef context, JSObjectRef object, const char *type) {
2488 Type_privateData *internal(new Type_privateData(NULL, type));
2489 return JSObjectMake(context, Type_, internal);
2492 JSObjectRef Type_new(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
2495 @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"incorrect number of arguments to Type constructor" userInfo:nil];
2496 const char *type(CYCastCString(context, arguments[0]));
2497 return CYMakeType(context, object, type);
2501 static JSValueRef Type_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
2504 @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"incorrect number of arguments to type cast function" userInfo:nil];
2505 Type_privateData *internal(reinterpret_cast<Type_privateData *>(JSObjectGetPrivate(object)));
2506 sig::Type *type(internal->type_);
2507 ffi_type *ffi(internal->GetFFI());
2509 uint8_t value[ffi->size];
2511 CYPoolFFI(pool, context, type, ffi, value, arguments[0]);
2512 return CYFromFFI(context, type, ffi, value);
2516 static JSObjectRef Type_callAsConstructor(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
2519 @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"incorrect number of arguments to type cast function" userInfo:nil];
2520 Type_privateData *internal(reinterpret_cast<Type_privateData *>(JSObjectGetPrivate(object)));
2521 size_t size(count == 0 ? 0 : CYCastDouble(context, arguments[0]));
2523 void *value(malloc(internal->GetFFI()->size * size));
2524 return CYMakePointer(context, value, internal->type_, internal->ffi_, NULL);
2528 JSObjectRef Instance_new(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
2531 @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"incorrect number of arguments to Instance constructor" userInfo:nil];
2532 id self(count == 0 ? nil : CYCastPointer<id>(context, arguments[0]));
2533 return Instance::Make(context, self, Instance::None);
2537 JSObjectRef Functor_new(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
2540 @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"incorrect number of arguments to Functor constructor" userInfo:nil];
2541 const char *type(CYCastCString(context, arguments[1]));
2542 JSValueRef exception(NULL);
2543 if (JSValueIsInstanceOfConstructor(context, arguments[0], Function_, &exception)) {
2544 JSObjectRef function(CYCastJSObject(context, arguments[0]));
2545 return CYMakeFunctor(context, function, type);
2546 } else if (exception != NULL) {
2549 void (*function)()(CYCastPointer<void (*)()>(context, arguments[0]));
2550 return CYMakeFunctor(context, function, type);
2555 JSValueRef CYValue_getProperty_value(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) {
2556 CYValue *internal(reinterpret_cast<CYValue *>(JSObjectGetPrivate(object)));
2557 return CYCastJSValue(context, reinterpret_cast<uintptr_t>(internal->value_));
2560 JSValueRef Selector_getProperty_prototype(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) {
2564 static JSValueRef CYValue_callAsFunction_$cya(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
2565 CYValue *internal(reinterpret_cast<CYValue *>(JSObjectGetPrivate(_this)));
2566 Type_privateData *typical(internal->GetType());
2571 if (typical == NULL) {
2575 type = typical->type_;
2576 ffi = typical->ffi_;
2579 return CYMakePointer(context, &internal->value_, type, ffi, object);
2582 static JSValueRef CYValue_callAsFunction_valueOf(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
2583 CYValue *internal(reinterpret_cast<CYValue *>(JSObjectGetPrivate(_this)));
2586 return CYCastJSValue(context, reinterpret_cast<uintptr_t>(internal->value_));
2590 static JSValueRef CYValue_callAsFunction_toJSON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
2591 return CYValue_callAsFunction_valueOf(context, object, _this, count, arguments, exception);
2594 static JSValueRef CYValue_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
2595 CYValue *internal(reinterpret_cast<CYValue *>(JSObjectGetPrivate(_this)));
2597 sprintf(string, "%p", internal->value_);
2600 return CYCastJSValue(context, string);
2604 static JSValueRef Instance_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
2605 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(_this)));
2609 return CYCastJSValue(context, CYJSString(CYPoolNSCYON(NULL, internal->GetValue())));
2614 static JSValueRef Instance_callAsFunction_toJSON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
2615 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(_this)));
2619 NSString *key(count == 0 ? nil : CYCastNSString(NULL, CYJSString(context, arguments[0])));
2620 return CYCastJSValue(context, CYJSString([internal->GetValue() cy$toJSON:key]));
2625 static JSValueRef Instance_callAsFunction_toString(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
2626 Instance *data(reinterpret_cast<Instance *>(JSObjectGetPrivate(_this)));
2630 return CYCastJSValue(context, CYJSString([data->GetValue() description]));
2635 static JSValueRef Selector_callAsFunction_toString(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
2636 Selector_privateData *data(reinterpret_cast<Selector_privateData *>(JSObjectGetPrivate(_this)));
2639 return CYCastJSValue(context, sel_getName(data->GetValue()));
2643 static JSValueRef Selector_callAsFunction_toJSON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
2644 return Selector_callAsFunction_toString(context, object, _this, count, arguments, exception);
2647 static JSValueRef Selector_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
2648 Selector_privateData *internal(reinterpret_cast<Selector_privateData *>(JSObjectGetPrivate(_this)));
2649 const char *name(sel_getName(internal->GetValue()));
2653 return CYCastJSValue(context, CYJSString([NSString stringWithFormat:@"@selector(%s)", name]));
2658 static JSValueRef Selector_callAsFunction_type(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
2661 @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"incorrect number of arguments to Selector.type" userInfo:nil];
2663 Selector_privateData *internal(reinterpret_cast<Selector_privateData *>(JSObjectGetPrivate(_this)));
2664 Class _class(CYCastNSObject(pool, context, arguments[0]));
2665 SEL sel(internal->GetValue());
2666 if (Method method = class_getInstanceMethod(_class, sel))
2667 return CYCastJSValue(context, method_getTypeEncoding(method));
2668 else if (NSString *type = [[Bridge_ objectAtIndex:1] objectForKey:CYCastNSString(pool, sel_getName(sel))])
2669 return CYCastJSValue(context, CYJSString(type));
2671 return CYJSNull(context);
2675 static JSValueRef Type_callAsFunction_toString(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
2677 Type_privateData *internal(reinterpret_cast<Type_privateData *>(JSObjectGetPrivate(_this)));
2679 const char *type(sig::Unparse(pool, internal->type_));
2681 return CYCastJSValue(context, CYJSString(type));
2686 static JSValueRef Type_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
2688 Type_privateData *internal(reinterpret_cast<Type_privateData *>(JSObjectGetPrivate(_this)));
2690 const char *type(sig::Unparse(pool, internal->type_));
2692 return CYCastJSValue(context, CYJSString([NSString stringWithFormat:@"new Type(%@)", [[NSString stringWithUTF8String:type] cy$toCYON]]));
2697 static JSValueRef Type_callAsFunction_toJSON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
2698 return Type_callAsFunction_toString(context, object, _this, count, arguments, exception);
2701 static JSStaticValue CYValue_staticValues[2] = {
2702 {"value", &CYValue_getProperty_value, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete},
2703 {NULL, NULL, NULL, 0}
2706 static JSStaticValue Pointer_staticValues[2] = {
2707 {"$cyi", &Pointer_getProperty_$cyi, &Pointer_setProperty_$cyi, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2708 {NULL, NULL, NULL, 0}
2711 static JSStaticFunction Pointer_staticFunctions[4] = {
2712 {"toCYON", &CYValue_callAsFunction_toCYON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2713 {"toJSON", &CYValue_callAsFunction_toJSON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2714 {"valueOf", &CYValue_callAsFunction_valueOf, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2718 static JSStaticFunction Struct_staticFunctions[2] = {
2719 {"$cya", &Struct_callAsFunction_$cya, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2723 static JSStaticFunction Functor_staticFunctions[4] = {
2724 {"toCYON", &CYValue_callAsFunction_toCYON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2725 {"toJSON", &CYValue_callAsFunction_toJSON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2726 {"valueOf", &CYValue_callAsFunction_valueOf, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2730 /*static JSStaticValue Selector_staticValues[2] = {
2731 {"prototype", &Selector_getProperty_prototype, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete},
2732 {NULL, NULL, NULL, 0}
2735 static JSStaticValue Instance_staticValues[2] = {
2736 {"value", &CYValue_getProperty_value, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete},
2737 {NULL, NULL, NULL, 0}
2740 static JSStaticFunction Instance_staticFunctions[5] = {
2741 {"$cya", &CYValue_callAsFunction_$cya, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2742 {"toCYON", &Instance_callAsFunction_toCYON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2743 {"toJSON", &Instance_callAsFunction_toJSON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2744 {"toString", &Instance_callAsFunction_toString, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2748 static JSStaticFunction Internal_staticFunctions[2] = {
2749 {"$cya", &Internal_callAsFunction_$cya, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2753 static JSStaticFunction Selector_staticFunctions[5] = {
2754 {"toCYON", &Selector_callAsFunction_toCYON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2755 {"toJSON", &Selector_callAsFunction_toJSON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2756 {"toString", &Selector_callAsFunction_toString, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2757 {"type", &Selector_callAsFunction_type, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2761 static JSStaticFunction Type_staticFunctions[4] = {
2762 {"toCYON", &Type_callAsFunction_toCYON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2763 {"toJSON", &Type_callAsFunction_toJSON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2764 {"toString", &Type_callAsFunction_toString, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2768 CYDriver::CYDriver(const std::string &filename) :
2773 filename_(filename),
2779 CYDriver::~CYDriver() {
2783 void cy::parser::error(const cy::parser::location_type &location, const std::string &message) {
2784 CYDriver::Error error;
2785 error.location_ = location;
2786 error.message_ = message;
2787 driver.errors_.push_back(error);
2790 void CYSetArgs(int argc, const char *argv[]) {
2791 JSContextRef context(CYGetJSContext());
2792 JSValueRef args[argc];
2793 for (int i(0); i != argc; ++i)
2794 args[i] = CYCastJSValue(context, argv[i]);
2795 JSValueRef exception(NULL);
2796 JSObjectRef array(JSObjectMakeArray(context, argc, args, &exception));
2797 CYThrow(context, exception);
2798 CYSetProperty(context, System_, CYJSString("args"), array);
2801 JSObjectRef CYGetGlobalObject(JSContextRef context) {
2802 return JSContextGetGlobalObject(context);
2805 const char *CYExecute(apr_pool_t *pool, const char *code) { _pooled
2806 JSContextRef context(CYGetJSContext());
2807 JSValueRef exception(NULL), result;
2810 result = JSEvaluateScript(context, CYJSString(code), NULL, NULL, 0, &exception);
2811 } catch (const char *error) {
2815 if (exception != NULL) { error:
2820 if (JSValueIsUndefined(context, result))
2823 const char *json(CYPoolCCYON(pool, context, result, &exception));
2824 if (exception != NULL)
2827 CYSetProperty(context, CYGetGlobalObject(context), Result_, result);
2831 bool CYRecvAll_(int socket, uint8_t *data, size_t size) {
2832 while (size != 0) if (size_t writ = _syscall(recv(socket, data, size, 0))) {
2840 bool CYSendAll_(int socket, const uint8_t *data, size_t size) {
2841 while (size != 0) if (size_t writ = _syscall(send(socket, data, size, 0))) {
2853 const char * volatile data_;
2856 // XXX: this is "tre lame"
2857 @interface CYClient_ : NSObject {
2860 - (void) execute:(NSValue *)value;
2864 @implementation CYClient_
2866 - (void) execute:(NSValue *)value {
2867 CYExecute_ *execute(reinterpret_cast<CYExecute_ *>([value pointerValue]));
2868 const char *data(execute->data_);
2869 execute->data_ = NULL;
2870 execute->data_ = CYExecute(execute->pool_, data);
2879 apr_thread_t *thread_;
2881 CYClient(int socket) :
2887 _syscall(close(socket_));
2890 void Handle() { _pooled
2891 CYClient_ *client = [[[CYClient_ alloc] init] autorelease];
2895 if (!CYRecvAll(socket_, &size, sizeof(size)))
2899 char *data(new(pool) char[size + 1]);
2900 if (!CYRecvAll(socket_, data, size))
2904 CYDriver driver("");
2905 cy::parser parser(driver);
2907 driver.data_ = data;
2908 driver.size_ = size;
2911 if (parser.parse() != 0 || !driver.errors_.empty()) {
2913 size = _not(size_t);
2915 std::ostringstream str;
2916 driver.source_->Show(str);
2917 std::string code(str.str());
2918 CYExecute_ execute = {pool, code.c_str()};
2919 [client performSelectorOnMainThread:@selector(execute:) withObject:[NSValue valueWithPointer:&execute] waitUntilDone:YES];
2920 json = execute.data_;
2921 size = json == NULL ? _not(size_t) : strlen(json);
2924 if (!CYSendAll(socket_, &size, sizeof(size)))
2927 if (!CYSendAll(socket_, json, size))
2933 static void * APR_THREAD_FUNC OnClient(apr_thread_t *thread, void *data) {
2934 CYClient *client(reinterpret_cast<CYClient *>(data));
2940 extern "C" void CYHandleClient(apr_pool_t *pool, int socket) {
2941 CYClient *client(new(pool) CYClient(socket));
2942 apr_threadattr_t *attr;
2943 _aprcall(apr_threadattr_create(&attr, client->pool_));
2944 _aprcall(apr_thread_create(&client->thread_, attr, &OnClient, client, client->pool_));
2947 MSInitialize { _pooled
2948 _aprcall(apr_initialize());
2949 _aprcall(apr_pool_create(&Pool_, NULL));
2951 Type_privateData::Object = new(Pool_) Type_privateData(Pool_, "@");
2952 Type_privateData::Selector = new(Pool_) Type_privateData(Pool_, ":");
2954 Bridge_ = [[NSMutableArray arrayWithContentsOfFile:@"/usr/lib/libcycript.plist"] retain];
2956 NSCFBoolean_ = objc_getClass("NSCFBoolean");
2957 NSCFType_ = objc_getClass("NSCFType");
2958 NSMessageBuilder_ = objc_getClass("NSMessageBuilder");
2959 NSZombie_ = objc_getClass("_NSZombie_");
2960 Object_ = objc_getClass("Object");
2963 JSGlobalContextRef CYGetJSContext() {
2964 if (Context_ == NULL) {
2965 JSClassDefinition definition;
2967 definition = kJSClassDefinitionEmpty;
2968 definition.className = "Functor";
2969 definition.staticFunctions = Functor_staticFunctions;
2970 definition.callAsFunction = &Functor_callAsFunction;
2971 definition.finalize = &Finalize;
2972 Functor_ = JSClassCreate(&definition);
2974 definition = kJSClassDefinitionEmpty;
2975 definition.className = "Instance";
2976 definition.staticValues = Instance_staticValues;
2977 definition.staticFunctions = Instance_staticFunctions;
2978 definition.hasProperty = &Instance_hasProperty;
2979 definition.getProperty = &Instance_getProperty;
2980 definition.setProperty = &Instance_setProperty;
2981 definition.deleteProperty = &Instance_deleteProperty;
2982 definition.getPropertyNames = &Instance_getPropertyNames;
2983 definition.callAsConstructor = &Instance_callAsConstructor;
2984 definition.finalize = &Finalize;
2985 Instance_ = JSClassCreate(&definition);
2987 definition = kJSClassDefinitionEmpty;
2988 definition.className = "Internal";
2989 definition.staticFunctions = Internal_staticFunctions;
2990 definition.hasProperty = &Internal_hasProperty;
2991 definition.getProperty = &Internal_getProperty;
2992 definition.setProperty = &Internal_setProperty;
2993 definition.getPropertyNames = &Internal_getPropertyNames;
2994 definition.finalize = &Finalize;
2995 Internal_ = JSClassCreate(&definition);
2997 definition = kJSClassDefinitionEmpty;
2998 definition.className = "Pointer";
2999 definition.staticValues = Pointer_staticValues;
3000 definition.staticFunctions = Pointer_staticFunctions;
3001 definition.getProperty = &Pointer_getProperty;
3002 definition.setProperty = &Pointer_setProperty;
3003 definition.finalize = &Finalize;
3004 Pointer_ = JSClassCreate(&definition);
3006 definition = kJSClassDefinitionEmpty;
3007 definition.className = "Selector";
3008 definition.staticValues = CYValue_staticValues;
3009 //definition.staticValues = Selector_staticValues;
3010 definition.staticFunctions = Selector_staticFunctions;
3011 definition.callAsFunction = &Selector_callAsFunction;
3012 definition.finalize = &Finalize;
3013 Selector_ = JSClassCreate(&definition);
3015 definition = kJSClassDefinitionEmpty;
3016 definition.className = "Struct";
3017 definition.staticFunctions = Struct_staticFunctions;
3018 definition.getProperty = &Struct_getProperty;
3019 definition.setProperty = &Struct_setProperty;
3020 definition.getPropertyNames = &Struct_getPropertyNames;
3021 definition.finalize = &Finalize;
3022 Struct_ = JSClassCreate(&definition);
3024 definition = kJSClassDefinitionEmpty;
3025 definition.className = "Type";
3026 definition.staticFunctions = Type_staticFunctions;
3027 //definition.getProperty = &Type_getProperty;
3028 definition.callAsFunction = &Type_callAsFunction;
3029 definition.callAsConstructor = &Type_callAsConstructor;
3030 definition.finalize = &Finalize;
3031 Type_ = JSClassCreate(&definition);
3033 definition = kJSClassDefinitionEmpty;
3034 definition.className = "Runtime";
3035 definition.getProperty = &Runtime_getProperty;
3036 Runtime_ = JSClassCreate(&definition);
3038 definition = kJSClassDefinitionEmpty;
3039 definition.className = "ObjectiveC::Classes";
3040 definition.getProperty = &ObjectiveC_Classes_getProperty;
3041 definition.getPropertyNames = &ObjectiveC_Classes_getPropertyNames;
3042 ObjectiveC_Classes_ = JSClassCreate(&definition);
3044 definition = kJSClassDefinitionEmpty;
3045 definition.className = "ObjectiveC::Images";
3046 definition.getProperty = &ObjectiveC_Images_getProperty;
3047 definition.getPropertyNames = &ObjectiveC_Images_getPropertyNames;
3048 ObjectiveC_Images_ = JSClassCreate(&definition);
3050 definition = kJSClassDefinitionEmpty;
3051 definition.className = "ObjectiveC::Image::Classes";
3052 definition.getProperty = &ObjectiveC_Image_Classes_getProperty;
3053 definition.getPropertyNames = &ObjectiveC_Image_Classes_getPropertyNames;
3054 definition.finalize = &Finalize;
3055 ObjectiveC_Image_Classes_ = JSClassCreate(&definition);
3057 definition = kJSClassDefinitionEmpty;
3058 definition.className = "ObjectiveC::Protocols";
3059 definition.getProperty = &ObjectiveC_Protocols_getProperty;
3060 definition.getPropertyNames = &ObjectiveC_Protocols_getPropertyNames;
3061 ObjectiveC_Protocols_ = JSClassCreate(&definition);
3063 definition = kJSClassDefinitionEmpty;
3064 //definition.getProperty = &Global_getProperty;
3065 JSClassRef Global(JSClassCreate(&definition));
3067 JSGlobalContextRef context(JSGlobalContextCreate(Global));
3070 JSObjectRef global(CYGetGlobalObject(context));
3072 JSObjectSetPrototype(context, global, JSObjectMake(context, Runtime_, NULL));
3073 ObjectiveC_ = JSObjectMake(context, NULL, NULL);
3074 CYSetProperty(context, global, CYJSString("ObjectiveC"), ObjectiveC_);
3076 CYSetProperty(context, ObjectiveC_, CYJSString("classes"), JSObjectMake(context, ObjectiveC_Classes_, NULL));
3077 CYSetProperty(context, ObjectiveC_, CYJSString("images"), JSObjectMake(context, ObjectiveC_Images_, NULL));
3078 CYSetProperty(context, ObjectiveC_, CYJSString("protocols"), JSObjectMake(context, ObjectiveC_Protocols_, NULL));
3080 CYSetProperty(context, global, CYJSString("Functor"), JSObjectMakeConstructor(context, Functor_, &Functor_new));
3081 CYSetProperty(context, global, CYJSString("Instance"), JSObjectMakeConstructor(context, Instance_, &Instance_new));
3082 CYSetProperty(context, global, CYJSString("Pointer"), JSObjectMakeConstructor(context, Pointer_, &Pointer_new));
3083 CYSetProperty(context, global, CYJSString("Selector"), JSObjectMakeConstructor(context, Selector_, &Selector_new));
3084 CYSetProperty(context, global, CYJSString("Type"), JSObjectMakeConstructor(context, Type_, &Type_new));
3086 MSHookFunction(&objc_registerClassPair, MSHake(objc_registerClassPair));
3088 class_addMethod(NSCFType_, @selector(cy$toJSON:), reinterpret_cast<IMP>(&NSCFType$cy$toJSON), "@12@0:4@8");
3090 CYSetProperty(context, global, CYJSString("objc_registerClassPair"), JSObjectMakeFunctionWithCallback(context, CYJSString("objc_registerClassPair"), &objc_registerClassPair_));
3091 CYSetProperty(context, global, CYJSString("objc_msgSend"), JSObjectMakeFunctionWithCallback(context, CYJSString("objc_msgSend"), &$objc_msgSend));
3093 System_ = JSObjectMake(context, NULL, NULL);
3094 CYSetProperty(context, global, CYJSString("system"), System_);
3095 CYSetProperty(context, System_, CYJSString("args"), CYJSNull(context));
3096 //CYSetProperty(context, System_, CYJSString("global"), global);
3098 CYSetProperty(context, System_, CYJSString("print"), JSObjectMakeFunctionWithCallback(context, CYJSString("print"), &System_print));
3100 Result_ = JSStringCreateWithUTF8CString("_");
3102 length_ = JSStringCreateWithUTF8CString("length");
3103 message_ = JSStringCreateWithUTF8CString("message");
3104 name_ = JSStringCreateWithUTF8CString("name");
3105 toCYON_ = JSStringCreateWithUTF8CString("toCYON");
3106 toJSON_ = JSStringCreateWithUTF8CString("toJSON");
3108 Array_ = CYCastJSObject(context, CYGetProperty(context, global, CYJSString("Array")));
3109 Function_ = CYCastJSObject(context, CYGetProperty(context, global, CYJSString("Function")));