]>
git.saurik.com Git - cycript.git/blob - ObjectiveC/Replace.cpp
9d2d1ab72e6bcdf420cc053526bc4eb166f00e67
   1 /* Cycript - Optimizing JavaScript Compiler/Runtime 
   2  * Copyright (C) 2009-2010  Jay Freeman (saurik) 
   5 /* GNU Lesser General Public License, Version 3 {{{ */ 
   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. 
  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. 
  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/>. 
  22 #include "Replace.hpp" 
  23 #include "ObjectiveC/Syntax.hpp" 
  27 CYStatement 
*CYCategory::Replace(CYContext 
&context
) { 
  28     CYVariable 
*cyc($
V("$cyc")), *cys($
V("$cys")); 
  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))); 
  38 CYExpression 
*CYClass::Replace_(CYContext 
&context
) { 
  39     CYVariable 
*cyc($
V("$cyc")), *cys($
V("$cys")); 
  41     CYExpression 
*name(name_ 
!= NULL 
? name_
->ClassName(context
, false) : $
C1($
V("$cyq"), $
S("CY$"))); 
  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
))->* 
  52     ), super_ 
== NULL 
? $ 
CYNull() : super_
); 
  55 CYExpression 
*CYClassExpression::Replace(CYContext 
&context
) { 
  56     return Replace_(context
); 
  59 CYStatement 
*CYClassStatement::Replace(CYContext 
&context
) { 
  60     return $
E(Replace_(context
)); 
  63 CYStatement 
*CYField::Replace(CYContext 
&context
) const { 
  67 CYStatement 
*CYImport::Replace(CYContext 
&context
) { 
  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")); 
  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"), 
  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
)) 
  93 CYFunctionParameter 
*CYMessageParameter::Parameters(CYContext 
&context
) const { $
T(NULL
) 
  94     CYFunctionParameter 
*next(next_
->Parameters(context
)); 
  95     return name_ 
== NULL 
? next 
: $ 
CYFunctionParameter(name_
, next
); 
  98 CYSelector 
*CYMessageParameter::Selector(CYContext 
&context
) const { 
  99     return $ 
CYSelector(SelectorPart(context
)); 
 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
); 
 107 CYStatement 
*CYProtocol::Replace(CYContext 
&context
) const { $
T(NULL
) 
 108     return $ 
CYBlock($$
->* 
 109         next_
->Replace(context
)->* 
 110         $
E($
C2($
V("class_addProtocol"), 
 116 CYExpression 
*CYSelector::Replace(CYContext 
&context
) { 
 117     return $
N1($
V("Selector"), name_
->Replace(context
)); 
 120 CYString 
*CYSelectorPart::Replace(CYContext 
&context
) { 
 121     std::ostringstream str
; 
 122     CYForEach (part
, this) { 
 123         if (part
->name_ 
!= NULL
) 
 124             str 
<< part
->name_
->Word(); 
 128     return $
S(apr_pstrdup($pool
, str
.str().c_str())); 
 131 CYExpression 
*CYSendDirect::Replace(CYContext 
&context
) { 
 132     std::ostringstream name
; 
 133     CYArgument 
**argument(&arguments_
); 
 134     CYSelectorPart 
*selector(NULL
), *current(NULL
); 
 136     while (*argument 
!= NULL
) { 
 137         if ((*argument
)->name_ 
!= NULL
) { 
 138             CYSelectorPart 
*part($ 
CYSelectorPart((*argument
)->name_
, (*argument
)->value_ 
!= NULL
)); 
 139             if (selector 
== NULL
) 
 142                 current
->SetNext(part
); 
 144             (*argument
)->name_ 
= NULL
; 
 147         if ((*argument
)->value_ 
== NULL
) 
 148             *argument 
= (*argument
)->next_
; 
 150             argument 
= &(*argument
)->next_
; 
 153     return $
C2($
V("objc_msgSend"), self_
, ($ 
CYSelector(selector
))->Replace(context
), arguments_
); 
 156 CYExpression 
*CYSendSuper::Replace(CYContext 
&context
) { 
 157     return $ 
CYSendDirect($
V("cyr"), arguments_
);