]> git.saurik.com Git - cycript.git/blobdiff - Library.cpp
Automatically generate FFI bridges using libclang.
[cycript.git] / Library.cpp
index 59decd8351cd0b4e4f463e2b1d1ea7c2238b55cb..05cdde163c5b482b0681bcf70e764870765a9244 100644 (file)
@@ -32,6 +32,7 @@
 
 #include <sys/mman.h>
 
+#include "Code.hpp"
 #include "ConvertUTF.h"
 #include "Driver.hpp"
 #include "Error.hpp"
@@ -103,15 +104,20 @@ bool CYGetOffset(const char *value, ssize_t &index) {
 }
 /* }}} */
 /* JavaScript *ify {{{ */
-void CYStringify(std::ostringstream &str, const char *data, size_t size) {
-    unsigned quot(0), apos(0);
-    for (const char *value(data), *end(data + size); value != end; ++value)
-        if (*value == '"')
-            ++quot;
-        else if (*value == '\'')
-            ++apos;
-
-    bool single(quot > apos);
+void CYStringify(std::ostringstream &str, const char *data, size_t size, bool c) {
+    bool single;
+    if (c)
+        single = false;
+    else {
+        unsigned quot(0), apos(0);
+        for (const char *value(data), *end(data + size); value != end; ++value)
+            if (*value == '"')
+                ++quot;
+            else if (*value == '\'')
+                ++apos;
+
+        single = quot > apos;
+    }
 
     str << (single ? '\'' : '"');
 
@@ -172,6 +178,13 @@ void CYStringify(std::ostringstream &str, const char *data, size_t size) {
 }
 
 void CYNumerify(std::ostringstream &str, double value) {
+    if (std::isinf(value)) {
+        if (value < 0)
+            str << '-';
+        str << "Infinity";
+        return;
+    }
+
     char string[32];
     // XXX: I want this to print 1e3 rather than 1000
     sprintf(string, "%.17g", value);
@@ -217,7 +230,7 @@ _visible bool CYStartsWith(const CYUTF8String &haystack, const CYUTF8String &nee
     return haystack.size >= needle.size && strncmp(haystack.data, needle.data, needle.size) == 0;
 }
 
-CYUTF8String CYPoolCode(CYPool &pool, std::istream &stream) {
+CYUTF8String CYPoolCode(CYPool &pool, std::streambuf &stream) {
     CYLocalPool local;
     CYDriver driver(local, stream);
     _assert(!driver.Parse());
@@ -233,6 +246,11 @@ CYUTF8String CYPoolCode(CYPool &pool, std::istream &stream) {
     return $pool.strdup(str.str().c_str());
 }
 
+CYUTF8String CYPoolCode(CYPool &pool, CYUTF8String code) {
+    CYStream stream(code.data, code.data + code.size);
+    return CYPoolCode(pool, stream);
+}
+
 CYPool &CYGetGlobalPool() {
     static CYPool pool;
     return pool;