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

LF: SBAnyExceptionMessage queries for unknown packages. #9804

Merged
merged 2 commits into from
May 26, 2021
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
Original file line number Diff line number Diff line change
Expand Up @@ -1575,15 +1575,30 @@ private[lf] object SBuiltin {

/** $any-exception-message :: AnyException -> Text */
final case object SBAnyExceptionMessage extends SBuiltin(1) {
private def exceptionMessage(tyCon: TypeConName, value: SValue, machine: Machine) =
machine.ctrl = SEApp(SEVal(ExceptionMessageDefRef(tyCon)), Array(SEValue(value)))

override private[speedy] final def execute(
args: util.ArrayList[SValue],
machine: Machine,
): Unit = {
getSException(args, 0) match {
case SAnyException(ty, innerValue) =>
machine.ctrl = SEApp(exceptionMessage(ty), Array(SEValue(innerValue)))
case SAnyException(Ast.TTyCon(tyCon), innerValue) =>
if (!machine.compiledPackages.packageIds.contains(tyCon.packageId))
throw SpeedyHungry(
SResultNeedPackage(
tyCon.packageId,
{ packages =>
machine.compiledPackages = packages
exceptionMessage(tyCon, innerValue, machine)
},
)
)
exceptionMessage(tyCon, innerValue, machine)
case exception: SArithmeticError =>
machine.returnValue = SText(exception.message)
case SAnyException(ty, _) =>
crash(s"$ty is not a valid exception type")
}
}
}
Expand All @@ -1600,13 +1615,6 @@ private[lf] object SBuiltin {
}
}

private def exceptionMessage(ty: Ast.Type): SExpr =
ty match {
case Ast.TTyCon(tyCon) =>
SEVal(ExceptionMessageDefRef(tyCon))
case _ => crash(s"$ty is not a valid exception type")
}

/** $to_any
* :: t
* -> Any (where t = ty)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import com.daml.lf.data._
import com.daml.lf.language.Ast._
import com.daml.lf.speedy.SError.{DamlEUnhandledException, SError, SErrorCrash}
import com.daml.lf.speedy.SExpr._
import com.daml.lf.speedy.SResult.{SResultError, SResultFinalValue}
import com.daml.lf.speedy.SResult.{SResultError, SResultFinalValue, SResultNeedPackage}
import com.daml.lf.speedy.SValue.{SValue => _, _}
import com.daml.lf.testing.parser.Implicits._
import com.daml.lf.value.Value
Expand Down Expand Up @@ -1485,6 +1485,22 @@ class SBuiltinTest extends AnyFreeSpec with Matchers with TableDrivenPropertyChe
}
}

"AnyExceptionMessage" - {

"request unknown packageId" in {
eval(
e"""ANY_EXCEPTION_MESSAGE (to_any_exception @Mod:Exception (Mod:Exception {}))"""
) shouldBe Right(SText("some nice error message"))
// FIXME: should be
// e"""ANY_EXCEPTION_MESSAGE (to_any_exception @'-unknown-package-':Mod:Exception ('-unknown-package-'Mod:Exception {}))"""
// but the parser seems buggy.
eval(
e"""ANY_EXCEPTION_MESSAGE (to_any_exception @'-unknown-package-':Mod:Exception (Mod:Exception {}))"""
) shouldBe Left(SErrorCrash(s"need package '-unknown-package-'"))
}

}

}

object SBuiltinTest {
Expand All @@ -1496,6 +1512,10 @@ object SBuiltinTest {
record MyUnit = { };
record Tuple a b = { fst: a, snd: b };
enum Color = Red | Green | Blue;
record @serializable Exception = {} ;
exception Exception = {
message \(e: Mod:Exception) -> "some nice error message"
} ;
}

"""
Expand Down Expand Up @@ -1527,6 +1547,7 @@ object SBuiltinTest {
val value = machine.run() match {
case SResultFinalValue(v) => v
case SResultError(err) => throw Goodbye(err)
case SResultNeedPackage(pkgId, _) => throw Goodbye(SErrorCrash(s"need package '$pkgId'"))
case res => throw new RuntimeException(s"Got unexpected interpretation result $res")
}

Expand Down