]>
Commit | Line | Data |
---|---|---|
b3378a02 JF |
1 | /* Cycript - Optimizing JavaScript Compiler/Runtime |
2 | * Copyright (C) 2009-2010 Jay Freeman (saurik) | |
b53b30c1 JF |
3 | */ |
4 | ||
b3378a02 | 5 | /* GNU Lesser General Public License, Version 3 {{{ */ |
b53b30c1 | 6 | /* |
b3378a02 JF |
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. | |
b53b30c1 | 11 | * |
b3378a02 JF |
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. | |
b53b30c1 | 16 | * |
b3378a02 JF |
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 | **/ | |
b53b30c1 JF |
20 | /* }}} */ |
21 | ||
4de0686f | 22 | #include "Replace.hpp" |
3c1c3635 | 23 | #include "ObjectiveC/Syntax.hpp" |
4de0686f | 24 | |
4de0686f JF |
25 | #include <sstream> |
26 | ||
4de0686f JF |
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))->* | |
725075ae | 33 | $E($ CYAssign($V("$cym"), $C1($V("object_getClass"), cyc)))->* |
4de0686f JF |
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)))->* | |
64b8d29f | 47 | protocols_->Replace(context)->* |
4de0686f JF |
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 | ||
1ba6903e JF |
67 | CYStatement *CYImport::Replace(CYContext &context) { |
68 | return this; | |
69 | } | |
70 | ||
4de0686f JF |
71 | CYStatement *CYMessage::Replace(CYContext &context, bool replace) const { $T(NULL) |
72 | CYVariable *cyn($V("$cyn")); | |
73 | CYVariable *cyt($V("$cyt")); | |
cacd1a88 JF |
74 | CYVariable *self($V("self")); |
75 | CYVariable *_class($V(instance_ ? "$cys" : "$cyp")); | |
4de0686f JF |
76 | |
77 | return $ CYBlock($$->* | |
78 | next_->Replace(context, replace)->* | |
79 | $E($ CYAssign(cyn, parameters_->Selector(context)))->* | |
cacd1a88 | 80 | $E($ CYAssign(cyt, $C1($M(cyn, $S("type")), _class)))->* |
4de0686f JF |
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)), $$->* | |
39f3897f | 85 | $ CYVar($L1($L($I("$cyr"), $N2($V("Super"), self, _class))))->* |
cacd1a88 | 86 | $ CYReturn($C1($M($F(NULL, NULL, code_), $S("call")), self)) |
4de0686f JF |
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 | ||
64b8d29f JF |
107 | CYStatement *CYProtocol::Replace(CYContext &context) const { $T(NULL) |
108 | return $ CYBlock($$->* | |
109 | next_->Replace(context)->* | |
110 | $E($C2($V("class_addProtocol"), | |
111 | $V("$cyc"), name_ | |
112 | )) | |
113 | ); | |
114 | } | |
115 | ||
4de0686f JF |
116 | CYExpression *CYSelector::Replace(CYContext &context) { |
117 | return $N1($V("Selector"), name_->Replace(context)); | |
118 | } | |
119 | ||
120 | CYString *CYSelectorPart::Replace(CYContext &context) { | |
121 | std::ostringstream str; | |
122 | for (const CYSelectorPart *part(this); part != NULL; part = part->next_) { | |
123 | if (part->name_ != NULL) | |
69688ca1 | 124 | str << part->name_->Word(); |
4de0686f JF |
125 | if (part->value_) |
126 | str << ':'; | |
127 | } | |
2eb8215d | 128 | return $S(apr_pstrdup($pool, str.str().c_str())); |
4de0686f JF |
129 | } |
130 | ||
cacd1a88 | 131 | CYExpression *CYSendDirect::Replace(CYContext &context) { |
4de0686f JF |
132 | std::ostringstream name; |
133 | CYArgument **argument(&arguments_); | |
2385c806 | 134 | CYSelectorPart *selector(NULL), *current(NULL); |
4de0686f JF |
135 | |
136 | while (*argument != NULL) { | |
137 | if ((*argument)->name_ != NULL) { | |
2385c806 JF |
138 | CYSelectorPart *part($ CYSelectorPart((*argument)->name_, (*argument)->value_ != NULL)); |
139 | if (selector == NULL) | |
140 | selector = part; | |
141 | if (current != NULL) | |
142 | current->SetNext(part); | |
143 | current = part; | |
4de0686f | 144 | (*argument)->name_ = NULL; |
4de0686f JF |
145 | } |
146 | ||
147 | if ((*argument)->value_ == NULL) | |
148 | *argument = (*argument)->next_; | |
149 | else | |
150 | argument = &(*argument)->next_; | |
151 | } | |
152 | ||
2385c806 | 153 | return $C2($V("objc_msgSend"), self_, ($ CYSelector(selector))->Replace(context), arguments_); |
4de0686f | 154 | } |
cacd1a88 JF |
155 | |
156 | CYExpression *CYSendSuper::Replace(CYContext &context) { | |
2eb8215d | 157 | return $ CYSendDirect($V("cyr"), arguments_); |
cacd1a88 | 158 | } |