Skip to content

Commit

Permalink
use PackageSignature inside Interface
Browse files Browse the repository at this point in the history
  • Loading branch information
remyhaemmerle-da committed Jun 2, 2021
1 parent 20b2bb9 commit 7b6818e
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ class ReplService(
val (pkgId, pkg) = Decode.decodeArchiveFromInputStream(req.getPackage.newInput)
val newSignatures = signatures.updated(pkgId, AstUtil.toSignature(pkg))
val newCompiledDefinitions = compiledDefinitions ++
new Compiler(language.Interface(newSignatures), compilerConfig)
new Compiler(new language.Interface(newSignatures), compilerConfig)
.unsafeCompilePackage(pkgId, pkg)
signatures = newSignatures
compiledDefinitions = newCompiledDefinitions
Expand Down Expand Up @@ -260,7 +260,7 @@ class ReplService(
}

val signatures = this.signatures.updated(homePackageId, AstUtil.toSignature(pkg))
val interface = language.Interface(signatures)
val interface = new language.Interface(signatures)
val defs =
new Compiler(interface, compilerConfig).unsafeCompilePackage(homePackageId, pkg)
val compiledPackages =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ class Context(val contextId: Context.ContextId, languageVersion: LanguageVersion
if (unloadPackages.nonEmpty || newPackages.nonEmpty) {
val invalidPackages = unloadModules ++ newPackages.keys
val newExtSignature = extSignatures -- unloadPackages ++ AstUtil.toSignatures(newPackages)
val interface = language.Interface(newExtSignature)
val interface = new language.Interface(newExtSignature)
val newExtDefns = extDefns.view.filterKeys(sdef => !invalidPackages(sdef.packageId)) ++
assertRight(Compiler.compilePackages(interface, newPackages, compilerConfig))
// we update only if we manage to compile the new packages
Expand All @@ -129,7 +129,7 @@ class Context(val contextId: Context.ContextId, languageVersion: LanguageVersion
newModules
}

val interface = language.Interface(this.allSignatures)
val interface = new language.Interface(this.allSignatures)
val compiler = new Compiler(interface, compilerConfig)

modulesToCompile.foreach { mod =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ private[lf] final class ConcurrentCompiledPackages(compilerConfig: Compiler.Conf
new ConcurrentHashMap()

override def packageIds: scala.collection.Set[PackageId] = signatures.keySet
override def interface: Interface = Interface(signatures)
override def interface: Interface = new Interface(signatures)
override def getDefinition(dref: speedy.SExpr.SDefinitionRef): Option[speedy.SDefinition] =
Option(definitions.get(dref))

Expand Down Expand Up @@ -90,12 +90,15 @@ private[lf] final class ConcurrentCompiledPackages(compilerConfig: Compiler.Conf
// package once. Other concurrent calls to add this package will block
// waiting for the first one to finish.
if (!signatures.contains(pkgId)) {
val tempSignature = language.Interface(Map(pkgId -> pkg) orElse signatures)
val pkgSignature = AstUtil.toSignature(pkg)
val extendedSignatures =
new language.Interface(Map(pkgId -> pkgSignature) orElse signatures)

// Compile the speedy definitions for this package.
val defns =
try {
new speedy.Compiler(tempSignature, compilerConfig).unsafeCompilePackage(pkgId, pkg)
new speedy.Compiler(extendedSignatures, compilerConfig)
.unsafeCompilePackage(pkgId, pkg)
} catch {
case CompilationError(msg) =>
return ResultError(Error(s"Compilation Error: $msg"))
Expand All @@ -112,7 +115,7 @@ private[lf] final class ConcurrentCompiledPackages(compilerConfig: Compiler.Conf
deps union packageDeps.get(dependency)
}
packageDeps.put(pkgId, deps)
signatures.put(pkgId, AstUtil.toSignature(pkg))
signatures.put(pkgId, pkgSignature)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ private[lf] object PureCompiledPackages {
defns: Map[SDefinitionRef, SDefinition],
compilerConfig: Compiler.Config,
): PureCompiledPackages =
new PureCompiledPackages(packages.keySet, Interface(packages), defns, compilerConfig)
new PureCompiledPackages(packages.keySet, new Interface(packages), defns, compilerConfig)

def build(
packages: Map[PackageId, Package],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,7 @@ object Ast {

type DValue = GenDValue[Expr]
object DValue extends GenDValueCompanion[Expr]
type DValueSignature = GenDValue[Unit]
object DValueSignature extends GenDValueCompanion[Unit]

type Definition = GenDefinition[Expr]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,20 @@ package language
import com.daml.lf.data.Ref._
import com.daml.lf.language.Ast._

private[lf] case class Interface(signatures: PartialFunction[PackageId, GenPackage[_]]) {
private[lf] class Interface(signatures: PartialFunction[PackageId, PackageSignature]) {

import Interface._

def lookupPackage(pkgId: PackageId): Either[LookupError, GenPackage[_]] =
def lookupPackage(pkgId: PackageId): Either[LookupError, PackageSignature] =
signatures.lift(pkgId).toRight(LookupError.Package(pkgId))

def lookupModule(
pkgId: PackageId,
modName: ModuleName,
): Either[LookupError, GenModule[_]] =
): Either[LookupError, ModuleSignature] =
lookupPackage(pkgId).flatMap(_.modules.get(modName).toRight(LookupError.Module(pkgId, modName)))

def lookupDefinition(name: TypeConName): Either[LookupError, GenDefinition[_]] =
def lookupDefinition(name: TypeConName): Either[LookupError, DefinitionSignature] =
lookupModule(name.packageId, name.qualifiedName.module).flatMap(
_.definitions.get(name.qualifiedName.name).toRight(LookupError.Definition(name))
)
Expand Down Expand Up @@ -101,29 +101,29 @@ private[lf] case class Interface(signatures: PartialFunction[PackageId, GenPacka
}
}

def lookupTemplate(name: TypeConName): Either[LookupError, GenTemplate[_]] =
def lookupTemplate(name: TypeConName): Either[LookupError, TemplateSignature] =
lookupModule(name.packageId, name.qualifiedName.module).flatMap(
_.templates.get(name.qualifiedName.name).toRight(LookupError.Template(name))
)

def lookupChoice(
tmpName: TypeConName,
chName: ChoiceName,
): Either[LookupError, GenTemplateChoice[_]] =
): Either[LookupError, TemplateChoiceSignature] =
lookupTemplate(tmpName).flatMap(
_.choices.get(chName).toRight(LookupError.Choice(tmpName, chName))
)

def lookupTemplateKey(name: TypeConName): Either[LookupError, GenTemplateKey[_]] =
def lookupTemplateKey(name: TypeConName): Either[LookupError, TemplateKeySignature] =
lookupTemplate(name).flatMap(_.key.toRight(LookupError.TemplateKey(name)))

def lookupValue(name: ValueRef): Either[LookupError, GenDValue[_]] =
def lookupValue(name: ValueRef): Either[LookupError, DValueSignature] =
lookupDefinition(name).flatMap {
case valueDef: GenDValue[_] => Right(valueDef)
case valueDef: DValueSignature => Right(valueDef)
case _ => Left(LookupError.Value(name))
}

def lookupException(name: TypeConName): Either[LookupError, GenDefException[_]] =
def lookupException(name: TypeConName): Either[LookupError, DefExceptionSignature] =
lookupModule(name.packageId, name.qualifiedName.module).flatMap(
_.exceptions.get(name.qualifiedName.name).toRight(LookupError.Exception(name))
)
Expand All @@ -135,7 +135,10 @@ private[lf] case class Interface(signatures: PartialFunction[PackageId, GenPacka

object Interface {

val Empty = Interface(PartialFunction.empty)
val Empty = new Interface(PartialFunction.empty)

def apply(packages: Map[PackageId, Package]): Interface =
new Interface(Util.toSignatures(packages))

case class DataRecordInfo(
dataType: DDataType,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ object Converter {
}

private def fromAnyChoice(
lookupChoice: (Identifier, Name) => Either[String, GenTemplateChoice[_]],
lookupChoice: (Identifier, Name) => Either[String, TemplateChoiceSignature],
translator: preprocessing.ValueTranslator,
templateId: Identifier,
choiceName: ChoiceName,
Expand Down Expand Up @@ -361,7 +361,7 @@ object Converter {
}

def translateExerciseResult(
lookupChoice: (Identifier, Name) => Either[String, GenTemplateChoice[_]],
lookupChoice: (Identifier, Name) => Either[String, TemplateChoiceSignature],
translator: preprocessing.ValueTranslator,
result: ScriptLedgerClient.ExerciseResult,
) = {
Expand All @@ -376,7 +376,7 @@ object Converter {
}

def translateTransactionTree(
lookupChoice: (Identifier, Name) => Either[String, GenTemplateChoice[_]],
lookupChoice: (Identifier, Name) => Either[String, TemplateChoiceSignature],
translator: preprocessing.ValueTranslator,
scriptIds: ScriptIds,
tree: ScriptLedgerClient.TransactionTree,
Expand Down Expand Up @@ -424,7 +424,7 @@ object Converter {
// fill in the values for the continuation.
def fillCommandResults(
compiledPackages: CompiledPackages,
lookupChoice: (Identifier, Name) => Either[String, GenTemplateChoice[_]],
lookupChoice: (Identifier, Name) => Either[String, TemplateChoiceSignature],
translator: preprocessing.ValueTranslator,
initialFreeAp: SValue,
allEventResults: Seq[ScriptLedgerClient.CommandResult],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import com.daml.lf.data.Ref.{Identifier, Name, PackageId, Party}
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.language.Ast.{GenTemplateChoice, Type}
import com.daml.lf.speedy.SError.DamlEUserError
import com.daml.lf.speedy.SExpr.{SEApp, SEValue}
import com.daml.lf.speedy.{SExpr, SValue}
Expand Down Expand Up @@ -78,16 +77,16 @@ object ScriptF {
_clients =
_clients.copy(party_participants = _clients.party_participants + (party -> participant))
}
def lookupChoice(id: Identifier, choice: Name): Either[String, GenTemplateChoice[_]] =
def lookupChoice(id: Identifier, choice: Name): Either[String, Ast.TemplateChoiceSignature] =
compiledPackages.interface.lookupChoice(id, choice).left.map(_.pretty)

def lookupKeyTy(id: Identifier): Either[String, Type] =
def lookupKeyTy(id: Identifier): Either[String, Ast.Type] =
compiledPackages.interface.lookupTemplateKey(id) match {
case Right(key) => Right(key.typ)
case Left(err) => Left(err.pretty)
}

def translateValue(ty: Type, value: Value[ContractId]): Either[String, SValue] =
def translateValue(ty: Ast.Type, value: Value[ContractId]): Either[String, SValue] =
valueTranslator.translateValue(ty, value).left.map(_.toString)

}
Expand Down

0 comments on commit 7b6818e

Please sign in to comment.