static JSClassRef Runtime_;
static JSClassRef Selector_;
static JSClassRef Struct_;
+static JSClassRef Type_;
static JSObjectRef Array_;
static JSObjectRef Function_;
virtual ~CYData() {
}
- static void *operator new(size_t size) {
- apr_pool_t *pool;
- apr_pool_create(&pool, NULL);
+ static void *operator new(size_t size, apr_pool_t *pool) {
void *data(apr_palloc(pool, size));
reinterpret_cast<CYData *>(data)->pool_ = pool;
return data;
}
+ static void *operator new(size_t size) {
+ apr_pool_t *pool;
+ apr_pool_create(&pool, NULL);
+ return operator new(size, pool);
+ }
+
static void operator delete(void *data) {
apr_pool_destroy(reinterpret_cast<CYData *>(data)->pool_);
}
}
};
-struct Type_privateData {
- apr_pool_t *pool_;
+void Structor_(apr_pool_t *pool, const char *name, const char *types, sig::Type *&type) {
+ if (name == NULL)
+ return;
+ CYPoolTry {
+ if (NSMutableArray *entry = [[Bridge_ objectAtIndex:2] objectForKey:[NSString stringWithUTF8String:name]])
+ switch ([[entry objectAtIndex:0] intValue]) {
+ case 0: {
+ sig::Parse(pool, &type->data.signature, [[entry objectAtIndex:1] UTF8String], &Structor_);
+ } break;
+
+ case 1: {
+ sig::Signature signature;
+ sig::Parse(pool, &signature, [[entry objectAtIndex:1] UTF8String], &Structor_);
+ type = signature.elements[0].type;
+ } break;
+ }
+ } CYPoolCatch()
+}
+
+struct Type_privateData :
+ CYData
+{
ffi_type *ffi_;
sig::Type *type_;
- Type_privateData(apr_pool_t *pool, sig::Type *type) :
- pool_(pool),
+ void Set(sig::Type *type) {
+ type_ = new(pool_) sig::Type;
+ sig::Copy(pool_, *type_, *type);
+ }
+
+ Type_privateData(const char *type) :
ffi_(NULL)
{
- if (type != NULL) {
- type_ = new(pool) sig::Type;
- sig::Copy(pool, *type_, *type);
- }
+ sig::Signature signature;
+ sig::Parse(pool_, &signature, type, &Structor_);
+ type_ = signature.elements[0].type;
}
- Type_privateData(apr_pool_t *pool, sig::Type *type, ffi_type *ffi) :
- pool_(pool)
+ Type_privateData(sig::Type *type) :
+ ffi_(NULL)
{
- ffi_ = new(pool) ffi_type;
- sig::Copy(pool, *ffi_, *ffi);
- type_ = new(pool) sig::Type;
- sig::Copy(pool, *type_, *type);
+ if (type != NULL)
+ Set(type);
+ }
+
+ Type_privateData(sig::Type *type, ffi_type *ffi) {
+ ffi_ = new(pool_) ffi_type;
+ sig::Copy(pool_, *ffi_, *ffi);
+ Set(type);
}
ffi_type *GetFFI() {
Pointer(void *value, sig::Type *type, JSObjectRef owner) :
CYValue(value),
owner_(owner),
- type_(new(pool_) Type_privateData(pool_, type))
+ type_(new(pool_) Type_privateData(type))
{
}
};
JSObjectRef CYMakeStruct(JSContextRef context, void *data, sig::Type *type, ffi_type *ffi, JSObjectRef owner) {
Struct_privateData *internal(new Struct_privateData(owner));
apr_pool_t *pool(internal->pool_);
- Type_privateData *typical(new(pool) Type_privateData(pool, type, ffi));
+ Type_privateData *typical(new(pool) Type_privateData(type, ffi));
internal->type_ = typical;
if (owner != NULL)
return JSObjectMake(context, Struct_, internal);
}
-void Structor_(apr_pool_t *pool, const char *name, const char *types, sig::Type *&type) {
- if (name == NULL)
- return;
-
- CYPoolTry {
- if (NSMutableArray *entry = [[Bridge_ objectAtIndex:2] objectForKey:[NSString stringWithUTF8String:name]])
- switch ([[entry objectAtIndex:0] intValue]) {
- case 0: {
- sig::Parse(pool, &type->data.signature, [[entry objectAtIndex:1] UTF8String], &Structor_);
- } break;
-
- case 1: {
- sig::Signature signature;
- sig::Parse(pool, &signature, [[entry objectAtIndex:1] UTF8String], &Structor_);
- type = signature.elements[0].type;
- } break;
- }
- } CYPoolCatch()
-}
-
struct Functor_privateData :
CYValue
{
if (Method method = class_getInstanceMethod(_class, _cmd))
type = method_getTypeEncoding(method);
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)
+ CYTry {
+ 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
}
void *setup[2];
} CYCatch
}
+JSObjectRef CYMakeType(JSContextRef context, JSObjectRef object, const char *type) {
+ Type_privateData *internal(new Type_privateData(type));
+ return JSObjectMake(context, Type_, internal);
+}
+
+JSObjectRef Type_new(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
+ CYTry {
+ if (count != 1)
+ @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"incorrect number of arguments to Type constructor" userInfo:nil];
+ const char *type(CYCastCString(context, arguments[0]));
+ return CYMakeType(context, object, type);
+ } CYCatch
+}
+
+static JSValueRef Type_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
+ CYTry {
+ if (count != 1)
+ @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"incorrect number of arguments to type cast function" userInfo:nil];
+ Type_privateData *internal(reinterpret_cast<Type_privateData *>(JSObjectGetPrivate(object)));
+ 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, false);
+ } CYCatch
+}
+
+static JSObjectRef Type_callAsConstructor(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
+ CYTry {
+ if (count > 1)
+ @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"incorrect number of arguments to type cast function" userInfo:nil];
+ Type_privateData *internal(reinterpret_cast<Type_privateData *>(JSObjectGetPrivate(object)));
+ // XXX: alignment?
+ void *value(malloc(internal->GetFFI()->size));
+ return CYMakePointer(context, value, internal->type_, NULL);
+ } CYCatch
+}
+
JSObjectRef Functor_new(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
CYTry {
if (count != 2)
return NULL;
}
+void Unlink() {
+ pid_t pid(getpid());
+ char path[104];
+ sprintf(path, "/tmp/.s.cy.%u", pid);
+ unlink(path);
+}
+
MSInitialize { _pooled
_aprcall(apr_initialize());
_aprcall(apr_pool_create(&Pool_, NULL));
try {
_syscall(bind(Socket_, reinterpret_cast<sockaddr *>(&address), SUN_LEN(&address)));
+ atexit(&Unlink);
_syscall(listen(Socket_, 0));
apr_threadattr_t *attr;
if (Context_ == NULL) {
JSClassDefinition definition;
- definition = kJSClassDefinitionEmpty;
- definition.className = "Pointer";
- definition.staticFunctions = Pointer_staticFunctions;
- definition.getProperty = &Pointer_getProperty;
- definition.setProperty = &Pointer_setProperty;
- definition.finalize = &CYData::Finalize;
- Pointer_ = JSClassCreate(&definition);
-
definition = kJSClassDefinitionEmpty;
definition.className = "Functor";
definition.staticFunctions = Functor_staticFunctions;
Functor_ = JSClassCreate(&definition);
definition = kJSClassDefinitionEmpty;
- definition.className = "Struct";
- definition.getProperty = &Struct_getProperty;
- definition.setProperty = &Struct_setProperty;
- definition.getPropertyNames = &Struct_getPropertyNames;
+ definition.className = "Instance";
+ definition.staticValues = CYValue_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;
- Struct_ = JSClassCreate(&definition);
+ Instance_ = JSClassCreate(&definition);
+
+ definition = kJSClassDefinitionEmpty;
+ definition.className = "Pointer";
+ definition.staticFunctions = Pointer_staticFunctions;
+ definition.getProperty = &Pointer_getProperty;
+ definition.setProperty = &Pointer_setProperty;
+ definition.finalize = &CYData::Finalize;
+ Pointer_ = JSClassCreate(&definition);
definition = kJSClassDefinitionEmpty;
definition.className = "Selector";
Selector_ = JSClassCreate(&definition);
definition = kJSClassDefinitionEmpty;
- definition.className = "Instance";
- definition.staticValues = CYValue_staticValues;
- definition.staticFunctions = Instance_staticFunctions;
- definition.getProperty = &Instance_getProperty;
- definition.setProperty = &Instance_setProperty;
- definition.deleteProperty = &Instance_deleteProperty;
- definition.callAsConstructor = &Instance_callAsConstructor;
+ definition.className = "Struct";
+ definition.getProperty = &Struct_getProperty;
+ definition.setProperty = &Struct_setProperty;
+ definition.getPropertyNames = &Struct_getPropertyNames;
definition.finalize = &CYData::Finalize;
- Instance_ = JSClassCreate(&definition);
+ Struct_ = JSClassCreate(&definition);
+
+ definition = kJSClassDefinitionEmpty;
+ definition.className = "Type";
+ definition.callAsFunction = &Type_callAsFunction;
+ definition.callAsConstructor = &Type_callAsConstructor;
+ definition.finalize = &CYData::Finalize;
+ Type_ = JSClassCreate(&definition);
definition = kJSClassDefinitionEmpty;
definition.className = "Runtime";
CYSetProperty(context, global, CYJSString("ObjectiveC"), JSObjectMake(context, Runtime_, NULL));
CYSetProperty(context, global, CYJSString("Functor"), JSObjectMakeConstructor(context, Functor_, &Functor_new));
+ CYSetProperty(context, global, CYJSString("Instance"), JSObjectMakeConstructor(context, Instance_, NULL));
CYSetProperty(context, global, CYJSString("Pointer"), JSObjectMakeConstructor(context, Pointer_, &Pointer_new));
CYSetProperty(context, global, CYJSString("Selector"), JSObjectMakeConstructor(context, Selector_, &Selector_new));
+ CYSetProperty(context, global, CYJSString("Type"), JSObjectMakeConstructor(context, Type_, &Type_new));
MSHookFunction(&objc_registerClassPair, MSHake(objc_registerClassPair));