]> git.saurik.com Git - cycript.git/blame - ObjectiveC.mm
Use .length in order to deal with Array.
[cycript.git] / ObjectiveC.mm
CommitLineData
b53b30c1
JF
1/* Cycript - Remote Execution Server and Disassembler
2 * Copyright (C) 2009 Jay Freeman (saurik)
3*/
4
5/* Modified BSD License {{{ */
6/*
7 * Redistribution and use in source and binary
8 * forms, with or without modification, are permitted
9 * provided that the following conditions are met:
10 *
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
18 * distribution.
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.
22 *
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.
37*/
38/* }}} */
39
4de0686f
JF
40#include "Replace.hpp"
41#include "ObjectiveC.hpp"
42
43#include <objc/runtime.h>
44#include <sstream>
45
b53b30c1 46/* Objective-C Output {{{ */
4de0686f
JF
47void CYCategory::Output(CYOutput &out, CYFlags flags) const {
48 out << "(function($cys,$cyp,$cyc,$cyn,$cyt){";
49 out << "$cyp=object_getClass($cys);";
50 out << "$cyc=$cys;";
51 if (messages_ != NULL)
52 messages_->Output(out, true);
53 out << "})(";
54 name_->ClassName(out, true);
55 out << ')';
56 out << ';';
57}
58
59void 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,";
64 if (name_ != NULL)
65 name_->ClassName(out, false);
66 else
67 out << "$cyq(\"CY$\")";
68 out << ",0);";
69 out << "$cym=object_getClass($cyc);";
70 if (fields_ != NULL)
71 fields_->Output(out);
72 if (messages_ != NULL)
73 messages_->Output(out, false);
74 out << "objc_registerClassPair($cyc);";
75 out << "return $cyc;";
76 out << "}(";
77 if (super_ != NULL)
78 super_->Output(out, CYPA, CYNoFlags);
79 else
80 out << "null";
81 out << "))";
82}
83
84void CYClassExpression::Output(CYOutput &out, CYFlags flags) const {
85 CYClass::Output(out, flags);
86}
87
88void CYClassStatement::Output(CYOutput &out, CYFlags flags) const {
89 CYClass::Output(out, flags);
90}
91
92void CYField::Output(CYOutput &out) const {
4de0686f
JF
93}
94
95void CYMessage::Output(CYOutput &out, bool replace) const {
4644480a
JF
96 out << (instance_ ? '-' : '+');
97
4de0686f
JF
98 for (CYMessageParameter *parameter(parameters_); parameter != NULL; parameter = parameter->next_)
99 if (parameter->tag_ != NULL) {
4644480a 100 out << ' ' << *parameter->tag_;
4de0686f 101 if (parameter->name_ != NULL)
4644480a 102 out << ':' << *parameter->name_;
4de0686f 103 }
4644480a
JF
104
105 out << code_;
4de0686f
JF
106}
107
108void CYSelector::Output(CYOutput &out, CYFlags flags) const {
109 out << "@selector" << '(' << name_ << ')';
110}
111
112void CYSelectorPart::Output(CYOutput &out) const {
113 out << name_;
114 if (value_)
115 out << ':';
116 out << next_;
117}
118
119void CYSend::Output(CYOutput &out, CYFlags flags) const {
4de0686f
JF
120 for (CYArgument *argument(arguments_); argument != NULL; argument = argument->next_)
121 if (argument->name_ != NULL) {
4644480a 122 out << ' ' << *argument->name_;
4de0686f 123 if (argument->value_ != NULL)
4644480a 124 out << ':' << *argument->value_;
4de0686f 125 }
cacd1a88
JF
126}
127
128void CYSendDirect::Output(CYOutput &out, CYFlags flags) const {
129 out << '[';
130 self_->Output(out, CYPA, CYNoFlags);
131 CYSend::Output(out, flags);
132 out << ']';
133}
4de0686f 134
cacd1a88
JF
135void CYSendSuper::Output(CYOutput &out, CYFlags flags) const {
136 out << '[' << "super";
137 CYSend::Output(out, flags);
4644480a 138 out << ']';
4de0686f 139}
b53b30c1
JF
140/* }}} */
141/* Objective-C Replace {{{ */
4de0686f
JF
142CYStatement *CYCategory::Replace(CYContext &context) {
143 CYVariable *cyc($V("$cyc")), *cys($V("$cys"));
144
145 return $E($C1($F(NULL, $P5("$cys", "$cyp", "$cyc", "$cyn", "$cyt"), $$->*
146 $E($ CYAssign($V("$cyp"), $C1($V("object_getClass"), cys)))->*
147 $E($ CYAssign(cyc, cys))->*
148 messages_->Replace(context, true)
149 ), name_->ClassName(context, true)));
150}
151
152CYExpression *CYClass::Replace_(CYContext &context) {
153 CYVariable *cyc($V("$cyc")), *cys($V("$cys"));
154
155 CYExpression *name(name_ != NULL ? name_->ClassName(context, false) : $C1($V("$cyq"), $S("CY$")));
156
157 return $C1($F(NULL, $P6("$cys", "$cyp", "$cyc", "$cyn", "$cyt", "$cym"), $$->*
158 $E($ CYAssign($V("$cyp"), $C1($V("object_getClass"), cys)))->*
159 $E($ CYAssign(cyc, $C3($V("objc_allocateClassPair"), cys, name, $D(0))))->*
160 $E($ CYAssign($V("$cym"), $C1($V("object_getClass"), cyc)))->*
161 fields_->Replace(context)->*
162 messages_->Replace(context, false)->*
163 $E($C1($V("objc_registerClassPair"), cyc))->*
164 $ CYReturn(cyc)
165 ), super_ == NULL ? $ CYNull() : super_);
166}
167
168CYExpression *CYClassExpression::Replace(CYContext &context) {
169 return Replace_(context);
170}
171
172CYStatement *CYClassStatement::Replace(CYContext &context) {
173 return $E(Replace_(context));
174}
175
176CYStatement *CYField::Replace(CYContext &context) const {
177 return NULL;
178}
179
180CYStatement *CYMessage::Replace(CYContext &context, bool replace) const { $T(NULL)
181 CYVariable *cyn($V("$cyn"));
182 CYVariable *cyt($V("$cyt"));
cacd1a88
JF
183 CYVariable *self($V("self"));
184 CYVariable *_class($V(instance_ ? "$cys" : "$cyp"));
4de0686f
JF
185
186 return $ CYBlock($$->*
187 next_->Replace(context, replace)->*
188 $E($ CYAssign(cyn, parameters_->Selector(context)))->*
cacd1a88 189 $E($ CYAssign(cyt, $C1($M(cyn, $S("type")), _class)))->*
4de0686f
JF
190 $E($C4($V(replace ? "class_replaceMethod" : "class_addMethod"),
191 $V(instance_ ? "$cyc" : "$cym"),
192 cyn,
193 $N2($V("Functor"), $F(NULL, $P2("self", "_cmd", parameters_->Parameters(context)), $$->*
cacd1a88
JF
194 $ CYVar($ CYDeclarations($ CYDeclaration($I("$cyr"), $N2($V("Super"), self, _class))))->*
195 $ CYReturn($C1($M($F(NULL, NULL, code_), $S("call")), self))
4de0686f
JF
196 ), cyt),
197 cyt
198 ))
199 );
200}
201
202CYFunctionParameter *CYMessageParameter::Parameters(CYContext &context) const { $T(NULL)
203 CYFunctionParameter *next(next_->Parameters(context));
204 return name_ == NULL ? next : $ CYFunctionParameter(name_, next);
205}
206
207CYSelector *CYMessageParameter::Selector(CYContext &context) const {
208 return $ CYSelector(SelectorPart(context));
209}
210
211CYSelectorPart *CYMessageParameter::SelectorPart(CYContext &context) const { $T(NULL)
212 CYSelectorPart *next(next_->SelectorPart(context));
213 return tag_ == NULL ? next : $ CYSelectorPart(tag_, name_ != NULL, next);
214}
215
216CYExpression *CYSelector::Replace(CYContext &context) {
217 return $N1($V("Selector"), name_->Replace(context));
218}
219
220CYString *CYSelectorPart::Replace(CYContext &context) {
221 std::ostringstream str;
222 for (const CYSelectorPart *part(this); part != NULL; part = part->next_) {
223 if (part->name_ != NULL)
224 str << part->name_->Value();
225 if (part->value_)
226 str << ':';
227 }
228 return $S(apr_pstrdup(context.pool_, str.str().c_str()));
229}
230
cacd1a88 231CYExpression *CYSendDirect::Replace(CYContext &context) {
4de0686f
JF
232 std::ostringstream name;
233 CYArgument **argument(&arguments_);
234
235 while (*argument != NULL) {
236 if ((*argument)->name_ != NULL) {
237 name << *(*argument)->name_;
238 (*argument)->name_ = NULL;
239 if ((*argument)->value_ != NULL)
240 name << ':';
241 }
242
243 if ((*argument)->value_ == NULL)
244 *argument = (*argument)->next_;
245 else
246 argument = &(*argument)->next_;
247 }
248
249 SEL sel(sel_registerName(name.str().c_str()));
250 double address(static_cast<double>(reinterpret_cast<uintptr_t>(sel)));
251
252 return $C2($V("objc_msgSend"), self_, $D(address), arguments_);
253}
cacd1a88
JF
254
255CYExpression *CYSendSuper::Replace(CYContext &context) {
256 return $ CYSendDirect($V("$cyr"), arguments_);
257}
b53b30c1 258/* }}} */