]> git.saurik.com Git - cycript.git/blobdiff - Complete.cpp
Do not allow JavaMethod calls on non-Java objects.
[cycript.git] / Complete.cpp
index 6ce6671c7d0cdde879990da4faeebd4f70197cf8..820895c9c91776af12b040caace6244758274367 100644 (file)
@@ -1,5 +1,5 @@
-/* Cycript - Optimizing JavaScript Compiler/Runtime
- * Copyright (C) 2009-2015  Jay Freeman (saurik)
+/* Cycript - The Truly Universal Scripting Language
+ * Copyright (C) 2009-2016  Jay Freeman (saurik)
 */
 
 /* GNU Affero General Public License, Version 3 {{{ */
@@ -28,7 +28,7 @@
 static CYExpression *ParseExpression(CYPool &pool, CYUTF8String code) {
     std::stringstream stream;
     stream << '(' << code << ')';
-    CYDriver driver(pool, stream);
+    CYDriver driver(pool, *stream.rdbuf());
     if (driver.Parse() || !driver.errors_.empty())
         return NULL;
 
@@ -51,7 +51,7 @@ static CYExpression *ParseExpression(CYPool &pool, CYUTF8String code) {
 _visible char **CYComplete(const char *word, const std::string &line, CYUTF8String (*run)(CYPool &pool, const std::string &)) {
     CYLocalPool pool;
 
-    std::istringstream stream(line);
+    std::stringbuf stream(line);
     CYDriver driver(pool, stream);
 
     driver.auto_ = true;
@@ -89,6 +89,20 @@ _visible char **CYComplete(const char *word, const std::string &line, CYUTF8Stri
                 prefix << (*part)->word_ << ':';
         } break;
 
+        case CYDriver::AutoResolve:
+            expression = $M(driver.context_, $S("$cyr"));
+        break;
+
+        case CYDriver::AutoStruct:
+            expression = $ CYThis();
+            prefix << "$cys";
+        break;
+
+        case CYDriver::AutoEnum:
+            expression = $ CYThis();
+            prefix << "$cye";
+        break;
+
         default:
             _assert(false);
     }
@@ -104,8 +118,12 @@ _visible char **CYComplete(const char *word, const std::string &line, CYUTF8Stri
     "       if (false) {\n"
     "           for (var name in object)\n"
     "               if (name.substring(0, entire) == prefix)\n"
-    "                   names.push(name.substr(before));\n"
+    "                   names.push(name);\n"
     "       } else do {\n"
+    "           if (object.hasOwnProperty(\"cy$complete\")) {\n"
+    "               names = names.concat(object.cy$complete(prefix));\n"
+    "               continue;\n"
+    "           }\n"
     "           try {\n"
     "               var local = Object.getOwnPropertyNames(object);\n"
     "           } catch (e) {\n"
@@ -113,7 +131,7 @@ _visible char **CYComplete(const char *word, const std::string &line, CYUTF8Stri
     "           }\n"
     "           for (var name of local)\n"
     "               if (name.substring(0, entire) == prefix)\n"
-    "                   names.push(name.substr(before));\n"
+    "                   names.push(name);\n"
     "       } while (object = typeof object === 'object' ? Object.getPrototypeOf(object) : object.__proto__);\n"
     "       return names;\n"
     "   }\n"
@@ -138,7 +156,7 @@ _visible char **CYComplete(const char *word, const std::string &line, CYUTF8Stri
         return NULL;
 
     // XXX: use an std::set?
-    typedef std::vector<std::string> Completions;
+    typedef std::vector<CYUTF8String> Completions;
     Completions completions;
 
     std::string common;
@@ -152,27 +170,32 @@ _visible char **CYComplete(const char *word, const std::string &line, CYUTF8Stri
         CYString *string(dynamic_cast<CYString *>(value->value_));
         _assert(string != NULL);
 
-        std::string completion;
+        CYUTF8String completion;
         if (string->size_ != 0)
-            completion.assign(string->value_, string->size_);
+            completion = {string->value_, string->size_};
         else if (driver.mode_ == CYDriver::AutoMessage)
             completion = "]";
         else
             continue;
 
+        completion.data += begin.size();
+        completion.size -= begin.size();
+
+        if (CYStartsWith(completion, "$cy"))
+            continue;
         completions.push_back(completion);
 
         if (!rest) {
             common = completion;
             rest = true;
         } else {
-            size_t limit(completion.size()), size(common.size());
+            size_t limit(completion.size), size(common.size());
             if (size > limit)
                 common = common.substr(0, limit);
             else
                 limit = size;
             for (limit = 0; limit != size; ++limit)
-                if (common[limit] != completion[limit])
+                if (common[limit] != completion.data[limit])
                     break;
             if (limit != size)
                 common = common.substr(0, limit);
@@ -194,7 +217,7 @@ _visible char **CYComplete(const char *word, const std::string &line, CYUTF8Stri
     results[0] = strdup(common.c_str());
     size_t index(0);
     for (Completions::const_iterator i(completions.begin()); i != completions.end(); ++i)
-        results[++index] = strdup(i->c_str());
+        results[++index] = strdup(i->data);
     results[count + 1] = NULL;
 
     return results;