]> git.saurik.com Git - cycript.git/blobdiff - Library.mm
Locations, array literal elisions, and fixed prompting.
[cycript.git] / Library.mm
index 4dd24143afdea9eb195a4e4c067740ddda0276e9..2dc719e83b69613868929803b2014616903b2cce 100644 (file)
@@ -45,8 +45,7 @@
 #include "sig/parse.hpp"
 #include "sig/ffi_type.hpp"
 
-#include <apr-1/apr_pools.h>
-#include <apr-1/apr_strings.h>
+#include "Pooling.hpp"
 
 #include <unistd.h>
 
 #include <ext/stdio_filebuf.h>
 #include <set>
 #include <map>
+#include <sstream>
 
 #include "Parser.hpp"
+#include "Cycript.tab.hh"
 
 #undef _assert
 #undef _trace
     CFLog(kCFLogLevelNotice, CFSTR("_trace():%u"), __LINE__); \
 } while (false)
 
-/* APR Pool Helpers {{{ */
-void *operator new(size_t size, apr_pool_t *pool) {
-    return apr_palloc(pool, size);
-}
-
-void *operator new [](size_t size, apr_pool_t *pool) {
-    return apr_palloc(pool, size);
-}
-
-class CYPool {
-  private:
-    apr_pool_t *pool_;
-
-  public:
-    CYPool() {
-        apr_pool_create(&pool_, NULL);
-    }
-
-    ~CYPool() {
-        apr_pool_destroy(pool_);
-    }
-
-    operator apr_pool_t *() const {
-        return pool_;
-    }
-
-    char *operator ()(const char *data) const {
-        return apr_pstrdup(pool_, data);
-    }
-
-    char *operator ()(const char *data, size_t size) const {
-        return apr_pstrndup(pool_, data, size);
-    }
-};
-/* }}} */
 
 #define _pooled _H<NSAutoreleasePool> _pool([[NSAutoreleasePool alloc] init], true);
 
@@ -348,22 +314,22 @@ JSStringRef CYCopyJSString(JSContextRef context, JSValueRef value) {
 }
 
 // XXX: this is not a safe handle
-class CYString {
+class CYJSString {
   private:
     JSStringRef string_;
 
   public:
     template <typename Arg0_>
-    CYString(Arg0_ arg0) {
+    CYJSString(Arg0_ arg0) {
         string_ = CYCopyJSString(arg0);
     }
 
     template <typename Arg0_, typename Arg1_>
-    CYString(Arg0_ arg0, Arg1_ arg1) {
+    CYJSString(Arg0_ arg0, Arg1_ arg1) {
         string_ = CYCopyJSString(arg0, arg1);
     }
 
-    ~CYString() {
+    ~CYJSString() {
         JSStringRelease(string_);
     }
 
@@ -377,7 +343,7 @@ CFStringRef CYCopyCFString(JSStringRef value) {
 }
 
 CFStringRef CYCopyCFString(JSContextRef context, JSValueRef value) {
-    return CYCopyCFString(CYString(context, value));
+    return CYCopyCFString(CYJSString(context, value));
 }
 
 double CYCastDouble(JSContextRef context, JSValueRef value) {
@@ -460,7 +426,7 @@ void CYThrow(JSContextRef context, id error, JSValueRef *exception) {
 
 - (id) objectForKey:(id)key {
     JSValueRef exception(NULL);
-    JSValueRef value(JSObjectGetProperty(context_, object_, CYString(key), &exception));
+    JSValueRef value(JSObjectGetProperty(context_, object_, CYJSString(key), &exception));
     CYThrow(context_, exception);
     return CYCastNSObject(context_, value);
 }
@@ -474,14 +440,14 @@ void CYThrow(JSContextRef context, id error, JSValueRef *exception) {
 
 - (void) setObject:(id)object forKey:(id)key {
     JSValueRef exception(NULL);
-    JSObjectSetProperty(context_, object_, CYString(key), CYCastJSValue(context_, object), kJSPropertyAttributeNone, &exception);
+    JSObjectSetProperty(context_, object_, CYJSString(key), CYCastJSValue(context_, object), kJSPropertyAttributeNone, &exception);
     CYThrow(context_, exception);
 }
 
 - (void) removeObjectForKey:(id)key {
     JSValueRef exception(NULL);
     // XXX: this returns a bool... throw exception, or ignore?
-    JSObjectDeleteProperty(context_, object_, CYString(key), &exception);
+    JSObjectDeleteProperty(context_, object_, CYJSString(key), &exception);
     CYThrow(context_, exception);
 }
 
@@ -668,7 +634,7 @@ JSObjectRef CYMakeFunction(JSContextRef context, void *function, const char *typ
 
 void CYSetProperty(JSContextRef context, JSObjectRef object, const char *name, JSValueRef value) {
     JSValueRef exception(NULL);
-    JSObjectSetProperty(context, object, CYString(name), value, kJSPropertyAttributeNone, &exception);
+    JSObjectSetProperty(context, object, CYJSString(name), value, kJSPropertyAttributeNone, &exception);
     CYThrow(context, exception);
 }
 
@@ -681,7 +647,7 @@ char *CYPoolCString(apr_pool_t *pool, JSStringRef value) {
 }
 
 char *CYPoolCString(apr_pool_t *pool, JSContextRef context, JSValueRef value) {
-    return CYPoolCString(pool, CYString(context, value));
+    return CYPoolCString(pool, CYJSString(context, value));
 }
 
 // XXX: this macro is unhygenic
@@ -822,7 +788,7 @@ JSValueRef CYFromFFI(JSContextRef context, sig::Type *type, void *data) {
 
         case sig::string_P: {
             if (char *utf8 = *reinterpret_cast<char **>(data))
-                value = JSValueMakeString(context, CYString(utf8));
+                value = JSValueMakeString(context, CYJSString(utf8));
             else goto null;
         } break;
 
@@ -875,7 +841,7 @@ static JSValueRef Global_getProperty(JSContextRef context, JSObjectRef object, J
         if (NSMutableArray *entry = [Bridge_ objectForKey:name])
             switch ([[entry objectAtIndex:0] intValue]) {
                 case 0:
-                    return JSEvaluateScript(JSGetContext(), CYString([entry objectAtIndex:1]), NULL, NULL, 0, NULL);
+                    return JSEvaluateScript(JSGetContext(), CYJSString([entry objectAtIndex:1]), NULL, NULL, 0, NULL);
                 case 1:
                     return CYMakeFunction(context, [name cy$symbol], [[entry objectAtIndex:1] UTF8String]);
                 case 2:
@@ -951,13 +917,112 @@ static JSStaticValue Pointer_staticValues[2] = {
     {NULL, NULL, NULL, 0}
 };
 
-void cyparse(CYParser *parser);
-extern int cydebug;
+CYDriver::CYDriver(const std::string &filename) :
+    state_(CYClear),
+    data_(NULL),
+    size_(0),
+    filename_(filename),
+    source_(NULL)
+{
+    ScannerInit();
+}
+
+CYDriver::~CYDriver() {
+    ScannerDestroy();
+}
+
+void cy::parser::error(const cy::parser::location_type &location, const std::string &message) {
+    CYDriver::Error error;
+    error.location_ = location;
+    error.message_ = message;
+    driver.errors_.push_back(error);
+}
 
 void CYConsole(FILE *fin, FILE *fout, FILE *ferr) {
-    cydebug = 1;
-    CYParser parser;
-    cyparse(&parser);
+    std::string line;
+
+    __gnu_cxx::stdio_filebuf<char> bin(fin, std::ios::in);
+    std::istream sin(&bin);
+
+    restart: while (!feof(fin)) { _pooled
+        fputs("cy# ", fout);
+        fflush(fout);
+
+        std::string command;
+        std::vector<std::string> lines;
+
+      read:
+        if (!std::getline(sin, line))
+            break;
+
+        lines.push_back(line);
+        command += line;
+
+        CYDriver driver("");
+        cy::parser parser(driver);
+
+        driver.data_ = command.c_str();
+        driver.size_ = command.size();
+
+        if (parser.parse() != 0) {
+            for (CYDriver::Errors::const_iterator i(driver.errors_.begin()); i != driver.errors_.end(); ++i) {
+                cy::position begin(i->location_.begin);
+                if (begin.line != lines.size() || begin.column - 1 != lines.back().size()) {
+                    std::cerr << i->message_ << std::endl;
+                    goto restart;
+                }
+            }
+
+            driver.errors_.clear();
+
+            fputs("cy> ", fout);
+            fflush(fout);
+
+            command += '\n';
+            goto read;
+        }
+
+        if (driver.source_ == NULL)
+            goto restart;
+
+        std::ostringstream str;
+        driver.source_->Show(str);
+
+        std::string code(str.str());
+        std::cout << code << std::endl;
+
+        JSStringRef script(JSStringCreateWithUTF8CString(code.c_str()));
+
+        JSContextRef context(JSGetContext());
+
+        JSValueRef exception(NULL);
+        JSValueRef result(JSEvaluateScript(context, script, NULL, NULL, 0, &exception));
+        JSStringRelease(script);
+
+        if (exception != NULL)
+            result = exception;
+
+        if (JSValueIsUndefined(context, result))
+            goto restart;
+
+        CFStringRef json;
+
+        @try { json:
+            json = JSValueToJSONCopy(context, result);
+        } @catch (id error) {
+            CYThrow(context, error, &result);
+            goto json;
+        }
+
+        fputs([reinterpret_cast<const NSString *>(json) UTF8String], fout);
+        CFRelease(json);
+
+        fputs("\n", fout);
+        fflush(fout);
+    }
+
+    fputs("\n", fout);
+    fflush(fout);
 }
 
 MSInitialize { _pooled
@@ -1021,7 +1086,7 @@ MSInitialize { _pooled
 
     CYSetProperty(context, global, "ffi", JSObjectMakeConstructor(context, Functor_, &ffi));
 
-    CYSetProperty(context, global, "objc_msgSend", JSObjectMakeFunctionWithCallback(context, CYString("objc_msgSend"), &$objc_msgSend));
+    CYSetProperty(context, global, "objc_msgSend", JSObjectMakeFunctionWithCallback(context, CYJSString("objc_msgSend"), &$objc_msgSend));
 
     Bridge_ = [[NSMutableDictionary dictionaryWithContentsOfFile:@"/usr/lib/libcycript.plist"] retain];
 
@@ -1030,7 +1095,7 @@ MSInitialize { _pooled
     length_ = JSStringCreateWithUTF8CString("length");
 
     JSValueRef exception(NULL);
-    JSValueRef value(JSObjectGetProperty(JSGetContext(), global, CYString("Array"), &exception));
+    JSValueRef value(JSObjectGetProperty(JSGetContext(), global, CYJSString("Array"), &exception));
     CYThrow(context, exception);
     Array_ = JSValueToObject(JSGetContext(), value, &exception);
     CYThrow(context, exception);