Skip to content

Commit

Permalink
fix aliased list (PValue) utility definition
Browse files Browse the repository at this point in the history
  • Loading branch information
michele-nuzzi committed Aug 8, 2024
1 parent 996307e commit 0a7748f
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 19 deletions.
25 changes: 25 additions & 0 deletions packages/onchain/src/pluts/lib/__tests__/Syntax.plet.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { DataI } from "@harmoniclabs/plutus-data";
import { compile, int, passert, pData, perror, pfn, plet, pmatch, PScriptContext, PTxOut, punIData, PValue, Term, termTypeToString, unit } from "../.."

test("plet keeps utility", () => {

const contract = pfn([
PTxOut.type
], unit)
((input) => {

const ownValue = plet( input.value );

expect( ownValue.lovelaces instanceof Term ).toBe( true );
expect( typeof ownValue.amountOf === "function" ).toBe( true );

const isValueLocked = (
ownValue.lovelaces.lt( 100 )
);

return passert.$( isValueLocked );
})

const compiled = compile( contract );

})
1 change: 1 addition & 0 deletions packages/onchain/src/pluts/lib/plet/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { UtilityTermOf, addUtilityForType } from "../std/UtilityTerms/addUtility
import { makeMockUtilityTerm } from "../std/UtilityTerms/mockUtilityTerms/makeMockUtilityTerm";
import { getCallStackAt } from "../../../utils/getCallStackAt";
import { IRTerm } from "../../../IR/IRTerm";
import { termTypeToString } from "../../type_system";

export type LettedTerm<PVarT extends PType, SomeExtension extends object> =
Term<PVarT> & SomeExtension extends Term<PAlias<PVarT, {}>> ?
Expand Down
3 changes: 3 additions & 0 deletions packages/onchain/src/pluts/lib/std/UtilityTerms/TermList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,9 @@ export function addPListMethods<PElemsT extends PType>( _lst: Term<PList<PElemsT
{
const elemsT = getElemsT( _lst.type );

// this clone is causing problems for aliases
// TODO: remove clone and test
/** @todo remove clone and test */
const lst = new Term(
list( elemsT ),
// needs to be wrapped to prevent the garbage collector to collect garbage (lst)
Expand Down
16 changes: 2 additions & 14 deletions packages/onchain/src/pluts/lib/std/UtilityTerms/TermStruct.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Term } from "../../../Term";
import type { PStruct, StructInstance } from "../../../PTypes/PStruct/pstruct";
import type { TermFn } from "../../../PTypes/PFn/PFn";
import { StructDefinition, isStructType, isStructDefinition, data, list, int, pair, Methods } from "../../../type_system";
import { StructDefinition, isStructType, isStructDefinition, data, list, int, pair, Methods, termTypeToString } from "../../../type_system";
import { peqData, } from "../../builtins/data";
import { PBool } from "../../../PTypes/PBool";
import { TermBool } from "./TermBool";
Expand Down Expand Up @@ -39,7 +39,7 @@ export type TermStruct<SDef extends StructDefinition, SMethods extends Methods>
readonly eq: ( other: Term<PStruct<SDef, {}>> | Term<PData> ) => TermBool

readonly raw: RawStruct

} &
(
IsSingleKey<SDef> extends true ?
Expand Down Expand Up @@ -118,17 +118,6 @@ export function addPStructMethods<
getElemAtTerm( i ).$( letted_fieldsListData )
),
ctorName + "::" + thisFieldName
// (dbn, ir) => {
// if(ctorName + "::" + thisFieldName !== "PScriptContext::purpose") return;
//
// const [ _dbn, term ] = getNormalizedLettedArgs( ir.dbn, ir.value ) ?? [ 0, new IRVar( 0 )] ;
// console.log(
// "PScriptContext::purpose at dbn:", dbn,
// "\nnormalized value:", prettyIRJsonStr( term, 2, { hoisted: false } ),
// "\nnormalized value hash:", toHex( term.hash ),
// "\nnormalized dbn:", _dbn,
// );
// }
),
thisFieldType
),
Expand All @@ -137,7 +126,6 @@ export function addPStructMethods<
configurable: false
}
);

}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { defineNonDeletableNormalProperty } from "@harmoniclabs/obj-utils";
import { termTypeToString } from "../../../type_system/utils";
import { addUserMethods } from "./userMethods/addUserMethods";
import { addBaseUtilityTerm, BaseUtilityTermExtension } from "./BaseUtilityTerm";
import { punsafeConvertType } from "../../punsafeConvertType";

// given the index returns the previous number ( PrevNum[2] -> 1; etc... )
type PrevNum = [ never, 0, 1, 2, 3, 4, 5, 6 ];
Expand Down Expand Up @@ -103,9 +104,10 @@ export function addPAliasMethods<
aliasTerm: Term<PAlias<PAliased,AMethods>>
): TermAlias<PAliased, AMethods>
{
aliasTerm = addBaseUtilityTerm( aliasTerm );
const originalType = aliasTerm.type;

aliasTerm = addBaseUtilityTerm( aliasTerm );

if( originalType[0] !== PrimType.Alias )
{
console.error( originalType );
Expand All @@ -119,9 +121,14 @@ export function addPAliasMethods<

const aliasedType = unwrapAlias( originalType );

aliasTerm = addUtilityForType( aliasedType )( aliasTerm ) as any;

aliasTerm = addUserMethods( aliasTerm, originalType[2] as AMethods );
// intentionally discarding the result
// if we are aliasing a list, adding utility clones the term
// so changes the type
// so we loose the alias and possible methods
// this is really a bug in `addPListMethods`
// but for now this is the workaround
void addUtilityForType( aliasedType )( aliasTerm ) as any;
void addUserMethods( aliasTerm, originalType[2] as AMethods );

return aliasTerm as any;
}
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,14 @@ export const fromData_minimal = _fromData;

export function _fromData<T extends TermType>( t: T ): ( term: Term<PData> ) => Term<ToPType<T>>
{
if( isTaggedAsAlias( t ) ) return _fromData( unwrapAlias( t as any ) ) as any;
if( isTaggedAsAlias( t ) ) return (( term: any ) => {
term = _fromData( unwrapAlias( t as any ) )( term );
return new Term(
t,
term.toIR,
Boolean(term.isConstant)
) as any;
}) as any;

// unwrap asData before `t extends data`
if( t[0] === PrimType.AsData ) t = t[1] as T;
Expand Down

0 comments on commit 0a7748f

Please sign in to comment.