]> git.saurik.com Git - cycript.git/blobdiff - Execute.cpp
CYDisplay{Start,Finish} obsolete by rl_redisplay.
[cycript.git] / Execute.cpp
index 87e7de246736daf69b74a0e1cabdd0e552ea741b..7641df789d2e7b62eec9bdac6cc04a1d510c2458 100644 (file)
@@ -22,6 +22,9 @@
 #include "Internal.hpp"
 
 #include <dlfcn.h>
+#include <dirent.h>
+#include <fcntl.h>
+#include <unistd.h>
 
 #include "cycript.hpp"
 
@@ -32,6 +35,7 @@
 #include "Execute.hpp"
 
 #include <sys/mman.h>
+#include <sys/stat.h>
 
 #include <iostream>
 #include <set>
@@ -451,18 +455,21 @@ static JSValueRef Array_callAsFunction_toCYON(JSContextRef context, JSObjectRef
     bool comma(false);
 
     for (size_t index(0), count(CYCastDouble(context, length)); index != count; ++index) {
-        JSValueRef value(CYGetProperty(context, _this, index));
-
         if (comma)
             str << ',';
         else
             comma = true;
 
-        if (!JSValueIsUndefined(context, value))
-            str << CYPoolCCYON(pool, context, value);
-        else {
-            str << ',';
-            comma = false;
+        try {
+            JSValueRef value(CYGetProperty(context, _this, index));
+            if (!JSValueIsUndefined(context, value))
+                str << CYPoolCCYON(pool, context, value);
+            else {
+                str << ',';
+                comma = false;
+            }
+        } catch (const CYException &error) {
+            str << "@error";
         }
     }
 
@@ -1291,9 +1298,8 @@ JSObjectRef CYGetGlobalObject(JSContextRef context) {
     return JSContextGetGlobalObject(context);
 }
 
-const char *CYExecute(CYPool &pool, CYUTF8String code) {
-    JSContextRef context(CYGetJSContext());
-    JSValueRef exception(NULL), result;
+const char *CYExecute(JSContextRef context, CYPool &pool, CYUTF8String code) {
+    JSValueRef exception(NULL);
 
     void *handle;
     if (hooks_ != NULL && hooks_->ExecuteStart != NULL)
@@ -1301,8 +1307,9 @@ const char *CYExecute(CYPool &pool, CYUTF8String code) {
     else
         handle = NULL;
 
-    const char *json;
+    try {
 
+    JSValueRef result;
     try {
         result = JSEvaluateScript(context, CYJSString(code), NULL, NULL, 0, &exception);
     } catch (const char *error) {
@@ -1315,6 +1322,7 @@ const char *CYExecute(CYPool &pool, CYUTF8String code) {
     if (JSValueIsUndefined(context, result))
         return NULL;
 
+    const char *json;
     try {
         json = CYPoolCCYON(pool, context, result, &exception);
     } catch (const char *error) {
@@ -1326,9 +1334,13 @@ const char *CYExecute(CYPool &pool, CYUTF8String code) {
 
     CYSetProperty(context, CYGetGlobalObject(context), Result_, result);
 
-    if (hooks_ != NULL && hooks_->ExecuteEnd != NULL)
-        (*hooks_->ExecuteEnd)(context, handle);
     return json;
+
+    } catch (...) {
+        if (hooks_ != NULL && hooks_->ExecuteEnd != NULL)
+            (*hooks_->ExecuteEnd)(context, handle);
+        throw;
+    }
 }
 
 extern "C" void CydgetSetupContext(JSGlobalContextRef context) {
@@ -1459,6 +1471,66 @@ JSGlobalContextRef CYGetJSContext(JSContextRef context) {
     return reinterpret_cast<Context *>(JSObjectGetPrivate(CYCastJSObject(context, CYGetProperty(context, CYGetGlobalObject(context), cy_s))))->context_;
 }
 
+extern "C" bool CydgetMemoryParse(const uint16_t **data, size_t *size);
+
+void *CYMapFile(const char *path, size_t *psize) {
+    int fd;
+    _syscall(fd = open(path, O_RDONLY));
+
+    struct stat stat;
+    _syscall(fstat(fd, &stat));
+    size_t size(stat.st_size);
+
+    *psize = size;
+
+    void *base;
+    _syscall(base = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0));
+
+    _syscall(close(fd));
+    return base;
+}
+
+static void CYRunSetups(JSContextRef context) {
+    std::string folder("/etc/cycript/setup.d");
+    DIR *setups(opendir(folder.c_str()));
+    if (setups == NULL)
+        return;
+
+    for (;;) {
+        dirent setup;
+        dirent *result;
+        _syscall(readdir_r(setups, &setup, &result));
+
+        if (result == NULL)
+            break;
+        _assert(result == &setup);
+
+        const char *name(setup.d_name);
+        size_t length(strlen(name));
+        if (length < 4)
+            continue;
+
+        if (name[0] == '.')
+            continue;
+        if (memcmp(name + length - 3, ".cy", 3) != 0)
+            continue;
+
+        std::string script(folder + "/" + name);
+        CYUTF8String utf8;
+        utf8.data = reinterpret_cast<char *>(CYMapFile(script.c_str(), &utf8.size));
+
+        CYPool pool;
+        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));
+        free(const_cast<uint16_t *>(utf16.data));
+    }
+
+    _syscall(closedir(setups));
+}
+
 extern "C" void CYSetupContext(JSGlobalContextRef context) {
     CYInitializeDynamic();
 
@@ -1548,6 +1620,8 @@ extern "C" void CYSetupContext(JSGlobalContextRef context) {
         (*hooks_->SetupContext)(context);
 
     CYArrayPush(context, alls, cycript);
+
+    CYRunSetups(context);
 }
 
 JSGlobalContextRef CYGetJSContext() {