X-Git-Url: https://git.saurik.com/cycript.git/blobdiff_plain/57d557146c1aa8f72348d62ce461080ba5b2b5d2..1e8d80477a3e058a30c477955f1e0c56deb6e956:/ObjectiveC/Replace.cpp diff --git a/ObjectiveC/Replace.cpp b/ObjectiveC/Replace.cpp index d90eecf..01eb727 100644 --- a/ObjectiveC/Replace.cpp +++ b/ObjectiveC/Replace.cpp @@ -1,5 +1,5 @@ -/* Cycript - Optimizing JavaScript Compiler/Runtime - * Copyright (C) 2009-2014 Jay Freeman (saurik) +/* Cycript - The Truly Universal Scripting Language + * Copyright (C) 2009-2016 Jay Freeman (saurik) */ /* GNU Affero General Public License, Version 3 {{{ */ @@ -19,20 +19,18 @@ **/ /* }}} */ -#include "Replace.hpp" -#include "ObjectiveC/Syntax.hpp" - #include -static CYExpression *MessageType(CYContext &context, CYTypedIdentifier *type, CYMessageParameter *next, CYExpression *extra = NULL) { - if (type == NULL) - return NULL; +#include "Replace.hpp" +#include "ObjectiveC/Syntax.hpp" + +static CYExpression *MessageType(CYContext &context, CYType *type, CYMessageParameter *next, CYExpression *extra = NULL) { CYExpression *left($C0($M(type->Replace(context), $S("toString")))); if (extra != NULL) left = $ CYAdd(left, extra); - if (next == NULL || next->name_ == NULL) + if (next == NULL || next->type_ == NULL) return left; CYExpression *right(next->TypeSignature(context)); @@ -45,54 +43,44 @@ static CYExpression *MessageType(CYContext &context, CYTypedIdentifier *type, CY CYStatement *CYCategory::Replace(CYContext &context) { CYVariable *cyc($V("$cyc")), *cys($V("$cys")); - return $E($C1($F(NULL, $P6($L("$cys"), $L("$cyp"), $L("$cyc"), $L("$cyn"), $L("$cyt"), $L("$cym")), $$->* + return $E($C1($F(NULL, $P6($B($I("$cys")), $B($I("$cyp")), $B($I("$cyc")), $B($I("$cyn")), $B($I("$cyt")), $B($I("$cym"))), $$->* $E($ CYAssign($V("$cyp"), $C1($V("object_getClass"), cys)))->* $E($ CYAssign(cyc, cys))->* $E($ CYAssign($V("$cym"), $C1($V("object_getClass"), cyc)))->* messages_->Replace(context, true) - ), name_->ClassName(context, true))); + ), $C1($V("objc_getClass"), $S(name_)))); } -CYExpression *CYClass::Replace_(CYContext &context) { +CYStatement *CYImplementation::Replace(CYContext &context) { CYVariable *cyc($V("$cyc")), *cys($V("$cys")); - CYExpression *name(name_ != NULL ? name_->ClassName(context, false) : $C1($V("$cyq"), $S("CY$"))); - - return $C1($F(NULL, $P6($L("$cys"), $L("$cyp"), $L("$cyc"), $L("$cyn"), $L("$cyt"), $L("$cym")), $$->* + return $E($C1($F(NULL, $P6($B($I("$cys")), $B($I("$cyp")), $B($I("$cyc")), $B($I("$cyn")), $B($I("$cyt")), $B($I("$cym"))), $$->* $E($ CYAssign($V("$cyp"), $C1($V("object_getClass"), cys)))->* - $E($ CYAssign(cyc, $C3($V("objc_allocateClassPair"), cys, name, $D(0))))->* + $E($ CYAssign(cyc, $C3($V("objc_allocateClassPair"), cys, $S(name_), $D(0))))->* $E($ CYAssign($V("$cym"), $C1($V("object_getClass"), cyc)))->* protocols_->Replace(context)->* fields_->Replace(context)->* messages_->Replace(context, false)->* $E($C1($V("objc_registerClassPair"), cyc))->* $ CYReturn(cyc) - ), super_ == NULL ? $ CYNull() : super_); + ), extends_ == NULL ? $ CYNull() : extends_)); } -CYExpression *CYClassExpression::Replace(CYContext &context) { - return Replace_(context); -} - -CYStatement *CYClassStatement::Replace(CYContext &context) { - return $E(Replace_(context)); -} - -CYStatement *CYField::Replace(CYContext &context) const { $T(NULL) +CYStatement *CYImplementationField::Replace(CYContext &context) const { $T(NULL) CYVariable *cyn($V("$cyn")); CYVariable *cyt($V("$cyt")); - CYExpression *type($C0($M(typed_->Replace(context), $S("toString")))); + CYExpression *type($C0($M(type_->Replace(context), $S("toString")))); return $ CYBlock($$->* $E($ CYAssign(cyt, type))->* $E($ CYAssign(cyn, $N1($V("Type"), cyt)))->* - $E($C5($V("class_addIvar"), $V("$cyc"), $S(typed_->identifier_->Word()), $M(cyn, $S("size")), $M(cyn, $S("alignment")), cyt))->* + $E($C5($V("class_addIvar"), $V("$cyc"), name_->PropertyName(context), $M(cyn, $S("size")), $M(cyn, $S("alignment")), cyt))->* next_->Replace(context) ); } -CYExpression *CYInstanceLiteral::Replace(CYContext &context) { +CYTarget *CYInstanceLiteral::Replace(CYContext &context) { return $N1($V("Instance"), number_); } @@ -102,18 +90,16 @@ CYStatement *CYMessage::Replace(CYContext &context, bool replace) const { $T(NUL CYVariable *self($V("self")); CYVariable *_class($V(instance_ ? "$cys" : "$cyp")); - CYExpression *type(TypeSignature(context) ?: $C1($M(cyn, $S("type")), _class)); - return $ CYBlock($$->* next_->Replace(context, replace)->* $E($ CYAssign(cyn, parameters_->Selector(context)))->* - $E($ CYAssign(cyt, type))->* + $E($ CYAssign(cyt, TypeSignature(context)))->* $E($C4($V(replace ? "class_replaceMethod" : "class_addMethod"), $V(instance_ ? "$cyc" : "$cym"), cyn, - $N2($V("Functor"), $F(NULL, $P2($L("self"), $L("_cmd"), parameters_->Parameters(context)), $$->* - $ CYVar($L1($L("$cyr", $N2($V("objc_super"), self, _class))))->* - $ CYReturn($C1($M($F(NULL, NULL, code_), $S("call")), self)) + $N2($V("Functor"), $F(NULL, $P2($B($I("self")), $B($I("_cmd")), parameters_->Parameters(context)), $$->* + $ CYVar($B1($B($I("$cyr"), $N2($V("objc_super"), self, _class))))->* + $ CYReturn($C1($M($F(NULL, NULL, code_.code_), $S("call")), self)) ), cyt), cyt )) @@ -126,7 +112,7 @@ CYExpression *CYMessage::TypeSignature(CYContext &context) const { CYFunctionParameter *CYMessageParameter::Parameters(CYContext &context) const { $T(NULL) CYFunctionParameter *next(next_->Parameters(context)); - return name_ == NULL ? next : $ CYFunctionParameter($ CYDeclaration(name_), next); + return type_ == NULL ? next : $ CYFunctionParameter($B(identifier_), next); } CYSelector *CYMessageParameter::Selector(CYContext &context) const { @@ -135,19 +121,41 @@ CYSelector *CYMessageParameter::Selector(CYContext &context) const { CYSelectorPart *CYMessageParameter::SelectorPart(CYContext &context) const { $T(NULL) CYSelectorPart *next(next_->SelectorPart(context)); - return tag_ == NULL ? next : $ CYSelectorPart(tag_, name_ != NULL, next); + return name_ == NULL ? next : $ CYSelectorPart(name_, type_ != NULL, next); } CYExpression *CYMessageParameter::TypeSignature(CYContext &context) const { return MessageType(context, type_, next_); } -CYExpression *CYBox::Replace(CYContext &context) { +CYTarget *CYBox::Replace(CYContext &context) { return $C1($M($V("Instance"), $S("box")), value_); } -CYExpression *CYObjCBlock::Replace(CYContext &context) { - return $C1($ CYEncodedType(($ CYTypedIdentifier(*typed_))->Modify($ CYTypeBlockWith(parameters_))), $ CYFunctionExpression(NULL, parameters_->Parameters(context), statements_)); +CYTarget *CYObjCArray::Replace(CYContext &context) { + size_t count(0); + CYForEach (element, elements_) + ++count; + return $ CYSendDirect($V("NSArray"), $C_($ CYWord("arrayWithObjects"), $ CYArray(elements_), $C_($ CYWord("count"), $D(count)))); +} + +CYTarget *CYObjCDictionary::Replace(CYContext &context) { + CYList keys; + CYList values; + size_t count(0); + + CYForEach (pair, pairs_) { + keys->*$ CYElementValue(pair->key_); + values->*$ CYElementValue(pair->value_); + ++count; + } + + return $ CYSendDirect($V("NSDictionary"), $C_($ CYWord("dictionaryWithObjects"), $ CYArray(values), $C_($ CYWord("forKeys"), $ CYArray(keys), $C_($ CYWord("count"), $D(count))))); +} + +CYTarget *CYObjCBlock::Replace(CYContext &context) { + // XXX: wtf is happening here? + return $C1($ CYTypeExpression(($ CYType(*typed_))->Modify($ CYTypeBlockWith(parameters_))), $ CYFunctionExpression(NULL, parameters_->Parameters(context), code_)); } CYStatement *CYProtocol::Replace(CYContext &context) const { $T(NULL) @@ -159,8 +167,8 @@ CYStatement *CYProtocol::Replace(CYContext &context) const { $T(NULL) ); } -CYExpression *CYSelector::Replace(CYContext &context) { - return $C1($V("sel_registerName"), name_->Replace(context)); +CYTarget *CYSelector::Replace(CYContext &context) { + return $C1($V("sel_registerName"), parts_->Replace(context)); } CYString *CYSelectorPart::Replace(CYContext &context) { @@ -174,7 +182,7 @@ CYString *CYSelectorPart::Replace(CYContext &context) { return $S($pool.strdup(str.str().c_str())); } -CYExpression *CYSendDirect::Replace(CYContext &context) { +CYTarget *CYSendDirect::Replace(CYContext &context) { std::ostringstream name; CYArgument **argument(&arguments_); CYSelectorPart *selector(NULL), *current(NULL); @@ -199,6 +207,6 @@ CYExpression *CYSendDirect::Replace(CYContext &context) { return $C2($V("objc_msgSend"), self_, selector->Replace(context), arguments_); } -CYExpression *CYSendSuper::Replace(CYContext &context) { +CYTarget *CYSendSuper::Replace(CYContext &context) { return $ CYSendDirect($V("$cyr"), arguments_); }