]> git.saurik.com Git - cycript.git/blob - ObjectiveC/Replace.cpp
Bridge NSBlock as a callable JavaScript function.
[cycript.git] / ObjectiveC / Replace.cpp
1 /* Cycript - Optimizing JavaScript Compiler/Runtime
2 * Copyright (C) 2009-2010 Jay Freeman (saurik)
3 */
4
5 /* GNU Lesser General Public License, Version 3 {{{ */
6 /*
7 * Cycript is free software: you can redistribute it and/or modify it under
8 * the terms of the GNU Lesser General Public License as published by the
9 * Free Software Foundation, either version 3 of the License, or (at your
10 * option) any later version.
11 *
12 * Cycript is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15 * License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with Cycript. If not, see <http://www.gnu.org/licenses/>.
19 **/
20 /* }}} */
21
22 #include "Replace.hpp"
23 #include "ObjectiveC/Syntax.hpp"
24
25 #include <sstream>
26
27 CYStatement *CYCategory::Replace(CYContext &context) {
28 CYVariable *cyc($V("$cyc")), *cys($V("$cys"));
29
30 return $E($C1($F(NULL, $P5("$cys", "$cyp", "$cyc", "$cyn", "$cyt"), $$->*
31 $E($ CYAssign($V("$cyp"), $C1($V("object_getClass"), cys)))->*
32 $E($ CYAssign(cyc, cys))->*
33 $E($ CYAssign($V("$cym"), $C1($V("object_getClass"), cyc)))->*
34 messages_->Replace(context, true)
35 ), name_->ClassName(context, true)));
36 }
37
38 CYExpression *CYClass::Replace_(CYContext &context) {
39 CYVariable *cyc($V("$cyc")), *cys($V("$cys"));
40
41 CYExpression *name(name_ != NULL ? name_->ClassName(context, false) : $C1($V("$cyq"), $S("CY$")));
42
43 return $C1($F(NULL, $P6("$cys", "$cyp", "$cyc", "$cyn", "$cyt", "$cym"), $$->*
44 $E($ CYAssign($V("$cyp"), $C1($V("object_getClass"), cys)))->*
45 $E($ CYAssign(cyc, $C3($V("objc_allocateClassPair"), cys, name, $D(0))))->*
46 $E($ CYAssign($V("$cym"), $C1($V("object_getClass"), cyc)))->*
47 protocols_->Replace(context)->*
48 fields_->Replace(context)->*
49 messages_->Replace(context, false)->*
50 $E($C1($V("objc_registerClassPair"), cyc))->*
51 $ CYReturn(cyc)
52 ), super_ == NULL ? $ CYNull() : super_);
53 }
54
55 CYExpression *CYClassExpression::Replace(CYContext &context) {
56 return Replace_(context);
57 }
58
59 CYStatement *CYClassStatement::Replace(CYContext &context) {
60 return $E(Replace_(context));
61 }
62
63 CYStatement *CYField::Replace(CYContext &context) const {
64 return NULL;
65 }
66
67 CYStatement *CYImport::Replace(CYContext &context) {
68 return this;
69 }
70
71 CYStatement *CYMessage::Replace(CYContext &context, bool replace) const { $T(NULL)
72 CYVariable *cyn($V("$cyn"));
73 CYVariable *cyt($V("$cyt"));
74 CYVariable *self($V("self"));
75 CYVariable *_class($V(instance_ ? "$cys" : "$cyp"));
76
77 return $ CYBlock($$->*
78 next_->Replace(context, replace)->*
79 $E($ CYAssign(cyn, parameters_->Selector(context)))->*
80 $E($ CYAssign(cyt, $C1($M(cyn, $S("type")), _class)))->*
81 $E($C4($V(replace ? "class_replaceMethod" : "class_addMethod"),
82 $V(instance_ ? "$cyc" : "$cym"),
83 cyn,
84 $N2($V("Functor"), $F(NULL, $P2("self", "_cmd", parameters_->Parameters(context)), $$->*
85 $ CYVar($L1($L($I("$cyr"), $N2($V("Super"), self, _class))))->*
86 $ CYReturn($C1($M($F(NULL, NULL, code_), $S("call")), self))
87 ), cyt),
88 cyt
89 ))
90 );
91 }
92
93 CYFunctionParameter *CYMessageParameter::Parameters(CYContext &context) const { $T(NULL)
94 CYFunctionParameter *next(next_->Parameters(context));
95 return name_ == NULL ? next : $ CYFunctionParameter(name_, next);
96 }
97
98 CYSelector *CYMessageParameter::Selector(CYContext &context) const {
99 return $ CYSelector(SelectorPart(context));
100 }
101
102 CYSelectorPart *CYMessageParameter::SelectorPart(CYContext &context) const { $T(NULL)
103 CYSelectorPart *next(next_->SelectorPart(context));
104 return tag_ == NULL ? next : $ CYSelectorPart(tag_, name_ != NULL, next);
105 }
106
107 CYExpression *CYBox::Replace(CYContext &context) {
108 return $C1($M($V("Instance"), $S("box")), value_);
109 }
110
111 CYStatement *CYProtocol::Replace(CYContext &context) const { $T(NULL)
112 return $ CYBlock($$->*
113 next_->Replace(context)->*
114 $E($C2($V("class_addProtocol"),
115 $V("$cyc"), name_
116 ))
117 );
118 }
119
120 CYExpression *CYSelector::Replace(CYContext &context) {
121 return $N1($V("Selector"), name_->Replace(context));
122 }
123
124 CYString *CYSelectorPart::Replace(CYContext &context) {
125 std::ostringstream str;
126 CYForEach (part, this) {
127 if (part->name_ != NULL)
128 str << part->name_->Word();
129 if (part->value_)
130 str << ':';
131 }
132 return $S(apr_pstrdup($pool, str.str().c_str()));
133 }
134
135 CYExpression *CYSendDirect::Replace(CYContext &context) {
136 std::ostringstream name;
137 CYArgument **argument(&arguments_);
138 CYSelectorPart *selector(NULL), *current(NULL);
139
140 while (*argument != NULL) {
141 if ((*argument)->name_ != NULL) {
142 CYSelectorPart *part($ CYSelectorPart((*argument)->name_, (*argument)->value_ != NULL));
143 if (selector == NULL)
144 selector = part;
145 if (current != NULL)
146 current->SetNext(part);
147 current = part;
148 (*argument)->name_ = NULL;
149 }
150
151 if ((*argument)->value_ == NULL)
152 *argument = (*argument)->next_;
153 else
154 argument = &(*argument)->next_;
155 }
156
157 return $C2($V("objc_msgSend"), self_, ($ CYSelector(selector))->Replace(context), arguments_);
158 }
159
160 CYExpression *CYSendSuper::Replace(CYContext &context) {
161 return $ CYSendDirect($V("$cyr"), arguments_);
162 }