/* }}} */
#include <substrate.h>
+#include <dlfcn.h>
+
#include "cycript.hpp"
#include "sig/parse.hpp"
} \
}
+#ifndef __APPLE__
+#define class_getSuperclass GSObjCSuper
+#define object_getClass GSObjCClass
+#endif
+
void CYThrow(JSContextRef context, JSValueRef value);
const char *CYPoolCCYON(apr_pool_t *pool, JSContextRef context, JSValueRef value, JSValueRef *exception);
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 == nil ? nil : object_getClass(object)));
+ JSObjectSetPrototype(context, value, CYGetClassPrototype(context, object_getClass(object)));
return value;
}
return (Type_) CYPoolRelease_(pool, (id) object);
}
-id CYCastNSObject_(apr_pool_t *pool, JSContextRef context, JSObjectRef object) {
+NSObject *CYCastNSObject_(apr_pool_t *pool, JSContextRef context, JSObjectRef object) {
JSValueRef exception(NULL);
bool array(JSValueIsInstanceOfConstructor(context, object, Array_, &exception));
CYThrow(context, exception);
return CYPoolRelease(pool, [value initWithJSObject:object inContext:context]);
}
-id CYCastNSObject(apr_pool_t *pool, JSContextRef context, JSObjectRef object) {
+NSObject *CYCastNSObject(apr_pool_t *pool, JSContextRef context, JSObjectRef object) {
if (!JSValueIsObjectOfClass(context, object, Instance_))
return CYCastNSObject_(pool, context, object);
else {
object = (id) (CYCastBool(context, value) ? kCFBooleanTrue : kCFBooleanFalse);
copy = false;
#else
- object = [[NSNumber alloc] initWithBoolean:value];
+ object = [[NSNumber alloc] initWithBool:CYCastBool(context, value)];
copy = true;
#endif
break;
return [object retain];
}
-id CYCastNSObject(apr_pool_t *pool, JSContextRef context, JSValueRef value) {
+NSObject *CYCastNSObject(apr_pool_t *pool, JSContextRef context, JSValueRef value) {
return CYNSObject(pool, context, value, true);
}
Class _class(object_getClass(value));
SEL sel(@selector(cy$toCYON));
- if (Method toCYON = class_getInstanceMethod(_class, sel))
+ if (objc_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:))) {
+ else if (objc_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;
}
static bool CYImplements(id object, Class _class, SEL selector, bool devoid) {
- if (Method method = class_getInstanceMethod(_class, selector)) {
+ if (objc_method *method = class_getInstanceMethod(_class, selector)) {
if (!devoid)
return true;
char type[16];
return false;
}
-static const char *CYPoolTypeEncoding(apr_pool_t *pool, Class _class, SEL sel, Method method) {
+static const char *CYPoolTypeEncoding(apr_pool_t *pool, Class _class, SEL sel, objc_method *method) {
if (method != NULL)
return method_getTypeEncoding(method);
else if (NSString *type = [[Bridge_ objectAtIndex:1] objectForKey:CYCastNSString(pool, sel_getName(sel))])
const char *name(CYPoolCString(pool, property));
if (SEL sel = sel_getUid(name))
- if (Method method = class_getInstanceMethod(_class, sel))
+ if (objc_method *method = class_getInstanceMethod(_class, sel))
return CYMakeMessage(context, sel, method_getImplementation(method), method_getTypeEncoding(method));
return NULL;
SEL sel(sel_registerName(name));
- Method method(class_getInstanceMethod(_class, sel));
+ objc_method *method(class_getInstanceMethod(_class, sel));
const char *type;
IMP imp;
const char *name(CYPoolCString(pool, property));
if (SEL sel = sel_getUid(name))
- if (Method method = class_getInstanceMethod(_class, sel)) {
+ if (objc_method *method = class_getInstanceMethod(_class, sel)) {
objc_method_list list = {NULL, 1, {method}};
class_removeMethods(_class, &list);
return true;
Class _class(internal->GetValue());
unsigned int size;
- Method *data(class_copyMethodList(_class, &size));
+ objc_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);
CYTry {
NSString *name(CYCastNSString(pool, property));
- NSString *data(CYCastNSObject(pool, context, value));
+ NSObject *data(CYCastNSObject(pool, context, value));
CYPoolTry {
if ([self cy$setProperty:name to:data])
const char *type;
Class _class(object_getClass(self));
- if (Method method = class_getInstanceMethod(_class, _cmd))
+ if (objc_method *method = class_getInstanceMethod(_class, _cmd))
type = method_getTypeEncoding(method);
else {
CYTry {
if (count != 1)
@throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"incorrect number of arguments to objc_registerClassPair" userInfo:nil];
CYPool pool;
- Class _class(CYCastNSObject(pool, context, arguments[0]));
+ NSObject *value(CYCastNSObject(pool, context, arguments[0]));
+ if (value == NULL || !CYIsClass(value))
+ @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"incorrect number of arguments to objc_registerClassPair" userInfo:nil];
+ Class _class((Class) value);
$objc_registerClassPair(_class);
return CYJSUndefined(context);
} CYCatch
@throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"incorrect number of arguments to Selector.type" userInfo:nil];
CYPool pool;
Selector_privateData *internal(reinterpret_cast<Selector_privateData *>(JSObjectGetPrivate(_this)));
- Class _class(CYCastNSObject(pool, context, arguments[0]));
- SEL sel(internal->GetValue());
- Method method(class_getInstanceMethod(_class, sel));
- const char *type(CYPoolTypeEncoding(pool, _class, sel, method));
- return type == NULL ? CYJSNull(context) : CYCastJSValue(context, CYJSString(type));
+ NSObject *value(CYCastNSObject(pool, context, arguments[0]));
+ if (value == NULL) lookup:
+ // XXX: do a lookup of some kind
+ return CYJSNull(context);
+ else if (!CYIsClass(value))
+ @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"Selector.type takes a Class" userInfo:nil];
+ else {
+ Class _class((Class) value);
+ SEL sel(internal->GetValue());
+ if (objc_method *method = class_getInstanceMethod(_class, sel)) {
+ const char *type(CYPoolTypeEncoding(pool, _class, sel, method));
+ return type == NULL ? CYJSNull(context) : CYCastJSValue(context, CYJSString(type));
+ } else goto lookup;
+ }
} CYCatch
}