X-Git-Url: https://git.saurik.com/cycript.git/blobdiff_plain/766aef7a086cb0a95b8c9a16ffdf176dde614fe3..3c1c3635db4635a7aef0a5f625678378f48b6696:/Library.mm diff --git a/Library.mm b/Library.mm index fe9f968..ad1ecf4 100644 --- a/Library.mm +++ b/Library.mm @@ -40,6 +40,8 @@ #include #include +#include "Internal.hpp" + #include #include @@ -50,23 +52,12 @@ #include "Pooling.hpp" -#ifdef __OBJC__ -#include "Struct.hpp" -#endif - #ifdef __APPLE__ #include #include #include #endif -#ifdef __OBJC__ -#ifdef __APPLE__ -#include -#endif -#include -#endif - #include #include @@ -83,19 +74,11 @@ #undef _assert #undef _trace -#ifdef __OBJC__ -#define _throw(name, args...) \ - @throw [NSException exceptionWithName:name reason:[NSString stringWithFormat:@args] userInfo:nil] -#else -#define _throw(name, args...) ({ \ - CYPool pool; \ - throw std::string(apr_psprintf(pool, args)); \ -}) -#endif +void CYThrow(const char *format, ...); #define _assert(test, args...) do { \ if (!(test)) \ - _throw(NSInternalInconsistencyException, "*** _assert(%s):%s(%u):%s [errno=%d]", #test, __FILE__, __LINE__, __FUNCTION__, errno); \ + throw CYError(context, "*** _assert(%s):%s(%u):%s [errno=%d]", #test, __FILE__, __LINE__, __FUNCTION__, errno); \ } while (false) #define _trace() do { \ @@ -115,33 +98,14 @@ #define CYTry \ try #define CYCatch \ - CYCatch_ \ - catch (...) { \ + catch (const CYError &error) { \ + *exception = error.value_; \ + return NULL; \ + } catch (...) { \ *exception = CYCastJSValue(context, "catch(...)"); \ return NULL; \ } -#ifdef __OBJC__ -#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]; \ - } \ -} -#else -#define CYPoolTry { -#define CYPoolCatch } -#endif - #ifndef __APPLE__ #define class_getSuperclass GSObjCSuper #define object_getClass GSObjCClass @@ -153,6 +117,19 @@ char *sqlite3_column_pooled(apr_pool_t *pool, sqlite3_stmt *stmt, int n) { else return NULL; } +struct CYError { + JSContextRef context_; + JSValueRef value_; + + CYError(JSContextRef context, JSValueRef value) : + context_(context), + value_(value) + { + } + + CYError(JSContextRef context, const char *format, ...); +}; + void CYThrow(JSContextRef context, JSValueRef value); JSValueRef CYSendMessage(apr_pool_t *pool, JSContextRef context, id self, Class super, SEL _cmd, size_t count, const JSValueRef arguments[], bool initialize, JSValueRef *exception); @@ -166,6 +143,11 @@ struct CYUTF8String { size(size) { } + + bool operator ==(const char *value) const { + size_t length(strlen(data)); + return length == size && memcmp(value, data, length) == 0; + } }; struct CYUTF16String { @@ -179,6 +161,14 @@ struct CYUTF16String { } }; +struct CYHooks { + void *(*ExecuteStart)(); + void (*ExecuteEnd)(void *); + JSValueRef (*RuntimeProperty)(JSContextRef, CYUTF8String); + bool (*PoolFFI)(apr_pool_t *, JSContextRef, sig::Type *, ffi_type *, void *, JSValueRef); + JSValueRef (*FromFFI)(JSContextRef, sig::Type *, ffi_type *, void *, bool, JSObjectRef); +} *hooks_; + /* JavaScript Properties {{{ */ JSValueRef CYGetProperty(JSContextRef context, JSObjectRef object, size_t index) { JSValueRef exception(NULL); @@ -280,7 +270,7 @@ static CYUTF16String CYCastUTF16String(JSStringRef value) { return CYUTF16String(JSStringGetCharactersPtr(value), JSStringGetLength(value)); } -static CYUTF8String CYPoolUTF8String(apr_pool_t *pool, JSStringRef value) { +static CYUTF8String CYPoolUTF8String(apr_pool_t *pool, JSContextRef context, JSStringRef value) { _assert(pool != NULL); CYUTF16String utf16(CYCastUTF16String(value)); @@ -303,14 +293,14 @@ static CYUTF8String CYPoolUTF8String(apr_pool_t *pool, JSStringRef value) { return utf8; } -static const char *CYPoolCString(apr_pool_t *pool, JSStringRef value) { - CYUTF8String utf8(CYPoolUTF8String(pool, value)); +static const char *CYPoolCString(apr_pool_t *pool, JSContextRef context, JSStringRef value) { + CYUTF8String utf8(CYPoolUTF8String(pool, context, value)); _assert(memchr(utf8.data, '\0', utf8.size) == NULL); return utf8.data; } static const char *CYPoolCString(apr_pool_t *pool, JSContextRef context, JSValueRef value) { - return JSValueIsNull(context, value) ? NULL : CYPoolCString(pool, CYJSString(context, value)); + return JSValueIsNull(context, value) ? NULL : CYPoolCString(pool, context, CYJSString(context, value)); } /* }}} */ /* C Strings {{{ */ @@ -342,100 +332,6 @@ static const char *CYPoolCString(apr_pool_t *pool, JSContextRef context, JSValue /* }}} */ -#ifdef __OBJC__ -/* Objective-C Pool Release {{{ */ -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; - } -} - -template -Type_ CYPoolRelease(apr_pool_t *pool, Type_ object) { - return (Type_) CYPoolRelease_(pool, (id) object); -} -/* }}} */ -/* Objective-C Strings {{{ */ -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(NSInternalInconsistencyException, "[NSString getCString:maxLength:encoding:] == NO"); - return string; - } -} - -JSStringRef CYCopyJSString_(NSString *value) { -#ifdef __APPLE__ - return JSStringCreateWithCFString(reinterpret_cast(value)); -#else - CYPool pool; - return CYCopyJSString(CYPoolCString(pool, value)); -#endif -} - -JSStringRef CYCopyJSString(id value) { - if (value == nil) - return NULL; - // XXX: this definition scares me; is anyone using this?! - NSString *string([value description]); - return CYCopyJSString_(string); -} - -NSString *CYCopyNSString(const CYUTF8String &value) { -#ifdef __APPLE__ - return (NSString *) CFStringCreateWithBytes(kCFAllocatorDefault, reinterpret_cast(value.data), value.size, kCFStringEncodingUTF8, true); -#else - return [[NSString alloc] initWithBytes:value.data length:value.size encoding:NSUTF8StringEncoding]; -#endif -} - -NSString *CYCopyNSString(JSStringRef value) { -#ifdef __APPLE__ - return (NSString *) JSStringCopyCFString(kCFAllocatorDefault, value); -#else - return CYCopyNSString(CYCastCString_(value)); -#endif -} - -NSString *CYCopyNSString(JSContextRef context, JSValueRef value) { - return CYCopyNSString(CYJSString(context, value)); -} - -NSString *CYCastNSString(apr_pool_t *pool, const CYUTF8String &value) { - return CYPoolRelease(pool, CYCopyNSString(value)); -} - -NSString *CYCastNSString(apr_pool_t *pool, SEL sel) { - const char *name(sel_getName(sel)); - return CYPoolRelease(pool, CYCopyNSString(CYUTF8String(name, strlen(name)))); -} - -NSString *CYCastNSString(apr_pool_t *pool, JSStringRef value) { - return CYPoolRelease(pool, CYCopyNSString(value)); -} - -CYUTF8String CYCastUTF8String(NSString *value) { - NSData *data([value dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:NO]); - return CYUTF8String(reinterpret_cast([data bytes]), [data length]); -} -/* }}} */ -#endif - /* Index Offsets {{{ */ size_t CYGetIndex(const CYUTF8String &value) { if (value.data[0] != '0') { @@ -448,8 +344,8 @@ size_t CYGetIndex(const CYUTF8String &value) { return _not(size_t); } -size_t CYGetIndex(apr_pool_t *pool, JSStringRef value) { - return CYGetIndex(CYPoolUTF8String(pool, value)); +size_t CYGetIndex(apr_pool_t *pool, JSContextRef context, JSStringRef value) { + return CYGetIndex(CYPoolUTF8String(pool, context, value)); } bool CYGetOffset(const char *value, ssize_t &index) { @@ -465,16 +361,6 @@ bool CYGetOffset(const char *value, ssize_t &index) { return false; } - -#ifdef __OBJC__ -size_t CYGetIndex(NSString *value) { - return CYGetIndex(CYCastUTF8String(value)); -} - -bool CYGetOffset(apr_pool_t *pool, NSString *value, ssize_t &index) { - return CYGetOffset(CYPoolCString(pool, value), index); -} -#endif /* }}} */ /* JavaScript *ify {{{ */ @@ -528,7 +414,6 @@ void CYNumerify(std::ostringstream &str, double value) { sprintf(string, "%.17g", value); str << string; } -/* }}} */ bool CYIsKey(CYUTF8String value) { const char *data(value.data); @@ -551,6 +436,7 @@ bool CYIsKey(CYUTF8String value) { return true; } +/* }}} */ static JSGlobalContextRef Context_; static JSObjectRef System_; @@ -560,23 +446,8 @@ static JSClassRef Pointer_; static JSClassRef Runtime_; static JSClassRef Struct_; -#ifdef __OBJC__ -static JSClassRef Instance_; -static JSClassRef Internal_; -static JSClassRef Message_; -static JSClassRef Messages_; -static JSClassRef Selector_; -static JSClassRef Super_; - -static JSClassRef ObjectiveC_Classes_; -static JSClassRef ObjectiveC_Image_Classes_; -static JSClassRef ObjectiveC_Images_; -static JSClassRef ObjectiveC_Protocols_; - -static JSObjectRef Instance_prototype_; -#endif - static JSObjectRef Array_; +static JSObjectRef Error_; static JSObjectRef Function_; static JSObjectRef String_; @@ -590,522 +461,1368 @@ static JSStringRef toCYON_; static JSStringRef toJSON_; static JSObjectRef Object_prototype_; +static JSObjectRef Function_prototype_; static JSObjectRef Array_prototype_; static JSObjectRef Array_pop_; static JSObjectRef Array_push_; static JSObjectRef Array_splice_; -#ifdef __OBJC__ -#ifdef __APPLE__ -static Class NSCFBoolean_; -static Class NSCFType_; -#endif - -static Class NSArray_; -static Class NSDictionary_; -static Class NSMessageBuilder_; -static Class NSZombie_; -static Class Object_; -#endif - sqlite3 *Bridge_; -static void Finalize(JSObjectRef object) { +void Finalize(JSObjectRef object) { delete reinterpret_cast(JSObjectGetPrivate(object)); } -class Type_privateData; - -struct CYValue : - CYData +struct CStringMapLess : + std::binary_function { - void *value_; - - CYValue() { + _finline bool operator ()(const char *lhs, const char *rhs) const { + return strcmp(lhs, rhs) < 0; } +}; - CYValue(const void *value) : - value_(const_cast(value)) - { - } +void Structor_(apr_pool_t *pool, const char *name, const char *types, sig::Type *&type) { + if (name == NULL) + return; - CYValue(const CYValue &rhs) : - value_(rhs.value_) - { - } + JSContextRef context(NULL); - virtual Type_privateData *GetType() const { - return NULL; - } -}; + sqlite3_stmt *statement; -struct CYOwned : - CYValue -{ - private: - JSContextRef context_; - JSObjectRef owner_; + _sqlcall(sqlite3_prepare(Bridge_, + "select " + "\"bridge\".\"mode\", " + "\"bridge\".\"value\" " + "from \"bridge\" " + "where" + " \"bridge\".\"mode\" in (3, 4) and" + " \"bridge\".\"name\" = ?" + " limit 1" + , -1, &statement, NULL)); - public: - CYOwned(void *value, JSContextRef context, JSObjectRef owner) : - CYValue(value), - context_(context), - owner_(owner) - { - if (owner_ != NULL) - JSValueProtect(context_, owner_); - } + _sqlcall(sqlite3_bind_text(statement, 1, name, -1, SQLITE_STATIC)); - virtual ~CYOwned() { - if (owner_ != NULL) - JSValueUnprotect(context_, owner_); - } + int mode; + const char *value; - JSObjectRef GetOwner() const { - return owner_; + if (_sqlcall(sqlite3_step(statement)) == SQLITE_DONE) { + mode = -1; + value = NULL; + } else { + mode = sqlite3_column_int(statement, 0); + value = sqlite3_column_pooled(pool, statement, 1); } -}; -#ifdef __OBJC__ -struct Selector_privateData : - CYValue -{ - Selector_privateData(SEL value) : - CYValue(value) - { - } + _sqlcall(sqlite3_finalize(statement)); - SEL GetValue() const { - return reinterpret_cast(value_); - } + switch (mode) { + default: + _assert(false); + case -1: + break; - virtual Type_privateData *GetType() const; -}; + case 3: { + sig::Parse(pool, &type->data.signature, value, &Structor_); + } break; -// XXX: trick this out with associated objects! -JSValueRef CYGetClassPrototype(JSContextRef context, id self) { - if (self == nil) - return Instance_prototype_; + case 4: { + sig::Signature signature; + sig::Parse(pool, &signature, value, &Structor_); + type = signature.elements[0].type; + } break; + } +} - // XXX: I need to think through multi-context - typedef std::map CacheMap; - static CacheMap cache_; +JSClassRef Type_privateData::Class_; - 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 +struct Pointer : + CYOwned { - enum Flags { - None = 0, - Transient = (1 << 0), - Uninitialized = (1 << 1), - }; - - Flags flags_; + Type_privateData *type_; - Instance(id value, Flags flags) : - CYValue(value), - flags_(flags) + Pointer(void *value, JSContextRef context, JSObjectRef owner, sig::Type *type) : + CYOwned(value, context, owner), + type_(new(pool_) Type_privateData(type)) { } - - 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))); - JSObjectSetPrototype(context, value, CYGetClassPrototype(context, object_getClass(object))); - return value; - } - - id GetValue() const { - return reinterpret_cast(value_); - } - - bool IsUninitialized() const { - return (flags_ & Uninitialized) != 0; - } - - virtual Type_privateData *GetType() const; }; -namespace cy { -struct Super : - Instance +struct Struct_privateData : + CYOwned { - Class class_; + Type_privateData *type_; - Super(id value, Class _class) : - Instance(value, Instance::Transient), - class_(_class) + Struct_privateData(JSContextRef context, JSObjectRef owner) : + CYOwned(NULL, context, owner) { } +}; - static JSObjectRef Make(JSContextRef context, id object, Class _class) { - JSObjectRef value(JSObjectMake(context, Super_, new Super(object, _class))); - return value; +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(context, 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; } -}; } -struct Messages : + return JSObjectMake(context, Struct_, internal); +} + +struct Functor_privateData : CYValue { - Messages(Class value) : - CYValue(value) - { - } + sig::Signature signature_; + ffi_cif cif_; - static JSObjectRef Make(JSContextRef context, Class _class, bool array = false) { - JSObjectRef value(JSObjectMake(context, Messages_, new Messages(_class))); - if (_class == NSArray_) - array = true; - if (Class super = class_getSuperclass(_class)) - JSObjectSetPrototype(context, value, Messages::Make(context, super, array)); - /*else if (array) - JSObjectSetPrototype(context, value, Array_prototype_);*/ - return value; + 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_); } - Class GetValue() const { - return reinterpret_cast(value_); - } + void (*GetValue())() const { + return reinterpret_cast(value_); +} }; -struct Internal : - CYOwned +struct Closure_privateData : + Functor_privateData { - Internal(id value, JSContextRef context, JSObjectRef owner) : - CYOwned(value, context, owner) - { - } + JSContextRef context_; + JSObjectRef function_; - static JSObjectRef Make(JSContextRef context, id object, JSObjectRef owner) { - return JSObjectMake(context, Internal_, new Internal(object, context, owner)); + Closure_privateData(JSContextRef context, JSObjectRef function, const char *type) : + Functor_privateData(type, NULL), + context_(context), + function_(function) + { + JSValueProtect(context_, function_); } - id GetValue() const { - return reinterpret_cast(value_); + virtual ~Closure_privateData() { + JSValueUnprotect(context_, function_); } }; -#endif -namespace sig { +JSValueRef CYCastJSValue(JSContextRef context, bool value) { + return JSValueMakeBoolean(context, value); +} -void Copy(apr_pool_t *pool, Type &lhs, Type &rhs); +JSValueRef CYCastJSValue(JSContextRef context, double value) { + return JSValueMakeNumber(context, value); +} -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); +#define CYCastJSValue_(Type_) \ + JSValueRef CYCastJSValue(JSContextRef context, Type_ value) { \ + return JSValueMakeNumber(context, static_cast(value)); \ } - lhs.offset = rhs.offset; + +CYCastJSValue_(int) +CYCastJSValue_(unsigned int) +CYCastJSValue_(long int) +CYCastJSValue_(long unsigned int) +CYCastJSValue_(long long int) +CYCastJSValue_(long long unsigned int) + +JSValueRef CYJSUndefined(JSContextRef context) { + return JSValueMakeUndefined(context); } -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]); +double CYCastDouble(const char *value, size_t size) { + char *end; + double number(strtod(value, &end)); + if (end != value + size) + return NAN; + return number; } -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; +double CYCastDouble(const char *value) { + return CYCastDouble(value, strlen(value)); +} - if (sig::IsAggregate(rhs.primitive)) - Copy(pool, lhs.data.signature, rhs.data.signature); - else { - sig::Type *&lht(lhs.data.data.type); - sig::Type *&rht(rhs.data.data.type); +double CYCastDouble(JSContextRef context, JSValueRef value) { + JSValueRef exception(NULL); + double number(JSValueToNumber(context, value, &exception)); + CYThrow(context, exception); + return number; +} - if (rht == NULL) - lht = NULL; - else { - lht = new(pool) Type; - Copy(pool, *lht, *rht); - } +bool CYCastBool(JSContextRef context, JSValueRef value) { + return JSValueToBoolean(context, value); +} - lhs.data.data.size = rhs.data.data.size; - } +JSValueRef CYJSNull(JSContextRef context) { + return JSValueMakeNull(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]); - } - } +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)); } -struct CStringMapLess : - std::binary_function -{ - _finline bool operator ()(const char *lhs, const char *rhs) const { - return strcmp(lhs, rhs) < 0; - } -}; +JSObjectRef CYCastJSObject(JSContextRef context, JSValueRef value) { + JSValueRef exception(NULL); + JSObjectRef object(JSValueToObject(context, value, &exception)); + CYThrow(context, exception); + return object; +} -void Structor_(apr_pool_t *pool, const char *name, const char *types, sig::Type *&type) { - if (name == NULL) - return; +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; +} - sqlite3_stmt *statement; +bool CYIsCallable(JSContextRef context, JSValueRef value) { + return value != NULL && JSValueIsObject(context, value) && JSObjectIsFunction(context, (JSObjectRef) value); +} - _sqlcall(sqlite3_prepare(Bridge_, - "select " - "\"bridge\".\"mode\", " - "\"bridge\".\"value\" " - "from \"bridge\" " - "where" - " \"bridge\".\"mode\" in (3, 4) and" - " \"bridge\".\"name\" = ?" - " limit 1" - , -1, &statement, NULL)); +const char *CYPoolCCYON(apr_pool_t *pool, JSContextRef context, JSObjectRef object); - _sqlcall(sqlite3_bind_text(statement, 1, name, -1, SQLITE_STATIC)); +static JSValueRef System_print(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { + if (count == 0) + printf("\n"); + else + printf("%s\n", CYCastCString(context, arguments[0])); + return CYJSUndefined(context); +} CYCatch } - int mode; - const char *value; +static size_t Nonce_(0); - if (_sqlcall(sqlite3_step(statement)) == SQLITE_DONE) { - mode = -1; - value = NULL; - } else { - mode = sqlite3_column_int(statement, 0); - value = sqlite3_column_pooled(pool, statement, 1); - } +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); +} - _sqlcall(sqlite3_finalize(statement)); +static JSValueRef Cycript_gc_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { + JSGarbageCollect(context); + return CYJSUndefined(context); +} - switch (mode) { - default: - _assert(false); - case -1: - break; +const char *CYPoolCCYON(apr_pool_t *pool, JSContextRef context, JSValueRef value, JSValueRef *exception) { CYTry { + switch (JSType type = JSValueGetType(context, value)) { + case kJSTypeUndefined: + return "undefined"; + case kJSTypeNull: + return "null"; + case kJSTypeBoolean: + return CYCastBool(context, value) ? "true" : "false"; - case 3: { - sig::Parse(pool, &type->data.signature, value, &Structor_); + case kJSTypeNumber: { + std::ostringstream str; + CYNumerify(str, CYCastDouble(context, value)); + std::string value(str.str()); + return apr_pstrmemdup(pool, value.c_str(), value.size()); } break; - case 4: { - sig::Signature signature; - sig::Parse(pool, &signature, value, &Structor_); - type = signature.elements[0].type; + case kJSTypeString: { + std::ostringstream str; + CYUTF8String string(CYPoolUTF8String(pool, context, CYJSString(context, value))); + CYStringify(str, string.data, string.size); + std::string value(str.str()); + return apr_pstrmemdup(pool, value.c_str(), value.size()); } break; - } -} - -struct Type_privateData : - CYData -{ -#ifdef __OBJC__ - static Type_privateData *Object; - static Type_privateData *Selector; -#endif - - static JSClassRef Class_; - - ffi_type *ffi_; - sig::Type *type_; - void Set(sig::Type *type) { - type_ = new(pool_) sig::Type; - sig::Copy(pool_, *type_, *type); + case kJSTypeObject: + return CYPoolCCYON(pool, context, (JSObjectRef) value); + default: + throw CYError(context, "JSValueGetType() == 0x%x", type); } +} CYCatch } - Type_privateData(apr_pool_t *pool, const char *type) : - ffi_(NULL) - { - if (pool != NULL) - pool_ = pool; +const char *CYPoolCCYON(apr_pool_t *pool, JSContextRef context, JSValueRef value) { + JSValueRef exception(NULL); + const char *cyon(CYPoolCCYON(pool, context, value, &exception)); + CYThrow(context, exception); + return cyon; +} + +const char *CYPoolCCYON(apr_pool_t *pool, JSContextRef context, JSObjectRef object) { + JSValueRef toCYON(CYGetProperty(context, object, toCYON_)); + if (CYIsCallable(context, toCYON)) { + JSValueRef value(CYCallAsFunction(context, (JSObjectRef) toCYON, object, 0, NULL)); + return CYPoolCString(pool, context, value); + } + + JSValueRef toJSON(CYGetProperty(context, object, toJSON_)); + if (CYIsCallable(context, toJSON)) { + JSValueRef arguments[1] = {CYCastJSValue(context, CYJSString(""))}; + JSValueRef exception(NULL); + const char *cyon(CYPoolCCYON(pool, context, CYCallAsFunction(context, (JSObjectRef) toJSON, object, 1, arguments), &exception)); + CYThrow(context, exception); + return cyon; + } + + std::ostringstream str; + + str << '{'; + + // XXX: this is, sadly, going to leak + JSPropertyNameArrayRef names(JSObjectCopyPropertyNames(context, object)); + + bool comma(false); + + for (size_t index(0), count(JSPropertyNameArrayGetCount(names)); index != count; ++index) { + JSStringRef name(JSPropertyNameArrayGetNameAtIndex(names, index)); + JSValueRef value(CYGetProperty(context, object, name)); + + if (comma) + str << ','; + else + comma = true; + + CYUTF8String string(CYPoolUTF8String(pool, context, name)); + if (CYIsKey(string)) + str << string.data; + else + CYStringify(str, string.data, string.size); + + str << ':' << CYPoolCCYON(pool, context, value); + } + + str << '}'; + + JSPropertyNameArrayRelease(names); + + std::string string(str.str()); + return apr_pstrmemdup(pool, string.c_str(), string.size()); +} + +static JSValueRef Array_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { + CYPool pool; + std::ostringstream str; + + str << '['; + + JSValueRef length(CYGetProperty(context, _this, length_)); + bool comma(false); + + for (size_t index(0), count(CYCastDouble(context, length)); index != count; ++index) { + JSValueRef value(CYGetProperty(context, _this, index)); + + if (comma) + str << ','; + else + comma = true; + + if (!JSValueIsUndefined(context, value)) + str << CYPoolCCYON(pool, context, value); + else { + str << ','; + comma = false; + } + } + + str << ']'; + + std::string value(str.str()); + return CYCastJSValue(context, CYJSString(CYUTF8String(value.c_str(), value.size()))); +} CYCatch } + +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); +} + +static JSObjectRef CYMakeFunctor(JSContextRef context, void (*function)(), const char *type) { + Functor_privateData *internal(new Functor_privateData(type, function)); + return JSObjectMake(context, Functor_, internal); +} + +static bool CYGetOffset(apr_pool_t *pool, JSContextRef context, JSStringRef value, ssize_t &index) { + return CYGetOffset(CYPoolCString(pool, context, value), index); +} + +static 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: + double number(CYCastDouble(context, value)); + if (std::isnan(number)) + throw CYError(context, "cannot convert value to pointer"); + return reinterpret_cast(static_cast(static_cast(number))); + } +} + +template +static _finline Type_ CYCastPointer(JSContextRef context, JSValueRef value) { + return reinterpret_cast(CYCastPointer_(context, value)); +} + +#define CYObjectiveTry \ + try +#define CYObjectiveCatch \ + catch (const CYError &error) { \ + @throw CYCastNSObject(NULL, error.context_, error.value_); \ + } + +#define CYPoolTry { \ + id _saved(nil); \ + NSAutoreleasePool *_pool([[NSAutoreleasePool alloc] init]); \ + @try +#define CYPoolCatch(value) \ + @catch (NSException *error) { \ + _saved = [error retain]; \ + throw CYError(context, CYCastJSValue(context, error)); \ + return value; \ + } @finally { \ + [_pool release]; \ + if (_saved != nil) \ + [_saved autorelease]; \ + } \ +} + +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); + 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::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 CYError(context, "unable to extract structure value"); + } + } + + CYPoolFFI(pool, context, element->type, field, base, rhs); + // XXX: alignment? + base += field->size; + } + } break; + + case sig::void_P: + break; + + default: + if (hooks_ != NULL && hooks_->PoolFFI != NULL) + if ((*hooks_->PoolFFI)(pool, context, type, ffi, data, value)) + return; + + fprintf(stderr, "CYPoolFFI(%c)\n", type->primitive); + _assert(false); + } +} + +static JSValueRef CYFromFFI(JSContextRef context, sig::Type *type, ffi_type *ffi, void *data, bool initialize = false, JSObjectRef owner = NULL) { + switch (type->primitive) { + case sig::boolean_P: + return CYCastJSValue(context, *reinterpret_cast(data)); + +#define CYFromFFI_(primitive, native) \ + case sig::primitive ## _P: \ + return CYCastJSValue(context, *reinterpret_cast(data)); \ + + 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::pointer_P: + if (void *pointer = *reinterpret_cast(data)) + return CYMakePointer(context, pointer, type->data.data.type, ffi, owner); + else goto null; + + case sig::string_P: + if (char *utf8 = *reinterpret_cast(data)) + return CYCastJSValue(context, utf8); + else goto null; + + case sig::struct_P: + return CYMakeStruct(context, data, type, ffi, owner); + case sig::void_P: + return CYJSUndefined(context); + + null: + return CYJSNull(context); + default: + if (hooks_ != NULL && hooks_->FromFFI != NULL) + if (JSValueRef value = (*hooks_->FromFFI)(context, type, ffi, data, initialize, owner)) + return value; + + fprintf(stderr, "CYFromFFI(%c)\n", type->primitive); + _assert(false); + } +} + +static 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); +} + +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(CYGetJSContext(), function, type)); + + ffi_closure *closure((ffi_closure *) _syscall(mmap( + NULL, sizeof(ffi_closure), + PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, + -1, 0 + ))); + + 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; + + return internal; +} + +static JSObjectRef CYMakeFunctor(JSContextRef context, JSObjectRef function, const char *type) { + Closure_privateData *internal(CYMakeFunctor_(context, function, type, &FunctionClosure_)); + return JSObjectMake(context, Functor_, internal); +} + +static JSObjectRef CYMakeFunctor(JSContextRef context, JSValueRef value, const char *type) { + JSValueRef exception(NULL); + 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); + } +} + +static bool Index_(apr_pool_t *pool, JSContextRef context, 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, context, 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; +} + +static JSValueRef Pointer_getIndex(JSContextRef context, JSObjectRef object, size_t index, JSValueRef *exception) { CYTry { + 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->GetOwner() ?: object); + return CYFromFFI(context, typical->type_, ffi, base, false, owner); +} CYCatch } + +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_); + + if (typical->type_ == NULL) + return NULL; + + ssize_t offset; + if (!CYGetOffset(pool, context, property, offset)) + return NULL; + + return Pointer_getIndex(context, object, offset, exception); +} + +static JSValueRef Pointer_getProperty_$cyi(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { + return Pointer_getIndex(context, object, 0, exception); +} + +static bool Pointer_setIndex(JSContextRef context, JSObjectRef object, size_t index, JSValueRef value, JSValueRef *exception) { CYTry { + 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; + + CYPoolFFI(NULL, context, typical->type_, ffi, base, value); + return true; +} CYCatch } + +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_); + + if (typical->type_ == NULL) + return NULL; + + ssize_t offset; + if (!CYGetOffset(pool, context, property, offset)) + return NULL; + + return Pointer_setIndex(context, object, offset, value, exception); +} + +static bool Pointer_setProperty_$cyi(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) { + return Pointer_setIndex(context, object, 0, value, exception); +} + +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); +} + +static JSValueRef Struct_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry { + CYPool pool; + Struct_privateData *internal(reinterpret_cast(JSObjectGetPrivate(object))); + Type_privateData *typical(internal->type_); + + ssize_t index; + uint8_t *base; + + if (!Index_(pool, context, internal, property, index, base)) + return NULL; + + JSObjectRef owner(internal->GetOwner() ?: object); + + return CYFromFFI(context, typical->type_->data.signature.elements[index].type, typical->GetFFI()->elements[index], base, false, owner); +} CYCatch } + +static bool Struct_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) { CYTry { + CYPool pool; + Struct_privateData *internal(reinterpret_cast(JSObjectGetPrivate(object))); + Type_privateData *typical(internal->type_); + + ssize_t index; + uint8_t *base; + + if (!Index_(pool, context, internal, property, index, base)) + return false; + + CYPoolFFI(NULL, context, typical->type_->data.signature.elements[index].type, typical->GetFFI()->elements[index], base, value); + return true; +} CYCatch } + +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_); + + if (type == NULL) + return; + + size_t count(type->data.signature.count); + sig::Element *elements(type->data.signature.elements); + + char number[32]; + + 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)); + } +} + +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 CYError(context, "incorrect number of arguments to ffi function"); + + size_t size(setups + count); + void *values[size]; + memcpy(values, setup, sizeof(void *) * setups); + + 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[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, cif->rtype, value, initialize); +} CYCatch } + +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 JSObjectRef CYMakeType(JSContextRef context, const char *type) { + Type_privateData *internal(new Type_privateData(NULL, type)); + return JSObjectMake(context, Type_privateData::Class_, internal); +} + +static JSObjectRef CYMakeType(JSContextRef context, sig::Type *type) { + Type_privateData *internal(new Type_privateData(type)); + return JSObjectMake(context, Type_privateData::Class_, internal); +} + +static void *CYCastSymbol(const char *name) { + return dlsym(RTLD_DEFAULT, name); +} + +static JSValueRef Runtime_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry { + CYPool pool; + CYUTF8String name(CYPoolUTF8String(pool, context, property)); + + if (hooks_ != NULL && hooks_->RuntimeProperty != NULL) + if (JSValueRef value = (*hooks_->RuntimeProperty)(context, name)) + return value; + + sqlite3_stmt *statement; + + _sqlcall(sqlite3_prepare(Bridge_, + "select " + "\"bridge\".\"mode\", " + "\"bridge\".\"value\" " + "from \"bridge\" " + "where" + " \"bridge\".\"name\" = ?" + " limit 1" + , -1, &statement, NULL)); + + _sqlcall(sqlite3_bind_text(statement, 1, name.data, name.size, SQLITE_STATIC)); + + int mode; + const char *value; + + if (_sqlcall(sqlite3_step(statement)) == SQLITE_DONE) { + mode = -1; + value = NULL; + } else { + mode = sqlite3_column_int(statement, 0); + value = sqlite3_column_pooled(pool, statement, 1); + } + + _sqlcall(sqlite3_finalize(statement)); + + switch (mode) { + default: + _assert(false); + case -1: + return NULL; + + case 0: + return JSEvaluateScript(CYGetJSContext(), CYJSString(value), NULL, NULL, 0, NULL); + case 1: + return CYMakeFunctor(context, reinterpret_cast(CYCastSymbol(name.data)), value); + + case 2: { + // XXX: this is horrendously inefficient + sig::Signature signature; + sig::Parse(pool, &signature, value, &Structor_); + ffi_cif cif; + sig::sig_ffi_cif(pool, &sig::ObjectiveC, &signature, &cif); + return CYFromFFI(context, signature.elements[0].type, cif.rtype, CYCastSymbol(name.data)); + } + + // XXX: implement case 3 + case 4: + return CYMakeType(context, value); + } +} CYCatch } + +static JSObjectRef Pointer_new(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { + if (count != 2) + throw CYError(context, "incorrect number of arguments to Functor constructor"); + + void *value(CYCastPointer(context, arguments[0])); + const char *type(CYCastCString(context, arguments[1])); + + CYPool pool; + + sig::Signature signature; + sig::Parse(pool, &signature, type, &Structor_); + + return CYMakePointer(context, value, signature.elements[0].type, NULL, NULL); +} CYCatch } + +static JSObjectRef Type_new(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { + if (count != 1) + throw CYError(context, "incorrect number of arguments to Type constructor"); + const char *type(CYCastCString(context, arguments[0])); + return CYMakeType(context, type); +} CYCatch } + +static JSValueRef Type_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry { + Type_privateData *internal(reinterpret_cast(JSObjectGetPrivate(object))); + + sig::Type type; + + if (JSStringIsEqualToUTF8CString(property, "$cyi")) { + type.primitive = sig::pointer_P; + type.data.data.size = 0; + } else { + CYPool pool; + size_t index(CYGetIndex(pool, context, 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) { CYTry { + Type_privateData *internal(reinterpret_cast(JSObjectGetPrivate(object))); + + if (count != 1) + throw CYError(context, "incorrect number of arguments to type cast function"); + 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 } + +static JSObjectRef Type_callAsConstructor(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { + if (count != 0) + throw CYError(context, "incorrect number of arguments to type cast function"); + Type_privateData *internal(reinterpret_cast(JSObjectGetPrivate(object))); + + 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 } + +static JSObjectRef Functor_new(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { + if (count != 2) + throw CYError(context, "incorrect number of arguments to Functor constructor"); + const char *type(CYCastCString(context, arguments[1])); + return CYMakeFunctor(context, arguments[0], type); +} CYCatch } + +static JSValueRef CYValue_callAsFunction_valueOf(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { + CYValue *internal(reinterpret_cast(JSObjectGetPrivate(_this))); + return CYCastJSValue(context, reinterpret_cast(internal->value_)); +} CYCatch } + +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); +} + +static JSValueRef CYValue_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { +CYValue *internal(reinterpret_cast(JSObjectGetPrivate(_this))); + char string[32]; + sprintf(string, "%p", internal->value_); + + return CYCastJSValue(context, string); +} 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_)); + return CYCastJSValue(context, CYJSString(type)); +} 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_)); + size_t size(strlen(type)); + char *cyon(new(pool) char[12 + size + 1]); + memcpy(cyon, "new Type(\"", 10); + cyon[12 + size] = '\0'; + cyon[12 + size - 2] = '"'; + cyon[12 + size - 1] = ')'; + memcpy(cyon + 10, type, size); + return CYCastJSValue(context, CYJSString(cyon)); +} 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 Pointer_staticValues[2] = { + {"$cyi", &Pointer_getProperty_$cyi, &Pointer_setProperty_$cyi, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, + {NULL, NULL, NULL, 0} +}; + +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} +}; + +static JSStaticFunction Struct_staticFunctions[2] = { + {"$cya", &Struct_callAsFunction_$cya, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, + {NULL, NULL, 0} +}; + +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} +}; + +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} +}; + +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) { + JSContextRef context(CYGetJSContext()); + JSValueRef exception(NULL), result; + + void *handle; + if (hooks_ != NULL && hooks_->ExecuteStart != NULL) + handle = (*hooks_->ExecuteStart)(); + else + handle = NULL; + + const char *json; + + 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; + + try { + json = CYPoolCCYON(pool, context, result, &exception); + } catch (const char *error) { + return error; + } + + if (exception != NULL) + goto error; + + CYSetProperty(context, CYGetGlobalObject(context), Result_, result); + + if (hooks_ != NULL && hooks_->ExecuteEnd != NULL) + (*hooks_->ExecuteEnd)(handle); + return json; +} + +static apr_pool_t *Pool_; + +apr_pool_t *CYGetGlobalPool() { + return Pool_; +} + +MSInitialize { + JSContextRef context(NULL); + _aprcall(apr_initialize()); + _aprcall(apr_pool_create(&Pool_, NULL)); + _sqlcall(sqlite3_open("/usr/lib/libcycript.db", &Bridge_)); +} + +void CYThrow(JSContextRef context, JSValueRef value) { + if (value == NULL) + return; + throw CYError(context, value); +} + +CYError::CYError(JSContextRef context, const char *format, ...) { + if (context == NULL) + context = CYGetJSContext(); + + CYPool pool; + + va_list args; + va_start (args, format); + const char *message(apr_pvsprintf(pool, format, args)); + va_end (args); + + JSValueRef arguments[1] = {CYCastJSValue(context, CYJSString(message))}; + + JSValueRef exception(NULL); + value_ = JSObjectCallAsConstructor(context, Error_, 1, arguments, &exception); + CYThrow(context, exception); +} + +void CYObjectiveC(JSContextRef context, JSObjectRef global); + +#ifdef __OBJC__ +/* Objective-C {{{ */ +#include +#include "ObjectiveC/Internal.hpp" +#include "Struct.hpp" + +#ifdef __APPLE__ +#include +#endif + +/* Objective-C Pool Release {{{ */ +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; + } +} + +template +Type_ CYPoolRelease(apr_pool_t *pool, Type_ object) { + return (Type_) CYPoolRelease_(pool, (id) object); +} +/* }}} */ +/* Objective-C Strings {{{ */ +const char *CYPoolCString(apr_pool_t *pool, JSContextRef context, 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 CYError(context, "[NSString getCString:maxLength:encoding:] == NO"); + return string; + } +} + +JSStringRef CYCopyJSString_(NSString *value) { +#ifdef __APPLE__ + return JSStringCreateWithCFString(reinterpret_cast(value)); +#else + CYPool pool; + return CYCopyJSString(CYPoolCString(pool, value)); +#endif +} + +JSStringRef CYCopyJSString(id value) { + if (value == nil) + return NULL; + // XXX: this definition scares me; is anyone using this?! + NSString *string([value description]); + return CYCopyJSString_(string); +} + +NSString *CYCopyNSString(const CYUTF8String &value) { +#ifdef __APPLE__ + return (NSString *) CFStringCreateWithBytes(kCFAllocatorDefault, reinterpret_cast(value.data), value.size, kCFStringEncodingUTF8, true); +#else + return [[NSString alloc] initWithBytes:value.data length:value.size encoding:NSUTF8StringEncoding]; +#endif +} + +NSString *CYCopyNSString(JSStringRef value) { +#ifdef __APPLE__ + return (NSString *) JSStringCopyCFString(kCFAllocatorDefault, value); +#else + return CYCopyNSString(CYCastCString_(value)); +#endif +} + +NSString *CYCopyNSString(JSContextRef context, JSValueRef value) { + return CYCopyNSString(CYJSString(context, value)); +} + +NSString *CYCastNSString(apr_pool_t *pool, const CYUTF8String &value) { + return CYPoolRelease(pool, CYCopyNSString(value)); +} + +NSString *CYCastNSString(apr_pool_t *pool, SEL sel) { + const char *name(sel_getName(sel)); + return CYPoolRelease(pool, CYCopyNSString(CYUTF8String(name, strlen(name)))); +} + +NSString *CYCastNSString(apr_pool_t *pool, JSStringRef value) { + return CYPoolRelease(pool, CYCopyNSString(value)); +} - sig::Signature signature; - sig::Parse(pool_, &signature, type, &Structor_); - type_ = signature.elements[0].type; - } +CYUTF8String CYCastUTF8String(NSString *value) { + NSData *data([value dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:NO]); + return CYUTF8String(reinterpret_cast([data bytes]), [data length]); +} +/* }}} */ - Type_privateData(sig::Type *type) : - ffi_(NULL) - { - if (type != NULL) - Set(type); - } +void CYThrow(JSContextRef context, NSException *error, JSValueRef *exception) { + if (exception == NULL) + throw error; + *exception = CYCastJSValue(context, error); +} - Type_privateData(sig::Type *type, ffi_type *ffi) { - ffi_ = new(pool_) ffi_type; - sig::Copy(pool_, *ffi_, *ffi); - Set(type); - } +size_t CYGetIndex(NSString *value) { + return CYGetIndex(CYCastUTF8String(value)); +} - ffi_type *GetFFI() { - if (ffi_ == NULL) { - ffi_ = new(pool_) ffi_type; +bool CYGetOffset(apr_pool_t *pool, JSContextRef context, NSString *value, ssize_t &index) { + return CYGetOffset(CYPoolCString(pool, context, value), index); +} - sig::Element element; - element.name = NULL; - element.type = type_; - element.offset = 0; +static JSClassRef Instance_; +static JSClassRef Internal_; +static JSClassRef Message_; +static JSClassRef Messages_; +static JSClassRef Selector_; +static JSClassRef Super_; - sig::Signature signature; - signature.elements = &element; - signature.count = 1; +static JSClassRef ObjectiveC_Classes_; +static JSClassRef ObjectiveC_Image_Classes_; +static JSClassRef ObjectiveC_Images_; +static JSClassRef ObjectiveC_Protocols_; - ffi_cif cif; - sig::sig_ffi_cif(pool_, &sig::ObjectiveC, &signature, &cif); - *ffi_ = *cif.rtype; - } +static JSObjectRef Instance_prototype_; - return ffi_; - } -}; +#ifdef __APPLE__ +static Class NSCFBoolean_; +static Class NSCFType_; +#endif -JSClassRef Type_privateData::Class_; +static Class NSArray_; +static Class NSDictionary_; +static Class NSMessageBuilder_; +static Class NSZombie_; +static Class Object_; -#ifdef __OBJC__ -Type_privateData *Type_privateData::Object; -Type_privateData *Type_privateData::Selector; +static Type_privateData *Object_type; +static Type_privateData *Selector_type; Type_privateData *Instance::GetType() const { - return Type_privateData::Object; + return Object_type; } Type_privateData *Selector_privateData::GetType() const { - return Type_privateData::Selector; + return Selector_type; } -#endif - -struct Pointer : - CYOwned -{ - Type_privateData *type_; - Pointer(void *value, JSContextRef context, JSObjectRef owner, sig::Type *type) : - CYOwned(value, context, owner), - type_(new(pool_) Type_privateData(type)) - { - } -}; +// XXX: trick this out with associated objects! +JSValueRef CYGetClassPrototype(JSContextRef context, id self) { + if (self == nil) + return Instance_prototype_; -struct Struct_privateData : - CYOwned -{ - Type_privateData *type_; + // XXX: I need to think through multi-context + typedef std::map CacheMap; + static CacheMap cache_; - Struct_privateData(JSContextRef context, JSObjectRef owner) : - CYOwned(NULL, context, owner) - { - } -}; + JSValueRef &value(cache_[self]); + if (value != NULL) + return value; -typedef std::map TypeMap; -static TypeMap Types_; + JSClassRef _class(NULL); + JSValueRef prototype; -JSObjectRef CYMakeStruct(JSContextRef context, void *data, sig::Type *type, ffi_type *ffi, JSObjectRef 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; + if (self == NSArray_) + prototype = Array_prototype_; + else if (self == NSDictionary_) + prototype = Object_prototype_; + else + prototype = CYGetClassPrototype(context, class_getSuperclass(self)); - 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; - } + JSObjectRef object(JSObjectMake(context, _class, NULL)); + JSObjectSetPrototype(context, object, prototype); - return JSObjectMake(context, Struct_, internal); + JSValueProtect(context, object); + value = object; + return object; } -struct Functor_privateData : - CYValue -{ - sig::Signature signature_; - ffi_cif cif_; - - 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_); - } +JSObjectRef Messages::Make(JSContextRef context, Class _class, bool array) { + JSObjectRef value(JSObjectMake(context, Messages_, new Messages(_class))); + if (_class == NSArray_) + array = true; + if (Class super = class_getSuperclass(_class)) + JSObjectSetPrototype(context, value, Messages::Make(context, super, array)); + /*else if (array) + JSObjectSetPrototype(context, value, Array_prototype_);*/ + return value; +} - void (*GetValue())() const { - return reinterpret_cast(value_); - } -}; +JSObjectRef Internal::Make(JSContextRef context, id object, JSObjectRef owner) { + return JSObjectMake(context, Internal_, new Internal(object, context, owner)); +} -struct Closure_privateData : - Functor_privateData -{ - JSContextRef context_; - JSObjectRef function_; +namespace cy { +JSObjectRef Super::Make(JSContextRef context, id object, Class _class) { + JSObjectRef value(JSObjectMake(context, Super_, new Super(object, _class))); + return value; +} } - Closure_privateData(JSContextRef context, JSObjectRef function, const char *type) : - Functor_privateData(type, NULL), - context_(context), - function_(function) - { - JSValueProtect(context_, function_); - } +JSObjectRef Instance::Make(JSContextRef context, id object, Flags flags) { + JSObjectRef value(JSObjectMake(context, Instance_, new Instance(object, flags))); + JSObjectSetPrototype(context, value, CYGetClassPrototype(context, object_getClass(object))); + return value; +} - virtual ~Closure_privateData() { - JSValueUnprotect(context_, function_); - } -}; +Instance::~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]; +} -#ifdef __OBJC__ struct Message_privateData : - Functor_privateData +Functor_privateData { SEL sel_; @@ -1128,33 +1845,7 @@ JSObjectRef CYMakeInstance(JSContextRef context, id object, bool transient) { return Instance::Make(context, object, flags); } -#endif - -JSValueRef CYCastJSValue(JSContextRef context, bool value) { - return JSValueMakeBoolean(context, value); -} - -JSValueRef CYCastJSValue(JSContextRef context, double value) { - return JSValueMakeNumber(context, value); -} - -#define CYCastJSValue_(Type_) \ - JSValueRef CYCastJSValue(JSContextRef context, Type_ value) { \ - return JSValueMakeNumber(context, static_cast(value)); \ - } - -CYCastJSValue_(int) -CYCastJSValue_(unsigned int) -CYCastJSValue_(long int) -CYCastJSValue_(long unsigned int) -CYCastJSValue_(long long int) -CYCastJSValue_(long long unsigned int) - -JSValueRef CYJSUndefined(JSContextRef context) { - return JSValueMakeUndefined(context); -} -#ifdef __OBJC__ @interface NSMethodSignature (Cycript) - (NSString *) _typeString; @end @@ -1178,9 +1869,7 @@ JSValueRef CYJSUndefined(JSContextRef context) { @protocol Cycript - (JSValueRef) cy$JSValueInContext:(JSContextRef)context; @end -#endif -#ifdef __OBJC__ NSString *CYCastNSCYON(id value) { NSString *string; @@ -1215,9 +1904,7 @@ NSString *CYCastNSCYON(id value) { return string; } -#endif -#ifdef __OBJC__ #ifdef __APPLE__ struct PropertyAttributes { CYPool pool_; @@ -1290,54 +1977,168 @@ struct PropertyAttributes { temp[1] = 'e'; temp[2] = 't'; - if (length != 0) { - temp[3] = toupper(name[0]); - memcpy(temp + 4, name + 1, length - 1); - } + 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_; + } + +}; +#endif + +#ifdef __APPLE__ +NSObject *NSCFType$cy$toJSON(id self, SEL sel, NSString *key) { + return [(NSString *) CFCopyDescription((CFTypeRef) self) autorelease]; +} +#endif + +#ifndef __APPLE__ +@interface CYWebUndefined : NSObject { +} + ++ (CYWebUndefined *) undefined; + +@end + +@implementation CYWebUndefined + ++ (CYWebUndefined *) undefined { + static CYWebUndefined *instance_([[CYWebUndefined alloc] init]); + return instance_; +} + +@end + +#define WebUndefined CYWebUndefined +#endif + +/* Bridge: CYJSObject {{{ */ +@interface CYJSObject : NSMutableDictionary { + JSObjectRef object_; + JSContextRef context_; +} + +- (id) initWithJSObject:(JSObjectRef)object inContext:(JSContextRef)context; + +- (NSObject *) cy$toJSON:(NSString *)key; + +- (NSUInteger) count; +- (id) objectForKey:(id)key; +- (NSEnumerator *) keyEnumerator; +- (void) setObject:(id)object forKey:(id)key; +- (void) removeObjectForKey:(id)key; + +@end +/* }}} */ +/* Bridge: CYJSArray {{{ */ +@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 +/* }}} */ - temp[length + 3] = ':'; - temp[length + 4] = '\0'; - setter_ = temp; - } +NSObject *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]); +} - return setter_; +NSObject *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(); } +} -}; -#endif -#endif +NSNumber *CYCopyNSNumber(JSContextRef context, JSValueRef value) { + return [[NSNumber alloc] initWithDouble:CYCastDouble(context, value)]; +} -#ifdef __OBJC__ +id CYNSObject(apr_pool_t *pool, JSContextRef context, JSValueRef value, bool cast) { + id object; + bool copy; + + switch (JSType type = JSValueGetType(context, value)) { + case kJSTypeUndefined: + object = [WebUndefined undefined]; + copy = false; + break; + + case kJSTypeNull: + return NULL; + break; + + case kJSTypeBoolean: #ifdef __APPLE__ -NSObject *NSCFType$cy$toJSON(id self, SEL sel, NSString *key) { - return [(NSString *) CFCopyDescription((CFTypeRef) self) autorelease]; -} -#endif + object = (id) (CYCastBool(context, value) ? kCFBooleanTrue : kCFBooleanFalse); + copy = false; +#else + object = [[NSNumber alloc] initWithBool:CYCastBool(context, value)]; + copy = true; #endif + break; -#ifdef __OBJC__ -#ifndef __APPLE__ -@interface CYWebUndefined : NSObject { -} + case kJSTypeNumber: + object = CYCopyNSNumber(context, value); + copy = true; + break; -+ (CYWebUndefined *) undefined; + case kJSTypeString: + object = CYCopyNSString(context, value); + copy = true; + break; -@end + case kJSTypeObject: + // XXX: this might could be more efficient + object = CYCastNSObject(pool, context, (JSObjectRef) value); + copy = false; + break; -@implementation CYWebUndefined + default: + throw CYError(context, "JSValueGetType() == 0x%x", type); + break; + } -+ (CYWebUndefined *) undefined { - static CYWebUndefined *instance_([[CYWebUndefined alloc] init]); - return instance_; + if (cast != copy) + return object; + else if (copy) + return CYPoolRelease(pool, object); + else + return [object retain]; } -@end +NSObject *CYCastNSObject(apr_pool_t *pool, JSContextRef context, JSValueRef value) { + return CYNSObject(pool, context, value, true); +} -#define WebUndefined CYWebUndefined -#endif -#endif +NSObject *CYCopyNSObject(apr_pool_t *pool, JSContextRef context, JSValueRef value) { + return CYNSObject(pool, context, value, false); +} -#ifdef __OBJC__ /* Bridge: NSArray {{{ */ @implementation NSArray (Cycript) @@ -1530,9 +2331,9 @@ NSObject *NSCFType$cy$toJSON(id self, SEL sel, NSString *key) { return [self cy$JSType] != kJSTypeBoolean ? [self stringValue] : [self boolValue] ? @"true" : @"false"; } -- (JSValueRef) cy$JSValueInContext:(JSContextRef)context { +- (JSValueRef) cy$JSValueInContext:(JSContextRef)context { CYObjectiveTry { return [self cy$JSType] != kJSTypeBoolean ? CYCastJSValue(context, [self doubleValue]) : CYCastJSValue(context, [self boolValue]); -} +} CYObjectiveCatch } @end /* }}} */ @@ -1556,9 +2357,9 @@ NSObject *NSCFType$cy$toJSON(id self, SEL sel, NSString *key) { /* Bridge: NSObject {{{ */ @implementation NSObject (Cycript) -- (JSValueRef) cy$JSValueInContext:(JSContextRef)context { +- (JSValueRef) cy$JSValueInContext:(JSContextRef)context { CYObjectiveTry { return CYMakeInstance(context, self, false); -} +} CYObjectiveCatch } - (JSType) cy$JSType { return kJSTypeObject; @@ -1618,192 +2419,44 @@ NSObject *NSCFType$cy$toJSON(id self, SEL sel, NSString *key) { return self; } -- (NSString *) cy$toCYON { - std::ostringstream str; - CYUTF8String string(CYCastUTF8String(self)); - CYStringify(str, string.data, string.size); - std::string value(str.str()); - return CYCastNSString(NULL, CYUTF8String(value.c_str(), value.size())); -} - -- (NSString *) cy$toKey { - if (CYIsKey(CYCastUTF8String(self))) - return self; - return [self cy$toCYON]; -} - -@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 -/* }}} */ - -/* Bridge: CYJSObject {{{ */ -@interface CYJSObject : NSMutableDictionary { - JSObjectRef object_; - JSContextRef context_; -} - -- (id) initWithJSObject:(JSObjectRef)object inContext:(JSContextRef)context; - -- (NSObject *) cy$toJSON:(NSString *)key; - -- (NSUInteger) count; -- (id) objectForKey:(id)key; -- (NSEnumerator *) keyEnumerator; -- (void) setObject:(id)object forKey:(id)key; -- (void) removeObjectForKey:(id)key; - -@end -/* }}} */ -/* Bridge: CYJSArray {{{ */ -@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 -/* }}} */ -#endif - -#ifdef __OBJC__ -NSObject *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]); -} - -NSObject *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(); - } -} -#endif - -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; -} - -#ifdef __OBJC__ -NSNumber *CYCopyNSNumber(JSContextRef context, JSValueRef value) { - return [[NSNumber alloc] initWithDouble:CYCastDouble(context, value)]; -} -#endif - -bool CYCastBool(JSContextRef context, JSValueRef value) { - return JSValueToBoolean(context, value); -} - -#ifdef __OBJC__ -id CYNSObject(apr_pool_t *pool, JSContextRef context, JSValueRef value, bool cast) { - id object; - bool copy; - - switch (JSType type = JSValueGetType(context, value)) { - case kJSTypeUndefined: - object = [WebUndefined undefined]; - copy = false; - break; - - case kJSTypeNull: - return NULL; - break; - - case kJSTypeBoolean: -#ifdef __APPLE__ - object = (id) (CYCastBool(context, value) ? kCFBooleanTrue : kCFBooleanFalse); - copy = false; -#else - object = [[NSNumber alloc] initWithBool:CYCastBool(context, value)]; - copy = true; -#endif - break; - - case kJSTypeNumber: - object = CYCopyNSNumber(context, value); - copy = true; - break; - - case kJSTypeString: - object = CYCopyNSString(context, value); - copy = true; - break; +- (NSString *) cy$toCYON { + std::ostringstream str; + CYUTF8String string(CYCastUTF8String(self)); + CYStringify(str, string.data, string.size); + std::string value(str.str()); + return CYCastNSString(NULL, CYUTF8String(value.c_str(), value.size())); +} - case kJSTypeObject: - // XXX: this might could be more efficient - object = CYCastNSObject(pool, context, (JSObjectRef) value); - copy = false; - break; +- (NSString *) cy$toKey { + if (CYIsKey(CYCastUTF8String(self))) + return self; + return [self cy$toCYON]; +} - default: - _throw(NSInternalInconsistencyException, "JSValueGetType() == 0x%x", type); - break; - } +@end +/* }}} */ +/* Bridge: WebUndefined {{{ */ +@implementation WebUndefined (Cycript) - if (cast != copy) - return object; - else if (copy) - return CYPoolRelease(pool, object); - else - return [object retain]; +- (JSType) cy$JSType { + return kJSTypeUndefined; } -NSObject *CYCastNSObject(apr_pool_t *pool, JSContextRef context, JSValueRef value) { - return CYNSObject(pool, context, value, true); +- (NSObject *) cy$toJSON:(NSString *)key { + return self; } -NSObject *CYCopyNSObject(apr_pool_t *pool, JSContextRef context, JSValueRef value) { - return CYNSObject(pool, context, value, false); + +- (NSString *) cy$toCYON { + return @"undefined"; } +- (JSValueRef) cy$JSValueInContext:(JSContextRef)context { CYObjectiveTry { + return CYJSUndefined(context); +} CYObjectiveCatch } + +@end +/* }}} */ + static bool CYIsClass(id self) { // XXX: this is a lame object_isClass return class_getInstanceMethod(object_getClass(self), @selector(alloc)) != NULL; @@ -1813,7 +2466,7 @@ Class CYCastClass(apr_pool_t *pool, JSContextRef context, JSValueRef value) { id self(CYCastNSObject(pool, context, value)); if (CYIsClass(self)) return (Class) self; - _throw(NSInvalidArgumentException, "got something that is not a Class"); + throw CYError(context, "got something that is not a Class"); return NULL; } @@ -1825,85 +2478,32 @@ NSArray *CYCastNSArray(JSPropertyNameArrayRef names) { [array addObject:CYCastNSString(pool, JSPropertyNameArrayGetNameAtIndex(names, index))]; return array; } -#endif - -void CYThrow(JSContextRef context, JSValueRef value) { - if (value == NULL) - return; -#ifdef __OBJC__ - @throw CYCastNSObject(NULL, context, value); -#else - // XXX: fix this - throw "throwing something"; -#endif -} - -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)); -} -#ifdef __OBJC__ -JSValueRef CYCastJSValue(JSContextRef context, id value) { +JSValueRef CYCastJSValue(JSContextRef context, id value) { CYPoolTry { if (value == nil) return CYJSNull(context); else if ([value respondsToSelector:@selector(cy$JSValueInContext:)]) return [value cy$JSValueInContext:context]; else return CYMakeInstance(context, value, false); -} -#endif - -JSObjectRef CYCastJSObject(JSContextRef context, JSValueRef value) { - JSValueRef exception(NULL); - JSObjectRef object(JSValueToObject(context, value, &exception)); - CYThrow(context, exception); - return object; -} - -void CYThrow(JSContextRef context, NSException *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) { - return value != NULL && JSValueIsObject(context, value) && JSObjectIsFunction(context, (JSObjectRef) value); -} - -const char *CYPoolCCYON(apr_pool_t *pool, JSContextRef context, JSObjectRef object); +} CYPoolCatch(NULL) } -#ifdef __OBJC__ @implementation CYJSObject -- (id) initWithJSObject:(JSObjectRef)object inContext:(JSContextRef)context { +- (id) initWithJSObject:(JSObjectRef)object inContext:(JSContextRef)context { CYObjectiveTry { if ((self = [super init]) != nil) { object_ = object; context_ = context; JSValueProtect(context_, object_); } return self; -} +} CYObjectiveCatch } -- (void) dealloc { +- (void) dealloc { CYObjectiveTry { JSValueUnprotect(context_, object_); [super dealloc]; -} +} CYObjectiveCatch } -- (NSObject *) cy$toJSON:(NSString *)key { +- (NSObject *) cy$toJSON:(NSString *)key { CYObjectiveTry { JSValueRef toJSON(CYGetProperty(context_, object_, toJSON_)); if (!CYIsCallable(context_, toJSON)) return [super cy$toJSON:key]; @@ -1913,9 +2513,9 @@ const char *CYPoolCCYON(apr_pool_t *pool, JSContextRef context, JSObjectRef obje // XXX: do I really want an NSNull here?! return CYCastNSObject(NULL, context_, value) ?: [NSNull null]; } -} +} CYObjectiveCatch } -- (NSString *) cy$toCYON { +- (NSString *) cy$toCYON { CYObjectiveTry { CYPool pool; JSValueRef exception(NULL); const char *cyon(CYPoolCCYON(pool, context_, object_)); @@ -1924,61 +2524,61 @@ const char *CYPoolCCYON(apr_pool_t *pool, JSContextRef context, JSObjectRef obje return [super cy$toCYON]; else return [NSString stringWithUTF8String:cyon]; -} +} CYObjectiveCatch } -- (NSUInteger) count { +- (NSUInteger) count { CYObjectiveTry { JSPropertyNameArrayRef names(JSObjectCopyPropertyNames(context_, object_)); size_t size(JSPropertyNameArrayGetCount(names)); JSPropertyNameArrayRelease(names); return size; -} +} CYObjectiveCatch } -- (id) objectForKey:(id)key { +- (id) objectForKey:(id)key { CYObjectiveTry { JSValueRef value(CYGetProperty(context_, object_, CYJSString(key))); if (JSValueIsUndefined(context_, value)) return nil; return CYCastNSObject(NULL, context_, value) ?: [NSNull null]; -} +} CYObjectiveCatch } -- (NSEnumerator *) keyEnumerator { +- (NSEnumerator *) keyEnumerator { CYObjectiveTry { JSPropertyNameArrayRef names(JSObjectCopyPropertyNames(context_, object_)); NSEnumerator *enumerator([CYCastNSArray(names) objectEnumerator]); JSPropertyNameArrayRelease(names); return enumerator; -} +} CYObjectiveCatch } -- (void) setObject:(id)object forKey:(id)key { +- (void) setObject:(id)object forKey:(id)key { CYObjectiveTry { CYSetProperty(context_, object_, CYJSString(key), CYCastJSValue(context_, object)); -} +} CYObjectiveCatch } -- (void) removeObjectForKey:(id)key { +- (void) removeObjectForKey:(id)key { CYObjectiveTry { JSValueRef exception(NULL); (void) JSObjectDeleteProperty(context_, object_, CYJSString(key), &exception); CYThrow(context_, exception); -} +} CYObjectiveCatch } @end @implementation CYJSArray -- (id) initWithJSObject:(JSObjectRef)object inContext:(JSContextRef)context { +- (id) initWithJSObject:(JSObjectRef)object inContext:(JSContextRef)context { CYObjectiveTry { if ((self = [super init]) != nil) { object_ = object; context_ = context; JSValueProtect(context_, object_); } return self; -} +} CYObjectiveCatch } -- (void) dealloc { +- (void) dealloc { CYObjectiveTry { JSValueUnprotect(context_, object_); [super dealloc]; -} +} CYObjectiveCatch } -- (NSUInteger) count { +- (NSUInteger) count { CYObjectiveTry { return CYCastDouble(context_, CYGetProperty(context_, object_, length_)); -} +} CYObjectiveCatch } -- (id) objectAtIndex:(NSUInteger)index { +- (id) objectAtIndex:(NSUInteger)index { CYObjectiveTry { size_t bounds([self count]); if (index >= bounds) @throw [NSException exceptionWithName:NSRangeException reason:[NSString stringWithFormat:@"*** -[CYJSArray objectAtIndex:]: index (%zu) beyond bounds (%zu)", index, bounds] userInfo:nil]; @@ -1986,17 +2586,17 @@ const char *CYPoolCCYON(apr_pool_t *pool, JSContextRef context, JSObjectRef obje JSValueRef value(JSObjectGetPropertyAtIndex(context_, object_, index, &exception)); CYThrow(context_, exception); return CYCastNSObject(NULL, context_, value) ?: [NSNull null]; -} +} CYObjectiveCatch } -- (void) addObject:(id)object { +- (void) addObject:(id)object { CYObjectiveTry { JSValueRef exception(NULL); JSValueRef arguments[1]; arguments[0] = CYCastJSValue(context_, object); JSObjectCallAsFunction(context_, Array_push_, object_, 1, arguments, &exception); CYThrow(context_, exception); -} +} CYObjectiveCatch } -- (void) insertObject:(id)object atIndex:(NSUInteger)index { +- (void) insertObject:(id)object atIndex:(NSUInteger)index { CYObjectiveTry { size_t bounds([self count] + 1); if (index >= bounds) @throw [NSException exceptionWithName:NSRangeException reason:[NSString stringWithFormat:@"*** -[CYJSArray insertObject:atIndex:]: index (%zu) beyond bounds (%zu)", index, bounds] userInfo:nil]; @@ -2006,162 +2606,36 @@ const char *CYPoolCCYON(apr_pool_t *pool, JSContextRef context, JSObjectRef obje 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 { - size_t bounds([self count]); - if (index >= bounds) - @throw [NSException exceptionWithName:NSRangeException reason:[NSString stringWithFormat:@"*** -[CYJSArray removeObjectAtIndex:]: index (%zu) beyond bounds (%zu)", index, bounds] userInfo:nil]; - 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 { - size_t bounds([self count]); - if (index >= bounds) - @throw [NSException exceptionWithName:NSRangeException reason:[NSString stringWithFormat:@"*** -[CYJSArray replaceObjectAtIndex:withObject:]: index (%zu) beyond bounds (%zu)", index, bounds] userInfo:nil]; - CYSetProperty(context_, object_, index, CYCastJSValue(context_, object)); -} - -@end -#endif - -const char *CYPoolCCYON(apr_pool_t *pool, JSContextRef context, JSValueRef value, JSValueRef *exception) { - switch (JSType type = JSValueGetType(context, value)) { - case kJSTypeUndefined: - return "undefined"; - case kJSTypeNull: - return "null"; - case kJSTypeBoolean: - return CYCastBool(context, value) ? "true" : "false"; - - case kJSTypeNumber: { - std::ostringstream str; - CYNumerify(str, CYCastDouble(context, value)); - std::string value(str.str()); - return apr_pstrmemdup(pool, value.c_str(), value.size()); - } break; - - case kJSTypeString: { - std::ostringstream str; - CYUTF8String string(CYPoolUTF8String(pool, CYJSString(context, value))); - CYStringify(str, string.data, string.size); - std::string value(str.str()); - return apr_pstrmemdup(pool, value.c_str(), value.size()); - } break; - - case kJSTypeObject: CYTry { - return CYPoolCCYON(pool, context, (JSObjectRef) value); - } CYCatch - - default: - _throw(NSInternalInconsistencyException, "JSValueGetType() == 0x%x", type); - return NULL; - break; - } -} - -const char *CYPoolCCYON(apr_pool_t *pool, JSContextRef context, JSValueRef value) { - JSValueRef exception(NULL); - const char *cyon(CYPoolCCYON(pool, context, value, &exception)); - CYThrow(context, exception); - return cyon; -} - -const char *CYPoolCCYON(apr_pool_t *pool, JSContextRef context, JSObjectRef object) { - JSValueRef toCYON(CYGetProperty(context, object, toCYON_)); - if (CYIsCallable(context, toCYON)) { - JSValueRef value(CYCallAsFunction(context, (JSObjectRef) toCYON, object, 0, NULL)); - return CYPoolCString(pool, context, value); - } - - JSValueRef toJSON(CYGetProperty(context, object, toJSON_)); - if (CYIsCallable(context, toJSON)) { - JSValueRef arguments[1] = {CYCastJSValue(context, CYJSString(""))}; - JSValueRef exception(NULL); - const char *cyon(CYPoolCCYON(pool, context, CYCallAsFunction(context, (JSObjectRef) toJSON, object, 1, arguments), &exception)); - CYThrow(context, exception); - return cyon; - } - - std::ostringstream str; - - str << '{'; - - // XXX: this is, sadly, going to leak - JSPropertyNameArrayRef names(JSObjectCopyPropertyNames(context, object)); - - bool comma(false); - - for (size_t index(0), count(JSPropertyNameArrayGetCount(names)); index != count; ++index) { - JSStringRef name(JSPropertyNameArrayGetNameAtIndex(names, index)); - JSValueRef value(CYGetProperty(context, object, name)); - - if (comma) - str << ','; - else - comma = true; - - CYUTF8String string(CYPoolUTF8String(pool, name)); - if (CYIsKey(string)) - str << string.data; - else - CYStringify(str, string.data, string.size); - - str << ':' << CYPoolCCYON(pool, context, value); - } - - str << '}'; - - JSPropertyNameArrayRelease(names); - - std::string string(str.str()); - return apr_pstrmemdup(pool, string.c_str(), string.size()); -} - -static JSValueRef Array_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { - CYPool pool; - std::ostringstream str; - - str << '['; - - JSValueRef length(CYGetProperty(context, _this, length_)); - bool comma(false); - - for (size_t index(0), count(CYCastDouble(context, length)); index != count; ++index) { - JSValueRef value(CYGetProperty(context, _this, index)); - - if (comma) - str << ','; - else - comma = true; - - if (!JSValueIsUndefined(context, value)) - str << CYPoolCCYON(pool, context, value); - else { - str << ','; - comma = false; - } - } + CYThrow(context_, exception); +} CYObjectiveCatch } - str << ']'; +- (void) removeLastObject { CYObjectiveTry { + JSValueRef exception(NULL); + JSObjectCallAsFunction(context_, Array_pop_, object_, 0, NULL, &exception); + CYThrow(context_, exception); +} CYObjectiveCatch } - std::string value(str.str()); - return CYCastJSValue(context, CYJSString(CYUTF8String(value.c_str(), value.size()))); -} CYCatch } +- (void) removeObjectAtIndex:(NSUInteger)index { CYObjectiveTry { + size_t bounds([self count]); + if (index >= bounds) + @throw [NSException exceptionWithName:NSRangeException reason:[NSString stringWithFormat:@"*** -[CYJSArray removeObjectAtIndex:]: index (%zu) beyond bounds (%zu)", index, bounds] userInfo:nil]; + 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); +} CYObjectiveCatch } + +- (void) replaceObjectAtIndex:(NSUInteger)index withObject:(id)object { CYObjectiveTry { + size_t bounds([self count]); + if (index >= bounds) + @throw [NSException exceptionWithName:NSRangeException reason:[NSString stringWithFormat:@"*** -[CYJSArray replaceObjectAtIndex:withObject:]: index (%zu) beyond bounds (%zu)", index, bounds] userInfo:nil]; + CYSetProperty(context_, object_, index, CYCastJSValue(context_, object)); +} CYObjectiveCatch } + +@end -#ifdef __OBJC__ // XXX: use objc_getAssociatedObject and objc_setAssociatedObject on 10.6 struct CYInternal : CYData @@ -2218,54 +2692,12 @@ struct CYInternal : CYSetProperty(context, object_, name, value); } }; -#endif -#ifdef __OBJC__ static JSObjectRef CYMakeSelector(JSContextRef context, SEL sel) { Selector_privateData *internal(new Selector_privateData(sel)); return JSObjectMake(context, Selector_, internal); } -#endif - -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); -} - -static JSObjectRef CYMakeFunctor(JSContextRef context, void (*function)(), const char *type) { - Functor_privateData *internal(new Functor_privateData(type, function)); - return JSObjectMake(context, Functor_, internal); -} - -static bool CYGetOffset(apr_pool_t *pool, JSStringRef value, ssize_t &index) { - return CYGetOffset(CYPoolCString(pool, value), index); -} - -static 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: - double number(CYCastDouble(context, value)); - if (std::isnan(number)) - _throw(NSInvalidArgumentException, "cannot convert value to pointer"); - return reinterpret_cast(static_cast(static_cast(number))); - } -} - -template -static _finline Type_ CYCastPointer(JSContextRef context, JSValueRef value) { - return reinterpret_cast(CYCastPointer_(context, value)); -} -#ifdef __OBJC__ static SEL CYCastSEL(JSContextRef context, JSValueRef value) { if (JSValueIsObjectOfClass(context, value, Selector_)) { Selector_privateData *internal(reinterpret_cast(JSObjectGetPrivate((JSObjectRef) value))); @@ -2273,33 +2705,25 @@ static SEL CYCastSEL(JSContextRef context, JSValueRef value) { } else return CYCastPointer(context, value); } -#endif -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); - break; +void *CYObjectiveC_ExecuteStart() { + return (void *) [[NSAutoreleasePool alloc] init]; +} -#define CYPoolFFI_(primitive, native) \ - case sig::primitive ## _P: \ - *reinterpret_cast(data) = CYCastDouble(context, value); \ - break; +void CYObjectiveC_ExecuteEnd(void *handle) { + return [(NSAutoreleasePool *) handle release]; +} - 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) +JSValueRef CYObjectiveC_RuntimeProperty(JSContextRef context, CYUTF8String name) { + if (name == "nil") + return Instance::Make(context, nil); + if (Class _class = objc_getClass(name.data)) + return CYMakeInstance(context, _class, true); + return NULL; +} -#ifdef __OBJC__ +static bool CYObjectiveC_PoolFFI(apr_pool_t *pool, JSContextRef context, sig::Type *type, ffi_type *ffi, void *data, JSValueRef value) { + switch (type->primitive) { case sig::object_P: case sig::typename_P: *reinterpret_cast(data) = CYCastNSObject(pool, context, value); @@ -2308,132 +2732,47 @@ static void CYPoolFFI(apr_pool_t *pool, JSContextRef context, sig::Type *type, f case sig::selector_P: *reinterpret_cast(data) = CYCastSEL(context, value); break; -#endif - - 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(NSInvalidArgumentException, "unable to extract structure value"); - } - } - - CYPoolFFI(pool, context, element->type, field, base, rhs); - // XXX: alignment? - base += field->size; - } - } break; - - case sig::void_P: - break; default: - fprintf(stderr, "CYPoolFFI(%c)\n", type->primitive); - _assert(false); + return false; } -} -static JSValueRef CYFromFFI(JSContextRef context, sig::Type *type, ffi_type *ffi, void *data, bool initialize = false, JSObjectRef owner = NULL) { - JSValueRef value; + return true; +} +static JSValueRef CYObjectiveC_FromFFI(JSContextRef context, sig::Type *type, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) { 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) - -#ifdef __OBJC__ - case sig::object_P: { + case sig::object_P: if (id object = *reinterpret_cast(data)) { - value = CYCastJSValue(context, object); + JSValueRef value(CYCastJSValue(context, object)); if (initialize) [object release]; + return value; } else goto null; - } break; case sig::typename_P: - value = CYMakeInstance(context, *reinterpret_cast(data), true); - break; + return CYMakeInstance(context, *reinterpret_cast(data), true); case sig::selector_P: if (SEL sel = *reinterpret_cast(data)) - value = CYMakeSelector(context, sel); - else goto null; - break; -#endif - - 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); + return CYMakeSelector(context, sel); 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; - + return CYJSNull(context); default: - fprintf(stderr, "CYFromFFI(%c)\n", type->primitive); - _assert(false); + return NULL; } - - return value; } -#ifdef __OBJC__ +CYHooks CYObjectiveCHooks = { + &CYObjectiveC_ExecuteStart, + &CYObjectiveC_ExecuteEnd, + &CYObjectiveC_RuntimeProperty, + &CYObjectiveC_PoolFFI, + &CYObjectiveC_FromFFI, +}; + static bool CYImplements(id object, Class _class, SEL selector, bool devoid) { if (objc_method *method = class_getInstanceMethod(_class, selector)) { if (!devoid) @@ -2447,10 +2786,8 @@ static bool CYImplements(id object, Class _class, SEL selector, bool devoid) { // XXX: possibly use a more "awesome" check? return false; } -#endif -#ifdef __OBJC__ -static const char *CYPoolTypeEncoding(apr_pool_t *pool, Class _class, SEL sel, objc_method *method) { +static const char *CYPoolTypeEncoding(apr_pool_t *pool, JSContextRef context, Class _class, SEL sel, objc_method *method) { if (method != NULL) return method_getTypeEncoding(method); @@ -2478,94 +2815,35 @@ static const char *CYPoolTypeEncoding(apr_pool_t *pool, Class _class, SEL sel, o mode = -1; value = NULL; } else { - mode = sqlite3_column_int(statement, 0); - value = sqlite3_column_pooled(pool, statement, 1); - } - - _sqlcall(sqlite3_finalize(statement)); - - if (value != NULL) - return value; - - return NULL; -} -#endif - -static 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); -} - -#ifdef __OBJC__ -static 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]); - - JSObjectRef _this(CYCastJSObject(context, values[0])); - - JSValueRef value(CYCallAsFunction(context, internal->function_, _this, count - 2, values + 2)); - CYPoolFFI(NULL, context, internal->signature_.elements[0].type, internal->cif_.rtype, result, value); -} -#endif - -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(CYGetJSContext(), function, type)); - - ffi_closure *closure((ffi_closure *) _syscall(mmap( - NULL, sizeof(ffi_closure), - PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, - -1, 0 - ))); - - ffi_status status(ffi_prep_closure(closure, &internal->cif_, callback, internal)); - _assert(status == FFI_OK); + mode = sqlite3_column_int(statement, 0); + value = sqlite3_column_pooled(pool, statement, 1); + } - _syscall(mprotect(closure, sizeof(*closure), PROT_READ | PROT_EXEC)); + _sqlcall(sqlite3_finalize(statement)); - internal->value_ = closure; + if (value != NULL) + return value; - return internal; + return NULL; } -static JSObjectRef CYMakeFunctor(JSContextRef context, JSObjectRef function, const char *type) { - Closure_privateData *internal(CYMakeFunctor_(context, function, type, &FunctionClosure_)); - return JSObjectMake(context, Functor_, internal); -} +static void MessageClosure_(ffi_cif *cif, void *result, void **arguments, void *arg) { + Closure_privateData *internal(reinterpret_cast(arg)); -static JSObjectRef CYMakeFunctor(JSContextRef context, JSValueRef value, const char *type) { - JSValueRef exception(NULL); - bool function(JSValueIsInstanceOfConstructor(context, value, Function_, &exception)); - CYThrow(context, exception); + JSContextRef context(internal->context_); - if (function) { - JSObjectRef function(CYCastJSObject(context, value)); - return CYMakeFunctor(context, function, type); - } else { - void (*function)()(CYCastPointer(context, value)); - return CYMakeFunctor(context, function, type); - } + 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]); + + JSObjectRef _this(CYCastJSObject(context, values[0])); + + JSValueRef value(CYCallAsFunction(context, internal->function_, _this, count - 2, values + 2)); + CYPoolFFI(NULL, context, internal->signature_.elements[0].type, internal->cif_.rtype, result, value); } -#ifdef __OBJC__ 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); @@ -2582,7 +2860,7 @@ static bool Messages_hasProperty(JSContextRef context, JSObjectRef object, JSStr Class _class(internal->GetValue()); CYPool pool; - const char *name(CYPoolCString(pool, property)); + const char *name(CYPoolCString(pool, context, property)); if (SEL sel = sel_getUid(name)) if (class_getInstanceMethod(_class, sel) != NULL) @@ -2596,7 +2874,7 @@ static JSValueRef Messages_getProperty(JSContextRef context, JSObjectRef object, Class _class(internal->GetValue()); CYPool pool; - const char *name(CYPoolCString(pool, property)); + const char *name(CYPoolCString(pool, context, property)); if (SEL sel = sel_getUid(name)) if (objc_method *method = class_getInstanceMethod(_class, sel)) @@ -2610,7 +2888,7 @@ static bool Messages_setProperty(JSContextRef context, JSObjectRef object, JSStr Class _class(internal->GetValue()); CYPool pool; - const char *name(CYPoolCString(pool, property)); + const char *name(CYPoolCString(pool, context, property)); SEL sel(sel_registerName(name)); @@ -2624,7 +2902,7 @@ static bool Messages_setProperty(JSContextRef context, JSObjectRef object, JSStr type = sig::Unparse(pool, &message->signature_); imp = reinterpret_cast(message->GetValue()); } else { - type = CYPoolTypeEncoding(pool, _class, sel, method); + type = CYPoolTypeEncoding(pool, context, _class, sel, method); imp = CYMakeMessage(context, value, type); } @@ -2689,7 +2967,7 @@ static bool Instance_hasProperty(JSContextRef context, JSObjectRef object, JSStr return true; } CYPoolCatch(false) - const char *string(CYPoolCString(pool, name)); + const char *string(CYPoolCString(pool, context, name)); if (class_getProperty(_class, string) != NULL) return true; @@ -2701,450 +2979,229 @@ static bool Instance_hasProperty(JSContextRef context, JSObjectRef object, JSStr return false; } -static JSValueRef Instance_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { +static JSValueRef Instance_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry { Instance *internal(reinterpret_cast(JSObjectGetPrivate(object))); id self(internal->GetValue()); if (JSStringIsEqualToUTF8CString(property, "$cyi")) return Internal::Make(context, self, object); - CYTry { - CYPool pool; - NSString *name(CYCastNSString(pool, property)); + CYPool pool; + NSString *name(CYCastNSString(pool, property)); - if (CYInternal *internal = CYInternal::Get(self)) - if (JSValueRef value = internal->GetProperty(context, property)) - return value; + if (CYInternal *internal = CYInternal::Get(self)) + if (JSValueRef value = internal->GetProperty(context, property)) + return value; - CYPoolTry { - if (NSObject *data = [self cy$getProperty:name]) - return CYCastJSValue(context, data); - } CYPoolCatch(NULL) + CYPoolTry { + if (NSObject *data = [self cy$getProperty:name]) + return CYCastJSValue(context, data); + } CYPoolCatch(NULL) - const char *string(CYPoolCString(pool, name)); - Class _class(object_getClass(self)); + const char *string(CYPoolCString(pool, context, name)); + Class _class(object_getClass(self)); #ifdef __APPLE__ - if (objc_property_t property = class_getProperty(_class, string)) { - PropertyAttributes attributes(property); - SEL sel(sel_registerName(attributes.Getter())); - return CYSendMessage(pool, context, self, NULL, sel, 0, NULL, false, exception); - } + if (objc_property_t property = class_getProperty(_class, string)) { + PropertyAttributes attributes(property); + SEL sel(sel_registerName(attributes.Getter())); + return CYSendMessage(pool, context, self, NULL, sel, 0, NULL, false, exception); + } #endif - if (SEL sel = sel_getUid(string)) - if (CYImplements(self, _class, sel, true)) - return CYSendMessage(pool, context, self, NULL, sel, 0, NULL, false, exception); + if (SEL sel = sel_getUid(string)) + if (CYImplements(self, _class, sel, true)) + return CYSendMessage(pool, context, self, NULL, sel, 0, NULL, false, exception); - return NULL; - } CYCatch -} + return NULL; +} CYCatch } -static bool Instance_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) { +static bool Instance_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) { CYTry { Instance *internal(reinterpret_cast(JSObjectGetPrivate(object))); id self(internal->GetValue()); CYPool pool; - CYTry { - NSString *name(CYCastNSString(pool, property)); - NSObject *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)); - -#ifdef __APPLE__ - 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, NULL, sel, 1, arguments, false, exception); - return true; - } - } -#endif - - 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, NULL, sel, 1, arguments, false, exception); - } + NSString *name(CYCastNSString(pool, property)); + NSObject *data(CYCastNSObject(pool, context, value)); - if (CYInternal *internal = CYInternal::Set(self)) { - internal->SetProperty(context, property, value); + CYPoolTry { + if ([self cy$setProperty:name to:data]) return true; - } - - return false; - } CYCatch -} - -static bool Instance_deleteProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { - Instance *internal(reinterpret_cast(JSObjectGetPrivate(object))); - id self(internal->GetValue()); - - CYTry { - CYPoolTry { - NSString *name(CYCastNSString(NULL, property)); - return [self cy$deleteProperty:name]; - } CYPoolCatch(NULL) - } CYCatch -} - -static void Instance_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) { - Instance *internal(reinterpret_cast(JSObjectGetPrivate(object))); - id self(internal->GetValue()); + } CYPoolCatch(NULL) - CYPool pool; + const char *string(CYPoolCString(pool, context, name)); Class _class(object_getClass(self)); #ifdef __APPLE__ - { - 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); - } -#endif -} - -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 -} - -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; - - 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)); - - 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 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); + 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, NULL, sel, 1, arguments, false, exception); return true; } - - return false; - } CYCatch -} - -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)); - - Internal_getPropertyNames_(_class, names); -} - -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->GetOwner(); -} + } #endif -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) - 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; + size_t length(strlen(string)); - sig::Element *elements(type->data.signature.elements); + char set[length + 5]; - 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; - } - } + set[0] = 's'; + set[1] = 'e'; + set[2] = 't'; - return false; - } else { - index = static_cast(number); - if (index != number || index < 0 || static_cast(index) >= count) - return false; + if (string[0] != '\0') { + set[3] = toupper(string[0]); + memcpy(set + 4, string + 1, length - 1); } - 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; -} - -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->GetOwner() ?: object); - - CYTry { - return CYFromFFI(context, typical->type_, ffi, base, false, owner); - } CYCatch -} - -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_); - - if (typical->type_ == NULL) - return NULL; - - ssize_t offset; - if (!CYGetOffset(pool, property, offset)) - return NULL; - - return Pointer_getIndex(context, object, offset, exception); -} - -static JSValueRef Pointer_getProperty_$cyi(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { - return Pointer_getIndex(context, object, 0, exception); -} - -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_); + set[length + 3] = ':'; + set[length + 4] = '\0'; - ffi_type *ffi(typical->GetFFI()); - - uint8_t *base(reinterpret_cast(internal->value_)); - base += ffi->size * index; + if (SEL sel = sel_getUid(set)) + if (CYImplements(self, _class, sel, false)) { + JSValueRef arguments[1] = {value}; + CYSendMessage(pool, context, self, NULL, sel, 1, arguments, false, exception); + } - CYTry { - CYPoolFFI(NULL, context, typical->type_, ffi, base, value); + if (CYInternal *internal = CYInternal::Set(self)) { + internal->SetProperty(context, property, value); return true; - } CYCatch -} + } -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_); + return false; +} CYCatch } - if (typical->type_ == NULL) - return NULL; +static bool Instance_deleteProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry { + Instance *internal(reinterpret_cast(JSObjectGetPrivate(object))); + id self(internal->GetValue()); - ssize_t offset; - if (!CYGetOffset(pool, property, offset)) - return NULL; + CYPoolTry { + NSString *name(CYCastNSString(NULL, property)); + return [self cy$deleteProperty:name]; + } CYPoolCatch(NULL) +} CYCatch } - return Pointer_setIndex(context, object, offset, value, exception); -} +static void Instance_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) { + Instance *internal(reinterpret_cast(JSObjectGetPrivate(object))); + id self(internal->GetValue()); -static bool Pointer_setProperty_$cyi(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) { - return Pointer_setIndex(context, object, 0, value, exception); -} + CYPool pool; + Class _class(object_getClass(self)); -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); +#ifdef __APPLE__ + { + 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); + } +#endif } -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_); - - ssize_t index; - uint8_t *base; +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 } - CYTry { - if (!Index_(pool, internal, property, index, base)) - return NULL; +static bool Instance_hasInstance(JSContextRef context, JSObjectRef constructor, JSValueRef instance, JSValueRef *exception) { CYTry { + Instance *internal(reinterpret_cast(JSObjectGetPrivate((JSObjectRef) constructor))); + Class _class(internal->GetValue()); + if (!CYIsClass(_class)) + return false; - JSObjectRef owner(internal->GetOwner() ?: object); + if (JSValueIsObjectOfClass(context, instance, Instance_)) { + Instance *linternal(reinterpret_cast(JSObjectGetPrivate((JSObjectRef) instance))); + // XXX: this isn't always safe + return [linternal->GetValue() isKindOfClass:_class]; + } - return CYFromFFI(context, typical->type_->data.signature.elements[index].type, typical->GetFFI()->elements[index], base, false, owner); - } CYCatch -} + return false; +} CYCatch } -static bool Struct_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) { +static bool Internal_hasProperty(JSContextRef context, JSObjectRef object, JSStringRef property) { + Internal *internal(reinterpret_cast(JSObjectGetPrivate(object))); CYPool pool; - Struct_privateData *internal(reinterpret_cast(JSObjectGetPrivate(object))); - Type_privateData *typical(internal->type_); - - ssize_t index; - uint8_t *base; - CYTry { - if (!Index_(pool, internal, property, index, base)) - return false; + id self(internal->GetValue()); + const char *name(CYPoolCString(pool, context, property)); - CYPoolFFI(NULL, context, typical->type_->data.signature.elements[index].type, typical->GetFFI()->elements[index], base, value); + if (object_getInstanceVariable(self, name, NULL) != NULL) return true; - } CYCatch + + return false; } -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_); +static JSValueRef Internal_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry { + Internal *internal(reinterpret_cast(JSObjectGetPrivate(object))); + CYPool pool; - if (type == NULL) - return; + id self(internal->GetValue()); + const char *name(CYPoolCString(pool, context, property)); - size_t count(type->data.signature.count); - sig::Element *elements(type->data.signature.elements); + 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)); + } - char number[32]; + return NULL; +} CYCatch } - for (size_t index(0); index != count; ++index) { - const char *name; - name = elements[index].name; +static bool Internal_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) { CYTry { + Internal *internal(reinterpret_cast(JSObjectGetPrivate(object))); + CYPool pool; - if (name == NULL) { - sprintf(number, "%lu", index); - name = number; - } + id self(internal->GetValue()); + const char *name(CYPoolCString(pool, context, property)); - JSPropertyNameAccumulatorAddName(names, CYJSString(name)); + 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; } -} -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(NSInvalidArgumentException, "incorrect number of arguments to ffi function"); + return false; +} CYCatch } - size_t size(setups + count); - void *values[size]; - memcpy(values, setup, sizeof(void *) * setups); +static void Internal_getPropertyNames_(Class _class, JSPropertyNameAccumulatorRef names) { + if (Class super = class_getSuperclass(_class)) + Internal_getPropertyNames_(super, names); - 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[ffi->size]; - CYPoolFFI(pool, context, element->type, ffi, values[index], arguments[index - setups]); - } + 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; - uint8_t value[cif->rtype->size]; - ffi_call(cif, function, value, values); + id self(internal->GetValue()); + Class _class(object_getClass(self)); - return CYFromFFI(context, signature->elements[0].type, cif->rtype, value, initialize); - } CYCatch + Internal_getPropertyNames_(_class, names); } -#ifdef __OBJC__ -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 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->GetOwner(); } +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))); @@ -3166,29 +3223,27 @@ static void ObjectiveC_Classes_getPropertyNames(JSContextRef context, JSObjectRe free(data); } -static JSValueRef ObjectiveC_Image_Classes_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { +static JSValueRef ObjectiveC_Image_Classes_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry { 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 -} + CYPool pool; + const char *name(CYPoolCString(pool, context, 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))); @@ -3199,27 +3254,25 @@ static void ObjectiveC_Image_Classes_getPropertyNames(JSContextRef context, JSOb 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 JSValueRef ObjectiveC_Images_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry { + CYPool pool; + const char *name(CYPoolCString(pool, context, 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; @@ -3229,15 +3282,13 @@ static void ObjectiveC_Images_getPropertyNames(JSContextRef context, JSObjectRef 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 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 void ObjectiveC_Protocols_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) { unsigned int size; @@ -3246,112 +3297,15 @@ static void ObjectiveC_Protocols_getPropertyNames(JSContextRef context, JSObject JSPropertyNameAccumulatorAddName(names, CYJSString(protocol_getName(data[i]))); free(data); } -#endif - -static JSObjectRef CYMakeType(JSContextRef context, const char *type) { - Type_privateData *internal(new Type_privateData(NULL, type)); - return JSObjectMake(context, Type_privateData::Class_, internal); -} - -static JSObjectRef CYMakeType(JSContextRef context, sig::Type *type) { - Type_privateData *internal(new Type_privateData(type)); - return JSObjectMake(context, Type_privateData::Class_, internal); -} - -static void *CYCastSymbol(const char *name) { - return dlsym(RTLD_DEFAULT, name); -} - -static JSValueRef Runtime_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { -#ifdef __OBJC__ - if (JSStringIsEqualToUTF8CString(property, "nil")) - return Instance::Make(context, nil); -#endif - - CYTry { - CYPool pool; - const char *name(CYPoolCString(pool, property)); - -#ifdef __OBJC__ - if (Class _class = objc_getClass(name)) - return CYMakeInstance(context, _class, true); -#endif - - sqlite3_stmt *statement; - - _sqlcall(sqlite3_prepare(Bridge_, - "select " - "\"bridge\".\"mode\", " - "\"bridge\".\"value\" " - "from \"bridge\" " - "where" - " \"bridge\".\"name\" = ?" - " limit 1" - , -1, &statement, NULL)); - - _sqlcall(sqlite3_bind_text(statement, 1, name, -1, SQLITE_STATIC)); - - int mode; - const char *value; - - if (_sqlcall(sqlite3_step(statement)) == SQLITE_DONE) { - mode = -1; - value = NULL; - } else { - mode = sqlite3_column_int(statement, 0); - value = sqlite3_column_pooled(pool, statement, 1); - } - - _sqlcall(sqlite3_finalize(statement)); - - switch (mode) { - default: - _assert(false); - case -1: - return NULL; - - case 0: - return JSEvaluateScript(CYGetJSContext(), CYJSString(value), NULL, NULL, 0, NULL); - case 1: - return CYMakeFunctor(context, reinterpret_cast(CYCastSymbol(name)), value); - - case 2: { - // XXX: this is horrendously inefficient - sig::Signature signature; - sig::Parse(pool, &signature, value, &Structor_); - ffi_cif cif; - sig::sig_ffi_cif(pool, &sig::ObjectiveC, &signature, &cif); - return CYFromFFI(context, signature.elements[0].type, cif.rtype, CYCastSymbol(name)); - } - - // XXX: implement case 3 - case 4: - return CYMakeType(context, value); - } - } CYCatch -} -#ifdef __OBJC__ 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 ); } -#endif - -static JSValueRef System_print(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { - CYTry { - if (count == 0) - printf("\n"); - else - printf("%s\n", CYCastCString(context, arguments[0])); - return CYJSUndefined(context); - } CYCatch -} -#ifdef __OBJC__ -JSValueRef CYSendMessage(apr_pool_t *pool, JSContextRef context, id self, Class _class, SEL _cmd, size_t count, const JSValueRef arguments[], bool initialize, JSValueRef *exception) { +JSValueRef CYSendMessage(apr_pool_t *pool, JSContextRef context, id self, Class _class, SEL _cmd, size_t count, const JSValueRef arguments[], bool initialize, JSValueRef *exception) { CYTry { const char *type; if (_class == NULL) @@ -3360,14 +3314,12 @@ JSValueRef CYSendMessage(apr_pool_t *pool, JSContextRef context, id self, Class if (objc_method *method = class_getInstanceMethod(_class, _cmd)) type = method_getTypeEncoding(method); else { - CYTry { - CYPoolTry { - NSMethodSignature *method([self methodSignatureForSelector:_cmd]); - if (method == nil) - _throw(NSInvalidArgumentException, "unrecognized selector %s sent to object %p", sel_getName(_cmd), self); - type = CYPoolCString(pool, [method _typeString]); - } CYPoolCatch(NULL) - } CYCatch + CYPoolTry { + NSMethodSignature *method([self methodSignatureForSelector:_cmd]); + if (method == nil) + throw CYError(context, "unrecognized selector %s sent to object %p", sel_getName(_cmd), self); + type = CYPoolCString(pool, context, [method _typeString]); + } CYPoolCatch(NULL) } objc_super super = {self, _class}; @@ -3385,19 +3337,12 @@ JSValueRef CYSendMessage(apr_pool_t *pool, JSContextRef context, id self, Class void (*function)() = stret(cif.rtype) ? reinterpret_cast(&objc_msgSendSuper_stret) : reinterpret_cast(&objc_msgSendSuper); return CYCallFunction(pool, context, 2, setup, count, arguments, initialize, exception, &signature, &cif, function); -} -#endif - -static size_t Nonce_(0); +} CYCatch } -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) { CYTry { + if (count < 2) + throw CYError(context, "too few arguments to objc_msgSend"); -#ifdef __OBJC__ -static JSValueRef $objc_msgSend(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYPool pool; bool uninitialized; @@ -3406,39 +3351,32 @@ static JSValueRef $objc_msgSend(JSContextRef context, JSObjectRef object, JSObje SEL _cmd; Class _class; - CYTry { - if (count < 2) - _throw(NSInvalidArgumentException, "too few arguments to objc_msgSend"); - - if (JSValueIsObjectOfClass(context, arguments[0], Super_)) { - cy::Super *internal(reinterpret_cast(JSObjectGetPrivate((JSObjectRef) arguments[0]))); - self = internal->GetValue(); - _class = internal->class_;; - uninitialized = false; - } else if (JSValueIsObjectOfClass(context, arguments[0], Instance_)) { - Instance *internal(reinterpret_cast(JSObjectGetPrivate((JSObjectRef) arguments[0]))); - self = internal->GetValue(); - _class = nil; - uninitialized = internal->IsUninitialized(); - if (uninitialized) - internal->value_ = nil; - } else { - self = CYCastNSObject(pool, context, arguments[0]); - _class = nil; - uninitialized = false; - } + if (JSValueIsObjectOfClass(context, arguments[0], Super_)) { + cy::Super *internal(reinterpret_cast(JSObjectGetPrivate((JSObjectRef) arguments[0]))); + self = internal->GetValue(); + _class = internal->class_;; + uninitialized = false; + } else if (JSValueIsObjectOfClass(context, arguments[0], Instance_)) { + Instance *internal(reinterpret_cast(JSObjectGetPrivate((JSObjectRef) arguments[0]))); + self = internal->GetValue(); + _class = nil; + uninitialized = internal->IsUninitialized(); + if (uninitialized) + internal->value_ = nil; + } else { + self = CYCastNSObject(pool, context, arguments[0]); + _class = nil; + uninitialized = false; + } - if (self == nil) - return CYJSNull(context); + if (self == nil) + return CYJSNull(context); - _cmd = CYCastSEL(context, arguments[1]); - } CYCatch + _cmd = CYCastSEL(context, arguments[1]); return CYSendMessage(pool, context, self, _class, _cmd, count - 2, arguments + 2, uninitialized, exception); -} -#endif +} CYCatch } -#ifdef __OBJC__ /* Hook: objc_registerClassPair {{{ */ // XXX: replace this with associated objects @@ -3460,28 +3398,19 @@ MSHook(void, objc_registerClassPair, Class _class) { _objc_registerClassPair(_class); } -static JSValueRef objc_registerClassPair_(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { - CYTry { - if (count != 1) - _throw(NSInvalidArgumentException, "incorrect number of arguments to objc_registerClassPair"); - CYPool pool; - NSObject *value(CYCastNSObject(pool, context, arguments[0])); - if (value == NULL || !CYIsClass(value)) - _throw(NSInvalidArgumentException, "incorrect number of arguments to objc_registerClassPair"); - Class _class((Class) value); - $objc_registerClassPair(_class); - return CYJSUndefined(context); - } CYCatch -} -/* }}} */ -#endif - -static JSValueRef Cycript_gc_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { - JSGarbageCollect(context); +static JSValueRef objc_registerClassPair_(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { + if (count != 1) + throw CYError(context, "incorrect number of arguments to objc_registerClassPair"); + CYPool pool; + NSObject *value(CYCastNSObject(pool, context, arguments[0])); + if (value == NULL || !CYIsClass(value)) + throw CYError(context, "incorrect number of arguments to objc_registerClassPair"); + Class _class((Class) value); + $objc_registerClassPair(_class); return CYJSUndefined(context); -} +} CYCatch } +/* }}} */ -#ifdef __OBJC__ 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; @@ -3503,147 +3432,30 @@ static JSValueRef Message_callAsFunction(JSContextRef context, JSObjectRef objec return CYCallFunction(pool, context, 2, setup, count, arguments, false, exception, &internal->signature_, &internal->cif_, internal->GetValue()); } -#endif -static JSValueRef Functor_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { +static JSObjectRef Super_new(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { + if (count != 2) + throw CYError(context, "incorrect number of arguments to Super constructor"); 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()); -} - -#ifdef __OBJC__ -static JSObjectRef Super_new(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { - CYTry { - if (count != 2) - _throw(NSInvalidArgumentException, "incorrect number of arguments to Super constructor"); - CYPool pool; - id self(CYCastNSObject(pool, context, arguments[0])); - Class _class(CYCastClass(pool, context, arguments[1])); - return cy::Super::Make(context, self, _class); - } CYCatch -} - -static JSObjectRef Selector_new(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { - CYTry { - if (count != 1) - _throw(NSInvalidArgumentException, "incorrect number of arguments to Selector constructor"); - const char *name(CYCastCString(context, arguments[0])); - return CYMakeSelector(context, sel_registerName(name)); - } CYCatch -} -#endif - -static JSObjectRef Pointer_new(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { - CYTry { - if (count != 2) - _throw(NSInvalidArgumentException, "incorrect number of arguments to Functor constructor"); - - void *value(CYCastPointer(context, arguments[0])); - const char *type(CYCastCString(context, arguments[1])); - - CYPool pool; - - sig::Signature signature; - sig::Parse(pool, &signature, type, &Structor_); - - return CYMakePointer(context, value, signature.elements[0].type, NULL, NULL); - } CYCatch -} - -static JSObjectRef Type_new(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { - CYTry { - if (count != 1) - _throw(NSInvalidArgumentException, "incorrect number of arguments to Type constructor"); - const char *type(CYCastCString(context, arguments[0])); - 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 { - CYPool pool; - size_t index(CYGetIndex(pool, 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(NSInvalidArgumentException, "incorrect number of arguments to type cast function"); - 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 -} - -static JSObjectRef Type_callAsConstructor(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { - CYTry { - if (count != 0) - _throw(NSInvalidArgumentException, "incorrect number of arguments to type cast function"); - Type_privateData *internal(reinterpret_cast(JSObjectGetPrivate(object))); - - 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 -} + id self(CYCastNSObject(pool, context, arguments[0])); + Class _class(CYCastClass(pool, context, arguments[1])); + return cy::Super::Make(context, self, _class); +} CYCatch } -#ifdef __OBJC__ -static JSObjectRef Instance_new(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { - CYTry { - if (count > 1) - _throw(NSInvalidArgumentException, "incorrect number of arguments to Instance constructor"); - id self(count == 0 ? nil : CYCastPointer(context, arguments[0])); - return Instance::Make(context, self); - } CYCatch -} -#endif +static JSObjectRef Selector_new(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { + if (count != 1) + throw CYError(context, "incorrect number of arguments to Selector constructor"); + const char *name(CYCastCString(context, arguments[0])); + return CYMakeSelector(context, sel_registerName(name)); +} CYCatch } -static JSObjectRef Functor_new(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { - CYTry { - if (count != 2) - _throw(NSInvalidArgumentException, "incorrect number of arguments to Functor constructor"); - const char *type(CYCastCString(context, arguments[1])); - return CYMakeFunctor(context, arguments[0], type); - } CYCatch -} +static JSObjectRef Instance_new(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { + if (count > 1) + throw CYError(context, "incorrect number of arguments to Instance constructor"); + id self(count == 0 ? nil : CYCastPointer(context, arguments[0])); + return Instance::Make(context, self); +} CYCatch } -#ifdef __OBJC__ 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_)); @@ -3666,45 +3478,19 @@ static JSValueRef CYValue_callAsFunction_$cya(JSContextRef context, JSObjectRef return CYMakePointer(context, &internal->value_, type, ffi, object); } -#endif - -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))); - - CYTry { - return CYCastJSValue(context, reinterpret_cast(internal->value_)); - } CYCatch -} - -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); -} - -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_); - - CYTry { - return CYCastJSValue(context, string); - } CYCatch -} -#ifdef __OBJC__ 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())); } -static JSValueRef Instance_getProperty_protocol(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { +static JSValueRef Instance_getProperty_protocol(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry { Instance *internal(reinterpret_cast(JSObjectGetPrivate(object))); id self(internal->GetValue()); if (!CYIsClass(self)) return CYJSUndefined(context); - CYTry { - return CYGetClassPrototype(context, self); - } CYCatch -} + return CYGetClassPrototype(context, self); +} CYCatch } static JSValueRef Instance_getProperty_messages(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { Instance *internal(reinterpret_cast(JSObjectGetPrivate(object))); @@ -3714,150 +3500,78 @@ static JSValueRef Instance_getProperty_messages(JSContextRef context, JSObjectRe return Messages::Make(context, self); } -static JSValueRef Instance_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { +static JSValueRef Instance_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { if (!JSValueIsObjectOfClass(context, _this, Instance_)) return NULL; Instance *internal(reinterpret_cast(JSObjectGetPrivate(_this))); + return CYCastJSValue(context, CYJSString(CYCastNSCYON(internal->GetValue()))); +} CYCatch } - CYTry { - CYPoolTry { - return CYCastJSValue(context, CYJSString(CYCastNSCYON(internal->GetValue()))); - } CYPoolCatch(NULL) - } CYCatch -} - -static JSValueRef Instance_callAsFunction_toJSON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { +static JSValueRef Instance_callAsFunction_toJSON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { 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 -} + 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) { +static JSValueRef Instance_callAsFunction_toString(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { if (!JSValueIsObjectOfClass(context, _this, Instance_)) return NULL; Instance *internal(reinterpret_cast(JSObjectGetPrivate(_this))); - CYTry { - CYPoolTry { - return CYCastJSValue(context, CYJSString([internal->GetValue() description])); - } CYPoolCatch(NULL) - } CYCatch -} + CYPoolTry { + // XXX: this seems like a stupid implementation; what if it crashes? why not use the CYONifier backend? + return CYCastJSValue(context, CYJSString([internal->GetValue() description])); + } CYPoolCatch(NULL) +} CYCatch } -static JSValueRef Selector_callAsFunction_toString(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { +static JSValueRef Selector_callAsFunction_toString(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { Selector_privateData *internal(reinterpret_cast(JSObjectGetPrivate(_this))); - - CYTry { - return CYCastJSValue(context, sel_getName(internal->GetValue())); - } CYCatch -} + 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) { +static JSValueRef Selector_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { 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(NSInvalidArgumentException, "incorrect number of arguments to Selector.type"); - CYPool pool; - Selector_privateData *internal(reinterpret_cast(JSObjectGetPrivate(_this))); - if (Class _class = CYCastClass(pool, context, arguments[0])) { - SEL sel(internal->GetValue()); - if (objc_method *method = class_getInstanceMethod(_class, sel)) - if (const char *type = CYPoolTypeEncoding(pool, _class, sel, method)) - return CYCastJSValue(context, CYJSString(type)); - } - - // XXX: do a lookup of some kind - return CYJSNull(context); - } CYCatch -} -#endif - -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_)); - return CYCastJSValue(context, CYJSString(type)); - } CYCatch -} + CYPoolTry { + return CYCastJSValue(context, CYJSString([NSString stringWithFormat:@"@selector(%s)", name])); + } 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_)); - size_t size(strlen(type)); - char *cyon(new(pool) char[12 + size + 1]); - memcpy(cyon, "new Type(\"", 10); - cyon[12 + size] = '\0'; - cyon[12 + size - 2] = '"'; - cyon[12 + size - 1] = ')'; - memcpy(cyon + 10, type, size); - return CYCastJSValue(context, CYJSString(cyon)); - } 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 CYError(context, "incorrect number of arguments to Selector.type"); + CYPool pool; + Selector_privateData *internal(reinterpret_cast(JSObjectGetPrivate(_this))); + if (Class _class = CYCastClass(pool, context, arguments[0])) { + SEL sel(internal->GetValue()); + if (objc_method *method = class_getInstanceMethod(_class, sel)) + if (const char *type = CYPoolTypeEncoding(pool, context, _class, sel, method)) + return CYCastJSValue(context, CYJSString(type)); + } -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); -} + // XXX: do a lookup of some kind + return CYJSNull(context); +} CYCatch } -#ifdef __OBJC__ static JSStaticValue Selector_staticValues[2] = { {"value", &CYValue_getProperty_value, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete}, {NULL, NULL, NULL, 0} }; -#endif - -static JSStaticValue Pointer_staticValues[2] = { - {"$cyi", &Pointer_getProperty_$cyi, &Pointer_setProperty_$cyi, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, - {NULL, NULL, NULL, 0} -}; - -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} -}; - -static JSStaticFunction Struct_staticFunctions[2] = { - {"$cya", &Struct_callAsFunction_$cya, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, - {NULL, NULL, 0} -}; - -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} -}; -#ifdef __OBJC__ static JSStaticValue Instance_staticValues[5] = { {"constructor", &Instance_getProperty_constructor, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, {"messages", &Instance_getProperty_messages, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, @@ -3886,80 +3600,12 @@ static JSStaticFunction Selector_staticFunctions[5] = { {"type", &Selector_callAsFunction_type, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, {NULL, NULL, 0} }; -#endif - -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} -}; - -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) { - JSContextRef context(CYGetJSContext()); - JSValueRef exception(NULL), result; - - const char *json; - - CYPoolTry { - 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; - - try { - json = CYPoolCCYON(pool, context, result, &exception); - } catch (const char *error) { - return error; - } - - if (exception != NULL) - goto error; - } CYPoolCatch(NULL) - - CYSetProperty(context, CYGetGlobalObject(context), Result_, result); - return json; -} - -static apr_pool_t *Pool_; - -apr_pool_t *CYGetGlobalPool() { - return Pool_; -} - -MSInitialize { - _aprcall(apr_initialize()); - _aprcall(apr_pool_create(&Pool_, NULL)); - _sqlcall(sqlite3_open("/usr/lib/libcycript.db", &Bridge_)); +void CYObjectiveC(JSContextRef context, JSObjectRef global) { + hooks_ = &CYObjectiveCHooks; -#ifdef __OBJC__ - Type_privateData::Object = new(Pool_) Type_privateData(Pool_, "@"); - Type_privateData::Selector = new(Pool_) Type_privateData(Pool_, ":"); + Object_type = new(Pool_) Type_privateData(Pool_, "@"); + Selector_type = new(Pool_) Type_privateData(Pool_, ":"); #ifdef __APPLE__ NSCFBoolean_ = objc_getClass("NSCFBoolean"); @@ -3971,8 +3617,123 @@ MSInitialize { NSMessageBuilder_ = objc_getClass("NSMessageBuilder"); NSZombie_ = objc_getClass("_NSZombie_"); Object_ = objc_getClass("Object"); + + JSClassDefinition 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.hasInstance = &Instance_hasInstance; + 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 = "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 = "Selector"; + definition.staticValues = Selector_staticValues; + definition.staticFunctions = Selector_staticFunctions; + definition.callAsFunction = &Selector_callAsFunction; + definition.finalize = &Finalize; + Selector_ = JSClassCreate(&definition); + + definition = kJSClassDefinitionEmpty; + definition.className = "Super"; + definition.staticFunctions = Internal_staticFunctions; + definition.finalize = &Finalize; + Super_ = 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; + 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); + + JSObjectRef 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)); + + JSObjectRef Instance(JSObjectMakeConstructor(context, Instance_, &Instance_new)); + JSObjectRef Message(JSObjectMakeConstructor(context, Message_, NULL)); + JSObjectRef Selector(JSObjectMakeConstructor(context, Selector_, &Selector_new)); + JSObjectRef Super(JSObjectMakeConstructor(context, Super_, &Super_new)); + + Instance_prototype_ = (JSObjectRef) CYGetProperty(context, Instance, prototype_); + JSValueProtect(context, Instance_prototype_); + + CYSetProperty(context, global, CYJSString("Instance"), Instance); + CYSetProperty(context, global, CYJSString("Selector"), Selector); + CYSetProperty(context, global, CYJSString("Super"), Super); + + 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)); + + JSObjectSetPrototype(context, (JSObjectRef) CYGetProperty(context, Message, prototype_), Function_prototype_); + JSObjectSetPrototype(context, (JSObjectRef) CYGetProperty(context, Selector, prototype_), Function_prototype_); + + MSHookFunction(&objc_registerClassPair, MSHake(objc_registerClassPair)); + +#ifdef __APPLE__ + class_addMethod(NSCFType_, @selector(cy$toJSON:), reinterpret_cast(&NSCFType$cy$toJSON), "@12@0:4@8"); #endif } +/* }}} */ +#endif JSGlobalContextRef CYGetJSContext() { if (Context_ == NULL) { @@ -4030,6 +3791,9 @@ JSGlobalContextRef CYGetJSContext() { Array_ = CYCastJSObject(context, CYGetProperty(context, global, CYJSString("Array"))); JSValueProtect(context, Array_); + Error_ = CYCastJSObject(context, CYGetProperty(context, global, CYJSString("Error"))); + JSValueProtect(context, Error_); + Function_ = CYCastJSObject(context, CYGetProperty(context, global, CYJSString("Function"))); JSValueProtect(context, Function_); @@ -4061,134 +3825,15 @@ JSGlobalContextRef CYGetJSContext() { JSObjectRef Functor(JSObjectMakeConstructor(context, Functor_, &Functor_new)); - JSValueRef function(CYGetProperty(context, Function_, prototype_)); - -/* Objective-C Classes {{{ */ -#ifdef __OBJC__ - 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.hasInstance = &Instance_hasInstance; - 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 = "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 = "Selector"; - definition.staticValues = Selector_staticValues; - definition.staticFunctions = Selector_staticFunctions; - definition.callAsFunction = &Selector_callAsFunction; - definition.finalize = &Finalize; - Selector_ = JSClassCreate(&definition); - - definition = kJSClassDefinitionEmpty; - definition.className = "Super"; - definition.staticFunctions = Internal_staticFunctions; - definition.finalize = &Finalize; - Super_ = 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; - 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); - - JSObjectRef 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)); + Function_prototype_ = (JSObjectRef) CYGetProperty(context, Function_, prototype_); + JSValueProtect(context, Function_prototype_); - JSObjectRef Instance(JSObjectMakeConstructor(context, Instance_, &Instance_new)); - JSObjectRef Message(JSObjectMakeConstructor(context, Message_, NULL)); - JSObjectRef Selector(JSObjectMakeConstructor(context, Selector_, &Selector_new)); - JSObjectRef Super(JSObjectMakeConstructor(context, Super_, &Super_new)); - - Instance_prototype_ = (JSObjectRef) CYGetProperty(context, Instance, prototype_); - JSValueProtect(context, Instance_prototype_); - - CYSetProperty(context, global, CYJSString("Instance"), Instance); - CYSetProperty(context, global, CYJSString("Selector"), Selector); - CYSetProperty(context, global, CYJSString("Super"), Super); - - 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)); - - JSObjectSetPrototype(context, (JSObjectRef) CYGetProperty(context, Message, prototype_), function); - JSObjectSetPrototype(context, (JSObjectRef) CYGetProperty(context, Selector, prototype_), function); -#endif -/* }}} */ - - JSObjectSetPrototype(context, (JSObjectRef) CYGetProperty(context, Functor, prototype_), function); + JSObjectSetPrototype(context, (JSObjectRef) CYGetProperty(context, Functor, prototype_), Function_prototype_); CYSetProperty(context, global, CYJSString("Functor"), Functor); CYSetProperty(context, global, CYJSString("Pointer"), JSObjectMakeConstructor(context, Pointer_, &Pointer_new)); CYSetProperty(context, global, CYJSString("Type"), JSObjectMakeConstructor(context, Type_privateData::Class_, &Type_new)); -#ifdef __OBJC__ - MSHookFunction(&objc_registerClassPair, MSHake(objc_registerClassPair)); -#endif - -#ifdef __OBJC__ -#ifdef __APPLE__ - class_addMethod(NSCFType_, @selector(cy$toJSON:), reinterpret_cast(&NSCFType$cy$toJSON), "@12@0:4@8"); -#endif -#endif - JSObjectRef cycript(JSObjectMake(context, NULL, NULL)); CYSetProperty(context, global, CYJSString("Cycript"), cycript); CYSetProperty(context, cycript, CYJSString("gc"), JSObjectMakeFunctionWithCallback(context, CYJSString("gc"), &Cycript_gc_callAsFunction)); @@ -4205,6 +3850,8 @@ JSGlobalContextRef CYGetJSContext() { CYSetProperty(context, System_, CYJSString("print"), JSObjectMakeFunctionWithCallback(context, CYJSString("print"), &System_print)); Result_ = JSStringCreateWithUTF8CString("_"); + + CYObjectiveC(context, global); } return Context_;