X-Git-Url: https://git.saurik.com/cycript.git/blobdiff_plain/306919689b01f196599cc0c2109a1b589de88e93..a2b8d7ada7c9b527d46e781a217292603379e9e0:/ObjectiveC/Replace.cpp diff --git a/ObjectiveC/Replace.cpp b/ObjectiveC/Replace.cpp index 85b7c9d..c3a885c 100644 --- a/ObjectiveC/Replace.cpp +++ b/ObjectiveC/Replace.cpp @@ -1,40 +1,22 @@ -/* Cycript - Inlining/Optimizing JavaScript Compiler - * Copyright (C) 2009 Jay Freeman (saurik) +/* Cycript - Optimizing JavaScript Compiler/Runtime + * Copyright (C) 2009-2013 Jay Freeman (saurik) */ -/* Modified BSD License {{{ */ +/* GNU General Public License, Version 3 {{{ */ /* - * Redistribution and use in source and binary - * forms, with or without modification, are permitted - * provided that the following conditions are met: + * Cycript is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation, either version 3 of the License, + * or (at your option) any later version. * - * 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. + * Cycript is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. * - * 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. -*/ + * You should have received a copy of the GNU General Public License + * along with Cycript. If not, see . +**/ /* }}} */ #include "Replace.hpp" @@ -42,10 +24,28 @@ #include +static CYExpression *MessageType(CYContext &context, CYExpression *type, CYMessageParameter *next, CYExpression *extra = NULL) { + if (type == NULL) + return NULL; + + CYExpression *left($C0($M(type, $S("toString")))); + if (extra != NULL) + left = $ CYAdd(left, extra); + + if (next == NULL || next->name_ == NULL) + return left; + + CYExpression *right(next->TypeSignature(context)); + if (right == NULL) + return NULL; + + return $ CYAdd(left, right); +} + CYStatement *CYCategory::Replace(CYContext &context) { CYVariable *cyc($V("$cyc")), *cys($V("$cys")); - return $E($C1($F(NULL, $P5("$cys", "$cyp", "$cyc", "$cyn", "$cyt"), $$->* + return $E($C1($F(NULL, $P6($L("$cys"), $L("$cyp"), $L("$cyc"), $L("$cyn"), $L("$cyt"), $L("$cym")), $$->* $E($ CYAssign($V("$cyp"), $C1($V("object_getClass"), cys)))->* $E($ CYAssign(cyc, cys))->* $E($ CYAssign($V("$cym"), $C1($V("object_getClass"), cyc)))->* @@ -58,7 +58,7 @@ CYExpression *CYClass::Replace_(CYContext &context) { CYExpression *name(name_ != NULL ? name_->ClassName(context, false) : $C1($V("$cyq"), $S("CY$"))); - return $C1($F(NULL, $P6("$cys", "$cyp", "$cyc", "$cyn", "$cyt", "$cym"), $$->* + return $C1($F(NULL, $P6($L("$cys"), $L("$cyp"), $L("$cyc"), $L("$cyn"), $L("$cyt"), $L("$cym")), $$->* $E($ CYAssign($V("$cyp"), $C1($V("object_getClass"), cys)))->* $E($ CYAssign(cyc, $C3($V("objc_allocateClassPair"), cys, name, $D(0))))->* $E($ CYAssign($V("$cym"), $C1($V("object_getClass"), cyc)))->* @@ -78,12 +78,22 @@ CYStatement *CYClassStatement::Replace(CYContext &context) { return $E(Replace_(context)); } -CYStatement *CYField::Replace(CYContext &context) const { - return NULL; +CYStatement *CYField::Replace(CYContext &context) const { $T(NULL) + CYVariable *cyn($V("$cyn")); + CYVariable *cyt($V("$cyt")); + + CYExpression *type($C0($M(type_, $S("toString")))); + + return $ CYBlock($$->* + next_->Replace(context)->* + $E($ CYAssign(cyt, type))->* + $E($ CYAssign(cyn, $N1($V("Type"), cyt)))->* + $E($C5($V("class_addIvar"), $V("$cyc"), $S(name_->Word()), $M(cyn, $S("size")), $M(cyn, $S("alignment")), cyt)) + ); } CYStatement *CYImport::Replace(CYContext &context) { - return this; + return $ CYVar($L1($L(module_->part_->Word(), $C1($V("require"), module_->Replace(context, "/"))))); } CYStatement *CYMessage::Replace(CYContext &context, bool replace) const { $T(NULL) @@ -92,15 +102,17 @@ CYStatement *CYMessage::Replace(CYContext &context, bool replace) const { $T(NUL CYVariable *self($V("self")); CYVariable *_class($V(instance_ ? "$cys" : "$cyp")); + CYExpression *type(TypeSignature(context) ?: $C1($M(cyn, $S("type")), _class)); + return $ CYBlock($$->* next_->Replace(context, replace)->* $E($ CYAssign(cyn, parameters_->Selector(context)))->* - $E($ CYAssign(cyt, $C1($M(cyn, $S("type")), _class)))->* + $E($ CYAssign(cyt, type))->* $E($C4($V(replace ? "class_replaceMethod" : "class_addMethod"), $V(instance_ ? "$cyc" : "$cym"), cyn, - $N2($V("Functor"), $F(NULL, $P2("self", "_cmd", parameters_->Parameters(context)), $$->* - $ CYVar($L1($L($I("$cyr"), $N2($V("Super"), self, _class))))->* + $N2($V("Functor"), $F(NULL, $P2($L("self"), $L("_cmd"), parameters_->Parameters(context)), $$->* + $ CYVar($L1($L("$cyr", $N2($V("Super"), self, _class))))->* $ CYReturn($C1($M($F(NULL, NULL, code_), $S("call")), self)) ), cyt), cyt @@ -108,9 +120,13 @@ CYStatement *CYMessage::Replace(CYContext &context, bool replace) const { $T(NUL ); } +CYExpression *CYMessage::TypeSignature(CYContext &context) const { + return MessageType(context, type_, parameters_, $S("@:")); +} + CYFunctionParameter *CYMessageParameter::Parameters(CYContext &context) const { $T(NULL) CYFunctionParameter *next(next_->Parameters(context)); - return name_ == NULL ? next : $ CYFunctionParameter(name_, next); + return name_ == NULL ? next : $ CYFunctionParameter($ CYDeclaration(name_), next); } CYSelector *CYMessageParameter::Selector(CYContext &context) const { @@ -122,6 +138,24 @@ CYSelectorPart *CYMessageParameter::SelectorPart(CYContext &context) const { $T( return tag_ == NULL ? next : $ CYSelectorPart(tag_, name_ != NULL, next); } +CYExpression *CYMessageParameter::TypeSignature(CYContext &context) const { + return MessageType(context, type_, next_); +} + +CYString *CYModule::Replace(CYContext &context, const char *separator) const { + if (next_ == NULL) + return $ CYString(part_); + return $ CYString($pool.strcat(next_->Replace(context, separator)->Value(), separator, part_->Word(), NULL)); +} + +CYExpression *CYBox::Replace(CYContext &context) { + return $C1($M($V("Instance"), $S("box")), value_); +} + +CYExpression *CYObjCBlock::Replace(CYContext &context) { + return $C1($ CYEncodedType(($ CYTypedIdentifier(*typed_))->Modify($ CYTypeBlockWith(parameters_))), $ CYFunctionExpression(NULL, parameters_->Parameters(context), statements_)); +} + CYStatement *CYProtocol::Replace(CYContext &context) const { $T(NULL) return $ CYBlock($$->* next_->Replace(context)->* @@ -132,18 +166,18 @@ CYStatement *CYProtocol::Replace(CYContext &context) const { $T(NULL) } CYExpression *CYSelector::Replace(CYContext &context) { - return $N1($V("Selector"), name_->Replace(context)); + return $C1($V("sel_registerName"), name_->Replace(context)); } CYString *CYSelectorPart::Replace(CYContext &context) { std::ostringstream str; - for (const CYSelectorPart *part(this); part != NULL; part = part->next_) { + CYForEach (part, this) { if (part->name_ != NULL) str << part->name_->Word(); if (part->value_) str << ':'; } - return $S(apr_pstrdup($pool, str.str().c_str())); + return $S($pool.strdup(str.str().c_str())); } CYExpression *CYSendDirect::Replace(CYContext &context) { @@ -172,5 +206,5 @@ CYExpression *CYSendDirect::Replace(CYContext &context) { } CYExpression *CYSendSuper::Replace(CYContext &context) { - return $ CYSendDirect($V("cyr"), arguments_); + return $ CYSendDirect($V("$cyr"), arguments_); }