From: Jay Freeman (saurik) Date: Fri, 8 Jan 2016 21:15:49 +0000 (-0800) Subject: Work around iOS "" for Message's toCYON. X-Git-Tag: v0.9.590~30 X-Git-Url: https://git.saurik.com/cycript.git/commitdiff_plain/6419a40fb7c8e1c92e2bb7a6691f071fbfa76e77?ds=inline Work around iOS "" for Message's toCYON. --- diff --git a/Execute.cpp b/Execute.cpp index c068780..57c812f 100644 --- a/Execute.cpp +++ b/Execute.cpp @@ -189,7 +189,7 @@ JSObjectRef CYObjectMakeArray(JSContextRef context, size_t length, const JSValue } static JSClassRef All_; -JSClassRef Functor_; +JSClassRef cy::Functor::Class_; static JSClassRef Global_; JSStringRef Array_s; @@ -657,7 +657,7 @@ JSObjectRef CYMakePointer(JSContextRef context, void *pointer, const sig::Type & static JSValueRef CYMakeFunctor(JSContextRef context, void (*function)(), bool variadic, const sig::Signature &signature) { if (function == NULL) return CYJSNull(context); - return JSObjectMake(context, Functor_, new cy::Functor(function, variadic, signature)); + return JSObjectMake(context, cy::Functor::Class_, new cy::Functor(function, variadic, signature)); } // XXX: remove this, as it is really stupid @@ -668,7 +668,7 @@ static JSObjectRef CYMakeFunctor(JSContextRef context, const char *symbol, const cy::Functor *internal(new cy::Functor(function, encoding)); ++internal->count_; - return JSObjectMake(context, Functor_, internal); + return JSObjectMake(context, cy::Functor::Class_, internal); } bool CYGetOffset(CYPool &pool, JSContextRef context, JSStringRef value, ssize_t &index) { @@ -976,7 +976,7 @@ Closure_privateData *CYMakeFunctor_(JSContextRef context, JSObjectRef function, static JSObjectRef CYMakeFunctor(JSContextRef context, JSObjectRef function, const sig::Signature &signature) { Closure_privateData *internal(CYMakeFunctor_(context, function, signature, &FunctionAdapter_)); - JSObjectRef object(JSObjectMake(context, Functor_, internal)); + JSObjectRef object(JSObjectMake(context, cy::Functor::Class_, internal)); // XXX: see above notes about needing to leak JSValueProtect(CYGetJSContext(context), object); return object; @@ -1768,22 +1768,8 @@ static JSValueRef Functor_callAsFunction_toCYON(JSContextRef context, JSObjectRe sig::Function function(internal->variadic_); sig::Copy(pool, function.signature, internal->signature_); - CYPropertyName *name; - - auto typed(CYDecodeType(pool, &function)); { - std::ostringstream str; - Dl_info info; - if (dladdr(value, &info) == 0) { - str << (void *) value; - name = new(pool) CYNumber(reinterpret_cast(value)); - } else { - str << info.dli_sname; - off_t offset(value - reinterpret_cast(info.dli_saddr)); - if (offset != 0) - str << "+0x" << std::hex << offset; - name = new(pool) CYString(pool.strdup(str.str().c_str())); - } - } + CYPropertyName *name(internal->GetName(pool)); + auto typed(CYDecodeType(pool, &function)); std::ostringstream str; CYOptions options; @@ -1793,6 +1779,19 @@ static JSValueRef Functor_callAsFunction_toCYON(JSContextRef context, JSObjectRe return CYCastJSValue(context, CYJSString(str.str())); } CYCatch(NULL) } +CYPropertyName *cy::Functor::GetName(CYPool &pool) const { + Dl_info info; + if (dladdr(reinterpret_cast(value_), &info) == 0 || strcmp(info.dli_sname, "") == 0) + return new(pool) CYNumber(reinterpret_cast(value_)); + + std::ostringstream str; + str << info.dli_sname; + off_t offset(reinterpret_cast(value_) - reinterpret_cast(info.dli_saddr)); + if (offset != 0) + str << "+0x" << std::hex << offset; + return new(pool) CYString(pool.strdup(str.str().c_str())); +} + static JSValueRef Pointer_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { std::set *objects(CYCastObjects(context, _this, count, arguments)); @@ -1962,19 +1961,11 @@ static JSStaticFunction Functor_staticFunctions[4] = { {NULL, NULL, 0} }; -namespace cy { - JSStaticFunction const * const Functor::StaticFunctions = Functor_staticFunctions; -} - static JSStaticValue Functor_staticValues[2] = { {"$cyt", &Functor_getProperty_$cyt, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, {NULL, NULL, NULL, 0} }; -namespace cy { - JSStaticValue const * const Functor::StaticValues = Functor_staticValues; -} - static JSStaticValue Type_staticValues[4] = { {"alignment", &Type_getProperty_alignment, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, {"name", &Type_getProperty_name, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, @@ -2137,11 +2128,11 @@ void CYInitializeDynamic() { definition = kJSClassDefinitionEmpty; definition.className = "Functor"; - definition.staticFunctions = cy::Functor::StaticFunctions; + definition.staticFunctions = Functor_staticFunctions; definition.staticValues = Functor_staticValues; definition.callAsFunction = &Functor_callAsFunction; definition.finalize = &CYFinalize; - Functor_ = JSClassCreate(&definition); + cy::Functor::Class_ = JSClassCreate(&definition); definition = kJSClassDefinitionEmpty; definition.className = "Pointer"; @@ -2462,8 +2453,10 @@ extern "C" void CYSetupContext(JSGlobalContextRef context) { CYSetPrototype(context, CYCastJSObject(context, CYGetProperty(context, CString, prototype_s)), String_prototype); CYSetProperty(context, cycript, CYJSString("CString"), CString); - JSObjectRef Functor(JSObjectMakeConstructor(context, Functor_, &Functor_new)); - CYSetPrototype(context, CYCastJSObject(context, CYGetProperty(context, Functor, prototype_s)), Function_prototype); + JSObjectRef Functor(JSObjectMakeConstructor(context, cy::Functor::Class_, &Functor_new)); + JSObjectRef Functor_prototype(CYCastJSObject(context, CYGetProperty(context, Functor, prototype_s))); + CYSetPrototype(context, Functor_prototype, Function_prototype); + CYSetProperty(context, cy, CYJSString("Functor_prototype"), Functor_prototype); CYSetProperty(context, cycript, CYJSString("Functor"), Functor); CYSetProperty(context, cycript, CYJSString("Pointer"), JSObjectMakeConstructor(context, CYPrivate::Class_, &Pointer_new)); diff --git a/Internal.hpp b/Internal.hpp index 8c7bc19..4e37d7a 100644 --- a/Internal.hpp +++ b/Internal.hpp @@ -34,11 +34,11 @@ #include "Pooling.hpp" #include "Utility.hpp" +struct CYPropertyName; + JSGlobalContextRef CYGetJSContext(JSContextRef context); sig::Type *Structor_(CYPool &pool, sig::Aggregate *aggregate); -extern JSClassRef Functor_; - struct CYRoot : CYData { @@ -202,6 +202,9 @@ namespace cy { struct Functor : CYRoot { + public: + static JSClassRef Class_; + private: void set() { sig::sig_ffi_cif(*pool_, variadic_ ? signature_.count : 0, signature_, &cif_); @@ -229,8 +232,7 @@ struct Functor : set(); } - static JSStaticFunction const * const StaticFunctions; - static JSStaticValue const * const StaticValues; + virtual CYPropertyName *GetName(CYPool &pool) const; }; } struct Closure_privateData : diff --git a/ObjectiveC/Library.mm b/ObjectiveC/Library.mm index 431da0d..7392a2e 100644 --- a/ObjectiveC/Library.mm +++ b/ObjectiveC/Library.mm @@ -357,6 +357,8 @@ struct Message_privateData : { } + virtual CYPropertyName *GetName(CYPool &pool) const; + static JSObjectRef Make(JSContextRef context, SEL sel, const char *type, IMP value); }; @@ -1592,7 +1594,9 @@ static JSValueRef MessageAdapter_(JSContextRef context, size_t count, JSValueRef JSObjectRef Message_privateData::Make(JSContextRef context, SEL sel, const char *type, IMP value) { Message_privateData *internal(new Message_privateData(sel, type, value)); - return JSObjectMake(context, Message_privateData::Class_, internal); + JSObjectRef object(JSObjectMake(context, Message_privateData::Class_, internal)); + CYSetPrototype(context, object, CYGetCachedValue(context, CYJSString("Functor_prototype"))); + return object; } static IMP CYMakeMessage(JSContextRef context, JSValueRef value, const char *encoding) { @@ -2567,6 +2571,10 @@ static JSValueRef Message_callAsFunction(JSContextRef context, JSObjectRef objec return CYCallFunction(pool, context, 2, setup, count, arguments, false, true, internal->signature_, &internal->cif_, internal->value_); } CYCatch(NULL) } +CYPropertyName *Message_privateData::GetName(CYPool &pool) const { + return new(pool) CYString(pool.strcat(":", sel_getName(sel_), NULL)); +} + static JSObjectRef Super_new(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { if (count != 2) throw CYJSError(context, "incorrect number of arguments to objc_super constructor"); @@ -2921,8 +2929,7 @@ void CYObjectiveC_Initialize() { /*XXX*/ JSContextRef context(NULL); CYPoolTry { definition = kJSClassDefinitionEmpty; definition.className = "Message"; - definition.staticFunctions = cy::Functor::StaticFunctions; - definition.staticValues = cy::Functor::StaticValues; + definition.parentClass = cy::Functor::Class_; definition.callAsFunction = &Message_callAsFunction; definition.finalize = &CYFinalize; Message_privateData::Class_ = JSClassCreate(&definition);