]> git.saurik.com Git - cycript.git/commitdiff
Milestone #1: YUI has been destroyed, with room to spare.
authorJay Freeman (saurik) <saurik@saurik.com>
Mon, 16 Nov 2009 11:54:50 +0000 (11:54 +0000)
committerJay Freeman (saurik) <saurik@saurik.com>
Mon, 16 Nov 2009 11:54:50 +0000 (11:54 +0000)
Context.hpp
Execute.cpp
Output.cpp
Parser.hpp
Replace.cpp
makefile

index dfa0b62a24cd8a20305530400bb1f1bea53a49c1..994f3b6a951c19598466d6e9a4c8a9c474eb978d 100644 (file)
 
 #include "Options.hpp"
 
+class CYProgram;
 class CYScope;
 
 struct CYContext {
     apr_pool_t *pool_;
     CYOptions &options_;
     CYScope *scope_;
+    CYProgram *program_;
 
     CYContext(apr_pool_t *pool, CYOptions &options) :
         pool_(pool),
         options_(options),
-        scope_(NULL)
+        scope_(NULL),
+        program_(NULL)
     {
     }
 
index 3f094191652c548ad502052322771ee7214a75a8..1382769ed08a6e10f6594c0460b6cfaeb3f96150 100644 (file)
@@ -280,7 +280,7 @@ struct Struct_privateData :
     }
 };
 
-typedef std::map<const char *, Type_privateData *, CStringLess> TypeMap;
+typedef std::map<const char *, Type_privateData *, CYCStringLess> TypeMap;
 static TypeMap Types_;
 
 JSObjectRef CYMakeStruct(JSContextRef context, void *data, sig::Type *type, ffi_type *ffi, JSObjectRef owner) {
index 27ea3fcff21b87fbd7a4e537bdfb1546f51e4111..d3cf17caa53a0e06c33bf1b255dd5f1e64dd4cf1 100644 (file)
@@ -89,12 +89,10 @@ CYOutput &CYOutput::operator <<(char rhs) {
         if (right_) {
             out_ << '\n';
             right_ = false;
-            goto mode;
-        }
+        } goto done;
     } else goto work;
 
     right_ = true;
-  mode:
     mode_ = NoMode;
     goto done;
 
@@ -236,7 +234,8 @@ void Catch::Output(CYOutput &out) const {
 
 void CYComment::Output(CYOutput &out, CYFlags flags) const {
     out << '\r';
-    out << value_;
+    out.out_ << value_;
+    out.right_ = true;
     out << '\r';
 }
 
@@ -352,7 +351,6 @@ void CYElement::Output(CYOutput &out) const {
 }
 
 void CYEmpty::Output(CYOutput &out, CYFlags flags) const {
-    out << '`';
     out.Terminate();
 }
 
index dc3c907f30976b0aa02af2f712aee32917c97b95..dd9263ec71bf493984f84145be434254a7ae6d37 100644 (file)
@@ -239,6 +239,7 @@ _finline std::ostream &operator <<(std::ostream &lhs, const CYWord &rhs) {
 }
 
 struct CYIdentifier :
+    CYNext<CYIdentifier>,
     CYWord
 {
     CYIdentifier *replace_;
@@ -283,7 +284,7 @@ struct CYLabel :
     virtual void Output(CYOutput &out, CYFlags flags) const;
 };
 
-struct CStringLess :
+struct CYCStringLess :
     std::binary_function<const char *, const char *, bool>
 {
     _finline bool operator ()(const char *lhs, const char *rhs) const {
@@ -295,25 +296,28 @@ struct CYIdentifierValueLess :
     std::binary_function<CYIdentifier *, CYIdentifier *, bool>
 {
     _finline bool operator ()(CYIdentifier *lhs, CYIdentifier *rhs) const {
-        return CStringLess()(lhs->Word(), rhs->Word());
+        return CYCStringLess()(lhs->Word(), rhs->Word());
     }
 };
 
 enum CYIdentifierFlags {
     CYIdentifierArgument,
-    CYIdentifierInternal,
-    CYIdentifierVariable
+    CYIdentifierVariable,
+    CYIdentifierOther,
+    CYIdentifierMagic,
 };
 
+typedef std::set<const char *, CYCStringLess> CYCStringSet;
 typedef std::set<CYIdentifier *, CYIdentifierValueLess> CYIdentifierValueSet;
 typedef std::set<CYIdentifier *> CYIdentifierAddressSet;
+typedef std::vector<CYIdentifier *> CYIdentifierAddressVector;
 typedef std::map<CYIdentifier *, CYIdentifierFlags> CYIdentifierAddressFlagsMap;
 
 struct CYScope {
     CYScope *parent_;
     CYIdentifierValueSet identifiers_;
     CYIdentifierAddressFlagsMap internal_;
-    unsigned offset_;
+    size_t offset_;
 
     CYScope() :
         parent_(NULL),
@@ -321,7 +325,7 @@ struct CYScope {
     {
     }
 
-    void Add(CYContext &context, CYIdentifierAddressSet &external);
+    void Add(CYContext &context, CYIdentifierAddressVector &external);
     void Scope(CYContext &context, CYStatement *&statements);
 };
 
@@ -330,6 +334,8 @@ struct CYProgram :
     CYThing
 {
     CYStatement *statements_;
+    CYIdentifierAddressVector rename_;
+    CYCStringSet external_;
 
     CYProgram(CYStatement *statements) :
         statements_(statements)
@@ -337,7 +343,6 @@ struct CYProgram :
     }
 
     virtual void Replace(CYContext &context);
-
     virtual void Output(CYOutput &out) const;
 };
 
@@ -1340,7 +1345,8 @@ struct CYFunction :
     virtual ~CYFunction() {
     }
 
-    virtual void Replace_(CYContext &context);
+    void Inject(CYContext &context);
+    virtual void Replace_(CYContext &context, bool outer);
     virtual void Output(CYOutput &out, CYFlags flags) const;
 };
 
index fc024612461a84fa7849cca8cf2ce12d0133bf77..e376922a65e8827f3ca28caa570be3afb4b5dd35 100644 (file)
@@ -331,10 +331,21 @@ CYStatement *CYForEachInComprehension::Replace(CYContext &context, CYStatement *
     )));
 }
 
-void CYFunction::Replace_(CYContext &context) {
+void CYFunction::Inject(CYContext &context) {
+    name_ = name_->Replace(context);
+    context.scope_->internal_.insert(CYIdentifierAddressFlagsMap::value_type(name_, CYIdentifierOther));
+}
+
+void CYFunction::Replace_(CYContext &context, bool outer) {
+    if (outer)
+        Inject(context);
+
     parent_ = context.scope_;
     context.scope_ = this;
 
+    if (!outer && name_ != NULL)
+        Inject(context);
+
     parameters_->Replace(context);
     code_.Replace(context);
 
@@ -343,7 +354,7 @@ void CYFunction::Replace_(CYContext &context) {
 }
 
 CYExpression *CYFunctionExpression::Replace(CYContext &context) {
-    Replace_(context);
+    Replace_(context, false);
     return this;
 }
 
@@ -354,7 +365,7 @@ void CYFunctionParameter::Replace(CYContext &context) { $T()
 }
 
 CYStatement *CYFunctionStatement::Replace(CYContext &context) {
-    Replace_(context);
+    Replace_(context, true);
     return this;
 }
 
@@ -456,10 +467,50 @@ CYExpression *CYPrefix::Replace(CYContext &context) {
 
 void CYProgram::Replace(CYContext &context) {
     parent_ = context.scope_;
+    CYProgram *program(context.program_);
+
     context.scope_ = this;
+    context.program_ = this;
+
     statements_ = statements_->ReplaceAll(context);
+
     context.scope_ = parent_;
     Scope(context, statements_);
+    context.program_ = program;
+
+    size_t offset(0);
+
+    // XXX: totalling the probable occurrences and sorting by them would improve the result
+    for (CYIdentifierAddressVector::const_iterator i(rename_.begin()); i != rename_.end(); ++i, ++offset) {
+        const char *name;
+
+        if (context.options_.verbose_)
+            name = apr_psprintf(context.pool_, "$%"APR_SIZE_T_FMT"", offset);
+        else {
+            char id[8];
+            id[7] = '\0';
+
+          id:
+            unsigned position(7), local(offset + 1);
+
+            do {
+                unsigned index(local % 53);
+                local /= 53;
+                id[--position] = index == 0 ? '0' : index < 27 ? index - 1 + 'a' : index - 27 + 'A';
+            } while (local != 0);
+
+            if (external_.find(id + position) != external_.end()) {
+                ++offset;
+                goto id;
+            }
+
+            name = apr_pstrmemdup(context.pool_, id + position, 7 - position);
+            // XXX: at some point, this could become a keyword
+        }
+
+        for (CYIdentifier *identifier(*i); identifier != NULL; identifier = identifier->next_)
+            identifier->Set(name);
+    }
 }
 
 void CYProperty::Replace(CYContext &context) { $T()
@@ -472,8 +523,8 @@ CYStatement *CYReturn::Replace(CYContext &context) {
     return this;
 }
 
-void CYScope::Add(CYContext &context, CYIdentifierAddressSet &external) {
-    for (CYIdentifierAddressSet::const_iterator i(external.begin()); i != external.end(); ++i) {
+void CYScope::Add(CYContext &context, CYIdentifierAddressVector &external) {
+    for (CYIdentifierAddressVector::const_iterator i(external.begin()); i != external.end(); ++i) {
         std::pair<CYIdentifierAddressSet::iterator, bool> insert(identifiers_.insert(*i));
         if (!insert.second)
             (*i)->replace_ = *insert.first;
@@ -481,36 +532,34 @@ void CYScope::Add(CYContext &context, CYIdentifierAddressSet &external) {
 }
 
 void CYScope::Scope(CYContext &context, CYStatement *&statements) {
-    CYIdentifierAddressSet external;
+    CYIdentifierAddressVector external;
 
-    if (context.options_.verbose_)
-        std::cout << this << ':';
+    for (CYIdentifierValueSet::const_iterator i(identifiers_.begin()); i != identifiers_.end(); ++i)
+        if (internal_.find(*i) == internal_.end())
+            external.push_back(*i);
 
     CYDeclarations *last(NULL), *curr(NULL);
+    CYProgram *program(context.program_);
+
+    // XXX: we don't want to do this in order, we want to sort it by probable occurrence
+    for (CYIdentifierAddressFlagsMap::const_iterator i(internal_.begin()); i != internal_.end(); ++i) {
+        if (program != NULL && i->second != CYIdentifierMagic) {
+            if (program->rename_.size() <= offset_)
+                program->rename_.resize(offset_ + 1);
+            CYIdentifier *&identifier(program->rename_[offset_++]);
+            i->first->SetNext(identifier);
+            identifier = i->first;
+        }
 
-    for (CYIdentifierValueSet::const_iterator i(identifiers_.begin()); i != identifiers_.end(); ++i)
-        if (internal_.find(*i) == internal_.end()) {
-            if (context.options_.verbose_)
-                std::cout << ' ' << (*i)->Word() << '@' << static_cast<const CYWord *>(*i);
-            external.insert(*i);
-        } else {
-            if (context.options_.verbose_) {
-                std::cout << ' ' << offset_ << ':' << (*i)->Word() << '@' << static_cast<const CYWord *>(*i);
-                (*i)->Set(apr_psprintf(context.pool_, "$%u", offset_++));
-            } else {
-                (*i)->Set(apr_psprintf(context.pool_, "$%u", offset_++));
-            }
-
-            CYDeclarations *next($ CYDeclarations($ CYDeclaration(*i)));
+        if (i->second == CYIdentifierVariable) {
+            CYDeclarations *next($ CYDeclarations($ CYDeclaration(i->first)));
             if (last == NULL)
                 last = next;
             if (curr != NULL)
                 curr->SetNext(next);
             curr = next;
         }
-
-    if (context.options_.verbose_)
-        std::cout << " ->" << parent_ << std::endl;
+    }
 
     if (last != NULL) {
         CYVar *var($ CYVar(last));
@@ -522,7 +571,9 @@ void CYScope::Scope(CYContext &context, CYStatement *&statements) {
         if (parent_->offset_ < offset_)
             parent_->offset_ = offset_;
         parent_->Add(context, external);
-    }
+    } else if (program != NULL)
+        for (CYIdentifierAddressVector::const_iterator i(external.begin()); i != external.end(); ++i)
+            program->external_.insert((*i)->Word());
 }
 
 CYStatement *CYStatement::Collapse(CYContext &context) {
index 430e6d6c6e069438931a2bd54f9cff0f624069b6..ed5ae2869d4d016b8ebfa41022c2e93120320b8a 100644 (file)
--- a/makefile
+++ b/makefile
@@ -140,7 +140,7 @@ package: $(deb)
 test: $(deb)
        dpkg -i $(deb)
        if [[ -e target.cy ]]; then cycript -c target.cy && echo; fi
-       if [[ -e jquery.js ]]; then cycript -c jquery.js >jquery.cyc.js; gzip -9c jquery.cyc.js >jquery.cyc.js.gz; ls -la jquery.{cyc,yui}.js{,.gz}; fi
+       if [[ -e jquery.js ]]; then /usr/bin/time cycript -c jquery.js >jquery.cyc.js; gzip -9c jquery.cyc.js >jquery.cyc.js.gz; ls -la jquery.{cyc,yui}.js{,.gz}; fi
        if [[ -e test.cy ]]; then cycript test.cy; fi
 
 .PHONY: all clean extra package control