X-Git-Url: https://git.saurik.com/cycript.git/blobdiff_plain/1431b560bcd021cc70299381040a1ead1cb11fbd..faf692074675c34e376b22384780ae0f97aa9ce4:/Library.mm diff --git a/Library.mm b/Library.mm index bb424d4..6537ffd 100644 --- a/Library.mm +++ b/Library.mm @@ -1,4 +1,4 @@ -/* Cyrker - Remove Execution Server and Disassembler +/* Cycript - Remove Execution Server and Disassembler * Copyright (C) 2009 Jay Freeman (saurik) */ @@ -40,37 +40,33 @@ #define _GNU_SOURCE #include -#include "Struct.hpp" +#include "cycript.hpp" #include "sig/parse.hpp" #include "sig/ffi_type.hpp" -#include -#include - -#include +#include "Pooling.hpp" +#include "Struct.hpp" #include #include -#include -#include - -#include -#include -#include -#include -#include -#include - #include -#include -#include -#include +#include #include #include +#include +#include + +#include +#include + +#include "Parser.hpp" +#include "Cycript.tab.hh" + +#include #undef _assert #undef _trace @@ -84,843 +80,2565 @@ CFLog(kCFLogLevelNotice, CFSTR("_trace():%u"), __LINE__); \ } while (false) -/* Objective-C Handle<> {{{ */ -template -class _H { - typedef _H This_; +#define CYPoolTry { \ + id _saved(nil); \ + NSAutoreleasePool *_pool([[NSAutoreleasePool alloc] init]); \ + @try +#define CYPoolCatch(value) \ + @catch (NSException *error) { \ + _saved = [error retain]; \ + @throw; \ + return value; \ + } @finally { \ + [_pool release]; \ + if (_saved != nil) \ + [_saved autorelease]; \ + } \ +} + +static JSGlobalContextRef Context_; +static JSObjectRef System_; +static JSObjectRef ObjectiveC_; - private: - Type_ *value_; +static JSClassRef Functor_; +static JSClassRef Instance_; +static JSClassRef Internal_; +static JSClassRef Message_; +static JSClassRef Pointer_; +static JSClassRef Prototype_; +static JSClassRef Runtime_; +static JSClassRef Selector_; +static JSClassRef Struct_; +static JSClassRef Type_; - _finline void Retain_() { - if (value_ != nil) - [value_ retain]; - } +static JSClassRef ObjectiveC_Classes_; +static JSClassRef ObjectiveC_Image_Classes_; +static JSClassRef ObjectiveC_Images_; +static JSClassRef ObjectiveC_Protocols_; + +static JSObjectRef Array_; +static JSObjectRef Function_; + +static JSStringRef Result_; + +static JSStringRef length_; +static JSStringRef message_; +static JSStringRef name_; +static JSStringRef prototype_; +static JSStringRef toCYON_; +static JSStringRef toJSON_; + +static JSObjectRef Array_prototype_; +static JSObjectRef Array_pop_; +static JSObjectRef Array_push_; +static JSObjectRef Array_splice_; + +static Class NSArray_; +static Class NSCFBoolean_; +static Class NSCFType_; +static Class NSMessageBuilder_; +static Class NSZombie_; +static Class Object_; + +static NSArray *Bridge_; + +static void Finalize(JSObjectRef object) { + delete reinterpret_cast(JSObjectGetPrivate(object)); +} - _finline void Clear_() { - if (value_ != nil) - [value_ release]; +class Type_privateData; + +struct CYValue : + CYData +{ + void *value_; + + CYValue() { } - public: - _finline _H(const This_ &rhs) : - value_(rhs.value_ == nil ? nil : [rhs.value_ retain]) + CYValue(void *value) : + value_(value) { } - _finline _H(Type_ *value = NULL, bool mended = false) : - value_(value) + CYValue(const CYValue &rhs) : + value_(rhs.value_) { - if (!mended) - Retain_(); } - _finline ~_H() { - Clear_(); + virtual Type_privateData *GetType() const { + return NULL; } +}; - _finline operator Type_ *() const { - return value_; +struct Selector_privateData : + CYValue +{ + Selector_privateData(SEL value) : + CYValue(value) + { } - _finline This_ &operator =(Type_ *value) { - if (value_ != value) { - Type_ *old(value_); - value_ = value; - Retain_(); - if (old != nil) - [old release]; - } return *this; + SEL GetValue() const { + return reinterpret_cast(value_); } + + virtual Type_privateData *GetType() const; }; -/* }}} */ -/* APR Pool Helpers {{{ */ -void *operator new(size_t size, apr_pool_t *pool) { - return apr_palloc(pool, size); -} -void *operator new [](size_t size, apr_pool_t *pool) { - return apr_palloc(pool, size); -} +struct Instance : + CYValue +{ + enum Flags { + None = 0, + Transient = (1 << 0), + Uninitialized = (1 << 1), + }; -class CYPool { - private: - apr_pool_t *pool_; + Flags flags_; - public: - CYPool() { - apr_pool_create(&pool_, NULL); + Instance(id value, Flags flags) : + CYValue(value), + flags_(flags) + { } - ~CYPool() { - apr_pool_destroy(pool_); + virtual ~Instance() { + if ((flags_ & Transient) == 0) + // XXX: does this handle background threads correctly? + [GetValue() performSelector:@selector(release) withObject:nil afterDelay:0]; } - operator apr_pool_t *() const { - return pool_; + static JSObjectRef Make(JSContextRef context, id object, Flags flags = None) { + JSObjectRef value(JSObjectMake(context, Instance_, new Instance(object, flags))); + if (object != nil) + for (Class _class(object_getClass(object)); _class != nil; _class = class_getSuperclass(_class)) + if (_class == NSArray_) { + JSObjectSetPrototype(context, value, Array_prototype_); + break; + } + return value; } -}; -/* }}} */ -#define _pooled _H _pool([[NSAutoreleasePool alloc] init], true); - -static JSContextRef Context_; - -static JSClassRef Functor_; -static JSClassRef Instance_; -static JSClassRef Pointer_; -static JSClassRef Selector_; + id GetValue() const { + return reinterpret_cast(value_); + } -static JSObjectRef Array_; + bool IsUninitialized() const { + return (flags_ & Uninitialized) != 0; + } -static JSStringRef name_; -static JSStringRef message_; -static JSStringRef length_; + virtual Type_privateData *GetType() const; +}; -static Class NSCFBoolean_; +struct Prototype : + CYValue +{ + Prototype(Class value) : + CYValue(value) + { + } -static NSMutableDictionary *Bridge_; + static JSObjectRef Make(JSContextRef context, Class _class, bool array = false) { + JSObjectRef value(JSObjectMake(context, Prototype_, new Prototype(_class))); + if (_class == NSArray_) + array = true; + if (Class super = class_getSuperclass(_class)) + JSObjectSetPrototype(context, value, Prototype::Make(context, super, array)); + /*else if (array) + JSObjectSetPrototype(context, value, Array_prototype_);*/ + return value; + } -struct Client { - CFHTTPMessageRef message_; - CFSocketRef socket_; + Class GetValue() const { + return reinterpret_cast(value_); + } }; -JSObjectRef CYMakeObject(JSContextRef context, id object) { - return JSObjectMake(context, Instance_, [object retain]); -} +struct Internal : + CYValue +{ + JSObjectRef owner_; -@interface NSMethodSignature (Cycript) -- (NSString *) _typeString; -@end + Internal(id value, JSObjectRef owner) : + CYValue(value), + owner_(owner) + { + } -@interface NSObject (Cycript) -- (NSString *) cy$toJSON; -- (JSValueRef) cy$JSValueInContext:(JSContextRef)context; -@end + static JSObjectRef Make(JSContextRef context, id object, JSObjectRef owner) { + return JSObjectMake(context, Internal_, new Internal(object, owner)); + } -@interface NSString (Cycript) -- (void *) cy$symbol; -@end + id GetValue() const { + return reinterpret_cast(value_); + } +}; -@interface NSNumber (Cycript) -- (void *) cy$symbol; -@end +namespace sig { -@implementation NSObject (Cycript) +void Copy(apr_pool_t *pool, Type &lhs, Type &rhs); -- (NSString *) cy$toJSON { - return [self description]; +void Copy(apr_pool_t *pool, Element &lhs, Element &rhs) { + lhs.name = apr_pstrdup(pool, rhs.name); + if (rhs.type == NULL) + lhs.type = NULL; + else { + lhs.type = new(pool) Type; + Copy(pool, *lhs.type, *rhs.type); + } + lhs.offset = rhs.offset; } -- (JSValueRef) cy$JSValueInContext:(JSContextRef)context { - return CYMakeObject(context, self); +void Copy(apr_pool_t *pool, Signature &lhs, Signature &rhs) { + size_t count(rhs.count); + lhs.count = count; + lhs.elements = new(pool) Element[count]; + for (size_t index(0); index != count; ++index) + Copy(pool, lhs.elements[index], rhs.elements[index]); } -@end +void Copy(apr_pool_t *pool, Type &lhs, Type &rhs) { + lhs.primitive = rhs.primitive; + lhs.name = apr_pstrdup(pool, rhs.name); + lhs.flags = rhs.flags; -@implementation WebUndefined (Cycript) + if (sig::IsAggregate(rhs.primitive)) + Copy(pool, lhs.data.signature, rhs.data.signature); + else { + if (rhs.data.data.type != NULL) { + lhs.data.data.type = new(pool) Type; + Copy(pool, *lhs.data.data.type, *rhs.data.data.type); + } -- (NSString *) cy$toJSON { - return @"undefined"; + lhs.data.data.size = rhs.data.data.size; + } } -- (JSValueRef) cy$JSValueInContext:(JSContextRef)context { - return JSValueMakeUndefined(context); +void Copy(apr_pool_t *pool, ffi_type &lhs, ffi_type &rhs) { + lhs.size = rhs.size; + lhs.alignment = rhs.alignment; + lhs.type = rhs.type; + if (rhs.elements == NULL) + lhs.elements = NULL; + else { + size_t count(0); + while (rhs.elements[count] != NULL) + ++count; + + lhs.elements = new(pool) ffi_type *[count + 1]; + lhs.elements[count] = NULL; + + for (size_t index(0); index != count; ++index) { + // XXX: if these are libffi native then you can just take them + ffi_type *ffi(new(pool) ffi_type); + lhs.elements[index] = ffi; + sig::Copy(pool, *ffi, *rhs.elements[index]); + } + } } -@end +} -@implementation NSArray (Cycript) +struct CStringMapLess : + std::binary_function +{ + _finline bool operator ()(const char *lhs, const char *rhs) const { + return strcmp(lhs, rhs) < 0; + } +}; -- (NSString *) cy$toJSON { - NSMutableString *json([[[NSMutableString alloc] init] autorelease]); - [json appendString:@"["]; +void Structor_(apr_pool_t *pool, const char *name, const char *types, sig::Type *&type) { + if (name == NULL) + return; - bool comma(false); - for (id object in self) { - if (comma) - [json appendString:@","]; - else - comma = true; - [json appendString:[object cy$toJSON]]; - } + CYPoolTry { + if (NSMutableArray *entry = [[Bridge_ objectAtIndex:2] objectForKey:[NSString stringWithUTF8String:name]]) + switch ([[entry objectAtIndex:0] intValue]) { + case 0: { + sig::Parse(pool, &type->data.signature, [[entry objectAtIndex:1] UTF8String], &Structor_); + } break; - [json appendString:@"]"]; - return json; + case 1: { + sig::Signature signature; + sig::Parse(pool, &signature, [[entry objectAtIndex:1] UTF8String], &Structor_); + type = signature.elements[0].type; + } break; + } + } CYPoolCatch() } -@end - -@implementation NSDictionary (Cycript) +struct Type_privateData : + CYData +{ + static Type_privateData *Object; + static Type_privateData *Selector; -- (NSString *) cy$toJSON { - NSMutableString *json([[[NSMutableString alloc] init] autorelease]); - [json appendString:@"("]; - [json appendString:@"{"]; + ffi_type *ffi_; + sig::Type *type_; - bool comma(false); - for (id key in self) { - if (comma) - [json appendString:@","]; - else - comma = true; - [json appendString:[key cy$toJSON]]; - [json appendString:@":"]; - NSObject *object([self objectForKey:key]); - [json appendString:[object cy$toJSON]]; + void Set(sig::Type *type) { + type_ = new(pool_) sig::Type; + sig::Copy(pool_, *type_, *type); } - [json appendString:@"})"]; - return json; -} - -@end + Type_privateData(apr_pool_t *pool, const char *type) : + ffi_(NULL) + { + if (pool != NULL) + pool_ = pool; -@implementation NSNumber (Cycript) + sig::Signature signature; + sig::Parse(pool_, &signature, type, &Structor_); + type_ = signature.elements[0].type; + } -- (NSString *) cy$toJSON { - return [self class] != NSCFBoolean_ ? [self stringValue] : [self boolValue] ? @"true" : @"false"; -} + Type_privateData(sig::Type *type) : + ffi_(NULL) + { + if (type != NULL) + Set(type); + } -- (JSValueRef) cy$JSValueInContext:(JSContextRef)context { - return [self class] != NSCFBoolean_ ? JSValueMakeNumber(context, [self doubleValue]) : JSValueMakeBoolean(context, [self boolValue]); -} + Type_privateData(sig::Type *type, ffi_type *ffi) { + ffi_ = new(pool_) ffi_type; + sig::Copy(pool_, *ffi_, *ffi); + Set(type); + } -- (void *) cy$symbol { - return [self pointerValue]; -} + ffi_type *GetFFI() { + if (ffi_ == NULL) { + ffi_ = new(pool_) ffi_type; -@end + sig::Element element; + element.name = NULL; + element.type = type_; + element.offset = 0; -@implementation NSString (Cycript) + sig::Signature signature; + signature.elements = &element; + signature.count = 1; -- (NSString *) cy$toJSON { - CFMutableStringRef json(CFStringCreateMutableCopy(kCFAllocatorDefault, 0, (CFStringRef) self)); + ffi_cif cif; + sig::sig_ffi_cif(pool_, &sig::ObjectiveC, &signature, &cif); + *ffi_ = *cif.rtype; + } - CFStringFindAndReplace(json, CFSTR("\\"), CFSTR("\\\\"), CFRangeMake(0, CFStringGetLength(json)), 0); - CFStringFindAndReplace(json, CFSTR("\""), CFSTR("\\\""), CFRangeMake(0, CFStringGetLength(json)), 0); - CFStringFindAndReplace(json, CFSTR("\t"), CFSTR("\\t"), CFRangeMake(0, CFStringGetLength(json)), 0); - CFStringFindAndReplace(json, CFSTR("\r"), CFSTR("\\r"), CFRangeMake(0, CFStringGetLength(json)), 0); - CFStringFindAndReplace(json, CFSTR("\n"), CFSTR("\\n"), CFRangeMake(0, CFStringGetLength(json)), 0); + return ffi_; + } +}; - CFStringInsert(json, 0, CFSTR("\"")); - CFStringAppend(json, CFSTR("\"")); +Type_privateData *Type_privateData::Object; +Type_privateData *Type_privateData::Selector; - return [reinterpret_cast(json) autorelease]; +Type_privateData *Instance::GetType() const { + return Type_privateData::Object; } -- (void *) cy$symbol { - return dlsym(RTLD_DEFAULT, [self UTF8String]); +Type_privateData *Selector_privateData::GetType() const { + return Type_privateData::Selector; } -@end +struct Pointer : + CYValue +{ + JSObjectRef owner_; + Type_privateData *type_; -@interface CYJSObject : NSDictionary { - JSObjectRef object_; - JSContextRef context_; -} + Pointer(void *value, sig::Type *type, JSObjectRef owner) : + CYValue(value), + owner_(owner), + type_(new(pool_) Type_privateData(type)) + { + } +}; -- (id) initWithJSObject:(JSObjectRef)object inContext:(JSContextRef)context; +struct Struct_privateData : + CYValue +{ + JSObjectRef owner_; + Type_privateData *type_; -- (NSUInteger) count; -- (id) objectForKey:(id)key; -- (NSEnumerator *) keyEnumerator; -- (void) setObject:(id)object forKey:(id)key; -- (void) removeObjectForKey:(id)key; + Struct_privateData(JSObjectRef owner) : + owner_(owner) + { + } +}; -@end +typedef std::map TypeMap; +static TypeMap Types_; + +JSObjectRef CYMakeStruct(JSContextRef context, void *data, sig::Type *type, ffi_type *ffi, JSObjectRef owner) { + Struct_privateData *internal(new Struct_privateData(owner)); + apr_pool_t *pool(internal->pool_); + Type_privateData *typical(new(pool) Type_privateData(type, ffi)); + internal->type_ = typical; + + if (owner != NULL) + internal->value_ = data; + else { + size_t size(typical->GetFFI()->size); + void *copy(apr_palloc(internal->pool_, size)); + memcpy(copy, data, size); + internal->value_ = copy; + } -@interface CYJSArray : NSArray { - JSObjectRef object_; - JSContextRef context_; + return JSObjectMake(context, Struct_, internal); } -- (id) initWithJSObject:(JSObjectRef)object inContext:(JSContextRef)context; +struct Functor_privateData : + CYValue +{ + sig::Signature signature_; + ffi_cif cif_; -- (NSUInteger) count; -- (id) objectAtIndex:(NSUInteger)index; -@end + Functor_privateData(const char *type, void (*value)()) : + CYValue(reinterpret_cast(value)) + { + sig::Parse(pool_, &signature_, type, &Structor_); + sig::sig_ffi_cif(pool_, &sig::ObjectiveC, &signature_, &cif_); + } -JSContextRef JSGetContext() { - return Context_; -} + void (*GetValue())() const { + return reinterpret_cast(value_); + } +}; -#define CYCatch \ - @catch (id error) { \ - CYThrow(context, error, exception); \ - return NULL; \ +struct Closure_privateData : + Functor_privateData +{ + JSContextRef context_; + JSObjectRef function_; + + Closure_privateData(const char *type) : + Functor_privateData(type, NULL) + { } +}; -void CYThrow(JSContextRef context, JSValueRef value); +struct Message_privateData : + Functor_privateData +{ + SEL sel_; -id CYCastNSObject(JSContextRef context, JSObjectRef object) { - if (JSValueIsObjectOfClass(context, object, Instance_)) - return reinterpret_cast(JSObjectGetPrivate(object)); - JSValueRef exception(NULL); - bool array(JSValueIsInstanceOfConstructor(context, object, Array_, &exception)); - CYThrow(context, exception); - if (array) - return [[[CYJSArray alloc] initWithJSObject:object inContext:context] autorelease]; - return [[[CYJSObject alloc] initWithJSObject:object inContext:context] autorelease]; -} + Message_privateData(SEL sel, const char *type, IMP value = NULL) : + Functor_privateData(type, reinterpret_cast(value)), + sel_(sel) + { + } +}; -JSStringRef CYCopyJSString(id value) { - return JSStringCreateWithCFString(reinterpret_cast([value description])); -} +JSObjectRef CYMakeInstance(JSContextRef context, id object, bool transient) { + Instance::Flags flags; -JSStringRef CYCopyJSString(const char *value) { - return JSStringCreateWithUTF8CString(value); -} + if (transient) + flags = Instance::Transient; + else { + flags = Instance::None; + object = [object retain]; + } -JSStringRef CYCopyJSString(JSStringRef value) { - return JSStringRetain(value); + return Instance::Make(context, object, flags); } -JSStringRef CYCopyJSString(JSContextRef context, JSValueRef value) { - JSValueRef exception(NULL); - JSStringRef string(JSValueToStringCopy(context, value, &exception)); - CYThrow(context, exception); - return string; +const char *CYPoolCString(apr_pool_t *pool, NSString *value) { + if (pool == NULL) + return [value UTF8String]; + else { + size_t size([value maximumLengthOfBytesUsingEncoding:NSUTF8StringEncoding] + 1); + char *string(new(pool) char[size]); + if (![value getCString:string maxLength:size encoding:NSUTF8StringEncoding]) + @throw [NSException exceptionWithName:NSInternalInconsistencyException reason:@"[NSString getCString:maxLength:encoding:] == NO" userInfo:nil]; + return string; + } } -// XXX: this is not a safe handle -class CYString { - private: - JSStringRef string_; - - public: - template - CYString(Arg0_ arg0) { - string_ = CYCopyJSString(arg0); - } +JSValueRef CYCastJSValue(JSContextRef context, bool value) { + return JSValueMakeBoolean(context, value); +} - template - CYString(Arg0_ arg0, Arg1_ arg1) { - string_ = CYCopyJSString(arg0, arg1); - } +JSValueRef CYCastJSValue(JSContextRef context, double value) { + return JSValueMakeNumber(context, value); +} - ~CYString() { - JSStringRelease(string_); +#define CYCastJSValue_(Type_) \ + JSValueRef CYCastJSValue(JSContextRef context, Type_ value) { \ + return JSValueMakeNumber(context, static_cast(value)); \ } - operator JSStringRef() const { - return string_; - } -}; +CYCastJSValue_(int) +CYCastJSValue_(unsigned int) +CYCastJSValue_(long int) +CYCastJSValue_(long unsigned int) +CYCastJSValue_(long long int) +CYCastJSValue_(long long unsigned int) -CFStringRef CYCopyCFString(JSStringRef value) { - return JSStringCopyCFString(kCFAllocatorDefault, value); +JSValueRef CYJSUndefined(JSContextRef context) { + return JSValueMakeUndefined(context); } -CFStringRef CYCopyCFString(JSContextRef context, JSValueRef value) { - return CYCopyCFString(CYString(context, value)); +size_t CYGetIndex(const char *value) { + if (value[0] != '0') { + char *end; + size_t index(strtoul(value, &end, 10)); + if (value + strlen(value) == end) + return index; + } else if (value[1] == '\0') + return 0; + return _not(size_t); } -double CYCastDouble(JSContextRef context, JSValueRef value) { - JSValueRef exception(NULL); - double number(JSValueToNumber(context, value, &exception)); - CYThrow(context, exception); - return number; +size_t CYGetIndex(apr_pool_t *pool, NSString *value) { + return CYGetIndex(CYPoolCString(pool, value)); } -CFNumberRef CYCopyCFNumber(JSContextRef context, JSValueRef value) { - double number(CYCastDouble(context, value)); - return CFNumberCreate(kCFAllocatorDefault, kCFNumberDoubleType, &number); +bool CYGetOffset(const char *value, ssize_t &index) { + if (value[0] != '0') { + char *end; + index = strtol(value, &end, 10); + if (value + strlen(value) == end) + return true; + } else if (value[1] == '\0') { + index = 0; + return true; + } + + return false; } -NSString *CYCastNSString(JSStringRef value) { - return [reinterpret_cast(CYCopyCFString(value)) autorelease]; +bool CYGetOffset(apr_pool_t *pool, NSString *value, ssize_t &index) { + return CYGetOffset(CYPoolCString(pool, value), index); } -CFTypeRef CYCopyCFType(JSContextRef context, JSValueRef value) { - switch (JSType type = JSValueGetType(context, value)) { - case kJSTypeUndefined: - return CFRetain([WebUndefined undefined]); - case kJSTypeNull: - return nil; - case kJSTypeBoolean: - return CFRetain(JSValueToBoolean(context, value) ? kCFBooleanTrue : kCFBooleanFalse); - case kJSTypeNumber: - return CYCopyCFNumber(context, value); - case kJSTypeString: - return CYCopyCFString(context, value); - case kJSTypeObject: - return CFRetain((CFTypeRef) CYCastNSObject(context, (JSObjectRef) value)); +NSString *CYPoolNSCYON(apr_pool_t *pool, id value); + +@interface NSMethodSignature (Cycript) +- (NSString *) _typeString; +@end + +@interface NSObject (Cycript) + +- (JSValueRef) cy$JSValueInContext:(JSContextRef)context; +- (JSType) cy$JSType; + +- (NSObject *) cy$toJSON:(NSString *)key; +- (NSString *) cy$toCYON; +- (NSString *) cy$toKey; + +- (bool) cy$hasProperty:(NSString *)name; +- (NSObject *) cy$getProperty:(NSString *)name; +- (bool) cy$setProperty:(NSString *)name to:(NSObject *)value; +- (bool) cy$deleteProperty:(NSString *)name; + +@end + +@protocol Cycript +- (JSValueRef) cy$JSValueInContext:(JSContextRef)context; +@end + +@interface NSString (Cycript) +- (void *) cy$symbol; +@end + +struct PropertyAttributes { + CYPool pool_; + + const char *name; + + const char *variable; + + const char *getter_; + const char *setter_; + + bool readonly; + bool copy; + bool retain; + bool nonatomic; + bool dynamic; + bool weak; + bool garbage; + + PropertyAttributes(objc_property_t property) : + variable(NULL), + getter_(NULL), + setter_(NULL), + readonly(false), + copy(false), + retain(false), + nonatomic(false), + dynamic(false), + weak(false), + garbage(false) + { + name = property_getName(property); + const char *attributes(property_getAttributes(property)); + + for (char *state, *token(apr_strtok(apr_pstrdup(pool_, attributes), ",", &state)); token != NULL; token = apr_strtok(NULL, ",", &state)) { + switch (*token) { + case 'R': readonly = true; break; + case 'C': copy = true; break; + case '&': retain = true; break; + case 'N': nonatomic = true; break; + case 'G': getter_ = token + 1; break; + case 'S': setter_ = token + 1; break; + case 'V': variable = token + 1; break; + } + } + + /*if (variable == NULL) { + variable = property_getName(property); + size_t size(strlen(variable)); + char *name(new(pool_) char[size + 2]); + name[0] = '_'; + memcpy(name + 1, variable, size); + name[size + 1] = '\0'; + variable = name; + }*/ + } + + const char *Getter() { + if (getter_ == NULL) + getter_ = apr_pstrdup(pool_, name); + return getter_; + } + + const char *Setter() { + if (setter_ == NULL && !readonly) { + size_t length(strlen(name)); + + char *temp(new(pool_) char[length + 5]); + temp[0] = 's'; + temp[1] = 'e'; + temp[2] = 't'; + + if (length != 0) { + temp[3] = toupper(name[0]); + memcpy(temp + 4, name + 1, length - 1); + } + + temp[length + 3] = ':'; + temp[length + 4] = '\0'; + setter_ = temp; + } + + return setter_; + } + +}; + +@implementation NSProxy (Cycript) + +- (NSObject *) cy$toJSON:(NSString *)key { + return [self description]; +} + +- (NSString *) cy$toCYON { + return [[self cy$toJSON:@""] cy$toCYON]; +} + +@end + +@implementation NSObject (Cycript) + +- (JSValueRef) cy$JSValueInContext:(JSContextRef)context { + return CYMakeInstance(context, self, false); +} + +- (JSType) cy$JSType { + return kJSTypeObject; +} + +- (NSObject *) cy$toJSON:(NSString *)key { + return [self description]; +} + +- (NSString *) cy$toCYON { + return [[self cy$toJSON:@""] cy$toCYON]; +} + +- (NSString *) cy$toKey { + return [self cy$toCYON]; +} + +- (bool) cy$hasProperty:(NSString *)name { + return false; +} + +- (NSObject *) cy$getProperty:(NSString *)name { + return nil; +} + +- (bool) cy$setProperty:(NSString *)name to:(NSObject *)value { + return false; +} + +- (bool) cy$deleteProperty:(NSString *)name { + return false; +} + +@end + +NSString *NSCFType$cy$toJSON(id self, SEL sel, NSString *key) { + return [(NSString *) CFCopyDescription((CFTypeRef) self) autorelease]; +} + +@implementation WebUndefined (Cycript) + +- (JSType) cy$JSType { + return kJSTypeUndefined; +} + +- (NSObject *) cy$toJSON:(NSString *)key { + return self; +} + +- (NSString *) cy$toCYON { + return @"undefined"; +} + +- (JSValueRef) cy$JSValueInContext:(JSContextRef)context { + return CYJSUndefined(context); +} + +@end + +@implementation NSNull (Cycript) + +- (JSType) cy$JSType { + return kJSTypeNull; +} + +- (NSObject *) cy$toJSON:(NSString *)key { + return self; +} + +- (NSString *) cy$toCYON { + return @"null"; +} + +@end + +@implementation NSArray (Cycript) + +- (NSString *) cy$toCYON { + NSMutableString *json([[[NSMutableString alloc] init] autorelease]); + [json appendString:@"["]; + + bool comma(false); + for (id object in self) { + if (comma) + [json appendString:@","]; + else + comma = true; + if (object == nil || [object cy$JSType] != kJSTypeUndefined) + [json appendString:CYPoolNSCYON(NULL, object)]; + else { + [json appendString:@","]; + comma = false; + } + } + + [json appendString:@"]"]; + return json; +} + +- (bool) cy$hasProperty:(NSString *)name { + if ([name isEqualToString:@"length"]) + return true; + + size_t index(CYGetIndex(NULL, name)); + if (index == _not(size_t) || index >= [self count]) + return [super cy$hasProperty:name]; + else + return true; +} + +- (NSObject *) cy$getProperty:(NSString *)name { + if ([name isEqualToString:@"length"]) + return [NSNumber numberWithUnsignedInteger:[self count]]; + + size_t index(CYGetIndex(NULL, name)); + if (index == _not(size_t) || index >= [self count]) + return [super cy$getProperty:name]; + else + return [self objectAtIndex:index]; +} + +@end + +@implementation NSMutableArray (Cycript) + +- (bool) cy$setProperty:(NSString *)name to:(NSObject *)value { + if ([name isEqualToString:@"length"]) { + // XXX: is this not intelligent? + NSUInteger size([(NSNumber *)value unsignedIntegerValue]); + NSUInteger count([self count]); + if (size < count) + [self removeObjectsInRange:NSMakeRange(size, count - size)]; + else if (size != count) { + WebUndefined *undefined([WebUndefined undefined]); + for (size_t i(count); i != size; ++i) + [self addObject:undefined]; + } + return true; + } + + size_t index(CYGetIndex(NULL, name)); + if (index == _not(size_t)) + return [super cy$setProperty:name to:value]; + + id object(value ?: [NSNull null]); + + size_t count([self count]); + if (index < count) + [self replaceObjectAtIndex:index withObject:object]; + else { + if (index != count) { + WebUndefined *undefined([WebUndefined undefined]); + for (size_t i(count); i != index; ++i) + [self addObject:undefined]; + } + + [self addObject:object]; + } + + return true; +} + +- (bool) cy$deleteProperty:(NSString *)name { + size_t index(CYGetIndex(NULL, name)); + if (index == _not(size_t) || index >= [self count]) + return [super cy$deleteProperty:name]; + [self replaceObjectAtIndex:index withObject:[WebUndefined undefined]]; + return true; +} + +@end + +@implementation NSDictionary (Cycript) + +- (NSString *) cy$toCYON { + NSMutableString *json([[[NSMutableString alloc] init] autorelease]); + [json appendString:@"{"]; + + bool comma(false); + for (id key in self) { + if (comma) + [json appendString:@","]; + else + comma = true; + [json appendString:[key cy$toKey]]; + [json appendString:@":"]; + NSObject *object([self objectForKey:key]); + [json appendString:CYPoolNSCYON(NULL, object)]; + } + + [json appendString:@"}"]; + return json; +} + +- (bool) cy$hasProperty:(NSString *)name { + return [self objectForKey:name] != nil; +} + +- (NSObject *) cy$getProperty:(NSString *)name { + return [self objectForKey:name]; +} + +@end + +@implementation NSMutableDictionary (Cycript) + +- (bool) cy$setProperty:(NSString *)name to:(NSObject *)value { + [self setObject:(value ?: [NSNull null]) forKey:name]; + return true; +} + +- (bool) cy$deleteProperty:(NSString *)name { + if ([self objectForKey:name] == nil) + return false; + else { + [self removeObjectForKey:name]; + return true; + } +} + +@end + +@implementation NSNumber (Cycript) + +- (JSType) cy$JSType { + // XXX: this just seems stupid + return [self class] == NSCFBoolean_ ? kJSTypeBoolean : kJSTypeNumber; +} + +- (NSObject *) cy$toJSON:(NSString *)key { + return self; +} + +- (NSString *) cy$toCYON { + return [self cy$JSType] != kJSTypeBoolean ? [self stringValue] : [self boolValue] ? @"true" : @"false"; +} + +- (JSValueRef) cy$JSValueInContext:(JSContextRef)context { + return [self cy$JSType] != kJSTypeBoolean ? CYCastJSValue(context, [self doubleValue]) : CYCastJSValue(context, [self boolValue]); +} + +@end + +@implementation NSString (Cycript) + +- (JSType) cy$JSType { + return kJSTypeString; +} + +- (NSObject *) cy$toJSON:(NSString *)key { + return self; +} + +- (NSString *) cy$toCYON { + // XXX: this should use the better code from Output.cpp + CFMutableStringRef json(CFStringCreateMutableCopy(kCFAllocatorDefault, 0, (CFStringRef) self)); + + CFStringFindAndReplace(json, CFSTR("\\"), CFSTR("\\\\"), CFRangeMake(0, CFStringGetLength(json)), 0); + CFStringFindAndReplace(json, CFSTR("\""), CFSTR("\\\""), CFRangeMake(0, CFStringGetLength(json)), 0); + CFStringFindAndReplace(json, CFSTR("\t"), CFSTR("\\t"), CFRangeMake(0, CFStringGetLength(json)), 0); + CFStringFindAndReplace(json, CFSTR("\r"), CFSTR("\\r"), CFRangeMake(0, CFStringGetLength(json)), 0); + CFStringFindAndReplace(json, CFSTR("\n"), CFSTR("\\n"), CFRangeMake(0, CFStringGetLength(json)), 0); + + CFStringInsert(json, 0, CFSTR("\"")); + CFStringAppend(json, CFSTR("\"")); + + return [reinterpret_cast(json) autorelease]; +} + +- (NSString *) cy$toKey { + const char *value([self UTF8String]); + size_t size(strlen(value)); + + if (size == 0) + goto cyon; + + if (DigitRange_[value[0]]) { + size_t index(CYGetIndex(NULL, self)); + if (index == _not(size_t)) + goto cyon; + } else { + if (!WordStartRange_[value[0]]) + goto cyon; + for (size_t i(1); i != size; ++i) + if (!WordEndRange_[value[i]]) + goto cyon; + } + + return self; + + cyon: + return [self cy$toCYON]; +} + +- (void *) cy$symbol { + CYPool pool; + return dlsym(RTLD_DEFAULT, CYPoolCString(pool, self)); +} + +@end + +@interface CYJSObject : NSMutableDictionary { + JSObjectRef object_; + JSContextRef context_; +} + +- (id) initWithJSObject:(JSObjectRef)object inContext:(JSContextRef)context; + +- (NSString *) cy$toJSON:(NSString *)key; + +- (NSUInteger) count; +- (id) objectForKey:(id)key; +- (NSEnumerator *) keyEnumerator; +- (void) setObject:(id)object forKey:(id)key; +- (void) removeObjectForKey:(id)key; + +@end + +@interface CYJSArray : NSMutableArray { + JSObjectRef object_; + JSContextRef context_; +} + +- (id) initWithJSObject:(JSObjectRef)object inContext:(JSContextRef)context; + +- (NSUInteger) count; +- (id) objectAtIndex:(NSUInteger)index; + +- (void) addObject:(id)anObject; +- (void) insertObject:(id)anObject atIndex:(NSUInteger)index; +- (void) removeLastObject; +- (void) removeObjectAtIndex:(NSUInteger)index; +- (void) replaceObjectAtIndex:(NSUInteger)index withObject:(id)anObject; + +@end + +CYRange DigitRange_ (0x3ff000000000000LLU, 0x000000000000000LLU); // 0-9 +CYRange WordStartRange_(0x000001000000000LLU, 0x7fffffe87fffffeLLU); // A-Za-z_$ +CYRange WordEndRange_ (0x3ff001000000000LLU, 0x7fffffe87fffffeLLU); // A-Za-z_$0-9 + +#define CYTry \ + @try +#define CYCatch \ + @catch (id error) { \ + CYThrow(context, error, exception); \ + return NULL; \ + } + +void CYThrow(JSContextRef context, JSValueRef value); + +apr_status_t CYPoolRelease_(void *data) { + id object(reinterpret_cast(data)); + [object release]; + return APR_SUCCESS; +} + +id CYPoolRelease(apr_pool_t *pool, id object) { + if (object == nil) + return nil; + else if (pool == NULL) + return [object autorelease]; + else { + apr_pool_cleanup_register(pool, object, &CYPoolRelease_, &apr_pool_cleanup_null); + return object; + } +} + +CFTypeRef CYPoolRelease(apr_pool_t *pool, CFTypeRef object) { + return (CFTypeRef) CYPoolRelease(pool, (id) object); +} + +id CYCastNSObject_(apr_pool_t *pool, JSContextRef context, JSObjectRef object) { + JSValueRef exception(NULL); + bool array(JSValueIsInstanceOfConstructor(context, object, Array_, &exception)); + CYThrow(context, exception); + id value(array ? [CYJSArray alloc] : [CYJSObject alloc]); + return CYPoolRelease(pool, [value initWithJSObject:object inContext:context]); +} + +id CYCastNSObject(apr_pool_t *pool, JSContextRef context, JSObjectRef object) { + if (!JSValueIsObjectOfClass(context, object, Instance_)) + return CYCastNSObject_(pool, context, object); + else { + Instance *internal(reinterpret_cast(JSObjectGetPrivate(object))); + return internal->GetValue(); + } +} + +JSStringRef CYCopyJSString(id value) { + return value == NULL ? NULL : JSStringCreateWithCFString(reinterpret_cast([value description])); +} + +JSStringRef CYCopyJSString(const char *value) { + return value == NULL ? NULL : JSStringCreateWithUTF8CString(value); +} + +JSStringRef CYCopyJSString(JSStringRef value) { + return value == NULL ? NULL : JSStringRetain(value); +} + +JSStringRef CYCopyJSString(JSContextRef context, JSValueRef value) { + if (JSValueIsNull(context, value)) + return NULL; + JSValueRef exception(NULL); + JSStringRef string(JSValueToStringCopy(context, value, &exception)); + CYThrow(context, exception); + return string; +} + +class CYJSString { + private: + JSStringRef string_; + + void Clear_() { + if (string_ != NULL) + JSStringRelease(string_); + } + + public: + CYJSString(const CYJSString &rhs) : + string_(CYCopyJSString(rhs.string_)) + { + } + + template + CYJSString(Arg0_ arg0) : + string_(CYCopyJSString(arg0)) + { + } + + template + CYJSString(Arg0_ arg0, Arg1_ arg1) : + string_(CYCopyJSString(arg0, arg1)) + { + } + + CYJSString &operator =(const CYJSString &rhs) { + Clear_(); + string_ = CYCopyJSString(rhs.string_); + return *this; + } + + ~CYJSString() { + Clear_(); + } + + void Clear() { + Clear_(); + string_ = NULL; + } + + operator JSStringRef() const { + return string_; + } +}; + +CFStringRef CYCopyCFString(JSStringRef value) { + return JSStringCopyCFString(kCFAllocatorDefault, value); +} + +CFStringRef CYCopyCFString(JSContextRef context, JSValueRef value) { + return CYCopyCFString(CYJSString(context, value)); +} + +double CYCastDouble(const char *value, size_t size) { + char *end; + double number(strtod(value, &end)); + if (end != value + size) + return NAN; + return number; +} + +double CYCastDouble(const char *value) { + return CYCastDouble(value, strlen(value)); +} + +double CYCastDouble(JSContextRef context, JSValueRef value) { + JSValueRef exception(NULL); + double number(JSValueToNumber(context, value, &exception)); + CYThrow(context, exception); + return number; +} + +CFNumberRef CYCopyCFNumber(JSContextRef context, JSValueRef value) { + double number(CYCastDouble(context, value)); + return CFNumberCreate(kCFAllocatorDefault, kCFNumberDoubleType, &number); +} + +CFStringRef CYCopyCFString(const char *value) { + return CFStringCreateWithCString(kCFAllocatorDefault, value, kCFStringEncodingUTF8); +} + +NSString *CYCastNSString(apr_pool_t *pool, const char *value) { + return (NSString *) CYPoolRelease(pool, CYCopyCFString(value)); +} + +NSString *CYCastNSString(apr_pool_t *pool, JSStringRef value) { + return (NSString *) CYPoolRelease(pool, CYCopyCFString(value)); +} + +bool CYCastBool(JSContextRef context, JSValueRef value) { + return JSValueToBoolean(context, value); +} + +CFTypeRef CYCFType(apr_pool_t *pool, JSContextRef context, JSValueRef value, bool cast) { + CFTypeRef object; + bool copy; + + switch (JSType type = JSValueGetType(context, value)) { + case kJSTypeUndefined: + object = [WebUndefined undefined]; + copy = false; + break; + + case kJSTypeNull: + return NULL; + break; + + case kJSTypeBoolean: + object = CYCastBool(context, value) ? kCFBooleanTrue : kCFBooleanFalse; + copy = false; + break; + + case kJSTypeNumber: + object = CYCopyCFNumber(context, value); + copy = true; + break; + + case kJSTypeString: + object = CYCopyCFString(context, value); + copy = true; + break; + + case kJSTypeObject: + // XXX: this might could be more efficient + object = (CFTypeRef) CYCastNSObject(pool, context, (JSObjectRef) value); + copy = false; + break; + + default: + @throw [NSException exceptionWithName:NSInternalInconsistencyException reason:[NSString stringWithFormat:@"JSValueGetType() == 0x%x", type] userInfo:nil]; + break; + } + + if (cast != copy) + return object; + else if (copy) + return CYPoolRelease(pool, object); + else + return CFRetain(object); +} + +CFTypeRef CYCastCFType(apr_pool_t *pool, JSContextRef context, JSValueRef value) { + return CYCFType(pool, context, value, true); +} + +CFTypeRef CYCopyCFType(apr_pool_t *pool, JSContextRef context, JSValueRef value) { + return CYCFType(pool, context, value, false); +} + +NSArray *CYCastNSArray(JSPropertyNameArrayRef names) { + CYPool pool; + size_t size(JSPropertyNameArrayGetCount(names)); + NSMutableArray *array([NSMutableArray arrayWithCapacity:size]); + for (size_t index(0); index != size; ++index) + [array addObject:CYCastNSString(pool, JSPropertyNameArrayGetNameAtIndex(names, index))]; + return array; +} + +id CYCastNSObject(apr_pool_t *pool, JSContextRef context, JSValueRef value) { + return reinterpret_cast(CYCastCFType(pool, context, value)); +} + +void CYThrow(JSContextRef context, JSValueRef value) { + if (value == NULL) + return; + @throw CYCastNSObject(NULL, context, value); +} + +JSValueRef CYJSNull(JSContextRef context) { + return JSValueMakeNull(context); +} + +JSValueRef CYCastJSValue(JSContextRef context, JSStringRef value) { + return value == NULL ? CYJSNull(context) : JSValueMakeString(context, value); +} + +JSValueRef CYCastJSValue(JSContextRef context, const char *value) { + return CYCastJSValue(context, CYJSString(value)); +} + +JSValueRef CYCastJSValue(JSContextRef context, id value) { + if (value == nil) + return CYJSNull(context); + else if ([value respondsToSelector:@selector(cy$JSValueInContext:)]) + return [value cy$JSValueInContext:context]; + else + return CYMakeInstance(context, value, false); +} + +JSObjectRef CYCastJSObject(JSContextRef context, JSValueRef value) { + JSValueRef exception(NULL); + JSObjectRef object(JSValueToObject(context, value, &exception)); + CYThrow(context, exception); + return object; +} + +JSValueRef CYGetProperty(JSContextRef context, JSObjectRef object, size_t index) { + JSValueRef exception(NULL); + JSValueRef value(JSObjectGetPropertyAtIndex(context, object, index, &exception)); + CYThrow(context, exception); + return value; +} + +JSValueRef CYGetProperty(JSContextRef context, JSObjectRef object, JSStringRef name) { + JSValueRef exception(NULL); + JSValueRef value(JSObjectGetProperty(context, object, name, &exception)); + CYThrow(context, exception); + return value; +} + +void CYSetProperty(JSContextRef context, JSObjectRef object, size_t index, JSValueRef value) { + JSValueRef exception(NULL); + JSObjectSetPropertyAtIndex(context, object, index, value, &exception); + CYThrow(context, exception); +} + +void CYSetProperty(JSContextRef context, JSObjectRef object, JSStringRef name, JSValueRef value) { + JSValueRef exception(NULL); + JSObjectSetProperty(context, object, name, value, kJSPropertyAttributeNone, &exception); + CYThrow(context, exception); +} + +void CYThrow(JSContextRef context, id error, JSValueRef *exception) { + if (exception == NULL) + throw error; + *exception = CYCastJSValue(context, error); +} + +JSValueRef CYCallAsFunction(JSContextRef context, JSObjectRef function, JSObjectRef _this, size_t count, JSValueRef arguments[]) { + JSValueRef exception(NULL); + JSValueRef value(JSObjectCallAsFunction(context, function, _this, count, arguments, &exception)); + CYThrow(context, exception); + return value; +} + +bool CYIsCallable(JSContextRef context, JSValueRef value) { + // XXX: this isn't actually correct + return value != NULL && JSValueIsObject(context, value); +} + +@implementation CYJSObject + +- (id) initWithJSObject:(JSObjectRef)object inContext:(JSContextRef)context { + if ((self = [super init]) != nil) { + object_ = object; + context_ = context; + } return self; +} + +- (NSObject *) cy$toJSON:(NSString *)key { + JSValueRef toJSON(CYGetProperty(context_, object_, toJSON_)); + if (!CYIsCallable(context_, toJSON)) + return [super cy$toJSON:key]; + else { + JSValueRef arguments[1] = {CYCastJSValue(context_, key)}; + JSValueRef value(CYCallAsFunction(context_, (JSObjectRef) toJSON, object_, 1, arguments)); + // XXX: do I really want an NSNull here?! + return CYCastNSObject(NULL, context_, value) ?: [NSNull null]; + } +} + +- (NSString *) cy$toCYON { + JSValueRef toCYON(CYGetProperty(context_, object_, toCYON_)); + if (!CYIsCallable(context_, toCYON)) + return [super cy$toCYON]; + else { + JSValueRef value(CYCallAsFunction(context_, (JSObjectRef) toCYON, object_, 0, NULL)); + return CYCastNSString(NULL, CYJSString(context_, value)); + } +} + +- (NSUInteger) count { + JSPropertyNameArrayRef names(JSObjectCopyPropertyNames(context_, object_)); + size_t size(JSPropertyNameArrayGetCount(names)); + JSPropertyNameArrayRelease(names); + return size; +} + +- (id) objectForKey:(id)key { + return CYCastNSObject(NULL, context_, CYGetProperty(context_, object_, CYJSString(key))) ?: [NSNull null]; +} + +- (NSEnumerator *) keyEnumerator { + JSPropertyNameArrayRef names(JSObjectCopyPropertyNames(context_, object_)); + NSEnumerator *enumerator([CYCastNSArray(names) objectEnumerator]); + JSPropertyNameArrayRelease(names); + return enumerator; +} + +- (void) setObject:(id)object forKey:(id)key { + CYSetProperty(context_, object_, CYJSString(key), CYCastJSValue(context_, object)); +} + +- (void) removeObjectForKey:(id)key { + JSValueRef exception(NULL); + (void) JSObjectDeleteProperty(context_, object_, CYJSString(key), &exception); + CYThrow(context_, exception); +} + +@end + +@implementation CYJSArray + +- (id) initWithJSObject:(JSObjectRef)object inContext:(JSContextRef)context { + if ((self = [super init]) != nil) { + object_ = object; + context_ = context; + } return self; +} + +- (NSUInteger) count { + return CYCastDouble(context_, CYGetProperty(context_, object_, length_)); +} + +- (id) objectAtIndex:(NSUInteger)index { + JSValueRef exception(NULL); + JSValueRef value(JSObjectGetPropertyAtIndex(context_, object_, index, &exception)); + CYThrow(context_, exception); + return CYCastNSObject(NULL, context_, value) ?: [NSNull null]; +} + +- (void) addObject:(id)object { + JSValueRef exception(NULL); + JSValueRef arguments[1]; + arguments[0] = CYCastJSValue(context_, object); + JSObjectCallAsFunction(context_, Array_push_, object_, 1, arguments, &exception); + CYThrow(context_, exception); +} + +- (void) insertObject:(id)object atIndex:(NSUInteger)index { + JSValueRef exception(NULL); + JSValueRef arguments[3]; + arguments[0] = CYCastJSValue(context_, index); + arguments[1] = CYCastJSValue(context_, 0); + arguments[2] = CYCastJSValue(context_, object); + JSObjectCallAsFunction(context_, Array_splice_, object_, 3, arguments, &exception); + CYThrow(context_, exception); +} + +- (void) removeLastObject { + JSValueRef exception(NULL); + JSObjectCallAsFunction(context_, Array_pop_, object_, 0, NULL, &exception); + CYThrow(context_, exception); +} + +- (void) removeObjectAtIndex:(NSUInteger)index { + JSValueRef exception(NULL); + JSValueRef arguments[2]; + arguments[0] = CYCastJSValue(context_, index); + arguments[1] = CYCastJSValue(context_, 1); + JSObjectCallAsFunction(context_, Array_splice_, object_, 2, arguments, &exception); + CYThrow(context_, exception); +} + +- (void) replaceObjectAtIndex:(NSUInteger)index withObject:(id)object { + CYSetProperty(context_, object_, index, CYCastJSValue(context_, object)); +} + +@end + +NSString *CYCopyNSCYON(id value) { + NSString *string; + + if (value == nil) + string = @"nil"; + else { + Class _class(object_getClass(value)); + SEL sel(@selector(cy$toCYON)); + + if (Method toCYON = class_getInstanceMethod(_class, sel)) + string = reinterpret_cast(method_getImplementation(toCYON))(value, sel); + else if (Method methodSignatureForSelector = class_getInstanceMethod(_class, @selector(methodSignatureForSelector:))) { + if (reinterpret_cast(method_getImplementation(methodSignatureForSelector))(value, @selector(methodSignatureForSelector:), sel) != nil) + string = [value cy$toCYON]; + else goto fail; + } else fail: { + if (value == NSZombie_) + string = @"_NSZombie_"; + else if (_class == NSZombie_) + string = [NSString stringWithFormat:@"<_NSZombie_: %p>", value]; + // XXX: frowny /in/ the pants + else if (value == NSMessageBuilder_ || value == Object_) + string = nil; + else + string = [NSString stringWithFormat:@"%@", value]; + } + + // XXX: frowny pants + if (string == nil) + string = @"undefined"; + } + + return [string retain]; +} + +NSString *CYCopyNSCYON(JSContextRef context, JSValueRef value, JSValueRef *exception) { + if (JSValueIsNull(context, value)) + return [@"null" retain]; + + CYTry { + CYPoolTry { + return CYCopyNSCYON(CYCastNSObject(NULL, context, value)); + } CYPoolCatch(NULL) + } CYCatch +} + +NSString *CYPoolNSCYON(apr_pool_t *pool, id value) { + return CYPoolRelease(pool, static_cast(CYCopyNSCYON(value))); +} + +const char *CYPoolCCYON(apr_pool_t *pool, JSContextRef context, JSValueRef value, JSValueRef *exception) { + if (NSString *json = CYCopyNSCYON(context, value, exception)) { + const char *string(CYPoolCString(pool, json)); + [json release]; + return string; + } else return NULL; +} + +// XXX: use objc_getAssociatedObject and objc_setAssociatedObject on 10.6 +struct CYInternal : + CYData +{ + JSObjectRef object_; + + CYInternal() : + object_(NULL) + { + } + + ~CYInternal() { + // XXX: delete object_? ;( + } + + static CYInternal *Get(id self) { + CYInternal *internal(NULL); + if (object_getInstanceVariable(self, "cy$internal_", reinterpret_cast(&internal)) == NULL) { + // XXX: do something epic? ;P + } + + return internal; + } + + static CYInternal *Set(id self) { + CYInternal *internal(NULL); + if (Ivar ivar = object_getInstanceVariable(self, "cy$internal_", reinterpret_cast(&internal))) { + if (internal == NULL) { + internal = new CYInternal(); + object_setIvar(self, ivar, reinterpret_cast(internal)); + } + } else { + // XXX: do something epic? ;P + } + + return internal; + } + + bool HasProperty(JSContextRef context, JSStringRef name) { + if (object_ == NULL) + return false; + return JSObjectHasProperty(context, object_, name); + } + + JSValueRef GetProperty(JSContextRef context, JSStringRef name) { + if (object_ == NULL) + return NULL; + return CYGetProperty(context, object_, name); + } + + void SetProperty(JSContextRef context, JSStringRef name, JSValueRef value) { + if (object_ == NULL) + object_ = JSObjectMake(context, NULL, NULL); + CYSetProperty(context, object_, name, value); + } +}; + +JSObjectRef CYMakeSelector(JSContextRef context, SEL sel) { + Selector_privateData *internal(new Selector_privateData(sel)); + return JSObjectMake(context, Selector_, internal); +} + +JSObjectRef CYMakePointer(JSContextRef context, void *pointer, sig::Type *type, ffi_type *ffi, JSObjectRef owner) { + Pointer *internal(new Pointer(pointer, type, owner)); + return JSObjectMake(context, Pointer_, internal); +} + +JSObjectRef CYMakeFunctor(JSContextRef context, void (*function)(), const char *type) { + Functor_privateData *internal(new Functor_privateData(type, function)); + return JSObjectMake(context, Functor_, internal); +} + +const char *CYPoolCString(apr_pool_t *pool, JSStringRef value) { + if (pool == NULL) { + const char *string([CYCastNSString(NULL, value) UTF8String]); + return string; + } else { + size_t size(JSStringGetMaximumUTF8CStringSize(value)); + char *string(new(pool) char[size]); + JSStringGetUTF8CString(value, string, size); + return string; + } +} + +const char *CYPoolCString(apr_pool_t *pool, JSContextRef context, JSValueRef value) { + return JSValueIsNull(context, value) ? NULL : CYPoolCString(pool, CYJSString(context, value)); +} + +bool CYGetOffset(apr_pool_t *pool, JSStringRef value, ssize_t &index) { + return CYGetOffset(CYPoolCString(pool, value), index); +} + +// XXX: this macro is unhygenic +#define CYCastCString(context, value) ({ \ + char *utf8; \ + if (value == NULL) \ + utf8 = NULL; \ + else if (JSStringRef string = CYCopyJSString(context, value)) { \ + size_t size(JSStringGetMaximumUTF8CStringSize(string)); \ + utf8 = reinterpret_cast(alloca(size)); \ + JSStringGetUTF8CString(string, utf8, size); \ + JSStringRelease(string); \ + } else \ + utf8 = NULL; \ + utf8; \ +}) + +void *CYCastPointer_(JSContextRef context, JSValueRef value) { + switch (JSValueGetType(context, value)) { + case kJSTypeNull: + return NULL; + /*case kJSTypeString: + return dlsym(RTLD_DEFAULT, CYCastCString(context, value)); + case kJSTypeObject: + if (JSValueIsObjectOfClass(context, value, Pointer_)) { + Pointer *internal(reinterpret_cast(JSObjectGetPrivate((JSObjectRef) value))); + return internal->value_; + }*/ default: - @throw [NSException exceptionWithName:NSInternalInconsistencyException reason:[NSString stringWithFormat:@"JSValueGetType() == 0x%x", type] userInfo:nil]; + double number(CYCastDouble(context, value)); + if (std::isnan(number)) + @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"cannot convert value to pointer" userInfo:nil]; + return reinterpret_cast(static_cast(static_cast(number))); } } -NSArray *CYCastNSArray(JSPropertyNameArrayRef names) { - size_t size(JSPropertyNameArrayGetCount(names)); - NSMutableArray *array([NSMutableArray arrayWithCapacity:size]); - for (size_t index(0); index != size; ++index) - [array addObject:CYCastNSString(JSPropertyNameArrayGetNameAtIndex(names, index))]; - return array; +template +_finline Type_ CYCastPointer(JSContextRef context, JSValueRef value) { + return reinterpret_cast(CYCastPointer_(context, value)); } -id CYCastNSObject(JSContextRef context, JSValueRef value) { - const NSObject *object(reinterpret_cast(CYCopyCFType(context, value))); - return object == nil ? nil : [object autorelease]; -} +SEL CYCastSEL(JSContextRef context, JSValueRef value) { + if (JSValueIsObjectOfClass(context, value, Selector_)) { + Selector_privateData *internal(reinterpret_cast(JSObjectGetPrivate((JSObjectRef) value))); + return reinterpret_cast(internal->value_); + } else + return CYCastPointer(context, value); +} + +void CYPoolFFI(apr_pool_t *pool, JSContextRef context, sig::Type *type, ffi_type *ffi, void *data, JSValueRef value) { + switch (type->primitive) { + case sig::boolean_P: + *reinterpret_cast(data) = JSValueToBoolean(context, value); + break; + +#define CYPoolFFI_(primitive, native) \ + case sig::primitive ## _P: \ + *reinterpret_cast(data) = CYCastDouble(context, value); \ + break; + + CYPoolFFI_(uchar, unsigned char) + CYPoolFFI_(char, char) + CYPoolFFI_(ushort, unsigned short) + CYPoolFFI_(short, short) + CYPoolFFI_(ulong, unsigned long) + CYPoolFFI_(long, long) + CYPoolFFI_(uint, unsigned int) + CYPoolFFI_(int, int) + CYPoolFFI_(ulonglong, unsigned long long) + CYPoolFFI_(longlong, long long) + CYPoolFFI_(float, float) + CYPoolFFI_(double, double) + + case sig::object_P: + case sig::typename_P: + *reinterpret_cast(data) = CYCastNSObject(pool, context, value); + break; + + case sig::selector_P: + *reinterpret_cast(data) = CYCastSEL(context, value); + break; + + case sig::pointer_P: + *reinterpret_cast(data) = CYCastPointer(context, value); + break; + + case sig::string_P: + *reinterpret_cast(data) = CYPoolCString(pool, context, value); + break; + + case sig::struct_P: { + uint8_t *base(reinterpret_cast(data)); + JSObjectRef aggregate(JSValueIsObject(context, value) ? (JSObjectRef) value : NULL); + for (size_t index(0); index != type->data.signature.count; ++index) { + sig::Element *element(&type->data.signature.elements[index]); + ffi_type *field(ffi->elements[index]); + + JSValueRef rhs; + if (aggregate == NULL) + rhs = value; + else { + rhs = CYGetProperty(context, aggregate, index); + if (JSValueIsUndefined(context, rhs)) { + if (element->name != NULL) + rhs = CYGetProperty(context, aggregate, CYJSString(element->name)); + else + goto undefined; + if (JSValueIsUndefined(context, rhs)) undefined: + @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"unable to extract structure value" userInfo:nil]; + } + } + + CYPoolFFI(pool, context, element->type, field, base, rhs); + // XXX: alignment? + base += field->size; + } + } break; + + case sig::void_P: + break; + + default: + NSLog(@"CYPoolFFI(%c)\n", type->primitive); + _assert(false); + } +} + +JSValueRef CYFromFFI(JSContextRef context, sig::Type *type, ffi_type *ffi, void *data, bool initialize = false, JSObjectRef owner = NULL) { + JSValueRef value; + + switch (type->primitive) { + case sig::boolean_P: + value = CYCastJSValue(context, *reinterpret_cast(data)); + break; + +#define CYFromFFI_(primitive, native) \ + case sig::primitive ## _P: \ + value = CYCastJSValue(context, *reinterpret_cast(data)); \ + break; + + CYFromFFI_(uchar, unsigned char) + CYFromFFI_(char, char) + CYFromFFI_(ushort, unsigned short) + CYFromFFI_(short, short) + CYFromFFI_(ulong, unsigned long) + CYFromFFI_(long, long) + CYFromFFI_(uint, unsigned int) + CYFromFFI_(int, int) + CYFromFFI_(ulonglong, unsigned long long) + CYFromFFI_(longlong, long long) + CYFromFFI_(float, float) + CYFromFFI_(double, double) + + case sig::object_P: { + if (id object = *reinterpret_cast(data)) { + value = CYCastJSValue(context, object); + if (initialize) + [object release]; + } else goto null; + } break; + + case sig::typename_P: + value = CYMakeInstance(context, *reinterpret_cast(data), true); + break; + + case sig::selector_P: + if (SEL sel = *reinterpret_cast(data)) + value = CYMakeSelector(context, sel); + else goto null; + break; + + case sig::pointer_P: + if (void *pointer = *reinterpret_cast(data)) + value = CYMakePointer(context, pointer, type->data.data.type, ffi, owner); + else goto null; + break; + + case sig::string_P: + if (char *utf8 = *reinterpret_cast(data)) + value = CYCastJSValue(context, utf8); + else goto null; + break; + + case sig::struct_P: + value = CYMakeStruct(context, data, type, ffi, owner); + break; + + case sig::void_P: + value = CYJSUndefined(context); + break; + + null: + value = CYJSNull(context); + break; + + default: + NSLog(@"CYFromFFI(%c)\n", type->primitive); + _assert(false); + } + + return value; +} + +static bool CYImplements(id object, Class _class, SEL selector, bool devoid) { + if (Method method = class_getInstanceMethod(_class, selector)) { + if (!devoid) + return true; + char type[16]; + method_getReturnType(method, type, sizeof(type)); + if (type[0] != 'v') + return true; + } + + // XXX: possibly use a more "awesome" check? + return false; +} + +const char *CYPoolTypeEncoding(apr_pool_t *pool, Class _class, SEL sel, Method method) { + if (method != NULL) + return method_getTypeEncoding(method); + else if (NSString *type = [[Bridge_ objectAtIndex:1] objectForKey:CYCastNSString(pool, sel_getName(sel))]) + return CYPoolCString(pool, type); + else + return NULL; +} + +void FunctionClosure_(ffi_cif *cif, void *result, void **arguments, void *arg) { + Closure_privateData *internal(reinterpret_cast(arg)); + + JSContextRef context(internal->context_); + + size_t count(internal->cif_.nargs); + JSValueRef values[count]; + + for (size_t index(0); index != count; ++index) + values[index] = CYFromFFI(context, internal->signature_.elements[1 + index].type, internal->cif_.arg_types[index], arguments[index]); + + JSValueRef value(CYCallAsFunction(context, internal->function_, NULL, count, values)); + CYPoolFFI(NULL, context, internal->signature_.elements[0].type, internal->cif_.rtype, result, value); +} + +void MessageClosure_(ffi_cif *cif, void *result, void **arguments, void *arg) { + Closure_privateData *internal(reinterpret_cast(arg)); + + JSContextRef context(internal->context_); + + size_t count(internal->cif_.nargs); + JSValueRef values[count]; + + for (size_t index(0); index != count; ++index) + values[index] = CYFromFFI(context, internal->signature_.elements[1 + index].type, internal->cif_.arg_types[index], arguments[index]); -void CYThrow(JSContextRef context, JSValueRef value) { - if (value == NULL) - return; - @throw CYCastNSObject(context, value); -} + JSObjectRef _this(CYCastJSObject(context, values[0])); -JSValueRef CYCastJSValue(JSContextRef context, id value) { - return value == nil ? JSValueMakeNull(context) : [value cy$JSValueInContext:context]; + JSValueRef value(CYCallAsFunction(context, internal->function_, _this, count - 2, values + 2)); + CYPoolFFI(NULL, context, internal->signature_.elements[0].type, internal->cif_.rtype, result, value); } -void CYThrow(JSContextRef context, id error, JSValueRef *exception) { - *exception = CYCastJSValue(context, error); -} +Closure_privateData *CYMakeFunctor_(JSContextRef context, JSObjectRef function, const char *type, void (*callback)(ffi_cif *, void *, void **, void *)) { + // XXX: in case of exceptions this will leak + // XXX: in point of fact, this may /need/ to leak :( + Closure_privateData *internal(new Closure_privateData(type)); -@implementation CYJSObject + ffi_closure *closure((ffi_closure *) _syscall(mmap( + NULL, sizeof(ffi_closure), + PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, + -1, 0 + ))); -- (id) initWithJSObject:(JSObjectRef)object inContext:(JSContextRef)context { - if ((self = [super init]) != nil) { - object_ = object; - context_ = context; - } return self; + ffi_status status(ffi_prep_closure(closure, &internal->cif_, callback, internal)); + _assert(status == FFI_OK); + + _syscall(mprotect(closure, sizeof(*closure), PROT_READ | PROT_EXEC)); + + internal->value_ = closure; + + internal->context_ = CYGetJSContext(); + internal->function_ = function; + + return internal; } -- (NSUInteger) count { - JSPropertyNameArrayRef names(JSObjectCopyPropertyNames(context_, object_)); - size_t size(JSPropertyNameArrayGetCount(names)); - JSPropertyNameArrayRelease(names); - return size; +JSObjectRef CYMakeFunctor(JSContextRef context, JSObjectRef function, const char *type) { + Closure_privateData *internal(CYMakeFunctor_(context, function, type, &FunctionClosure_)); + return JSObjectMake(context, Functor_, internal); } -- (id) objectForKey:(id)key { +static JSObjectRef CYMakeFunctor(JSContextRef context, JSValueRef value, const char *type) { JSValueRef exception(NULL); - JSValueRef value(JSObjectGetProperty(context_, object_, CYString(key), &exception)); - CYThrow(context_, exception); - return CYCastNSObject(context_, value); + bool function(JSValueIsInstanceOfConstructor(context, value, Function_, &exception)); + CYThrow(context, exception); + + if (function) { + JSObjectRef function(CYCastJSObject(context, value)); + return CYMakeFunctor(context, function, type); + } else { + void (*function)()(CYCastPointer(context, value)); + return CYMakeFunctor(context, function, type); + } } -- (NSEnumerator *) keyEnumerator { - JSPropertyNameArrayRef names(JSObjectCopyPropertyNames(context_, object_)); - NSEnumerator *enumerator([CYCastNSArray(names) objectEnumerator]); - JSPropertyNameArrayRelease(names); - return enumerator; +static JSObjectRef CYMakeMessage(JSContextRef context, SEL sel, IMP imp, const char *type) { + Message_privateData *internal(new Message_privateData(sel, type, imp)); + return JSObjectMake(context, Message_, internal); } -- (void) setObject:(id)object forKey:(id)key { - JSValueRef exception(NULL); - JSObjectSetProperty(context_, object_, CYString(key), CYCastJSValue(context_, object), kJSPropertyAttributeNone, &exception); - CYThrow(context_, exception); +static IMP CYMakeMessage(JSContextRef context, JSValueRef value, const char *type) { + JSObjectRef function(CYCastJSObject(context, value)); + Closure_privateData *internal(CYMakeFunctor_(context, function, type, &MessageClosure_)); + return reinterpret_cast(internal->GetValue()); } -- (void) removeObjectForKey:(id)key { - JSValueRef exception(NULL); - // XXX: this returns a bool... throw exception, or ignore? - JSObjectDeleteProperty(context_, object_, CYString(key), &exception); - CYThrow(context_, exception); +static bool Prototype_hasProperty(JSContextRef context, JSObjectRef object, JSStringRef property) { + Prototype *internal(reinterpret_cast(JSObjectGetPrivate(object))); + Class _class(internal->GetValue()); + + CYPool pool; + const char *name(CYPoolCString(pool, property)); + + if (SEL sel = sel_getUid(name)) + if (class_getInstanceMethod(_class, sel) != NULL) + return true; + + return false; } -@end +static JSValueRef Prototype_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { + Prototype *internal(reinterpret_cast(JSObjectGetPrivate(object))); + Class _class(internal->GetValue()); -@implementation CYJSArray + CYPool pool; + const char *name(CYPoolCString(pool, property)); -- (id) initWithJSObject:(JSObjectRef)object inContext:(JSContextRef)context { - if ((self = [super init]) != nil) { - object_ = object; - context_ = context; - } return self; + if (SEL sel = sel_getUid(name)) + if (Method method = class_getInstanceMethod(_class, sel)) + return CYMakeMessage(context, sel, method_getImplementation(method), method_getTypeEncoding(method)); + + return NULL; } -- (NSUInteger) count { - JSValueRef exception(NULL); - JSValueRef value(JSObjectGetProperty(context_, object_, length_, &exception)); - CYThrow(context_, exception); - return CYCastDouble(context_, value); +static bool Prototype_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) { + Prototype *internal(reinterpret_cast(JSObjectGetPrivate(object))); + Class _class(internal->GetValue()); + + CYPool pool; + const char *name(CYPoolCString(pool, property)); + + SEL sel(sel_registerName(name)); + + Method method(class_getInstanceMethod(_class, sel)); + + const char *type; + IMP imp; + + if (JSValueIsObjectOfClass(context, value, Message_)) { + Message_privateData *message(reinterpret_cast(JSObjectGetPrivate((JSObjectRef) value))); + type = sig::Unparse(pool, &message->signature_); + imp = reinterpret_cast(message->GetValue()); + } else { + type = CYPoolTypeEncoding(pool, _class, sel, method); + imp = CYMakeMessage(context, value, type); + } + + if (method != NULL) + method_setImplementation(method, imp); + else + class_replaceMethod(_class, sel, imp, type); + + return true; } -- (id) objectAtIndex:(NSUInteger)index { - JSValueRef exception(NULL); - JSValueRef value(JSObjectGetPropertyAtIndex(context_, object_, index, &exception)); - CYThrow(context_, exception); - id object(CYCastNSObject(context_, value)); - return object == nil ? [NSNull null] : object; +#if !__OBJC2__ +static bool Prototype_deleteProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { + Prototype *internal(reinterpret_cast(JSObjectGetPrivate(object))); + Class _class(internal->GetValue()); + + CYPool pool; + const char *name(CYPoolCString(pool, property)); + + if (SEL sel = sel_getUid(name)) + if (Method method = class_getInstanceMethod(_class, sel)) { + objc_method_list list = {NULL, 1, {method}}; + class_removeMethods(_class, &list); + return true; + } + + return false; } +#endif -@end +static void Prototype_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) { + Prototype *internal(reinterpret_cast(JSObjectGetPrivate(object))); + Class _class(internal->GetValue()); -CFStringRef JSValueToJSONCopy(JSContextRef context, JSValueRef value) { - id object(CYCastNSObject(context, value)); - return reinterpret_cast([(object == nil ? @"null" : [object cy$toJSON]) retain]); + unsigned int size; + Method *data(class_copyMethodList(_class, &size)); + for (size_t i(0); i != size; ++i) + JSPropertyNameAccumulatorAddName(names, CYJSString(sel_getName(method_getName(data[i])))); + free(data); } -static void OnData(CFSocketRef socket, CFSocketCallBackType type, CFDataRef address, const void *value, void *info) { - switch (type) { - case kCFSocketDataCallBack: - CFDataRef data(reinterpret_cast(value)); - Client *client(reinterpret_cast(info)); +static bool Instance_hasProperty(JSContextRef context, JSObjectRef object, JSStringRef property) { + Instance *internal(reinterpret_cast(JSObjectGetPrivate(object))); + id self(internal->GetValue()); - if (client->message_ == NULL) - client->message_ = CFHTTPMessageCreateEmpty(kCFAllocatorDefault, TRUE); + if (JSStringIsEqualToUTF8CString(property, "$cyi")) + return true; - if (!CFHTTPMessageAppendBytes(client->message_, CFDataGetBytePtr(data), CFDataGetLength(data))) - CFLog(kCFLogLevelError, CFSTR("CFHTTPMessageAppendBytes()")); - else if (CFHTTPMessageIsHeaderComplete(client->message_)) { - CFURLRef url(CFHTTPMessageCopyRequestURL(client->message_)); - Boolean absolute; - CFStringRef path(CFURLCopyStrictPath(url, &absolute)); - CFRelease(client->message_); + CYPool pool; + NSString *name(CYCastNSString(pool, property)); - CFStringRef code(CFURLCreateStringByReplacingPercentEscapes(kCFAllocatorDefault, path, CFSTR(""))); - CFRelease(path); + if (CYInternal *internal = CYInternal::Get(self)) + if (internal->HasProperty(context, property)) + return true; - JSStringRef script(JSStringCreateWithCFString(code)); - CFRelease(code); + CYPoolTry { + if ([self cy$hasProperty:name]) + return true; + } CYPoolCatch(false) - JSValueRef result(JSEvaluateScript(JSGetContext(), script, NULL, NULL, 0, NULL)); - JSStringRelease(script); + const char *string(CYPoolCString(pool, name)); + Class _class(object_getClass(self)); - CFHTTPMessageRef response(CFHTTPMessageCreateResponse(kCFAllocatorDefault, 200, NULL, kCFHTTPVersion1_1)); - CFHTTPMessageSetHeaderFieldValue(response, CFSTR("Content-Type"), CFSTR("application/json; charset=utf-8")); + if (class_getProperty(_class, string) != NULL) + return true; - CFStringRef json(JSValueToJSONCopy(JSGetContext(), result)); - CFDataRef body(CFStringCreateExternalRepresentation(kCFAllocatorDefault, json, kCFStringEncodingUTF8, NULL)); - CFRelease(json); + if (SEL sel = sel_getUid(string)) + if (CYImplements(self, _class, sel, true)) + return true; - CFStringRef length(CFStringCreateWithFormat(kCFAllocatorDefault, NULL, CFSTR("%u"), CFDataGetLength(body))); - CFHTTPMessageSetHeaderFieldValue(response, CFSTR("Content-Length"), length); - CFRelease(length); + return false; +} - CFHTTPMessageSetBody(response, body); - CFRelease(body); +static JSValueRef Instance_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { + Instance *internal(reinterpret_cast(JSObjectGetPrivate(object))); + id self(internal->GetValue()); - CFDataRef serialized(CFHTTPMessageCopySerializedMessage(response)); - CFRelease(response); + if (JSStringIsEqualToUTF8CString(property, "$cyi")) + return Internal::Make(context, self, object); - CFSocketSendData(socket, NULL, serialized, 0); - CFRelease(serialized); + CYTry { + CYPool pool; + NSString *name(CYCastNSString(pool, property)); - CFRelease(url); - } - break; - } -} + if (CYInternal *internal = CYInternal::Get(self)) + if (JSValueRef value = internal->GetProperty(context, property)) + return value; -static void OnAccept(CFSocketRef socket, CFSocketCallBackType type, CFDataRef address, const void *value, void *info) { - switch (type) { - case kCFSocketAcceptCallBack: - Client *client(new Client()); + CYPoolTry { + if (NSObject *data = [self cy$getProperty:name]) + return CYCastJSValue(context, data); + } CYPoolCatch(NULL) - client->message_ = NULL; + const char *string(CYPoolCString(pool, name)); + Class _class(object_getClass(self)); - CFSocketContext context; - context.version = 0; - context.info = client; - context.retain = NULL; - context.release = NULL; - context.copyDescription = NULL; + if (objc_property_t property = class_getProperty(_class, string)) { + PropertyAttributes attributes(property); + SEL sel(sel_registerName(attributes.Getter())); + return CYSendMessage(pool, context, self, sel, 0, NULL, false, exception); + } - client->socket_ = CFSocketCreateWithNative(kCFAllocatorDefault, *reinterpret_cast(value), kCFSocketDataCallBack, &OnData, &context); + if (SEL sel = sel_getUid(string)) + if (CYImplements(self, _class, sel, true)) + return CYSendMessage(pool, context, self, sel, 0, NULL, false, exception); - CFRunLoopAddSource(CFRunLoopGetCurrent(), CFSocketCreateRunLoopSource(kCFAllocatorDefault, client->socket_, 0), kCFRunLoopDefaultMode); - break; - } + return NULL; + } CYCatch } -static JSValueRef Instance_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { _pooled - @try { - NSString *name(CYCastNSString(property)); - NSLog(@"%@", name); - return NULL; +static bool Instance_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) { + Instance *internal(reinterpret_cast(JSObjectGetPrivate(object))); + id self(internal->GetValue()); + + CYPool pool; + + CYTry { + NSString *name(CYCastNSString(pool, property)); + NSString *data(CYCastNSObject(pool, context, value)); + + CYPoolTry { + if ([self cy$setProperty:name to:data]) + return true; + } CYPoolCatch(NULL) + + const char *string(CYPoolCString(pool, name)); + Class _class(object_getClass(self)); + + if (objc_property_t property = class_getProperty(_class, string)) { + PropertyAttributes attributes(property); + if (const char *setter = attributes.Setter()) { + SEL sel(sel_registerName(setter)); + JSValueRef arguments[1] = {value}; + CYSendMessage(pool, context, self, sel, 1, arguments, false, exception); + return true; + } + } + + size_t length(strlen(string)); + + char set[length + 5]; + + set[0] = 's'; + set[1] = 'e'; + set[2] = 't'; + + if (string[0] != '\0') { + set[3] = toupper(string[0]); + memcpy(set + 4, string + 1, length - 1); + } + + set[length + 3] = ':'; + set[length + 4] = '\0'; + + if (SEL sel = sel_getUid(set)) + if (CYImplements(self, _class, sel, false)) { + JSValueRef arguments[1] = {value}; + CYSendMessage(pool, context, self, sel, 1, arguments, false, exception); + } + + if (CYInternal *internal = CYInternal::Set(self)) { + internal->SetProperty(context, property, value); + return true; + } + + return false; } CYCatch } -typedef id jocData; +static bool Instance_deleteProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { + Instance *internal(reinterpret_cast(JSObjectGetPrivate(object))); + id self(internal->GetValue()); -static JSObjectRef Instance_callAsConstructor(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { _pooled - @try { - id data(reinterpret_cast(JSObjectGetPrivate(object))); - return CYMakeObject(context, [[data alloc] autorelease]); + CYTry { + CYPoolTry { + NSString *name(CYCastNSString(NULL, property)); + return [self cy$deleteProperty:name]; + } CYPoolCatch(NULL) } CYCatch } -struct ptrData { - apr_pool_t *pool_; - void *value_; - sig::Type type_; +static void Instance_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) { + Instance *internal(reinterpret_cast(JSObjectGetPrivate(object))); + id self(internal->GetValue()); - void *operator new(size_t size) { - apr_pool_t *pool; - apr_pool_create(&pool, NULL); - void *data(apr_palloc(pool, size)); - reinterpret_cast(data)->pool_ = pool; - return data;; - } + CYPool pool; + Class _class(object_getClass(self)); - ptrData(void *value) : - value_(value) { + unsigned int size; + objc_property_t *data(class_copyPropertyList(_class, &size)); + for (size_t i(0); i != size; ++i) + JSPropertyNameAccumulatorAddName(names, CYJSString(property_getName(data[i]))); + free(data); } -}; +} -struct ffiData : ptrData { - sig::Signature signature_; - ffi_cif cif_; +static JSObjectRef Instance_callAsConstructor(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { + CYTry { + Instance *internal(reinterpret_cast(JSObjectGetPrivate(object))); + JSObjectRef value(Instance::Make(context, [internal->GetValue() alloc], Instance::Uninitialized)); + return value; + } CYCatch +} - ffiData(void (*value)(), const char *type) : - ptrData(reinterpret_cast(value)) - { - sig::Parse(pool_, &signature_, type); - sig::sig_ffi_cif(pool_, &sig::ObjectiveC, &signature_, &cif_); - } -}; +static bool Internal_hasProperty(JSContextRef context, JSObjectRef object, JSStringRef property) { + Internal *internal(reinterpret_cast(JSObjectGetPrivate(object))); + CYPool pool; -struct selData : ptrData { - selData(SEL value) : - ptrData(value) - { - } -}; + id self(internal->GetValue()); + const char *name(CYPoolCString(pool, property)); + + if (object_getInstanceVariable(self, name, NULL) != NULL) + return true; + + return false; +} + +static JSValueRef Internal_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { + Internal *internal(reinterpret_cast(JSObjectGetPrivate(object))); + CYPool pool; + + CYTry { + id self(internal->GetValue()); + const char *name(CYPoolCString(pool, property)); -static void Pointer_finalize(JSObjectRef object) { - ptrData *data(reinterpret_cast(JSObjectGetPrivate(object))); - apr_pool_destroy(data->pool_); + if (Ivar ivar = object_getInstanceVariable(self, name, NULL)) { + Type_privateData type(pool, ivar_getTypeEncoding(ivar)); + return CYFromFFI(context, type.type_, type.GetFFI(), reinterpret_cast(self) + ivar_getOffset(ivar)); + } + + return NULL; + } CYCatch } -static void Instance_finalize(JSObjectRef object) { - id data(reinterpret_cast(JSObjectGetPrivate(object))); - [data release]; +static bool Internal_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) { + Internal *internal(reinterpret_cast(JSObjectGetPrivate(object))); + CYPool pool; + + CYTry { + id self(internal->GetValue()); + const char *name(CYPoolCString(pool, property)); + + if (Ivar ivar = object_getInstanceVariable(self, name, NULL)) { + Type_privateData type(pool, ivar_getTypeEncoding(ivar)); + CYPoolFFI(pool, context, type.type_, type.GetFFI(), reinterpret_cast(self) + ivar_getOffset(ivar), value); + return true; + } + + return false; + } CYCatch } -JSObjectRef CYMakeFunction(JSContextRef context, void (*function)(), const char *type) { - ffiData *data(new ffiData(function, type)); - return JSObjectMake(context, Functor_, data); +static void Internal_getPropertyNames_(Class _class, JSPropertyNameAccumulatorRef names) { + if (Class super = class_getSuperclass(_class)) + Internal_getPropertyNames_(super, names); + + unsigned int size; + Ivar *data(class_copyIvarList(_class, &size)); + for (size_t i(0); i != size; ++i) + JSPropertyNameAccumulatorAddName(names, CYJSString(ivar_getName(data[i]))); + free(data); } +static void Internal_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) { + Internal *internal(reinterpret_cast(JSObjectGetPrivate(object))); + CYPool pool; + + id self(internal->GetValue()); + Class _class(object_getClass(self)); -JSObjectRef CYMakeFunction(JSContextRef context, void *function, const char *type) { - return CYMakeFunction(context, reinterpret_cast(function), type); + Internal_getPropertyNames_(_class, names); } -void CYSetProperty(JSContextRef context, JSObjectRef object, const char *name, JSValueRef value) { - JSValueRef exception(NULL); - JSObjectSetProperty(context, object, CYString(name), value, kJSPropertyAttributeNone, &exception); - CYThrow(context, exception); +static JSValueRef Internal_callAsFunction_$cya(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { + Internal *internal(reinterpret_cast(JSObjectGetPrivate(object))); + return internal->owner_; } -char *CYPoolCString(apr_pool_t *pool, JSStringRef value) { - size_t size(JSStringGetMaximumUTF8CStringSize(value)); - char *string(new(pool) char[size]); - JSStringGetUTF8CString(value, string, size); - JSStringRelease(value); - return string; +bool Index_(apr_pool_t *pool, Struct_privateData *internal, JSStringRef property, ssize_t &index, uint8_t *&base) { + Type_privateData *typical(internal->type_); + sig::Type *type(typical->type_); + if (type == NULL) + return false; + + const char *name(CYPoolCString(pool, property)); + size_t length(strlen(name)); + double number(CYCastDouble(name, length)); + + size_t count(type->data.signature.count); + + if (std::isnan(number)) { + if (property == NULL) + return false; + + sig::Element *elements(type->data.signature.elements); + + for (size_t local(0); local != count; ++local) { + sig::Element *element(&elements[local]); + if (element->name != NULL && strcmp(name, element->name) == 0) { + index = local; + goto base; + } + } + + return false; + } else { + index = static_cast(number); + if (index != number || index < 0 || static_cast(index) >= count) + return false; + } + + base: + ffi_type **elements(typical->GetFFI()->elements); + + base = reinterpret_cast(internal->value_); + for (ssize_t local(0); local != index; ++local) + base += elements[local]->size; + + return true; } -char *CYPoolCString(apr_pool_t *pool, JSContextRef context, JSValueRef value) { - return CYPoolCString(pool, CYString(context, value)); +static JSValueRef Pointer_getIndex(JSContextRef context, JSObjectRef object, size_t index, JSValueRef *exception) { + Pointer *internal(reinterpret_cast(JSObjectGetPrivate(object))); + Type_privateData *typical(internal->type_); + + ffi_type *ffi(typical->GetFFI()); + + uint8_t *base(reinterpret_cast(internal->value_)); + base += ffi->size * index; + + JSObjectRef owner(internal->owner_ ?: object); + + CYTry { + return CYFromFFI(context, typical->type_, ffi, base, false, owner); + } CYCatch } -// XXX: this macro is unhygenic -#define CYCastCString(context, value) ({ \ - JSValueRef exception(NULL); \ - JSStringRef string(JSValueToStringCopy(context, value, &exception)); \ - CYThrow(context, exception); \ - size_t size(JSStringGetMaximumUTF8CStringSize(string)); \ - char *utf8(reinterpret_cast(alloca(size))); \ - JSStringGetUTF8CString(string, utf8, size); \ - JSStringRelease(string); \ - utf8; \ -}) +static JSValueRef Pointer_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { + CYPool pool; + Pointer *internal(reinterpret_cast(JSObjectGetPrivate(object))); + Type_privateData *typical(internal->type_); -SEL CYCastSEL(JSContextRef context, JSValueRef value) { - if (JSValueIsNull(context, value)) + if (typical->type_ == NULL) return NULL; - else if (JSValueIsObjectOfClass(context, value, Selector_)) { - selData *data(reinterpret_cast(JSObjectGetPrivate((JSObjectRef) value))); - return reinterpret_cast(data->value_); - } else - return sel_registerName(CYCastCString(context, value)); + + ssize_t offset; + if (!CYGetOffset(pool, property, offset)) + return NULL; + + return Pointer_getIndex(context, object, offset, exception); } -void *CYCastPointer(JSContextRef context, JSValueRef value) { - switch (JSValueGetType(context, value)) { - case kJSTypeNull: - return NULL; - case kJSTypeString: - return dlsym(RTLD_DEFAULT, CYCastCString(context, value)); - case kJSTypeObject: - if (JSValueIsObjectOfClass(context, value, Pointer_)) { - ptrData *data(reinterpret_cast(JSObjectGetPrivate((JSObjectRef) value))); - return data->value_; - } - default: - return reinterpret_cast(static_cast(CYCastDouble(context, value))); - } +static JSValueRef Pointer_getProperty_$cyi(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { + return Pointer_getIndex(context, object, 0, exception); } -void CYPoolFFI(apr_pool_t *pool, JSContextRef context, sig::Type *type, void *data, JSValueRef value) { - switch (type->primitive) { - case sig::boolean_P: - *reinterpret_cast(data) = JSValueToBoolean(context, value); - break; +static bool Pointer_setIndex(JSContextRef context, JSObjectRef object, size_t index, JSValueRef value, JSValueRef *exception) { + Pointer *internal(reinterpret_cast(JSObjectGetPrivate(object))); + Type_privateData *typical(internal->type_); -#define CYPoolFFI_(primitive, native) \ - case sig::primitive ## _P: \ - *reinterpret_cast(data) = CYCastDouble(context, value); \ - break; + ffi_type *ffi(typical->GetFFI()); - CYPoolFFI_(uchar, unsigned char) - CYPoolFFI_(char, char) - CYPoolFFI_(ushort, unsigned short) - CYPoolFFI_(short, short) - CYPoolFFI_(ulong, unsigned long) - CYPoolFFI_(long, long) - CYPoolFFI_(uint, unsigned int) - CYPoolFFI_(int, int) - CYPoolFFI_(ulonglong, unsigned long long) - CYPoolFFI_(longlong, long long) - CYPoolFFI_(float, float) - CYPoolFFI_(double, double) + uint8_t *base(reinterpret_cast(internal->value_)); + base += ffi->size * index; - case sig::object_P: - case sig::typename_P: - *reinterpret_cast(data) = CYCastNSObject(context, value); - break; + CYTry { + CYPoolFFI(NULL, context, typical->type_, ffi, base, value); + return true; + } CYCatch +} - case sig::selector_P: - *reinterpret_cast(data) = CYCastSEL(context, value); - break; +static bool Pointer_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) { + CYPool pool; + Pointer *internal(reinterpret_cast(JSObjectGetPrivate(object))); + Type_privateData *typical(internal->type_); - case sig::pointer_P: - *reinterpret_cast(data) = CYCastPointer(context, value); - break; + if (typical->type_ == NULL) + return NULL; - case sig::string_P: - *reinterpret_cast(data) = CYPoolCString(pool, context, value); - break; + ssize_t offset; + if (!CYGetOffset(pool, property, offset)) + return NULL; - case sig::struct_P: - goto fail; + return Pointer_setIndex(context, object, offset, value, exception); +} - case sig::void_P: - break; +static bool Pointer_setProperty_$cyi(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) { + return Pointer_setIndex(context, object, 0, value, exception); +} - default: fail: - NSLog(@"CYPoolFFI(%c)\n", type->primitive); - _assert(false); - } +static JSValueRef Struct_callAsFunction_$cya(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { + Struct_privateData *internal(reinterpret_cast(JSObjectGetPrivate(_this))); + Type_privateData *typical(internal->type_); + return CYMakePointer(context, internal->value_, typical->type_, typical->ffi_, _this); } -JSValueRef CYFromFFI(JSContextRef context, sig::Type *type, void *data) { - JSValueRef value; +static JSValueRef Struct_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { + CYPool pool; + Struct_privateData *internal(reinterpret_cast(JSObjectGetPrivate(object))); + Type_privateData *typical(internal->type_); - switch (type->primitive) { - case sig::boolean_P: - value = JSValueMakeBoolean(context, *reinterpret_cast(data)); - break; + ssize_t index; + uint8_t *base; -#define CYFromFFI_(primitive, native) \ - case sig::primitive ## _P: \ - value = JSValueMakeNumber(context, *reinterpret_cast(data)); \ - break; + if (!Index_(pool, internal, property, index, base)) + return NULL; - CYFromFFI_(uchar, unsigned char) - CYFromFFI_(char, char) - CYFromFFI_(ushort, unsigned short) - CYFromFFI_(short, short) - CYFromFFI_(ulong, unsigned long) - CYFromFFI_(long, long) - CYFromFFI_(uint, unsigned int) - CYFromFFI_(int, int) - CYFromFFI_(ulonglong, unsigned long long) - CYFromFFI_(longlong, long long) - CYFromFFI_(float, float) - CYFromFFI_(double, double) + JSObjectRef owner(internal->owner_ ?: object); + + CYTry { + return CYFromFFI(context, typical->type_->data.signature.elements[index].type, typical->GetFFI()->elements[index], base, false, owner); + } CYCatch +} - case sig::object_P: - case sig::typename_P: { - value = CYCastJSValue(context, *reinterpret_cast(data)); - } break; +static bool Struct_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) { + CYPool pool; + Struct_privateData *internal(reinterpret_cast(JSObjectGetPrivate(object))); + Type_privateData *typical(internal->type_); - case sig::selector_P: { - if (SEL sel = *reinterpret_cast(data)) { - selData *data(new selData(sel)); - value = JSObjectMake(context, Selector_, data); - } else goto null; - } break; + ssize_t index; + uint8_t *base; - case sig::pointer_P: { - if (void *pointer = *reinterpret_cast(data)) { - ptrData *data(new ptrData(pointer)); - value = JSObjectMake(context, Pointer_, data); - } else goto null; - } break; + if (!Index_(pool, internal, property, index, base)) + return false; - case sig::string_P: { - if (char *utf8 = *reinterpret_cast(data)) - value = JSValueMakeString(context, CYString(utf8)); - else goto null; - } break; + CYTry { + CYPoolFFI(NULL, context, typical->type_->data.signature.elements[index].type, typical->GetFFI()->elements[index], base, value); + return true; + } CYCatch +} - case sig::struct_P: - goto fail; +static void Struct_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) { + Struct_privateData *internal(reinterpret_cast(JSObjectGetPrivate(object))); + Type_privateData *typical(internal->type_); + sig::Type *type(typical->type_); - case sig::void_P: - value = JSValueMakeUndefined(context); - break; + if (type == NULL) + return; - null: - value = JSValueMakeNull(context); - break; + size_t count(type->data.signature.count); + sig::Element *elements(type->data.signature.elements); - default: fail: - NSLog(@"CYFromFFI(%c)\n", type->primitive); - _assert(false); - } + char number[32]; - return value; + for (size_t index(0); index != count; ++index) { + const char *name; + name = elements[index].name; + + if (name == NULL) { + sprintf(number, "%lu", index); + name = number; + } + + JSPropertyNameAccumulatorAddName(names, CYJSString(name)); + } } -static JSValueRef CYCallFunction(JSContextRef context, size_t count, const JSValueRef *arguments, JSValueRef *exception, sig::Signature *signature, ffi_cif *cif, void (*function)()) { _pooled - @try { - if (count != signature->count - 1) +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)()) { + CYTry { + if (setups + count != signature->count - 1) @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"incorrect number of arguments to ffi function" userInfo:nil]; - CYPool pool; - void *values[count]; + size_t size(setups + count); + void *values[size]; + memcpy(values, setup, sizeof(void *) * setups); - for (unsigned index(0); index != count; ++index) { + for (size_t index(setups); index != size; ++index) { sig::Element *element(&signature->elements[index + 1]); + ffi_type *ffi(cif->arg_types[index]); // XXX: alignment? - values[index] = new(pool) uint8_t[cif->arg_types[index]->size]; - CYPoolFFI(pool, context, element->type, values[index], arguments[index]); + values[index] = new(pool) uint8_t[ffi->size]; + CYPoolFFI(pool, context, element->type, ffi, values[index], arguments[index - setups]); } uint8_t value[cif->rtype->size]; ffi_call(cif, function, value, values); - return CYFromFFI(context, signature->elements[0].type, value); + return CYFromFFI(context, signature->elements[0].type, cif->rtype, value, initialize); + } CYCatch +} + +static JSValueRef ObjectiveC_Classes_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { + CYTry { + CYPool pool; + NSString *name(CYCastNSString(pool, property)); + if (Class _class = NSClassFromString(name)) + return CYMakeInstance(context, _class, true); + return NULL; + } CYCatch +} + +static void ObjectiveC_Classes_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) { + size_t size(objc_getClassList(NULL, 0)); + Class *data(reinterpret_cast(malloc(sizeof(Class) * size))); + + get: + size_t writ(objc_getClassList(data, size)); + if (size < writ) { + size = writ; + if (Class *copy = reinterpret_cast(realloc(data, sizeof(Class) * writ))) { + data = copy; + goto get; + } else goto done; + } + + for (size_t i(0); i != writ; ++i) + JSPropertyNameAccumulatorAddName(names, CYJSString(class_getName(data[i]))); + + done: + free(data); +} + +static JSValueRef ObjectiveC_Image_Classes_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { + const char *internal(reinterpret_cast(JSObjectGetPrivate(object))); + + CYTry { + CYPool pool; + const char *name(CYPoolCString(pool, property)); + unsigned int size; + const char **data(objc_copyClassNamesForImage(internal, &size)); + JSValueRef value; + for (size_t i(0); i != size; ++i) + if (strcmp(name, data[i]) == 0) { + if (Class _class = objc_getClass(name)) { + value = CYMakeInstance(context, _class, true); + goto free; + } else + break; + } + value = NULL; + free: + free(data); + return value; + } CYCatch +} + +static void ObjectiveC_Image_Classes_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) { + const char *internal(reinterpret_cast(JSObjectGetPrivate(object))); + unsigned int size; + const char **data(objc_copyClassNamesForImage(internal, &size)); + for (size_t i(0); i != size; ++i) + JSPropertyNameAccumulatorAddName(names, CYJSString(data[i])); + free(data); +} + +static JSValueRef ObjectiveC_Images_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { + CYTry { + CYPool pool; + const char *name(CYPoolCString(pool, property)); + unsigned int size; + const char **data(objc_copyImageNames(&size)); + for (size_t i(0); i != size; ++i) + if (strcmp(name, data[i]) == 0) { + name = data[i]; + goto free; + } + name = NULL; + free: + free(data); + if (name == NULL) + return NULL; + JSObjectRef value(JSObjectMake(context, NULL, NULL)); + CYSetProperty(context, value, CYJSString("classes"), JSObjectMake(context, ObjectiveC_Image_Classes_, const_cast(name))); + return value; + } CYCatch +} + +static void ObjectiveC_Images_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) { + unsigned int size; + const char **data(objc_copyImageNames(&size)); + for (size_t i(0); i != size; ++i) + JSPropertyNameAccumulatorAddName(names, CYJSString(data[i])); + free(data); +} + +static JSValueRef ObjectiveC_Protocols_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { + CYTry { + CYPool pool; + NSString *name(CYCastNSString(pool, property)); + if (Protocol *protocol = NSProtocolFromString(name)) + return CYMakeInstance(context, protocol, true); + return NULL; } CYCatch } -static JSValueRef Global_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { _pooled - @try { - NSString *name(CYCastNSString(property)); +static void ObjectiveC_Protocols_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) { + unsigned int size; + Protocol **data(objc_copyProtocolList(&size)); + for (size_t i(0); i != size; ++i) + JSPropertyNameAccumulatorAddName(names, CYJSString(protocol_getName(data[i]))); + free(data); +} + +static JSValueRef Runtime_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { + if (JSStringIsEqualToUTF8CString(property, "nil")) + return Instance::Make(context, nil); + + CYTry { + CYPool pool; + NSString *name(CYCastNSString(pool, property)); if (Class _class = NSClassFromString(name)) - return CYMakeObject(context, _class); - if (NSMutableArray *entry = [Bridge_ objectForKey:name]) + return CYMakeInstance(context, _class, true); + if (NSMutableArray *entry = [[Bridge_ objectAtIndex:0] objectForKey:name]) switch ([[entry objectAtIndex:0] intValue]) { case 0: - return JSEvaluateScript(JSGetContext(), CYString([entry objectAtIndex:1]), NULL, NULL, 0, NULL); + return JSEvaluateScript(CYGetJSContext(), CYJSString([entry objectAtIndex:1]), NULL, NULL, 0, NULL); case 1: - return CYMakeFunction(context, [name cy$symbol], [[entry objectAtIndex:1] UTF8String]); + return CYMakeFunctor(context, reinterpret_cast([name cy$symbol]), CYPoolCString(pool, [entry objectAtIndex:1])); case 2: - CYPool pool; + // XXX: this is horrendously inefficient sig::Signature signature; - sig::Parse(pool, &signature, [[entry objectAtIndex:1] UTF8String]); - return CYFromFFI(context, signature.elements[0].type, [name cy$symbol]); + sig::Parse(pool, &signature, CYPoolCString(pool, [entry objectAtIndex:1]), &Structor_); + ffi_cif cif; + sig::sig_ffi_cif(pool, &sig::ObjectiveC, &signature, &cif); + return CYFromFFI(context, signature.elements[0].type, cif.rtype, [name cy$symbol]); } return NULL; } CYCatch @@ -933,330 +2651,818 @@ bool stret(ffi_type *ffi_type) { ); } -static JSValueRef $objc_msgSend(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { _pooled +extern "C" { + int *_NSGetArgc(void); + char ***_NSGetArgv(void); + int UIApplicationMain(int argc, char *argv[], NSString *principalClassName, NSString *delegateClassName); +} + +static JSValueRef System_print(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { + CYTry { + NSLog(@"%s", CYCastCString(context, arguments[0])); + return CYJSUndefined(context); + } CYCatch +} + +JSValueRef CYSendMessage(apr_pool_t *pool, JSContextRef context, id self, SEL _cmd, size_t count, const JSValueRef arguments[], bool initialize, JSValueRef *exception) { const char *type; - @try { + Class _class(object_getClass(self)); + if (Method method = class_getInstanceMethod(_class, _cmd)) + type = method_getTypeEncoding(method); + else { + CYTry { + CYPoolTry { + NSMethodSignature *method([self methodSignatureForSelector:_cmd]); + if (method == nil) + @throw [NSException exceptionWithName:NSInvalidArgumentException reason:[NSString stringWithFormat:@"unrecognized selector %s sent to object %p", sel_getName(_cmd), self] userInfo:nil]; + type = CYPoolCString(pool, [method _typeString]); + } CYPoolCatch(NULL) + } CYCatch + } + + void *setup[2]; + setup[0] = &self; + setup[1] = &_cmd; + + sig::Signature signature; + sig::Parse(pool, &signature, type, &Structor_); + + ffi_cif cif; + sig::sig_ffi_cif(pool, &sig::ObjectiveC, &signature, &cif); + + void (*function)() = stret(cif.rtype) ? reinterpret_cast(&objc_msgSend_stret) : reinterpret_cast(&objc_msgSend); + return CYCallFunction(pool, context, 2, setup, count, arguments, initialize, exception, &signature, &cif, function); +} + +static JSValueRef $objc_msgSend(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { + CYPool pool; + + bool uninitialized; + + id self; + SEL _cmd; + + CYTry { if (count < 2) @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"too few arguments to objc_msgSend" userInfo:nil]; - id self(CYCastNSObject(context, arguments[0])); + if (JSValueIsObjectOfClass(context, arguments[0], Instance_)) { + Instance *internal(reinterpret_cast(JSObjectGetPrivate((JSObjectRef) arguments[0]))); + self = internal->GetValue(); + uninitialized = internal->IsUninitialized(); + if (uninitialized) + internal->value_ = nil; + } else { + self = CYCastNSObject(pool, context, arguments[0]); + uninitialized = false; + } + if (self == nil) - return JSValueMakeNull(context); + return CYJSNull(context); + + _cmd = CYCastSEL(context, arguments[1]); + } CYCatch + + return CYSendMessage(pool, context, self, _cmd, count - 2, arguments + 2, uninitialized, exception); +} + +MSHook(void, CYDealloc, id self, SEL sel) { + CYInternal *internal; + object_getInstanceVariable(self, "cy$internal_", reinterpret_cast(&internal)); + if (internal != NULL) + delete internal; + _CYDealloc(self, sel); +} + +MSHook(void, objc_registerClassPair, Class _class) { + Class super(class_getSuperclass(_class)); + if (super == NULL || class_getInstanceVariable(super, "cy$internal_") == NULL) { + class_addIvar(_class, "cy$internal_", sizeof(CYInternal *), log2(sizeof(CYInternal *)), "^{CYInternal}"); + MSHookMessage(_class, @selector(dealloc), MSHake(CYDealloc)); + } - SEL _cmd(CYCastSEL(context, arguments[1])); - NSMethodSignature *method([self methodSignatureForSelector:_cmd]); - if (method == nil) - @throw [NSException exceptionWithName:NSInvalidArgumentException reason:[NSString stringWithFormat:@"unrecognized selector %s sent to object %p", sel_getName(_cmd), self] userInfo:nil]; + _objc_registerClassPair(_class); +} - type = [[method _typeString] UTF8String]; +static JSValueRef objc_registerClassPair_(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { + CYTry { + if (count != 1) + @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"incorrect number of arguments to objc_registerClassPair" userInfo:nil]; + CYPool pool; + Class _class(CYCastNSObject(pool, context, arguments[0])); + $objc_registerClassPair(_class); + return CYJSUndefined(context); } CYCatch +} + +static JSValueRef Selector_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { + JSValueRef setup[count + 2]; + setup[0] = _this; + setup[1] = object; + memcpy(setup + 2, arguments, sizeof(JSValueRef) * count); + return $objc_msgSend(context, NULL, NULL, count + 2, setup, exception); +} +static JSValueRef Message_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYPool pool; + Message_privateData *internal(reinterpret_cast(JSObjectGetPrivate(object))); - sig::Signature signature; - sig::Parse(pool, &signature, type); + // XXX: handle Instance::Uninitialized? + id self(CYCastNSObject(pool, context, _this)); - ffi_cif cif; - sig::sig_ffi_cif(pool, &sig::ObjectiveC, &signature, &cif); + void *setup[2]; + setup[0] = &self; + setup[1] = &internal->sel_; - void (*function)() = stret(cif.rtype) ? reinterpret_cast(&objc_msgSend_stret) : reinterpret_cast(&objc_msgSend); - return CYCallFunction(context, count, arguments, exception, &signature, &cif, function); + return CYCallFunction(pool, context, 2, setup, count, arguments, false, exception, &internal->signature_, &internal->cif_, internal->GetValue()); +} + +static JSValueRef Functor_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { + CYPool pool; + Functor_privateData *internal(reinterpret_cast(JSObjectGetPrivate(object))); + return CYCallFunction(pool, context, 0, NULL, count, arguments, false, exception, &internal->signature_, &internal->cif_, internal->GetValue()); } -static JSValueRef ffi_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { - ffiData *data(reinterpret_cast(JSObjectGetPrivate(object))); - return CYCallFunction(context, count, arguments, exception, &data->signature_, &data->cif_, reinterpret_cast(data->value_)); +JSObjectRef Selector_new(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { + CYTry { + if (count != 1) + @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"incorrect number of arguments to Selector constructor" userInfo:nil]; + const char *name(CYCastCString(context, arguments[0])); + return CYMakeSelector(context, sel_registerName(name)); + } CYCatch } -JSObjectRef ffi(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { - @try { +JSObjectRef Pointer_new(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { + CYTry { if (count != 2) - @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"incorrect number of arguments to ffi constructor" userInfo:nil]; - void *function(CYCastPointer(context, arguments[0])); + @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"incorrect number of arguments to Functor constructor" userInfo:nil]; + + void *value(CYCastPointer(context, arguments[0])); const char *type(CYCastCString(context, arguments[1])); - return CYMakeFunction(context, function, type); + + CYPool pool; + + sig::Signature signature; + sig::Parse(pool, &signature, type, &Structor_); + + return CYMakePointer(context, value, signature.elements[0].type, NULL, NULL); } CYCatch } -JSValueRef Pointer_getProperty_value(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { - ptrData *data(reinterpret_cast(JSObjectGetPrivate(object))); - return JSValueMakeNumber(context, reinterpret_cast(data->value_)); +JSObjectRef CYMakeType(JSContextRef context, JSObjectRef object, const char *type) { + Type_privateData *internal(new Type_privateData(NULL, type)); + return JSObjectMake(context, Type_, internal); } -static JSStaticValue Pointer_staticValues[2] = { - {"value", &Pointer_getProperty_value, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete}, - {NULL, NULL, NULL, 0} -}; +JSObjectRef Type_new(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { + CYTry { + if (count != 1) + @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"incorrect number of arguments to Type constructor" userInfo:nil]; + const char *type(CYCastCString(context, arguments[0])); + return CYMakeType(context, object, type); + } CYCatch +} -enum CYTokenType { - /*CYTokenBreak, CYTokenCase, CYTokenCatch, CYTokenContinue, CYTokenDefault, - CYTokenDelete, CYTokenDo, CYTokenElse, CYTokenFinally, CYTokenFor, - CYTokenFunction, CYTokenIf, CYTokenIn, CYTokenInstanceOf, CYTokenNew, - CYTokenReturn, CYTokenSwitch, CYTokenThis, CYTokenThrow, CYTokenTry, - CYTokenTypeOf, CYTokenVar, CYTokenVoid, CYTokenWhile, CYTokenWith,*/ +static JSValueRef Type_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { + CYTry { + if (count != 1) + @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"incorrect number of arguments to type cast function" userInfo:nil]; + Type_privateData *internal(reinterpret_cast(JSObjectGetPrivate(object))); + sig::Type *type(internal->type_); + ffi_type *ffi(internal->GetFFI()); + // XXX: alignment? + uint8_t value[ffi->size]; + CYPool pool; + CYPoolFFI(pool, context, type, ffi, value, arguments[0]); + return CYFromFFI(context, type, ffi, value); + } CYCatch +} - CYTokenWord, CYTokenPunctuation, CYTokenLiteral, - CYTokenSemiColon, CYTokenOpen, CYTokenClose, -}; +static JSObjectRef Type_callAsConstructor(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { + CYTry { + if (count > 1) + @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"incorrect number of arguments to type cast function" userInfo:nil]; + Type_privateData *internal(reinterpret_cast(JSObjectGetPrivate(object))); + size_t size(count == 0 ? 0 : CYCastDouble(context, arguments[0])); + // XXX: alignment? + void *value(malloc(internal->GetFFI()->size * size)); + return CYMakePointer(context, value, internal->type_, internal->ffi_, NULL); + } CYCatch +} -struct CYToken { - enum CYTokenType type_; - char *value_; -}; +JSObjectRef Instance_new(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { + CYTry { + if (count > 1) + @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"incorrect number of arguments to Instance constructor" userInfo:nil]; + id self(count == 0 ? nil : CYCastPointer(context, arguments[0])); + return Instance::Make(context, self); + } CYCatch +} -struct CYExpression { -}; +JSObjectRef Functor_new(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { + CYTry { + if (count != 2) + @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"incorrect number of arguments to Functor constructor" userInfo:nil]; + const char *type(CYCastCString(context, arguments[1])); + return CYMakeFunctor(context, arguments[0], type); + } CYCatch +} -struct CYRange { - uint64_t lo_; - uint64_t hi_; +JSValueRef CYValue_getProperty_value(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { + CYValue *internal(reinterpret_cast(JSObjectGetPrivate(object))); + return CYCastJSValue(context, reinterpret_cast(internal->value_)); +} - CYRange(uint64_t lo, uint64_t hi) : - lo_(lo), hi_(hi) - { - } +static JSValueRef CYValue_callAsFunction_$cya(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { + CYValue *internal(reinterpret_cast(JSObjectGetPrivate(_this))); + Type_privateData *typical(internal->GetType()); - bool operator [](uint8_t value) const { - return (value >> 7) && (value >> 6 ? hi_ : lo_) >> (value & 0x3f) & 0x1; - } + sig::Type *type; + ffi_type *ffi; - void operator()(uint8_t value) { - if (value >> 7) - return; - (value >> 6 ? hi_ : lo_) |= uint64_t(0x1) << (value & 0x3f); + if (typical == NULL) { + type = NULL; + ffi = NULL; + } else { + type = typical->type_; + ffi = typical->ffi_; } -}; -CYRange WordStartRange_(0x1000000000LLU,0x7fffffe87fffffeLLU); // A-Za-z_$ -CYRange WordEndRange_(0x3ff001000000000LLU,0x7fffffe87fffffeLLU); // A-Za-z_$0-9 -CYRange NumberRange_(0x3ff400000000000LLU,0x100007e0100007eLLU); // 0-9.eExXA-Fa-f -CYRange PunctuationRange_(0xfc00fc6200000001LLU,0x5000000040000000LLU); // -.,;<>=!+*/%&|^~?: + return CYMakePointer(context, &internal->value_, type, ffi, object); +} -struct CYParser { - FILE *file_; +static JSValueRef CYValue_callAsFunction_valueOf(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { + CYValue *internal(reinterpret_cast(JSObjectGetPrivate(_this))); - size_t capacity_; - char *data_; + CYTry { + return CYCastJSValue(context, reinterpret_cast(internal->value_)); + } CYCatch +} - size_t offset_; - size_t size_; +static JSValueRef CYValue_callAsFunction_toJSON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { + return CYValue_callAsFunction_valueOf(context, object, _this, count, arguments, exception); +} - CYParser(FILE *file) : - file_(file), - capacity_(1024), - data_(reinterpret_cast(malloc(capacity_))), - offset_(0), - size_(0) - { - } +static JSValueRef CYValue_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { + CYValue *internal(reinterpret_cast(JSObjectGetPrivate(_this))); + char string[32]; + sprintf(string, "%p", internal->value_); - ~CYParser() { - // XXX: this will not deconstruct in constructor failures - free(data_); - } + CYTry { + return CYCastJSValue(context, string); + } CYCatch +} - bool ReadLine() { - offset_ = 0; - data_[capacity_ - 1] = ~'\0'; +static JSValueRef Instance_getProperty_constructor(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { + Instance *internal(reinterpret_cast(JSObjectGetPrivate(object))); + return Instance::Make(context, object_getClass(internal->GetValue())); +} - start: - if (fgets(data_, capacity_, file_) == NULL) - return false; +static JSValueRef Instance_getProperty_prototype(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { + Instance *internal(reinterpret_cast(JSObjectGetPrivate(object))); + id self(internal->GetValue()); + // XXX: this is a lame object_isClass + if (class_getInstanceMethod(object_getClass(self), @selector(alloc)) == NULL) + return CYJSUndefined(context); + return Prototype::Make(context, self); +} - check: - if (data_[capacity_ - 1] != '\0') { - size_ = strlen(data_); - if (size_ == 0) - goto start; - } else if (data_[capacity_ - 2] == '\n') - size_ = capacity_ - 2; - else { - size_t capacity(capacity_ * 2); - char *data(reinterpret_cast(realloc(data_, capacity))); - _assert(data != NULL); - data_ = data; - size_ = capacity_ - 1; - capacity_ = capacity; - fgets(data_ + size_, capacity_ - size_, file_); - goto check; - } +static JSValueRef Instance_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { + Instance *internal(reinterpret_cast(JSObjectGetPrivate(_this))); - return true; - } + CYTry { + CYPoolTry { + return CYCastJSValue(context, CYJSString(CYPoolNSCYON(NULL, internal->GetValue()))); + } CYPoolCatch(NULL) + } CYCatch +} - _finline void ScanRange(const CYRange &range) { - while (range[data_[++offset_]]); - } +static JSValueRef Instance_callAsFunction_toJSON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { + Instance *internal(reinterpret_cast(JSObjectGetPrivate(_this))); - CYToken *CYParseToken(apr_pool_t *pool, bool expecting) { - char next; + CYTry { + CYPoolTry { + NSString *key(count == 0 ? nil : CYCastNSString(NULL, CYJSString(context, arguments[0]))); + return CYCastJSValue(context, CYJSString([internal->GetValue() cy$toJSON:key])); + } CYPoolCatch(NULL) + } CYCatch +} - for (;;) { - if (offset_ == size_ && (!expecting || !ReadLine())) - return false; - next = data_[offset_]; - if (next != ' ' && next != '\t') - break; - ++offset_; - } +static JSValueRef Instance_callAsFunction_toString(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { + Instance *internal(reinterpret_cast(JSObjectGetPrivate(_this))); - CYTokenType type; - size_t index(offset_); + CYTry { + CYPoolTry { + return CYCastJSValue(context, CYJSString([internal->GetValue() description])); + } CYPoolCatch(NULL) + } CYCatch +} - if (WordStartRange_[next]) { - ScanRange(WordEndRange_); - type = CYTokenWord; - } else if (next == '"' || next == '\'') { - _assert(false); - } else if (next == '.') { - char after(data_[offset_ + 1]); - if (after >= '0' && next <= '9') - goto number; - goto punctuation; - } else if (next >= '0' && next <= '9') { - number: - ScanRange(NumberRange_); - type = CYTokenLiteral; - } else if (PunctuationRange_[next]) { - punctuation: - ScanRange(PunctuationRange_); - type = CYTokenPunctuation; - } else if (next == '(' || next == '{' || next == '[') { - ++offset_; - type = CYTokenOpen; - } else if (next == ')' || next == '}' || next == ']') { - ++offset_; - type = CYTokenClose; - } else if (next == ';') { - ++offset_; - type = CYTokenSemiColon; - } else { - _assert(false); - } +static JSValueRef Selector_callAsFunction_toString(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { + Selector_privateData *internal(reinterpret_cast(JSObjectGetPrivate(_this))); - CYToken *token(new(pool) CYToken()); - token->type_ = type; - token->value_ = apr_pstrndup(pool, data_ + index, offset_ - index); - return token; - } + CYTry { + return CYCastJSValue(context, sel_getName(internal->GetValue())); + } CYCatch +} + +static JSValueRef Selector_callAsFunction_toJSON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { + return Selector_callAsFunction_toString(context, object, _this, count, arguments, exception); +} + +static JSValueRef Selector_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { + Selector_privateData *internal(reinterpret_cast(JSObjectGetPrivate(_this))); + const char *name(sel_getName(internal->GetValue())); + + CYTry { + CYPoolTry { + return CYCastJSValue(context, CYJSString([NSString stringWithFormat:@"@selector(%s)", name])); + } CYPoolCatch(NULL) + } CYCatch +} + +static JSValueRef Selector_callAsFunction_type(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { + CYTry { + if (count != 1) + @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"incorrect number of arguments to Selector.type" userInfo:nil]; + CYPool pool; + Selector_privateData *internal(reinterpret_cast(JSObjectGetPrivate(_this))); + Class _class(CYCastNSObject(pool, context, arguments[0])); + SEL sel(internal->GetValue()); + Method method(class_getInstanceMethod(_class, sel)); + const char *type(CYPoolTypeEncoding(pool, _class, sel, method)); + return type == NULL ? CYJSNull(context) : CYCastJSValue(context, CYJSString(type)); + } CYCatch +} + +static JSValueRef Type_callAsFunction_toString(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { + CYTry { + Type_privateData *internal(reinterpret_cast(JSObjectGetPrivate(_this))); + CYPool pool; + const char *type(sig::Unparse(pool, internal->type_)); + CYPoolTry { + return CYCastJSValue(context, CYJSString(type)); + } CYPoolCatch(NULL) + } CYCatch +} + +static JSValueRef Type_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { + CYTry { + Type_privateData *internal(reinterpret_cast(JSObjectGetPrivate(_this))); + CYPool pool; + const char *type(sig::Unparse(pool, internal->type_)); + CYPoolTry { + return CYCastJSValue(context, CYJSString([NSString stringWithFormat:@"new Type(%@)", [[NSString stringWithUTF8String:type] cy$toCYON]])); + } CYPoolCatch(NULL) + } CYCatch +} + +static JSValueRef Type_callAsFunction_toJSON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { + return Type_callAsFunction_toString(context, object, _this, count, arguments, exception); +} + +static JSStaticValue CYValue_staticValues[2] = { + {"value", &CYValue_getProperty_value, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete}, + {NULL, NULL, NULL, 0} }; -void CYConsole(FILE *fin, FILE *fout, FILE *ferr) { - std::string line; +static JSStaticValue Pointer_staticValues[2] = { + {"$cyi", &Pointer_getProperty_$cyi, &Pointer_setProperty_$cyi, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, + {NULL, NULL, NULL, 0} +}; - __gnu_cxx::stdio_filebuf bin(fin, std::ios::in); - std::istream sin(&bin); +static JSStaticFunction Pointer_staticFunctions[4] = { + {"toCYON", &CYValue_callAsFunction_toCYON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, + {"toJSON", &CYValue_callAsFunction_toJSON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, + {"valueOf", &CYValue_callAsFunction_valueOf, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, + {NULL, NULL, 0} +}; - for (;;) { _pooled - fputs(">>> ", fout); - fflush(fout); +static JSStaticFunction Struct_staticFunctions[2] = { + {"$cya", &Struct_callAsFunction_$cya, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, + {NULL, NULL, 0} +}; - if (!std::getline(sin, line)) - break; +static JSStaticFunction Functor_staticFunctions[4] = { + {"toCYON", &CYValue_callAsFunction_toCYON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, + {"toJSON", &CYValue_callAsFunction_toJSON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, + {"valueOf", &CYValue_callAsFunction_valueOf, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, + {NULL, NULL, 0} +}; - JSStringRef script(JSStringCreateWithUTF8CString(line.c_str())); +static JSStaticValue Instance_staticValues[4] = { + {"constructor", &Instance_getProperty_constructor, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, + {"prototype", &Instance_getProperty_prototype, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, + {"value", &CYValue_getProperty_value, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, + {NULL, NULL, NULL, 0} +}; - JSContextRef context(JSGetContext()); +static JSStaticFunction Instance_staticFunctions[5] = { + {"$cya", &CYValue_callAsFunction_$cya, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, + {"toCYON", &Instance_callAsFunction_toCYON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, + {"toJSON", &Instance_callAsFunction_toJSON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, + {"toString", &Instance_callAsFunction_toString, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, + {NULL, NULL, 0} +}; - JSValueRef exception(NULL); - JSValueRef result(JSEvaluateScript(context, script, NULL, NULL, 0, &exception)); - JSStringRelease(script); +static JSStaticFunction Internal_staticFunctions[2] = { + {"$cya", &Internal_callAsFunction_$cya, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, + {NULL, NULL, 0} +}; - if (exception != NULL) - result = exception; +static JSStaticFunction Selector_staticFunctions[5] = { + {"toCYON", &Selector_callAsFunction_toCYON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, + {"toJSON", &Selector_callAsFunction_toJSON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, + {"toString", &Selector_callAsFunction_toString, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, + {"type", &Selector_callAsFunction_type, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, + {NULL, NULL, 0} +}; - if (!JSValueIsUndefined(context, result)) { - CFStringRef json; +static JSStaticFunction Type_staticFunctions[4] = { + {"toCYON", &Type_callAsFunction_toCYON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, + {"toJSON", &Type_callAsFunction_toJSON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, + {"toString", &Type_callAsFunction_toString, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, + {NULL, NULL, 0} +}; - @try { json: - json = JSValueToJSONCopy(context, result); - } @catch (id error) { - CYThrow(context, error, &result); - goto json; - } +CYDriver::CYDriver(const std::string &filename) : + state_(CYClear), + data_(NULL), + size_(0), + file_(NULL), + filename_(filename), + source_(NULL) +{ + ScannerInit(); +} - fputs([reinterpret_cast(json) UTF8String], fout); - CFRelease(json); +CYDriver::~CYDriver() { + ScannerDestroy(); +} - fputs("\n", fout); - fflush(fout); - } +void cy::parser::error(const cy::parser::location_type &location, const std::string &message) { + CYDriver::Error error; + error.location_ = location; + error.message_ = message; + driver.errors_.push_back(error); +} + +void CYSetArgs(int argc, const char *argv[]) { + JSContextRef context(CYGetJSContext()); + JSValueRef args[argc]; + for (int i(0); i != argc; ++i) + args[i] = CYCastJSValue(context, argv[i]); + JSValueRef exception(NULL); + JSObjectRef array(JSObjectMakeArray(context, argc, args, &exception)); + CYThrow(context, exception); + CYSetProperty(context, System_, CYJSString("args"), array); +} + +JSObjectRef CYGetGlobalObject(JSContextRef context) { + return JSContextGetGlobalObject(context); +} + +const char *CYExecute(apr_pool_t *pool, const char *code) { _pooled + JSContextRef context(CYGetJSContext()); + JSValueRef exception(NULL), result; + + try { + result = JSEvaluateScript(context, CYJSString(code), NULL, NULL, 0, &exception); + } catch (const char *error) { + return error; + } + + if (exception != NULL) { error: + result = exception; + exception = NULL; } + + if (JSValueIsUndefined(context, result)) + return NULL; + + const char *json(CYPoolCCYON(pool, context, result, &exception)); + if (exception != NULL) + goto error; + + CYSetProperty(context, CYGetGlobalObject(context), Result_, result); + return json; } -MSInitialize { _pooled - apr_initialize(); +bool CYRecvAll_(int socket, uint8_t *data, size_t size) { + while (size != 0) if (size_t writ = _syscall(recv(socket, data, size, 0))) { + data += writ; + size -= writ; + } else + return false; + return true; +} - NSCFBoolean_ = objc_getClass("NSCFBoolean"); +bool CYSendAll_(int socket, const uint8_t *data, size_t size) { + while (size != 0) if (size_t writ = _syscall(send(socket, data, size, 0))) { + data += writ; + size -= writ; + } else + return false; + return true; +} + +apr_pool_t *Pool_; + +struct CYExecute_ { + apr_pool_t *pool_; + const char * volatile data_; +}; + +// XXX: this is "tre lame" +@interface CYClient_ : NSObject { +} - pid_t pid(getpid()); +- (void) execute:(NSValue *)value; - struct sockaddr_in address; - address.sin_len = sizeof(address); - address.sin_family = AF_INET; - address.sin_addr.s_addr = INADDR_ANY; - address.sin_port = htons(10000 + pid); +@end - CFDataRef data(CFDataCreate(kCFAllocatorDefault, reinterpret_cast(&address), sizeof(address))); +@implementation CYClient_ - CFSocketSignature signature; - signature.protocolFamily = AF_INET; - signature.socketType = SOCK_STREAM; - signature.protocol = IPPROTO_TCP; - signature.address = data; +- (void) execute:(NSValue *)value { + CYExecute_ *execute(reinterpret_cast([value pointerValue])); + const char *data(execute->data_); + execute->data_ = NULL; + execute->data_ = CYExecute(execute->pool_, data); +} - CFSocketRef socket(CFSocketCreateWithSocketSignature(kCFAllocatorDefault, &signature, kCFSocketAcceptCallBack, &OnAccept, NULL)); - CFRunLoopAddSource(CFRunLoopGetCurrent(), CFSocketCreateRunLoopSource(kCFAllocatorDefault, socket, 0), kCFRunLoopDefaultMode); +@end - JSClassDefinition definition; +struct CYClient : + CYData +{ + int socket_; + apr_thread_t *thread_; - definition = kJSClassDefinitionEmpty; - definition.className = "Pointer"; - definition.staticValues = Pointer_staticValues; - definition.finalize = &Pointer_finalize; - Pointer_ = JSClassCreate(&definition); + CYClient(int socket) : + socket_(socket) + { + } - definition = kJSClassDefinitionEmpty; - definition.className = "Functor"; - definition.parentClass = Pointer_; - definition.callAsFunction = &ffi_callAsFunction; - Functor_ = JSClassCreate(&definition); + ~CYClient() { + _syscall(close(socket_)); + } - definition = kJSClassDefinitionEmpty; - definition.className = "Selector"; - definition.parentClass = Pointer_; - Selector_ = JSClassCreate(&definition); + void Handle() { _pooled + CYClient_ *client = [[[CYClient_ alloc] init] autorelease]; - definition = kJSClassDefinitionEmpty; - definition.className = "Instance_"; - definition.getProperty = &Instance_getProperty; - definition.callAsConstructor = &Instance_callAsConstructor; - definition.finalize = &Instance_finalize; - Instance_ = JSClassCreate(&definition); + for (;;) { + size_t size; + if (!CYRecvAll(socket_, &size, sizeof(size))) + return; + + CYPool pool; + char *data(new(pool) char[size + 1]); + if (!CYRecvAll(socket_, data, size)) + return; + data[size] = '\0'; + + CYDriver driver(""); + cy::parser parser(driver); + + driver.data_ = data; + driver.size_ = size; + + const char *json; + if (parser.parse() != 0 || !driver.errors_.empty()) { + json = NULL; + size = _not(size_t); + } else { + std::ostringstream str; + driver.source_->Show(str); + std::string code(str.str()); + CYExecute_ execute = {pool, code.c_str()}; + [client performSelectorOnMainThread:@selector(execute:) withObject:[NSValue valueWithPointer:&execute] waitUntilDone:YES]; + json = execute.data_; + size = json == NULL ? _not(size_t) : strlen(json); + } - definition = kJSClassDefinitionEmpty; - definition.getProperty = &Global_getProperty; - JSClassRef Global(JSClassCreate(&definition)); + if (!CYSendAll(socket_, &size, sizeof(size))) + return; + if (json != NULL) + if (!CYSendAll(socket_, json, size)) + return; + } + } +}; - JSContextRef context(JSGlobalContextCreate(Global)); - Context_ = context; +static void * APR_THREAD_FUNC OnClient(apr_thread_t *thread, void *data) { + CYClient *client(reinterpret_cast(data)); + client->Handle(); + delete client; + return NULL; +} - JSObjectRef global(JSContextGetGlobalObject(context)); +extern "C" void CYHandleClient(apr_pool_t *pool, int socket) { + CYClient *client(new(pool) CYClient(socket)); + apr_threadattr_t *attr; + _aprcall(apr_threadattr_create(&attr, client->pool_)); + _aprcall(apr_thread_create(&client->thread_, attr, &OnClient, client, client->pool_)); +} - CYSetProperty(context, global, "ffi", JSObjectMakeConstructor(context, Functor_, &ffi)); +MSInitialize { _pooled + _aprcall(apr_initialize()); + _aprcall(apr_pool_create(&Pool_, NULL)); - CYSetProperty(context, global, "objc_msgSend", JSObjectMakeFunctionWithCallback(context, CYString("objc_msgSend"), &$objc_msgSend)); + Type_privateData::Object = new(Pool_) Type_privateData(Pool_, "@"); + Type_privateData::Selector = new(Pool_) Type_privateData(Pool_, ":"); - Bridge_ = [[NSMutableDictionary dictionaryWithContentsOfFile:@"/usr/lib/libcycript.plist"] retain]; + Bridge_ = [[NSMutableArray arrayWithContentsOfFile:@"/usr/lib/libcycript.plist"] retain]; - name_ = JSStringCreateWithUTF8CString("name"); - message_ = JSStringCreateWithUTF8CString("message"); - length_ = JSStringCreateWithUTF8CString("length"); + NSArray_ = objc_getClass("NSArray"); + NSCFBoolean_ = objc_getClass("NSCFBoolean"); + NSCFType_ = objc_getClass("NSCFType"); + NSMessageBuilder_ = objc_getClass("NSMessageBuilder"); + NSZombie_ = objc_getClass("_NSZombie_"); + Object_ = objc_getClass("Object"); +} + +JSGlobalContextRef CYGetJSContext() { + if (Context_ == NULL) { + JSClassDefinition definition; + + definition = kJSClassDefinitionEmpty; + definition.className = "Functor"; + definition.staticFunctions = Functor_staticFunctions; + definition.callAsFunction = &Functor_callAsFunction; + definition.finalize = &Finalize; + Functor_ = JSClassCreate(&definition); + + definition = kJSClassDefinitionEmpty; + definition.className = "Instance"; + definition.staticValues = Instance_staticValues; + definition.staticFunctions = Instance_staticFunctions; + definition.hasProperty = &Instance_hasProperty; + definition.getProperty = &Instance_getProperty; + definition.setProperty = &Instance_setProperty; + definition.deleteProperty = &Instance_deleteProperty; + definition.getPropertyNames = &Instance_getPropertyNames; + definition.callAsConstructor = &Instance_callAsConstructor; + definition.finalize = &Finalize; + Instance_ = JSClassCreate(&definition); + + definition = kJSClassDefinitionEmpty; + definition.className = "Internal"; + definition.staticFunctions = Internal_staticFunctions; + definition.hasProperty = &Internal_hasProperty; + definition.getProperty = &Internal_getProperty; + definition.setProperty = &Internal_setProperty; + definition.getPropertyNames = &Internal_getPropertyNames; + definition.finalize = &Finalize; + Internal_ = JSClassCreate(&definition); + + definition = kJSClassDefinitionEmpty; + definition.className = "Message"; + definition.staticFunctions = Functor_staticFunctions; + definition.callAsFunction = &Message_callAsFunction; + definition.finalize = &Finalize; + Message_ = JSClassCreate(&definition); + + definition = kJSClassDefinitionEmpty; + definition.className = "Pointer"; + definition.staticValues = Pointer_staticValues; + definition.staticFunctions = Pointer_staticFunctions; + definition.getProperty = &Pointer_getProperty; + definition.setProperty = &Pointer_setProperty; + definition.finalize = &Finalize; + Pointer_ = JSClassCreate(&definition); + + definition = kJSClassDefinitionEmpty; + definition.className = "Prototype"; + definition.hasProperty = &Prototype_hasProperty; + definition.getProperty = &Prototype_getProperty; + definition.setProperty = &Prototype_setProperty; +#if !__OBJC2__ + definition.deleteProperty = &Prototype_deleteProperty; +#endif + definition.getPropertyNames = &Prototype_getPropertyNames; + definition.finalize = &Finalize; + Prototype_ = JSClassCreate(&definition); + + definition = kJSClassDefinitionEmpty; + definition.className = "Selector"; + definition.staticValues = CYValue_staticValues; + definition.staticFunctions = Selector_staticFunctions; + definition.callAsFunction = &Selector_callAsFunction; + definition.finalize = &Finalize; + Selector_ = JSClassCreate(&definition); + + definition = kJSClassDefinitionEmpty; + definition.className = "Struct"; + definition.staticFunctions = Struct_staticFunctions; + definition.getProperty = &Struct_getProperty; + definition.setProperty = &Struct_setProperty; + definition.getPropertyNames = &Struct_getPropertyNames; + definition.finalize = &Finalize; + Struct_ = JSClassCreate(&definition); + + definition = kJSClassDefinitionEmpty; + definition.className = "Type"; + definition.staticFunctions = Type_staticFunctions; + //definition.getProperty = &Type_getProperty; + definition.callAsFunction = &Type_callAsFunction; + definition.callAsConstructor = &Type_callAsConstructor; + definition.finalize = &Finalize; + Type_ = JSClassCreate(&definition); + + definition = kJSClassDefinitionEmpty; + definition.className = "Runtime"; + definition.getProperty = &Runtime_getProperty; + Runtime_ = JSClassCreate(&definition); + + definition = kJSClassDefinitionEmpty; + definition.className = "ObjectiveC::Classes"; + definition.getProperty = &ObjectiveC_Classes_getProperty; + definition.getPropertyNames = &ObjectiveC_Classes_getPropertyNames; + ObjectiveC_Classes_ = JSClassCreate(&definition); + + definition = kJSClassDefinitionEmpty; + definition.className = "ObjectiveC::Images"; + definition.getProperty = &ObjectiveC_Images_getProperty; + definition.getPropertyNames = &ObjectiveC_Images_getPropertyNames; + ObjectiveC_Images_ = JSClassCreate(&definition); + + definition = kJSClassDefinitionEmpty; + definition.className = "ObjectiveC::Image::Classes"; + definition.getProperty = &ObjectiveC_Image_Classes_getProperty; + definition.getPropertyNames = &ObjectiveC_Image_Classes_getPropertyNames; + definition.finalize = &Finalize; + ObjectiveC_Image_Classes_ = JSClassCreate(&definition); + + definition = kJSClassDefinitionEmpty; + definition.className = "ObjectiveC::Protocols"; + definition.getProperty = &ObjectiveC_Protocols_getProperty; + definition.getPropertyNames = &ObjectiveC_Protocols_getPropertyNames; + ObjectiveC_Protocols_ = JSClassCreate(&definition); + + definition = kJSClassDefinitionEmpty; + //definition.getProperty = &Global_getProperty; + JSClassRef Global(JSClassCreate(&definition)); + + JSGlobalContextRef context(JSGlobalContextCreate(Global)); + Context_ = context; + + JSObjectRef global(CYGetGlobalObject(context)); + + JSObjectSetPrototype(context, global, JSObjectMake(context, Runtime_, NULL)); + ObjectiveC_ = JSObjectMake(context, NULL, NULL); + CYSetProperty(context, global, CYJSString("ObjectiveC"), ObjectiveC_); + + CYSetProperty(context, ObjectiveC_, CYJSString("classes"), JSObjectMake(context, ObjectiveC_Classes_, NULL)); + CYSetProperty(context, ObjectiveC_, CYJSString("images"), JSObjectMake(context, ObjectiveC_Images_, NULL)); + CYSetProperty(context, ObjectiveC_, CYJSString("protocols"), JSObjectMake(context, ObjectiveC_Protocols_, NULL)); + + Array_ = CYCastJSObject(context, CYGetProperty(context, global, CYJSString("Array"))); + Function_ = CYCastJSObject(context, CYGetProperty(context, global, CYJSString("Function"))); + + length_ = JSStringCreateWithUTF8CString("length"); + message_ = JSStringCreateWithUTF8CString("message"); + name_ = JSStringCreateWithUTF8CString("name"); + prototype_ = JSStringCreateWithUTF8CString("prototype"); + toCYON_ = JSStringCreateWithUTF8CString("toCYON"); + toJSON_ = JSStringCreateWithUTF8CString("toJSON"); + + Array_prototype_ = CYCastJSObject(context, CYGetProperty(context, Array_, prototype_)); + Array_pop_ = CYCastJSObject(context, CYGetProperty(context, Array_prototype_, CYJSString("pop"))); + Array_push_ = CYCastJSObject(context, CYGetProperty(context, Array_prototype_, CYJSString("push"))); + Array_splice_ = CYCastJSObject(context, CYGetProperty(context, Array_prototype_, CYJSString("splice"))); + + JSObjectRef Functor(JSObjectMakeConstructor(context, Functor_, &Functor_new)); + JSObjectRef Message(JSObjectMakeConstructor(context, Message_, NULL)); + JSObjectRef Selector(JSObjectMakeConstructor(context, Selector_, &Selector_new)); + + JSValueRef function(CYGetProperty(context, Function_, prototype_)); + JSObjectSetPrototype(context, (JSObjectRef) CYGetProperty(context, Message, prototype_), function); + JSObjectSetPrototype(context, (JSObjectRef) CYGetProperty(context, Functor, prototype_), function); + JSObjectSetPrototype(context, (JSObjectRef) CYGetProperty(context, Selector, prototype_), function); + + CYSetProperty(context, global, CYJSString("Functor"), Functor); + CYSetProperty(context, global, CYJSString("Instance"), JSObjectMakeConstructor(context, Instance_, &Instance_new)); + CYSetProperty(context, global, CYJSString("Pointer"), JSObjectMakeConstructor(context, Pointer_, &Pointer_new)); + CYSetProperty(context, global, CYJSString("Selector"), Selector); + CYSetProperty(context, global, CYJSString("Type"), JSObjectMakeConstructor(context, Type_, &Type_new)); + + MSHookFunction(&objc_registerClassPair, MSHake(objc_registerClassPair)); + + class_addMethod(NSCFType_, @selector(cy$toJSON:), reinterpret_cast(&NSCFType$cy$toJSON), "@12@0:4@8"); + + CYSetProperty(context, global, CYJSString("objc_registerClassPair"), JSObjectMakeFunctionWithCallback(context, CYJSString("objc_registerClassPair"), &objc_registerClassPair_)); + CYSetProperty(context, global, CYJSString("objc_msgSend"), JSObjectMakeFunctionWithCallback(context, CYJSString("objc_msgSend"), &$objc_msgSend)); + + System_ = JSObjectMake(context, NULL, NULL); + CYSetProperty(context, global, CYJSString("system"), System_); + CYSetProperty(context, System_, CYJSString("args"), CYJSNull(context)); + //CYSetProperty(context, System_, CYJSString("global"), global); + + CYSetProperty(context, System_, CYJSString("print"), JSObjectMakeFunctionWithCallback(context, CYJSString("print"), &System_print)); + + Result_ = JSStringCreateWithUTF8CString("_"); + } - JSValueRef exception(NULL); - JSValueRef value(JSObjectGetProperty(JSGetContext(), global, CYString("Array"), &exception)); - CYThrow(context, exception); - Array_ = JSValueToObject(JSGetContext(), value, &exception); - CYThrow(context, exception); + return Context_; }