]> git.saurik.com Git - cycript.git/blobdiff - Output.cpp
Added bridging for nlist_64, activated memory manager, and setup a remap from singlet...
[cycript.git] / Output.cpp
index a35ab818232858c3ca9958f3cf4bc4c24ff419e2..3396b483207c555a34fccf778fdee0d364ad0786 100644 (file)
@@ -3,6 +3,9 @@
 #include <iostream>
 #include <iomanip>
 
+#include <objc/runtime.h>
+#include <sstream>
+
 _finline CYFlags operator ~(CYFlags rhs) {
     return static_cast<CYFlags>(~static_cast<unsigned>(rhs));
 }
@@ -43,7 +46,7 @@ bool CYTrue::Value() const {
 
 void CYAddressOf::Output(std::ostream &out, CYFlags flags) const {
     rhs_->Output(out, 1, CYLeft(flags));
-    out << ".$()";
+    out << ".addressOf()";
 }
 
 void CYArgument::Output(std::ostream &out) const {
@@ -224,18 +227,18 @@ void CYExpression::Part(std::ostream &out) const {
 
 void CYCompound::Output(std::ostream &out, CYFlags flags) const {
     if (CYExpression *expression = expressions_)
-        if (CYExpression *next = expression->next_)
-            expression->Output(out, flags);
-        else {
+        if (CYExpression *next = expression->next_) {
             expression->Output(out, CYLeft(flags));
             CYFlags center(CYCenter(flags));
             while (next != NULL) {
+                expression = next;
                 out << ',';
                 next = expression->next_;
                 CYFlags right(next != NULL ? center : CYRight(flags));
                 expression->Output(out, right);
             }
-        }
+        } else
+            expression->Output(out, flags);
 }
 
 void CYExpression::Output(std::ostream &out, unsigned precedence, CYFlags flags) const {
@@ -353,6 +356,8 @@ void CYMember::Output(std::ostream &out, CYFlags flags) const {
 }
 
 void CYMessage::Output(std::ostream &out) const {
+    if (next_ != NULL)
+        next_->Output(out);
     out << "$cyn=new Selector(\"";
     for (CYMessageParameter *parameter(parameter_); parameter != NULL; parameter = parameter->next_)
         if (parameter->tag_ != NULL) {
@@ -363,20 +368,14 @@ void CYMessage::Output(std::ostream &out) const {
     out << "\");";
     out << "$cyt=$cyn.type($cys," << (instance_ ? "true" : "false") << ");";
     out << "class_addMethod($cy" << (instance_ ? 'c' : 'm') << ",$cyn,";
-    out << "new Functor(function(";
-    bool comma(false);
+    out << "new Functor(function(self,_cmd";
     for (CYMessageParameter *parameter(parameter_); parameter != NULL; parameter = parameter->next_)
-        if (parameter->name_ != NULL) {
-            if (comma)
-                out << ',';
-            else
-                comma = true;
-            out << *parameter->name_;
-        }
-    out << "){";
+        if (parameter->name_ != NULL)
+            out << ',' << *parameter->name_;
+    out << "){return function(){";
     if (body_ != NULL)
         body_->Show(out);
-    out << "},$cyt),$cyt);";
+    out << "}.call(self);},$cyt),$cyt);";
 }
 
 void CYNew::Output(std::ostream &out, CYFlags flags) const {
@@ -432,7 +431,8 @@ void CYPrefix::Output(std::ostream &out, CYFlags flags) const {
 }
 
 void CYProperty::Output(std::ostream &out) const {
-    out << *name_ << ':';
+    out << *name_;
+    out << ':';
     value_->Output(out, CYPA, CYNoFlags);
     if (next_ != NULL) {
         out << ',';
@@ -466,14 +466,15 @@ void CYSelectorPart::Output(std::ostream &out) const {
 void CYSend::Output(std::ostream &out, CYFlags flags) const {
     out << "objc_msgSend(";
     self_->Output(out, CYPA, CYNoFlags);
-    out << ",\"";
+    out << ",";
+    std::ostringstream name;
     for (CYArgument *argument(arguments_); argument != NULL; argument = argument->next_)
         if (argument->name_ != NULL) {
-            out << *argument->name_;
+            name << *argument->name_;
             if (argument->value_ != NULL)
-                out << ':';
+                name << ':';
         }
-    out << "\"";
+    out << reinterpret_cast<void *>(sel_registerName(name.str().c_str()));
     for (CYArgument *argument(arguments_); argument != NULL; argument = argument->next_)
         if (argument->value_ != NULL) {
             out << ",";
@@ -498,10 +499,18 @@ void CYSource::Output(std::ostream &out, bool block) const {
 }
 
 void CYString::Output(std::ostream &out, CYFlags flags) const {
-    out << '\"';
+    unsigned quot(0), apos(0);
+    for (const char *value(value_), *end(value_ + size_); value != end; ++value)
+        if (*value == '"')
+            ++quot;
+        else if (*value == '\'')
+            ++apos;
+
+    bool single(quot > apos);
+
+    out << (single ? '\'' : '"');
     for (const char *value(value_), *end(value_ + size_); value != end; ++value)
         switch (*value) {
-            case '"': out << "\\\""; break;
             case '\\': out << "\\\\"; break;
             case '\b': out << "\\b"; break;
             case '\f': out << "\\f"; break;
@@ -510,13 +519,25 @@ void CYString::Output(std::ostream &out, CYFlags flags) const {
             case '\t': out << "\\t"; break;
             case '\v': out << "\\v"; break;
 
+            case '"':
+                if (!single)
+                    out << "\\\"";
+                else goto simple;
+            break;
+
+            case '\'':
+                if (single)
+                    out << "\\'";
+                else goto simple;
+            break;
+
             default:
                 if (*value < 0x20 || *value >= 0x7f)
                     out << "\\x" << std::setbase(16) << std::setw(2) << std::setfill('0') << unsigned(*value);
-                else
+                else simple:
                     out << *value;
         }
-    out << '\"';
+    out << (single ? '\'' : '"');
 }
 
 void CYSwitch::Output(std::ostream &out) const {
@@ -555,12 +576,11 @@ void CYTry::Output(std::ostream &out) const {
 }
 
 void CYVariable::Output(std::ostream &out, CYFlags flags) const {
-    bool protect((flags & CYNoLeader) != 0);
-    if (protect)
-        out << '(';
+    if ((flags & CYNoLeader) != 0)
+        out << ' ';
     out << *name_;
-    if (protect)
-        out << ')';
+    if ((flags & CYNoTrailer) != 0)
+        out << ' ';
 }
 
 void CYWhile::Output(std::ostream &out) const {