#include "Pooling.hpp"
#include "Struct.hpp"
-#include <unistd.h>
-
#include <CoreFoundation/CoreFoundation.h>
#include <CoreFoundation/CFLogUtilities.h>
#include <WebKit/WebScriptObject.h>
-#include <sys/types.h>
-#include <sys/socket.h>
-#include <netinet/in.h>
-#include <sys/un.h>
-
#include <sys/mman.h>
#include <iostream>
#include "Parser.hpp"
#include "Cycript.tab.hh"
-#include <fcntl.h>
-
#include <apr-1/apr_thread_proc.h>
#undef _assert
static JSClassRef Functor_;
static JSClassRef Instance_;
static JSClassRef Internal_;
+static JSClassRef Message_;
static JSClassRef Pointer_;
+static JSClassRef Prototype_;
static JSClassRef Runtime_;
static JSClassRef Selector_;
static JSClassRef Struct_;
static JSStringRef length_;
static JSStringRef message_;
static JSStringRef name_;
+static JSStringRef prototype_;
static JSStringRef toCYON_;
static JSStringRef toJSON_;
+static JSObjectRef Array_prototype_;
+static JSObjectRef Array_pop_;
+static JSObjectRef Array_push_;
+static JSObjectRef Array_splice_;
+
+static Class NSArray_;
static Class NSCFBoolean_;
static Class NSCFType_;
static Class NSMessageBuilder_;
static NSArray *Bridge_;
-struct CYData {
- apr_pool_t *pool_;
-
- virtual ~CYData() {
- }
-
- 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_);
- }
+static void Finalize(JSObjectRef object) {
+ delete reinterpret_cast<CYData *>(JSObjectGetPrivate(object));
+}
- static void Finalize(JSObjectRef object) {
- delete reinterpret_cast<CYData *>(JSObjectGetPrivate(object));
- }
-};
+class Type_privateData;
struct CYValue :
CYData
value_(rhs.value_)
{
}
+
+ virtual Type_privateData *GetType() const {
+ return NULL;
+ }
};
struct Selector_privateData :
SEL GetValue() const {
return reinterpret_cast<SEL>(value_);
}
+
+ virtual Type_privateData *GetType() const;
};
struct Instance :
[GetValue() performSelector:@selector(release) withObject:nil afterDelay:0];
}
- static JSObjectRef Make(JSContextRef context, id object, Flags flags) {
- return JSObjectMake(context, Instance_, new Instance(object, flags));
+ static JSObjectRef Make(JSContextRef context, id object, Flags flags = None) {
+ JSObjectRef value(JSObjectMake(context, Instance_, new Instance(object, flags)));
+ if (object != nil)
+ for (Class _class(object_getClass(object)); _class != nil; _class = class_getSuperclass(_class))
+ if (_class == NSArray_) {
+ JSObjectSetPrototype(context, value, Array_prototype_);
+ break;
+ }
+ return value;
}
id GetValue() const {
bool IsUninitialized() const {
return (flags_ & Uninitialized) != 0;
}
+
+ virtual Type_privateData *GetType() const;
+};
+
+struct Prototype :
+ CYValue
+{
+ Prototype(Class value) :
+ CYValue(value)
+ {
+ }
+
+ static JSObjectRef Make(JSContextRef context, Class _class, bool array = false) {
+ JSObjectRef value(JSObjectMake(context, Prototype_, new Prototype(_class)));
+ if (_class == NSArray_)
+ array = true;
+ if (Class super = class_getSuperclass(_class))
+ JSObjectSetPrototype(context, value, Prototype::Make(context, super, array));
+ /*else if (array)
+ JSObjectSetPrototype(context, value, Array_prototype_);*/
+ return value;
+ }
+
+ Class GetValue() const {
+ return reinterpret_cast<Class>(value_);
+ }
};
struct Internal :
struct Type_privateData :
CYData
{
+ static Type_privateData *Object;
+ static Type_privateData *Selector;
+
ffi_type *ffi_;
sig::Type *type_;
}
};
+Type_privateData *Type_privateData::Object;
+Type_privateData *Type_privateData::Selector;
+
+Type_privateData *Instance::GetType() const {
+ return Type_privateData::Object;
+}
+
+Type_privateData *Selector_privateData::GetType() const {
+ return Type_privateData::Selector;
+}
+
struct Pointer :
CYValue
{
sig::Signature signature_;
ffi_cif cif_;
+
Functor_privateData(const char *type, void (*value)()) :
CYValue(reinterpret_cast<void *>(value))
{
sig::Parse(pool_, &signature_, type, &Structor_);
sig::sig_ffi_cif(pool_, &sig::ObjectiveC, &signature_, &cif_);
}
+
+ void (*GetValue())() const {
+ return reinterpret_cast<void (*)()>(value_);
+ }
};
-struct ffoData :
+struct Closure_privateData :
Functor_privateData
{
JSContextRef context_;
JSObjectRef function_;
- ffoData(const char *type) :
+ Closure_privateData(const char *type) :
Functor_privateData(type, NULL)
{
}
};
+struct Message_privateData :
+ Functor_privateData
+{
+ SEL sel_;
+
+ Message_privateData(SEL sel, const char *type, IMP value = NULL) :
+ Functor_privateData(type, reinterpret_cast<void (*)()>(value)),
+ sel_(sel)
+ {
+ }
+};
+
JSObjectRef CYMakeInstance(JSContextRef context, id object, bool transient) {
Instance::Flags flags;
return JSValueMakeUndefined(context);
}
-bool CYGetIndex(const char *value, ssize_t &index) {
+size_t CYGetIndex(const char *value) {
+ if (value[0] != '0') {
+ char *end;
+ size_t index(strtoul(value, &end, 10));
+ if (value + strlen(value) == end)
+ return index;
+ } else if (value[1] == '\0')
+ return 0;
+ return _not(size_t);
+}
+
+size_t CYGetIndex(apr_pool_t *pool, NSString *value) {
+ return CYGetIndex(CYPoolCString(pool, value));
+}
+
+bool CYGetOffset(const char *value, ssize_t &index) {
if (value[0] != '0') {
char *end;
index = strtol(value, &end, 10);
return false;
}
-bool CYGetIndex(apr_pool_t *pool, NSString *value, ssize_t &index) {
- return CYGetIndex(CYPoolCString(pool, value), index);
+bool CYGetOffset(apr_pool_t *pool, NSString *value, ssize_t &index) {
+ return CYGetOffset(CYPoolCString(pool, value), index);
}
NSString *CYPoolNSCYON(apr_pool_t *pool, id value);
- (NSString *) cy$toCYON;
- (NSString *) cy$toKey;
+- (bool) cy$hasProperty:(NSString *)name;
- (NSObject *) cy$getProperty:(NSString *)name;
- (bool) cy$setProperty:(NSString *)name to:(NSObject *)value;
- (bool) cy$deleteProperty:(NSString *)name;
return [self cy$toCYON];
}
+- (bool) cy$hasProperty:(NSString *)name {
+ return false;
+}
+
- (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;
}
[json appendString:@","];
else
comma = true;
- if ([object cy$JSType] != kJSTypeUndefined)
+ if (object == nil || [object cy$JSType] != kJSTypeUndefined)
[json appendString:CYPoolNSCYON(NULL, object)];
else {
[json appendString:@","];
return json;
}
+- (bool) cy$hasProperty:(NSString *)name {
+ if ([name isEqualToString:@"length"])
+ return true;
+
+ size_t index(CYGetIndex(NULL, name));
+ if (index == _not(size_t) || index >= [self count])
+ return [super cy$hasProperty:name];
+ else
+ return true;
+}
+
- (NSObject *) cy$getProperty:(NSString *)name {
if ([name isEqualToString:@"length"])
return [NSNumber numberWithUnsignedInteger:[self count]];
- ssize_t index;
- if (!CYGetIndex(NULL, name, index) || index < 0 || index >= static_cast<ssize_t>([self count]))
+ size_t index(CYGetIndex(NULL, name));
+ if (index == _not(size_t) || index >= [self count])
return [super cy$getProperty:name];
else
return [self objectAtIndex:index];
@implementation NSMutableArray (Cycript)
- (bool) cy$setProperty:(NSString *)name to:(NSObject *)value {
- ssize_t index;
- if (!CYGetIndex(NULL, name, index) || index < 0 || index >= static_cast<ssize_t>([self count]))
+ if ([name isEqualToString:@"length"]) {
+ // XXX: is this not intelligent?
+ NSUInteger size([(NSNumber *)value unsignedIntegerValue]);
+ NSUInteger count([self count]);
+ if (size < count)
+ [self removeObjectsInRange:NSMakeRange(size, count - size)];
+ else if (size != count) {
+ WebUndefined *undefined([WebUndefined undefined]);
+ for (size_t i(count); i != size; ++i)
+ [self addObject:undefined];
+ }
+ return true;
+ }
+
+ size_t index(CYGetIndex(NULL, name));
+ if (index == _not(size_t))
return [super cy$setProperty:name to:value];
+
+ id object(value ?: [NSNull null]);
+
+ size_t count([self count]);
+ if (index < count)
+ [self replaceObjectAtIndex:index withObject:object];
else {
- [self replaceObjectAtIndex:index withObject:(value ?: [NSNull null])];
- return true;
+ if (index != count) {
+ WebUndefined *undefined([WebUndefined undefined]);
+ for (size_t i(count); i != index; ++i)
+ [self addObject:undefined];
+ }
+
+ [self addObject:object];
}
+
+ return true;
}
- (bool) cy$deleteProperty:(NSString *)name {
- ssize_t index;
- if (!CYGetIndex(NULL, name, index) || index < 0 || index >= static_cast<ssize_t>([self count]))
+ size_t index(CYGetIndex(NULL, name));
+ if (index == _not(size_t) || index >= [self count])
return [super cy$deleteProperty:name];
- else {
- [self removeObjectAtIndex:index];
- return true;
- }
+ [self replaceObjectAtIndex:index withObject:[WebUndefined undefined]];
+ return true;
}
@end
return json;
}
+- (bool) cy$hasProperty:(NSString *)name {
+ return [self objectForKey:name] != nil;
+}
+
- (NSObject *) cy$getProperty:(NSString *)name {
return [self objectForKey:name];
}
goto cyon;
if (DigitRange_[value[0]]) {
- ssize_t index;
- if (!CYGetIndex(NULL, self, index) || index < 0)
+ size_t index(CYGetIndex(NULL, self));
+ if (index == _not(size_t))
goto cyon;
} else {
if (!WordStartRange_[value[0]])
@end
-@interface CYJSObject : NSDictionary {
+@interface CYJSObject : NSMutableDictionary {
JSObjectRef object_;
JSContextRef context_;
}
@end
-@interface CYJSArray : NSArray {
+@interface CYJSArray : NSMutableArray {
JSObjectRef object_;
JSContextRef context_;
}
- (NSUInteger) count;
- (id) objectAtIndex:(NSUInteger)index;
+- (void) addObject:(id)anObject;
+- (void) insertObject:(id)anObject atIndex:(NSUInteger)index;
+- (void) removeLastObject;
+- (void) removeObjectAtIndex:(NSUInteger)index;
+- (void) replaceObjectAtIndex:(NSUInteger)index withObject:(id)anObject;
+
@end
CYRange DigitRange_ (0x3ff000000000000LLU, 0x000000000000000LLU); // 0-9
if (!JSValueIsObjectOfClass(context, object, Instance_))
return CYCastNSObject_(pool, context, object);
else {
- Instance *data(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
- return data->GetValue();
+ Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
+ return internal->GetValue();
}
}
return value;
}
+void CYSetProperty(JSContextRef context, JSObjectRef object, size_t index, JSValueRef value) {
+ JSValueRef exception(NULL);
+ JSObjectSetPropertyAtIndex(context, object, index, value, &exception);
+ CYThrow(context, exception);
+}
+
void CYSetProperty(JSContextRef context, JSObjectRef object, JSStringRef name, JSValueRef value) {
JSValueRef exception(NULL);
JSObjectSetProperty(context, object, name, value, kJSPropertyAttributeNone, &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 {
+ 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 {
+ 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 {
+ CYSetProperty(context_, object_, index, CYCastJSValue(context_, object));
+}
+
@end
NSString *CYCopyNSCYON(id value) {
- Class _class(object_getClass(value));
- SEL sel(@selector(cy$toCYON));
-
NSString *string;
- 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];
- else if (value == NSMessageBuilder_ || value == Object_)
- string = nil;
- else
- string = [NSString stringWithFormat:@"%@", value];
+ 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";
}
- // 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) ?: [NSNull null]);
+ return CYCopyNSCYON(CYCastNSObject(NULL, context, value));
} CYPoolCatch(NULL)
} CYCatch
}
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;
};
JSObjectRef CYMakeSelector(JSContextRef context, SEL sel) {
- Selector_privateData *data(new Selector_privateData(sel));
- return JSObjectMake(context, Selector_, data);
+ Selector_privateData *internal(new Selector_privateData(sel));
+ return JSObjectMake(context, Selector_, internal);
}
JSObjectRef CYMakePointer(JSContextRef context, void *pointer, sig::Type *type, ffi_type *ffi, JSObjectRef owner) {
- Pointer *data(new Pointer(pointer, type, owner));
- return JSObjectMake(context, Pointer_, data);
+ Pointer *internal(new Pointer(pointer, type, owner));
+ return JSObjectMake(context, Pointer_, internal);
}
JSObjectRef CYMakeFunctor(JSContextRef context, void (*function)(), const char *type) {
- Functor_privateData *data(new Functor_privateData(type, function));
- return JSObjectMake(context, Functor_, data);
+ Functor_privateData *internal(new Functor_privateData(type, function));
+ return JSObjectMake(context, Functor_, internal);
}
const char *CYPoolCString(apr_pool_t *pool, JSStringRef value) {
return JSValueIsNull(context, value) ? NULL : CYPoolCString(pool, CYJSString(context, value));
}
-bool CYGetIndex(apr_pool_t *pool, JSStringRef value, ssize_t &index) {
- return CYGetIndex(CYPoolCString(pool, value), index);
+bool CYGetOffset(apr_pool_t *pool, JSStringRef value, ssize_t &index) {
+ return CYGetOffset(CYPoolCString(pool, value), index);
}
// XXX: this macro is unhygenic
return dlsym(RTLD_DEFAULT, CYCastCString(context, value));
case kJSTypeObject:
if (JSValueIsObjectOfClass(context, value, Pointer_)) {
- Pointer *data(reinterpret_cast<Pointer *>(JSObjectGetPrivate((JSObjectRef) value)));
- return data->value_;
+ Pointer *internal(reinterpret_cast<Pointer *>(JSObjectGetPrivate((JSObjectRef) value)));
+ return internal->value_;
}*/
default:
double number(CYCastDouble(context, value));
SEL CYCastSEL(JSContextRef context, JSValueRef value) {
if (JSValueIsObjectOfClass(context, value, Selector_)) {
- Selector_privateData *data(reinterpret_cast<Selector_privateData *>(JSObjectGetPrivate((JSObjectRef) value)));
- return reinterpret_cast<SEL>(data->value_);
+ Selector_privateData *internal(reinterpret_cast<Selector_privateData *>(JSObjectGetPrivate((JSObjectRef) value)));
+ return reinterpret_cast<SEL>(internal->value_);
} else
return CYCastPointer<SEL>(context, value);
}
return value;
}
-static JSValueRef Instance_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) {
+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;
+}
+
+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;
+}
+
+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);
+}
+
+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);
+}
+
+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(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;
+
+ internal->context_ = CYGetJSContext();
+ internal->function_ = function;
+
+ return internal;
+}
+
+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 Prototype_hasProperty(JSContextRef context, JSObjectRef object, JSStringRef property) {
+ Prototype *internal(reinterpret_cast<Prototype *>(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 Prototype_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) {
+ Prototype *internal(reinterpret_cast<Prototype *>(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 Prototype_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) {
+ Prototype *internal(reinterpret_cast<Prototype *>(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 Prototype_deleteProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) {
+ Prototype *internal(reinterpret_cast<Prototype *>(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 Prototype_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) {
+ Prototype *internal(reinterpret_cast<Prototype *>(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;
+
+ CYPoolTry {
+ if ([self cy$hasProperty:name])
+ return true;
+ } CYPoolCatch(false)
+
+ const char *string(CYPoolCString(pool, name));
+ Class _class(object_getClass(self));
+
+ 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 {
- id self(CYCastNSObject(pool, context, object));
+ CYPool pool;
NSString *name(CYCastNSString(pool, property));
if (CYInternal *internal = CYInternal::Get(self))
}
if (SEL sel = sel_getUid(string))
- // XXX: possibly use a more "awesome" check?
- if (class_getInstanceMethod(_class, sel) != NULL)
+ if (CYImplements(self, _class, sel, true))
return CYSendMessage(pool, context, self, sel, 0, NULL, false, exception);
return NULL;
} CYCatch
}
-static JSValueRef Instance_getProperty_$cyi(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) {
+static bool Instance_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) {
Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
- return Internal::Make(context, internal->GetValue(), object);
-}
+ id self(internal->GetValue());
-static bool Instance_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) {
CYPool pool;
CYTry {
- id self(CYCastNSObject(pool, context, object));
NSString *name(CYCastNSString(pool, property));
NSString *data(CYCastNSObject(pool, context, value));
set[length + 4] = '\0';
if (SEL sel = sel_getUid(set))
- // XXX: possibly use a more "awesome" check?
- if (class_getInstanceMethod(_class, sel) != NULL) {
+ if (CYImplements(self, _class, sel, false)) {
JSValueRef arguments[1] = {value};
CYSendMessage(pool, context, self, sel, 1, arguments, false, exception);
}
}
static bool Instance_deleteProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) {
+ Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
+ id self(internal->GetValue());
+
CYTry {
CYPoolTry {
- id self(CYCastNSObject(NULL, context, object));
NSString *name(CYCastNSString(NULL, property));
return [self cy$deleteProperty:name];
} CYPoolCatch(NULL)
}
static void Instance_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) {
+ Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
+ id self(internal->GetValue());
+
CYPool pool;
- NSString *self(CYCastNSObject(pool, context, object));
Class _class(object_getClass(self));
{
static JSObjectRef Instance_callAsConstructor(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
CYTry {
- Instance *data(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
- JSObjectRef value(Instance::Make(context, [data->GetValue() alloc], Instance::Uninitialized));
+ Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
+ JSObjectRef value(Instance::Make(context, [internal->GetValue() alloc], Instance::Uninitialized));
return value;
} CYCatch
}
+static bool Internal_hasProperty(JSContextRef context, JSObjectRef object, JSStringRef property) {
+ Internal *internal(reinterpret_cast<Internal *>(JSObjectGetPrivate(object)));
+ CYPool pool;
+
+ id self(internal->GetValue());
+ const char *name(CYPoolCString(pool, property));
+
+ if (object_getInstanceVariable(self, name, NULL) != NULL)
+ return true;
+
+ return false;
+}
+
static JSValueRef Internal_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) {
Internal *internal(reinterpret_cast<Internal *>(JSObjectGetPrivate(object)));
CYPool pool;
} CYCatch
}
+static void Internal_getPropertyNames_(Class _class, JSPropertyNameAccumulatorRef names) {
+ if (Class super = class_getSuperclass(_class))
+ Internal_getPropertyNames_(super, names);
+
+ unsigned int size;
+ Ivar *data(class_copyIvarList(_class, &size));
+ for (size_t i(0); i != size; ++i)
+ JSPropertyNameAccumulatorAddName(names, CYJSString(ivar_getName(data[i])));
+ free(data);
+}
+
static void Internal_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) {
Internal *internal(reinterpret_cast<Internal *>(JSObjectGetPrivate(object)));
CYPool pool;
id self(internal->GetValue());
Class _class(object_getClass(self));
- for (Class super(_class); super != NULL; super = class_getSuperclass(super)) {
- unsigned int size;
- Ivar *data(class_copyIvarList(super, &size));
- for (size_t i(0); i != size; ++i)
- JSPropertyNameAccumulatorAddName(names, CYJSString(ivar_getName(data[i])));
- free(data);
- }
+ Internal_getPropertyNames_(_class, names);
}
static JSValueRef Internal_callAsFunction_$cya(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
if (typical->type_ == NULL)
return NULL;
- ssize_t index;
- if (!CYGetIndex(pool, property, index))
+ ssize_t offset;
+ if (!CYGetOffset(pool, property, offset))
return NULL;
- return Pointer_getIndex(context, object, index, exception);
+ return Pointer_getIndex(context, object, offset, exception);
}
static JSValueRef Pointer_getProperty_$cyi(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) {
if (typical->type_ == NULL)
return NULL;
- ssize_t index;
- if (!CYGetIndex(pool, property, index))
+ ssize_t offset;
+ if (!CYGetOffset(pool, property, offset))
return NULL;
- return Pointer_setIndex(context, object, 0, value, exception);
+ return Pointer_setIndex(context, object, offset, value, exception);
}
static bool Pointer_setProperty_$cyi(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) {
} CYCatch
}
-void Closure_(ffi_cif *cif, void *result, void **arguments, void *arg) {
- ffoData *data(reinterpret_cast<ffoData *>(arg));
-
- JSContextRef context(data->context_);
-
- size_t count(data->cif_.nargs);
- JSValueRef values[count];
-
- for (size_t index(0); index != count; ++index)
- values[index] = CYFromFFI(context, data->signature_.elements[1 + index].type, data->cif_.arg_types[index], arguments[index]);
-
- JSValueRef value(CYCallAsFunction(context, data->function_, NULL, count, values));
- CYPoolFFI(NULL, context, data->signature_.elements[0].type, data->cif_.rtype, result, value);
-}
-
-JSObjectRef CYMakeFunctor(JSContextRef context, JSObjectRef function, const char *type) {
- // XXX: in case of exceptions this will leak
- ffoData *data(new ffoData(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, &data->cif_, &Closure_, data));
- _assert(status == FFI_OK);
-
- _syscall(mprotect(closure, sizeof(*closure), PROT_READ | PROT_EXEC));
-
- data->value_ = closure;
-
- data->context_ = CYGetJSContext();
- data->function_ = function;
-
- return JSObjectMake(context, Functor_, data);
-}
-
static JSValueRef ObjectiveC_Classes_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) {
CYTry {
CYPool pool;
}
static JSValueRef Runtime_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) {
+ if (JSStringIsEqualToUTF8CString(property, "nil"))
+ return Instance::Make(context, nil);
+
CYTry {
CYPool pool;
NSString *name(CYCastNSString(pool, property));
@throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"too few arguments to objc_msgSend" userInfo:nil];
if (JSValueIsObjectOfClass(context, arguments[0], Instance_)) {
- Instance *data(reinterpret_cast<Instance *>(JSObjectGetPrivate((JSObjectRef) arguments[0])));
- self = data->GetValue();
- uninitialized = data->IsUninitialized();
+ Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate((JSObjectRef) arguments[0])));
+ self = internal->GetValue();
+ uninitialized = internal->IsUninitialized();
if (uninitialized)
- data->value_ = nil;
+ internal->value_ = nil;
} else {
self = CYCastNSObject(pool, context, arguments[0]);
uninitialized = false;
return $objc_msgSend(context, NULL, NULL, count + 2, setup, exception);
}
+static JSValueRef Message_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
+ CYPool pool;
+ Message_privateData *internal(reinterpret_cast<Message_privateData *>(JSObjectGetPrivate(object)));
+
+ // XXX: handle Instance::Uninitialized?
+ id self(CYCastNSObject(pool, context, _this));
+
+ void *setup[2];
+ setup[0] = &self;
+ setup[1] = &internal->sel_;
+
+ return CYCallFunction(pool, context, 2, setup, count, arguments, false, exception, &internal->signature_, &internal->cif_, internal->GetValue());
+}
+
static JSValueRef Functor_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
CYPool pool;
- Functor_privateData *data(reinterpret_cast<Functor_privateData *>(JSObjectGetPrivate(object)));
- return CYCallFunction(pool, context, 0, NULL, count, arguments, false, exception, &data->signature_, &data->cif_, reinterpret_cast<void (*)()>(data->value_));
+ Functor_privateData *internal(reinterpret_cast<Functor_privateData *>(JSObjectGetPrivate(object)));
+ return CYCallFunction(pool, context, 0, NULL, count, arguments, false, exception, &internal->signature_, &internal->cif_, internal->GetValue());
}
JSObjectRef Selector_new(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
} CYCatch
}
+JSObjectRef Instance_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 Instance constructor" userInfo:nil];
+ id self(count == 0 ? nil : CYCastPointer<id>(context, arguments[0]));
+ return Instance::Make(context, self);
+ } CYCatch
+}
+
JSObjectRef Functor_new(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
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 exception(NULL);
- if (JSValueIsInstanceOfConstructor(context, arguments[0], Function_, &exception)) {
- JSObjectRef function(CYCastJSObject(context, arguments[0]));
- return CYMakeFunctor(context, function, type);
- } else if (exception != NULL) {
- return NULL;
- } else {
- void (*function)()(CYCastPointer<void (*)()>(context, arguments[0]));
- return CYMakeFunctor(context, function, type);
- }
+ return CYMakeFunctor(context, arguments[0], type);
} CYCatch
}
return CYCastJSValue(context, reinterpret_cast<uintptr_t>(internal->value_));
}
-JSValueRef Selector_getProperty_prototype(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) {
- return Function_;
+static JSValueRef CYValue_callAsFunction_$cya(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
+ CYValue *internal(reinterpret_cast<CYValue *>(JSObjectGetPrivate(_this)));
+ Type_privateData *typical(internal->GetType());
+
+ sig::Type *type;
+ ffi_type *ffi;
+
+ if (typical == NULL) {
+ type = NULL;
+ ffi = NULL;
+ } else {
+ type = typical->type_;
+ ffi = typical->ffi_;
+ }
+
+ return CYMakePointer(context, &internal->value_, type, ffi, object);
}
static JSValueRef CYValue_callAsFunction_valueOf(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
+ CYValue *internal(reinterpret_cast<CYValue *>(JSObjectGetPrivate(_this)));
+
CYTry {
- CYValue *internal(reinterpret_cast<CYValue *>(JSObjectGetPrivate(_this)));
return CYCastJSValue(context, reinterpret_cast<uintptr_t>(internal->value_));
} CYCatch
}
}
static JSValueRef CYValue_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
+ CYValue *internal(reinterpret_cast<CYValue *>(JSObjectGetPrivate(_this)));
+ char string[32];
+ sprintf(string, "%p", internal->value_);
+
CYTry {
- CYValue *internal(reinterpret_cast<CYValue *>(JSObjectGetPrivate(_this)));
- char string[32];
- sprintf(string, "%p", internal->value_);
return CYCastJSValue(context, string);
} CYCatch
}
+static JSValueRef Instance_getProperty_constructor(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) {
+ Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
+ return Instance::Make(context, object_getClass(internal->GetValue()));
+}
+
+static JSValueRef Instance_getProperty_prototype(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) {
+ Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
+ id self(internal->GetValue());
+ // XXX: this is a lame object_isClass
+ if (class_getInstanceMethod(object_getClass(self), @selector(alloc)) == NULL)
+ return CYJSUndefined(context);
+ return Prototype::Make(context, self);
+}
+
static JSValueRef Instance_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
+ Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(_this)));
+
CYTry {
- Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(_this)));
CYPoolTry {
- return CYCastJSValue(context, CYJSString([internal->GetValue() cy$toCYON]));
+ return CYCastJSValue(context, CYJSString(CYPoolNSCYON(NULL, internal->GetValue())));
} CYPoolCatch(NULL)
} CYCatch
}
static JSValueRef Instance_callAsFunction_toJSON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
+ Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(_this)));
+
CYTry {
- Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(_this)));
CYPoolTry {
NSString *key(count == 0 ? nil : CYCastNSString(NULL, CYJSString(context, arguments[0])));
return CYCastJSValue(context, CYJSString([internal->GetValue() cy$toJSON:key]));
}
static JSValueRef Instance_callAsFunction_toString(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
+ Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(_this)));
+
CYTry {
- Instance *data(reinterpret_cast<Instance *>(JSObjectGetPrivate(_this)));
CYPoolTry {
- return CYCastJSValue(context, CYJSString([data->GetValue() description]));
+ return CYCastJSValue(context, CYJSString([internal->GetValue() description]));
} CYPoolCatch(NULL)
} CYCatch
}
static JSValueRef Selector_callAsFunction_toString(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
+ Selector_privateData *internal(reinterpret_cast<Selector_privateData *>(JSObjectGetPrivate(_this)));
+
CYTry {
- Selector_privateData *data(reinterpret_cast<Selector_privateData *>(JSObjectGetPrivate(_this)));
- return CYCastJSValue(context, sel_getName(data->GetValue()));
+ return CYCastJSValue(context, sel_getName(internal->GetValue()));
} CYCatch
}
}
static JSValueRef Selector_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
+ Selector_privateData *internal(reinterpret_cast<Selector_privateData *>(JSObjectGetPrivate(_this)));
+ const char *name(sel_getName(internal->GetValue()));
+
CYTry {
- Selector_privateData *internal(reinterpret_cast<Selector_privateData *>(JSObjectGetPrivate(_this)));
- const char *name(sel_getName(internal->GetValue()));
CYPoolTry {
return CYCastJSValue(context, CYJSString([NSString stringWithFormat:@"@selector(%s)", name]));
} CYPoolCatch(NULL)
Selector_privateData *internal(reinterpret_cast<Selector_privateData *>(JSObjectGetPrivate(_this)));
Class _class(CYCastNSObject(pool, context, arguments[0]));
SEL sel(internal->GetValue());
- if (Method method = class_getInstanceMethod(_class, sel))
- return CYCastJSValue(context, method_getTypeEncoding(method));
- else if (NSString *type = [[Bridge_ objectAtIndex:1] objectForKey:CYCastNSString(pool, sel_getName(sel))])
- return CYCastJSValue(context, CYJSString(type));
- else
- return CYJSNull(context);
+ Method method(class_getInstanceMethod(_class, sel));
+ const char *type(CYPoolTypeEncoding(pool, _class, sel, method));
+ return type == NULL ? CYJSNull(context) : CYCastJSValue(context, CYJSString(type));
} CYCatch
}
};
static JSStaticValue Pointer_staticValues[2] = {
- {"$cyi", &Pointer_getProperty_$cyi, &Pointer_setProperty_$cyi, kJSPropertyAttributeDontDelete},
+ {"$cyi", &Pointer_getProperty_$cyi, &Pointer_setProperty_$cyi, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
{NULL, NULL, NULL, 0}
};
{NULL, NULL, 0}
};
-/*static JSStaticValue Selector_staticValues[2] = {
- {"prototype", &Selector_getProperty_prototype, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete},
- {NULL, NULL, NULL, 0}
-};*/
-
-static JSStaticValue Instance_staticValues[3] = {
- {"value", &CYValue_getProperty_value, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete},
- {"$cyi", &Instance_getProperty_$cyi, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete},
+static JSStaticValue Instance_staticValues[4] = {
+ {"constructor", &Instance_getProperty_constructor, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
+ {"prototype", &Instance_getProperty_prototype, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
+ {"value", &CYValue_getProperty_value, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
{NULL, NULL, NULL, 0}
};
-static JSStaticFunction Instance_staticFunctions[4] = {
+static JSStaticFunction Instance_staticFunctions[5] = {
+ {"$cya", &CYValue_callAsFunction_$cya, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
{"toCYON", &Instance_callAsFunction_toCYON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
{"toJSON", &Instance_callAsFunction_toJSON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
{"toString", &Instance_callAsFunction_toString, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
state_(CYClear),
data_(NULL),
size_(0),
+ file_(NULL),
filename_(filename),
source_(NULL)
{
return true;
}
-static int Socket_;
apr_pool_t *Pool_;
struct CYExecute_ {
return NULL;
}
-static void * APR_THREAD_FUNC Cyrver(apr_thread_t *thread, void *data) {
- for (;;) {
- int socket(_syscall(accept(Socket_, NULL, NULL)));
- CYClient *client(new CYClient(socket));
- apr_threadattr_t *attr;
- _aprcall(apr_threadattr_create(&attr, Pool_));
- _aprcall(apr_thread_create(&client->thread_, attr, &OnClient, client, client->pool_));
- }
-
- return NULL;
-}
-
-void Unlink() {
- pid_t pid(getpid());
- char path[104];
- sprintf(path, "/tmp/.s.cy.%u", pid);
- unlink(path);
+extern "C" void CYHandleClient(apr_pool_t *pool, int socket) {
+ CYClient *client(new(pool) CYClient(socket));
+ apr_threadattr_t *attr;
+ _aprcall(apr_threadattr_create(&attr, client->pool_));
+ _aprcall(apr_thread_create(&client->thread_, attr, &OnClient, client, client->pool_));
}
MSInitialize { _pooled
_aprcall(apr_initialize());
_aprcall(apr_pool_create(&Pool_, NULL));
+ Type_privateData::Object = new(Pool_) Type_privateData(Pool_, "@");
+ Type_privateData::Selector = new(Pool_) Type_privateData(Pool_, ":");
+
Bridge_ = [[NSMutableArray arrayWithContentsOfFile:@"/usr/lib/libcycript.plist"] retain];
+ NSArray_ = objc_getClass("NSArray");
NSCFBoolean_ = objc_getClass("NSCFBoolean");
NSCFType_ = objc_getClass("NSCFType");
NSMessageBuilder_ = objc_getClass("NSMessageBuilder");
NSZombie_ = objc_getClass("_NSZombie_");
Object_ = objc_getClass("Object");
-
- Socket_ = _syscall(socket(PF_UNIX, SOCK_STREAM, 0));
-
- struct sockaddr_un address;
- memset(&address, 0, sizeof(address));
- address.sun_family = AF_UNIX;
-
- pid_t pid(getpid());
- sprintf(address.sun_path, "/tmp/.s.cy.%u", pid);
-
- try {
- _syscall(bind(Socket_, reinterpret_cast<sockaddr *>(&address), SUN_LEN(&address)));
- atexit(&Unlink);
- _syscall(listen(Socket_, 0));
-
- apr_threadattr_t *attr;
- _aprcall(apr_threadattr_create(&attr, Pool_));
-
- apr_thread_t *thread;
- _aprcall(apr_thread_create(&thread, attr, &Cyrver, NULL, Pool_));
- } catch (...) {
- NSLog(@"failed to setup Cyrver");
- }
}
JSGlobalContextRef CYGetJSContext() {
definition.className = "Functor";
definition.staticFunctions = Functor_staticFunctions;
definition.callAsFunction = &Functor_callAsFunction;
- definition.finalize = &CYData::Finalize;
+ definition.finalize = &Finalize;
Functor_ = JSClassCreate(&definition);
definition = kJSClassDefinitionEmpty;
definition.className = "Instance";
definition.staticValues = Instance_staticValues;
definition.staticFunctions = Instance_staticFunctions;
+ definition.hasProperty = &Instance_hasProperty;
definition.getProperty = &Instance_getProperty;
definition.setProperty = &Instance_setProperty;
definition.deleteProperty = &Instance_deleteProperty;
definition.getPropertyNames = &Instance_getPropertyNames;
definition.callAsConstructor = &Instance_callAsConstructor;
- definition.finalize = &CYData::Finalize;
+ definition.finalize = &Finalize;
Instance_ = JSClassCreate(&definition);
definition = kJSClassDefinitionEmpty;
definition.className = "Internal";
definition.staticFunctions = Internal_staticFunctions;
+ definition.hasProperty = &Internal_hasProperty;
definition.getProperty = &Internal_getProperty;
definition.setProperty = &Internal_setProperty;
definition.getPropertyNames = &Internal_getPropertyNames;
- definition.finalize = &CYData::Finalize;
+ definition.finalize = &Finalize;
Internal_ = JSClassCreate(&definition);
+ definition = kJSClassDefinitionEmpty;
+ definition.className = "Message";
+ definition.staticFunctions = Functor_staticFunctions;
+ definition.callAsFunction = &Message_callAsFunction;
+ definition.finalize = &Finalize;
+ Message_ = JSClassCreate(&definition);
+
definition = kJSClassDefinitionEmpty;
definition.className = "Pointer";
definition.staticValues = Pointer_staticValues;
definition.staticFunctions = Pointer_staticFunctions;
definition.getProperty = &Pointer_getProperty;
definition.setProperty = &Pointer_setProperty;
- definition.finalize = &CYData::Finalize;
+ definition.finalize = &Finalize;
Pointer_ = JSClassCreate(&definition);
+ definition = kJSClassDefinitionEmpty;
+ definition.className = "Prototype";
+ definition.hasProperty = &Prototype_hasProperty;
+ definition.getProperty = &Prototype_getProperty;
+ definition.setProperty = &Prototype_setProperty;
+#if !__OBJC2__
+ definition.deleteProperty = &Prototype_deleteProperty;
+#endif
+ definition.getPropertyNames = &Prototype_getPropertyNames;
+ definition.finalize = &Finalize;
+ Prototype_ = JSClassCreate(&definition);
+
definition = kJSClassDefinitionEmpty;
definition.className = "Selector";
definition.staticValues = CYValue_staticValues;
- //definition.staticValues = Selector_staticValues;
definition.staticFunctions = Selector_staticFunctions;
definition.callAsFunction = &Selector_callAsFunction;
- definition.finalize = &CYData::Finalize;
+ definition.finalize = &Finalize;
Selector_ = JSClassCreate(&definition);
definition = kJSClassDefinitionEmpty;
definition.getProperty = &Struct_getProperty;
definition.setProperty = &Struct_setProperty;
definition.getPropertyNames = &Struct_getPropertyNames;
- definition.finalize = &CYData::Finalize;
+ definition.finalize = &Finalize;
Struct_ = JSClassCreate(&definition);
definition = kJSClassDefinitionEmpty;
//definition.getProperty = &Type_getProperty;
definition.callAsFunction = &Type_callAsFunction;
definition.callAsConstructor = &Type_callAsConstructor;
- definition.finalize = &CYData::Finalize;
+ definition.finalize = &Finalize;
Type_ = JSClassCreate(&definition);
definition = kJSClassDefinitionEmpty;
definition.className = "ObjectiveC::Image::Classes";
definition.getProperty = &ObjectiveC_Image_Classes_getProperty;
definition.getPropertyNames = &ObjectiveC_Image_Classes_getPropertyNames;
- definition.finalize = &CYData::Finalize;
+ definition.finalize = &Finalize;
ObjectiveC_Image_Classes_ = JSClassCreate(&definition);
definition = kJSClassDefinitionEmpty;
CYSetProperty(context, ObjectiveC_, CYJSString("images"), JSObjectMake(context, ObjectiveC_Images_, NULL));
CYSetProperty(context, ObjectiveC_, CYJSString("protocols"), JSObjectMake(context, ObjectiveC_Protocols_, NULL));
- CYSetProperty(context, global, CYJSString("Functor"), JSObjectMakeConstructor(context, Functor_, &Functor_new));
- CYSetProperty(context, global, CYJSString("Instance"), JSObjectMakeConstructor(context, Instance_, NULL));
+ Array_ = CYCastJSObject(context, CYGetProperty(context, global, CYJSString("Array")));
+ Function_ = CYCastJSObject(context, CYGetProperty(context, global, CYJSString("Function")));
+
+ length_ = JSStringCreateWithUTF8CString("length");
+ message_ = JSStringCreateWithUTF8CString("message");
+ name_ = JSStringCreateWithUTF8CString("name");
+ prototype_ = JSStringCreateWithUTF8CString("prototype");
+ toCYON_ = JSStringCreateWithUTF8CString("toCYON");
+ toJSON_ = JSStringCreateWithUTF8CString("toJSON");
+
+ Array_prototype_ = CYCastJSObject(context, CYGetProperty(context, Array_, prototype_));
+ Array_pop_ = CYCastJSObject(context, CYGetProperty(context, Array_prototype_, CYJSString("pop")));
+ Array_push_ = CYCastJSObject(context, CYGetProperty(context, Array_prototype_, CYJSString("push")));
+ Array_splice_ = CYCastJSObject(context, CYGetProperty(context, Array_prototype_, CYJSString("splice")));
+
+ JSObjectRef Functor(JSObjectMakeConstructor(context, Functor_, &Functor_new));
+ JSObjectRef Message(JSObjectMakeConstructor(context, Message_, NULL));
+ JSObjectRef Selector(JSObjectMakeConstructor(context, Selector_, &Selector_new));
+
+ JSValueRef function(CYGetProperty(context, Function_, prototype_));
+ JSObjectSetPrototype(context, (JSObjectRef) CYGetProperty(context, Message, prototype_), function);
+ JSObjectSetPrototype(context, (JSObjectRef) CYGetProperty(context, Functor, prototype_), function);
+ JSObjectSetPrototype(context, (JSObjectRef) CYGetProperty(context, Selector, prototype_), function);
+
+ CYSetProperty(context, global, CYJSString("Functor"), Functor);
+ CYSetProperty(context, global, CYJSString("Instance"), JSObjectMakeConstructor(context, Instance_, &Instance_new));
CYSetProperty(context, global, CYJSString("Pointer"), JSObjectMakeConstructor(context, Pointer_, &Pointer_new));
- CYSetProperty(context, global, CYJSString("Selector"), JSObjectMakeConstructor(context, Selector_, &Selector_new));
+ CYSetProperty(context, global, CYJSString("Selector"), Selector);
CYSetProperty(context, global, CYJSString("Type"), JSObjectMakeConstructor(context, Type_, &Type_new));
MSHookFunction(&objc_registerClassPair, MSHake(objc_registerClassPair));
CYSetProperty(context, System_, CYJSString("print"), JSObjectMakeFunctionWithCallback(context, CYJSString("print"), &System_print));
Result_ = JSStringCreateWithUTF8CString("_");
-
- length_ = JSStringCreateWithUTF8CString("length");
- message_ = JSStringCreateWithUTF8CString("message");
- name_ = JSStringCreateWithUTF8CString("name");
- toCYON_ = JSStringCreateWithUTF8CString("toCYON");
- toJSON_ = JSStringCreateWithUTF8CString("toJSON");
-
- Array_ = CYCastJSObject(context, CYGetProperty(context, global, CYJSString("Array")));
- Function_ = CYCastJSObject(context, CYGetProperty(context, global, CYJSString("Function")));
}
return Context_;