]> git.saurik.com Git - cycript.git/blame - Replace.cpp
Fixed some CYInitialize issues noticed back on the iPhone.
[cycript.git] / Replace.cpp
CommitLineData
e91fbe93 1/* Cycript - Inlining/Optimizing JavaScript Compiler
4644480a
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
3b52fd1a
JF
40#include "Parser.hpp"
41
3b52fd1a
JF
42#include <iomanip>
43
4de0686f 44#include "Replace.hpp"
3b52fd1a 45
4644480a
JF
46CYExpression *CYAdd::Replace(CYContext &context) {
47 CYInfix::Replace(context);
48
49 CYExpression *lhp(lhs_->Primitive(context));
50 CYExpression *rhp(rhs_->Primitive(context));
51
52 CYString *lhs(dynamic_cast<CYString *>(lhp));
53 CYString *rhs(dynamic_cast<CYString *>(rhp));
54
55 if (lhs != NULL || rhs != NULL) {
56 if (lhs == NULL) {
57 lhs = lhp->String(context);
58 if (lhs == NULL)
59 return NULL;
60 } else if (rhs == NULL) {
61 rhs = rhp->String(context);
62 if (rhs == NULL)
63 return NULL;
64 }
65
66 return lhs->Concat(context, rhs);
67 }
68
69 if (CYNumber *lhn = lhp->Number(context))
70 if (CYNumber *rhn = rhp->Number(context))
71 return $D(lhn->Value() + rhn->Value());
72
73 return NULL;
74}
75
3b52fd1a
JF
76CYExpression *CYAddressOf::Replace(CYContext &context) {
77 CYPrefix::Replace(context);
78 return $C0($M(rhs_, $S("$cya")));
79}
80
81void CYArgument::Replace(CYContext &context) { $T()
82 context.Replace(value_);
83 next_->Replace(context);
84}
85
86CYExpression *CYArray::Replace(CYContext &context) {
87 elements_->Replace(context);
88 return NULL;
89}
90
91CYExpression *CYArrayComprehension::Replace(CYContext &context) {
92 CYVariable *cyv($V("$cyv"));
93
94 return $C0($F(NULL, $P1("$cyv", comprehensions_->Parameters(context)), $$->*
95 $E($ CYAssign(cyv, $ CYArray()))->*
96 comprehensions_->Replace(context, $E($C1($M(cyv, $S("push")), expression_)))->*
97 $ CYReturn(cyv)
98 ));
99}
100
101CYExpression *CYAssignment::Replace(CYContext &context) {
102 context.Replace(lhs_);
103 context.Replace(rhs_);
104 return NULL;
105}
106
107CYStatement *CYBlock::Replace(CYContext &context) {
108 statements_ = statements_->ReplaceAll(context);
109 return NULL;
110}
111
112CYStatement *CYBreak::Replace(CYContext &context) {
113 return NULL;
114}
115
116CYExpression *CYCall::Replace(CYContext &context) {
117 context.Replace(function_);
118 arguments_->Replace(context);
119 return NULL;
120}
121
37954781
JF
122namespace cy {
123namespace Syntax {
124
125void Catch::Replace(CYContext &context) { $T()
3b52fd1a
JF
126 code_.Replace(context);
127}
128
37954781
JF
129} }
130
3b52fd1a
JF
131void CYClause::Replace(CYContext &context) { $T()
132 context.Replace(case_);
133 statements_ = statements_->ReplaceAll(context);
134 next_->Replace(context);
135}
136
320ce753
JF
137CYStatement *CYComment::Replace(CYContext &context) {
138 return NULL;
139}
140
3b52fd1a
JF
141CYExpression *CYCompound::Replace(CYContext &context) {
142 expressions_ = expressions_->ReplaceAll(context);
143 return NULL;
144}
145
146CYFunctionParameter *CYComprehension::Parameters(CYContext &context) const { $T(NULL)
147 CYFunctionParameter *next(next_->Parameters(context));
148 if (CYFunctionParameter *parameter = Parameter(context)) {
149 parameter->SetNext(next);
150 return parameter;
151 } else
152 return next;
153}
154
155CYStatement *CYComprehension::Replace(CYContext &context, CYStatement *statement) const {
156 return next_ == NULL ? statement : next_->Replace(context, statement);
157}
158
159CYExpression *CYCondition::Replace(CYContext &context) {
160 context.Replace(test_);
161 context.Replace(true_);
162 context.Replace(false_);
163 return NULL;
164}
165
166CYStatement *CYContinue::Replace(CYContext &context) {
167 return NULL;
168}
169
170CYExpression *CYDeclaration::ForEachIn(CYContext &context) {
171 return $ CYVariable(identifier_);
172}
173
174void CYDeclaration::Replace(CYContext &context) {
175 context.Replace(initialiser_);
176}
177
550ee46a
JF
178CYProperty *CYDeclarations::Property(CYContext &context) { $T(NULL)
179 return $ CYProperty(declaration_->identifier_, declaration_->initialiser_ ?: $U, next_->Property(context));
180}
181
3b52fd1a
JF
182void CYDeclarations::Replace(CYContext &context) { $T()
183 declaration_->Replace(context);
184 next_->Replace(context);
185}
186
187CYExpression *CYDirectMember::Replace(CYContext &context) {
188 Replace_(context);
189 return NULL;
190}
191
192CYStatement *CYDoWhile::Replace(CYContext &context) {
193 context.Replace(test_);
194 context.Replace(code_);
195 return NULL;
196}
197
198void CYElement::Replace(CYContext &context) { $T()
199 context.Replace(value_);
200 next_->Replace(context);
201}
202
203CYStatement *CYEmpty::Replace(CYContext &context) {
204 return NULL;
205}
206
207CYStatement *CYExpress::Replace(CYContext &context) {
208 context.Replace(expression_);
209 return NULL;
210}
211
212CYExpression *CYExpression::ClassName(CYContext &context, bool object) {
213 return this;
214}
215
216CYExpression *CYExpression::ForEachIn(CYContext &context) {
217 return this;
218}
219
220CYExpression *CYExpression::ReplaceAll(CYContext &context) { $T(NULL)
221 CYExpression *replace(this);
222 context.Replace(replace);
223
224 if (CYExpression *next = next_->ReplaceAll(context))
225 replace->SetNext(next);
226 else
227 replace->SetNext(next_);
228
229 return replace;
230}
231
4644480a
JF
232CYNumber *CYFalse::Number(CYContext &context) {
233 return $D(0);
234}
235
236CYString *CYFalse::String(CYContext &context) {
237 return $S("false");
238}
239
3b52fd1a
JF
240void CYFinally::Replace(CYContext &context) { $T()
241 code_.Replace(context);
242}
243
244CYStatement *CYFor::Replace(CYContext &context) {
245 // XXX: initialiser_
246 context.Replace(test_);
247 context.Replace(increment_);
248 context.Replace(code_);
249 return NULL;
250}
251
252CYStatement *CYForIn::Replace(CYContext &context) {
253 // XXX: initialiser_
254 context.Replace(set_);
255 context.Replace(code_);
256 return NULL;
257}
258
259CYFunctionParameter *CYForInComprehension::Parameter(CYContext &context) const {
260 return $ CYFunctionParameter(name_);
261}
262
263CYStatement *CYForInComprehension::Replace(CYContext &context, CYStatement *statement) const {
264 return $ CYForIn($ CYVariable(name_), set_, CYComprehension::Replace(context, statement));
265}
266
267CYStatement *CYForEachIn::Replace(CYContext &context) {
268 CYVariable *cys($V("$cys")), *cyt($V("$cyt"));
269
550ee46a 270 return $ CYLet($L2($L($I("$cys"), set_), $L($I("$cyt"))), $$->*
3b52fd1a
JF
271 $ CYForIn(cyt, cys, $ CYBlock($$->*
272 $E($ CYAssign(initialiser_->ForEachIn(context), $M(cys, cyt)))->*
273 code_
274 ))
550ee46a 275 );
3b52fd1a
JF
276}
277
278CYFunctionParameter *CYForEachInComprehension::Parameter(CYContext &context) const {
279 return $ CYFunctionParameter(name_);
280}
281
282CYStatement *CYForEachInComprehension::Replace(CYContext &context, CYStatement *statement) const {
283 CYVariable *cys($V("$cys")), *name($ CYVariable(name_));
284
285 return $E($C0($F(NULL, $P1("$cys"), $$->*
286 $E($ CYAssign(cys, set_))->*
287 $ CYForIn(name, cys, $ CYBlock($$->*
288 $E($ CYAssign(name, $M(cys, name)))->*
289 CYComprehension::Replace(context, statement)
290 ))
291 )));
292}
293
294void CYFunction::Replace_(CYContext &context) {
295 code_.Replace(context);
296}
297
298CYExpression *CYFunctionExpression::Replace(CYContext &context) {
299 Replace_(context);
300 return NULL;
301}
302
303CYStatement *CYFunctionStatement::Replace(CYContext &context) {
304 Replace_(context);
305 return NULL;
306}
307
308CYStatement *CYIf::Replace(CYContext &context) {
309 context.Replace(test_);
310 context.Replace(true_);
311 context.Replace(false_);
312 return NULL;
313}
314
315CYFunctionParameter *CYIfComprehension::Parameter(CYContext &context) const {
316 return NULL;
317}
318
319CYStatement *CYIfComprehension::Replace(CYContext &context, CYStatement *statement) const {
320 return $ CYIf(test_, CYComprehension::Replace(context, statement));
321}
322
323CYExpression *CYIndirect::Replace(CYContext &context) {
324 CYPrefix::Replace(context);
325 return $M(rhs_, $S("$cyi"));
326}
327
328CYExpression *CYIndirectMember::Replace(CYContext &context) {
329 Replace_(context);
330 return $M($ CYIndirect(object_), property_);
331}
332
333CYExpression *CYInfix::Replace(CYContext &context) {
334 context.Replace(lhs_);
335 context.Replace(rhs_);
336 return NULL;
337}
338
339CYStatement *CYLabel::Replace(CYContext &context) {
340 context.Replace(statement_);
341 return NULL;
342}
343
550ee46a
JF
344CYStatement *CYLet::Replace(CYContext &context) {
345 return $ CYWith($ CYObject(declarations_->Property(context)), &code_);
346}
347
3b52fd1a
JF
348void CYMember::Replace_(CYContext &context) {
349 context.Replace(object_);
350 context.Replace(property_);
351}
352
3b52fd1a
JF
353CYExpression *CYNew::Replace(CYContext &context) {
354 context.Replace(constructor_);
355 arguments_->Replace(context);
356 return NULL;
357}
358
4644480a
JF
359CYNumber *CYNull::Number(CYContext &context) {
360 return $D(0);
361}
362
363CYString *CYNull::String(CYContext &context) {
364 return $S("null");
365}
366
367CYNumber *CYNumber::Number(CYContext &context) {
368 return this;
369}
370
371CYString *CYNumber::String(CYContext &context) {
372 // XXX: there is a precise algorithm for this
373 return $S(apr_psprintf(context.pool_, "%.17g", Value()));
374}
375
3b52fd1a
JF
376CYExpression *CYObject::Replace(CYContext &context) {
377 properties_->Replace(context);
378 return NULL;
379}
380
381CYExpression *CYPostfix::Replace(CYContext &context) {
382 context.Replace(lhs_);
383 return NULL;
384}
385
386CYExpression *CYPrefix::Replace(CYContext &context) {
387 context.Replace(rhs_);
388 return NULL;
389}
390
391void CYProgram::Replace(CYContext &context) {
392 statements_ = statements_->ReplaceAll(context);
393}
394
395void CYProperty::Replace(CYContext &context) { $T()
396 context.Replace(value_);
397 next_->Replace(context);
398}
399
400CYStatement *CYReturn::Replace(CYContext &context) {
401 context.Replace(value_);
402 return NULL;
403}
404
3b52fd1a
JF
405CYStatement *CYStatement::ReplaceAll(CYContext &context) { $T(NULL)
406 CYStatement *replace(this);
407 context.Replace(replace);
408
409 if (CYStatement *next = next_->ReplaceAll(context))
410 replace->SetNext(next);
411 else
412 replace->SetNext(next_);
413
414 return replace;
415}
416
4644480a
JF
417CYString *CYString::Concat(CYContext &context, CYString *rhs) const {
418 size_t size(size_ + rhs->size_);
419 char *value(new(context.pool_) char[size + 1]);
420 memcpy(value, value_, size_);
421 memcpy(value + size_, rhs->value_, rhs->size_);
422 value[size] = '\0';
a14eb702 423 return $S(value, size);
4644480a
JF
424}
425
426CYNumber *CYString::Number(CYContext &context) {
427 // XXX: there is a precise algorithm for this
428 return NULL;
429}
430
431CYString *CYString::String(CYContext &context) {
432 return this;
433}
434
3b52fd1a
JF
435CYStatement *CYSwitch::Replace(CYContext &context) {
436 context.Replace(value_);
437 clauses_->Replace(context);
438 return NULL;
439}
440
441CYExpression *CYThis::Replace(CYContext &context) {
442 return NULL;
443}
444
37954781
JF
445namespace cy {
446namespace Syntax {
447
448CYStatement *Throw::Replace(CYContext &context) {
3b52fd1a
JF
449 context.Replace(value_);
450 return NULL;
451}
452
37954781
JF
453} }
454
3b52fd1a
JF
455CYExpression *CYTrivial::Replace(CYContext &context) {
456 return NULL;
457}
458
4644480a
JF
459CYNumber *CYTrue::Number(CYContext &context) {
460 return $D(1);
461}
462
463CYString *CYTrue::String(CYContext &context) {
464 return $S("true");
465}
466
37954781
JF
467namespace cy {
468namespace Syntax {
469
470CYStatement *Try::Replace(CYContext &context) {
3b52fd1a
JF
471 code_.Replace(context);
472 catch_->Replace(context);
473 finally_->Replace(context);
474 return NULL;
475}
476
37954781
JF
477} }
478
3b52fd1a
JF
479CYStatement *CYVar::Replace(CYContext &context) {
480 declarations_->Replace(context);
481 return NULL;
482}
483
484CYExpression *CYVariable::Replace(CYContext &context) {
485 return NULL;
486}
487
488CYStatement *CYWhile::Replace(CYContext &context) {
489 context.Replace(test_);
490 context.Replace(code_);
491 return NULL;
492}
493
494CYStatement *CYWith::Replace(CYContext &context) {
495 context.Replace(scope_);
496 context.Replace(code_);
497 return NULL;
498}
499
500CYExpression *CYWord::ClassName(CYContext &context, bool object) {
501 CYString *name($S(this));
502 if (object)
503 return $C1($V("objc_getClass"), name);
504 else
505 return name;
506}