X-Git-Url: https://git.saurik.com/cycript.git/blobdiff_plain/b4aa79afea0c26718af8880c3ee8fea63eb21563..283e7e33dbc363c7bba72e3838b5e0d61c92bf2b:/Library.mm?ds=sidebyside diff --git a/Library.mm b/Library.mm index 64f7562..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,36 +253,21 @@ 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_; - //size_t count_; Type_privateData(apr_pool_t *pool, sig::Type *type, ffi_type *ffi) { sig::Copy(pool, type_, *type); sig::Copy(pool, ffi_, *ffi); - - /*sig::Element element; - element.name = NULL; - element.type = type; - element.offset = 0; - - sig::Signature signature; - signature.elements = &element; - signature.count = 1; - - ffi_cif cif; - sig::sig_ffi_cif(pool, &sig::ObjectiveC, &signature, &cif); - ffi_ = *cif.rtype;*/ - - /*if (type_->type != FFI_TYPE_STRUCT) - count_ = 0; - else { - size_t count(0); - while (type_->elements[count] != NULL) - ++count; - count_ = count; - }*/ } }; @@ -322,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_; @@ -354,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]; @@ -368,7 +358,7 @@ const char *CYPoolCString(apr_pool_t *pool, NSString *value) { size_t size([value maximumLengthOfBytesUsingEncoding:NSUTF8StringEncoding] + 1); char *string(new(pool) char[size]); if (![value getCString:string maxLength:size encoding:NSUTF8StringEncoding]) - @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"[NSString getCString:maxLength:encoding:] == NO" userInfo:nil]; + @throw [NSException exceptionWithName:NSInternalInconsistencyException reason:@"[NSString getCString:maxLength:encoding:] == NO" userInfo:nil]; return string; } } @@ -397,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 @@ -407,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; @@ -424,6 +433,92 @@ JSValueRef CYJSUndefined(JSContextRef context) { - (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 NSObject (Cycript) - (JSType) cy$JSType { @@ -438,23 +533,27 @@ JSValueRef CYJSUndefined(JSContextRef context) { 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); } - (NSObject *) cy$getProperty:(NSString *)name { - if (![name isEqualToString:@"prototype"]) - NSLog(@"get:%@", name); + /*if (![name isEqualToString:@"prototype"]) + NSLog(@"get:%@", name);*/ return nil; } - (bool) cy$setProperty:(NSString *)name to:(NSObject *)value { - NSLog(@"set:%@", name); + //NSLog(@"set:%@", name); return false; } - (bool) cy$deleteProperty:(NSString *)name { - NSLog(@"delete:%@", name); + //NSLog(@"delete:%@", name); return false; } @@ -521,8 +620,11 @@ JSValueRef CYJSUndefined(JSContextRef context) { } - (NSObject *) cy$getProperty:(NSString *)name { - int index([name intValue]); - if (index < 0 || index >= static_cast([self count])) + if ([name isEqualToString:@"length"]) + return [NSNumber numberWithUnsignedInteger:[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]; @@ -533,8 +635,8 @@ JSValueRef CYJSUndefined(JSContextRef context) { @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])]; @@ -543,8 +645,8 @@ JSValueRef CYJSUndefined(JSContextRef context) { } - (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]; @@ -558,7 +660,7 @@ JSValueRef CYJSUndefined(JSContextRef context) { - (NSString *) cy$toCYON { NSMutableString *json([[[NSMutableString alloc] init] autorelease]); - [json appendString:@"({"]; + [json appendString:@"{"]; bool comma(false); for (id key in self) { @@ -566,13 +668,13 @@ JSValueRef CYJSUndefined(JSContextRef context) { [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; } @@ -636,6 +738,7 @@ JSValueRef CYJSUndefined(JSContextRef context) { } - (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); @@ -650,6 +753,30 @@ JSValueRef CYJSUndefined(JSContextRef context) { 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)); @@ -686,8 +813,9 @@ JSValueRef CYJSUndefined(JSContextRef context) { @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_; @@ -763,7 +891,8 @@ class CYJSString { JSStringRef string_; void Clear_() { - JSStringRelease(string_); + if (string_ != NULL) + JSStringRelease(string_); } public: @@ -1090,31 +1219,61 @@ const char *CYPoolCYONString(apr_pool_t *pool, JSContextRef context, JSValueRef } static JSValueRef Instance_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { + CYPool pool; + CYTry { - CYPool pool; NSString *self(CYCastNSObject(pool, context, object)); NSString *name(CYCastNSString(pool, property)); - NSObject *data([self cy$getProperty:name]); - return data == nil ? NULL : CYCastJSValue(context, data); + + CYPoolTry { + if (NSObject *data = [self cy$getProperty:name]) + return CYCastJSValue(context, data); + } CYPoolCatch(NULL) + + if (objc_property_t property = class_getProperty(object_getClass(self), [name UTF8String])) { + PropertyAttributes attributes(property); + SEL sel(sel_registerName(attributes.Getter())); + return CYSendMessage(pool, context, self, sel, 0, NULL, exception); + } + + return NULL; } CYCatch } static bool Instance_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) { + CYPool pool; + CYTry { - CYPool pool; NSString *self(CYCastNSObject(pool, context, object)); NSString *name(CYCastNSString(pool, property)); NSString *data(CYCastNSObject(pool, context, value)); - return [self cy$setProperty:name to:data]; + + CYPoolTry { + if ([self cy$setProperty:name to:data]) + return true; + } CYPoolCatch(NULL) + + if (objc_property_t property = class_getProperty(object_getClass(self), [name UTF8String])) { + PropertyAttributes attributes(property); + if (const char *setter = attributes.Setter()) { + SEL sel(sel_registerName(setter)); + JSValueRef arguments[1] = {value}; + CYSendMessage(pool, context, self, sel, 1, arguments, exception); + return true; + } + } + + return false; } CYCatch } static bool Instance_deleteProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry { - CYPool pool; - NSString *self(CYCastNSObject(pool, context, object)); - NSString *name(CYCastNSString(pool, property)); - return [self cy$deleteProperty:name]; + CYPoolTry { + NSString *self(CYCastNSObject(NULL, context, object)); + NSString *name(CYCastNSString(NULL, property)); + return [self cy$deleteProperty:name]; + } CYPoolCatch(NULL) } CYCatch } @@ -1182,16 +1341,6 @@ const char *CYPoolCString(apr_pool_t *pool, JSContextRef context, JSValueRef val utf8; \ }) -SEL CYCastSEL(JSContextRef context, JSValueRef value) { - if (JSValueIsNull(context, value)) - return NULL; - else if (JSValueIsObjectOfClass(context, value, Selector_)) { - Selector_privateData *data(reinterpret_cast(JSObjectGetPrivate((JSObjectRef) value))); - return reinterpret_cast(data->value_); - } else - return sel_registerName(CYCastCString(context, value)); -} - void *CYCastPointer_(JSContextRef context, JSValueRef value) { switch (JSValueGetType(context, value)) { case kJSTypeNull: @@ -1216,6 +1365,14 @@ _finline Type_ CYCastPointer(JSContextRef context, JSValueRef value) { return reinterpret_cast(CYCastPointer_(context, value)); } +SEL CYCastSEL(JSContextRef context, JSValueRef value) { + if (JSValueIsObjectOfClass(context, value, Selector_)) { + Selector_privateData *data(reinterpret_cast(JSObjectGetPrivate((JSObjectRef) value))); + return reinterpret_cast(data->value_); + } else + return CYCastPointer(context, value); +} + void CYPoolFFI(apr_pool_t *pool, JSContextRef context, sig::Type *type, ffi_type *ffi, void *data, JSValueRef value) { switch (type->primitive) { case sig::boolean_P: @@ -1259,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; @@ -1357,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; @@ -1377,52 +1562,75 @@ 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 JSValueRef CYCallFunction(JSContextRef context, size_t count, const JSValueRef *arguments, JSValueRef *exception, sig::Signature *signature, ffi_cif *cif, void (*function)()) { +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 (count != signature->count - 1) + if (setups + count != signature->count - 1) @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"incorrect number of arguments to ffi function" userInfo:nil]; - CYPool pool; - void *values[count]; + size_t size(setups + count); + void *values[size]; + memcpy(values, setup, sizeof(void *) * setups); - for (unsigned index(0); index != count; ++index) { + for (size_t index(setups); index != size; ++index) { sig::Element *element(&signature->elements[index + 1]); ffi_type *ffi(cif->arg_types[index]); // XXX: alignment? values[index] = new(pool) uint8_t[ffi->size]; - CYPoolFFI(pool, context, element->type, ffi, values[index], arguments[index]); + CYPoolFFI(pool, context, element->type, ffi, values[index], arguments[index - setups]); } uint8_t value[cif->rtype->size]; @@ -1486,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]); @@ -1518,65 +1726,83 @@ 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 } -static JSValueRef $objc_msgSend(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { +JSValueRef CYSendMessage(apr_pool_t *pool, JSContextRef context, id self, SEL _cmd, size_t count, const JSValueRef arguments[], JSValueRef *exception) { const char *type; + Class _class(object_getClass(self)); + if (Method method = class_getInstanceMethod(_class, _cmd)) + type = method_getTypeEncoding(method); + else { + CYPoolTry { + NSMethodSignature *method([self methodSignatureForSelector:_cmd]); + if (method == nil) + @throw [NSException exceptionWithName:NSInvalidArgumentException reason:[NSString stringWithFormat:@"unrecognized selector %s sent to object %p", sel_getName(_cmd), self] userInfo:nil]; + type = CYPoolCString(pool, [method _typeString]); + } CYPoolCatch(NULL) + } + + void *setup[2]; + setup[0] = &self; + setup[1] = &_cmd; + + sig::Signature signature; + sig::Parse(pool, &signature, type, &Structor_); + + ffi_cif cif; + sig::sig_ffi_cif(pool, &sig::ObjectiveC, &signature, &cif); + + void (*function)() = stret(cif.rtype) ? reinterpret_cast(&objc_msgSend_stret) : reinterpret_cast(&objc_msgSend); + return CYCallFunction(pool, context, 2, setup, count, arguments, exception, &signature, &cif, function); +} + +static JSValueRef $objc_msgSend(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYPool pool; + id self; + SEL _cmd; + CYTry { if (count < 2) @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"too few arguments to objc_msgSend" userInfo:nil]; - id self(CYCastNSObject(pool, context, arguments[0])); + self = CYCastNSObject(pool, context, arguments[0]); if (self == nil) return CYJSNull(context); - SEL _cmd(CYCastSEL(context, arguments[1])); - - Class _class(object_getClass(self)); - if (Method method = class_getInstanceMethod(_class, _cmd)) - type = method_getTypeEncoding(method); - else { - CYPoolTry { - NSMethodSignature *method([self methodSignatureForSelector:_cmd]); - if (method == nil) - @throw [NSException exceptionWithName:NSInvalidArgumentException reason:[NSString stringWithFormat:@"unrecognized selector %s sent to object %p", sel_getName(_cmd), self] userInfo:nil]; - type = CYPoolCString(pool, [method _typeString]); - } CYPoolCatch(NULL) - } + _cmd = CYCastSEL(context, arguments[1]); } CYCatch - sig::Signature signature; - sig::Parse(pool, &signature, type); - - ffi_cif cif; - sig::sig_ffi_cif(pool, &sig::ObjectiveC, &signature, &cif); - - void (*function)() = stret(cif.rtype) ? reinterpret_cast(&objc_msgSend_stret) : reinterpret_cast(&objc_msgSend); - return CYCallFunction(context, count, arguments, exception, &signature, &cif, function); + return CYSendMessage(pool, context, self, _cmd, count - 2, arguments + 2, exception); } static JSValueRef Selector_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { JSValueRef setup[count + 2]; setup[0] = _this; setup[1] = object; - memmove(setup + 2, arguments, sizeof(JSValueRef) * count); + memcpy(setup + 2, arguments, sizeof(JSValueRef) * count); return $objc_msgSend(context, NULL, NULL, count + 2, setup, exception); } static JSValueRef Functor_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { + CYPool pool; Functor_privateData *data(reinterpret_cast(JSObjectGetPrivate(object))); - return CYCallFunction(context, count, arguments, exception, &data->signature_, &data->cif_, reinterpret_cast(data->value_)); + return CYCallFunction(pool, context, 0, NULL, count, arguments, exception, &data->signature_, &data->cif_, reinterpret_cast(data->value_)); } JSObjectRef Selector_new(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { @@ -1626,6 +1852,15 @@ static JSValueRef Pointer_callAsFunction_toJSON(JSContextRef context, JSObjectRe return Pointer_callAsFunction_valueOf(context, object, _this, count, arguments, exception); } +static JSValueRef Pointer_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { + CYTry { + Pointer_privateData *data(reinterpret_cast(JSObjectGetPrivate(_this))); + char string[32]; + sprintf(string, "%p", data->value_); + return CYCastJSValue(context, string); + } CYCatch +} + static JSValueRef Instance_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { Instance_privateData *data(reinterpret_cast(JSObjectGetPrivate(_this))); @@ -1698,7 +1933,8 @@ static JSStaticValue Pointer_staticValues[2] = { {NULL, NULL, NULL, 0} }; -static JSStaticFunction Pointer_staticFunctions[3] = { +static JSStaticFunction Pointer_staticFunctions[4] = { + {"toCYON", &Pointer_callAsFunction_toCYON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, {"toJSON", &Pointer_callAsFunction_toJSON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, {"valueOf", &Pointer_callAsFunction_valueOf, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, {NULL, NULL, 0} @@ -1788,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);