#include <set>
#include <map>
+#include <cmath>
+
#include "Parser.hpp"
#include "Cycript.tab.hh"
CFLog(kCFLogLevelNotice, CFSTR("_trace():%u"), __LINE__); \
} while (false)
+#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]; \
+ } \
+}
+
static JSGlobalContextRef Context_;
static JSObjectRef System_;
static JSClassRef Functor_;
static JSClassRef Instance_;
static JSClassRef Pointer_;
+static JSClassRef Runtime_;
static JSClassRef Selector_;
+static JSClassRef Struct_;
static JSObjectRef Array_;
static JSObjectRef Function_;
static Class NSCFBoolean_;
-static NSMutableDictionary *Bridge_;
+static NSArray *Bridge_;
struct Client {
CFHTTPMessageRef message_;
CFSocketRef socket_;
};
-struct ptrData {
+struct CYData {
apr_pool_t *pool_;
- void *value_;
- sig::Type type_;
+
+ virtual ~CYData() {
+ }
void *operator new(size_t size) {
apr_pool_t *pool;
apr_pool_create(&pool, NULL);
void *data(apr_palloc(pool, size));
- reinterpret_cast<ptrData *>(data)->pool_ = pool;
+ reinterpret_cast<CYData *>(data)->pool_ = pool;
return data;;
}
- ptrData(void *value) :
- value_(value)
- {
+ static void Finalize(JSObjectRef object) {
+ CYData *data(reinterpret_cast<CYData *>(JSObjectGetPrivate(object)));
+ data->~CYData();
+ apr_pool_destroy(data->pool_);
}
+};
- virtual ~ptrData() {
+struct Pointer_privateData :
+ CYData
+{
+ void *value_;
+ sig::Type type_;
+
+ Pointer_privateData() {
+ }
+
+ Pointer_privateData(void *value) :
+ value_(value)
+ {
}
};
-struct ffiData : ptrData {
+struct Functor_privateData :
+ Pointer_privateData
+{
sig::Signature signature_;
ffi_cif cif_;
- ffiData(const char *type, void (*value)()) :
- ptrData(reinterpret_cast<void *>(value))
+ Functor_privateData(const char *type, void (*value)()) :
+ Pointer_privateData(reinterpret_cast<void *>(value))
{
sig::Parse(pool_, &signature_, type);
sig::sig_ffi_cif(pool_, &sig::ObjectiveC, &signature_, &cif_);
}
};
-struct ffoData : ffiData {
+struct ffoData :
+ Functor_privateData
+{
JSContextRef context_;
JSObjectRef function_;
ffoData(const char *type) :
- ffiData(type, NULL)
+ Functor_privateData(type, NULL)
{
}
};
-struct selData : ptrData {
- selData(SEL value) :
- ptrData(value)
+struct Selector_privateData : Pointer_privateData {
+ Selector_privateData(SEL value) :
+ Pointer_privateData(value)
{
}
}
};
-struct jocData : ptrData {
+struct Instance_privateData :
+ Pointer_privateData
+{
bool transient_;
- jocData(id value, bool transient) :
- ptrData(value)
+ Instance_privateData(id value, bool transient) :
+ Pointer_privateData(value)
{
}
- virtual ~jocData() {
+ virtual ~Instance_privateData() {
if (!transient_)
[GetValue() release];
}
}
};
-JSObjectRef CYMakeInstance(JSContextRef context, id object, bool transient = true) {
+namespace sig {
+
+void Copy(apr_pool_t *pool, Type &lhs, Type &rhs);
+
+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);
+ }
+ lhs.offset = rhs.offset;
+}
+
+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]);
+}
+
+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;
+
+ if (sig::IsAggregate(rhs.primitive))
+ Copy(pool, lhs.data.signature, rhs.data.signature);
+ else {
+ if (rhs.data.data.type != NULL) {
+ lhs.data.data.type = new(pool) Type;
+ Copy(pool, *lhs.data.data.type, *rhs.data.data.type);
+ }
+
+ lhs.data.data.size = rhs.data.data.size;
+ }
+}
+
+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]);
+ }
+ }
+}
+
+}
+
+struct Type_privateData {
+ sig::Type type_;
+ ffi_type ffi_;
+ //size_t count_;
+
+ Type_privateData(apr_pool_t *pool, sig::Type *type, ffi_type *ffi) {
+ sig::Copy(pool, type_, *type);
+ sig::Copy(pool, ffi_, *ffi);
+
+ /*sig::Element element;
+ element.name = NULL;
+ element.type = type;
+ element.offset = 0;
+
+ sig::Signature signature;
+ signature.elements = &element;
+ signature.count = 1;
+
+ ffi_cif cif;
+ sig::sig_ffi_cif(pool, &sig::ObjectiveC, &signature, &cif);
+ ffi_ = *cif.rtype;*/
+
+ /*if (type_->type != FFI_TYPE_STRUCT)
+ count_ = 0;
+ else {
+ size_t count(0);
+ while (type_->elements[count] != NULL)
+ ++count;
+ count_ = count;
+ }*/
+ }
+};
+
+struct Struct_privateData :
+ Pointer_privateData
+{
+ JSObjectRef owner_;
+ Type_privateData *type_;
+
+ Struct_privateData() {
+ }
+};
+
+struct CStringMapLess :
+ std::binary_function<const char *, const char *, bool>
+{
+ _finline bool operator ()(const char *lhs, const char *rhs) const {
+ return strcmp(lhs, rhs) < 0;
+ }
+};
+
+typedef std::map<const char *, Type_privateData *, CStringMapLess> TypeMap;
+static TypeMap Types_;
+
+JSObjectRef CYMakeStruct(JSContextRef context, void *data, sig::Type *type, ffi_type *ffi, JSObjectRef owner) {
+ Struct_privateData *internal(new Struct_privateData());
+ apr_pool_t *pool(internal->pool_);
+ Type_privateData *typical(new(pool) Type_privateData(pool, type, ffi));
+ internal->type_ = typical;
+
+ if (owner != NULL) {
+ internal->owner_ = owner;
+ internal->value_ = data;
+ } else {
+ internal->owner_ = NULL;
+
+ size_t size(typical->ffi_.size);
+ void *copy(apr_palloc(internal->pool_, size));
+ memcpy(copy, data, size);
+ internal->value_ = copy;
+ }
+
+ return JSObjectMake(context, Struct_, internal);
+}
+
+JSObjectRef CYMakeInstance(JSContextRef context, id object, bool transient) {
if (!transient)
object = [object retain];
- jocData *data(new jocData(object, transient));
+ Instance_privateData *data(new Instance_privateData(object, transient));
return JSObjectMake(context, Instance_, data);
}
@interface NSObject (Cycript)
- (bool) cy$isUndefined;
- (NSString *) cy$toJSON;
-- (JSValueRef) cy$JSValueInContext:(JSContextRef)context;
+- (JSValueRef) cy$JSValueInContext:(JSContextRef)context transient:(bool)transient;
+- (NSObject *) cy$getProperty:(NSString *)name;
+- (bool) cy$setProperty:(NSString *)name to:(NSObject *)value;
+- (bool) cy$deleteProperty:(NSString *)name;
@end
@interface NSString (Cycript)
return [self description];
}
-- (JSValueRef) cy$JSValueInContext:(JSContextRef)context {
- return CYMakeInstance(context, self);
+- (JSValueRef) cy$JSValueInContext:(JSContextRef)context transient:(bool)transient {
+ return CYMakeInstance(context, self, transient);
+}
+
+- (NSObject *) cy$getProperty:(NSString *)name {
+ if (![name isEqualToString:@"prototype"])
+ NSLog(@"get:%@", name);
+ return nil;
+}
+
+- (bool) cy$setProperty:(NSString *)name to:(NSObject *)value {
+ NSLog(@"set:%@", name);
+ return false;
+}
+
+- (bool) cy$deleteProperty:(NSString *)name {
+ NSLog(@"delete:%@", name);
+ return false;
}
@end
return @"undefined";
}
-- (JSValueRef) cy$JSValueInContext:(JSContextRef)context {
+- (JSValueRef) cy$JSValueInContext:(JSContextRef)context transient:(bool)transient {
return CYJSUndefined(context);
}
return json;
}
+- (NSObject *) cy$getProperty:(NSString *)name {
+ int index([name intValue]);
+ if (index < 0 || index >= static_cast<int>([self count]))
+ return [super cy$getProperty:name];
+ else
+ return [self objectAtIndex:index];
+}
+
+@end
+
+@implementation NSMutableArray (Cycript)
+
+- (bool) cy$setProperty:(NSString *)name to:(NSObject *)value {
+ int index([name intValue]);
+ if (index < 0 || index >= static_cast<int>([self count]))
+ return [super cy$setProperty:name to:value];
+ else {
+ [self replaceObjectAtIndex:index withObject:(value ?: [NSNull null])];
+ return true;
+ }
+}
+
+- (bool) cy$deleteProperty:(NSString *)name {
+ int index([name intValue]);
+ if (index < 0 || index >= static_cast<int>([self count]))
+ return [super cy$deleteProperty:name];
+ else {
+ [self removeObjectAtIndex:index];
+ return true;
+ }
+}
+
@end
@implementation NSDictionary (Cycript)
return json;
}
+- (NSObject *) cy$getProperty:(NSString *)name {
+ return [self objectForKey:name];
+}
+
+@end
+
+@implementation NSMutableDictionary (Cycript)
+
+- (bool) cy$setProperty:(NSString *)name to:(NSObject *)value {
+ [self setObject:(value ?: [NSNull null]) forKey:name];
+ return true;
+}
+
+- (bool) cy$deleteProperty:(NSString *)name {
+ if ([self objectForKey:name] == nil)
+ return false;
+ else {
+ [self removeObjectForKey:name];
+ return true;
+ }
+}
+
@end
@implementation NSNumber (Cycript)
return [self class] != NSCFBoolean_ ? [self stringValue] : [self boolValue] ? @"true" : @"false";
}
-- (JSValueRef) cy$JSValueInContext:(JSContextRef)context {
+- (JSValueRef) cy$JSValueInContext:(JSContextRef)context transient:(bool)transient {
return [self class] != NSCFBoolean_ ? CYCastJSValue(context, [self doubleValue]) : CYCastJSValue(context, [self boolValue]);
}
return Context_;
}
+#define CYTry \
+ @try
#define CYCatch \
@catch (id error) { \
- NSLog(@"e:%@", error); \
CYThrow(context, error, exception); \
return NULL; \
}
}
}
+CFTypeRef CYPoolRelease(apr_pool_t *pool, CFTypeRef object) {
+ return (CFTypeRef) CYPoolRelease(pool, (id) object);
+}
+
id CYCastNSObject(apr_pool_t *pool, JSContextRef context, JSObjectRef object) {
if (JSValueIsObjectOfClass(context, object, Instance_)) {
- jocData *data(reinterpret_cast<jocData *>(JSObjectGetPrivate(object)));
+ Instance_privateData *data(reinterpret_cast<Instance_privateData *>(JSObjectGetPrivate(object)));
return data->GetValue();
}
return CYCopyCFString(CYJSString(context, value));
}
+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));
return CFNumberCreate(kCFAllocatorDefault, kCFNumberDoubleType, &number);
}
+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 CYPoolRelease(pool, reinterpret_cast<const NSString *>(CYCopyCFString(value)));
+ return (NSString *) CYPoolRelease(pool, CYCopyCFString(value));
}
bool CYCastBool(JSContextRef context, JSValueRef value) {
if (cast != copy)
return object;
else if (copy)
- return CYPoolRelease(pool, (id) object);
+ return CYPoolRelease(pool, object);
else
return CFRetain(object);
}
return CYCastJSValue(context, CYJSString(value));
}
-JSValueRef CYCastJSValue(JSContextRef context, id value) {
- return value == nil ? CYJSNull(context) : [value cy$JSValueInContext:context];
+JSValueRef CYCastJSValue(JSContextRef context, id value, bool transient = true) {
+ return value == nil ? CYJSNull(context) : [value cy$JSValueInContext:context transient:transient];
}
JSObjectRef CYCastJSObject(JSContextRef context, JSValueRef value) {
return object;
}
+JSValueRef CYGetProperty(JSContextRef context, JSObjectRef object, size_t index) {
+ JSValueRef exception(NULL);
+ JSValueRef value(JSObjectGetPropertyAtIndex(context, object, index, &exception));
+ CYThrow(context, exception);
+ return value;
+}
+
JSValueRef CYGetProperty(JSContextRef context, JSObjectRef object, JSStringRef name) {
JSValueRef exception(NULL);
JSValueRef value(JSObjectGetProperty(context, object, name, &exception));
}
void CYThrow(JSContextRef context, id error, JSValueRef *exception) {
+ if (exception == NULL)
+ throw error;
*exception = CYCastJSValue(context, error);
}
@end
-CFStringRef CYCopyJSONString(JSContextRef context, JSValueRef value) { _pooled
- id object(CYCastNSObject(NULL, context, value));
- return reinterpret_cast<CFStringRef>([(object == nil ? @"null" : [object cy$toJSON]) retain]);
+CFStringRef CYCopyJSONString(JSContextRef context, JSValueRef value, JSValueRef *exception) {
+ CYTry {
+ CYPoolTry {
+ id object(CYCastNSObject(NULL, context, value));
+ return reinterpret_cast<CFStringRef>([(object == nil ? @"null" : [object cy$toJSON]) retain]);
+ } CYPoolCatch(NULL)
+ } CYCatch
}
-const char *CYPoolJSONString(apr_pool_t *pool, JSContextRef context, JSValueRef value) {
- NSString *json((NSString *) CYCopyJSONString(context, value));
- const char *string(CYPoolCString(pool, json));
- [json release];
- return string;
+const char *CYPoolJSONString(apr_pool_t *pool, JSContextRef context, JSValueRef value, JSValueRef *exception) {
+ if (NSString *json = (NSString *) CYCopyJSONString(context, value, exception)) {
+ const char *string(CYPoolCString(pool, json));
+ [json release];
+ return string;
+ } else return NULL;
}
static void OnData(CFSocketRef socket, CFSocketCallBackType type, CFDataRef address, const void *value, void *info) {
CFHTTPMessageRef response(CFHTTPMessageCreateResponse(kCFAllocatorDefault, 200, NULL, kCFHTTPVersion1_1));
CFHTTPMessageSetHeaderFieldValue(response, CFSTR("Content-Type"), CFSTR("application/json; charset=utf-8"));
- CFStringRef json(CYCopyJSONString(CYGetJSContext(), result));
+ CFStringRef json(CYCopyJSONString(CYGetJSContext(), result, NULL));
CFDataRef body(CFStringCreateExternalRepresentation(kCFAllocatorDefault, json, kCFStringEncodingUTF8, NULL));
CFRelease(json);
}
static JSValueRef Instance_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) {
- @try {
+ CYTry {
CYPool pool;
+ NSString *self(CYCastNSObject(pool, context, object));
NSString *name(CYCastNSString(pool, property));
- NSLog(@"get:%@", name);
- return NULL;
+ NSObject *data([self cy$getProperty:name]);
+ return data == nil ? NULL : CYCastJSValue(context, data);
} CYCatch
}
static bool Instance_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) {
- @try {
+ CYTry {
CYPool pool;
+ NSString *self(CYCastNSObject(pool, context, object));
NSString *name(CYCastNSString(pool, property));
- NSLog(@"set:%@", name);
- return false;
+ NSString *data(CYCastNSObject(pool, context, value));
+ return [self cy$setProperty:name to:data];
} CYCatch
}
static bool Instance_deleteProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) {
- @try {
+ CYTry {
CYPool pool;
+ NSString *self(CYCastNSObject(pool, context, object));
NSString *name(CYCastNSString(pool, property));
- NSLog(@"delete:%@", name);
- return false;
+ return [self cy$deleteProperty:name];
} CYCatch
}
static JSObjectRef Instance_callAsConstructor(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
- @try {
- jocData *data(reinterpret_cast<jocData *>(JSObjectGetPrivate(object)));
- return CYMakeInstance(context, [data->GetValue() alloc]);
+ CYTry {
+ Instance_privateData *data(reinterpret_cast<Instance_privateData *>(JSObjectGetPrivate(object)));
+ return CYMakeInstance(context, [data->GetValue() alloc], true);
} CYCatch
}
JSObjectRef CYMakeSelector(JSContextRef context, SEL sel) {
- selData *data(new selData(sel));
+ Selector_privateData *data(new Selector_privateData(sel));
return JSObjectMake(context, Selector_, data);
}
JSObjectRef CYMakePointer(JSContextRef context, void *pointer) {
- ptrData *data(new ptrData(pointer));
+ Pointer_privateData *data(new Pointer_privateData(pointer));
return JSObjectMake(context, Pointer_, data);
}
-static void Pointer_finalize(JSObjectRef object) {
- ptrData *data(reinterpret_cast<ptrData *>(JSObjectGetPrivate(object)));
- data->~ptrData();
- apr_pool_destroy(data->pool_);
-}
-
JSObjectRef CYMakeFunctor(JSContextRef context, void (*function)(), const char *type) {
- ffiData *data(new ffiData(type, function));
+ Functor_privateData *data(new Functor_privateData(type, function));
return JSObjectMake(context, Functor_, data);
}
-const char *CYPoolCString(apr_pool_t *pool, JSStringRef value) {
- if (pool == NULL)
- return [CYCastNSString(NULL, value) UTF8String];
- else {
+const char *CYPoolCString(apr_pool_t *pool, JSStringRef value, size_t *length = NULL) {
+ if (pool == NULL) {
+ const char *string([CYCastNSString(NULL, value) UTF8String]);
+ if (length != NULL)
+ *length = strlen(string);
+ return string;
+ } else {
size_t size(JSStringGetMaximumUTF8CStringSize(value));
char *string(new(pool) char[size]);
JSStringGetUTF8CString(value, string, size);
+ // XXX: this is ironic
+ if (length != NULL)
+ *length = strlen(string);
return string;
}
}
-const char *CYPoolCString(apr_pool_t *pool, JSContextRef context, JSValueRef value) {
- if (JSValueIsNull(context, value))
+const char *CYPoolCString(apr_pool_t *pool, JSContextRef context, JSValueRef value, size_t *length = NULL) {
+ if (!JSValueIsNull(context, value))
+ return CYPoolCString(pool, CYJSString(context, value), length);
+ else {
+ if (length != NULL)
+ *length = 0;
return NULL;
- return CYPoolCString(pool, CYJSString(context, value));
+ }
}
// XXX: this macro is unhygenic
if (JSValueIsNull(context, value))
return NULL;
else if (JSValueIsObjectOfClass(context, value, Selector_)) {
- selData *data(reinterpret_cast<selData *>(JSObjectGetPrivate((JSObjectRef) value)));
+ Selector_privateData *data(reinterpret_cast<Selector_privateData *>(JSObjectGetPrivate((JSObjectRef) value)));
return reinterpret_cast<SEL>(data->value_);
} else
return sel_registerName(CYCastCString(context, value));
switch (JSValueGetType(context, value)) {
case kJSTypeNull:
return NULL;
- case kJSTypeString:
+ /*case kJSTypeString:
return dlsym(RTLD_DEFAULT, CYCastCString(context, value));
case kJSTypeObject:
if (JSValueIsObjectOfClass(context, value, Pointer_)) {
- ptrData *data(reinterpret_cast<ptrData *>(JSObjectGetPrivate((JSObjectRef) value)));
+ Pointer_privateData *data(reinterpret_cast<Pointer_privateData *>(JSObjectGetPrivate((JSObjectRef) value)));
return data->value_;
- }
+ }*/
default:
- return reinterpret_cast<void *>(static_cast<uintptr_t>(CYCastDouble(context, value)));
+ 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)));
}
}
return reinterpret_cast<Type_>(CYCastPointer_(context, value));
}
-void CYPoolFFI(apr_pool_t *pool, JSContextRef context, sig::Type *type, void *data, JSValueRef value) {
+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);
*reinterpret_cast<const char **>(data) = CYPoolCString(pool, context, value);
break;
- case sig::struct_P:
- goto fail;
+ case sig::struct_P: {
+ uint8_t *base(reinterpret_cast<uint8_t *>(data));
+ bool aggregate(JSValueIsObject(context, value));
+ for (size_t index(0); index != type->data.signature.count; ++index) {
+ ffi_type *element(ffi->elements[index]);
+ JSValueRef rhs(aggregate ? CYGetProperty(context, (JSObjectRef) value, index) : value);
+ CYPoolFFI(pool, context, type->data.signature.elements[index].type, element, base, rhs);
+ // XXX: alignment?
+ base += element->size;
+ }
+ } break;
case sig::void_P:
break;
- default: fail:
+ default:
NSLog(@"CYPoolFFI(%c)\n", type->primitive);
_assert(false);
}
}
-JSValueRef CYFromFFI(JSContextRef context, sig::Type *type, void *data) {
+JSValueRef CYFromFFI(JSContextRef context, sig::Type *type, ffi_type *ffi, void *data, JSObjectRef owner = NULL) {
JSValueRef value;
switch (type->primitive) {
break;
case sig::typename_P:
- value = CYMakeInstance(context, *reinterpret_cast<Class *>(data));
+ value = CYMakeInstance(context, *reinterpret_cast<Class *>(data), true);
break;
case sig::selector_P:
break;
case sig::struct_P:
- goto fail;
+ value = CYMakeStruct(context, data, type, ffi, owner);
+ break;
case sig::void_P:
value = CYJSUndefined(context);
value = CYJSNull(context);
break;
- default: fail:
+ default:
NSLog(@"CYFromFFI(%c)\n", type->primitive);
_assert(false);
}
return value;
}
+void Index_(Struct_privateData *internal, double number, ssize_t &index, uint8_t *&base) {
+ Type_privateData *typical(internal->type_);
+
+ index = static_cast<ssize_t>(number);
+ if (index != number)
+ @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"struct index non-integral" userInfo:nil];
+ if (index < 0)
+ @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"struct index negative" userInfo:nil];
+
+ base = reinterpret_cast<uint8_t *>(internal->value_);
+ for (ssize_t local(0); local != index; ++local)
+ if (ffi_type *element = typical->ffi_.elements[local])
+ base += element->size;
+ else
+ @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"struct index out-of-range" userInfo:nil];
+}
+
+static JSValueRef Struct_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) {
+ CYTry {
+ CYPool pool;
+ Struct_privateData *internal(reinterpret_cast<Struct_privateData *>(JSObjectGetPrivate(object)));
+ Type_privateData *typical(internal->type_);
+
+ size_t length;
+ const char *name(CYPoolCString(pool, property, &length));
+ double number(CYCastDouble(name, length));
+
+ if (std::isnan(number)) {
+ // XXX: implement!
+ return NULL;
+ }
+
+ ssize_t index;
+ uint8_t *base;
+
+ Index_(internal, number, index, base);
+
+ return CYFromFFI(context, typical->type_.data.signature.elements[index].type, typical->ffi_.elements[index], base, object);
+ } CYCatch
+}
+
+static bool Struct_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) {
+ CYTry {
+ CYPool pool;
+ Struct_privateData *internal(reinterpret_cast<Struct_privateData *>(JSObjectGetPrivate(object)));
+ Type_privateData *typical(internal->type_);
+
+ size_t length;
+ const char *name(CYPoolCString(pool, property, &length));
+ double number(CYCastDouble(name, length));
+
+ if (std::isnan(number)) {
+ // XXX: implement!
+ return false;
+ }
+
+ ssize_t index;
+ uint8_t *base;
+
+ Index_(internal, number, index, base);
+
+ CYPoolFFI(NULL, context, typical->type_.data.signature.elements[index].type, typical->ffi_.elements[index], base, value);
+ return true;
+ } CYCatch
+}
+
static JSValueRef CYCallFunction(JSContextRef context, size_t count, const JSValueRef *arguments, JSValueRef *exception, sig::Signature *signature, ffi_cif *cif, void (*function)()) {
- @try {
+ CYTry {
if (count != signature->count - 1)
@throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"incorrect number of arguments to ffi function" userInfo:nil];
for (unsigned index(0); index != count; ++index) {
sig::Element *element(&signature->elements[index + 1]);
+ ffi_type *ffi(cif->arg_types[index]);
// XXX: alignment?
- values[index] = new(pool) uint8_t[cif->arg_types[index]->size];
- CYPoolFFI(pool, context, element->type, values[index], arguments[index]);
+ values[index] = new(pool) uint8_t[ffi->size];
+ CYPoolFFI(pool, context, element->type, ffi, values[index], arguments[index]);
}
uint8_t value[cif->rtype->size];
ffi_call(cif, function, value, values);
- return CYFromFFI(context, signature->elements[0].type, value);
+ return CYFromFFI(context, signature->elements[0].type, cif->rtype, value);
} CYCatch
}
void Closure_(ffi_cif *cif, void *result, void **arguments, void *arg) {
- NSLog(@"Closure()");
ffoData *data(reinterpret_cast<ffoData *>(arg));
JSContextRef context(data->context_);
JSValueRef values[count];
for (size_t index(0); index != count; ++index)
- values[index] = CYFromFFI(context, data->signature_.elements[1 + index].type, arguments[index]);
+ values[index] = CYFromFFI(context, data->signature_.elements[1 + index].type, data->cif_.arg_types[index], arguments[index]);
JSValueRef exception(NULL);
JSValueRef value(JSObjectCallAsFunction(context, data->function_, NULL, count, values, &exception));
CYThrow(context, exception);
- CYPoolFFI(NULL, context, data->signature_.elements[0].type, result, value);
+ CYPoolFFI(NULL, context, data->signature_.elements[0].type, data->cif_.rtype, result, value);
}
JSObjectRef CYMakeFunctor(JSContextRef context, JSObjectRef function, const char *type) {
return JSObjectMake(context, Functor_, data);
}
-static JSValueRef Global_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) {
- @try {
+static JSValueRef Runtime_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);
- if (NSMutableArray *entry = [Bridge_ objectForKey:name])
+ return CYMakeInstance(context, _class, true);
+ if (NSMutableArray *entry = [[Bridge_ objectAtIndex:0] objectForKey:name])
switch ([[entry objectAtIndex:0] intValue]) {
case 0:
return JSEvaluateScript(CYGetJSContext(), CYJSString([entry objectAtIndex:1]), NULL, NULL, 0, NULL);
case 1:
return CYMakeFunctor(context, reinterpret_cast<void (*)()>([name cy$symbol]), CYPoolCString(pool, [entry objectAtIndex:1]));
case 2:
+ // XXX: this is horrendously inefficient
sig::Signature signature;
sig::Parse(pool, &signature, CYPoolCString(pool, [entry objectAtIndex:1]));
- return CYFromFFI(context, signature.elements[0].type, [name cy$symbol]);
+ ffi_cif cif;
+ sig::sig_ffi_cif(pool, &sig::ObjectiveC, &signature, &cif);
+ return CYFromFFI(context, signature.elements[0].type, cif.rtype, [name cy$symbol]);
}
return NULL;
} CYCatch
}
static JSValueRef System_print(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
- @try {
+ CYTry {
NSLog(@"%s", CYCastCString(context, arguments[0]));
return CYJSUndefined(context);
} CYCatch
}
static JSValueRef CYApplicationMain(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
- @try {
+ CYTry {
CYPool pool;
NSString *name(CYCastNSObject(pool, context, arguments[0]));
int argc(*_NSGetArgc());
CYPool pool;
- @try {
+ CYTry {
if (count < 2)
@throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"too few arguments to objc_msgSend" userInfo:nil];
Class _class(object_getClass(self));
if (Method method = class_getInstanceMethod(_class, _cmd))
type = method_getTypeEncoding(method);
- else { _pooled
- NSMethodSignature *method([self methodSignatureForSelector:_cmd]);
- if (method == nil)
- @throw [NSException exceptionWithName:NSInvalidArgumentException reason:[NSString stringWithFormat:@"unrecognized selector %s sent to object %p", sel_getName(_cmd), self] userInfo:nil];
- type = CYPoolCString(pool, [method _typeString]);
+ else {
+ CYPoolTry {
+ NSMethodSignature *method([self methodSignatureForSelector:_cmd]);
+ if (method == nil)
+ @throw [NSException exceptionWithName:NSInvalidArgumentException reason:[NSString stringWithFormat:@"unrecognized selector %s sent to object %p", sel_getName(_cmd), self] userInfo:nil];
+ type = CYPoolCString(pool, [method _typeString]);
+ } CYPoolCatch(NULL)
}
} CYCatch
}
static JSValueRef Functor_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
- ffiData *data(reinterpret_cast<ffiData *>(JSObjectGetPrivate(object)));
+ Functor_privateData *data(reinterpret_cast<Functor_privateData *>(JSObjectGetPrivate(object)));
return CYCallFunction(context, count, arguments, exception, &data->signature_, &data->cif_, reinterpret_cast<void (*)()>(data->value_));
}
JSObjectRef Selector_new(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
- @try {
+ CYTry {
if (count != 1)
@throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"incorrect number of arguments to Selector constructor" userInfo:nil];
const char *name(CYCastCString(context, arguments[0]));
}
JSObjectRef Functor_new(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
- @try {
+ CYTry {
if (count != 2)
@throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"incorrect number of arguments to Functor constructor" userInfo:nil];
const char *type(CYCastCString(context, arguments[1]));
}
JSValueRef Pointer_getProperty_value(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) {
- ptrData *data(reinterpret_cast<ptrData *>(JSObjectGetPrivate(object)));
+ Pointer_privateData *data(reinterpret_cast<Pointer_privateData *>(JSObjectGetPrivate(object)));
return CYCastJSValue(context, reinterpret_cast<uintptr_t>(data->value_));
}
return Function_;
}
-static JSValueRef Instance_callAsFunction_toString(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { _pooled
- @try {
- jocData *data(reinterpret_cast<jocData *>(JSObjectGetPrivate(_this)));
- return CYCastJSValue(context, CYJSString([data->GetValue() description]));
+static JSValueRef Pointer_callAsFunction_valueOf(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
+ CYTry {
+ Pointer_privateData *data(reinterpret_cast<Pointer_privateData *>(JSObjectGetPrivate(_this)));
+ return CYCastJSValue(context, reinterpret_cast<uintptr_t>(data->value_));
+ } CYCatch
+}
+
+static JSValueRef Instance_callAsFunction_toString(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
+ CYTry {
+ Instance_privateData *data(reinterpret_cast<Instance_privateData *>(JSObjectGetPrivate(_this)));
+ CYPoolTry {
+ return CYCastJSValue(context, CYJSString([data->GetValue() description]));
+ } CYPoolCatch(NULL)
} CYCatch
}
static JSValueRef Selector_callAsFunction_toString(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
- @try {
- selData *data(reinterpret_cast<selData *>(JSObjectGetPrivate(_this)));
+ CYTry {
+ Selector_privateData *data(reinterpret_cast<Selector_privateData *>(JSObjectGetPrivate(_this)));
return CYCastJSValue(context, sel_getName(data->GetValue()));
} CYCatch
}
static JSValueRef Selector_callAsFunction_type(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
- @try {
+ CYTry {
if (count != 2)
@throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"incorrect number of arguments to Selector.type" userInfo:nil];
CYPool pool;
- selData *data(reinterpret_cast<selData *>(JSObjectGetPrivate(_this)));
+ Selector_privateData *data(reinterpret_cast<Selector_privateData *>(JSObjectGetPrivate(_this)));
Class _class(CYCastNSObject(pool, context, arguments[0]));
bool instance(CYCastBool(context, arguments[1]));
SEL sel(data->GetValue());
if (Method method = (*(instance ? &class_getInstanceMethod : class_getClassMethod))(_class, sel))
return CYCastJSValue(context, method_getTypeEncoding(method));
- else if (NSString *type = [Bridge_ objectForKey:CYPoolRelease(pool, [[NSString alloc] initWithFormat:@":%s", sel_getName(sel)])])
+ else if (NSString *type = [[Bridge_ objectAtIndex:1] objectForKey:CYCastNSString(pool, sel_getName(sel))])
return CYCastJSValue(context, CYJSString(type));
else
return CYJSNull(context);
{NULL, NULL, NULL, 0}
};
+static JSStaticFunction Pointer_staticFunctions[2] = {
+ {"valueOf", &Pointer_callAsFunction_valueOf, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
+ {NULL, NULL, 0}
+};
+
/*static JSStaticValue Selector_staticValues[2] = {
{"prototype", &Selector_getProperty_prototype, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete},
{NULL, NULL, NULL, 0}
MSInitialize { _pooled
apr_initialize();
+ Bridge_ = [[NSMutableArray arrayWithContentsOfFile:@"/usr/lib/libcycript.plist"] retain];
+
NSCFBoolean_ = objc_getClass("NSCFBoolean");
pid_t pid(getpid());
definition = kJSClassDefinitionEmpty;
definition.className = "Pointer";
definition.staticValues = Pointer_staticValues;
- definition.finalize = &Pointer_finalize;
+ definition.staticFunctions = Pointer_staticFunctions;
+ definition.finalize = &CYData::Finalize;
Pointer_ = JSClassCreate(&definition);
definition = kJSClassDefinitionEmpty;
definition.className = "Functor";
- definition.parentClass = Pointer_;
+ definition.staticValues = Pointer_staticValues;
+ definition.staticFunctions = Pointer_staticFunctions;
definition.callAsFunction = &Functor_callAsFunction;
+ definition.finalize = &CYData::Finalize;
Functor_ = JSClassCreate(&definition);
+ definition = kJSClassDefinitionEmpty;
+ definition.className = "Struct";
+ definition.getProperty = &Struct_getProperty;
+ definition.setProperty = &Struct_setProperty;
+ definition.finalize = &CYData::Finalize;
+ Struct_ = JSClassCreate(&definition);
+
definition = kJSClassDefinitionEmpty;
definition.className = "Selector";
- definition.parentClass = Pointer_;
+ definition.staticValues = Pointer_staticValues;
//definition.staticValues = Selector_staticValues;
definition.staticFunctions = Selector_staticFunctions;
definition.callAsFunction = &Selector_callAsFunction;
+ definition.finalize = &CYData::Finalize;
Selector_ = JSClassCreate(&definition);
definition = kJSClassDefinitionEmpty;
definition.className = "Instance";
- definition.parentClass = Pointer_;
+ definition.staticValues = Pointer_staticValues;
definition.staticFunctions = Instance_staticFunctions;
definition.getProperty = &Instance_getProperty;
definition.setProperty = &Instance_setProperty;
definition.deleteProperty = &Instance_deleteProperty;
definition.callAsConstructor = &Instance_callAsConstructor;
+ definition.finalize = &CYData::Finalize;
Instance_ = JSClassCreate(&definition);
definition = kJSClassDefinitionEmpty;
- definition.getProperty = &Global_getProperty;
+ definition.className = "Runtime";
+ definition.getProperty = &Runtime_getProperty;
+ Runtime_ = JSClassCreate(&definition);
+
+ definition = kJSClassDefinitionEmpty;
+ //definition.getProperty = &Global_getProperty;
JSClassRef Global(JSClassCreate(&definition));
JSGlobalContextRef context(JSGlobalContextCreate(Global));
JSObjectRef global(JSContextGetGlobalObject(context));
+ JSObjectSetPrototype(context, global, JSObjectMake(context, Runtime_, NULL));
+ CYSetProperty(context, global, CYJSString("obc"), JSObjectMake(context, Runtime_, NULL));
+
CYSetProperty(context, global, CYJSString("Selector"), JSObjectMakeConstructor(context, Selector_, &Selector_new));
CYSetProperty(context, global, CYJSString("Functor"), JSObjectMakeConstructor(context, Functor_, &Functor_new));
CYSetProperty(context, System_, CYJSString("print"), JSObjectMakeFunctionWithCallback(context, CYJSString("print"), &System_print));
- Bridge_ = [[NSMutableDictionary dictionaryWithContentsOfFile:@"/usr/lib/libcycript.plist"] retain];
-
name_ = JSStringCreateWithUTF8CString("name");
message_ = JSStringCreateWithUTF8CString("message");
length_ = JSStringCreateWithUTF8CString("length");