]> git.saurik.com Git - cycript.git/blobdiff - Execute.cpp
Implement internal multiply (for the compiler).
[cycript.git] / Execute.cpp
index aff9a7e50cda572da73bb5bdd723d232112f29b6..898cd14e29d93f7c31b57a4dee0fd8dcb14d4754 100644 (file)
@@ -127,6 +127,7 @@ static JSClassRef Struct_;
 
 JSStringRef Array_s;
 JSStringRef cy_s;
+JSStringRef cyi_s;
 JSStringRef length_s;
 JSStringRef message_s;
 JSStringRef name_s;
@@ -341,8 +342,14 @@ static JSValueRef $cyq(JSContextRef context, JSObjectRef object, JSObjectRef _th
     return CYCastJSValue(context, name);
 } CYCatch(NULL) }
 
+static void (*JSSynchronousGarbageCollectForDebugging$)(JSContextRef);
+
+void CYGarbageCollect(JSContextRef context) {
+    (JSSynchronousGarbageCollectForDebugging$ ?: &JSGarbageCollect)(context);
+}
+
 static JSValueRef Cycript_gc_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
-    JSGarbageCollect(context);
+    CYGarbageCollect(context);
     return CYJSUndefined(context);
 } CYCatch(NULL) }
 
@@ -822,9 +829,9 @@ static JSValueRef Pointer_getProperty(JSContextRef context, JSObjectRef object,
         return internal->length_ == _not(size_t) ? CYJSUndefined(context) : CYCastJSValue(context, internal->length_);
 
     Type_privateData *typical(internal->type_);
-
     if (typical->type_ == NULL)
         return NULL;
+    sig::Type &type(*typical->type_);
 
     ssize_t offset;
     if (JSStringIsEqualToUTF8CString(property, "$cyi"))
@@ -832,13 +839,16 @@ static JSValueRef Pointer_getProperty(JSContextRef context, JSObjectRef object,
     else if (!CYGetOffset(pool, context, property, offset))
         return NULL;
 
+    if (type.primitive == sig::function_P)
+        return CYMakeFunctor(context, reinterpret_cast<void (*)()>(internal->value_), type.data.signature);
+
     ffi_type *ffi(typical->GetFFI());
 
     uint8_t *base(reinterpret_cast<uint8_t *>(internal->value_));
     base += ffi->size * offset;
 
     JSObjectRef owner(internal->GetOwner() ?: object);
-    return CYFromFFI(context, typical->type_, ffi, base, false, owner);
+    return CYFromFFI(context, &type, ffi, base, false, owner);
 } CYCatch(NULL) }
 
 static bool Pointer_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) { CYTry {
@@ -1244,13 +1254,26 @@ static JSValueRef Pointer_callAsFunction_toCYON(JSContextRef context, JSObjectRe
         JSObjectRef Array(CYGetCachedObject(context, CYJSString("Array_prototype")));
         JSObjectRef toCYON(CYCastJSObject(context, CYGetProperty(context, Array, toCYON_s)));
         return CYCallAsFunction(context, toCYON, _this, count, arguments);
-    } else {
+    } else if (internal->type_->type_ == NULL) pointer: {
         char string[32];
         sprintf(string, "%p", internal->value_);
         return CYCastJSValue(context, string);
+    } try {
+        JSValueRef value(CYGetProperty(context, _this, cyi_s));
+        if (JSValueIsUndefined(context, value))
+            goto pointer;
+        CYPool pool;
+        return CYCastJSValue(context, pool.strcat("&", CYPoolCCYON(pool, context, value), NULL));
+    } catch (const CYException &e) {
+        goto pointer;
     }
 } CYCatch(NULL) }
 
+static JSValueRef Pointer_getProperty_type(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
+    Pointer *internal(reinterpret_cast<Pointer *>(JSObjectGetPrivate(object)));
+    return CYMakeType(context, internal->type_->type_);
+} CYCatch(NULL) }
+
 static JSValueRef Functor_getProperty_type(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
     cy::Functor *internal(reinterpret_cast<cy::Functor *>(JSObjectGetPrivate(object)));
     return CYMakeType(context, &internal->signature_);
@@ -1299,6 +1322,11 @@ static JSStaticFunction Pointer_staticFunctions[4] = {
     {NULL, NULL, 0}
 };
 
+static JSStaticValue Pointer_staticValues[2] = {
+    {"type", &Pointer_getProperty_type, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
+    {NULL, NULL, NULL, 0}
+};
+
 static JSStaticFunction Struct_staticFunctions[2] = {
     {"$cya", &Struct_callAsFunction_$cya, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
     {NULL, NULL, 0}
@@ -1365,19 +1393,33 @@ JSObjectRef CYGetGlobalObject(JSContextRef context) {
     return JSContextGetGlobalObject(context);
 }
 
+class ExecutionHandle {
+  private:
+    JSContextRef context_;
+    void *handle_;
+
+  public:
+    ExecutionHandle(JSContextRef context) :
+        context_(context)
+    {
+        if (hooks_ != NULL && hooks_->ExecuteStart != NULL)
+            handle_ = (*hooks_->ExecuteStart)(context_);
+        else
+            handle_ = NULL;
+    }
+
+    ~ExecutionHandle() {
+        if (hooks_ != NULL && hooks_->ExecuteEnd != NULL)
+            (*hooks_->ExecuteEnd)(context_, handle_);
+    }
+};
+
 const char *CYExecute(JSContextRef context, CYPool &pool, CYUTF8String code) {
     JSValueRef exception(NULL);
 
-    void *handle;
-    if (hooks_ != NULL && hooks_->ExecuteStart != NULL)
-        handle = (*hooks_->ExecuteStart)(context);
-    else
-        handle = NULL;
-
-    try {
+    ExecutionHandle handle(context);
 
-    JSValueRef result;
-    try {
+    JSValueRef result; try {
         result = JSEvaluateScript(context, CYJSString(code), NULL, NULL, 0, &exception);
     } catch (const char *error) {
         return error;
@@ -1389,8 +1431,7 @@ const char *CYExecute(JSContextRef context, CYPool &pool, CYUTF8String code) {
     if (JSValueIsUndefined(context, result))
         return NULL;
 
-    const char *json;
-    try {
+    const char *json; try {
         json = CYPoolCCYON(pool, context, result, &exception);
     } catch (const char *error) {
         return error;
@@ -1402,16 +1443,6 @@ const char *CYExecute(JSContextRef context, CYPool &pool, CYUTF8String code) {
     CYSetProperty(context, CYGetGlobalObject(context), Result_, result);
 
     return json;
-
-    } catch (...) {
-        if (hooks_ != NULL && hooks_->ExecuteEnd != NULL)
-            (*hooks_->ExecuteEnd)(context, handle);
-        throw;
-    }
-}
-
-extern "C" void CydgetSetupContext(JSGlobalContextRef context) {
-    CYSetupContext(context);
 }
 
 static bool initialized_ = false;
@@ -1422,6 +1453,7 @@ void CYInitializeDynamic() {
     else return;
 
     JSObjectMakeArray$ = reinterpret_cast<JSObjectRef (*)(JSContextRef, size_t, const JSValueRef[], JSValueRef *)>(dlsym(RTLD_DEFAULT, "JSObjectMakeArray"));
+    JSSynchronousGarbageCollectForDebugging$ = reinterpret_cast<void (*)(JSContextRef)>(dlsym(RTLD_DEFAULT, "JSSynchronousGarbageCollectForDebugging"));
 
     JSClassDefinition definition;
 
@@ -1447,6 +1479,7 @@ void CYInitializeDynamic() {
     definition = kJSClassDefinitionEmpty;
     definition.className = "Pointer";
     definition.staticFunctions = Pointer_staticFunctions;
+    definition.staticValues = Pointer_staticValues;
     definition.getProperty = &Pointer_getProperty;
     definition.setProperty = &Pointer_setProperty;
     definition.finalize = &CYFinalize;
@@ -1477,6 +1510,7 @@ void CYInitializeDynamic() {
 
     Array_s = JSStringCreateWithUTF8CString("Array");
     cy_s = JSStringCreateWithUTF8CString("$cy");
+    cyi_s = JSStringCreateWithUTF8CString("$cyi");
     length_s = JSStringCreateWithUTF8CString("length");
     message_s = JSStringCreateWithUTF8CString("message");
     name_s = JSStringCreateWithUTF8CString("name");
@@ -1590,8 +1624,10 @@ static void CYRunSetups(JSContextRef context) {
         CYUTF16String utf16(CYPoolUTF16String(pool, utf8));
         munmap(const_cast<char *>(utf8.data), utf8.size);
 
-        if (CydgetMemoryParse(&utf16.data, &utf16.size))
-            CYExecute(context, pool, CYPoolUTF8String(pool, utf16));
+        // XXX: this should not be used
+        CydgetMemoryParse(&utf16.data, &utf16.size);
+
+        CYExecute(context, pool, CYPoolUTF8String(pool, utf16));
         free(const_cast<uint16_t *>(utf16.data));
     }
 
@@ -1741,11 +1777,11 @@ extern "C" void CYSetupContext(JSGlobalContextRef context) {
     CYRunSetups(context);
 }
 
+static JSGlobalContextRef context_;
+
 JSGlobalContextRef CYGetJSContext() {
     CYInitializeDynamic();
 
-    static JSGlobalContextRef context_;
-
     if (context_ == NULL) {
         context_ = JSGlobalContextCreate(Global_);
         CYSetupContext(context_);
@@ -1753,3 +1789,10 @@ JSGlobalContextRef CYGetJSContext() {
 
     return context_;
 }
+
+void CYDestroyContext() {
+    if (context_ == NULL)
+        return;
+    JSGlobalContextRelease(context_);
+    context_ = NULL;
+}