From c074e774a5364a868ddde950b37342f89ae936ce Mon Sep 17 00:00:00 2001 From: "Jay Freeman (saurik)" Date: Sun, 1 Nov 2009 05:07:51 +0000 Subject: [PATCH] Got true JS booleans working in GNUstep. --- ObjectiveC/Library.mm | 35 ++++++++++++++++++++++++++++++++++- cycript.hpp | 2 +- 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/ObjectiveC/Library.mm b/ObjectiveC/Library.mm index 81dfe86..5059300 100644 --- a/ObjectiveC/Library.mm +++ b/ObjectiveC/Library.mm @@ -231,6 +231,8 @@ static Class NSCFBoolean_; static Class NSCFType_; static Class NSMessageBuilder_; static Class NSZombie_; +#else +static Class NSBoolNumber_; #endif static Class NSArray_; @@ -574,6 +576,12 @@ NSNumber *CYCopyNSNumber(JSContextRef context, JSValueRef value) { return [[NSNumber alloc] initWithDouble:CYCastDouble(context, value)]; } +#ifndef __APPLE__ +@interface NSBoolNumber : NSNumber { +} +@end +#endif + id CYNSObject(apr_pool_t *pool, JSContextRef context, JSValueRef value, bool cast) { id object; bool copy; @@ -593,7 +601,7 @@ id CYNSObject(apr_pool_t *pool, JSContextRef context, JSValueRef value, bool cas object = (id) (CYCastBool(context, value) ? kCFBooleanTrue : kCFBooleanFalse); copy = false; #else - object = [[NSNumber alloc] initWithBool:CYCastBool(context, value)]; + object = [[NSBoolNumber alloc] initWithBool:CYCastBool(context, value)]; copy = true; #endif break; @@ -708,6 +716,29 @@ NSObject *CYCopyNSObject(apr_pool_t *pool, JSContextRef context, JSValueRef valu @end /* }}} */ +/* Bridge: NSBoolNumber {{{ */ +#ifndef __APPLE__ +@implementation NSBoolNumber (Cycript) + +- (JSType) cy$JSType { + return kJSTypeBoolean; +} + +- (NSObject *) cy$toJSON:(NSString *)key { + return self; +} + +- (NSString *) cy$toCYON { + return [self boolValue] ? @"true" : @"false"; +} + +- (JSValueRef) cy$JSValueInContext:(JSContextRef)context { CYObjectiveTry_(context) { + return CYCastJSValue(context, (bool) [self boolValue]); +} CYObjectiveCatch } + +@end +#endif +/* }}} */ /* Bridge: NSDictionary {{{ */ @implementation NSDictionary (Cycript) @@ -2213,6 +2244,8 @@ void CYObjectiveC_SetupContext(JSContextRef context) { NSCFType_ = objc_getClass("NSCFType"); NSMessageBuilder_ = objc_getClass("NSMessageBuilder"); NSZombie_ = objc_getClass("_NSZombie_"); +#else + NSBoolNumber_ = objc_getClass("NSBoolNumber"); #endif NSArray_ = objc_getClass("NSArray"); diff --git a/cycript.hpp b/cycript.hpp index ff16324..a1e1f02 100644 --- a/cycript.hpp +++ b/cycript.hpp @@ -93,7 +93,7 @@ JSValueRef CYGetProperty(JSContextRef context, JSObjectRef object, JSStringRef n void CYSetProperty(JSContextRef context, JSObjectRef object, size_t index, JSValueRef value); void CYSetProperty(JSContextRef context, JSObjectRef object, JSStringRef name, JSValueRef value, JSPropertyAttributes attributes = kJSPropertyAttributeNone); -//JSValueRef CYCastJSValue(JSContextRef context, bool value); +JSValueRef CYCastJSValue(JSContextRef context, bool value); JSValueRef CYCastJSValue(JSContextRef context, double value); JSValueRef CYCastJSValue(JSContextRef context, int value); JSValueRef CYCastJSValue(JSContextRef context, unsigned int value); -- 2.45.2