From 4644480a22eda2f7350a8997bb2109f325b576fe Mon Sep 17 00:00:00 2001 From: "Jay Freeman (saurik)" Date: Thu, 22 Oct 2009 21:14:50 +0000 Subject: [PATCH] Ported back to the iPhone. --- Connector.cpp | 39 +++++++++++++++ Cycript.y.in | 2 +- Library.mm | 13 ++--- Network.cpp | 39 +++++++++++++++ ObjectiveC.hpp | 43 ++++++++++++++++- ObjectiveC.mm | 40 +++++----------- Output.cpp | 111 ++++++++++++++++++++----------------------- Parser.cpp | 39 +++++++++++++++ Parser.hpp | 78 ++++++++++++++++++++---------- Pooling.hpp | 39 +++++++++++++++ Replace.cpp | 120 +++++++++++++++++++++++++++++++++++++++++++++++ Replace.hpp | 39 +++++++++++++++ cycript.hpp | 2 +- sig/ffi_type.hpp | 2 +- sig/parse.hpp | 2 +- sig/types.hpp | 2 +- 16 files changed, 482 insertions(+), 128 deletions(-) diff --git a/Connector.cpp b/Connector.cpp index 473309f..6f666cc 100644 --- a/Connector.cpp +++ b/Connector.cpp @@ -1,3 +1,42 @@ +/* Cycript - Remove Execution Server and Disassembler + * Copyright (C) 2009 Jay Freeman (saurik) +*/ + +/* Modified BSD License {{{ */ +/* + * Redistribution and use in source and binary + * forms, with or without modification, are permitted + * provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the + * above copyright notice, this list of conditions + * and the following disclaimer. + * 2. Redistributions in binary form must reproduce the + * above copyright notice, this list of conditions + * and the following disclaimer in the documentation + * and/or other materials provided with the + * distribution. + * 3. The name of the author may not be used to endorse + * or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, + * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR + * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ +/* }}} */ + #include #include "cycript.hpp" diff --git a/Cycript.y.in b/Cycript.y.in index 40126fa..7fef9ff 100644 --- a/Cycript.y.in +++ b/Cycript.y.in @@ -1,4 +1,4 @@ -/* Cycript - Remove Execution Server and Disassembler +/* Cycript - Remote Execution Server and Disassembler * Copyright (C) 2009 Jay Freeman (saurik) */ diff --git a/Library.mm b/Library.mm index b05138a..781de8e 100644 --- a/Library.mm +++ b/Library.mm @@ -1,4 +1,4 @@ -/* Cycript - Remove Execution Server and Disassembler +/* Cycript - Remote Execution Server and Disassembler * Copyright (C) 2009 Jay Freeman (saurik) */ @@ -204,7 +204,7 @@ class CYJSString { /* Objective-C Strings {{{ */ JSStringRef CYCopyJSString_(NSString *value) { #ifdef __APPLE__ - return JSStringCreateWithCFString(reinterpret_cast(string)); + return JSStringCreateWithCFString(reinterpret_cast(value)); #else return CYCopyJSString([value UTF8String]); #endif @@ -2806,12 +2806,12 @@ static void ObjectiveC_Protocols_getPropertyNames(JSContextRef context, JSObject static JSObjectRef CYMakeType(JSContextRef context, const char *type) { Type_privateData *internal(new Type_privateData(NULL, type)); - return JSObjectMake(context, Type_, internal); + return JSObjectMake(context, Type_privateData::Class, internal); } static JSObjectRef CYMakeType(JSContextRef context, sig::Type *type) { Type_privateData *internal(new Type_privateData(type)); - return JSObjectMake(context, Type_, internal); + return JSObjectMake(context, Type_privateData::Class, internal); } static JSValueRef Runtime_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { @@ -3622,7 +3622,8 @@ JSGlobalContextRef CYGetJSContext() { definition.callAsFunction = &Type_callAsFunction; definition.callAsConstructor = &Type_callAsConstructor; definition.finalize = &Finalize; - Type_privateData::Class = JSClassCreate(&definition); + // XXX: dude: just rename the damned variable + (Type_privateData::Class) = JSClassCreate(&definition); definition = kJSClassDefinitionEmpty; definition.className = "Runtime"; @@ -3705,7 +3706,7 @@ JSGlobalContextRef CYGetJSContext() { CYSetProperty(context, global, CYJSString("Instance"), Instance); CYSetProperty(context, global, CYJSString("Pointer"), JSObjectMakeConstructor(context, Pointer_, &Pointer_new)); CYSetProperty(context, global, CYJSString("Selector"), Selector); - CYSetProperty(context, global, CYJSString("Type"), JSObjectMakeConstructor(context, Type_, &Type_new)); + CYSetProperty(context, global, CYJSString("Type"), JSObjectMakeConstructor(context, Type_privateData::Class, &Type_new)); MSHookFunction(&objc_registerClassPair, MSHake(objc_registerClassPair)); diff --git a/Network.cpp b/Network.cpp index 4adc197..e38dfd8 100644 --- a/Network.cpp +++ b/Network.cpp @@ -1,5 +1,44 @@ #include "cycript.hpp" +/* Cycript - Remove Execution Server and Disassembler + * Copyright (C) 2009 Jay Freeman (saurik) +*/ + +/* Modified BSD License {{{ */ +/* + * Redistribution and use in source and binary + * forms, with or without modification, are permitted + * provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the + * above copyright notice, this list of conditions + * and the following disclaimer. + * 2. Redistributions in binary form must reproduce the + * above copyright notice, this list of conditions + * and the following disclaimer in the documentation + * and/or other materials provided with the + * distribution. + * 3. The name of the author may not be used to endorse + * or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, + * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR + * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ +/* }}} */ + #include #include diff --git a/ObjectiveC.hpp b/ObjectiveC.hpp index b45146c..230813f 100644 --- a/ObjectiveC.hpp +++ b/ObjectiveC.hpp @@ -1,3 +1,42 @@ +/* Cycript - Remote Execution Server and Disassembler + * Copyright (C) 2009 Jay Freeman (saurik) +*/ + +/* Modified BSD License {{{ */ +/* + * Redistribution and use in source and binary + * forms, with or without modification, are permitted + * provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the + * above copyright notice, this list of conditions + * and the following disclaimer. + * 2. Redistributions in binary form must reproduce the + * above copyright notice, this list of conditions + * and the following disclaimer in the documentation + * and/or other materials provided with the + * distribution. + * 3. The name of the author may not be used to endorse + * or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, + * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR + * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ +/* }}} */ + #ifndef CYCRIPT_OBJECTIVEC_HPP #define CYCRIPT_OBJECTIVEC_HPP @@ -69,13 +108,13 @@ struct CYMessage : bool instance_; CYExpression *type_; CYMessageParameter *parameters_; - CYStatement *statements_; + CYBlock code_; CYMessage(bool instance, CYExpression *type, CYMessageParameter *parameter, CYStatement *statements) : instance_(instance), type_(type), parameters_(parameter), - statements_(statements) + code_(statements) { } diff --git a/ObjectiveC.mm b/ObjectiveC.mm index 1e4e30d..4f9245d 100644 --- a/ObjectiveC.mm +++ b/ObjectiveC.mm @@ -50,30 +50,19 @@ void CYClassStatement::Output(CYOutput &out, CYFlags flags) const { } void CYField::Output(CYOutput &out) const { - // XXX: implement! } void CYMessage::Output(CYOutput &out, bool replace) const { - if (next_ != NULL) - next_->Output(out, replace); - out << "$cyn=new Selector(\""; + out << (instance_ ? '-' : '+'); + for (CYMessageParameter *parameter(parameters_); parameter != NULL; parameter = parameter->next_) if (parameter->tag_ != NULL) { - out << *parameter->tag_; + out << ' ' << *parameter->tag_; if (parameter->name_ != NULL) - out << ':'; + out << ':' << *parameter->name_; } - out << "\");"; - out << "$cyt=$cyn.type($cy" << (instance_ ? 's' : 'p') << ')' << ';'; - out << (replace ? "class_replaceMethod" : "class_addMethod") << '(' << (instance_ ? "$cyc" : "$cym") << ',' << "$cyn" << ','; - out << "new Functor(function(self,_cmd"; - for (CYMessageParameter *parameter(parameters_); parameter != NULL; parameter = parameter->next_) - if (parameter->name_ != NULL) - out << ',' << *parameter->name_; - out << "){return function(){"; - if (statements_ != NULL) - statements_->Multiple(out); - out << "}.call(self);},$cyt),$cyt);"; + + out << code_; } void CYSelector::Output(CYOutput &out, CYFlags flags) const { @@ -89,24 +78,17 @@ void CYSelectorPart::Output(CYOutput &out) const { void CYSend::Output(CYOutput &out, CYFlags flags) const { out << '['; + self_->Output(out, CYPA, CYNoFlags); - out << ']'; - std::ostringstream name; for (CYArgument *argument(arguments_); argument != NULL; argument = argument->next_) if (argument->name_ != NULL) { - name << *argument->name_; + out << ' ' << *argument->name_; if (argument->value_ != NULL) - name << ':'; + out << ':' << *argument->value_; } - out.out_ << reinterpret_cast(sel_registerName(name.str().c_str())); - for (CYArgument *argument(arguments_); argument != NULL; argument = argument->next_) - if (argument->value_ != NULL) { - out << ','; - argument->value_->Output(out, CYPA, CYNoFlags); - } - out << ')'; + out << ']'; } CYStatement *CYCategory::Replace(CYContext &context) { @@ -159,7 +141,7 @@ CYStatement *CYMessage::Replace(CYContext &context, bool replace) const { $T(NUL $V(instance_ ? "$cyc" : "$cym"), cyn, $N2($V("Functor"), $F(NULL, $P2("self", "_cmd", parameters_->Parameters(context)), $$->* - $ CYReturn($C1($M($F(NULL, NULL, statements_), $S("call")), $V("self"))) + $ CYReturn($C1($M($F(NULL, NULL, code_), $S("call")), $V("self"))) ), cyt), cyt )) diff --git a/Output.cpp b/Output.cpp index dec2df5..9ac31e5 100644 --- a/Output.cpp +++ b/Output.cpp @@ -1,3 +1,42 @@ +/* Cycript - Remove Execution Server and Disassembler + * Copyright (C) 2009 Jay Freeman (saurik) +*/ + +/* Modified BSD License {{{ */ +/* + * Redistribution and use in source and binary + * forms, with or without modification, are permitted + * provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the + * above copyright notice, this list of conditions + * and the following disclaimer. + * 2. Redistributions in binary form must reproduce the + * above copyright notice, this list of conditions + * and the following disclaimer in the documentation + * and/or other materials provided with the + * distribution. + * 3. The name of the author may not be used to endorse + * or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, + * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR + * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ +/* }}} */ + #include "Parser.hpp" #include @@ -126,21 +165,7 @@ void CYArray::Output(CYOutput &out, CYFlags flags) const { } void CYArrayComprehension::Output(CYOutput &out, CYFlags flags) const { - // XXX: I don't necc. need the ()s - out << "(function($cyv"; - for (CYComprehension *comprehension(comprehensions_); comprehension != NULL; comprehension = comprehension->next_) - if (const char *name = comprehension->Name()) - out << ',' << name; - out << "){"; - out << "$cyv=[];"; - comprehensions_->Output(out); - out << "$cyv.push("; - expression_->Output(out, CYPA, CYNoFlags); - out << ");"; - for (CYComprehension *comprehension(comprehensions_); comprehension != NULL; comprehension = comprehension->next_) - comprehension->End_(out); - out << "return $cyv;"; - out << "}())"; + out << '[' << *expression_ << ' ' << *comprehensions_ << ']'; } void CYAssignment::Output(CYOutput &out, CYFlags flags) const { @@ -208,11 +233,6 @@ void CYCompound::Output(CYOutput &out, CYFlags flags) const { expression->Output(out, flags); } -void CYComprehension::Output(CYOutput &out) const { - Begin_(out); - out << next_; -} - void CYCondition::Output(CYOutput &out, CYFlags flags) const { test_->Output(out, Precedence() - 1, CYLeft(flags)); out << ' ' << '?' << ' '; @@ -249,10 +269,6 @@ void CYDeclaration::ForIn(CYOutput &out, CYFlags flags) const { Output(out, CYRight(flags)); } -void CYDeclaration::ForEachIn(CYOutput &out) const { - out << *identifier_; -} - void CYDeclaration::Output(CYOutput &out, CYFlags flags) const { out << *identifier_; if (initialiser_ != NULL) { @@ -333,10 +349,6 @@ void CYExpression::For(CYOutput &out) const { Output(out, CYNoIn); } -void CYExpression::ForEachIn(CYOutput &out) const { - Output(out, CYPA, CYNoRightHand); -} - void CYExpression::ForIn(CYOutput &out, CYFlags flags) const { Output(out, flags | CYNoRightHand); } @@ -369,36 +381,14 @@ void CYFor::Output(CYOutput &out, CYFlags flags) const { } void CYForEachIn::Output(CYOutput &out, CYFlags flags) const { - out << "with({$cys:0,$cyt:0}){"; - - out << "$cys="; - set_->Output(out, CYPA, CYNoFlags); - out << ';'; - - out << "for($cyt in $cys){"; - - initialiser_->ForEachIn(out); - out << "=$cys[$cyt];"; - - code_->Multiple(out); - - out << '}'; - - out << '}'; -} - -void CYForEachInComprehension::Begin_(CYOutput &out) const { - out << "(function($cys){"; - out << "$cys="; - set_->Output(out, CYPA, CYNoFlags); - out << ';'; - - out << "for(" << *name_ << " in $cys){"; - out << *name_ << "=$cys[" << *name_ << "];"; + out << "for" << ' ' << "each" << ' ' << '('; + initialiser_->ForIn(out, CYNoIn); + out << "in" << *set_ << ')'; + code_->Single(out, CYRight(flags)); } -void CYForEachInComprehension::End_(CYOutput &out) const { - out << "}}());"; +void CYForEachInComprehension::Output(CYOutput &out) const { + out << "for" << ' ' << "each" << ' ' << '(' << *name_ << ' ' << "in" << ' ' << *set_ << ')' << next_; } void CYForIn::Output(CYOutput &out, CYFlags flags) const { @@ -408,8 +398,8 @@ void CYForIn::Output(CYOutput &out, CYFlags flags) const { code_->Single(out, CYRight(flags)); } -void CYForInComprehension::Begin_(CYOutput &out) const { - out << "for" << ' ' << '(' << *name_ << "in" << *set_ << ')'; +void CYForInComprehension::Output(CYOutput &out) const { + out << "for" << ' ' << '(' << *name_ << ' ' << "in" << ' ' << *set_ << ')'; } void CYFunction::Output(CYOutput &out, CYFlags flags) const { @@ -468,8 +458,8 @@ void CYIf::Output(CYOutput &out, CYFlags flags) const { out << '}'; } -void CYIfComprehension::Begin_(CYOutput &out) const { - out << "if" << '(' << *test_ << ')'; +void CYIfComprehension::Output(CYOutput &out) const { + out << "if" << ' ' << '(' << *test_ << ')' << next_; } void CYIndirectMember::Output(CYOutput &out, CYFlags flags) const { @@ -517,6 +507,7 @@ void CYNull::Output(CYOutput &out, CYFlags flags) const { void CYNumber::Output(CYOutput &out, CYFlags flags) const { char value[32]; + // XXX: I want this to print 1e3 rather than 1000 sprintf(value, "%.17g", Value()); out << value; } diff --git a/Parser.cpp b/Parser.cpp index 35ac7f7..8a1390d 100644 --- a/Parser.cpp +++ b/Parser.cpp @@ -1,3 +1,42 @@ +/* Cycript - Remove Execution Server and Disassembler + * Copyright (C) 2009 Jay Freeman (saurik) +*/ + +/* Modified BSD License {{{ */ +/* + * Redistribution and use in source and binary + * forms, with or without modification, are permitted + * provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the + * above copyright notice, this list of conditions + * and the following disclaimer. + * 2. Redistributions in binary form must reproduce the + * above copyright notice, this list of conditions + * and the following disclaimer in the documentation + * and/or other materials provided with the + * distribution. + * 3. The name of the author may not be used to endorse + * or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, + * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR + * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ +/* }}} */ + #include "Parser.hpp" #include "Cycript.tab.hh" diff --git a/Parser.hpp b/Parser.hpp index ec50ef9..1e8825d 100644 --- a/Parser.hpp +++ b/Parser.hpp @@ -1,4 +1,4 @@ -/* Cycript - Remove Execution Server and Disassembler +/* Cycript - Remote Execution Server and Disassembler * Copyright (C) 2009 Jay Freeman (saurik) */ @@ -280,6 +280,10 @@ struct CYBlock : { } + operator CYStatement *() const { + return statements_; + } + virtual CYStatement *Replace(CYContext &context); virtual void Output(CYOutput &out) const; @@ -343,10 +347,12 @@ struct CYForInitialiser { struct CYForInInitialiser { virtual void ForIn(CYOutput &out, CYFlags flags) const = 0; virtual const char *ForEachIn() const = 0; - virtual void ForEachIn(CYOutput &out) const = 0; virtual CYExpression *ForEachIn(CYContext &out) = 0; }; +struct CYNumber; +struct CYString; + struct CYExpression : CYNext, CYForInitialiser, @@ -364,7 +370,6 @@ struct CYExpression : virtual void ForIn(CYOutput &out, CYFlags flags) const; virtual const char *ForEachIn() const; - virtual void ForEachIn(CYOutput &out) const; virtual CYExpression *ForEachIn(CYContext &out); virtual void Output(CYOutput &out) const; @@ -378,6 +383,18 @@ struct CYExpression : virtual CYExpression *Replace(CYContext &context) = 0; + virtual CYExpression *Primitive(CYContext &context) { + return this; + } + + virtual CYNumber *Number(CYContext &context) { + return NULL; + } + + virtual CYString *String(CYContext &context) { + return NULL; + } + virtual const char *Word() const { return NULL; } @@ -441,17 +458,12 @@ struct CYComprehension : CYNext, CYThing { - void Output(CYOutput &out) const; virtual const char *Name() const = 0; - virtual void Begin_(CYOutput &out) const = 0; - - virtual void End_(CYOutput &out) const { - } - virtual CYFunctionParameter *Parameter(CYContext &context) const = 0; CYFunctionParameter *Parameters(CYContext &context) const; virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const; + virtual void Output(CYOutput &out) const = 0; }; struct CYForInComprehension : @@ -470,10 +482,9 @@ struct CYForInComprehension : return name_->Value(); } - virtual void Begin_(CYOutput &out) const; - virtual CYFunctionParameter *Parameter(CYContext &context) const; virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const; + virtual void Output(CYOutput &out) const; }; struct CYForEachInComprehension : @@ -492,11 +503,9 @@ struct CYForEachInComprehension : return name_->Value(); } - virtual void Begin_(CYOutput &out) const; - virtual void End_(CYOutput &out) const; - virtual CYFunctionParameter *Parameter(CYContext &context) const; virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const; + virtual void Output(CYOutput &out) const; }; struct CYIfComprehension : @@ -513,10 +522,9 @@ struct CYIfComprehension : return NULL; } - virtual void Begin_(CYOutput &out) const; - virtual CYFunctionParameter *Parameter(CYContext &context) const; virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const; + virtual void Output(CYOutput &out) const; }; struct CYArrayComprehension : @@ -618,6 +626,10 @@ struct CYString : virtual const char *Word() const; + virtual CYNumber *Number(CYContext &context); + virtual CYString *String(CYContext &context); + + virtual CYString *Concat(CYContext &out, CYString *rhs) const; virtual void Output(CYOutput &out, CYFlags flags) const; virtual void PropertyName(CYOutput &out) const; }; @@ -637,6 +649,9 @@ struct CYNumber : return value_; } + virtual CYNumber *Number(CYContext &context); + virtual CYString *String(CYContext &context); + virtual void Output(CYOutput &out, CYFlags flags) const; virtual void PropertyName(CYOutput &out) const; }; @@ -667,6 +682,9 @@ struct CYNull : { } + virtual CYNumber *Number(CYContext &context); + virtual CYString *String(CYContext &context); + virtual void Output(CYOutput &out, CYFlags flags) const; }; @@ -702,6 +720,9 @@ struct CYFalse : virtual bool Value() const { return false; } + + virtual CYNumber *Number(CYContext &context); + virtual CYString *String(CYContext &context); }; struct CYTrue : @@ -716,6 +737,9 @@ struct CYTrue : virtual bool Value() const { return true; } + + virtual CYNumber *Number(CYContext &context); + virtual CYString *String(CYContext &context); }; struct CYVariable : @@ -915,7 +939,6 @@ struct CYDeclaration : virtual void ForIn(CYOutput &out, CYFlags flags) const; virtual const char *ForEachIn() const; - virtual void ForEachIn(CYOutput &out) const; virtual CYExpression *ForEachIn(CYContext &out); void Replace(CYContext &context); @@ -1458,10 +1481,13 @@ struct CYIndirect : virtual CYExpression *Replace(CYContext &context); }; -#define CYPostfix_(op, name) \ +#define CYReplace \ + virtual CYExpression *Replace(CYContext &context); + +#define CYPostfix_(op, name, args...) \ struct CY ## name : \ CYPostfix \ - { \ + { args \ CY ## name(CYExpression *lhs) : \ CYPostfix(lhs) \ { \ @@ -1472,10 +1498,10 @@ struct CYIndirect : } \ }; -#define CYPrefix_(alphabetic, op, name) \ +#define CYPrefix_(alphabetic, op, name, args...) \ struct CY ## name : \ CYPrefix \ - { \ + { args \ CY ## name(CYExpression *rhs) : \ CYPrefix(rhs) \ { \ @@ -1488,10 +1514,10 @@ struct CYIndirect : } \ }; -#define CYInfix_(alphabetic, precedence, op, name) \ +#define CYInfix_(alphabetic, precedence, op, name, args...) \ struct CY ## name : \ CYInfix \ - { \ + { args \ CY ## name(CYExpression *lhs, CYExpression *rhs) : \ CYInfix(lhs, rhs) \ { \ @@ -1505,10 +1531,10 @@ struct CYIndirect : } \ }; -#define CYAssignment_(op, name) \ +#define CYAssignment_(op, name, args...) \ struct CY ## name ## Assign : \ CYAssignment \ - { \ + { args \ CY ## name ## Assign(CYExpression *lhs, CYExpression *rhs) : \ CYAssignment(lhs, rhs) \ { \ @@ -1535,7 +1561,7 @@ CYPrefix_(false, "!", LogicalNot) CYInfix_(false, 5, "*", Multiply) CYInfix_(false, 5, "/", Divide) CYInfix_(false, 5, "%", Modulus) -CYInfix_(false, 6, "+", Add) +CYInfix_(false, 6, "+", Add, CYReplace) CYInfix_(false, 6, "-", Subtract) CYInfix_(false, 7, "<<", ShiftLeft) CYInfix_(false, 7, ">>", ShiftRightSigned) diff --git a/Pooling.hpp b/Pooling.hpp index 84adad2..4fa5dae 100644 --- a/Pooling.hpp +++ b/Pooling.hpp @@ -1,3 +1,42 @@ +/* Cycript - Remote Execution Server and Disassembler + * Copyright (C) 2009 Jay Freeman (saurik) +*/ + +/* Modified BSD License {{{ */ +/* + * Redistribution and use in source and binary + * forms, with or without modification, are permitted + * provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the + * above copyright notice, this list of conditions + * and the following disclaimer. + * 2. Redistributions in binary form must reproduce the + * above copyright notice, this list of conditions + * and the following disclaimer in the documentation + * and/or other materials provided with the + * distribution. + * 3. The name of the author may not be used to endorse + * or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, + * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR + * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ +/* }}} */ + #ifndef CYPOOLING_HPP #define CYPOOLING_HPP diff --git a/Replace.cpp b/Replace.cpp index 987122b..0360fcf 100644 --- a/Replace.cpp +++ b/Replace.cpp @@ -1,9 +1,78 @@ +/* Cycript - Remove Execution Server and Disassembler + * Copyright (C) 2009 Jay Freeman (saurik) +*/ + +/* Modified BSD License {{{ */ +/* + * Redistribution and use in source and binary + * forms, with or without modification, are permitted + * provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the + * above copyright notice, this list of conditions + * and the following disclaimer. + * 2. Redistributions in binary form must reproduce the + * above copyright notice, this list of conditions + * and the following disclaimer in the documentation + * and/or other materials provided with the + * distribution. + * 3. The name of the author may not be used to endorse + * or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, + * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR + * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ +/* }}} */ + #include "Parser.hpp" #include #include "Replace.hpp" +CYExpression *CYAdd::Replace(CYContext &context) { + CYInfix::Replace(context); + + CYExpression *lhp(lhs_->Primitive(context)); + CYExpression *rhp(rhs_->Primitive(context)); + + CYString *lhs(dynamic_cast(lhp)); + CYString *rhs(dynamic_cast(rhp)); + + if (lhs != NULL || rhs != NULL) { + if (lhs == NULL) { + lhs = lhp->String(context); + if (lhs == NULL) + return NULL; + } else if (rhs == NULL) { + rhs = rhp->String(context); + if (rhs == NULL) + return NULL; + } + + return lhs->Concat(context, rhs); + } + + if (CYNumber *lhn = lhp->Number(context)) + if (CYNumber *rhn = rhp->Number(context)) + return $D(lhn->Value() + rhn->Value()); + + return NULL; +} + CYExpression *CYAddressOf::Replace(CYContext &context) { CYPrefix::Replace(context); return $C0($M(rhs_, $S("$cya"))); @@ -147,6 +216,14 @@ CYExpression *CYExpression::ReplaceAll(CYContext &context) { $T(NULL) return replace; } +CYNumber *CYFalse::Number(CYContext &context) { + return $D(0); +} + +CYString *CYFalse::String(CYContext &context) { + return $S("false"); +} + void CYFinally::Replace(CYContext &context) { $T() code_.Replace(context); } @@ -263,6 +340,23 @@ CYExpression *CYNew::Replace(CYContext &context) { return NULL; } +CYNumber *CYNull::Number(CYContext &context) { + return $D(0); +} + +CYString *CYNull::String(CYContext &context) { + return $S("null"); +} + +CYNumber *CYNumber::Number(CYContext &context) { + return this; +} + +CYString *CYNumber::String(CYContext &context) { + // XXX: there is a precise algorithm for this + return $S(apr_psprintf(context.pool_, "%.17g", Value())); +} + CYExpression *CYObject::Replace(CYContext &context) { properties_->Replace(context); return NULL; @@ -304,6 +398,24 @@ CYStatement *CYStatement::ReplaceAll(CYContext &context) { $T(NULL) return replace; } +CYString *CYString::Concat(CYContext &context, CYString *rhs) const { + size_t size(size_ + rhs->size_); + char *value(new(context.pool_) char[size + 1]); + memcpy(value, value_, size_); + memcpy(value + size_, rhs->value_, rhs->size_); + value[size] = '\0'; + return $S(value); +} + +CYNumber *CYString::Number(CYContext &context) { + // XXX: there is a precise algorithm for this + return NULL; +} + +CYString *CYString::String(CYContext &context) { + return this; +} + CYStatement *CYSwitch::Replace(CYContext &context) { context.Replace(value_); clauses_->Replace(context); @@ -323,6 +435,14 @@ CYExpression *CYTrivial::Replace(CYContext &context) { return NULL; } +CYNumber *CYTrue::Number(CYContext &context) { + return $D(1); +} + +CYString *CYTrue::String(CYContext &context) { + return $S("true"); +} + CYStatement *CYTry::Replace(CYContext &context) { code_.Replace(context); catch_->Replace(context); diff --git a/Replace.hpp b/Replace.hpp index de3c33a..c62a232 100644 --- a/Replace.hpp +++ b/Replace.hpp @@ -1,3 +1,42 @@ +/* Cycript - Remote Execution Server and Disassembler + * Copyright (C) 2009 Jay Freeman (saurik) +*/ + +/* Modified BSD License {{{ */ +/* + * Redistribution and use in source and binary + * forms, with or without modification, are permitted + * provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the + * above copyright notice, this list of conditions + * and the following disclaimer. + * 2. Redistributions in binary form must reproduce the + * above copyright notice, this list of conditions + * and the following disclaimer in the documentation + * and/or other materials provided with the + * distribution. + * 3. The name of the author may not be used to endorse + * or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, + * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR + * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ +/* }}} */ + #ifndef REPLACE_HPP #define REPLACE_HPP diff --git a/cycript.hpp b/cycript.hpp index 3d70f5b..8a96aae 100644 --- a/cycript.hpp +++ b/cycript.hpp @@ -1,4 +1,4 @@ -/* Cycript - Remove Execution Server and Disassembler +/* Cycript - Remote Execution Server and Disassembler * Copyright (C) 2009 Jay Freeman (saurik) */ diff --git a/sig/ffi_type.hpp b/sig/ffi_type.hpp index 0257d27..c30ffe6 100644 --- a/sig/ffi_type.hpp +++ b/sig/ffi_type.hpp @@ -1,4 +1,4 @@ -/* Cycript - Remove Execution Server and Disassembler +/* Cycript - Remote Execution Server and Disassembler * Copyright (C) 2009 Jay Freeman (saurik) */ diff --git a/sig/parse.hpp b/sig/parse.hpp index c569a07..15b6938 100644 --- a/sig/parse.hpp +++ b/sig/parse.hpp @@ -1,4 +1,4 @@ -/* Cycript - Remove Execution Server and Disassembler +/* Cycript - Remote Execution Server and Disassembler * Copyright (C) 2009 Jay Freeman (saurik) */ diff --git a/sig/types.hpp b/sig/types.hpp index 6af8fc6..0ffb56a 100644 --- a/sig/types.hpp +++ b/sig/types.hpp @@ -1,4 +1,4 @@ -/* Cycript - Remove Execution Server and Disassembler +/* Cycript - Remote Execution Server and Disassembler * Copyright (C) 2009 Jay Freeman (saurik) */ -- 2.47.2