]> git.saurik.com Git - cycript.git/blame - ObjectiveC/Replace.mm
Removed a virtual to avoid an optimizer vtable bug.
[cycript.git] / ObjectiveC / Replace.mm
CommitLineData
d15b59f5 1/* Cycript - Inlining/Optimizing JavaScript Compiler
b53b30c1
JF
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 40#include "Replace.hpp"
3c1c3635 41#include "ObjectiveC/Syntax.hpp"
4de0686f 42
aabea98c 43#include <Foundation/Foundation.h>
4de0686f
JF
44#include <sstream>
45
4de0686f
JF
46CYStatement *CYCategory::Replace(CYContext &context) {
47 CYVariable *cyc($V("$cyc")), *cys($V("$cys"));
48
49 return $E($C1($F(NULL, $P5("$cys", "$cyp", "$cyc", "$cyn", "$cyt"), $$->*
50 $E($ CYAssign($V("$cyp"), $C1($V("object_getClass"), cys)))->*
51 $E($ CYAssign(cyc, cys))->*
52 messages_->Replace(context, true)
53 ), name_->ClassName(context, true)));
54}
55
56CYExpression *CYClass::Replace_(CYContext &context) {
57 CYVariable *cyc($V("$cyc")), *cys($V("$cys"));
58
59 CYExpression *name(name_ != NULL ? name_->ClassName(context, false) : $C1($V("$cyq"), $S("CY$")));
60
61 return $C1($F(NULL, $P6("$cys", "$cyp", "$cyc", "$cyn", "$cyt", "$cym"), $$->*
62 $E($ CYAssign($V("$cyp"), $C1($V("object_getClass"), cys)))->*
63 $E($ CYAssign(cyc, $C3($V("objc_allocateClassPair"), cys, name, $D(0))))->*
64 $E($ CYAssign($V("$cym"), $C1($V("object_getClass"), cyc)))->*
65 fields_->Replace(context)->*
66 messages_->Replace(context, false)->*
67 $E($C1($V("objc_registerClassPair"), cyc))->*
68 $ CYReturn(cyc)
69 ), super_ == NULL ? $ CYNull() : super_);
70}
71
72CYExpression *CYClassExpression::Replace(CYContext &context) {
73 return Replace_(context);
74}
75
76CYStatement *CYClassStatement::Replace(CYContext &context) {
77 return $E(Replace_(context));
78}
79
80CYStatement *CYField::Replace(CYContext &context) const {
81 return NULL;
82}
83
84CYStatement *CYMessage::Replace(CYContext &context, bool replace) const { $T(NULL)
85 CYVariable *cyn($V("$cyn"));
86 CYVariable *cyt($V("$cyt"));
cacd1a88
JF
87 CYVariable *self($V("self"));
88 CYVariable *_class($V(instance_ ? "$cys" : "$cyp"));
4de0686f
JF
89
90 return $ CYBlock($$->*
91 next_->Replace(context, replace)->*
92 $E($ CYAssign(cyn, parameters_->Selector(context)))->*
cacd1a88 93 $E($ CYAssign(cyt, $C1($M(cyn, $S("type")), _class)))->*
4de0686f
JF
94 $E($C4($V(replace ? "class_replaceMethod" : "class_addMethod"),
95 $V(instance_ ? "$cyc" : "$cym"),
96 cyn,
97 $N2($V("Functor"), $F(NULL, $P2("self", "_cmd", parameters_->Parameters(context)), $$->*
39f3897f 98 $ CYVar($L1($L($I("$cyr"), $N2($V("Super"), self, _class))))->*
cacd1a88 99 $ CYReturn($C1($M($F(NULL, NULL, code_), $S("call")), self))
4de0686f
JF
100 ), cyt),
101 cyt
102 ))
103 );
104}
105
106CYFunctionParameter *CYMessageParameter::Parameters(CYContext &context) const { $T(NULL)
107 CYFunctionParameter *next(next_->Parameters(context));
108 return name_ == NULL ? next : $ CYFunctionParameter(name_, next);
109}
110
111CYSelector *CYMessageParameter::Selector(CYContext &context) const {
112 return $ CYSelector(SelectorPart(context));
113}
114
115CYSelectorPart *CYMessageParameter::SelectorPart(CYContext &context) const { $T(NULL)
116 CYSelectorPart *next(next_->SelectorPart(context));
117 return tag_ == NULL ? next : $ CYSelectorPart(tag_, name_ != NULL, next);
118}
119
120CYExpression *CYSelector::Replace(CYContext &context) {
121 return $N1($V("Selector"), name_->Replace(context));
122}
123
124CYString *CYSelectorPart::Replace(CYContext &context) {
125 std::ostringstream str;
126 for (const CYSelectorPart *part(this); part != NULL; part = part->next_) {
127 if (part->name_ != NULL)
128 str << part->name_->Value();
129 if (part->value_)
130 str << ':';
131 }
132 return $S(apr_pstrdup(context.pool_, str.str().c_str()));
133}
134
cacd1a88 135CYExpression *CYSendDirect::Replace(CYContext &context) {
4de0686f
JF
136 std::ostringstream name;
137 CYArgument **argument(&arguments_);
138
139 while (*argument != NULL) {
140 if ((*argument)->name_ != NULL) {
141 name << *(*argument)->name_;
142 (*argument)->name_ = NULL;
143 if ((*argument)->value_ != NULL)
144 name << ':';
145 }
146
147 if ((*argument)->value_ == NULL)
148 *argument = (*argument)->next_;
149 else
150 argument = &(*argument)->next_;
151 }
152
153 SEL sel(sel_registerName(name.str().c_str()));
154 double address(static_cast<double>(reinterpret_cast<uintptr_t>(sel)));
155
156 return $C2($V("objc_msgSend"), self_, $D(address), arguments_);
157}
cacd1a88
JF
158
159CYExpression *CYSendSuper::Replace(CYContext &context) {
160 return $ CYSendDirect($V("$cyr"), arguments_);
161}