- (JSValueRef) cy$valueOfInContext:(JSContextRef)context;
- (JSType) cy$JSType;
-- (NSObject *) cy$toJSON:(NSString *)key;
+- (JSValueRef) cy$toJSON:(NSString *)key inContext:(JSContextRef)context;
- (NSString *) cy$toCYON:(bool)objective;
- (bool) cy$hasProperty:(NSString *)name;
};
#endif
-#ifdef __APPLE__
-NSObject *NSCFType$cy$toJSON(id self, SEL sel, NSString *key) {
- return [(NSString *) CFCopyDescription((CFTypeRef) self) autorelease];
-}
-#endif
-
#ifndef __APPLE__
@interface CYWebUndefined : NSObject {
}
- (id) initWithJSObject:(JSObjectRef)object inContext:(JSContextRef)context;
-- (NSObject *) cy$toJSON:(NSString *)key;
-
- (NSUInteger) count;
- (id) objectForKey:(id)key;
- (NSEnumerator *) keyEnumerator;
return kJSTypeBoolean;
}
-- (NSObject *) cy$toJSON:(NSString *)key {
- return self;
-}
-
- (NSString *) cy$toCYON:(bool)objective {
NSString *value([self boolValue] ? @"true" : @"false");
return objective ? value : [NSString stringWithFormat:@"@%@", value];
return kJSTypeNumber;
}
-- (NSObject *) cy$toJSON:(NSString *)key {
- return self;
-}
-
- (NSString *) cy$toCYON:(bool)objective {
NSString *value([self cy$JSType] != kJSTypeBoolean ? [self stringValue] : [self boolValue] ? @"true" : @"false");
return objective ? value : [NSString stringWithFormat:@"@%@", value];
return kJSTypeNull;
}
-- (NSObject *) cy$toJSON:(NSString *)key {
- return self;
-}
-
- (NSString *) cy$toCYON:(bool)objective {
NSString *value(@"null");
return objective ? value : [NSString stringWithFormat:@"@%@", value];
}
+- (JSValueRef) cy$valueOfInContext:(JSContextRef)context { CYObjectiveTry_(context) {
+ return CYJSNull(context);
+} CYObjectiveCatch }
+
@end
/* }}} */
/* Bridge: NSObject {{{ */
return self;
}
+- (JSValueRef) cy$toJSON:(NSString *)key inContext:(JSContextRef)context {
+ return [self cy$valueOfInContext:context];
+}
+
- (JSValueRef) cy$valueOfInContext:(JSContextRef)context { CYObjectiveTry_(context) {
return NULL;
} CYObjectiveCatch }
return kJSTypeObject;
}
-- (NSObject *) cy$toJSON:(NSString *)key {
- return [self description];
-}
-
- (NSString *) cy$toCYON:(bool)objective {
- return [[self cy$toJSON:@""] cy$toCYON:objective];
+ return [[self description] cy$toCYON:objective];
}
- (bool) cy$hasProperty:(NSString *)name {
/* Bridge: NSProxy {{{ */
@implementation NSProxy (Cycript)
-- (NSObject *) cy$toJSON:(NSString *)key {
- return [self description];
-}
-
- (NSString *) cy$toCYON:(bool)objective {
- return [[self cy$toJSON:@""] cy$toCYON:objective];
+ return [[self description] cy$toCYON:objective];
}
@end
return kJSTypeString;
}
-- (NSObject *) cy$toJSON:(NSString *)key {
- return self;
-}
-
- (NSString *) cy$toCYON:(bool)objective {
std::ostringstream str;
if (!objective)
return kJSTypeUndefined;
}
-- (NSObject *) cy$toJSON:(NSString *)key {
- return self;
-}
-
- (NSString *) cy$toCYON {
NSString *value(@"undefined");
return value; // XXX: maybe use the below code, adding @undefined?
[super dealloc];
} CYObjectiveCatch }
-- (NSObject *) cy$toJSON:(NSString *)key { CYObjectiveTry {
- JSValueRef toJSON(CYGetProperty(context_, object_, toJSON_s));
- if (!CYIsCallable(context_, toJSON))
- return [super cy$toJSON:key];
- else {
- JSValueRef arguments[1] = {CYCastJSValue(context_, key)};
- JSValueRef value(CYCallAsFunction(context_, (JSObjectRef) toJSON, object_, 1, arguments));
- // XXX: do I really want an NSNull here?!
- return CYCastNSObject(NULL, context_, value) ?: [NSNull null];
- }
-} CYObjectiveCatch }
-
- (NSString *) cy$toCYON:(bool)objective { CYObjectiveTry {
CYPool pool;
JSValueRef exception(NULL);
return NULL;
Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(_this)));
+ id value(internal->GetValue());
CYPoolTry {
NSString *key;
key = nil;
else
key = CYCastNSString(NULL, context, CYJSString(context, arguments[0]));
- // XXX: check for support of cy$toJSON?
- return CYCastJSValue(context, CYJSString(context, [internal->GetValue() cy$toJSON:key]));
+
+ if (!CYImplements(value, object_getClass(value), @selector(cy$toJSON:inContext:)))
+ return CYJSUndefined(context);
+ else if (JSValueRef json = [value cy$toJSON:key inContext:context])
+ return json;
+ else
+ return CYCastJSValue(context, CYJSString(context, [value description]));
} CYPoolCatch(NULL)
} CYCatch return /*XXX*/ NULL; }
{NULL, NULL, 0}
};
+#ifdef __APPLE__
+JSValueRef NSCFType$cy$toJSON$inContext$(id self, SEL sel, JSValueRef key, JSContextRef context) { CYObjectiveTry_(context) {
+ return CYCastJSValue(context, [(NSString *) CFCopyDescription((CFTypeRef) self) autorelease]);
+} CYObjectiveCatch }
+#endif
+
void CYObjectiveC_Initialize() { /*XXX*/ JSContextRef context(NULL); CYPoolTry {
$objc_setAssociatedObject = reinterpret_cast<void (*)(id, void *, id value, objc_AssociationPolicy)>(dlsym(RTLD_DEFAULT, "objc_setAssociatedObject"));
$objc_getAssociatedObject = reinterpret_cast<id (*)(id, void *)>(dlsym(RTLD_DEFAULT, "objc_getAssociatedObject"));
ObjectiveC_Protocols_ = JSClassCreate(&definition);
#ifdef __APPLE__
- class_addMethod(NSCFType_, @selector(cy$toJSON:), reinterpret_cast<IMP>(&NSCFType$cy$toJSON), "@12@0:4@8");
+ class_addMethod(NSCFType_, @selector(cy$toJSON:inContext:), reinterpret_cast<IMP>(&NSCFType$cy$toJSON$inContext$), "^{OpaqueJSValue=}16@0:4@8^{OpaqueJSContext=}12");
#endif
} CYPoolCatch() }