X-Git-Url: https://git.saurik.com/cycript.git/blobdiff_plain/4afefdd93c6c33cd3919fc4f35674eb27c5ac90b..283e7e33dbc363c7bba72e3838b5e0d61c92bf2b:/Library.mm?ds=sidebyside diff --git a/Library.mm b/Library.mm index bc4a6c7..0083fb5 100644 --- a/Library.mm +++ b/Library.mm @@ -157,32 +157,6 @@ struct Pointer_privateData : } }; -struct Functor_privateData : - Pointer_privateData -{ - sig::Signature signature_; - ffi_cif cif_; - - Functor_privateData(const char *type, void (*value)()) : - Pointer_privateData(reinterpret_cast(value)) - { - sig::Parse(pool_, &signature_, type); - sig::sig_ffi_cif(pool_, &sig::ObjectiveC, &signature_, &cif_); - } -}; - -struct ffoData : - Functor_privateData -{ - JSContextRef context_; - JSObjectRef function_; - - ffoData(const char *type) : - Functor_privateData(type, NULL) - { - } -}; - struct Selector_privateData : Pointer_privateData { Selector_privateData(SEL value) : Pointer_privateData(value) @@ -279,6 +253,14 @@ void Copy(apr_pool_t *pool, ffi_type &lhs, ffi_type &rhs) { } +struct CStringMapLess : + std::binary_function +{ + _finline bool operator ()(const char *lhs, const char *rhs) const { + return strcmp(lhs, rhs) < 0; + } +}; + struct Type_privateData { sig::Type type_; ffi_type ffi_; @@ -299,14 +281,6 @@ struct Struct_privateData : } }; -struct CStringMapLess : - std::binary_function -{ - _finline bool operator ()(const char *lhs, const char *rhs) const { - return strcmp(lhs, rhs) < 0; - } -}; - typedef std::map TypeMap; static TypeMap Types_; @@ -331,6 +305,45 @@ JSObjectRef CYMakeStruct(JSContextRef context, void *data, sig::Type *type, ffi_ return JSObjectMake(context, Struct_, internal); } +void Structor_(apr_pool_t *pool, const char *name, const char *types, sig::Type *type) { + CYPoolTry { + if (NSMutableArray *entry = [[Bridge_ objectAtIndex:2] objectForKey:[NSString stringWithUTF8String:name]]) { + switch ([[entry objectAtIndex:0] intValue]) { + case 0: + static CYPool Pool_; + sig::Parse(Pool_, &type->data.signature, [[entry objectAtIndex:1] UTF8String], &Structor_); + break; + } + } + } CYPoolCatch() +} + +struct Functor_privateData : + Pointer_privateData +{ + sig::Signature signature_; + ffi_cif cif_; + + Functor_privateData(const char *type, void (*value)()) : + Pointer_privateData(reinterpret_cast(value)) + { + sig::Parse(pool_, &signature_, type, &Structor_); + sig::sig_ffi_cif(pool_, &sig::ObjectiveC, &signature_, &cif_); + } +}; + +struct ffoData : + Functor_privateData +{ + JSContextRef context_; + JSObjectRef function_; + + ffoData(const char *type) : + Functor_privateData(type, NULL) + { + } +}; + JSObjectRef CYMakeInstance(JSContextRef context, id object, bool transient) { if (!transient) object = [object retain]; @@ -374,6 +387,24 @@ JSValueRef CYJSUndefined(JSContextRef context) { return JSValueMakeUndefined(context); } +size_t CYCastIndex(const char *value) { + if (value[0] == '0') { + if (value[1] == '\0') + return 0; + } else { + char *end; + size_t index(strtoul(value, &end, 10)); + if (value + strlen(value) == end) + return index; + } + + return _not(size_t); +} + +size_t CYCastIndex(NSString *value) { + return CYCastIndex([value UTF8String]); +} + @interface NSMethodSignature (Cycript) - (NSString *) _typeString; @end @@ -384,6 +415,7 @@ JSValueRef CYJSUndefined(JSContextRef context) { - (NSObject *) cy$toJSON:(NSString *)key; - (NSString *) cy$toCYON; +- (NSString *) cy$toKey; - (JSValueRef) cy$JSValueInContext:(JSContextRef)context transient:(bool)transient; @@ -501,6 +533,10 @@ struct PropertyAttributes { return [[self cy$toJSON:@""] cy$toCYON]; } +- (NSString *) cy$toKey { + return [self cy$toCYON]; +} + - (JSValueRef) cy$JSValueInContext:(JSContextRef)context transient:(bool)transient { return CYMakeInstance(context, self, transient); } @@ -587,8 +623,8 @@ struct PropertyAttributes { if ([name isEqualToString:@"length"]) return [NSNumber numberWithUnsignedInteger:[self count]]; - int index([name intValue]); - if (index < 0 || index >= static_cast([self count])) + size_t index(CYCastIndex(name)); + if (index == _not(size_t) || index >= [self count]) return [super cy$getProperty:name]; else return [self objectAtIndex:index]; @@ -599,8 +635,8 @@ struct PropertyAttributes { @implementation NSMutableArray (Cycript) - (bool) cy$setProperty:(NSString *)name to:(NSObject *)value { - int index([name intValue]); - if (index < 0 || index >= static_cast([self count])) + size_t index(CYCastIndex(name)); + if (index == _not(size_t) || index >= [self count]) return [super cy$setProperty:name to:value]; else { [self replaceObjectAtIndex:index withObject:(value ?: [NSNull null])]; @@ -609,8 +645,8 @@ struct PropertyAttributes { } - (bool) cy$deleteProperty:(NSString *)name { - int index([name intValue]); - if (index < 0 || index >= static_cast([self count])) + size_t index(CYCastIndex(name)); + if (index == _not(size_t) || index >= [self count]) return [super cy$deleteProperty:name]; else { [self removeObjectAtIndex:index]; @@ -624,7 +660,7 @@ struct PropertyAttributes { - (NSString *) cy$toCYON { NSMutableString *json([[[NSMutableString alloc] init] autorelease]); - [json appendString:@"({"]; + [json appendString:@"{"]; bool comma(false); for (id key in self) { @@ -632,13 +668,13 @@ struct PropertyAttributes { [json appendString:@","]; else comma = true; - [json appendString:[key cy$toCYON]]; + [json appendString:[key cy$toKey]]; [json appendString:@":"]; NSObject *object([self objectForKey:key]); [json appendString:[object cy$toCYON]]; } - [json appendString:@"})"]; + [json appendString:@"}"]; return json; } @@ -702,6 +738,7 @@ struct PropertyAttributes { } - (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); @@ -716,6 +753,30 @@ struct PropertyAttributes { return [reinterpret_cast(json) autorelease]; } +- (NSString *) cy$toKey { + const char *value([self UTF8String]); + size_t size(strlen(value)); + + if (size == 0) + goto cyon; + + if (DigitRange_[value[0]]) { + if (CYCastIndex(self) == _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)); @@ -752,8 +813,9 @@ struct PropertyAttributes { @end -CYRange WordStartRange_(0x1000000000LLU,0x7fffffe87fffffeLLU); // A-Za-z_$ -CYRange WordEndRange_(0x3ff001000000000LLU,0x7fffffe87fffffeLLU); // A-Za-z_$0-9 +CYRange DigitRange_ (0x3ff000000000000LLU, 0x000000000000000LLU); // 0-9 +CYRange WordStartRange_(0x000001000000000LLU, 0x7fffffe87fffffeLLU); // A-Za-z_$ +CYRange WordEndRange_ (0x3ff001000000000LLU, 0x7fffffe87fffffeLLU); // A-Za-z_$0-9 JSGlobalContextRef CYGetJSContext() { return Context_; @@ -829,7 +891,8 @@ class CYJSString { JSStringRef string_; void Clear_() { - JSStringRelease(string_); + if (string_ != NULL) + JSStringRelease(string_); } public: @@ -1353,13 +1416,29 @@ void CYPoolFFI(apr_pool_t *pool, JSContextRef context, sig::Type *type, ffi_type case sig::struct_P: { uint8_t *base(reinterpret_cast(data)); - bool aggregate(JSValueIsObject(context, value)); + JSObjectRef aggregate(JSValueIsObject(context, value) ? (JSObjectRef) value : NULL); for (size_t index(0); index != type->data.signature.count; ++index) { - ffi_type *element(ffi->elements[index]); - JSValueRef rhs(aggregate ? CYGetProperty(context, (JSObjectRef) value, index) : value); - CYPoolFFI(pool, context, type->data.signature.elements[index].type, element, base, rhs); + sig::Element *element(&type->data.signature.elements[index]); + ffi_type *field(ffi->elements[index]); + + JSValueRef rhs; + if (aggregate == NULL) + rhs = value; + else { + rhs = CYGetProperty(context, aggregate, index); + if (JSValueIsUndefined(context, rhs)) { + if (element->name != NULL) + rhs = CYGetProperty(context, aggregate, CYJSString(element->name)); + else + goto undefined; + if (JSValueIsUndefined(context, rhs)) undefined: + @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"unable to extract structure value" userInfo:nil]; + } + } + + CYPoolFFI(pool, context, element->type, field, base, rhs); // XXX: alignment? - base += element->size; + base += field->size; } } break; @@ -1451,18 +1530,30 @@ bool Index_(apr_pool_t *pool, Struct_privateData *internal, JSStringRef property const char *name(CYPoolCString(pool, property, &length)); double number(CYCastDouble(name, length)); + size_t count(typical->type_.data.signature.count); + if (std::isnan(number)) { if (property == NULL) return false; - // XXX: implement! + sig::Element *elements(typical->type_.data.signature.elements); + + for (size_t local(0); local != count; ++local) { + sig::Element *element(&elements[local]); + if (element->name != NULL && strcmp(name, element->name) == 0) { + index = local; + goto base; + } + } + return false; } else { index = static_cast(number); - if (index != number || index < 0 || static_cast(index) >= typical->type_.data.signature.count) + if (index != number || index < 0 || static_cast(index) >= count) return false; } + base: base = reinterpret_cast(internal->value_); for (ssize_t local(0); local != index; ++local) base += typical->ffi_.elements[local]->size; @@ -1471,38 +1562,60 @@ bool Index_(apr_pool_t *pool, Struct_privateData *internal, JSStringRef property } static JSValueRef Struct_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { - CYTry { - CYPool pool; - Struct_privateData *internal(reinterpret_cast(JSObjectGetPrivate(object))); - Type_privateData *typical(internal->type_); + CYPool pool; + Struct_privateData *internal(reinterpret_cast(JSObjectGetPrivate(object))); + Type_privateData *typical(internal->type_); - ssize_t index; - uint8_t *base; + ssize_t index; + uint8_t *base; - if (!Index_(pool, internal, property, index, base)) - return NULL; + if (!Index_(pool, internal, property, index, base)) + return NULL; + CYTry { return CYFromFFI(context, typical->type_.data.signature.elements[index].type, typical->ffi_.elements[index], base, object); } CYCatch } static bool Struct_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) { - CYTry { - CYPool pool; - Struct_privateData *internal(reinterpret_cast(JSObjectGetPrivate(object))); - Type_privateData *typical(internal->type_); + CYPool pool; + Struct_privateData *internal(reinterpret_cast(JSObjectGetPrivate(object))); + Type_privateData *typical(internal->type_); - ssize_t index; - uint8_t *base; + ssize_t index; + uint8_t *base; - if (!Index_(pool, internal, property, index, base)) - return false; + if (!Index_(pool, internal, property, index, base)) + return false; + CYTry { CYPoolFFI(NULL, context, typical->type_.data.signature.elements[index].type, typical->ffi_.elements[index], base, value); return true; } CYCatch } +static void Struct_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) { + Struct_privateData *internal(reinterpret_cast(JSObjectGetPrivate(object))); + Type_privateData *typical(internal->type_); + + size_t count(typical->type_.data.signature.count); + sig::Element *elements(typical->type_.data.signature.elements); + + char number[32]; + + for (size_t index(0); index != count; ++index) { + const char *name; + name = elements[index].name; + + if (name == NULL) { + sprintf(number, "%lu", index); + name = number; + } + + JSPropertyNameAccumulatorAddName(names, CYJSString(name)); + } +} + JSValueRef CYCallFunction(apr_pool_t *pool, JSContextRef context, size_t setups, void *setup[], size_t count, const JSValueRef *arguments, JSValueRef *exception, sig::Signature *signature, ffi_cif *cif, void (*function)()) { CYTry { if (setups + count != signature->count - 1) @@ -1581,7 +1694,7 @@ static JSValueRef Runtime_getProperty(JSContextRef context, JSObjectRef object, case 2: // XXX: this is horrendously inefficient sig::Signature signature; - sig::Parse(pool, &signature, CYPoolCString(pool, [entry objectAtIndex:1])); + sig::Parse(pool, &signature, CYPoolCString(pool, [entry objectAtIndex:1]), &Structor_); ffi_cif cif; sig::sig_ffi_cif(pool, &sig::ObjectiveC, &signature, &cif); return CYFromFFI(context, signature.elements[0].type, cif.rtype, [name cy$symbol]); @@ -1613,13 +1726,19 @@ static JSValueRef System_print(JSContextRef context, JSObjectRef object, JSObjec static JSValueRef CYApplicationMain(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { CYPool pool; - NSString *name(CYCastNSObject(pool, context, arguments[0])); - int argc(*_NSGetArgc()); - char **argv(*_NSGetArgv()); + + int argc(CYCastDouble(context, arguments[0])); + char **argv(CYCastPointer(context, arguments[1])); + NSString *principal(CYCastNSObject(pool, context, arguments[2])); + NSString *delegate(CYCastNSObject(pool, context, arguments[3])); + + argc = *_NSGetArgc() - 1; + argv = *_NSGetArgv() + 1; for (int i(0); i != argc; ++i) NSLog(@"argv[%i]=%s", i, argv[i]); + _pooled - return CYCastJSValue(context, UIApplicationMain(argc, argv, name, name)); + return CYCastJSValue(context, UIApplicationMain(argc, argv, principal, delegate)); } CYCatch } @@ -1643,7 +1762,7 @@ JSValueRef CYSendMessage(apr_pool_t *pool, JSContextRef context, id self, SEL _c setup[1] = &_cmd; sig::Signature signature; - sig::Parse(pool, &signature, type); + sig::Parse(pool, &signature, type, &Structor_); ffi_cif cif; sig::sig_ffi_cif(pool, &sig::ObjectiveC, &signature, &cif); @@ -1905,6 +2024,7 @@ MSInitialize { _pooled definition.className = "Struct"; definition.getProperty = &Struct_getProperty; definition.setProperty = &Struct_setProperty; + definition.getPropertyNames = &Struct_getPropertyNames; definition.finalize = &CYData::Finalize; Struct_ = JSClassCreate(&definition);