mirror of
https://github.com/digital-asset/daml.git
synced 2024-09-20 01:07:18 +03:00
LF: Add a convenient companion object for ArrayList (#13684)
CHANGELOG_BEGIN CHANGELOG_END
This commit is contained in:
parent
a88ae129b5
commit
79af226966
@ -5,7 +5,6 @@ package com.daml.lf
|
||||
package engine
|
||||
package preprocessing
|
||||
|
||||
import java.util
|
||||
import com.daml.lf.data.{ImmArray, Ref}
|
||||
import com.daml.lf.language.{Ast, LookupError}
|
||||
import com.daml.lf.speedy.SValue
|
||||
@ -171,12 +170,6 @@ private[engine] final class Preprocessor(
|
||||
|
||||
private[preprocessing] object Preprocessor {
|
||||
|
||||
private[preprocessing] def ArrayList[X](as: X*): util.ArrayList[X] = {
|
||||
val a = new util.ArrayList[X](as.length)
|
||||
as.foreach(a.add)
|
||||
a
|
||||
}
|
||||
|
||||
@throws[Error.Preprocessing.Error]
|
||||
def handleLookup[X](either: Either[LookupError, X]): X = either match {
|
||||
case Right(v) => v
|
||||
|
@ -8,7 +8,7 @@ package preprocessing
|
||||
import com.daml.lf.data._
|
||||
import com.daml.lf.language.Ast._
|
||||
import com.daml.lf.language.{Util => AstUtil}
|
||||
import com.daml.lf.speedy.SValue
|
||||
import com.daml.lf.speedy.{ArrayList, SValue}
|
||||
import com.daml.lf.value.Value
|
||||
import com.daml.lf.value.Value._
|
||||
|
||||
@ -220,7 +220,7 @@ private[lf] final class ValueTranslator(
|
||||
SValue.SRecord(
|
||||
tyCon,
|
||||
fields.map(_._1),
|
||||
ArrayList(fields.map(_._2).toSeq: _*),
|
||||
fields.iterator.map(_._2).to(ArrayList),
|
||||
)
|
||||
|
||||
case ValueEnum(mbId, constructor) if tyArgs.isEmpty =>
|
||||
|
@ -4,7 +4,6 @@
|
||||
package com.daml.lf
|
||||
package engine
|
||||
|
||||
import java.util
|
||||
import java.io.File
|
||||
import com.daml.lf.archive.UniversalArchiveDecoder
|
||||
import com.daml.bazeltools.BazelRunfiles
|
||||
@ -25,7 +24,7 @@ import com.daml.lf.transaction.{
|
||||
import com.daml.lf.transaction.{Normalization, Validation, ReplayMismatch}
|
||||
import com.daml.lf.value.Value
|
||||
import Value._
|
||||
import com.daml.lf.speedy.{InitialSeeding, SValue, svalue}
|
||||
import com.daml.lf.speedy.{ArrayList, InitialSeeding, SValue, svalue}
|
||||
import com.daml.lf.speedy.SValue._
|
||||
import com.daml.lf.command._
|
||||
import com.daml.lf.engine.Error.Interpretation
|
||||
@ -2223,12 +2222,6 @@ object EngineTest {
|
||||
private def toContractId(s: String): ContractId =
|
||||
ContractId.V1.assertBuild(crypto.Hash.hashPrivateKey(s), dummySuffix)
|
||||
|
||||
private def ArrayList[X](as: X*): util.ArrayList[X] = {
|
||||
val a = new util.ArrayList[X](as.length)
|
||||
as.foreach(a.add)
|
||||
a
|
||||
}
|
||||
|
||||
private def findNodeByIdx[Cid](nodes: Map[NodeId, Node], idx: Int) =
|
||||
nodes.collectFirst { case (nodeId, node) if nodeId.index == idx => node }
|
||||
|
||||
|
@ -8,6 +8,7 @@ package preprocessing
|
||||
import com.daml.lf.data._
|
||||
import com.daml.lf.language.Ast
|
||||
import com.daml.lf.language.Util._
|
||||
import com.daml.lf.speedy.ArrayList
|
||||
import com.daml.lf.speedy.SValue._
|
||||
import com.daml.lf.value.Value
|
||||
import com.daml.lf.value.Value._
|
||||
@ -24,7 +25,6 @@ class ValueTranslatorSpec
|
||||
with Matchers
|
||||
with TableDrivenPropertyChecks {
|
||||
|
||||
import Preprocessor.ArrayList
|
||||
import com.daml.lf.testing.parser.Implicits._
|
||||
import com.daml.lf.transaction.test.TransactionBuilder.Implicits.{defaultPackageId => _, _}
|
||||
|
||||
|
@ -0,0 +1,70 @@
|
||||
// Copyright (c) 2022 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package com.daml.lf.speedy
|
||||
|
||||
import com.daml.scalautil.Statement.discard
|
||||
|
||||
import java.util
|
||||
import scala.collection.{View, mutable}
|
||||
import scala.jdk.CollectionConverters._
|
||||
|
||||
object ArrayList extends scala.collection.IterableFactory[util.ArrayList] {
|
||||
|
||||
override def from[A](source: IterableOnce[A]): util.ArrayList[A] = {
|
||||
val array = source.knownSize match {
|
||||
case size if size >= 0 => new util.ArrayList[A](size)
|
||||
case _ => empty[A]
|
||||
}
|
||||
source.iterator.foreach(array.add)
|
||||
array
|
||||
}
|
||||
|
||||
override def empty[A]: util.ArrayList[A] = new util.ArrayList()
|
||||
|
||||
def single[A](a: A): util.ArrayList[A] = {
|
||||
val array = new util.ArrayList[A](1)
|
||||
discard(array.add(a))
|
||||
array
|
||||
}
|
||||
|
||||
def double[A](a: A, b: A): util.ArrayList[A] = {
|
||||
val array = new util.ArrayList[A](2)
|
||||
discard(array.add(a))
|
||||
discard(array.add(b))
|
||||
array
|
||||
}
|
||||
|
||||
override def newBuilder[A]: mutable.Builder[A, util.ArrayList[A]] =
|
||||
new mutable.Builder[A, util.ArrayList[A]] {
|
||||
private[this] val array = empty[A]
|
||||
|
||||
override def clear(): Unit = array.clear()
|
||||
|
||||
override def result(): util.ArrayList[A] = array
|
||||
|
||||
override def addOne(elem: A): this.type = {
|
||||
discard[Boolean](array.add(elem))
|
||||
this
|
||||
}
|
||||
|
||||
override def sizeHint(size: Int): Unit =
|
||||
array.ensureCapacity(size)
|
||||
}
|
||||
|
||||
def unapplySeq[A](jl: util.ArrayList[A]): Some[collection.Seq[A]] = Some(jl.asScala)
|
||||
|
||||
object Implicits {
|
||||
implicit class ArrayListOps[A](val array: util.ArrayList[A]) extends AnyVal {
|
||||
def map[B](f: A => B): util.ArrayList[B] = {
|
||||
val result = new util.ArrayList[B](array.size())
|
||||
array.forEach(a => discard(result.add(f(a))))
|
||||
result
|
||||
}
|
||||
|
||||
def view: collection.View[A] = new View[A] {
|
||||
override def iterator: Iterator[A] = array.iterator().asScala
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -4,8 +4,6 @@
|
||||
package com.daml.lf
|
||||
package speedy
|
||||
|
||||
import java.util
|
||||
|
||||
import com.daml.lf.data.Ref._
|
||||
import com.daml.lf.data.{ImmArray, Numeric, Struct}
|
||||
import com.daml.lf.language.Ast._
|
||||
@ -549,7 +547,7 @@ private[lf] final class PhaseOne(
|
||||
go(exp, List.empty, List.empty)
|
||||
}
|
||||
|
||||
private def noArgs = new util.ArrayList[SValue](0)
|
||||
private def noArgs = ArrayList.empty[SValue]
|
||||
|
||||
private[this] def compileERecCon(
|
||||
env: Env,
|
||||
|
@ -9,7 +9,7 @@ import com.daml.lf.language.Ast
|
||||
import com.daml.scalautil.Statement.discard
|
||||
import java.lang.System
|
||||
import java.nio.file.{Files, Path}
|
||||
import java.util.ArrayList
|
||||
import java.util
|
||||
import scala.annotation.nowarn
|
||||
import scala.jdk.CollectionConverters._
|
||||
|
||||
@ -41,7 +41,7 @@ import scala.jdk.CollectionConverters._
|
||||
final class Profile {
|
||||
import Profile._
|
||||
private val start: Long = System.nanoTime()
|
||||
private[lf] val events: ArrayList[Event] = new ArrayList()
|
||||
private[lf] val events: util.ArrayList[Event] = ArrayList.empty
|
||||
var name: String = "Daml Engine profile"
|
||||
|
||||
def addOpenEvent(label: Label): Unit = {
|
||||
@ -154,8 +154,8 @@ object Profile {
|
||||
def fromProfile(profile: Profile) = {
|
||||
import scala.collection.mutable.HashMap
|
||||
|
||||
val frames = new ArrayList[FrameJson]()
|
||||
val frameIndices = new HashMap[String, Int]()
|
||||
val frames = ArrayList.empty[FrameJson]
|
||||
val frameIndices = HashMap.empty[String, Int]
|
||||
var endValue = 0L
|
||||
val events = profile.events.asScala.toList.map { event =>
|
||||
val eventType = if (event.open) "O" else "C"
|
||||
|
@ -11,6 +11,7 @@ import com.daml.lf.data._
|
||||
import com.daml.lf.data.Numeric.Scale
|
||||
import com.daml.lf.interpretation.{Error => IE}
|
||||
import com.daml.lf.language.Ast
|
||||
import com.daml.lf.speedy.ArrayList.Implicits._
|
||||
import com.daml.lf.speedy.SError._
|
||||
import com.daml.lf.speedy.SExpr._
|
||||
import com.daml.lf.speedy.SResult._
|
||||
@ -297,7 +298,7 @@ private[lf] object SBuiltin {
|
||||
private[speedy] def buildException(args: util.ArrayList[SValue]) =
|
||||
SArithmeticError(
|
||||
name,
|
||||
args.iterator.asScala.map(litToText(getClass.getCanonicalName, _)).to(ImmArray),
|
||||
args.view.map(litToText(getClass.getCanonicalName, _)).to(ImmArray),
|
||||
)
|
||||
|
||||
override private[speedy] def execute(
|
||||
@ -711,7 +712,7 @@ private[lf] object SBuiltin {
|
||||
/** $consMany[n] :: a -> ... -> List a -> List a */
|
||||
final case class SBConsMany(n: Int) extends SBuiltinPure(1 + n) {
|
||||
override private[speedy] def executePure(args: util.ArrayList[SValue]): SList =
|
||||
SList(args.subList(0, n).asScala.to(ImmArray) ++: getSList(args, n))
|
||||
SList(args.view.slice(0, n).to(ImmArray) ++: getSList(args, n))
|
||||
}
|
||||
|
||||
/** $cons :: a -> List a -> List a */
|
||||
@ -1883,7 +1884,7 @@ private[lf] object SBuiltin {
|
||||
private val closure: SValue = {
|
||||
val frame = Array.ofDim[SValue](0) // no free vars
|
||||
val arity = 3
|
||||
SPAP(PClosure(Profile.LabelUnset, equalListBody, frame), new util.ArrayList[SValue](), arity)
|
||||
SPAP(PClosure(Profile.LabelUnset, equalListBody, frame), ArrayList.empty, arity)
|
||||
}
|
||||
|
||||
override private[speedy] def execute(args: util.ArrayList[SValue], machine: Machine) = {
|
||||
|
@ -86,9 +86,9 @@ object SExpr {
|
||||
/* special case for nullary record constructors */
|
||||
b match {
|
||||
case SBRecCon(id, fields) if b.arity == 0 =>
|
||||
SRecord(id, fields, new util.ArrayList())
|
||||
SRecord(id, fields, ArrayList.empty)
|
||||
case _ =>
|
||||
SPAP(PBuiltin(b), new util.ArrayList(), b.arity)
|
||||
SPAP(PBuiltin(b), ArrayList.empty, b.arity)
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -189,7 +189,7 @@ object SExpr {
|
||||
i += 1
|
||||
}
|
||||
machine.returnValue =
|
||||
SPAP(PClosure(Profile.LabelUnset, body, sValues), new util.ArrayList[SValue](), arity)
|
||||
SPAP(PClosure(Profile.LabelUnset, body, sValues), ArrayList.empty, arity)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -115,7 +115,8 @@ sealed trait SValue {
|
||||
go(this)
|
||||
}
|
||||
|
||||
def mapContractId(f: V.ContractId => V.ContractId): SValue =
|
||||
def mapContractId(f: V.ContractId => V.ContractId): SValue = {
|
||||
import ArrayList.Implicits._
|
||||
this match {
|
||||
case SContractId(coid) => SContractId(f(coid))
|
||||
case SEnum(_, _, _) | _: SPrimLit | SToken | STNat(_) | STypeRep(_) => this
|
||||
@ -125,12 +126,11 @@ sealed trait SValue {
|
||||
PClosure(label, expr, vars.map(_.mapContractId(f)))
|
||||
case _: PBuiltin => prim
|
||||
}
|
||||
val args2 = mapArrayList(args, _.mapContractId(f))
|
||||
SPAP(prim2, args2, arity)
|
||||
SPAP(prim2, args.map(_.mapContractId(f)), arity)
|
||||
case SRecord(tycon, fields, values) =>
|
||||
SRecord(tycon, fields, mapArrayList(values, v => v.mapContractId(f)))
|
||||
SRecord(tycon, fields, values.map(_.mapContractId(f)))
|
||||
case SStruct(fields, values) =>
|
||||
SStruct(fields, mapArrayList(values, v => v.mapContractId(f)))
|
||||
SStruct(fields, values.map(_.mapContractId(f)))
|
||||
case SVariant(tycon, variant, rank, value) =>
|
||||
SVariant(tycon, variant, rank, value.mapContractId(f))
|
||||
case SList(lst) =>
|
||||
@ -145,6 +145,7 @@ sealed trait SValue {
|
||||
case SAny(ty, value) =>
|
||||
SAny(ty, value.mapContractId(f))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
object SValue {
|
||||
@ -265,12 +266,9 @@ object SValue {
|
||||
object SArithmeticError {
|
||||
val fields: ImmArray[Ref.Name] = ImmArray(ValueArithmeticError.fieldName)
|
||||
def apply(builtinName: String, args: ImmArray[String]): SAny = {
|
||||
val array = new util.ArrayList[SValue](1)
|
||||
discard(
|
||||
array.add(
|
||||
val array = ArrayList.single[SValue](
|
||||
SText(s"ArithmeticError while evaluating ($builtinName ${args.iterator.mkString(" ")}).")
|
||||
)
|
||||
)
|
||||
SAny(ValueArithmeticError.typ, SRecord(ValueArithmeticError.tyCon, fields, array))
|
||||
}
|
||||
// Assumes excep is properly typed
|
||||
@ -387,33 +385,13 @@ object SValue {
|
||||
assert(entryFields.indexOf(keyFieldName) == 0)
|
||||
assert(entryFields.indexOf(valueFieldName) == 1)
|
||||
|
||||
private def entry(key: SValue, value: SValue) = {
|
||||
val args = new util.ArrayList[SValue](2)
|
||||
discard(args.add(key))
|
||||
discard(args.add(value))
|
||||
SStruct(entryFields, args)
|
||||
}
|
||||
|
||||
def toList(entries: TreeMap[SValue, SValue]): SList =
|
||||
SList(
|
||||
entries.view
|
||||
.map { case (k, v) =>
|
||||
entry(k, v)
|
||||
}
|
||||
.map { case (k, v) => SStruct(entryFields, ArrayList.double(k, v)) }
|
||||
.to(FrontStack)
|
||||
)
|
||||
|
||||
private def mapArrayList(
|
||||
as: util.ArrayList[SValue],
|
||||
f: SValue => SValue,
|
||||
): util.ArrayList[SValue] = {
|
||||
val bs = new util.ArrayList[SValue](as.size)
|
||||
as.forEach { a =>
|
||||
discard(bs.add(f(a)))
|
||||
}
|
||||
bs
|
||||
}
|
||||
|
||||
private[this] val overflowUnderflow = Left("overflow/underflow")
|
||||
|
||||
}
|
||||
|
@ -764,14 +764,11 @@ private[lf] object Speedy {
|
||||
val lookupResult =
|
||||
assertRight(compiledPackages.interface.lookupDataRecord(tyCon))
|
||||
lazy val subst = lookupResult.subst(argTypes)
|
||||
val n = fields.length
|
||||
val values = new util.ArrayList[SValue](n)
|
||||
(lookupResult.dataRecord.fields.iterator zip fields.iterator).foreach {
|
||||
case ((_, fieldType), (_, fieldValue)) =>
|
||||
discard[Boolean](
|
||||
values.add(go(AstUtil.substitute(fieldType, subst), fieldValue))
|
||||
)
|
||||
val values = (lookupResult.dataRecord.fields.iterator zip fields.iterator)
|
||||
.map { case ((_, fieldType), (_, fieldValue)) =>
|
||||
go(AstUtil.substitute(fieldType, subst), fieldValue)
|
||||
}
|
||||
.to(ArrayList)
|
||||
SValue.SRecord(tyCon, lookupResult.dataRecord.fields.map(_._1), values)
|
||||
case V.ValueVariant(_, constructor, value) =>
|
||||
val info =
|
||||
|
@ -4,7 +4,6 @@
|
||||
package com.daml.lf
|
||||
package speedy
|
||||
|
||||
import java.util
|
||||
import com.daml.lf.data._
|
||||
import com.daml.lf.language.Ast._
|
||||
import com.daml.lf.speedy.SExpr.SExpr
|
||||
@ -44,7 +43,7 @@ class CompilerTest extends AnyWordSpec with Matchers {
|
||||
.fill(10 * 1000)(
|
||||
Command.Create(
|
||||
recordCon,
|
||||
SValue.SRecord(recordCon, ImmArray.Empty, new util.ArrayList()),
|
||||
SValue.SRecord(recordCon, ImmArray.Empty, ArrayList.empty),
|
||||
)
|
||||
)
|
||||
.toImmArray
|
||||
|
@ -4,7 +4,6 @@
|
||||
package com.daml.lf
|
||||
package speedy
|
||||
|
||||
import java.util
|
||||
import com.daml.lf.crypto.Hash
|
||||
import com.daml.lf.data._
|
||||
import com.daml.lf.interpretation.{Error => IE}
|
||||
@ -1698,12 +1697,7 @@ object SBuiltinTest {
|
||||
|
||||
private val entryFields = Struct.assertFromNameSeq(List(keyFieldName, valueFieldName))
|
||||
|
||||
private def mapEntry(k: String, v: SValue) = {
|
||||
val args = new util.ArrayList[SValue](2)
|
||||
args.add(SText(k))
|
||||
args.add(v)
|
||||
SStruct(entryFields, args)
|
||||
}
|
||||
private def mapEntry(k: String, v: SValue) = SStruct(entryFields, ArrayList(SText(k), v))
|
||||
|
||||
private def lit2string(x: SValue): String =
|
||||
x match {
|
||||
|
@ -4,7 +4,6 @@
|
||||
package com.daml.lf
|
||||
package speedy
|
||||
|
||||
import java.util
|
||||
import com.daml.lf.data.Ref._
|
||||
import com.daml.lf.data.{FrontStack, ImmArray, Struct}
|
||||
import com.daml.lf.language.Ast._
|
||||
@ -533,10 +532,4 @@ object SpeedyTest {
|
||||
private def intList(xs: Long*): String =
|
||||
if (xs.isEmpty) "(Nil @Int64)"
|
||||
else xs.mkString(s"(Cons @Int64 [", ", ", s"] (Nil @Int64))")
|
||||
|
||||
private def ArrayList[X](as: X*): util.ArrayList[X] = {
|
||||
val a = new util.ArrayList[X](as.length)
|
||||
as.foreach(a.add)
|
||||
a
|
||||
}
|
||||
}
|
||||
|
@ -11,7 +11,7 @@ import scalaz.std.either._
|
||||
import com.daml.lf.data.{ImmArray, Ref}
|
||||
import Ref._
|
||||
import com.daml.lf.language.Ast
|
||||
import com.daml.lf.speedy.SValue
|
||||
import com.daml.lf.speedy.{ArrayList, SValue}
|
||||
import SValue._
|
||||
import com.daml.lf.value.Value.ContractId
|
||||
|
||||
@ -62,7 +62,7 @@ private[daml] object Converter {
|
||||
|
||||
object DamlTuple2 {
|
||||
def unapply(v: SRecord): Option[(SValue, SValue)] = v match {
|
||||
case SRecord(Identifier(_, DaTypesTuple2), _, JavaList(fst, snd)) =>
|
||||
case SRecord(Identifier(_, DaTypesTuple2), _, ArrayList(fst, snd)) =>
|
||||
Some((fst, snd))
|
||||
case _ => None
|
||||
}
|
||||
@ -75,11 +75,6 @@ private[daml] object Converter {
|
||||
}
|
||||
}
|
||||
|
||||
object JavaList {
|
||||
def unapplySeq[A](jl: util.List[A]): Some[collection.Seq[A]] =
|
||||
Some(jl.asScala)
|
||||
}
|
||||
|
||||
object Implicits {
|
||||
implicit final class `intoOr and expect`[A](private val self: A) extends AnyVal {
|
||||
def intoOr[R, L](pf: A PartialFunction R)(orElse: => L): Either[L, R] =
|
||||
|
@ -19,7 +19,7 @@ import com.daml.lf.speedy.SBuiltin._
|
||||
import com.daml.lf.speedy.SExpr._
|
||||
import com.daml.lf.speedy.SResult._
|
||||
import com.daml.lf.speedy.SValue._
|
||||
import com.daml.lf.speedy.{Pretty, SValue, Speedy}
|
||||
import com.daml.lf.speedy.{ArrayList, Pretty, SValue, Speedy}
|
||||
import com.daml.lf.speedy.SExpr.SExpr
|
||||
import com.daml.lf.value.Value
|
||||
import com.daml.lf.value.Value.ContractId
|
||||
@ -186,7 +186,7 @@ object Converter {
|
||||
|
||||
def toAnyChoice(v: SValue): Either[String, AnyChoice] = {
|
||||
v match {
|
||||
case SRecord(_, _, JavaList(SAny(TTyCon(tyCon), choiceVal), _)) =>
|
||||
case SRecord(_, _, ArrayList(SAny(TTyCon(tyCon), choiceVal), _)) =>
|
||||
// This exploits the fact that in Daml, choice argument type names
|
||||
// and choice names match up.
|
||||
ChoiceName.fromString(tyCon.qualifiedName.name.toString).map(AnyChoice(_, choiceVal))
|
||||
@ -563,7 +563,7 @@ object Converter {
|
||||
case SRecord(
|
||||
_,
|
||||
_,
|
||||
JavaList(unitId, module, file @ _, startLine, startCol, endLine, endCol),
|
||||
ArrayList(unitId, module, file @ _, startLine, startCol, endLine, endCol),
|
||||
) =>
|
||||
for {
|
||||
unitId <- toText(unitId)
|
||||
@ -584,7 +584,7 @@ object Converter {
|
||||
|
||||
def toLocation(knownPackages: Map[String, PackageId], v: SValue): Either[String, Location] =
|
||||
v match {
|
||||
case SRecord(_, _, JavaList(definition, loc)) =>
|
||||
case SRecord(_, _, ArrayList(definition, loc)) =>
|
||||
for {
|
||||
// TODO[AH] This should be the outer definition. E.g. `main` in `main = do submit ...`.
|
||||
// However, the call-stack only gives us access to the inner definition, `submit` in this case.
|
||||
|
@ -38,6 +38,7 @@ import com.daml.lf.speedy.SExpr._
|
||||
import com.daml.lf.speedy.SResult._
|
||||
import com.daml.lf.speedy.SValue._
|
||||
import com.daml.lf.speedy.{
|
||||
ArrayList,
|
||||
Compiler,
|
||||
Pretty,
|
||||
SDefinition,
|
||||
@ -50,7 +51,7 @@ import com.daml.lf.speedy.{
|
||||
import com.daml.lf.value.Value.ContractId
|
||||
import com.daml.lf.value.json.ApiCodecCompressed
|
||||
import com.daml.logging.LoggingContext
|
||||
import com.daml.script.converter.Converter.{JavaList, unrollFree}
|
||||
import com.daml.script.converter.Converter.unrollFree
|
||||
import com.daml.script.converter.ConverterException
|
||||
import com.typesafe.scalalogging.StrictLogging
|
||||
import scalaz.OneAnd._
|
||||
@ -465,7 +466,7 @@ private[lf] class Runner(
|
||||
.flatMap(run(_))
|
||||
case Left(v) =>
|
||||
v match {
|
||||
case SRecord(_, _, JavaList(newState, _)) => {
|
||||
case SRecord(_, _, ArrayList(newState, _)) => {
|
||||
// Unwrap the Tuple2 we get from the inlined StateT.
|
||||
Future { newState }
|
||||
}
|
||||
|
@ -17,13 +17,12 @@ import com.daml.lf.data.Time.Timestamp
|
||||
import com.daml.lf.engine.script.ledgerinteraction.{ScriptLedgerClient, ScriptTimeMode}
|
||||
import com.daml.lf.language.Ast
|
||||
import com.daml.lf.speedy.SExpr.{SEApp, SEValue}
|
||||
import com.daml.lf.speedy.{SError, SValue}
|
||||
import com.daml.lf.speedy.{ArrayList, SError, SValue}
|
||||
import com.daml.lf.speedy.SExpr.SExpr
|
||||
import com.daml.lf.speedy.SValue._
|
||||
import com.daml.lf.speedy.Speedy.Machine
|
||||
import com.daml.lf.value.Value
|
||||
import com.daml.lf.value.Value.ContractId
|
||||
import com.daml.script.converter.Converter.JavaList
|
||||
import scalaz.{Foldable, OneAnd}
|
||||
import scalaz.syntax.traverse._
|
||||
import scalaz.std.either._
|
||||
@ -629,19 +628,19 @@ object ScriptF {
|
||||
} yield SubmitData(actAs, readAs.toSet, cmds, freeAp, stackTrace, continue)
|
||||
v match {
|
||||
// no location
|
||||
case SRecord(_, _, JavaList(sParty, SRecord(_, _, JavaList(freeAp)), continue)) =>
|
||||
case SRecord(_, _, ArrayList(sParty, SRecord(_, _, ArrayList(freeAp)), continue)) =>
|
||||
convert(OneAnd(sParty, List()), List(), freeAp, continue, None)
|
||||
// location
|
||||
case SRecord(_, _, JavaList(sParty, SRecord(_, _, JavaList(freeAp)), continue, loc)) =>
|
||||
case SRecord(_, _, ArrayList(sParty, SRecord(_, _, ArrayList(freeAp)), continue, loc)) =>
|
||||
convert(OneAnd(sParty, List()), List(), freeAp, continue, Some(loc))
|
||||
// multi-party actAs/readAs + location
|
||||
case SRecord(
|
||||
_,
|
||||
_,
|
||||
JavaList(
|
||||
SRecord(_, _, JavaList(hdAct, SList(tlAct))),
|
||||
ArrayList(
|
||||
SRecord(_, _, ArrayList(hdAct, SList(tlAct))),
|
||||
SList(read),
|
||||
SRecord(_, _, JavaList(freeAp)),
|
||||
SRecord(_, _, ArrayList(freeAp)),
|
||||
continue,
|
||||
loc,
|
||||
),
|
||||
@ -660,9 +659,9 @@ object ScriptF {
|
||||
stackTrace <- toStackTrace(ctx, stackTrace)
|
||||
} yield Query(readAs, tplId, stackTrace, continue)
|
||||
v match {
|
||||
case SRecord(_, _, JavaList(actAs, tplId, continue)) =>
|
||||
case SRecord(_, _, ArrayList(actAs, tplId, continue)) =>
|
||||
convert(actAs, tplId, None, continue)
|
||||
case SRecord(_, _, JavaList(actAs, tplId, continue, stackTrace)) =>
|
||||
case SRecord(_, _, ArrayList(actAs, tplId, continue, stackTrace)) =>
|
||||
convert(actAs, tplId, Some(stackTrace), continue)
|
||||
case _ => Left(s"Expected Query payload but got $v")
|
||||
}
|
||||
@ -683,9 +682,9 @@ object ScriptF {
|
||||
stackTrace <- toStackTrace(ctx, stackTrace)
|
||||
} yield QueryContractId(actAs, tplId, cid, stackTrace, continue)
|
||||
v match {
|
||||
case SRecord(_, _, JavaList(actAs, tplId, cid, continue)) =>
|
||||
case SRecord(_, _, ArrayList(actAs, tplId, cid, continue)) =>
|
||||
convert(actAs, tplId, cid, None, continue)
|
||||
case SRecord(_, _, JavaList(actAs, tplId, cid, continue, stackTrace)) =>
|
||||
case SRecord(_, _, ArrayList(actAs, tplId, cid, continue, stackTrace)) =>
|
||||
convert(actAs, tplId, cid, Some(stackTrace), continue)
|
||||
case _ => Left(s"Expected QueryContractId payload but got $v")
|
||||
}
|
||||
@ -706,9 +705,9 @@ object ScriptF {
|
||||
stackTrace <- toStackTrace(ctx, stackTrace)
|
||||
} yield QueryContractKey(actAs, tplId, key, stackTrace, continue)
|
||||
v match {
|
||||
case SRecord(_, _, JavaList(actAs, tplId, key, continue)) =>
|
||||
case SRecord(_, _, ArrayList(actAs, tplId, key, continue)) =>
|
||||
convert(actAs, tplId, key, None, continue)
|
||||
case SRecord(_, _, JavaList(actAs, tplId, key, continue, stackTrace)) =>
|
||||
case SRecord(_, _, ArrayList(actAs, tplId, key, continue, stackTrace)) =>
|
||||
convert(actAs, tplId, key, Some(stackTrace), continue)
|
||||
case _ => Left(s"Expected QueryContractKey payload but got $v")
|
||||
}
|
||||
@ -730,7 +729,7 @@ object ScriptF {
|
||||
case SRecord(
|
||||
_,
|
||||
_,
|
||||
JavaList(
|
||||
ArrayList(
|
||||
SText(displayName),
|
||||
SText(idHint),
|
||||
participantName,
|
||||
@ -741,7 +740,7 @@ object ScriptF {
|
||||
case SRecord(
|
||||
_,
|
||||
_,
|
||||
JavaList(
|
||||
ArrayList(
|
||||
SText(displayName),
|
||||
SText(idHint),
|
||||
participantName,
|
||||
@ -761,9 +760,9 @@ object ScriptF {
|
||||
stackTrace <- toStackTrace(ctx, stackTrace)
|
||||
} yield ListKnownParties(participantName, stackTrace, continue)
|
||||
v match {
|
||||
case SRecord(_, _, JavaList(participantName, continue)) =>
|
||||
case SRecord(_, _, ArrayList(participantName, continue)) =>
|
||||
convert(participantName, None, continue)
|
||||
case SRecord(_, _, JavaList(participantName, continue, stackTrace)) =>
|
||||
case SRecord(_, _, ArrayList(participantName, continue, stackTrace)) =>
|
||||
convert(participantName, Some(stackTrace), continue)
|
||||
case _ => Left(s"Expected ListKnownParties payload but got $v")
|
||||
}
|
||||
@ -775,7 +774,7 @@ object ScriptF {
|
||||
stackTrace <- toStackTrace(ctx, stackTrace)
|
||||
} yield GetTime(stackTrace, continue)
|
||||
v match {
|
||||
case SRecord(_, _, JavaList(continue, stackTrace)) => convert(Some(stackTrace), continue)
|
||||
case SRecord(_, _, ArrayList(continue, stackTrace)) => convert(Some(stackTrace), continue)
|
||||
case _ => convert(None, v)
|
||||
}
|
||||
}
|
||||
@ -787,8 +786,8 @@ object ScriptF {
|
||||
stackTrace <- toStackTrace(ctx, stackTrace)
|
||||
} yield SetTime(time, stackTrace, continue)
|
||||
v match {
|
||||
case SRecord(_, _, JavaList(time, continue)) => convert(time, None, continue)
|
||||
case SRecord(_, _, JavaList(time, continue, stackTrace)) =>
|
||||
case SRecord(_, _, ArrayList(time, continue)) => convert(time, None, continue)
|
||||
case SRecord(_, _, ArrayList(time, continue, stackTrace)) =>
|
||||
convert(time, Some(stackTrace), continue)
|
||||
case _ => Left(s"Expected SetTime payload but got $v")
|
||||
}
|
||||
@ -802,9 +801,13 @@ object ScriptF {
|
||||
}
|
||||
|
||||
v match {
|
||||
case SRecord(_, _, JavaList(SRecord(_, _, JavaList(SInt64(micros))), continue)) =>
|
||||
case SRecord(_, _, ArrayList(SRecord(_, _, ArrayList(SInt64(micros))), continue)) =>
|
||||
convert(micros, None, continue)
|
||||
case SRecord(_, _, JavaList(SRecord(_, _, JavaList(SInt64(micros))), continue, stackTrace)) =>
|
||||
case SRecord(
|
||||
_,
|
||||
_,
|
||||
ArrayList(SRecord(_, _, ArrayList(SInt64(micros))), continue, stackTrace),
|
||||
) =>
|
||||
convert(micros, Some(stackTrace), continue)
|
||||
case _ => Left(s"Expected Sleep payload but got $v")
|
||||
}
|
||||
@ -813,7 +816,7 @@ object ScriptF {
|
||||
|
||||
private def parseCatch(v: SValue): Either[String, Catch] = {
|
||||
v match {
|
||||
case SRecord(_, _, JavaList(act, handle)) =>
|
||||
case SRecord(_, _, ArrayList(act, handle)) =>
|
||||
Right(Catch(act, handle))
|
||||
case _ => Left(s"Expected Catch payload but got $v")
|
||||
}
|
||||
@ -822,7 +825,7 @@ object ScriptF {
|
||||
|
||||
private def parseThrow(v: SValue): Either[String, Throw] = {
|
||||
v match {
|
||||
case SRecord(_, _, JavaList(exc: SAny)) =>
|
||||
case SRecord(_, _, ArrayList(exc: SAny)) =>
|
||||
Right(Throw(exc))
|
||||
case _ => Left(s"Expected Throw payload but got $v")
|
||||
}
|
||||
@ -831,7 +834,7 @@ object ScriptF {
|
||||
|
||||
private def parseValidateUserId(ctx: Ctx, v: SValue): Either[String, ValidateUserId] =
|
||||
v match {
|
||||
case SRecord(_, _, JavaList(userName, continue, stackTrace)) =>
|
||||
case SRecord(_, _, ArrayList(userName, continue, stackTrace)) =>
|
||||
for {
|
||||
userName <- toText(userName)
|
||||
stackTrace <- toStackTrace(ctx, Some(stackTrace))
|
||||
@ -841,7 +844,7 @@ object ScriptF {
|
||||
|
||||
private def parseCreateUser(ctx: Ctx, v: SValue): Either[String, CreateUser] =
|
||||
v match {
|
||||
case SRecord(_, _, JavaList(user, rights, participant, continue, stackTrace)) =>
|
||||
case SRecord(_, _, ArrayList(user, rights, participant, continue, stackTrace)) =>
|
||||
for {
|
||||
user <- Converter.toUser(user)
|
||||
participant <- Converter.toParticipantName(participant)
|
||||
@ -853,7 +856,7 @@ object ScriptF {
|
||||
|
||||
private def parseGetUser(ctx: Ctx, v: SValue): Either[String, GetUser] =
|
||||
v match {
|
||||
case SRecord(_, _, JavaList(userId, participant, continue, stackTrace)) =>
|
||||
case SRecord(_, _, ArrayList(userId, participant, continue, stackTrace)) =>
|
||||
for {
|
||||
userId <- Converter.toUserId(userId)
|
||||
participant <- Converter.toParticipantName(participant)
|
||||
@ -864,7 +867,7 @@ object ScriptF {
|
||||
|
||||
private def parseDeleteUser(ctx: Ctx, v: SValue): Either[String, DeleteUser] =
|
||||
v match {
|
||||
case SRecord(_, _, JavaList(userId, participant, continue, stackTrace)) =>
|
||||
case SRecord(_, _, ArrayList(userId, participant, continue, stackTrace)) =>
|
||||
for {
|
||||
userId <- Converter.toUserId(userId)
|
||||
participant <- Converter.toParticipantName(participant)
|
||||
@ -875,7 +878,7 @@ object ScriptF {
|
||||
|
||||
private def parseListAllUsers(ctx: Ctx, v: SValue): Either[String, ListAllUsers] =
|
||||
v match {
|
||||
case SRecord(_, _, JavaList(participant, continue, stackTrace)) =>
|
||||
case SRecord(_, _, ArrayList(participant, continue, stackTrace)) =>
|
||||
for {
|
||||
participant <- Converter.toParticipantName(participant)
|
||||
stackTrace <- toStackTrace(ctx, Some(stackTrace))
|
||||
@ -885,7 +888,7 @@ object ScriptF {
|
||||
|
||||
private def parseGrantUserRights(ctx: Ctx, v: SValue): Either[String, GrantUserRights] =
|
||||
v match {
|
||||
case SRecord(_, _, JavaList(userId, rights, participant, continue, stackTrace)) =>
|
||||
case SRecord(_, _, ArrayList(userId, rights, participant, continue, stackTrace)) =>
|
||||
for {
|
||||
userId <- Converter.toUserId(userId)
|
||||
rights <- Converter.toList(rights, Converter.toUserRight)
|
||||
@ -897,7 +900,7 @@ object ScriptF {
|
||||
|
||||
private def parseRevokeUserRights(ctx: Ctx, v: SValue): Either[String, RevokeUserRights] =
|
||||
v match {
|
||||
case SRecord(_, _, JavaList(userId, rights, participant, continue, stackTrace)) =>
|
||||
case SRecord(_, _, ArrayList(userId, rights, participant, continue, stackTrace)) =>
|
||||
for {
|
||||
userId <- Converter.toUserId(userId)
|
||||
rights <- Converter.toList(rights, Converter.toUserRight)
|
||||
@ -909,7 +912,7 @@ object ScriptF {
|
||||
|
||||
private def parseListUserRights(ctx: Ctx, v: SValue): Either[String, ListUserRights] =
|
||||
v match {
|
||||
case SRecord(_, _, JavaList(userId, participant, continue, stackTrace)) =>
|
||||
case SRecord(_, _, ArrayList(userId, participant, continue, stackTrace)) =>
|
||||
for {
|
||||
userId <- Converter.toUserId(userId)
|
||||
participant <- Converter.toParticipantName(participant)
|
||||
|
@ -4,7 +4,6 @@
|
||||
package com.daml.lf.engine.script.test
|
||||
|
||||
import java.io.File
|
||||
import java.util
|
||||
|
||||
import com.daml.ledger.api.testing.utils.AkkaBeforeAndAfterAll
|
||||
import com.daml.lf.archive.{Dar, DarDecoder}
|
||||
@ -15,7 +14,7 @@ import com.daml.lf.engine.script.{Participants, Runner}
|
||||
import com.daml.lf.iface.EnvironmentInterface
|
||||
import com.daml.lf.iface.reader.InterfaceReader
|
||||
import com.daml.lf.language.Ast.Package
|
||||
import com.daml.lf.speedy.SValue
|
||||
import com.daml.lf.speedy.{ArrayList, SValue}
|
||||
import com.daml.lf.speedy.SValue.SRecord
|
||||
import org.scalatest.Suite
|
||||
import scalaz.\/-
|
||||
@ -46,10 +45,7 @@ trait AbstractScriptTest extends AkkaBeforeAndAfterAll {
|
||||
Runner.run(dar, scriptId, inputValue, clients, timeMode)
|
||||
}
|
||||
|
||||
def tuple(a: SValue, b: SValue) = {
|
||||
val vals = new util.ArrayList[SValue](2);
|
||||
vals.add(a)
|
||||
vals.add(b)
|
||||
def tuple(a: SValue, b: SValue) =
|
||||
SRecord(
|
||||
id = Identifier(
|
||||
PackageId.assertFromString(
|
||||
@ -58,7 +54,6 @@ trait AbstractScriptTest extends AkkaBeforeAndAfterAll {
|
||||
QualifiedName.assertFromString("DA.Types:Tuple2"),
|
||||
),
|
||||
fields = ImmArray(Name.assertFromString("_1"), Name.assertFromString("_2")),
|
||||
values = vals,
|
||||
values = ArrayList(a, b),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
@ -10,7 +10,7 @@ import scalaz.syntax.traverse._
|
||||
import com.daml.lf.data.{FrontStack, ImmArray}
|
||||
import com.daml.lf.data.Ref._
|
||||
import com.daml.lf.language.Ast._
|
||||
import com.daml.lf.speedy.SValue
|
||||
import com.daml.lf.speedy.{ArrayList, SValue}
|
||||
import com.daml.lf.speedy.SValue._
|
||||
import com.daml.lf.value.Value.ContractId
|
||||
import com.daml.ledger.api.v1.commands.{
|
||||
@ -315,7 +315,7 @@ object Converter {
|
||||
private def toFiniteDuration(value: SValue): Either[String, FiniteDuration] =
|
||||
value.expect(
|
||||
"RelTime",
|
||||
{ case SRecord(_, _, JavaList(SInt64(microseconds))) =>
|
||||
{ case SRecord(_, _, ArrayList(SInt64(microseconds))) =>
|
||||
FiniteDuration(microseconds, MICROSECONDS)
|
||||
},
|
||||
)
|
||||
@ -331,7 +331,7 @@ object Converter {
|
||||
private def toTemplateTypeRep(v: SValue): Either[String, Identifier] =
|
||||
v.expectE(
|
||||
"TemplateTypeRep",
|
||||
{ case SRecord(_, _, JavaList(id)) =>
|
||||
{ case SRecord(_, _, ArrayList(id)) =>
|
||||
toIdentifier(id)
|
||||
},
|
||||
)
|
||||
@ -339,7 +339,7 @@ object Converter {
|
||||
private def toRegisteredTemplate(v: SValue): Either[String, Identifier] =
|
||||
v.expectE(
|
||||
"RegisteredTemplate",
|
||||
{ case SRecord(_, _, JavaList(sttr)) =>
|
||||
{ case SRecord(_, _, ArrayList(sttr)) =>
|
||||
toTemplateTypeRep(sttr)
|
||||
},
|
||||
)
|
||||
@ -355,7 +355,7 @@ object Converter {
|
||||
private def toAnyContractId(v: SValue): Either[String, AnyContractId] =
|
||||
v.expectE(
|
||||
"AnyContractId",
|
||||
{ case SRecord(_, _, JavaList(stid, scid)) =>
|
||||
{ case SRecord(_, _, ArrayList(stid, scid)) =>
|
||||
for {
|
||||
templateId <- toTemplateTypeRep(stid)
|
||||
contractId <- toContractId(scid)
|
||||
@ -365,7 +365,7 @@ object Converter {
|
||||
|
||||
private[this] def toAnyTemplate(v: SValue): Either[String, AnyTemplate] = {
|
||||
v match {
|
||||
case SRecord(_, _, JavaList(SAny(TTyCon(tmplId), value))) =>
|
||||
case SRecord(_, _, ArrayList(SAny(TTyCon(tmplId), value))) =>
|
||||
Right(AnyTemplate(tmplId, value))
|
||||
case _ => Left(s"Expected AnyTemplate but got $v")
|
||||
}
|
||||
@ -373,7 +373,7 @@ object Converter {
|
||||
|
||||
private[this] def toAnyChoice(v: SValue): Either[String, AnyChoice] =
|
||||
v match {
|
||||
case SRecord(_, _, JavaList(SAny(TTyCon(tycon), value), _)) =>
|
||||
case SRecord(_, _, ArrayList(SAny(TTyCon(tycon), value), _)) =>
|
||||
// This exploits the fact that in Daml, choice argument type names
|
||||
// and choice names match up.
|
||||
ChoiceName.fromString(tycon.qualifiedName.name.toString).map(AnyChoice(_, value))
|
||||
@ -384,7 +384,7 @@ object Converter {
|
||||
private def toAnyContractKey(v: SValue): Either[String, AnyContractKey] =
|
||||
v.expect(
|
||||
"AnyContractKey",
|
||||
{ case SRecord(_, _, JavaList(SAny(_, v), _)) =>
|
||||
{ case SRecord(_, _, ArrayList(SAny(_, v), _)) =>
|
||||
AnyContractKey(v)
|
||||
},
|
||||
)
|
||||
@ -392,7 +392,7 @@ object Converter {
|
||||
private[this] def toCreate(v: SValue): Either[String, CreateCommand] =
|
||||
v.expectE(
|
||||
"CreateCommand",
|
||||
{ case SRecord(_, _, JavaList(sTpl)) =>
|
||||
{ case SRecord(_, _, ArrayList(sTpl)) =>
|
||||
for {
|
||||
anyTmpl <- toAnyTemplate(sTpl)
|
||||
templateArg <- toLedgerRecord(anyTmpl.arg)
|
||||
@ -403,7 +403,7 @@ object Converter {
|
||||
private[this] def toExercise(v: SValue): Either[String, ExerciseCommand] =
|
||||
v.expectE(
|
||||
"ExerciseCommand",
|
||||
{ case SRecord(_, _, JavaList(sAnyContractId, sChoiceVal)) =>
|
||||
{ case SRecord(_, _, ArrayList(sAnyContractId, sChoiceVal)) =>
|
||||
for {
|
||||
anyContractId <- toAnyContractId(sAnyContractId)
|
||||
anyChoice <- toAnyChoice(sChoiceVal)
|
||||
@ -420,7 +420,7 @@ object Converter {
|
||||
private[this] def toExerciseByKey(v: SValue): Either[String, ExerciseByKeyCommand] =
|
||||
v.expectE(
|
||||
"ExerciseByKeyCommand",
|
||||
{ case SRecord(_, _, JavaList(stplId, skeyVal, sChoiceVal)) =>
|
||||
{ case SRecord(_, _, ArrayList(stplId, skeyVal, sChoiceVal)) =>
|
||||
for {
|
||||
tplId <- toTemplateTypeRep(stplId)
|
||||
keyVal <- toAnyContractKey(skeyVal)
|
||||
@ -439,7 +439,7 @@ object Converter {
|
||||
private[this] def toCreateAndExercise(v: SValue): Either[String, CreateAndExerciseCommand] =
|
||||
v.expectE(
|
||||
"CreateAndExerciseCommand",
|
||||
{ case SRecord(_, _, JavaList(sTpl, sChoiceVal)) =>
|
||||
{ case SRecord(_, _, ArrayList(sTpl, sChoiceVal)) =>
|
||||
for {
|
||||
anyTmpl <- toAnyTemplate(sTpl)
|
||||
templateArg <- toLedgerRecord(anyTmpl.arg)
|
||||
|
Loading…
Reference in New Issue
Block a user