interfaces: add an experimental toTypeRep builtin. (#11378)

This adds an experimental `toTypeRep: forall t. t -> TypeRep` builtin.
It will only work on interface payloads and crash horribly otherwise.

CHANGELOG_BEGIN
CHANGELOG_END
This commit is contained in:
Robin Krom 2021-10-25 16:09:23 +02:00 committed by GitHub
parent 5654d5cb48
commit f89ecc6b95
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 0 deletions

View File

@ -5,6 +5,7 @@ module DA.Experimental.Interface(
interfaceCreate,
interfaceSignatory,
interfaceObserver,
toTypeRep,
) where
import GHC.Types (primitive)
@ -17,3 +18,6 @@ interfaceSignatory payload = primitive @"$RESOLVE_VIRTUAL_SIGNATORY" payload pay
interfaceObserver: t -> [Party]
interfaceObserver payload = primitive @"$RESOLVE_VIRTUAL_OBSERVER" payload payload
toTypeRep: t -> TypeRep
toTypeRep arg = primitive @"$TO_TYPE_REP" arg

View File

@ -8,6 +8,7 @@
module InterfaceExperimental where
import DA.Assert ((===))
import DA.Action (unless)
import DA.Experimental.Interface
import GHC.Types (primitive)
@ -62,13 +63,22 @@ template Asset
do
pure ()
template NotAsset
with
p : Party
where
signatory p
main = scenario do
alice <- getParty "Alice"
bob <- getParty "Bob"
let asset = Asset alice bob 15
let notAsset = NotAsset alice
let token = toToken asset
submit alice do
interfaceCreate token
interfaceSignatory token === [alice]
interfaceObserver token === [bob, alice]
unless (toTypeRep token == toTypeRep asset) $ abort "TypeReps are not equal"
unless (toTypeRep token /= toTypeRep notAsset) $ abort "TypeReps are equal"
pure ()

View File

@ -1667,9 +1667,17 @@ private[lf] object SBuiltin {
machine.returnValue = SInt64(42L)
}
private object SBExperimentalToTypeRep extends SBuiltinPure(1) {
override private[speedy] def executePure(args: util.ArrayList[SValue]): STypeRep = {
val id = getSRecord(args, 0).id
STypeRep(Ast.TTyCon(id))
}
}
private val mapping: Map[String, SExpr] =
List(
"ANSWER" -> SBExperimentalAnswer,
"TO_TYPE_REP" -> SBExperimentalToTypeRep,
"RESOLVE_VIRTUAL_CREATE" -> new SBResolveVirtual(CreateDefRef),
"RESOLVE_VIRTUAL_SIGNATORY" -> new SBResolveVirtual(SignatoriesDefRef),
"RESOLVE_VIRTUAL_OBSERVER" -> new SBResolveVirtual(ObserversDefRef),