X-Git-Url: https://git.saurik.com/cycript.git/blobdiff_plain/2b1c9594666c525586f1956c064d60ea2e7a0a3b..90dd6ff11965499c5a211e2b78755baa3ab2e09c:/Execute.cpp diff --git a/Execute.cpp b/Execute.cpp index 340a7fa..c6812a2 100644 --- a/Execute.cpp +++ b/Execute.cpp @@ -297,6 +297,7 @@ JSValueRef CYCastJSValue(JSContextRef context, double value) { #define CYCastJSValue_(Type_) \ JSValueRef CYCastJSValue(JSContextRef context, Type_ value) { \ + _assert(static_cast(static_cast(value)) == value); \ return JSValueMakeNumber(context, static_cast(value)); \ } @@ -1092,6 +1093,14 @@ static JSValueRef Functor_callAsFunction(JSContextRef context, JSObjectRef objec return CYCallFunction(pool, context, 0, NULL, count, arguments, false, &internal->signature_, &internal->cif_, internal->GetValue()); } CYCatch(NULL) } +static JSValueRef Pointer_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { + Pointer *internal(reinterpret_cast(JSObjectGetPrivate(object))); + if (internal->type_->type_->primitive != sig::function_P) + throw CYJSError(context, "cannot call a pointer to non-function"); + JSObjectRef functor(CYCastJSObject(context, CYGetProperty(context, object, cyi_s))); + return CYCallAsFunction(context, functor, _this, count, arguments); +} CYCatch(NULL) } + JSObjectRef CYMakeType(JSContextRef context, const char *encoding) { Type_privateData *internal(new Type_privateData(encoding)); return JSObjectMake(context, Type_privateData::Class_, internal); @@ -1510,7 +1519,7 @@ static JSValueRef CString_callAsFunction_toPointer(JSContextRef context, JSObjec return CYMakePointer(context, internal->value_, _not(size_t), &type, NULL, NULL); } CYCatch(NULL) } -static JSValueRef Functor_callAsFunction_toPointer(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { +static JSValueRef Functor_callAsFunction_$cya(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { CYPool pool; cy::Functor *internal(reinterpret_cast(JSObjectGetPrivate(_this))); @@ -1539,9 +1548,20 @@ static JSValueRef CYValue_callAsFunction_toJSON(JSContextRef context, JSObjectRe static JSValueRef CYValue_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { CYValue *internal(reinterpret_cast(JSObjectGetPrivate(_this))); - char string[32]; - sprintf(string, "%p", internal->value_); - return CYCastJSValue(context, string); + std::ostringstream str; + Dl_info info; + if (internal->value_ == NULL) + str << "NULL"; + else if (dladdr(internal->value_, &info) == 0) + str << internal->value_; + else { + str << info.dli_sname; + off_t offset(static_cast(internal->value_) - static_cast(info.dli_saddr)); + if (offset != 0) + str << "+0x" << std::hex << offset; + } + std::string value(str.str()); + return CYCastJSValue(context, CYJSString(CYUTF8String(value.c_str(), value.size()))); } CYCatch(NULL) } static JSValueRef Pointer_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { @@ -1610,7 +1630,7 @@ static JSValueRef CString_callAsFunction_toCYON(JSContextRef context, JSObjectRe const char *string(static_cast(internal->value_)); std::ostringstream str; str << "&"; - CYStringify(str, string, strlen(string)); + CYStringify(str, string, strlen(string), true); std::string value(str.str()); return CYCastJSValue(context, CYJSString(CYUTF8String(value.c_str(), value.size()))); } CYCatch(NULL) } @@ -1696,9 +1716,9 @@ static JSStaticFunction Struct_staticFunctions[2] = { }; static JSStaticFunction Functor_staticFunctions[5] = { + {"$cya", &Functor_callAsFunction_$cya, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, {"toCYON", &CYValue_callAsFunction_toCYON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, {"toJSON", &CYValue_callAsFunction_toJSON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, - {"toPointer", &Functor_callAsFunction_toPointer, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, {"valueOf", &CYValue_callAsFunction_valueOf, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, {NULL, NULL, 0} }; @@ -1871,6 +1891,7 @@ void CYInitializeDynamic() { definition.className = "Pointer"; definition.staticFunctions = Pointer_staticFunctions; definition.staticValues = Pointer_staticValues; + definition.callAsFunction = &Pointer_callAsFunction; definition.getProperty = &Pointer_getProperty; definition.setProperty = &Pointer_setProperty; definition.finalize = &CYFinalize; @@ -2071,7 +2092,13 @@ static JSValueRef require(JSContextRef context, JSObjectRef object, JSObjectRef CYCallAsFunction(context, function, NULL, 3, arguments); } - return CYGetProperty(context, module, property); + JSObjectRef exports(CYCastJSObject(context, CYGetProperty(context, module, property))); + + CYJSString _default("default"); + if (JSValueIsUndefined(context, CYGetProperty(context, exports, _default))) + CYSetProperty(context, exports, _default, exports, kJSPropertyAttributeDontEnum); + + return exports; } CYCatch(NULL) } static bool CYRunScript(JSGlobalContextRef context, const char *path) {