From: Jay Freeman (saurik) Date: Tue, 27 Apr 2010 20:58:39 +0000 (+0000) Subject: NSString was not even remotely correctly bridged. X-Git-Tag: v0.9.432~59 X-Git-Url: https://git.saurik.com/cycript.git/commitdiff_plain/6aa16f295d167b8a3d6803a862c049a8d5af433e?ds=inline NSString was not even remotely correctly bridged. --- diff --git a/ObjectiveC/Library.mm b/ObjectiveC/Library.mm index 1f225bc..817fa1a 100644 --- a/ObjectiveC/Library.mm +++ b/ObjectiveC/Library.mm @@ -1048,6 +1048,49 @@ NSObject *CYCopyNSObject(apr_pool_t *pool, JSContextRef context, JSValueRef valu return [self cy$toCYON]; } +- (bool) cy$hasProperty:(NSString *)name { + if ([name isEqualToString:@"length"]) + return true; + + size_t index(CYGetIndex(name)); + if (index == _not(size_t) || index >= [self length]) + return [super cy$hasProperty:name]; + else + return true; +} + +- (NSObject *) cy$getProperty:(NSString *)name { + if ([name isEqualToString:@"length"]) { + NSUInteger count([self length]); +#ifdef __APPLE__ + return [NSNumber numberWithUnsignedInteger:count]; +#else + return [NSNumber numberWithUnsignedInt:count]; +#endif + } + + size_t index(CYGetIndex(name)); + if (index == _not(size_t) || index >= [self length]) + return [super cy$getProperty:name]; + else + return [self substringWithRange:NSMakeRange(index, 1)]; +} + +- (void) cy$getPropertyNames:(JSPropertyNameAccumulatorRef)names inContext:(JSContextRef)context { + [super cy$getPropertyNames:names inContext:context]; + + for (size_t index(0), length([self length]); index != length; ++index) { + char name[32]; + sprintf(name, "%zu", index); + JSPropertyNameAccumulatorAddName(names, CYJSString(name)); + } +} + +// XXX: this might be overly restrictive for NSString; I think I need a half-way between /injecting/ implicit properties and /accepting/ implicit properties ++ (bool) cy$hasImplicitProperties { + return false; +} + @end /* }}} */ /* Bridge: WebUndefined {{{ */