X-Git-Url: https://git.saurik.com/cycript.git/blobdiff_plain/9a2db8b27264e51767f216b16a1ef559356ba437..fa389b0ff91fdcf932a5ea1c6d5d34edea583d03:/Library.mm?ds=sidebyside diff --git a/Library.mm b/Library.mm index 8116d53..3357080 100644 --- a/Library.mm +++ b/Library.mm @@ -51,6 +51,7 @@ #include #include +#include #include #include @@ -66,7 +67,7 @@ #include "Parser.hpp" #include "Cycript.tab.hh" -#include +#include #undef _assert #undef _trace @@ -96,6 +97,123 @@ } \ } +void CYThrow(JSContextRef context, JSValueRef value); + +const char *CYPoolCCYON(apr_pool_t *pool, JSContextRef context, JSValueRef value, JSValueRef *exception); +JSStringRef CYCopyJSString(const char *value); + +void CYSetProperty(JSContextRef context, JSObjectRef object, JSStringRef name, JSValueRef value); + +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)()); +JSValueRef CYSendMessage(apr_pool_t *pool, JSContextRef context, id self, SEL _cmd, size_t count, const JSValueRef arguments[], bool initialize, JSValueRef *exception); + +/* JavaScript Properties {{{ */ +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); +} +/* }}} */ +/* JavaScript Strings {{{ */ +JSStringRef CYCopyJSString(id value) { + // XXX: this definition scares me; is anyone using this?! + 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)); +} + +/* }}} */ + static JSGlobalContextRef Context_; static JSObjectRef System_; static JSObjectRef ObjectiveC_; @@ -104,8 +222,9 @@ static JSClassRef Functor_; static JSClassRef Instance_; static JSClassRef Internal_; static JSClassRef Message_; +static JSClassRef Messages_; +static JSClassRef NSArrayPrototype_; static JSClassRef Pointer_; -static JSClassRef Prototype_; static JSClassRef Runtime_; static JSClassRef Selector_; static JSClassRef Struct_; @@ -118,6 +237,7 @@ static JSClassRef ObjectiveC_Protocols_; static JSObjectRef Array_; static JSObjectRef Function_; +static JSObjectRef String_; static JSStringRef Result_; @@ -128,6 +248,9 @@ static JSStringRef prototype_; static JSStringRef toCYON_; static JSStringRef toJSON_; +static JSObjectRef Instance_prototype_; +static JSObjectRef Object_prototype_; + static JSObjectRef Array_prototype_; static JSObjectRef Array_pop_; static JSObjectRef Array_push_; @@ -136,6 +259,7 @@ static JSObjectRef Array_splice_; static Class NSArray_; static Class NSCFBoolean_; static Class NSCFType_; +static Class NSDictionary_; static Class NSMessageBuilder_; static Class NSZombie_; static Class Object_; @@ -186,6 +310,37 @@ struct Selector_privateData : virtual Type_privateData *GetType() const; }; +// XXX: trick this out with associated objects! +JSValueRef CYGetClassPrototype(JSContextRef context, id self) { + if (self == nil) + return Instance_prototype_; + + // XXX: I need to think through multi-context + typedef std::map CacheMap; + static CacheMap cache_; + + JSValueRef &value(cache_[self]); + if (value != NULL) + return value; + + JSClassRef _class(NULL); + JSValueRef prototype; + + if (self == NSArray_) + prototype = Array_prototype_; + else if (self == NSDictionary_) + prototype = Object_prototype_; + else + prototype = CYGetClassPrototype(context, class_getSuperclass(self)); + + JSObjectRef object(JSObjectMake(context, _class, NULL)); + JSObjectSetPrototype(context, object, prototype); + + JSValueProtect(context, object); + value = object; + return object; +} + struct Instance : CYValue { @@ -206,17 +361,13 @@ struct Instance : virtual ~Instance() { if ((flags_ & Transient) == 0) // XXX: does this handle background threads correctly? + // XXX: this simply does not work on the console because I'm stupid [GetValue() performSelector:@selector(release) withObject:nil afterDelay:0]; } 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; - } + JSObjectSetPrototype(context, value, CYGetClassPrototype(context, object == nil ? nil : object_getClass(object))); return value; } @@ -231,20 +382,20 @@ struct Instance : virtual Type_privateData *GetType() const; }; -struct Prototype : +struct Messages : CYValue { - Prototype(Class value) : + Messages(Class value) : CYValue(value) { } static JSObjectRef Make(JSContextRef context, Class _class, bool array = false) { - JSObjectRef value(JSObjectMake(context, Prototype_, new Prototype(_class))); + JSObjectRef value(JSObjectMake(context, Messages_, new Messages(_class))); if (_class == NSArray_) array = true; if (Class super = class_getSuperclass(_class)) - JSObjectSetPrototype(context, value, Prototype::Make(context, super, array)); + JSObjectSetPrototype(context, value, Messages::Make(context, super, array)); /*else if (array) JSObjectSetPrototype(context, value, Array_prototype_);*/ return value; @@ -255,19 +406,41 @@ struct Prototype : } }; -struct Internal : +struct CYOwned : CYValue { + private: + JSContextRef context_; JSObjectRef owner_; - Internal(id value, JSObjectRef owner) : + public: + CYOwned(void *value, JSContextRef context, JSObjectRef owner) : CYValue(value), + context_(context), owner_(owner) + { + JSValueProtect(context_, owner_); + } + + virtual ~CYOwned() { + JSValueUnprotect(context_, owner_); + } + + JSObjectRef GetOwner() const { + return owner_; + } +}; + +struct Internal : + CYOwned +{ + Internal(id value, JSContextRef context, JSObjectRef owner) : + CYOwned(value, context, owner) { } static JSObjectRef Make(JSContextRef context, id object, JSObjectRef owner) { - return JSObjectMake(context, Internal_, new Internal(object, owner)); + return JSObjectMake(context, Internal_, new Internal(object, context, owner)); } id GetValue() const { @@ -306,9 +479,14 @@ void Copy(apr_pool_t *pool, Type &lhs, Type &rhs) { 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); + sig::Type *&lht(lhs.data.data.type); + sig::Type *&rht(rhs.data.data.type); + + if (rht == NULL) + lht = NULL; + else { + lht = new(pool) Type; + Copy(pool, *lht, *rht); } lhs.data.data.size = rhs.data.data.size; @@ -440,27 +618,24 @@ Type_privateData *Selector_privateData::GetType() const { } struct Pointer : - CYValue + CYOwned { - JSObjectRef owner_; Type_privateData *type_; - Pointer(void *value, sig::Type *type, JSObjectRef owner) : - CYValue(value), - owner_(owner), + Pointer(void *value, JSContextRef context, JSObjectRef owner, sig::Type *type) : + CYOwned(value, context, owner), type_(new(pool_) Type_privateData(type)) { } }; struct Struct_privateData : - CYValue + CYOwned { - JSObjectRef owner_; Type_privateData *type_; - Struct_privateData(JSObjectRef owner) : - owner_(owner) + Struct_privateData(JSContextRef context, JSObjectRef owner) : + CYOwned(NULL, context, owner) { } }; @@ -469,7 +644,7 @@ 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)); + Struct_privateData *internal(new Struct_privateData(context, owner)); apr_pool_t *pool(internal->pool_); Type_privateData *typical(new(pool) Type_privateData(type, ffi)); internal->type_ = typical; @@ -511,9 +686,16 @@ struct Closure_privateData : JSContextRef context_; JSObjectRef function_; - Closure_privateData(const char *type) : - Functor_privateData(type, NULL) + Closure_privateData(JSContextRef context, JSObjectRef function, const char *type) : + Functor_privateData(type, NULL), + context_(context), + function_(function) { + JSValueProtect(context_, function_); + } + + virtual ~Closure_privateData() { + JSValueUnprotect(context_, function_); } }; @@ -589,10 +771,17 @@ size_t CYGetIndex(const char *value) { return _not(size_t); } +// XXX: fix this +static const char *CYPoolCString(apr_pool_t *pool, JSStringRef value); + size_t CYGetIndex(apr_pool_t *pool, NSString *value) { return CYGetIndex(CYPoolCString(pool, value)); } +size_t CYGetIndex(apr_pool_t *pool, JSStringRef value) { + return CYGetIndex(CYPoolCString(pool, value)); +} + bool CYGetOffset(const char *value, ssize_t &index) { if (value[0] != '0') { char *end; @@ -727,98 +916,11 @@ struct PropertyAttributes { }; -@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 - +/* Bridge: NSArray {{{ */ @implementation NSArray (Cycript) - (NSString *) cy$toCYON { @@ -866,7 +968,41 @@ NSString *NSCFType$cy$toJSON(id self, SEL sel, NSString *key) { } @end +/* }}} */ +/* Bridge: NSDictionary {{{ */ +@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 +/* }}} */ +/* Bridge: NSMutableArray {{{ */ @implementation NSMutableArray (Cycript) - (bool) cy$setProperty:(NSString *)name to:(NSObject *)value { @@ -915,39 +1051,8 @@ NSString *NSCFType$cy$toJSON(id self, SEL sel, NSString *key) { } @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 - +/* }}} */ +/* Bridge: NSMutableDictionary {{{ */ @implementation NSMutableDictionary (Cycript) - (bool) cy$setProperty:(NSString *)name to:(NSObject *)value { @@ -965,7 +1070,8 @@ NSString *NSCFType$cy$toJSON(id self, SEL sel, NSString *key) { } @end - +/* }}} */ +/* Bridge: NSNumber {{{ */ @implementation NSNumber (Cycript) - (JSType) cy$JSType { @@ -986,7 +1092,79 @@ NSString *NSCFType$cy$toJSON(id self, SEL sel, NSString *key) { } @end - +/* }}} */ +/* Bridge: NSNull {{{ */ +@implementation NSNull (Cycript) + +- (JSType) cy$JSType { + return kJSTypeNull; +} + +- (NSObject *) cy$toJSON:(NSString *)key { + return self; +} + +- (NSString *) cy$toCYON { + return @"null"; +} + +@end +/* }}} */ +/* Bridge: NSObject {{{ */ +@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 +/* }}} */ +/* Bridge: NSProxy {{{ */ +@implementation NSProxy (Cycript) + +- (NSObject *) cy$toJSON:(NSString *)key { + return [self description]; +} + +- (NSString *) cy$toCYON { + return [[self cy$toJSON:@""] cy$toCYON]; +} + +@end +/* }}} */ +/* Bridge: NSString {{{ */ @implementation NSString (Cycript) - (JSType) cy$JSType { @@ -1044,6 +1222,28 @@ NSString *NSCFType$cy$toJSON(id self, SEL sel, NSString *key) { } @end +/* }}} */ +/* Bridge: WebUndefined {{{ */ +@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 +/* }}} */ @interface CYJSObject : NSMutableDictionary { JSObjectRef object_; @@ -1080,10 +1280,6 @@ NSString *NSCFType$cy$toJSON(id self, SEL sel, NSString *key) { @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 \ @@ -1092,8 +1288,6 @@ CYRange WordEndRange_ (0x3ff001000000000LLU, 0x7fffffe87fffffeLLU); // A-Za-z_$ return NULL; \ } -void CYThrow(JSContextRef context, JSValueRef value); - apr_status_t CYPoolRelease_(void *data) { id object(reinterpret_cast(data)); [object release]; @@ -1132,82 +1326,6 @@ id CYCastNSObject(apr_pool_t *pool, JSContextRef context, JSObjectRef object) { } } -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)); @@ -1351,32 +1469,6 @@ JSObjectRef CYCastJSObject(JSContextRef context, JSValueRef value) { 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; @@ -1401,9 +1493,15 @@ bool CYIsCallable(JSContextRef context, JSValueRef value) { if ((self = [super init]) != nil) { object_ = object; context_ = context; + JSValueProtect(context_, object_); } return self; } +- (void) dealloc { + JSValueUnprotect(context_, object_); + [super dealloc]; +} + - (NSObject *) cy$toJSON:(NSString *)key { JSValueRef toJSON(CYGetProperty(context_, object_, toJSON_)); if (!CYIsCallable(context_, toJSON)) @@ -1418,12 +1516,11 @@ bool CYIsCallable(JSContextRef context, JSValueRef value) { - (NSString *) cy$toCYON { JSValueRef toCYON(CYGetProperty(context_, object_, toCYON_)); - if (!CYIsCallable(context_, toCYON)) + if (!CYIsCallable(context_, toCYON)) super: return [super cy$toCYON]; - else { - JSValueRef value(CYCallAsFunction(context_, (JSObjectRef) toCYON, object_, 0, NULL)); + else if (JSValueRef value = CYCallAsFunction(context_, (JSObjectRef) toCYON, object_, 0, NULL)) return CYCastNSString(NULL, CYJSString(context_, value)); - } + else goto super; } - (NSUInteger) count { @@ -1434,7 +1531,10 @@ bool CYIsCallable(JSContextRef context, JSValueRef value) { } - (id) objectForKey:(id)key { - return CYCastNSObject(NULL, context_, CYGetProperty(context_, object_, CYJSString(key))) ?: [NSNull null]; + JSValueRef value(CYGetProperty(context_, object_, CYJSString(key))); + if (JSValueIsUndefined(context_, value)) + return nil; + return CYCastNSObject(NULL, context_, value) ?: [NSNull null]; } - (NSEnumerator *) keyEnumerator { @@ -1462,9 +1562,15 @@ bool CYIsCallable(JSContextRef context, JSValueRef value) { if ((self = [super init]) != nil) { object_ = object; context_ = context; + JSValueProtect(context_, object_); } return self; } +- (void) dealloc { + JSValueUnprotect(context_, object_); + [super dealloc]; +} + - (NSUInteger) count { return CYCastDouble(context_, CYGetProperty(context_, object_, length_)); } @@ -1642,23 +1748,24 @@ struct CYInternal : } }; -JSObjectRef CYMakeSelector(JSContextRef context, SEL sel) { +static 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)); +static JSObjectRef CYMakePointer(JSContextRef context, void *pointer, sig::Type *type, ffi_type *ffi, JSObjectRef owner) { + Pointer *internal(new Pointer(pointer, context, owner, type)); return JSObjectMake(context, Pointer_, internal); } -JSObjectRef CYMakeFunctor(JSContextRef context, void (*function)(), const char *type) { +static 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) { +static const char *CYPoolCString(apr_pool_t *pool, JSStringRef value) { if (pool == NULL) { + // XXX: this could be much more efficient const char *string([CYCastNSString(NULL, value) UTF8String]); return string; } else { @@ -1669,11 +1776,11 @@ const char *CYPoolCString(apr_pool_t *pool, JSStringRef value) { } } -const char *CYPoolCString(apr_pool_t *pool, JSContextRef context, JSValueRef value) { +static 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) { +static bool CYGetOffset(apr_pool_t *pool, JSStringRef value, ssize_t &index) { return CYGetOffset(CYPoolCString(pool, value), index); } @@ -1692,7 +1799,7 @@ bool CYGetOffset(apr_pool_t *pool, JSStringRef value, ssize_t &index) { utf8; \ }) -void *CYCastPointer_(JSContextRef context, JSValueRef value) { +static void *CYCastPointer_(JSContextRef context, JSValueRef value) { switch (JSValueGetType(context, value)) { case kJSTypeNull: return NULL; @@ -1712,11 +1819,11 @@ void *CYCastPointer_(JSContextRef context, JSValueRef value) { } template -_finline Type_ CYCastPointer(JSContextRef context, JSValueRef value) { +static _finline Type_ CYCastPointer(JSContextRef context, JSValueRef value) { return reinterpret_cast(CYCastPointer_(context, value)); } -SEL CYCastSEL(JSContextRef context, JSValueRef value) { +static SEL CYCastSEL(JSContextRef context, JSValueRef value) { if (JSValueIsObjectOfClass(context, value, Selector_)) { Selector_privateData *internal(reinterpret_cast(JSObjectGetPrivate((JSObjectRef) value))); return reinterpret_cast(internal->value_); @@ -1724,7 +1831,7 @@ SEL CYCastSEL(JSContextRef context, JSValueRef value) { return CYCastPointer(context, value); } -void CYPoolFFI(apr_pool_t *pool, JSContextRef context, sig::Type *type, ffi_type *ffi, void *data, JSValueRef value) { +static 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); @@ -1802,7 +1909,7 @@ void CYPoolFFI(apr_pool_t *pool, JSContextRef context, sig::Type *type, ffi_type } } -JSValueRef CYFromFFI(JSContextRef context, sig::Type *type, ffi_type *ffi, void *data, bool initialize = false, JSObjectRef owner = NULL) { +static JSValueRef CYFromFFI(JSContextRef context, sig::Type *type, ffi_type *ffi, void *data, bool initialize = false, JSObjectRef owner = NULL) { JSValueRef value; switch (type->primitive) { @@ -1892,7 +1999,7 @@ static bool CYImplements(id object, Class _class, SEL selector, bool devoid) { return false; } -const char *CYPoolTypeEncoding(apr_pool_t *pool, Class _class, SEL sel, Method method) { +static 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))]) @@ -1901,7 +2008,7 @@ const char *CYPoolTypeEncoding(apr_pool_t *pool, Class _class, SEL sel, Method m return NULL; } -void FunctionClosure_(ffi_cif *cif, void *result, void **arguments, void *arg) { +static void FunctionClosure_(ffi_cif *cif, void *result, void **arguments, void *arg) { Closure_privateData *internal(reinterpret_cast(arg)); JSContextRef context(internal->context_); @@ -1916,7 +2023,7 @@ void FunctionClosure_(ffi_cif *cif, void *result, void **arguments, void *arg) { CYPoolFFI(NULL, context, internal->signature_.elements[0].type, internal->cif_.rtype, result, value); } -void MessageClosure_(ffi_cif *cif, void *result, void **arguments, void *arg) { +static void MessageClosure_(ffi_cif *cif, void *result, void **arguments, void *arg) { Closure_privateData *internal(reinterpret_cast(arg)); JSContextRef context(internal->context_); @@ -1933,10 +2040,10 @@ void MessageClosure_(ffi_cif *cif, void *result, void **arguments, void *arg) { CYPoolFFI(NULL, context, internal->signature_.elements[0].type, internal->cif_.rtype, result, value); } -Closure_privateData *CYMakeFunctor_(JSContextRef context, JSObjectRef function, const char *type, void (*callback)(ffi_cif *, void *, void **, void *)) { +static 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)); + Closure_privateData *internal(new Closure_privateData(CYGetJSContext(), function, type)); ffi_closure *closure((ffi_closure *) _syscall(mmap( NULL, sizeof(ffi_closure), @@ -1951,13 +2058,10 @@ Closure_privateData *CYMakeFunctor_(JSContextRef context, JSObjectRef function, internal->value_ = closure; - internal->context_ = CYGetJSContext(); - internal->function_ = function; - return internal; } -JSObjectRef CYMakeFunctor(JSContextRef context, JSObjectRef function, const char *type) { +static JSObjectRef CYMakeFunctor(JSContextRef context, JSObjectRef function, const char *type) { Closure_privateData *internal(CYMakeFunctor_(context, function, type, &FunctionClosure_)); return JSObjectMake(context, Functor_, internal); } @@ -1987,8 +2091,8 @@ static IMP CYMakeMessage(JSContextRef context, JSValueRef value, const char *typ return reinterpret_cast(internal->GetValue()); } -static bool Prototype_hasProperty(JSContextRef context, JSObjectRef object, JSStringRef property) { - Prototype *internal(reinterpret_cast(JSObjectGetPrivate(object))); +static bool Messages_hasProperty(JSContextRef context, JSObjectRef object, JSStringRef property) { + Messages *internal(reinterpret_cast(JSObjectGetPrivate(object))); Class _class(internal->GetValue()); CYPool pool; @@ -2001,8 +2105,8 @@ static bool Prototype_hasProperty(JSContextRef context, JSObjectRef object, JSSt return false; } -static JSValueRef Prototype_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { - Prototype *internal(reinterpret_cast(JSObjectGetPrivate(object))); +static JSValueRef Messages_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { + Messages *internal(reinterpret_cast(JSObjectGetPrivate(object))); Class _class(internal->GetValue()); CYPool pool; @@ -2015,8 +2119,8 @@ static JSValueRef Prototype_getProperty(JSContextRef context, JSObjectRef object return NULL; } -static bool Prototype_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) { - Prototype *internal(reinterpret_cast(JSObjectGetPrivate(object))); +static bool Messages_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) { + Messages *internal(reinterpret_cast(JSObjectGetPrivate(object))); Class _class(internal->GetValue()); CYPool pool; @@ -2047,8 +2151,8 @@ static bool Prototype_setProperty(JSContextRef context, JSObjectRef object, JSSt } #if !__OBJC2__ -static bool Prototype_deleteProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { - Prototype *internal(reinterpret_cast(JSObjectGetPrivate(object))); +static bool Messages_deleteProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { + Messages *internal(reinterpret_cast(JSObjectGetPrivate(object))); Class _class(internal->GetValue()); CYPool pool; @@ -2065,8 +2169,8 @@ static bool Prototype_deleteProperty(JSContextRef context, JSObjectRef object, J } #endif -static void Prototype_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) { - Prototype *internal(reinterpret_cast(JSObjectGetPrivate(object))); +static void Messages_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) { + Messages *internal(reinterpret_cast(JSObjectGetPrivate(object))); Class _class(internal->GetValue()); unsigned int size; @@ -2090,13 +2194,16 @@ static bool Instance_hasProperty(JSContextRef context, JSObjectRef object, JSStr if (internal->HasProperty(context, property)) return true; + Class _class(object_getClass(self)); + CYPoolTry { - if ([self cy$hasProperty:name]) - return true; + // XXX: this is an evil hack to deal with NSProxy; fix elsewhere + if (CYImplements(self, _class, @selector(cy$hasProperty:), false)) + if ([self cy$hasProperty:name]) + return true; } CYPoolCatch(false) const char *string(CYPoolCString(pool, name)); - Class _class(object_getClass(self)); if (class_getProperty(_class, string) != NULL) return true; @@ -2240,6 +2347,28 @@ static JSObjectRef Instance_callAsConstructor(JSContextRef context, JSObjectRef } CYCatch } +static bool CYIsClass(id self) { + // XXX: this is a lame object_isClass + return class_getInstanceMethod(object_getClass(self), @selector(alloc)) != NULL; +} + +static bool Instance_hasInstance(JSContextRef context, JSObjectRef constructor, JSValueRef instance, JSValueRef *exception) { + Instance *internal(reinterpret_cast(JSObjectGetPrivate((JSObjectRef) constructor))); + Class _class(internal->GetValue()); + if (!CYIsClass(_class)) + return false; + + if (JSValueIsObjectOfClass(context, instance, Instance_)) { + Instance *linternal(reinterpret_cast(JSObjectGetPrivate((JSObjectRef) instance))); + // XXX: this isn't always safe + CYTry { + return [linternal->GetValue() isKindOfClass:_class]; + } CYCatch + } + + return false; +} + static bool Internal_hasProperty(JSContextRef context, JSObjectRef object, JSStringRef property) { Internal *internal(reinterpret_cast(JSObjectGetPrivate(object))); CYPool pool; @@ -2311,10 +2440,10 @@ static void Internal_getPropertyNames(JSContextRef context, JSObjectRef object, 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_; + return internal->GetOwner(); } -bool Index_(apr_pool_t *pool, Struct_privateData *internal, JSStringRef property, ssize_t &index, uint8_t *&base) { +static 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) @@ -2366,7 +2495,7 @@ static JSValueRef Pointer_getIndex(JSContextRef context, JSObjectRef object, siz uint8_t *base(reinterpret_cast(internal->value_)); base += ffi->size * index; - JSObjectRef owner(internal->owner_ ?: object); + JSObjectRef owner(internal->GetOwner() ?: object); CYTry { return CYFromFFI(context, typical->type_, ffi, base, false, owner); @@ -2443,7 +2572,7 @@ static JSValueRef Struct_getProperty(JSContextRef context, JSObjectRef object, J if (!Index_(pool, internal, property, index, base)) return NULL; - JSObjectRef owner(internal->owner_ ?: object); + JSObjectRef owner(internal->GetOwner() ?: object); CYTry { return CYFromFFI(context, typical->type_->data.signature.elements[index].type, typical->GetFFI()->elements[index], base, false, owner); @@ -2629,6 +2758,16 @@ static void ObjectiveC_Protocols_getPropertyNames(JSContextRef context, JSObject free(data); } +static JSObjectRef CYMakeType(JSContextRef context, const char *type) { + Type_privateData *internal(new Type_privateData(NULL, type)); + return JSObjectMake(context, Type_, internal); +} + +static JSObjectRef CYMakeType(JSContextRef context, sig::Type *type) { + Type_privateData *internal(new Type_privateData(type)); + return JSObjectMake(context, Type_, internal); +} + static JSValueRef Runtime_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { if (JSStringIsEqualToUTF8CString(property, "nil")) return Instance::Make(context, nil); @@ -2652,11 +2791,17 @@ static JSValueRef Runtime_getProperty(JSContextRef context, JSObjectRef object, sig::sig_ffi_cif(pool, &sig::ObjectiveC, &signature, &cif); return CYFromFFI(context, signature.elements[0].type, cif.rtype, [name cy$symbol]); } + if (NSMutableArray *entry = [[Bridge_ objectAtIndex:2] objectForKey:name]) + switch ([[entry objectAtIndex:0] intValue]) { + // XXX: implement case 0 + case 1: + return CYMakeType(context, CYPoolCString(pool, [entry objectAtIndex:1])); + } return NULL; } CYCatch } -bool stret(ffi_type *ffi_type) { +static bool stret(ffi_type *ffi_type) { return ffi_type->type == FFI_TYPE_STRUCT && ( ffi_type->size > OBJC_MAX_STRUCT_BY_VALUE || struct_forward_array[ffi_type->size] != 0 @@ -2666,12 +2811,14 @@ bool stret(ffi_type *ffi_type) { 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])); + if (count == 0) + NSLog(@""); + else + NSLog(@"%s", CYCastCString(context, arguments[0])); return CYJSUndefined(context); } CYCatch } @@ -2707,6 +2854,14 @@ JSValueRef CYSendMessage(apr_pool_t *pool, JSContextRef context, id self, SEL _c return CYCallFunction(pool, context, 2, setup, count, arguments, initialize, exception, &signature, &cif, function); } +static size_t Nonce_(0); + +static JSValueRef $cyq(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { + char name[16]; + sprintf(name, "%s%zu", CYCastCString(context, arguments[0]), Nonce_++); + return CYCastJSValue(context, name); +} + static JSValueRef $objc_msgSend(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYPool pool; @@ -2739,6 +2894,9 @@ static JSValueRef $objc_msgSend(JSContextRef context, JSObjectRef object, JSObje return CYSendMessage(pool, context, self, _cmd, count - 2, arguments + 2, uninitialized, exception); } +/* Hook: objc_registerClassPair {{{ */ +// XXX: replace this with associated objects + MSHook(void, CYDealloc, id self, SEL sel) { CYInternal *internal; object_getInstanceVariable(self, "cy$internal_", reinterpret_cast(&internal)); @@ -2767,6 +2925,12 @@ static JSValueRef objc_registerClassPair_(JSContextRef context, JSObjectRef obje return CYJSUndefined(context); } CYCatch } +/* }}} */ + +static JSValueRef Cycript_gc_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { + JSGarbageCollect(context); + return CYJSUndefined(context); +} static JSValueRef Selector_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { JSValueRef setup[count + 2]; @@ -2796,7 +2960,7 @@ static JSValueRef Functor_callAsFunction(JSContextRef context, JSObjectRef objec return CYCallFunction(pool, context, 0, NULL, count, arguments, false, exception, &internal->signature_, &internal->cif_, internal->GetValue()); } -JSObjectRef Selector_new(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { +static 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]; @@ -2805,7 +2969,7 @@ JSObjectRef Selector_new(JSContextRef context, JSObjectRef object, size_t count, } CYCatch } -JSObjectRef Pointer_new(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { +static 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 Functor constructor" userInfo:nil]; @@ -2822,25 +2986,47 @@ JSObjectRef Pointer_new(JSContextRef context, JSObjectRef object, size_t count, } CYCatch } -JSObjectRef CYMakeType(JSContextRef context, JSObjectRef object, const char *type) { - Type_privateData *internal(new Type_privateData(NULL, type)); - return JSObjectMake(context, Type_, internal); -} - -JSObjectRef Type_new(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { +static 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); + return CYMakeType(context, type); + } CYCatch +} + +static JSValueRef Type_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { + Type_privateData *internal(reinterpret_cast(JSObjectGetPrivate(object))); + + CYTry { + sig::Type type; + + if (JSStringIsEqualToUTF8CString(property, "$cyi")) { + type.primitive = sig::pointer_P; + type.data.data.size = 0; + } else { + size_t index(CYGetIndex(NULL, property)); + if (index == _not(size_t)) + return NULL; + type.primitive = sig::array_P; + type.data.data.size = index; + } + + type.name = NULL; + type.flags = 0; + + type.data.data.type = internal->type_; + + return CYMakeType(context, &type); } CYCatch } static JSValueRef Type_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { + Type_privateData *internal(reinterpret_cast(JSObjectGetPrivate(object))); + 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? @@ -2853,17 +3039,26 @@ static JSValueRef Type_callAsFunction(JSContextRef context, JSObjectRef object, static JSObjectRef Type_callAsConstructor(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { - if (count > 1) + if (count != 0) @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); + + sig::Type *type(internal->type_); + size_t size; + + if (type->primitive != sig::array_P) + size = 0; + else { + size = type->data.data.size; + type = type->data.data.type; + } + + void *value(malloc(internal->GetFFI()->size)); + return CYMakePointer(context, value, type, NULL, NULL); } CYCatch } -JSObjectRef Instance_new(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { +static 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]; @@ -2872,7 +3067,7 @@ JSObjectRef Instance_new(JSContextRef context, JSObjectRef object, size_t count, } CYCatch } -JSObjectRef Functor_new(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { +static 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]; @@ -2881,7 +3076,7 @@ JSObjectRef Functor_new(JSContextRef context, JSObjectRef object, size_t count, } CYCatch } -JSValueRef CYValue_getProperty_value(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { +static 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_)); } @@ -2931,16 +3126,28 @@ static JSValueRef Instance_getProperty_constructor(JSContextRef context, JSObjec return Instance::Make(context, object_getClass(internal->GetValue())); } -static JSValueRef Instance_getProperty_prototype(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { +static JSValueRef Instance_getProperty_protocol(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { + Instance *internal(reinterpret_cast(JSObjectGetPrivate(object))); + id self(internal->GetValue()); + if (!CYIsClass(self)) + return CYJSUndefined(context); + CYTry { + return CYGetClassPrototype(context, self); + } CYCatch +} + +static JSValueRef Instance_getProperty_messages(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); + return Messages::Make(context, self); } static JSValueRef Instance_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { + if (!JSValueIsObjectOfClass(context, _this, Instance_)) + return NULL; + Instance *internal(reinterpret_cast(JSObjectGetPrivate(_this))); CYTry { @@ -2951,17 +3158,24 @@ static JSValueRef Instance_callAsFunction_toCYON(JSContextRef context, JSObjectR } static JSValueRef Instance_callAsFunction_toJSON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { + if (!JSValueIsObjectOfClass(context, _this, Instance_)) + return NULL; + Instance *internal(reinterpret_cast(JSObjectGetPrivate(_this))); CYTry { CYPoolTry { NSString *key(count == 0 ? nil : CYCastNSString(NULL, CYJSString(context, arguments[0]))); + // XXX: check for support of cy$toJSON? return CYCastJSValue(context, CYJSString([internal->GetValue() cy$toJSON:key])); } CYPoolCatch(NULL) } CYCatch } static JSValueRef Instance_callAsFunction_toString(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { + if (!JSValueIsObjectOfClass(context, _this, Instance_)) + return NULL; + Instance *internal(reinterpret_cast(JSObjectGetPrivate(_this))); CYTry { @@ -3063,9 +3277,10 @@ static JSStaticFunction Functor_staticFunctions[4] = { {NULL, NULL, 0} }; -static JSStaticValue Instance_staticValues[4] = { +static JSStaticValue Instance_staticValues[5] = { {"constructor", &Instance_getProperty_constructor, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, - {"prototype", &Instance_getProperty_prototype, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, + {"messages", &Instance_getProperty_messages, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, + {"prototype", &Instance_getProperty_protocol, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, {"value", &CYValue_getProperty_value, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, {NULL, NULL, NULL, 0} }; @@ -3098,28 +3313,6 @@ static JSStaticFunction Type_staticFunctions[4] = { {NULL, NULL, 0} }; -CYDriver::CYDriver(const std::string &filename) : - state_(CYClear), - data_(NULL), - size_(0), - file_(NULL), - filename_(filename), - source_(NULL) -{ - ScannerInit(); -} - -CYDriver::~CYDriver() { - ScannerDestroy(); -} - -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]; @@ -3153,7 +3346,14 @@ const char *CYExecute(apr_pool_t *pool, const char *code) { _pooled if (JSValueIsUndefined(context, result)) return NULL; - const char *json(CYPoolCCYON(pool, context, result, &exception)); + const char *json; + + try { + json = CYPoolCCYON(pool, context, result, &exception); + } catch (const char *error) { + return error; + } + if (exception != NULL) goto error; @@ -3161,25 +3361,7 @@ const char *CYExecute(apr_pool_t *pool, const char *code) { _pooled return json; } -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; -} - -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_; +static apr_pool_t *Pool_; struct CYExecute_ { apr_pool_t *pool_; @@ -3245,8 +3427,11 @@ struct CYClient : json = NULL; size = _not(size_t); } else { + CYContext context(driver.pool_); + driver.program_->Replace(context); std::ostringstream str; - driver.source_->Show(str); + CYOutput out(str); + out << *driver.program_; std::string code(str.str()); CYExecute_ execute = {pool, code.c_str()}; [client performSelectorOnMainThread:@selector(execute:) withObject:[NSValue valueWithPointer:&execute] waitUntilDone:YES]; @@ -3289,6 +3474,7 @@ MSInitialize { _pooled NSArray_ = objc_getClass("NSArray"); NSCFBoolean_ = objc_getClass("NSCFBoolean"); NSCFType_ = objc_getClass("NSCFType"); + NSDictionary_ = objc_getClass("NSDictonary"); NSMessageBuilder_ = objc_getClass("NSMessageBuilder"); NSZombie_ = objc_getClass("_NSZombie_"); Object_ = objc_getClass("Object"); @@ -3315,6 +3501,7 @@ JSGlobalContextRef CYGetJSContext() { definition.deleteProperty = &Instance_deleteProperty; definition.getPropertyNames = &Instance_getPropertyNames; definition.callAsConstructor = &Instance_callAsConstructor; + definition.hasInstance = &Instance_hasInstance; definition.finalize = &Finalize; Instance_ = JSClassCreate(&definition); @@ -3335,6 +3522,27 @@ JSGlobalContextRef CYGetJSContext() { definition.finalize = &Finalize; Message_ = JSClassCreate(&definition); + definition = kJSClassDefinitionEmpty; + definition.className = "Messages"; + definition.hasProperty = &Messages_hasProperty; + definition.getProperty = &Messages_getProperty; + definition.setProperty = &Messages_setProperty; +#if !__OBJC2__ + definition.deleteProperty = &Messages_deleteProperty; +#endif + definition.getPropertyNames = &Messages_getPropertyNames; + definition.finalize = &Finalize; + Messages_ = JSClassCreate(&definition); + + definition = kJSClassDefinitionEmpty; + definition.className = "NSArrayPrototype"; + //definition.hasProperty = &NSArrayPrototype_hasProperty; + //definition.getProperty = &NSArrayPrototype_getProperty; + //definition.setProperty = &NSArrayPrototype_setProperty; + //definition.deleteProperty = &NSArrayPrototype_deleteProperty; + //definition.getPropertyNames = &NSArrayPrototype_getPropertyNames; + NSArrayPrototype_ = JSClassCreate(&definition); + definition = kJSClassDefinitionEmpty; definition.className = "Pointer"; definition.staticValues = Pointer_staticValues; @@ -3344,18 +3552,6 @@ JSGlobalContextRef CYGetJSContext() { 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; @@ -3376,7 +3572,7 @@ JSGlobalContextRef CYGetJSContext() { definition = kJSClassDefinitionEmpty; definition.className = "Type"; definition.staticFunctions = Type_staticFunctions; - //definition.getProperty = &Type_getProperty; + definition.getProperty = &Type_getProperty; definition.callAsFunction = &Type_callAsFunction; definition.callAsConstructor = &Type_callAsConstructor; definition.finalize = &Finalize; @@ -3403,7 +3599,6 @@ JSGlobalContextRef CYGetJSContext() { 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; @@ -3431,6 +3626,7 @@ JSGlobalContextRef CYGetJSContext() { Array_ = CYCastJSObject(context, CYGetProperty(context, global, CYJSString("Array"))); Function_ = CYCastJSObject(context, CYGetProperty(context, global, CYJSString("Function"))); + String_ = CYCastJSObject(context, CYGetProperty(context, global, CYJSString("String"))); length_ = JSStringCreateWithUTF8CString("length"); message_ = JSStringCreateWithUTF8CString("message"); @@ -3439,22 +3635,28 @@ JSGlobalContextRef CYGetJSContext() { toCYON_ = JSStringCreateWithUTF8CString("toCYON"); toJSON_ = JSStringCreateWithUTF8CString("toJSON"); + JSObjectRef Object(CYCastJSObject(context, CYGetProperty(context, global, CYJSString("Object")))); + Object_prototype_ = CYCastJSObject(context, CYGetProperty(context, Object, prototype_)); + 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 Instance(JSObjectMakeConstructor(context, Instance_, &Instance_new)); JSObjectRef Message(JSObjectMakeConstructor(context, Message_, NULL)); JSObjectRef Selector(JSObjectMakeConstructor(context, Selector_, &Selector_new)); + Instance_prototype_ = (JSObjectRef) CYGetProperty(context, Instance, prototype_); + 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("Instance"), Instance); 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)); @@ -3463,8 +3665,13 @@ JSGlobalContextRef CYGetJSContext() { class_addMethod(NSCFType_, @selector(cy$toJSON:), reinterpret_cast(&NSCFType$cy$toJSON), "@12@0:4@8"); + JSObjectRef cycript(JSObjectMake(context, NULL, NULL)); + CYSetProperty(context, global, CYJSString("Cycript"), cycript); + CYSetProperty(context, cycript, CYJSString("gc"), JSObjectMakeFunctionWithCallback(context, CYJSString("gc"), &Cycript_gc_callAsFunction)); + 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)); + CYSetProperty(context, global, CYJSString("$cyq"), JSObjectMakeFunctionWithCallback(context, CYJSString("$cyq"), &$cyq)); System_ = JSObjectMake(context, NULL, NULL); CYSetProperty(context, global, CYJSString("system"), System_); @@ -3474,6 +3681,18 @@ JSGlobalContextRef CYGetJSContext() { CYSetProperty(context, System_, CYJSString("print"), JSObjectMakeFunctionWithCallback(context, CYJSString("print"), &System_print)); Result_ = JSStringCreateWithUTF8CString("_"); + + JSValueProtect(context, Array_); + JSValueProtect(context, Function_); + JSValueProtect(context, String_); + + JSValueProtect(context, Instance_prototype_); + JSValueProtect(context, Object_prototype_); + + JSValueProtect(context, Array_prototype_); + JSValueProtect(context, Array_pop_); + JSValueProtect(context, Array_push_); + JSValueProtect(context, Array_splice_); } return Context_;