]> git.saurik.com Git - cycript.git/blobdiff - Execute.cpp
Allow extern "C" {} and limit semicolon insertion.
[cycript.git] / Execute.cpp
index 1a3a2df9a2fabcdfd917de91c0904eff6a30c8b8..3d9d6c4c4f9a52b0308422d5d509ef48f0ee051a 100644 (file)
@@ -1093,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<Pointer *>(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);
@@ -1511,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<cy::Functor *>(JSObjectGetPrivate(_this)));
 
@@ -1540,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<CYValue *>(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<char *>(internal->value_) - static_cast<char *>(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 {
@@ -1697,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}
 };
@@ -1872,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;