Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(compiler): Handle errors from result handling [fixes LNG-247] #913

Merged
merged 1 commit into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions compiler/src/test/scala/aqua/compiler/AquaCompilerSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,10 @@ class AquaCompilerSpec extends AnyFlatSpec with Matchers {
val retVar = VarModel("ret", ScalarType.string)

val expected =
SeqRes.wrap(
getDataSrv("-relay-", "-relay-", ScalarType.string),
getDataSrv("peers", peers.name, peers.`type`),
XorRes.wrap(
XorRes.wrap(
SeqRes.wrap(
getDataSrv("-relay-", "-relay-", ScalarType.string),
getDataSrv("peers", peers.name, peers.`type`),
RestrictionRes(results.name, resultsType).wrap(
SeqRes.wrap(
ParRes.wrap(
Expand Down Expand Up @@ -203,9 +203,9 @@ class AquaCompilerSpec extends AnyFlatSpec with Matchers {
).leaf
)
),
errorCall(transformCfg, 0, initPeer)
respCall(transformCfg, flatResult, initPeer)
),
respCall(transformCfg, flatResult, initPeer)
errorCall(transformCfg, 0, initPeer)
)

exec.body.equalsOrShowDiff(expected) shouldBe (true)
Expand Down Expand Up @@ -276,8 +276,8 @@ class AquaCompilerSpec extends AnyFlatSpec with Matchers {
val resCanonVM = VarModel("-res-fix-0", CanonStreamType(ScalarType.string))
val resFlatVM = VarModel("-res-flat-0", ArrayType(ScalarType.string))

val expected = SeqRes.wrap(
XorRes.wrap(
val expected = XorRes.wrap(
SeqRes.wrap(
RestrictionRes(resVM.name, resStreamType).wrap(
SeqRes.wrap(
// res <- foo()
Expand All @@ -303,9 +303,9 @@ class AquaCompilerSpec extends AnyFlatSpec with Matchers {
).leaf
)
),
errorCall(transformCfg, 0, initPeer)
respCall(transformCfg, resFlatVM, initPeer)
),
respCall(transformCfg, resFlatVM, initPeer)
errorCall(transformCfg, 0, initPeer)
)

barfoo.body.equalsOrShowDiff(expected) should be(true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,22 @@ import aqua.types.*
import cats.syntax.show.*
import cats.syntax.option.*

// TODO: doc
/**
* Pre-transformer for top functions:
* - Get arguments
* - Get relay
* - Generate callbacks for function arguments
* - Handle result
* - Handle error
*
* @param argsProvider - provides arguments
* @param resultsHandler - handles results
* @param errorHandler - handles errors
* @param callback - generates callback for function argument
* @param relayVarName - name of the relay variable
* @param wrapCallableName - name of the generated wrapper function
* @param arrowCallbackPrefix - prefix for generated callbacks names
*/
case class FuncPreTransformer(
argsProvider: ArgsProvider,
resultsHandler: ResultsHandler,
Expand Down Expand Up @@ -50,7 +65,7 @@ case class FuncPreTransformer(
* removes function return
*
* @param func Function to transform
* @return
* @return Transformed function
*/
def preTransform(func: FuncArrow): FuncArrow = {
val returnType = ProductType(func.ret.map(_.`type`).map {
Expand Down Expand Up @@ -93,13 +108,13 @@ case class FuncPreTransformer(

val call = CallArrowRawTag.func(func.funcName, funcCall).leaf

val body = SeqTag.wrap(
provideArgs ++ List(
TryTag.wrap(
call,
handleError
)
) ++ handleResults
val body = TryTag.wrap(
SeqTag.wrap(
provideArgs
.appended(call)
.appendedAll(handleResults)
),
handleError
)

FuncArrow(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,29 +48,28 @@ class TransformSpec extends AnyFlatSpec with Matchers {

val procFC = fc.value.body

val expectedFC =
val expectedFC = XorRes.wrap(
SeqRes.wrap(
dataCall(bc, "-relay-", initPeer),
XorRes.wrap(
XorRes.wrap(
SeqRes.wrap(
through(relayV),
through(otherRelay),
callRes(1, otherPeer),
through(otherRelay),
through(relayV)
),
SeqRes.wrap(
through(otherRelay),
through(relayV),
through(initPeer),
failErrorRes
)
SeqRes.wrap(
through(relayV),
through(otherRelay),
callRes(1, otherPeer),
through(otherRelay),
through(relayV)
),
errorCall(bc, 0, initPeer)
SeqRes.wrap(
through(otherRelay),
through(relayV),
through(initPeer),
failErrorRes
)
),
respCall(bc, ret, initPeer)
)
),
errorCall(bc, 0, initPeer)
)

procFC.equalsOrShowDiff(expectedFC) should be(true)

Expand All @@ -80,9 +79,21 @@ class TransformSpec extends AnyFlatSpec with Matchers {

val ret = LiteralRaw.quote("return this")

/**
* func ret() -> string:
* srv0.fn0()
* on "other-peer":
* srv1.fn1()
* <- "return this"
*/
val func: FuncArrow = FuncArrow(
"ret",
SeqTag.wrap(callOp(0).leaf, OnTag(otherPeer, Chain.empty).wrap(callOp(1).leaf)),
SeqTag.wrap(
callOp(0).leaf,
OnTag(otherPeer, Chain.empty).wrap(
callOp(1).leaf
)
),
stringArrow,
ret :: Nil,
Map.empty,
Expand All @@ -96,43 +107,40 @@ class TransformSpec extends AnyFlatSpec with Matchers {

val procFC = fc.value.body

val expectedFC =
val expectedFC = XorRes.wrap(
SeqRes.wrap(
dataCall(bc, "-relay-", initPeer),
callRes(0, initPeer),
XorRes.wrap(
SeqRes.wrap(
callRes(0, initPeer),
XorRes.wrap(
SeqRes.wrap(
through(relayV),
callRes(1, otherPeer),
through(relayV)
),
SeqRes.wrap(
through(relayV),
through(initPeer),
failErrorRes
)
)
through(relayV),
callRes(1, otherPeer),
through(relayV)
),
errorCall(bc, 0, initPeer)
SeqRes.wrap(
through(relayV),
through(initPeer),
failErrorRes
)
),
respCall(bc, ret, initPeer)
)
),
errorCall(bc, 0, initPeer)
)

procFC.equalsOrShowDiff(expectedFC) should be(true)

}

"transform.forClient" should "link funcs correctly" in {
/*
func one() -> u64:
variable <- Demo.get42()
<- variable
func f1() -> string:
v <- srv1.fn1()
<- v

func two() -> u64:
variable <- one()
<- variable
func f2() -> string:
v <- f1()
<- v
*/

val f1: FuncArrow =
Expand Down Expand Up @@ -163,13 +171,13 @@ class TransformSpec extends AnyFlatSpec with Matchers {

val procFC = Transform.funcRes(f2, bc).value.body

val expectedFC = SeqRes.wrap(
dataCall(bc, "-relay-", initPeer),
XorRes.wrap(
val expectedFC = XorRes.wrap(
SeqRes.wrap(
dataCall(bc, "-relay-", initPeer),
callRes(1, initPeer),
errorCall(bc, 0, initPeer)
respCall(bc, VarRaw("v", ScalarType.string), initPeer)
),
respCall(bc, VarRaw("v", ScalarType.string), initPeer)
errorCall(bc, 0, initPeer)
)

procFC.equalsOrShowDiff(expectedFC) should be(true)
Expand Down