]> git.saurik.com Git - cycript.git/blobdiff - Library.cpp
Automatically generate FFI bridges using libclang.
[cycript.git] / Library.cpp
index 258ef61d004eb09e5de99307060f00793222a5be..05cdde163c5b482b0681bcf70e764870765a9244 100644 (file)
 **/
 /* }}} */
 
-#include <dlfcn.h>
-
 #include "cycript.hpp"
 
-#include "Pooling.hpp"
-
-#include <sys/mman.h>
-
 #include <iostream>
 #include <set>
 #include <map>
 #include <sstream>
 #include <cmath>
 
-#include "Error.hpp"
-#include "Execute.hpp"
-#include "Parser.hpp"
-#include "String.hpp"
+#include <dlfcn.h>
 
-#include "Cycript.tab.hh"
-#include "Driver.hpp"
+#include <sys/mman.h>
 
+#include "Code.hpp"
 #include "ConvertUTF.h"
+#include "Driver.hpp"
+#include "Error.hpp"
+#include "Execute.hpp"
+#include "Pooling.hpp"
+#include "String.hpp"
+#include "Syntax.hpp"
 
 template <>
 ::pthread_key_t CYLocal<CYPool>::key_ = Key_();
@@ -107,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 ? '\'' : '"');
 
@@ -176,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,24 +226,31 @@ double CYCastDouble(const char *value) {
     return CYCastDouble(value, strlen(value));
 }
 
-CYUTF8String CYPoolCode(CYPool &pool, std::istream &stream) {
+_visible bool CYStartsWith(const CYUTF8String &haystack, const CYUTF8String &needle) {
+    return haystack.size >= needle.size && strncmp(haystack.data, needle.data, needle.size) == 0;
+}
+
+CYUTF8String CYPoolCode(CYPool &pool, std::streambuf &stream) {
     CYLocalPool local;
     CYDriver driver(local, stream);
-
-    cy::parser parser(driver);
-    _assert(parser.parse() == 0);
+    _assert(!driver.Parse());
     _assert(driver.errors_.empty());
 
     CYOptions options;
     CYContext context(options);
-    driver.program_->Replace(context);
+    driver.script_->Replace(context);
 
     std::stringbuf str;
     CYOutput out(str, options);
-    out << *driver.program_;
+    out << *driver.script_;
     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;