1 /* Cycript - Remote Execution Server and Disassembler
2 * Copyright (C) 2009 Jay Freeman (saurik)
5 /* Modified BSD License {{{ */
7 * Redistribution and use in source and binary
8 * forms, with or without modification, are permitted
9 * provided that the following conditions are met:
11 * 1. Redistributions of source code must retain the
12 * above copyright notice, this list of conditions
13 * and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the
15 * above copyright notice, this list of conditions
16 * and the following disclaimer in the documentation
17 * and/or other materials provided with the
19 * 3. The name of the author may not be used to endorse
20 * or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS''
24 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
25 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
26 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
31 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
34 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
35 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
36 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 #include "Replace.hpp"
41 #include "ObjectiveC.hpp"
43 #include <objc/runtime.h>
46 /* Objective-C Output {{{ */
47 void CYCategory::Output(CYOutput &out, CYFlags flags) const {
48 out << "(function($cys,$cyp,$cyc,$cyn,$cyt){";
49 out << "$cyp=object_getClass($cys);";
51 if (messages_ != NULL)
52 messages_->Output(out, true);
54 name_->ClassName(out, true);
59 void CYClass::Output(CYOutput &out, CYFlags flags) const {
60 // XXX: I don't necc. need the ()s
61 out << "(function($cys,$cyp,$cyc,$cyn,$cyt,$cym){";
62 out << "$cyp=object_getClass($cys);";
63 out << "$cyc=objc_allocateClassPair($cys,";
65 name_->ClassName(out, false);
67 out << "$cyq(\"CY$\")";
69 out << "$cym=object_getClass($cyc);";
72 if (messages_ != NULL)
73 messages_->Output(out, false);
74 out << "objc_registerClassPair($cyc);";
75 out << "return $cyc;";
78 super_->Output(out, CYPA, CYNoFlags);
84 void CYClassExpression::Output(CYOutput &out, CYFlags flags) const {
85 CYClass::Output(out, flags);
88 void CYClassStatement::Output(CYOutput &out, CYFlags flags) const {
89 CYClass::Output(out, flags);
92 void CYField::Output(CYOutput &out) const {
95 void CYMessage::Output(CYOutput &out, bool replace) const {
96 out << (instance_ ? '-' : '+');
98 for (CYMessageParameter *parameter(parameters_); parameter != NULL; parameter = parameter->next_)
99 if (parameter->tag_ != NULL) {
100 out << ' ' << *parameter->tag_;
101 if (parameter->name_ != NULL)
102 out << ':' << *parameter->name_;
108 void CYSelector::Output(CYOutput &out, CYFlags flags) const {
109 out << "@selector" << '(' << name_ << ')';
112 void CYSelectorPart::Output(CYOutput &out) const {
119 void CYSend::Output(CYOutput &out, CYFlags flags) const {
122 self_->Output(out, CYPA, CYNoFlags);
124 for (CYArgument *argument(arguments_); argument != NULL; argument = argument->next_)
125 if (argument->name_ != NULL) {
126 out << ' ' << *argument->name_;
127 if (argument->value_ != NULL)
128 out << ':' << *argument->value_;
134 /* Objective-C Replace {{{ */
135 CYStatement *CYCategory::Replace(CYContext &context) {
136 CYVariable *cyc($V("$cyc")), *cys($V("$cys"));
138 return $E($C1($F(NULL, $P5("$cys", "$cyp", "$cyc", "$cyn", "$cyt"), $$->*
139 $E($ CYAssign($V("$cyp"), $C1($V("object_getClass"), cys)))->*
140 $E($ CYAssign(cyc, cys))->*
141 messages_->Replace(context, true)
142 ), name_->ClassName(context, true)));
145 CYExpression *CYClass::Replace_(CYContext &context) {
146 CYVariable *cyc($V("$cyc")), *cys($V("$cys"));
148 CYExpression *name(name_ != NULL ? name_->ClassName(context, false) : $C1($V("$cyq"), $S("CY$")));
150 return $C1($F(NULL, $P6("$cys", "$cyp", "$cyc", "$cyn", "$cyt", "$cym"), $$->*
151 $E($ CYAssign($V("$cyp"), $C1($V("object_getClass"), cys)))->*
152 $E($ CYAssign(cyc, $C3($V("objc_allocateClassPair"), cys, name, $D(0))))->*
153 $E($ CYAssign($V("$cym"), $C1($V("object_getClass"), cyc)))->*
154 fields_->Replace(context)->*
155 messages_->Replace(context, false)->*
156 $E($C1($V("objc_registerClassPair"), cyc))->*
158 ), super_ == NULL ? $ CYNull() : super_);
161 CYExpression *CYClassExpression::Replace(CYContext &context) {
162 return Replace_(context);
165 CYStatement *CYClassStatement::Replace(CYContext &context) {
166 return $E(Replace_(context));
169 CYStatement *CYField::Replace(CYContext &context) const {
173 CYStatement *CYMessage::Replace(CYContext &context, bool replace) const { $T(NULL)
174 CYVariable *cyn($V("$cyn"));
175 CYVariable *cyt($V("$cyt"));
177 return $ CYBlock($$->*
178 next_->Replace(context, replace)->*
179 $E($ CYAssign(cyn, parameters_->Selector(context)))->*
180 $E($ CYAssign(cyt, $C1($M(cyn, $S("type")), $V(instance_ ? "$cys" : "$cyp"))))->*
181 $E($C4($V(replace ? "class_replaceMethod" : "class_addMethod"),
182 $V(instance_ ? "$cyc" : "$cym"),
184 $N2($V("Functor"), $F(NULL, $P2("self", "_cmd", parameters_->Parameters(context)), $$->*
185 $ CYReturn($C1($M($F(NULL, NULL, code_), $S("call")), $V("self")))
192 CYFunctionParameter *CYMessageParameter::Parameters(CYContext &context) const { $T(NULL)
193 CYFunctionParameter *next(next_->Parameters(context));
194 return name_ == NULL ? next : $ CYFunctionParameter(name_, next);
197 CYSelector *CYMessageParameter::Selector(CYContext &context) const {
198 return $ CYSelector(SelectorPart(context));
201 CYSelectorPart *CYMessageParameter::SelectorPart(CYContext &context) const { $T(NULL)
202 CYSelectorPart *next(next_->SelectorPart(context));
203 return tag_ == NULL ? next : $ CYSelectorPart(tag_, name_ != NULL, next);
206 CYExpression *CYSelector::Replace(CYContext &context) {
207 return $N1($V("Selector"), name_->Replace(context));
210 CYString *CYSelectorPart::Replace(CYContext &context) {
211 std::ostringstream str;
212 for (const CYSelectorPart *part(this); part != NULL; part = part->next_) {
213 if (part->name_ != NULL)
214 str << part->name_->Value();
218 return $S(apr_pstrdup(context.pool_, str.str().c_str()));
221 CYExpression *CYSend::Replace(CYContext &context) {
222 std::ostringstream name;
223 CYArgument **argument(&arguments_);
225 while (*argument != NULL) {
226 if ((*argument)->name_ != NULL) {
227 name << *(*argument)->name_;
228 (*argument)->name_ = NULL;
229 if ((*argument)->value_ != NULL)
233 if ((*argument)->value_ == NULL)
234 *argument = (*argument)->next_;
236 argument = &(*argument)->next_;
239 SEL sel(sel_registerName(name.str().c_str()));
240 double address(static_cast<double>(reinterpret_cast<uintptr_t>(sel)));
242 return $C2($V("objc_msgSend"), self_, $D(address), arguments_);