+CFStringRef CYCopyCFString(const char *value) {
+ return CFStringCreateWithCString(kCFAllocatorDefault, value, kCFStringEncodingUTF8);
+}
+
+NSString *CYCastNSString(apr_pool_t *pool, const char *value) {
+ return (NSString *) CYPoolRelease(pool, CYCopyCFString(value));
+}
+
+NSString *CYCastNSString(apr_pool_t *pool, JSStringRef value) {
+ return (NSString *) CYPoolRelease(pool, CYCopyCFString(value));
+}
+
+bool CYCastBool(JSContextRef context, JSValueRef value) {
+ return JSValueToBoolean(context, value);
+}
+
+CFTypeRef CYCFType(apr_pool_t *pool, JSContextRef context, JSValueRef value, bool cast) {
+ CFTypeRef object;
+ bool copy;
+
+ switch (JSType type = JSValueGetType(context, value)) {
+ case kJSTypeUndefined:
+ object = [WebUndefined undefined];
+ copy = false;
+ break;
+
+ case kJSTypeNull:
+ return NULL;
+ break;
+
+ case kJSTypeBoolean:
+ object = CYCastBool(context, value) ? kCFBooleanTrue : kCFBooleanFalse;
+ copy = false;
+ break;
+
+ case kJSTypeNumber:
+ object = CYCopyCFNumber(context, value);
+ copy = true;
+ break;
+
+ case kJSTypeString:
+ object = CYCopyCFString(context, value);
+ copy = true;
+ break;
+
+ case kJSTypeObject:
+ // XXX: this might could be more efficient
+ object = (CFTypeRef) CYCastNSObject(pool, context, (JSObjectRef) value);
+ copy = false;
+ break;
+
+ default:
+ @throw [NSException exceptionWithName:NSInternalInconsistencyException reason:[NSString stringWithFormat:@"JSValueGetType() == 0x%x", type] userInfo:nil];
+ break;
+ }
+
+ if (cast != copy)
+ return object;
+ else if (copy)
+ return CYPoolRelease(pool, object);
+ else
+ return CFRetain(object);
+}
+
+CFTypeRef CYCastCFType(apr_pool_t *pool, JSContextRef context, JSValueRef value) {
+ return CYCFType(pool, context, value, true);
+}
+
+CFTypeRef CYCopyCFType(apr_pool_t *pool, JSContextRef context, JSValueRef value) {
+ return CYCFType(pool, context, value, false);
+}
+
+NSArray *CYCastNSArray(JSPropertyNameArrayRef names) {
+ CYPool pool;
+ size_t size(JSPropertyNameArrayGetCount(names));
+ NSMutableArray *array([NSMutableArray arrayWithCapacity:size]);
+ for (size_t index(0); index != size; ++index)
+ [array addObject:CYCastNSString(pool, JSPropertyNameArrayGetNameAtIndex(names, index))];
+ return array;
+}
+
+id CYCastNSObject(apr_pool_t *pool, JSContextRef context, JSValueRef value) {
+ return reinterpret_cast<const NSObject *>(CYCastCFType(pool, context, value));
+}
+
+void CYThrow(JSContextRef context, JSValueRef value) {
+ if (value == NULL)
+ return;
+ @throw CYCastNSObject(NULL, context, value);
+}
+
+JSValueRef CYJSNull(JSContextRef context) {
+ return JSValueMakeNull(context);
+}
+
+JSValueRef CYCastJSValue(JSContextRef context, JSStringRef value) {
+ return value == NULL ? CYJSNull(context) : JSValueMakeString(context, value);
+}
+
+JSValueRef CYCastJSValue(JSContextRef context, const char *value) {
+ return CYCastJSValue(context, CYJSString(value));
+}
+
+JSValueRef CYCastJSValue(JSContextRef context, id value) {
+ if (value == nil)
+ return CYJSNull(context);
+ else if ([value respondsToSelector:@selector(cy$JSValueInContext:)])
+ return [value cy$JSValueInContext:context];
+ else
+ return CYMakeInstance(context, value, false);
+}
+
+JSObjectRef CYCastJSObject(JSContextRef context, JSValueRef value) {
+ JSValueRef exception(NULL);
+ JSObjectRef object(JSValueToObject(context, value, &exception));
+ CYThrow(context, exception);
+ return object;
+}
+
+void CYThrow(JSContextRef context, id error, JSValueRef *exception) {
+ if (exception == NULL)
+ throw error;
+ *exception = CYCastJSValue(context, error);
+}
+
+JSValueRef CYCallAsFunction(JSContextRef context, JSObjectRef function, JSObjectRef _this, size_t count, JSValueRef arguments[]) {
+ JSValueRef exception(NULL);
+ JSValueRef value(JSObjectCallAsFunction(context, function, _this, count, arguments, &exception));
+ CYThrow(context, exception);
+ return value;
+}
+
+bool CYIsCallable(JSContextRef context, JSValueRef value) {
+ // XXX: this isn't actually correct
+ return value != NULL && JSValueIsObject(context, value);
+}
+
+@implementation CYJSObject
+
+- (id) initWithJSObject:(JSObjectRef)object inContext:(JSContextRef)context {
+ if ((self = [super init]) != nil) {
+ object_ = object;
+ context_ = context;
+ JSValueProtect(context_, object_);
+ } return self;
+}
+
+- (void) dealloc {
+ JSValueUnprotect(context_, object_);
+ [super dealloc];
+}
+
+- (NSObject *) cy$toJSON:(NSString *)key {
+ JSValueRef toJSON(CYGetProperty(context_, object_, toJSON_));
+ if (!CYIsCallable(context_, toJSON))
+ return [super cy$toJSON:key];
+ else {
+ JSValueRef arguments[1] = {CYCastJSValue(context_, key)};
+ JSValueRef value(CYCallAsFunction(context_, (JSObjectRef) toJSON, object_, 1, arguments));
+ // XXX: do I really want an NSNull here?!
+ return CYCastNSObject(NULL, context_, value) ?: [NSNull null];
+ }
+}
+
+- (NSString *) cy$toCYON {
+ JSValueRef toCYON(CYGetProperty(context_, object_, toCYON_));
+ if (!CYIsCallable(context_, toCYON)) super:
+ return [super cy$toCYON];
+ else if (JSValueRef value = CYCallAsFunction(context_, (JSObjectRef) toCYON, object_, 0, NULL))
+ return CYCastNSString(NULL, CYJSString(context_, value));
+ else goto super;
+}
+
+- (NSUInteger) count {
+ JSPropertyNameArrayRef names(JSObjectCopyPropertyNames(context_, object_));
+ size_t size(JSPropertyNameArrayGetCount(names));
+ JSPropertyNameArrayRelease(names);
+ return size;
+}
+
+- (id) objectForKey:(id)key {
+ JSValueRef value(CYGetProperty(context_, object_, CYJSString(key)));
+ if (JSValueIsUndefined(context_, value))
+ return nil;
+ return CYCastNSObject(NULL, context_, value) ?: [NSNull null];
+}
+
+- (NSEnumerator *) keyEnumerator {
+ JSPropertyNameArrayRef names(JSObjectCopyPropertyNames(context_, object_));
+ NSEnumerator *enumerator([CYCastNSArray(names) objectEnumerator]);
+ JSPropertyNameArrayRelease(names);
+ return enumerator;
+}
+
+- (void) setObject:(id)object forKey:(id)key {
+ CYSetProperty(context_, object_, CYJSString(key), CYCastJSValue(context_, object));
+}
+
+- (void) removeObjectForKey:(id)key {
+ JSValueRef exception(NULL);
+ (void) JSObjectDeleteProperty(context_, object_, CYJSString(key), &exception);
+ CYThrow(context_, exception);
+}
+
+@end
+
+@implementation CYJSArray
+
+- (id) initWithJSObject:(JSObjectRef)object inContext:(JSContextRef)context {
+ if ((self = [super init]) != nil) {
+ object_ = object;
+ context_ = context;
+ JSValueProtect(context_, object_);
+ } return self;
+}
+
+- (void) dealloc {
+ JSValueUnprotect(context_, object_);
+ [super dealloc];
+}
+
+- (NSUInteger) count {
+ return CYCastDouble(context_, CYGetProperty(context_, object_, length_));
+}
+
+- (id) objectAtIndex:(NSUInteger)index {
+ 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];
+ JSValueRef exception(NULL);
+ JSValueRef value(JSObjectGetPropertyAtIndex(context_, object_, index, &exception));
+ CYThrow(context_, exception);
+ return CYCastNSObject(NULL, context_, value) ?: [NSNull null];
+}
+
+- (void) addObject:(id)object {
+ JSValueRef exception(NULL);
+ JSValueRef arguments[1];
+ arguments[0] = CYCastJSValue(context_, object);
+ JSObjectCallAsFunction(context_, Array_push_, object_, 1, arguments, &exception);
+ CYThrow(context_, exception);
+}
+
+- (void) insertObject:(id)object atIndex:(NSUInteger)index {
+ 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];
+ JSValueRef exception(NULL);
+ JSValueRef arguments[3];
+ arguments[0] = CYCastJSValue(context_, index);
+ arguments[1] = CYCastJSValue(context_, 0);
+ arguments[2] = CYCastJSValue(context_, object);
+ JSObjectCallAsFunction(context_, Array_splice_, object_, 3, arguments, &exception);
+ CYThrow(context_, exception);
+}
+
+- (void) removeLastObject {
+ JSValueRef exception(NULL);
+ JSObjectCallAsFunction(context_, Array_pop_, object_, 0, NULL, &exception);
+ CYThrow(context_, exception);
+}
+
+- (void) removeObjectAtIndex:(NSUInteger)index {
+ 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
+
+NSString *CYCopyNSCYON(id value) {
+ NSString *string;
+
+ if (value == nil)
+ string = @"nil";
+ else {
+ Class _class(object_getClass(value));
+ SEL sel(@selector(cy$toCYON));
+
+ if (Method toCYON = class_getInstanceMethod(_class, sel))
+ string = reinterpret_cast<NSString *(*)(id, SEL)>(method_getImplementation(toCYON))(value, sel);
+ else if (Method methodSignatureForSelector = class_getInstanceMethod(_class, @selector(methodSignatureForSelector:))) {
+ if (reinterpret_cast<NSMethodSignature *(*)(id, SEL, SEL)>(method_getImplementation(methodSignatureForSelector))(value, @selector(methodSignatureForSelector:), sel) != nil)
+ string = [value cy$toCYON];
+ else goto fail;
+ } else fail: {
+ if (value == NSZombie_)
+ string = @"_NSZombie_";
+ else if (_class == NSZombie_)
+ string = [NSString stringWithFormat:@"<_NSZombie_: %p>", value];
+ // XXX: frowny /in/ the pants
+ else if (value == NSMessageBuilder_ || value == Object_)
+ string = nil;
+ else
+ string = [NSString stringWithFormat:@"%@", value];
+ }
+
+ // XXX: frowny pants
+ if (string == nil)
+ string = @"undefined";
+ }
+
+ return [string retain];
+}
+
+NSString *CYCopyNSCYON(JSContextRef context, JSValueRef value, JSValueRef *exception) {
+ if (JSValueIsNull(context, value))
+ return [@"null" retain];
+
+ CYTry {
+ CYPoolTry {
+ return CYCopyNSCYON(CYCastNSObject(NULL, context, value));
+ } CYPoolCatch(NULL)
+ } CYCatch
+}
+
+NSString *CYPoolNSCYON(apr_pool_t *pool, id value) {
+ return CYPoolRelease(pool, static_cast<id>(CYCopyNSCYON(value)));
+}
+
+const char *CYPoolCCYON(apr_pool_t *pool, JSContextRef context, JSValueRef value, JSValueRef *exception) {
+ if (NSString *json = CYCopyNSCYON(context, value, exception)) {
+ const char *string(CYPoolCString(pool, json));
+ [json release];
+ return string;
+ } else return NULL;
+}
+
+// XXX: use objc_getAssociatedObject and objc_setAssociatedObject on 10.6
+struct CYInternal :
+ CYData
+{
+ JSObjectRef object_;
+
+ CYInternal() :
+ object_(NULL)
+ {
+ }
+
+ ~CYInternal() {
+ // XXX: delete object_? ;(
+ }
+
+ static CYInternal *Get(id self) {
+ CYInternal *internal(NULL);
+ if (object_getInstanceVariable(self, "cy$internal_", reinterpret_cast<void **>(&internal)) == NULL) {
+ // XXX: do something epic? ;P
+ }
+
+ return internal;
+ }
+
+ static CYInternal *Set(id self) {
+ CYInternal *internal(NULL);
+ if (Ivar ivar = object_getInstanceVariable(self, "cy$internal_", reinterpret_cast<void **>(&internal))) {
+ if (internal == NULL) {
+ internal = new CYInternal();
+ object_setIvar(self, ivar, reinterpret_cast<id>(internal));
+ }
+ } else {
+ // XXX: do something epic? ;P
+ }
+
+ return internal;
+ }
+
+ bool HasProperty(JSContextRef context, JSStringRef name) {
+ if (object_ == NULL)
+ return false;
+ return JSObjectHasProperty(context, object_, name);
+ }
+
+ JSValueRef GetProperty(JSContextRef context, JSStringRef name) {
+ if (object_ == NULL)
+ return NULL;
+ return CYGetProperty(context, object_, name);
+ }
+
+ void SetProperty(JSContextRef context, JSStringRef name, JSValueRef value) {
+ if (object_ == NULL)
+ object_ = JSObjectMake(context, NULL, NULL);
+ CYSetProperty(context, object_, name, value);
+ }
+};
+
+static JSObjectRef CYMakeSelector(JSContextRef context, SEL sel) {
+ Selector_privateData *internal(new Selector_privateData(sel));
+ return JSObjectMake(context, Selector_, internal);
+}
+
+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 const char *CYPoolCString(apr_pool_t *pool, JSStringRef value) {
+ if (pool == NULL) {
+ // XXX: this could be much more efficient
+ const char *string([CYCastNSString(NULL, value) UTF8String]);
+ return string;
+ } else {
+ size_t size(JSStringGetMaximumUTF8CStringSize(value));
+ char *string(new(pool) char[size]);
+ JSStringGetUTF8CString(value, string, size);
+ return string;
+ }
+}
+
+static const char *CYPoolCString(apr_pool_t *pool, JSContextRef context, JSValueRef value) {
+ return JSValueIsNull(context, value) ? NULL : CYPoolCString(pool, CYJSString(context, value));
+}
+
+static bool CYGetOffset(apr_pool_t *pool, JSStringRef value, ssize_t &index) {
+ return CYGetOffset(CYPoolCString(pool, value), index);
+}
+
+// XXX: this macro is unhygenic
+#define CYCastCString(context, value) ({ \
+ char *utf8; \
+ if (value == NULL) \
+ utf8 = NULL; \
+ else if (JSStringRef string = CYCopyJSString(context, value)) { \
+ size_t size(JSStringGetMaximumUTF8CStringSize(string)); \
+ utf8 = reinterpret_cast<char *>(alloca(size)); \
+ JSStringGetUTF8CString(string, utf8, size); \
+ JSStringRelease(string); \
+ } else \
+ utf8 = NULL; \
+ utf8; \
+})
+
+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<Pointer *>(JSObjectGetPrivate((JSObjectRef) value)));
+ return internal->value_;
+ }*/
+ default:
+ double number(CYCastDouble(context, value));
+ if (std::isnan(number))
+ @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"cannot convert value to pointer" userInfo:nil];
+ return reinterpret_cast<void *>(static_cast<uintptr_t>(static_cast<long long>(number)));
+ }
+}
+
+template <typename Type_>
+static _finline Type_ CYCastPointer(JSContextRef context, JSValueRef value) {
+ return reinterpret_cast<Type_>(CYCastPointer_(context, value));
+}
+
+static SEL CYCastSEL(JSContextRef context, JSValueRef value) {
+ if (JSValueIsObjectOfClass(context, value, Selector_)) {
+ Selector_privateData *internal(reinterpret_cast<Selector_privateData *>(JSObjectGetPrivate((JSObjectRef) value)));
+ return reinterpret_cast<SEL>(internal->value_);
+ } else
+ return CYCastPointer<SEL>(context, value);
+}
+
+static void CYPoolFFI(apr_pool_t *pool, JSContextRef context, sig::Type *type, ffi_type *ffi, void *data, JSValueRef value) {
+ switch (type->primitive) {
+ case sig::boolean_P:
+ *reinterpret_cast<bool *>(data) = JSValueToBoolean(context, value);
+ break;
+
+#define CYPoolFFI_(primitive, native) \
+ case sig::primitive ## _P: \
+ *reinterpret_cast<native *>(data) = CYCastDouble(context, value); \
+ break;
+
+ CYPoolFFI_(uchar, unsigned char)
+ CYPoolFFI_(char, char)
+ CYPoolFFI_(ushort, unsigned short)
+ CYPoolFFI_(short, short)
+ CYPoolFFI_(ulong, unsigned long)
+ CYPoolFFI_(long, long)
+ CYPoolFFI_(uint, unsigned int)
+ CYPoolFFI_(int, int)
+ CYPoolFFI_(ulonglong, unsigned long long)
+ CYPoolFFI_(longlong, long long)
+ CYPoolFFI_(float, float)
+ CYPoolFFI_(double, double)
+
+ case sig::object_P:
+ case sig::typename_P:
+ *reinterpret_cast<id *>(data) = CYCastNSObject(pool, context, value);
+ break;
+
+ case sig::selector_P:
+ *reinterpret_cast<SEL *>(data) = CYCastSEL(context, value);
+ break;
+
+ case sig::pointer_P:
+ *reinterpret_cast<void **>(data) = CYCastPointer<void *>(context, value);
+ break;
+
+ case sig::string_P:
+ *reinterpret_cast<const char **>(data) = CYPoolCString(pool, context, value);
+ break;
+
+ case sig::struct_P: {
+ uint8_t *base(reinterpret_cast<uint8_t *>(data));
+ JSObjectRef aggregate(JSValueIsObject(context, value) ? (JSObjectRef) value : NULL);
+ for (size_t index(0); index != type->data.signature.count; ++index) {
+ sig::Element *element(&type->data.signature.elements[index]);
+ ffi_type *field(ffi->elements[index]);
+
+ JSValueRef rhs;
+ if (aggregate == NULL)
+ rhs = value;
+ else {
+ rhs = CYGetProperty(context, aggregate, index);
+ if (JSValueIsUndefined(context, rhs)) {
+ if (element->name != NULL)
+ rhs = CYGetProperty(context, aggregate, CYJSString(element->name));
+ else
+ goto undefined;
+ if (JSValueIsUndefined(context, rhs)) undefined:
+ @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"unable to extract structure value" userInfo:nil];
+ }
+ }
+
+ CYPoolFFI(pool, context, element->type, field, base, rhs);
+ // XXX: alignment?
+ base += field->size;
+ }
+ } break;
+
+ case sig::void_P:
+ break;
+
+ default:
+ NSLog(@"CYPoolFFI(%c)\n", type->primitive);
+ _assert(false);
+ }
+}
+
+static JSValueRef CYFromFFI(JSContextRef context, sig::Type *type, ffi_type *ffi, void *data, bool initialize = false, JSObjectRef owner = NULL) {
+ JSValueRef value;
+
+ switch (type->primitive) {
+ case sig::boolean_P:
+ value = CYCastJSValue(context, *reinterpret_cast<bool *>(data));
+ break;
+
+#define CYFromFFI_(primitive, native) \
+ case sig::primitive ## _P: \
+ value = CYCastJSValue(context, *reinterpret_cast<native *>(data)); \
+ break;
+
+ CYFromFFI_(uchar, unsigned char)
+ CYFromFFI_(char, char)
+ CYFromFFI_(ushort, unsigned short)
+ CYFromFFI_(short, short)
+ CYFromFFI_(ulong, unsigned long)
+ CYFromFFI_(long, long)
+ CYFromFFI_(uint, unsigned int)
+ CYFromFFI_(int, int)
+ CYFromFFI_(ulonglong, unsigned long long)
+ CYFromFFI_(longlong, long long)
+ CYFromFFI_(float, float)
+ CYFromFFI_(double, double)
+
+ case sig::object_P: {
+ if (id object = *reinterpret_cast<id *>(data)) {
+ value = CYCastJSValue(context, object);
+ if (initialize)
+ [object release];
+ } else goto null;
+ } break;
+
+ case sig::typename_P:
+ value = CYMakeInstance(context, *reinterpret_cast<Class *>(data), true);
+ break;
+
+ case sig::selector_P:
+ if (SEL sel = *reinterpret_cast<SEL *>(data))
+ value = CYMakeSelector(context, sel);
+ else goto null;
+ break;
+
+ case sig::pointer_P:
+ if (void *pointer = *reinterpret_cast<void **>(data))
+ value = CYMakePointer(context, pointer, type->data.data.type, ffi, owner);
+ else goto null;
+ break;
+
+ case sig::string_P:
+ if (char *utf8 = *reinterpret_cast<char **>(data))
+ value = CYCastJSValue(context, utf8);
+ else goto null;
+ break;
+
+ case sig::struct_P:
+ value = CYMakeStruct(context, data, type, ffi, owner);
+ break;
+
+ case sig::void_P:
+ value = CYJSUndefined(context);
+ break;
+
+ null:
+ value = CYJSNull(context);
+ break;
+
+ default:
+ NSLog(@"CYFromFFI(%c)\n", type->primitive);
+ _assert(false);
+ }
+
+ return value;
+}
+
+static bool CYImplements(id object, Class _class, SEL selector, bool devoid) {
+ if (Method method = class_getInstanceMethod(_class, selector)) {
+ if (!devoid)
+ return true;
+ char type[16];
+ method_getReturnType(method, type, sizeof(type));
+ if (type[0] != 'v')
+ return true;
+ }
+
+ // XXX: possibly use a more "awesome" check?
+ return false;
+}
+
+static const char *CYPoolTypeEncoding(apr_pool_t *pool, Class _class, SEL sel, Method method) {
+ if (method != NULL)
+ return method_getTypeEncoding(method);
+ else if (NSString *type = [[Bridge_ objectAtIndex:1] objectForKey:CYCastNSString(pool, sel_getName(sel))])
+ return CYPoolCString(pool, type);
+ else
+ return NULL;
+}
+
+static void FunctionClosure_(ffi_cif *cif, void *result, void **arguments, void *arg) {
+ Closure_privateData *internal(reinterpret_cast<Closure_privateData *>(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 void MessageClosure_(ffi_cif *cif, void *result, void **arguments, void *arg) {
+ Closure_privateData *internal(reinterpret_cast<Closure_privateData *>(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);
+}
+
+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<void (*)()>(context, value));
+ return CYMakeFunctor(context, function, type);
+ }
+}
+
+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);
+}
+
+static IMP CYMakeMessage(JSContextRef context, JSValueRef value, const char *type) {
+ JSObjectRef function(CYCastJSObject(context, value));
+ Closure_privateData *internal(CYMakeFunctor_(context, function, type, &MessageClosure_));
+ return reinterpret_cast<IMP>(internal->GetValue());
+}
+
+static bool Messages_hasProperty(JSContextRef context, JSObjectRef object, JSStringRef property) {
+ Messages *internal(reinterpret_cast<Messages *>(JSObjectGetPrivate(object)));
+ Class _class(internal->GetValue());
+
+ CYPool pool;
+ const char *name(CYPoolCString(pool, property));
+
+ if (SEL sel = sel_getUid(name))
+ if (class_getInstanceMethod(_class, sel) != NULL)
+ return true;
+
+ return false;
+}
+
+static JSValueRef Messages_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) {
+ Messages *internal(reinterpret_cast<Messages *>(JSObjectGetPrivate(object)));
+ Class _class(internal->GetValue());
+
+ CYPool pool;
+ const char *name(CYPoolCString(pool, property));
+
+ if (SEL sel = sel_getUid(name))
+ if (Method method = class_getInstanceMethod(_class, sel))
+ return CYMakeMessage(context, sel, method_getImplementation(method), method_getTypeEncoding(method));
+
+ return NULL;
+}
+
+static bool Messages_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) {
+ Messages *internal(reinterpret_cast<Messages *>(JSObjectGetPrivate(object)));
+ Class _class(internal->GetValue());
+
+ CYPool pool;
+ const char *name(CYPoolCString(pool, property));
+
+ SEL sel(sel_registerName(name));
+
+ Method method(class_getInstanceMethod(_class, sel));
+
+ const char *type;
+ IMP imp;
+
+ if (JSValueIsObjectOfClass(context, value, Message_)) {
+ Message_privateData *message(reinterpret_cast<Message_privateData *>(JSObjectGetPrivate((JSObjectRef) value)));
+ type = sig::Unparse(pool, &message->signature_);
+ imp = reinterpret_cast<IMP>(message->GetValue());
+ } else {
+ type = CYPoolTypeEncoding(pool, _class, sel, method);
+ imp = CYMakeMessage(context, value, type);
+ }
+
+ if (method != NULL)
+ method_setImplementation(method, imp);
+ else
+ class_replaceMethod(_class, sel, imp, type);
+
+ return true;
+}
+
+#if !__OBJC2__
+static bool Messages_deleteProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) {
+ Messages *internal(reinterpret_cast<Messages *>(JSObjectGetPrivate(object)));
+ Class _class(internal->GetValue());
+
+ CYPool pool;
+ const char *name(CYPoolCString(pool, property));
+
+ if (SEL sel = sel_getUid(name))
+ if (Method method = class_getInstanceMethod(_class, sel)) {
+ objc_method_list list = {NULL, 1, {method}};
+ class_removeMethods(_class, &list);
+ return true;
+ }
+
+ return false;
+}
+#endif
+
+static void Messages_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) {
+ Messages *internal(reinterpret_cast<Messages *>(JSObjectGetPrivate(object)));
+ Class _class(internal->GetValue());
+
+ unsigned int size;
+ Method *data(class_copyMethodList(_class, &size));
+ for (size_t i(0); i != size; ++i)
+ JSPropertyNameAccumulatorAddName(names, CYJSString(sel_getName(method_getName(data[i]))));
+ free(data);
+}
+
+static bool Instance_hasProperty(JSContextRef context, JSObjectRef object, JSStringRef property) {
+ Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
+ id self(internal->GetValue());
+
+ if (JSStringIsEqualToUTF8CString(property, "$cyi"))
+ return true;
+
+ CYPool pool;
+ NSString *name(CYCastNSString(pool, property));
+
+ if (CYInternal *internal = CYInternal::Get(self))
+ if (internal->HasProperty(context, property))
+ return true;
+
+ Class _class(object_getClass(self));
+
+ CYPoolTry {
+ // XXX: this is an evil hack to deal with NSProxy; fix elsewhere
+ if (CYImplements(self, _class, @selector(cy$hasProperty:), false))
+ if ([self cy$hasProperty:name])
+ return true;
+ } CYPoolCatch(false)
+
+ const char *string(CYPoolCString(pool, name));
+
+ if (class_getProperty(_class, string) != NULL)
+ return true;
+
+ if (SEL sel = sel_getUid(string))
+ if (CYImplements(self, _class, sel, true))
+ return true;
+
+ return false;
+}
+
+static JSValueRef Instance_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) {
+ Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
+ id self(internal->GetValue());
+
+ if (JSStringIsEqualToUTF8CString(property, "$cyi"))
+ return Internal::Make(context, self, object);
+
+ CYTry {
+ CYPool pool;
+ NSString *name(CYCastNSString(pool, property));
+
+ 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)
+
+ const char *string(CYPoolCString(pool, name));
+ Class _class(object_getClass(self));
+
+ if (objc_property_t property = class_getProperty(_class, string)) {
+ PropertyAttributes attributes(property);
+ SEL sel(sel_registerName(attributes.Getter()));
+ return CYSendMessage(pool, context, self, sel, 0, NULL, false, exception);
+ }
+
+ if (SEL sel = sel_getUid(string))
+ if (CYImplements(self, _class, sel, true))
+ return CYSendMessage(pool, context, self, sel, 0, NULL, false, exception);
+
+ return NULL;
+ } CYCatch
+}
+
+static bool Instance_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) {
+ Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
+ id self(internal->GetValue());
+
+ CYPool pool;
+
+ CYTry {
+ NSString *name(CYCastNSString(pool, property));
+ NSString *data(CYCastNSObject(pool, context, value));
+
+ CYPoolTry {
+ if ([self cy$setProperty:name to:data])
+ return true;
+ } CYPoolCatch(NULL)
+
+ const char *string(CYPoolCString(pool, name));
+ Class _class(object_getClass(self));
+
+ if (objc_property_t property = class_getProperty(_class, string)) {
+ PropertyAttributes attributes(property);
+ if (const char *setter = attributes.Setter()) {
+ SEL sel(sel_registerName(setter));
+ JSValueRef arguments[1] = {value};
+ CYSendMessage(pool, context, self, sel, 1, arguments, false, exception);
+ return true;
+ }
+ }
+
+ size_t length(strlen(string));
+
+ char set[length + 5];
+
+ set[0] = 's';
+ set[1] = 'e';
+ set[2] = 't';
+
+ if (string[0] != '\0') {
+ set[3] = toupper(string[0]);
+ memcpy(set + 4, string + 1, length - 1);
+ }
+
+ set[length + 3] = ':';
+ set[length + 4] = '\0';
+
+ if (SEL sel = sel_getUid(set))
+ if (CYImplements(self, _class, sel, false)) {
+ JSValueRef arguments[1] = {value};
+ CYSendMessage(pool, context, self, sel, 1, arguments, false, exception);
+ }
+
+ if (CYInternal *internal = CYInternal::Set(self)) {
+ internal->SetProperty(context, property, value);
+ return true;
+ }
+
+ return false;
+ } CYCatch