#undef _assert
#undef _trace
-/* XXX: bad _assert */
#define _assert(test) do { \
- if ((test)) break; \
- CFLog(kCFLogLevelNotice, CFSTR("_assert(%s):%u"), #test, __LINE__); \
- throw; \
+ if (!(test)) \
+ @throw [NSException exceptionWithName:NSInternalInconsistencyException reason:[NSString stringWithFormat:@"_assert(%s):%s(%u):%s", #test, __FILE__, __LINE__, __FUNCTION__] userInfo:nil]; \
} while (false)
#define _trace() do { \
return JSObjectMake(context, Instance_, [object retain]);
}
-@interface NSMethodSignature (Cyrver)
+@interface NSMethodSignature (Cycript)
- (NSString *) _typeString;
@end
-@interface NSObject (Cyrver)
+@interface NSObject (Cycript)
- (NSString *) cy$toJSON;
- (JSValueRef) cy$JSValueInContext:(JSContextRef)context;
@end
-@interface NSString (Cyrver)
+@interface NSString (Cycript)
- (void *) cy$symbol;
@end
-@interface NSNumber (Cyrver)
+@interface NSNumber (Cycript)
- (void *) cy$symbol;
@end
-@implementation NSObject (Cyrver)
+@implementation NSObject (Cycript)
- (NSString *) cy$toJSON {
return [self description];
@end
-@implementation WebUndefined (Cyrver)
+@implementation WebUndefined (Cycript)
- (NSString *) cy$toJSON {
return @"undefined";
@end
-@implementation NSArray (Cyrver)
+@implementation NSArray (Cycript)
- (NSString *) cy$toJSON {
NSMutableString *json([[[NSMutableString alloc] init] autorelease]);
@end
-@implementation NSDictionary (Cyrver)
+@implementation NSDictionary (Cycript)
- (NSString *) cy$toJSON {
NSMutableString *json([[[NSMutableString alloc] init] autorelease]);
@end
-@implementation NSNumber (Cyrver)
+@implementation NSNumber (Cycript)
- (NSString *) cy$toJSON {
return [self class] != NSCFBoolean_ ? [self stringValue] : [self boolValue] ? @"true" : @"false";
@end
-@implementation NSString (Cyrver)
+@implementation NSString (Cycript)
- (NSString *) cy$toJSON {
CFMutableStringRef json(CFStringCreateMutableCopy(kCFAllocatorDefault, 0, (CFStringRef) self));
return CYCopyCFString(CYString(context, value));
}
-CFNumberRef CYCopyCFNumber(JSContextRef context, JSValueRef value) {
+double CYCastDouble(JSContextRef context, JSValueRef value) {
JSValueRef exception(NULL);
double number(JSValueToNumber(context, value, &exception));
CYThrow(context, exception);
+ return number;
+}
+
+CFNumberRef CYCopyCFNumber(JSContextRef context, JSValueRef value) {
+ double number(CYCastDouble(context, value));
return CFNumberCreate(kCFAllocatorDefault, kCFNumberDoubleType, &number);
}
}
CFTypeRef CYCopyCFType(JSContextRef context, JSValueRef value) {
- switch (JSValueGetType(context, value)) {
+ switch (JSType type = JSValueGetType(context, value)) {
case kJSTypeUndefined:
return CFRetain([WebUndefined undefined]);
case kJSTypeNull:
case kJSTypeObject:
return CFRetain((CFTypeRef) CYCastNSObject(context, (JSObjectRef) value));
default:
- _assert(false);
+ @throw [NSException exceptionWithName:NSInternalInconsistencyException reason:[NSString stringWithFormat:@"JSValueGetType() == 0x%x", type] userInfo:nil];
}
}
- (void) removeObjectForKey:(id)key {
JSValueRef exception(NULL);
- // XXX: this returns a bool
+ // XXX: this returns a bool... throw exception, or ignore?
JSObjectDeleteProperty(context_, object_, CYString(key), &exception);
CYThrow(context_, exception);
}
JSValueRef exception(NULL);
JSValueRef value(JSObjectGetProperty(context_, object_, length_, &exception));
CYThrow(context_, exception);
- double number(JSValueToNumber(context_, value, &exception));
- CYThrow(context_, exception);
- return number;
+ return CYCastDouble(context_, value);
}
- (id) objectAtIndex:(NSUInteger)index {
}
}
-static JSValueRef Instance_getProperty(JSContextRef context, JSObjectRef object, JSStringRef propertyName, JSValueRef *exception) {
- return NULL;
+static JSValueRef Instance_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { _pooled
+ @try {
+ NSString *name(CYCastNSString(property));
+ NSLog(@"%@", name);
+ return NULL;
+ } CYCatch
}
typedef id jocData;
return CYPoolCString(pool, CYString(context, value));
}
-// XXX: this macro is dangerous
+// XXX: this macro is unhygenic
#define CYCastCString(context, value) ({ \
JSValueRef exception(NULL); \
JSStringRef string(JSValueToStringCopy(context, value, &exception)); \
case kJSTypeString:
return dlsym(RTLD_DEFAULT, CYCastCString(context, value));
case kJSTypeObject:
- // XXX: maybe support more than just pointers, like ffis and sels
if (JSValueIsObjectOfClass(context, value, Pointer_)) {
ptrData *data(reinterpret_cast<ptrData *>(JSObjectGetPrivate((JSObjectRef) value)));
return data->value_;
}
default:
- JSValueRef exception(NULL);
- double number(JSValueToNumber(context, value, &exception));
- CYThrow(context, exception);
- return reinterpret_cast<void *>(static_cast<uintptr_t>(number));
+ return reinterpret_cast<void *>(static_cast<uintptr_t>(CYCastDouble(context, value)));
}
}
break;
#define CYPoolFFI_(primitive, native) \
- case sig::primitive ## _P: { \
- JSValueRef exception(NULL); \
- double number(JSValueToNumber(context, value, &exception)); \
- CYThrow(context, exception); \
- *reinterpret_cast<native *>(data) = number; \
- } break;
+ case sig::primitive ## _P: \
+ *reinterpret_cast<native *>(data) = CYCastDouble(context, value); \
+ break;
CYPoolFFI_(uchar, unsigned char)
CYPoolFFI_(char, char)
if (SEL sel = *reinterpret_cast<SEL *>(data)) {
selData *data(new selData(sel));
value = JSObjectMake(context, Selector_, data);
- } else value = JSValueMakeNull(context);
+ } else goto null;
} break;
case sig::pointer_P: {
if (void *pointer = *reinterpret_cast<void **>(data)) {
ptrData *data(new ptrData(pointer));
value = JSObjectMake(context, Pointer_, data);
- } else value = JSValueMakeNull(context);
+ } else goto null;
} break;
case sig::string_P: {
- char *utf8(*reinterpret_cast<char **>(data));
- value = utf8 == NULL ? JSValueMakeNull(context) : JSValueMakeString(context, CYString(utf8));
+ if (char *utf8 = *reinterpret_cast<char **>(data))
+ value = JSValueMakeString(context, CYString(utf8));
+ else goto null;
} break;
case sig::struct_P:
goto fail;
case sig::void_P:
- value = NULL;
+ value = JSValueMakeUndefined(context);
+ break;
+
+ null:
+ value = JSValueMakeNull(context);
break;
default: fail:
static JSValueRef CYCallFunction(JSContextRef context, size_t count, const JSValueRef *arguments, JSValueRef *exception, sig::Signature *signature, ffi_cif *cif, void (*function)()) { _pooled
@try {
if (count != signature->count - 1)
- [NSException raise:NSInvalidArgumentException format:@"incorrect number of arguments to ffi function"];
+ @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"incorrect number of arguments to ffi function" userInfo:nil];
CYPool pool;
void *values[count];
} CYCatch
}
-static JSValueRef Global_getProperty(JSContextRef context, JSObjectRef object, JSStringRef name, JSValueRef *exception) { _pooled
+static JSValueRef Global_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { _pooled
@try {
- NSString *string(CYCastNSString(name));
- if (Class _class = NSClassFromString(string))
+ NSString *name(CYCastNSString(property));
+ if (Class _class = NSClassFromString(name))
return CYMakeObject(context, _class);
- if (NSMutableArray *entry = [Bridge_ objectForKey:string])
+ if (NSMutableArray *entry = [Bridge_ objectForKey:name])
switch ([[entry objectAtIndex:0] intValue]) {
case 0:
return JSEvaluateScript(JSGetContext(), CYString([entry objectAtIndex:1]), NULL, NULL, 0, NULL);
case 1:
- return CYMakeFunction(context, [string cy$symbol], [[entry objectAtIndex:1] UTF8String]);
+ return CYMakeFunction(context, [name cy$symbol], [[entry objectAtIndex:1] UTF8String]);
case 2:
CYPool pool;
sig::Signature signature;
sig::Parse(pool, &signature, [[entry objectAtIndex:1] UTF8String]);
- return CYFromFFI(context, signature.elements[0].type, [string cy$symbol]);
+ return CYFromFFI(context, signature.elements[0].type, [name cy$symbol]);
}
return NULL;
} CYCatch
@try {
if (count < 2)
- [NSException raise:NSInvalidArgumentException format:@"too few arguments to objc_msgSend"];
+ @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"too few arguments to objc_msgSend" userInfo:nil];
id self(CYCastNSObject(context, arguments[0]));
if (self == nil)
SEL _cmd(CYCastSEL(context, arguments[1]));
NSMethodSignature *method([self methodSignatureForSelector:_cmd]);
if (method == nil)
- [NSException raise:NSInvalidArgumentException format:@"unrecognized selector %s sent to object %p", sel_getName(_cmd), self];
+ @throw [NSException exceptionWithName:NSInvalidArgumentException reason:[NSString stringWithFormat:@"unrecognized selector %s sent to object %p", sel_getName(_cmd), self] userInfo:nil];
type = [[method _typeString] UTF8String];
} CYCatch
JSObjectRef ffi(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
@try {
if (count != 2)
- [NSException raise:NSInvalidArgumentException format:@"incorrect number of arguments to ffi constructor"];
+ @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"incorrect number of arguments to ffi constructor" userInfo:nil];
void *function(CYCastPointer(context, arguments[0]));
const char *type(CYCastCString(context, arguments[1]));
return CYMakeFunction(context, function, type);
} CYCatch
}
-JSValueRef Pointer_getProperty_value(JSContextRef context, JSObjectRef object, JSStringRef name, JSValueRef *exception) {
+JSValueRef Pointer_getProperty_value(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) {
ptrData *data(reinterpret_cast<ptrData *>(JSObjectGetPrivate(object)));
return JSValueMakeNumber(context, reinterpret_cast<uintptr_t>(data->value_));
}
CYSetProperty(context, global, "objc_msgSend", JSObjectMakeFunctionWithCallback(context, CYString("objc_msgSend"), &$objc_msgSend));
- Bridge_ = [[NSMutableDictionary dictionaryWithContentsOfFile:@"/usr/lib/libcyrver.plist"] retain];
+ Bridge_ = [[NSMutableDictionary dictionaryWithContentsOfFile:@"/usr/lib/libcycript.plist"] retain];
name_ = JSStringCreateWithUTF8CString("name");
message_ = JSStringCreateWithUTF8CString("message");