+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);
+ if (value + strlen(value) == end)
+ return true;
+ } else if (value[1] == '\0') {
+ index = 0;
+ return true;
+ }
+
+ return false;
+}
+
+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);
+
+@interface NSMethodSignature (Cycript)
+- (NSString *) _typeString;
+@end
+
+@interface NSObject (Cycript)
+
+- (JSValueRef) cy$JSValueInContext:(JSContextRef)context;
+- (JSType) cy$JSType;
+
+- (NSObject *) cy$toJSON:(NSString *)key;
+- (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;
+
+@end
+
+@protocol Cycript
+- (JSValueRef) cy$JSValueInContext:(JSContextRef)context;
+@end
+
+@interface NSString (Cycript)
+- (void *) cy$symbol;
+@end
+
+struct PropertyAttributes {
+ CYPool pool_;
+
+ const char *name;
+
+ const char *variable;
+
+ const char *getter_;
+ const char *setter_;
+
+ bool readonly;
+ bool copy;
+ bool retain;
+ bool nonatomic;
+ bool dynamic;
+ bool weak;
+ bool garbage;
+
+ PropertyAttributes(objc_property_t property) :
+ variable(NULL),
+ getter_(NULL),
+ setter_(NULL),
+ readonly(false),
+ copy(false),
+ retain(false),
+ nonatomic(false),
+ dynamic(false),
+ weak(false),
+ garbage(false)
+ {
+ name = property_getName(property);
+ const char *attributes(property_getAttributes(property));
+
+ for (char *state, *token(apr_strtok(apr_pstrdup(pool_, attributes), ",", &state)); token != NULL; token = apr_strtok(NULL, ",", &state)) {
+ switch (*token) {
+ case 'R': readonly = true; break;
+ case 'C': copy = true; break;
+ case '&': retain = true; break;
+ case 'N': nonatomic = true; break;
+ case 'G': getter_ = token + 1; break;
+ case 'S': setter_ = token + 1; break;
+ case 'V': variable = token + 1; break;
+ }
+ }
+
+ /*if (variable == NULL) {
+ variable = property_getName(property);
+ size_t size(strlen(variable));
+ char *name(new(pool_) char[size + 2]);
+ name[0] = '_';
+ memcpy(name + 1, variable, size);
+ name[size + 1] = '\0';
+ variable = name;
+ }*/
+ }
+
+ const char *Getter() {
+ if (getter_ == NULL)
+ getter_ = apr_pstrdup(pool_, name);
+ return getter_;
+ }
+
+ const char *Setter() {
+ if (setter_ == NULL && !readonly) {
+ size_t length(strlen(name));
+
+ char *temp(new(pool_) char[length + 5]);
+ temp[0] = 's';
+ temp[1] = 'e';
+ temp[2] = 't';
+
+ if (length != 0) {
+ temp[3] = toupper(name[0]);
+ memcpy(temp + 4, name + 1, length - 1);
+ }
+
+ temp[length + 3] = ':';
+ temp[length + 4] = '\0';
+ setter_ = temp;
+ }
+
+ return setter_;
+ }
+
+};
+
+@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
+
+@implementation NSArray (Cycript)
+
+- (NSString *) cy$toCYON {
+ NSMutableString *json([[[NSMutableString alloc] init] autorelease]);
+ [json appendString:@"["];
+
+ bool comma(false);
+ for (id object in self) {
+ if (comma)
+ [json appendString:@","];
+ else
+ comma = true;
+ if (object == nil || [object cy$JSType] != kJSTypeUndefined)
+ [json appendString:CYPoolNSCYON(NULL, object)];
+ else {
+ [json appendString:@","];
+ comma = false;
+ }
+ }
+
+ [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]];
+
+ 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 {
+ 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
+
+@implementation NSDictionary (Cycript)
+
+- (NSString *) cy$toCYON {
+ NSMutableString *json([[[NSMutableString alloc] init] autorelease]);
+ [json appendString:@"{"];
+
+ bool comma(false);
+ for (id key in self) {
+ if (comma)
+ [json appendString:@","];
+ else
+ comma = true;
+ [json appendString:[key cy$toKey]];
+ [json appendString:@":"];
+ NSObject *object([self objectForKey:key]);
+ [json appendString:CYPoolNSCYON(NULL, object)];
+ }
+
+ [json appendString:@"}"];
+ return json;
+}
+
+- (bool) cy$hasProperty:(NSString *)name {
+ return [self objectForKey:name] != nil;
+}
+
+- (NSObject *) cy$getProperty:(NSString *)name {
+ return [self objectForKey:name];
+}
+
+@end
+
+@implementation NSMutableDictionary (Cycript)
+
+- (bool) cy$setProperty:(NSString *)name to:(NSObject *)value {
+ [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
+
+@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
+
+@implementation NSString (Cycript)
+
+- (JSType) cy$JSType {
+ return kJSTypeString;
+}
+
+- (NSObject *) cy$toJSON:(NSString *)key {
+ return self;
+}
+
+- (NSString *) cy$toCYON {
+ // XXX: this should use the better code from Output.cpp
+ CFMutableStringRef json(CFStringCreateMutableCopy(kCFAllocatorDefault, 0, (CFStringRef) self));
+
+ CFStringFindAndReplace(json, CFSTR("\\"), CFSTR("\\\\"), CFRangeMake(0, CFStringGetLength(json)), 0);
+ CFStringFindAndReplace(json, CFSTR("\""), CFSTR("\\\""), CFRangeMake(0, CFStringGetLength(json)), 0);
+ CFStringFindAndReplace(json, CFSTR("\t"), CFSTR("\\t"), CFRangeMake(0, CFStringGetLength(json)), 0);
+ CFStringFindAndReplace(json, CFSTR("\r"), CFSTR("\\r"), CFRangeMake(0, CFStringGetLength(json)), 0);
+ CFStringFindAndReplace(json, CFSTR("\n"), CFSTR("\\n"), CFRangeMake(0, CFStringGetLength(json)), 0);
+
+ CFStringInsert(json, 0, CFSTR("\""));
+ CFStringAppend(json, CFSTR("\""));
+
+ return [reinterpret_cast<const NSString *>(json) autorelease];
+}
+
+- (NSString *) cy$toKey {
+ const char *value([self UTF8String]);
+ size_t size(strlen(value));
+
+ if (size == 0)
+ goto cyon;
+
+ if (DigitRange_[value[0]]) {
+ size_t index(CYGetIndex(NULL, self));
+ if (index == _not(size_t))
+ goto cyon;
+ } else {
+ if (!WordStartRange_[value[0]])
+ goto cyon;
+ for (size_t i(1); i != size; ++i)
+ if (!WordEndRange_[value[i]])
+ goto cyon;
+ }
+
+ return self;
+
+ cyon:
+ return [self cy$toCYON];
+}
+
+- (void *) cy$symbol {
+ CYPool pool;
+ return dlsym(RTLD_DEFAULT, CYPoolCString(pool, self));
+}
+
+@end
+
+@interface CYJSObject : NSMutableDictionary {
+ JSObjectRef object_;
+ JSContextRef context_;
+}
+
+- (id) initWithJSObject:(JSObjectRef)object inContext:(JSContextRef)context;
+
+- (NSString *) cy$toJSON:(NSString *)key;
+
+- (NSUInteger) count;
+- (id) objectForKey:(id)key;
+- (NSEnumerator *) keyEnumerator;
+- (void) setObject:(id)object forKey:(id)key;
+- (void) removeObjectForKey:(id)key;
+
+@end
+
+@interface CYJSArray : NSMutableArray {
+ JSObjectRef object_;
+ JSContextRef context_;
+}
+
+- (id) initWithJSObject:(JSObjectRef)object inContext:(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
+CYRange WordStartRange_(0x000001000000000LLU, 0x7fffffe87fffffeLLU); // A-Za-z_$
+CYRange WordEndRange_ (0x3ff001000000000LLU, 0x7fffffe87fffffeLLU); // A-Za-z_$0-9
+
+#define CYTry \
+ @try
+#define CYCatch \
+ @catch (id error) { \
+ CYThrow(context, error, exception); \
+ return NULL; \
+ }
+
+void CYThrow(JSContextRef context, JSValueRef value);
+
+apr_status_t CYPoolRelease_(void *data) {
+ id object(reinterpret_cast<id>(data));
+ [object release];
+ return APR_SUCCESS;
+}
+
+id CYPoolRelease(apr_pool_t *pool, id object) {
+ if (object == nil)
+ return nil;
+ else if (pool == NULL)
+ return [object autorelease];
+ else {
+ apr_pool_cleanup_register(pool, object, &CYPoolRelease_, &apr_pool_cleanup_null);
+ return object;
+ }
+}
+
+CFTypeRef CYPoolRelease(apr_pool_t *pool, CFTypeRef object) {
+ return (CFTypeRef) CYPoolRelease(pool, (id) object);
+}
+
+id CYCastNSObject_(apr_pool_t *pool, JSContextRef context, JSObjectRef object) {
+ JSValueRef exception(NULL);
+ bool array(JSValueIsInstanceOfConstructor(context, object, Array_, &exception));
+ CYThrow(context, exception);
+ id value(array ? [CYJSArray alloc] : [CYJSObject alloc]);
+ return CYPoolRelease(pool, [value initWithJSObject:object inContext:context]);
+}
+
+id CYCastNSObject(apr_pool_t *pool, JSContextRef context, JSObjectRef object) {
+ if (!JSValueIsObjectOfClass(context, object, Instance_))
+ return CYCastNSObject_(pool, context, object);
+ else {
+ Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
+ return internal->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_;
+ }
+};
+
+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) {
+ char *end;
+ double number(strtod(value, &end));
+ if (end != value + size)
+ return NAN;
+ return number;
+}
+
+double CYCastDouble(const char *value) {
+ return CYCastDouble(value, strlen(value));
+}
+
+double CYCastDouble(JSContextRef context, JSValueRef value) {
+ JSValueRef exception(NULL);
+ double number(JSValueToNumber(context, value, &exception));
+ CYThrow(context, exception);
+ return number;
+}
+
+CFNumberRef CYCopyCFNumber(JSContextRef context, JSValueRef value) {
+ double number(CYCastDouble(context, value));
+ return CFNumberCreate(kCFAllocatorDefault, kCFNumberDoubleType, &number);
+}
+
+CFStringRef CYCopyCFString(const char *value) {
+ return CFStringCreateWithCString(kCFAllocatorDefault, value, kCFStringEncodingUTF8);
+}
+
+NSString *CYCastNSString(apr_pool_t *pool, const char *value) {
+ return (NSString *) CYPoolRelease(pool, CYCopyCFString(value));
+}
+
+NSString *CYCastNSString(apr_pool_t *pool, JSStringRef value) {
+ return (NSString *) CYPoolRelease(pool, CYCopyCFString(value));
+}
+
+bool CYCastBool(JSContextRef context, JSValueRef value) {
+ return JSValueToBoolean(context, value);
+}
+
+CFTypeRef CYCFType(apr_pool_t *pool, JSContextRef context, JSValueRef value, bool cast) {
+ CFTypeRef object;
+ bool copy;
+
+ switch (JSType type = JSValueGetType(context, value)) {
+ case kJSTypeUndefined:
+ object = [WebUndefined undefined];
+ copy = false;
+ break;
+
+ case kJSTypeNull:
+ return NULL;
+ break;
+
+ case kJSTypeBoolean:
+ object = CYCastBool(context, value) ? kCFBooleanTrue : kCFBooleanFalse;
+ copy = false;
+ break;
+
+ case kJSTypeNumber:
+ object = CYCopyCFNumber(context, value);
+ copy = true;
+ break;
+
+ case kJSTypeString:
+ object = CYCopyCFString(context, value);
+ copy = true;
+ break;
+
+ case kJSTypeObject:
+ // XXX: this might could be more efficient
+ object = (CFTypeRef) CYCastNSObject(pool, context, (JSObjectRef) value);
+ copy = false;
+ break;
+
+ default:
+ @throw [NSException exceptionWithName:NSInternalInconsistencyException reason:[NSString stringWithFormat:@"JSValueGetType() == 0x%x", type] userInfo:nil];
+ break;
+ }
+
+ if (cast != copy)
+ return object;
+ else if (copy)
+ return CYPoolRelease(pool, object);
+ else
+ return CFRetain(object);
+}
+
+CFTypeRef CYCastCFType(apr_pool_t *pool, JSContextRef context, JSValueRef value) {
+ return CYCFType(pool, context, value, true);
+}
+
+CFTypeRef CYCopyCFType(apr_pool_t *pool, JSContextRef context, JSValueRef value) {
+ return CYCFType(pool, context, value, false);
+}
+
+NSArray *CYCastNSArray(JSPropertyNameArrayRef names) {
+ CYPool pool;
+ size_t size(JSPropertyNameArrayGetCount(names));
+ NSMutableArray *array([NSMutableArray arrayWithCapacity:size]);
+ for (size_t index(0); index != size; ++index)
+ [array addObject:CYCastNSString(pool, JSPropertyNameArrayGetNameAtIndex(names, index))];
+ return array;
+}
+
+id CYCastNSObject(apr_pool_t *pool, JSContextRef context, JSValueRef value) {
+ return reinterpret_cast<const NSObject *>(CYCastCFType(pool, context, value));