} \
}
+void CYThrow(JSContextRef context, JSValueRef value);
+
+/* JavaScript Properties {{{ */
+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));
+ CYThrow(context, exception);
+ 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);
+ CYThrow(context, exception);
+}
+/* }}} */
+/* JavaScript Strings {{{ */
+JSStringRef CYCopyJSString(id value) {
+ // XXX: this definition scares me; is anyone using this?!
+ return value == NULL ? NULL : JSStringCreateWithCFString(reinterpret_cast<CFStringRef>([value description]));
+}
+
+JSStringRef CYCopyJSString(const char *value) {
+ return value == NULL ? NULL : JSStringCreateWithUTF8CString(value);
+}
+
+JSStringRef CYCopyJSString(JSStringRef value) {
+ return value == NULL ? NULL : JSStringRetain(value);
+}
+
+JSStringRef CYCopyJSString(JSContextRef context, JSValueRef value) {
+ if (JSValueIsNull(context, value))
+ return NULL;
+ JSValueRef exception(NULL);
+ JSStringRef string(JSValueToStringCopy(context, value, &exception));
+ CYThrow(context, exception);
+ return string;
+}
+
+class CYJSString {
+ private:
+ JSStringRef string_;
+
+ void Clear_() {
+ if (string_ != NULL)
+ JSStringRelease(string_);
+ }
+
+ public:
+ CYJSString(const CYJSString &rhs) :
+ string_(CYCopyJSString(rhs.string_))
+ {
+ }
+
+ template <typename Arg0_>
+ CYJSString(Arg0_ arg0) :
+ string_(CYCopyJSString(arg0))
+ {
+ }
+
+ template <typename Arg0_, typename Arg1_>
+ CYJSString(Arg0_ arg0, Arg1_ arg1) :
+ string_(CYCopyJSString(arg0, arg1))
+ {
+ }
+
+ CYJSString &operator =(const CYJSString &rhs) {
+ Clear_();
+ string_ = CYCopyJSString(rhs.string_);
+ return *this;
+ }
+
+ ~CYJSString() {
+ Clear_();
+ }
+
+ void Clear() {
+ Clear_();
+ string_ = NULL;
+ }
+
+ operator JSStringRef() const {
+ return string_;
+ }
+};
+
+CFStringRef CYCopyCFString(JSStringRef value) {
+ return JSStringCopyCFString(kCFAllocatorDefault, value);
+}
+
+CFStringRef CYCopyCFString(JSContextRef context, JSValueRef value) {
+ return CYCopyCFString(CYJSString(context, value));
+}
+
+/* }}} */
+
static JSGlobalContextRef Context_;
static JSObjectRef System_;
static JSObjectRef ObjectiveC_;
static JSClassRef Instance_;
static JSClassRef Internal_;
static JSClassRef Message_;
+static JSClassRef Messages_;
+static JSClassRef NSArrayPrototype_;
static JSClassRef Pointer_;
-static JSClassRef Prototype_;
static JSClassRef Runtime_;
static JSClassRef Selector_;
static JSClassRef Struct_;
static JSObjectRef Array_;
static JSObjectRef Function_;
+static JSObjectRef String_;
static JSStringRef Result_;
static JSStringRef toCYON_;
static JSStringRef toJSON_;
+static JSObjectRef Instance_prototype_;
+static JSObjectRef Object_prototype_;
+
+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 NSDictionary_;
static Class NSMessageBuilder_;
static Class NSZombie_;
static Class Object_;
virtual Type_privateData *GetType() const;
};
+// XXX: trick this out with associated objects!
+JSValueRef CYGetClassPrototype(JSContextRef context, id self) {
+ if (self == nil)
+ return Instance_prototype_;
+
+ // XXX: I need to think through multi-context
+ typedef std::map<Class, JSValueRef> CacheMap;
+ static CacheMap cache_;
+
+ JSValueRef &value(cache_[self]);
+ if (value != NULL)
+ return value;
+
+ JSClassRef _class(NULL);
+ JSValueRef prototype;
+
+ if (self == NSArray_)
+ prototype = Array_prototype_;
+ else if (self == NSDictionary_)
+ prototype = Object_prototype_;
+ else
+ prototype = CYGetClassPrototype(context, class_getSuperclass(self));
+
+ JSObjectRef object(JSObjectMake(context, _class, NULL));
+ JSObjectSetPrototype(context, object, prototype);
+
+ JSValueProtect(context, object);
+ value = object;
+ return object;
+}
+
struct Instance :
CYValue
{
}
static JSObjectRef Make(JSContextRef context, id object, Flags flags = None) {
- return JSObjectMake(context, Instance_, new Instance(object, flags));
+ JSObjectRef value(JSObjectMake(context, Instance_, new Instance(object, flags)));
+ JSObjectSetPrototype(context, value, CYGetClassPrototype(context, object == nil ? nil : object_getClass(object)));
+ return value;
}
id GetValue() const {
virtual Type_privateData *GetType() const;
};
-struct Prototype :
+struct Messages :
CYValue
{
- Prototype(Class value) :
+ Messages(Class value) :
CYValue(value)
{
}
- static JSObjectRef Make(JSContextRef context, Class _class) {
- return JSObjectMake(context, Prototype_, new Prototype(_class));
+ static JSObjectRef Make(JSContextRef context, Class _class, bool array = false) {
+ JSObjectRef value(JSObjectMake(context, Messages_, new Messages(_class)));
+ if (_class == NSArray_)
+ array = true;
+ if (Class super = class_getSuperclass(_class))
+ JSObjectSetPrototype(context, value, Messages::Make(context, super, array));
+ /*else if (array)
+ JSObjectSetPrototype(context, value, Array_prototype_);*/
+ return value;
}
Class GetValue() const {
}
};
-struct Internal :
+struct CYOwned :
CYValue
{
+ private:
+ JSContextRef context_;
JSObjectRef owner_;
- Internal(id value, JSObjectRef owner) :
+ public:
+ CYOwned(void *value, JSContextRef context, JSObjectRef owner) :
CYValue(value),
+ context_(context),
owner_(owner)
+ {
+ JSValueProtect(context_, owner_);
+ }
+
+ virtual ~CYOwned() {
+ JSValueUnprotect(context_, owner_);
+ }
+
+ JSObjectRef GetOwner() const {
+ return owner_;
+ }
+};
+
+struct Internal :
+ CYOwned
+{
+ Internal(id value, JSContextRef context, JSObjectRef owner) :
+ CYOwned(value, context, owner)
{
}
static JSObjectRef Make(JSContextRef context, id object, JSObjectRef owner) {
- return JSObjectMake(context, Internal_, new Internal(object, owner));
+ return JSObjectMake(context, Internal_, new Internal(object, context, owner));
}
id GetValue() const {
}
struct Pointer :
- CYValue
+ CYOwned
{
- JSObjectRef owner_;
Type_privateData *type_;
- Pointer(void *value, sig::Type *type, JSObjectRef owner) :
- CYValue(value),
- owner_(owner),
+ Pointer(void *value, JSContextRef context, JSObjectRef owner, sig::Type *type) :
+ CYOwned(value, context, owner),
type_(new(pool_) Type_privateData(type))
{
}
};
struct Struct_privateData :
- CYValue
+ CYOwned
{
- JSObjectRef owner_;
Type_privateData *type_;
- Struct_privateData(JSObjectRef owner) :
- owner_(owner)
+ Struct_privateData(JSContextRef context, JSObjectRef owner) :
+ CYOwned(NULL, context, owner)
{
}
};
static TypeMap Types_;
JSObjectRef CYMakeStruct(JSContextRef context, void *data, sig::Type *type, ffi_type *ffi, JSObjectRef owner) {
- Struct_privateData *internal(new Struct_privateData(owner));
+ Struct_privateData *internal(new Struct_privateData(context, owner));
apr_pool_t *pool(internal->pool_);
Type_privateData *typical(new(pool) Type_privateData(type, ffi));
internal->type_ = typical;
JSContextRef context_;
JSObjectRef function_;
- Closure_privateData(const char *type) :
- Functor_privateData(type, NULL)
+ Closure_privateData(JSContextRef context, JSObjectRef function, const char *type) :
+ Functor_privateData(type, NULL),
+ context_(context),
+ function_(function)
{
+ JSValueProtect(context_, function_);
+ }
+
+ virtual ~Closure_privateData() {
+ JSValueUnprotect(context_, function_);
}
};
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);
};
-@implementation NSProxy (Cycript)
-
-- (NSObject *) cy$toJSON:(NSString *)key {
- return [self description];
-}
-
-- (NSString *) cy$toCYON {
- return [[self cy$toJSON:@""] cy$toCYON];
-}
-
-@end
-
-@implementation NSObject (Cycript)
-
-- (JSValueRef) cy$JSValueInContext:(JSContextRef)context {
- return CYMakeInstance(context, self, false);
-}
-
-- (JSType) cy$JSType {
- return kJSTypeObject;
-}
-
-- (NSObject *) cy$toJSON:(NSString *)key {
- return [self description];
-}
-
-- (NSString *) cy$toCYON {
- return [[self cy$toJSON:@""] cy$toCYON];
-}
-
-- (NSString *) cy$toKey {
- return [self cy$toCYON];
-}
-
-- (bool) cy$hasProperty:(NSString *)name {
- return false;
-}
-
-- (NSObject *) cy$getProperty:(NSString *)name {
- return nil;
-}
-
-- (bool) cy$setProperty:(NSString *)name to:(NSObject *)value {
- return false;
-}
-
-- (bool) cy$deleteProperty:(NSString *)name {
- return false;
-}
-
-@end
-
NSString *NSCFType$cy$toJSON(id self, SEL sel, NSString *key) {
return [(NSString *) CFCopyDescription((CFTypeRef) self) autorelease];
}
-@implementation WebUndefined (Cycript)
-
-- (JSType) cy$JSType {
- return kJSTypeUndefined;
-}
-
-- (NSObject *) cy$toJSON:(NSString *)key {
- return self;
-}
-
-- (NSString *) cy$toCYON {
- return @"undefined";
-}
-
-- (JSValueRef) cy$JSValueInContext:(JSContextRef)context {
- return CYJSUndefined(context);
-}
-
-@end
-
-@implementation NSNull (Cycript)
-
-- (JSType) cy$JSType {
- return kJSTypeNull;
-}
-
-- (NSObject *) cy$toJSON:(NSString *)key {
- return self;
-}
-
-- (NSString *) cy$toCYON {
- return @"null";
-}
-
-@end
-
+/* Bridge: NSArray {{{ */
@implementation NSArray (Cycript)
- (NSString *) cy$toCYON {
if ([name isEqualToString:@"length"])
return true;
- 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$hasProperty:name];
else
return true;
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];
}
@end
-
-@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]))
- return [super cy$setProperty:name to:value];
- else {
- [self replaceObjectAtIndex:index withObject:(value ?: [NSNull null])];
- return true;
- }
-}
-
-- (bool) cy$deleteProperty:(NSString *)name {
- ssize_t index;
- if (!CYGetIndex(NULL, name, index) || index < 0 || index >= static_cast<ssize_t>([self count]))
- return [super cy$deleteProperty:name];
- else {
- [self removeObjectAtIndex:index];
- return true;
- }
-}
-
-@end
-
+/* }}} */
+/* Bridge: NSDictionary {{{ */
@implementation NSDictionary (Cycript)
- (NSString *) cy$toCYON {
}
@end
+/* }}} */
+/* Bridge: NSMutableArray {{{ */
+@implementation NSMutableArray (Cycript)
+- (bool) cy$setProperty:(NSString *)name to:(NSObject *)value {
+ 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 {
+ 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 {
+ size_t index(CYGetIndex(NULL, name));
+ if (index == _not(size_t) || index >= [self count])
+ return [super cy$deleteProperty:name];
+ [self replaceObjectAtIndex:index withObject:[WebUndefined undefined]];
+ return true;
+}
+
+@end
+/* }}} */
+/* Bridge: NSMutableDictionary {{{ */
@implementation NSMutableDictionary (Cycript)
- (bool) cy$setProperty:(NSString *)name to:(NSObject *)value {
- [self setObject:(value ?: [NSNull null]) forKey:name];
- return true;
+ [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
+/* }}} */
+/* Bridge: NSNumber {{{ */
+@implementation NSNumber (Cycript)
+
+- (JSType) cy$JSType {
+ // XXX: this just seems stupid
+ return [self class] == NSCFBoolean_ ? kJSTypeBoolean : kJSTypeNumber;
+}
+
+- (NSObject *) cy$toJSON:(NSString *)key {
+ return self;
+}
+
+- (NSString *) cy$toCYON {
+ return [self cy$JSType] != kJSTypeBoolean ? [self stringValue] : [self boolValue] ? @"true" : @"false";
+}
+
+- (JSValueRef) cy$JSValueInContext:(JSContextRef)context {
+ return [self cy$JSType] != kJSTypeBoolean ? CYCastJSValue(context, [self doubleValue]) : CYCastJSValue(context, [self boolValue]);
+}
+
+@end
+/* }}} */
+/* Bridge: NSNull {{{ */
+@implementation NSNull (Cycript)
+
+- (JSType) cy$JSType {
+ return kJSTypeNull;
+}
+
+- (NSObject *) cy$toJSON:(NSString *)key {
+ return self;
+}
+
+- (NSString *) cy$toCYON {
+ return @"null";
+}
+
+@end
+/* }}} */
+/* Bridge: NSObject {{{ */
+@implementation NSObject (Cycript)
+
+- (JSValueRef) cy$JSValueInContext:(JSContextRef)context {
+ return CYMakeInstance(context, self, false);
+}
+
+- (JSType) cy$JSType {
+ return kJSTypeObject;
+}
+
+- (NSObject *) cy$toJSON:(NSString *)key {
+ return [self description];
+}
+
+- (NSString *) cy$toCYON {
+ return [[self cy$toJSON:@""] cy$toCYON];
+}
+
+- (NSString *) cy$toKey {
+ return [self cy$toCYON];
+}
+
+- (bool) cy$hasProperty:(NSString *)name {
+ return false;
+}
+
+- (NSObject *) cy$getProperty:(NSString *)name {
+ return nil;
+}
+
+- (bool) cy$setProperty:(NSString *)name to:(NSObject *)value {
+ return false;
}
- (bool) cy$deleteProperty:(NSString *)name {
- if ([self objectForKey:name] == nil)
- return false;
- else {
- [self removeObjectForKey:name];
- return true;
- }
+ return false;
}
@end
-
-@implementation NSNumber (Cycript)
-
-- (JSType) cy$JSType {
- // XXX: this just seems stupid
- return [self class] == NSCFBoolean_ ? kJSTypeBoolean : kJSTypeNumber;
-}
+/* }}} */
+/* Bridge: NSProxy {{{ */
+@implementation NSProxy (Cycript)
- (NSObject *) cy$toJSON:(NSString *)key {
- return self;
+ return [self description];
}
- (NSString *) cy$toCYON {
- return [self cy$JSType] != kJSTypeBoolean ? [self stringValue] : [self boolValue] ? @"true" : @"false";
-}
-
-- (JSValueRef) cy$JSValueInContext:(JSContextRef)context {
- return [self cy$JSType] != kJSTypeBoolean ? CYCastJSValue(context, [self doubleValue]) : CYCastJSValue(context, [self boolValue]);
+ return [[self cy$toJSON:@""] cy$toCYON];
}
@end
-
+/* }}} */
+/* Bridge: NSString {{{ */
@implementation NSString (Cycript)
- (JSType) cy$JSType {
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
+/* }}} */
+/* Bridge: WebUndefined {{{ */
+@implementation WebUndefined (Cycript)
+
+- (JSType) cy$JSType {
+ return kJSTypeUndefined;
+}
+
+- (NSObject *) cy$toJSON:(NSString *)key {
+ return self;
+}
+
+- (NSString *) cy$toCYON {
+ return @"undefined";
+}
+
+- (JSValueRef) cy$JSValueInContext:(JSContextRef)context {
+ return CYJSUndefined(context);
+}
+
+@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
return NULL; \
}
-void CYThrow(JSContextRef context, JSValueRef value);
-
apr_status_t CYPoolRelease_(void *data) {
id object(reinterpret_cast<id>(data));
[object release];
if (!JSValueIsObjectOfClass(context, object, Instance_))
return CYCastNSObject_(pool, context, object);
else {
- Instance *data(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
- return data->GetValue();
- }
-}
-
-JSStringRef CYCopyJSString(id value) {
- return value == NULL ? NULL : JSStringCreateWithCFString(reinterpret_cast<CFStringRef>([value description]));
-}
-
-JSStringRef CYCopyJSString(const char *value) {
- return value == NULL ? NULL : JSStringCreateWithUTF8CString(value);
-}
-
-JSStringRef CYCopyJSString(JSStringRef value) {
- return value == NULL ? NULL : JSStringRetain(value);
-}
-
-JSStringRef CYCopyJSString(JSContextRef context, JSValueRef value) {
- if (JSValueIsNull(context, value))
- return NULL;
- JSValueRef exception(NULL);
- JSStringRef string(JSValueToStringCopy(context, value, &exception));
- CYThrow(context, exception);
- return string;
-}
-
-class CYJSString {
- private:
- JSStringRef string_;
-
- void Clear_() {
- if (string_ != NULL)
- JSStringRelease(string_);
- }
-
- public:
- CYJSString(const CYJSString &rhs) :
- string_(CYCopyJSString(rhs.string_))
- {
- }
-
- template <typename Arg0_>
- CYJSString(Arg0_ arg0) :
- string_(CYCopyJSString(arg0))
- {
- }
-
- template <typename Arg0_, typename Arg1_>
- CYJSString(Arg0_ arg0, Arg1_ arg1) :
- string_(CYCopyJSString(arg0, arg1))
- {
- }
-
- CYJSString &operator =(const CYJSString &rhs) {
- Clear_();
- string_ = CYCopyJSString(rhs.string_);
- return *this;
- }
-
- ~CYJSString() {
- Clear_();
- }
-
- void Clear() {
- Clear_();
- string_ = NULL;
- }
-
- operator JSStringRef() const {
- return string_;
+ Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
+ return internal->GetValue();
}
-};
-
-CFStringRef CYCopyCFString(JSStringRef value) {
- return JSStringCopyCFString(kCFAllocatorDefault, value);
-}
-
-CFStringRef CYCopyCFString(JSContextRef context, JSValueRef value) {
- return CYCopyCFString(CYJSString(context, value));
}
double CYCastDouble(const char *value, size_t size) {
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));
- CYThrow(context, exception);
- return value;
-}
-
-void CYSetProperty(JSContextRef context, JSObjectRef object, JSStringRef name, JSValueRef value) {
- JSValueRef exception(NULL);
- JSObjectSetProperty(context, object, name, value, kJSPropertyAttributeNone, &exception);
- CYThrow(context, exception);
-}
-
void CYThrow(JSContextRef context, id error, JSValueRef *exception) {
if (exception == NULL)
throw error;
if ((self = [super init]) != nil) {
object_ = object;
context_ = context;
+ JSValueProtect(context_, object_);
} return self;
}
+- (void) dealloc {
+ JSValueUnprotect(context_, object_);
+ [super dealloc];
+}
+
- (NSObject *) cy$toJSON:(NSString *)key {
JSValueRef toJSON(CYGetProperty(context_, object_, toJSON_));
if (!CYIsCallable(context_, toJSON))
- (NSString *) cy$toCYON {
JSValueRef toCYON(CYGetProperty(context_, object_, toCYON_));
- if (!CYIsCallable(context_, toCYON))
+ if (!CYIsCallable(context_, toCYON)) super:
return [super cy$toCYON];
- else {
- JSValueRef value(CYCallAsFunction(context_, (JSObjectRef) toCYON, object_, 0, NULL));
+ else if (JSValueRef value = CYCallAsFunction(context_, (JSObjectRef) toCYON, object_, 0, NULL))
return CYCastNSString(NULL, CYJSString(context_, value));
- }
+ else goto super;
}
- (NSUInteger) count {
}
- (id) objectForKey:(id)key {
- return CYCastNSObject(NULL, context_, CYGetProperty(context_, object_, CYJSString(key))) ?: [NSNull null];
+ JSValueRef value(CYGetProperty(context_, object_, CYJSString(key)));
+ if (JSValueIsUndefined(context_, value))
+ return nil;
+ return CYCastNSObject(NULL, context_, value) ?: [NSNull null];
}
- (NSEnumerator *) keyEnumerator {
if ((self = [super init]) != nil) {
object_ = object;
context_ = context;
+ JSValueProtect(context_, object_);
} return self;
}
+- (void) dealloc {
+ JSValueUnprotect(context_, object_);
+ [super dealloc];
+}
+
- (NSUInteger) count {
return CYCastDouble(context_, CYGetProperty(context_, object_, length_));
}
- (id) objectAtIndex:(NSUInteger)index {
+ size_t bounds([self count]);
+ if (index >= bounds)
+ @throw [NSException exceptionWithName:NSRangeException reason:[NSString stringWithFormat:@"*** -[CYJSArray objectAtIndex:]: index (%zu) beyond bounds (%zu)", index, bounds] userInfo:nil];
JSValueRef exception(NULL);
JSValueRef value(JSObjectGetPropertyAtIndex(context_, object_, index, &exception));
CYThrow(context_, 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 {
+ size_t bounds([self count] + 1);
+ if (index >= bounds)
+ @throw [NSException exceptionWithName:NSRangeException reason:[NSString stringWithFormat:@"*** -[CYJSArray insertObject:atIndex:]: index (%zu) beyond bounds (%zu)", index, bounds] userInfo:nil];
+ 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 {
+ size_t bounds([self count]);
+ if (index >= bounds)
+ @throw [NSException exceptionWithName:NSRangeException reason:[NSString stringWithFormat:@"*** -[CYJSArray removeObjectAtIndex:]: index (%zu) beyond bounds (%zu)", index, bounds] userInfo:nil];
+ 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 {
+ size_t bounds([self count]);
+ if (index >= bounds)
+ @throw [NSException exceptionWithName:NSRangeException reason:[NSString stringWithFormat:@"*** -[CYJSArray replaceObjectAtIndex:withObject:]: index (%zu) beyond bounds (%zu)", index, bounds] userInfo:nil];
+ CYSetProperty(context_, object_, index, CYCastJSValue(context_, object));
+}
+
@end
NSString *CYCopyNSCYON(id value) {
};
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, context, owner, type));
+ 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);
}
}
void FunctionClosure_(ffi_cif *cif, void *result, void **arguments, void *arg) {
- Closure_privateData *data(reinterpret_cast<Closure_privateData *>(arg));
+ Closure_privateData *internal(reinterpret_cast<Closure_privateData *>(arg));
- JSContextRef context(data->context_);
+ JSContextRef context(internal->context_);
- size_t count(data->cif_.nargs);
+ size_t count(internal->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]);
+ values[index] = CYFromFFI(context, internal->signature_.elements[1 + index].type, internal->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);
+ 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 *data(reinterpret_cast<Closure_privateData *>(arg));
+ Closure_privateData *internal(reinterpret_cast<Closure_privateData *>(arg));
- JSContextRef context(data->context_);
+ JSContextRef context(internal->context_);
- size_t count(data->cif_.nargs);
+ size_t count(internal->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]);
+ 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, data->function_, _this, count - 2, values + 2));
- CYPoolFFI(NULL, context, data->signature_.elements[0].type, data->cif_.rtype, result, value);
+ 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));
+ Closure_privateData *internal(new Closure_privateData(CYGetJSContext(), function, type));
ffi_closure *closure((ffi_closure *) _syscall(mmap(
NULL, sizeof(ffi_closure),
internal->value_ = closure;
- internal->context_ = CYGetJSContext();
- internal->function_ = function;
-
return internal;
}
return reinterpret_cast<IMP>(internal->GetValue());
}
-static bool Prototype_hasProperty(JSContextRef context, JSObjectRef object, JSStringRef property) {
- Prototype *internal(reinterpret_cast<Prototype *>(JSObjectGetPrivate(object)));
+static bool Messages_hasProperty(JSContextRef context, JSObjectRef object, JSStringRef property) {
+ Messages *internal(reinterpret_cast<Messages *>(JSObjectGetPrivate(object)));
Class _class(internal->GetValue());
CYPool pool;
return false;
}
-static JSValueRef Prototype_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) {
- Prototype *internal(reinterpret_cast<Prototype *>(JSObjectGetPrivate(object)));
+static JSValueRef Messages_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) {
+ Messages *internal(reinterpret_cast<Messages *>(JSObjectGetPrivate(object)));
Class _class(internal->GetValue());
CYPool pool;
return NULL;
}
-static bool Prototype_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) {
- Prototype *internal(reinterpret_cast<Prototype *>(JSObjectGetPrivate(object)));
+static bool Messages_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) {
+ Messages *internal(reinterpret_cast<Messages *>(JSObjectGetPrivate(object)));
Class _class(internal->GetValue());
CYPool pool;
}
#if !__OBJC2__
-static bool Prototype_deleteProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) {
- Prototype *internal(reinterpret_cast<Prototype *>(JSObjectGetPrivate(object)));
+static bool Messages_deleteProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) {
+ Messages *internal(reinterpret_cast<Messages *>(JSObjectGetPrivate(object)));
Class _class(internal->GetValue());
CYPool pool;
}
#endif
-static void Prototype_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) {
- Prototype *internal(reinterpret_cast<Prototype *>(JSObjectGetPrivate(object)));
+static void Messages_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) {
+ Messages *internal(reinterpret_cast<Messages *>(JSObjectGetPrivate(object)));
Class _class(internal->GetValue());
unsigned int size;
if (internal->HasProperty(context, property))
return true;
+ Class _class(object_getClass(self));
+
CYPoolTry {
- if ([self cy$hasProperty:name])
- return true;
+ // XXX: this is an evil hack to deal with NSProxy; fix elsewhere
+ if (CYImplements(self, _class, @selector(cy$hasProperty:), false))
+ 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;
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
}
+bool CYIsClass(id self) {
+ // XXX: this is a lame object_isClass
+ return class_getInstanceMethod(object_getClass(self), @selector(alloc)) != NULL;
+}
+
+static bool Instance_hasInstance(JSContextRef context, JSObjectRef constructor, JSValueRef instance, JSValueRef *exception) {
+ Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate((JSObjectRef) constructor)));
+ Class _class(internal->GetValue());
+ if (!CYIsClass(_class))
+ return false;
+
+ if (JSValueIsObjectOfClass(context, instance, Instance_)) {
+ Instance *linternal(reinterpret_cast<Instance *>(JSObjectGetPrivate((JSObjectRef) instance)));
+ // XXX: this isn't always safe
+ CYTry {
+ return [linternal->GetValue() isKindOfClass:_class];
+ } CYCatch
+ }
+
+ return false;
+}
+
static bool Internal_hasProperty(JSContextRef context, JSObjectRef object, JSStringRef property) {
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) {
Internal *internal(reinterpret_cast<Internal *>(JSObjectGetPrivate(object)));
- return internal->owner_;
+ return internal->GetOwner();
}
bool Index_(apr_pool_t *pool, Struct_privateData *internal, JSStringRef property, ssize_t &index, uint8_t *&base) {
uint8_t *base(reinterpret_cast<uint8_t *>(internal->value_));
base += ffi->size * index;
- JSObjectRef owner(internal->owner_ ?: object);
+ JSObjectRef owner(internal->GetOwner() ?: object);
CYTry {
return CYFromFFI(context, typical->type_, ffi, base, false, owner);
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) {
if (!Index_(pool, internal, property, index, base))
return NULL;
- JSObjectRef owner(internal->owner_ ?: object);
+ JSObjectRef owner(internal->GetOwner() ?: object);
CYTry {
return CYFromFFI(context, typical->type_->data.signature.elements[index].type, typical->GetFFI()->elements[index], base, false, owner);
return CYCallFunction(pool, context, 2, setup, count, arguments, initialize, exception, &signature, &cif, function);
}
+static size_t Nonce_(0);
+
+static JSValueRef $cyq(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
+ char name[16];
+ sprintf(name, "%s%zu", CYCastCString(context, arguments[0]), Nonce_++);
+ return CYCastJSValue(context, name);
+}
+
static JSValueRef $objc_msgSend(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
CYPool pool;
@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 CYSendMessage(pool, context, self, _cmd, count - 2, arguments + 2, uninitialized, exception);
}
+/* Hook: objc_registerClassPair {{{ */
+// XXX: replace this with associated objects
+
MSHook(void, CYDealloc, id self, SEL sel) {
CYInternal *internal;
object_getInstanceVariable(self, "cy$internal_", reinterpret_cast<void **>(&internal));
return CYJSUndefined(context);
} CYCatch
}
+/* }}} */
static JSValueRef Selector_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
JSValueRef setup[count + 2];
return Instance::Make(context, object_getClass(internal->GetValue()));
}
-static JSValueRef Instance_getProperty_prototype(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) {
+static JSValueRef Instance_getProperty_protocol(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) {
+ Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
+ id self(internal->GetValue());
+ if (!CYIsClass(self))
+ return CYJSUndefined(context);
+ CYTry {
+ return CYGetClassPrototype(context, self);
+ } CYCatch
+}
+
+static JSValueRef Instance_getProperty_messages(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);
+ return Messages::Make(context, self);
}
static JSValueRef Instance_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
+ if (!JSValueIsObjectOfClass(context, _this, Instance_))
+ return NULL;
+
Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(_this)));
CYTry {
}
static JSValueRef Instance_callAsFunction_toJSON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
+ if (!JSValueIsObjectOfClass(context, _this, Instance_))
+ return NULL;
+
Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(_this)));
CYTry {
CYPoolTry {
NSString *key(count == 0 ? nil : CYCastNSString(NULL, CYJSString(context, arguments[0])));
+ // XXX: check for support of cy$toJSON?
return CYCastJSValue(context, CYJSString([internal->GetValue() cy$toJSON:key]));
} CYPoolCatch(NULL)
} CYCatch
}
static JSValueRef Instance_callAsFunction_toString(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
- Instance *data(reinterpret_cast<Instance *>(JSObjectGetPrivate(_this)));
+ if (!JSValueIsObjectOfClass(context, _this, Instance_))
+ return NULL;
+
+ Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(_this)));
CYTry {
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 *data(reinterpret_cast<Selector_privateData *>(JSObjectGetPrivate(_this)));
+ Selector_privateData *internal(reinterpret_cast<Selector_privateData *>(JSObjectGetPrivate(_this)));
CYTry {
- return CYCastJSValue(context, sel_getName(data->GetValue()));
+ return CYCastJSValue(context, sel_getName(internal->GetValue()));
} CYCatch
}
{NULL, NULL, 0}
};
-static JSStaticValue Instance_staticValues[4] = {
- {"constructor", &Instance_getProperty_constructor, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete},
- {"prototype", &Instance_getProperty_prototype, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete},
- {"value", &CYValue_getProperty_value, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete},
+static JSStaticValue Instance_staticValues[5] = {
+ {"constructor", &Instance_getProperty_constructor, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
+ {"messages", &Instance_getProperty_messages, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
+ {"prototype", &Instance_getProperty_protocol, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
+ {"value", &CYValue_getProperty_value, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
{NULL, NULL, NULL, 0}
};
if (JSValueIsUndefined(context, result))
return NULL;
- const char *json(CYPoolCCYON(pool, context, result, &exception));
+ const char *json;
+
+ try {
+ json = CYPoolCCYON(pool, context, result, &exception);
+ } catch (const char *error) {
+ return error;
+ }
+
if (exception != NULL)
goto error;
Bridge_ = [[NSMutableArray arrayWithContentsOfFile:@"/usr/lib/libcycript.plist"] retain];
+ NSArray_ = objc_getClass("NSArray");
NSCFBoolean_ = objc_getClass("NSCFBoolean");
NSCFType_ = objc_getClass("NSCFType");
+ NSDictionary_ = objc_getClass("NSDictonary");
NSMessageBuilder_ = objc_getClass("NSMessageBuilder");
NSZombie_ = objc_getClass("_NSZombie_");
Object_ = objc_getClass("Object");
definition.deleteProperty = &Instance_deleteProperty;
definition.getPropertyNames = &Instance_getPropertyNames;
definition.callAsConstructor = &Instance_callAsConstructor;
+ definition.hasInstance = &Instance_hasInstance;
definition.finalize = &Finalize;
Instance_ = JSClassCreate(&definition);
definition.finalize = &Finalize;
Message_ = JSClassCreate(&definition);
+ definition = kJSClassDefinitionEmpty;
+ definition.className = "Messages";
+ definition.hasProperty = &Messages_hasProperty;
+ definition.getProperty = &Messages_getProperty;
+ definition.setProperty = &Messages_setProperty;
+#if !__OBJC2__
+ definition.deleteProperty = &Messages_deleteProperty;
+#endif
+ definition.getPropertyNames = &Messages_getPropertyNames;
+ definition.finalize = &Finalize;
+ Messages_ = JSClassCreate(&definition);
+
+ definition = kJSClassDefinitionEmpty;
+ definition.className = "NSArrayPrototype";
+ //definition.hasProperty = &NSArrayPrototype_hasProperty;
+ //definition.getProperty = &NSArrayPrototype_getProperty;
+ //definition.setProperty = &NSArrayPrototype_setProperty;
+ //definition.deleteProperty = &NSArrayPrototype_deleteProperty;
+ //definition.getPropertyNames = &NSArrayPrototype_getPropertyNames;
+ NSArrayPrototype_ = JSClassCreate(&definition);
+
definition = kJSClassDefinitionEmpty;
definition.className = "Pointer";
definition.staticValues = Pointer_staticValues;
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.className = "ObjectiveC::Image::Classes";
definition.getProperty = &ObjectiveC_Image_Classes_getProperty;
definition.getPropertyNames = &ObjectiveC_Image_Classes_getPropertyNames;
- definition.finalize = &Finalize;
ObjectiveC_Image_Classes_ = JSClassCreate(&definition);
definition = kJSClassDefinitionEmpty;
Array_ = CYCastJSObject(context, CYGetProperty(context, global, CYJSString("Array")));
Function_ = CYCastJSObject(context, CYGetProperty(context, global, CYJSString("Function")));
+ String_ = CYCastJSObject(context, CYGetProperty(context, global, CYJSString("String")));
length_ = JSStringCreateWithUTF8CString("length");
message_ = JSStringCreateWithUTF8CString("message");
toCYON_ = JSStringCreateWithUTF8CString("toCYON");
toJSON_ = JSStringCreateWithUTF8CString("toJSON");
+ JSObjectRef Object(CYCastJSObject(context, CYGetProperty(context, global, CYJSString("Object"))));
+ Object_prototype_ = CYCastJSObject(context, CYGetProperty(context, Object, prototype_));
+
+ 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 Instance(JSObjectMakeConstructor(context, Instance_, &Instance_new));
JSObjectRef Message(JSObjectMakeConstructor(context, Message_, NULL));
+ JSObjectRef Selector(JSObjectMakeConstructor(context, Selector_, &Selector_new));
+
+ Instance_prototype_ = (JSObjectRef) CYGetProperty(context, Instance, prototype_);
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("Instance"), Instance);
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, global, CYJSString("objc_registerClassPair"), JSObjectMakeFunctionWithCallback(context, CYJSString("objc_registerClassPair"), &objc_registerClassPair_));
CYSetProperty(context, global, CYJSString("objc_msgSend"), JSObjectMakeFunctionWithCallback(context, CYJSString("objc_msgSend"), &$objc_msgSend));
+ CYSetProperty(context, global, CYJSString("$cyq"), JSObjectMakeFunctionWithCallback(context, CYJSString("$cyq"), &$cyq));
System_ = JSObjectMake(context, NULL, NULL);
CYSetProperty(context, global, CYJSString("system"), System_);
CYSetProperty(context, System_, CYJSString("print"), JSObjectMakeFunctionWithCallback(context, CYJSString("print"), &System_print));
Result_ = JSStringCreateWithUTF8CString("_");
+
+ JSValueProtect(context, Array_);
+ JSValueProtect(context, Function_);
+ JSValueProtect(context, String_);
+
+ JSValueProtect(context, Instance_prototype_);
+ JSValueProtect(context, Object_prototype_);
+
+ JSValueProtect(context, Array_prototype_);
+ JSValueProtect(context, Array_pop_);
+ JSValueProtect(context, Array_push_);
+ JSValueProtect(context, Array_splice_);
}
return Context_;