From 4f1892b0c85c3dc586f179ca7e3a689cdd9dfdc7 Mon Sep 17 00:00:00 2001 From: Remy Date: Wed, 8 Dec 2021 14:34:39 +0100 Subject: [PATCH] Speedy: Implement EToRequiredInterface and EFromRequiredInterface (#12046) Part of #11978. CHANGELOG_BEGIN CHANGELOG_END --- .../daml/lf/speedy/Compiler.scala | 8 +++---- .../daml/lf/speedy/SBuiltin.scala | 22 +++++++++++++++++++ 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/daml-lf/interpreter/src/main/scala/com/digitalasset/daml/lf/speedy/Compiler.scala b/daml-lf/interpreter/src/main/scala/com/digitalasset/daml/lf/speedy/Compiler.scala index d43a734ce8c1..0941a00f2ba8 100644 --- a/daml-lf/interpreter/src/main/scala/com/digitalasset/daml/lf/speedy/Compiler.scala +++ b/daml-lf/interpreter/src/main/scala/com/digitalasset/daml/lf/speedy/Compiler.scala @@ -578,11 +578,9 @@ private[lf] final class Compiler( case ECallInterface(iface, methodName, e) => SBCallInterface(iface, methodName)(compile(env, e)) case EToRequiredInterface(requiredIfaceId @ _, requiringIfaceId @ _, body @ _) => - // TODO https://github.com/digital-asset/daml/issues/11978 - throw CompilationError("EToRequiredInterface is not implemented") - case EFromRequiredInterface(requiredIfaceId @ _, requiringIfaceId @ _, body @ _) => - // TODO https://github.com/digital-asset/daml/issues/11978 - throw CompilationError("EFromRequiredInterface is not implemented") + compile(env, body) + case EFromRequiredInterface(requiredIfaceId @ _, requiringIfaceId, body @ _) => + SBFromRequiredInterface(requiringIfaceId)(compile(env, body)) case EExperimental(name, _) => SBExperimental(name) diff --git a/daml-lf/interpreter/src/main/scala/com/digitalasset/daml/lf/speedy/SBuiltin.scala b/daml-lf/interpreter/src/main/scala/com/digitalasset/daml/lf/speedy/SBuiltin.scala index e1cab2ee8920..5b2227fd2df6 100644 --- a/daml-lf/interpreter/src/main/scala/com/digitalasset/daml/lf/speedy/SBuiltin.scala +++ b/daml-lf/interpreter/src/main/scala/com/digitalasset/daml/lf/speedy/SBuiltin.scala @@ -1251,6 +1251,28 @@ private[lf] object SBuiltin { } } + // Convert an interface `requiredIface` to another interface `requiringIface`, if + // the `requiringIface` implements `requiredIface`. + final case class SBFromRequiredInterface( + requiringIface: TypeConName + ) extends SBuiltin(1) { + + override private[speedy] def execute( + args: util.ArrayList[SValue], + machine: Machine, + ) = { + val record = getSRecord(args, 0) + // TODO https://github.com/digital-asset/daml/issues/11345 + // The lookup is probably slow. We may want to investigate way to make the feature faster. + machine.returnValue = machine.compiledPackages.interface.lookupTemplate(record.id) match { + case Right(ifaceSignature) if ifaceSignature.implements.contains(requiringIface) => + SOptional(Some(record)) + case _ => + SOptional(None) + } + } + } + final case class SBCallInterface( ifaceId: TypeConName, methodName: MethodName,