From 796995fe7968a4d00d1c5a8085ce6425696935e4 Mon Sep 17 00:00:00 2001 From: Bobby Lat Date: Wed, 6 Nov 2024 10:53:50 +0800 Subject: [PATCH] fix: avoid updating op-metadata file directly as it is generated by a script --- scripts/build-op-module.ts | 31 ++++++++++++++++++++------ scripts/generate-op-types.ts | 4 ++-- src/awst_build/eb/op-module-builder.ts | 4 +++- src/awst_build/op-metadata.ts | 4 +--- 4 files changed, 30 insertions(+), 13 deletions(-) diff --git a/scripts/build-op-module.ts b/scripts/build-op-module.ts index 0122f5aa..27d6565d 100644 --- a/scripts/build-op-module.ts +++ b/scripts/build-op-module.ts @@ -162,6 +162,23 @@ const TYPE_MAP: Record = { any: AlgoTsType.Uint64 | AlgoTsType.Bytes, } +const INPUT_TYPE_MAP: Record = { + application: AlgoTsType.Application | AlgoTsType.Uint64, + asset: AlgoTsType.Asset | AlgoTsType.Uint64, +} + +const INPUT_ALGOTS_TYPE_MAP: Record = { + [AlgoTsType.Asset]: AlgoTsType.Asset | AlgoTsType.Uint64, + [AlgoTsType.Application]: AlgoTsType.Application | AlgoTsType.Uint64, + [AlgoTsType.Bytes]: AlgoTsType.Bytes, + [AlgoTsType.Uint64]: AlgoTsType.Uint64, + [AlgoTsType.Boolean]: AlgoTsType.Boolean, + [AlgoTsType.Account]: AlgoTsType.Account, + [AlgoTsType.Void]: AlgoTsType.Void, + [AlgoTsType.BigUint]: AlgoTsType.BigUint, + [AlgoTsType.Enum]: AlgoTsType.Enum +} + const ARG_ENUMS = Object.entries(langSpec.arg_enums).map(([name, values], index): EnumDef => { const enumValues = values.map( (v): EnumValue => ({ @@ -398,13 +415,13 @@ export function buildOpModule() { }, docs: member.doc, name: getEnumOpName(member.name, opNameConfig), - immediateArgs: def.immediate_args.map((i) => ({ name: camelCase(i.name), type: getMappedType(i.immediate_type, i.arg_enum) })), + immediateArgs: def.immediate_args.map((i) => ({ name: camelCase(i.name), type: getMappedType(i.immediate_type, i.arg_enum, true) })), stackArgs: def.stack_inputs.map((sa, i) => { if (i === enumArg.modifies_stack_input) { invariant(member.stackType, 'Member must have stack type') - return { name: camelCase(sa.name), type: member.stackType } + return { name: camelCase(sa.name), type: INPUT_ALGOTS_TYPE_MAP[member.stackType] ?? member.stackType } } - return { name: camelCase(sa.name), type: getMappedType(sa.stack_type, null) } + return { name: camelCase(sa.name), type: getMappedType(sa.stack_type, null, true) } }), returnTypes: def.stack_outputs.map((o, i) => { if (i === enumArg.modifies_stack_output) { @@ -421,8 +438,8 @@ export function buildOpModule() { type: 'op-function', opCode: opCode, name: getOpName(def.name, opNameConfig), - immediateArgs: def.immediate_args.map((i) => ({ name: camelCase(i.name), type: getMappedType(i.immediate_type, i.arg_enum) })), - stackArgs: def.stack_inputs.map((i) => ({ name: camelCase(i.name), type: getMappedType(i.stack_type, null) })), + immediateArgs: def.immediate_args.map((i) => ({ name: camelCase(i.name), type: getMappedType(i.immediate_type, i.arg_enum, true) })), + stackArgs: def.stack_inputs.map((i) => ({ name: camelCase(i.name), type: getMappedType(i.stack_type, null, true) })), returnTypes: def.stack_outputs.map((o) => getMappedType(o.stack_type, null)), docs: getOpDocs(def), }), @@ -542,7 +559,7 @@ function getEnumOpName(enumMember: string, config: OpNameConfig): string { return camelCase([config.prefix, enumMember].filter(Boolean).join('_')) } -function getMappedType(t: string | null, enumName: string | null): AlgoTsType { +function getMappedType(t: string | null, enumName: string | null, isInput: boolean = false): AlgoTsType { invariant(t !== 'arg_enum' || enumName !== undefined, 'Must provide enumName for arg_enum types') if (t === null || t === undefined) { throw new Error('Missing type') @@ -552,7 +569,7 @@ function getMappedType(t: string | null, enumName: string | null): AlgoTsType { invariant(enumDef, `Definition must exist for ${enumName}`) return enumDef.typeFlag } - const mappedType = TYPE_MAP[t ?? ''] + const mappedType = isInput ? INPUT_TYPE_MAP[t ?? ''] ?? TYPE_MAP[t ?? ''] : TYPE_MAP[t ?? ''] invariant(mappedType, `Mapped type must exist for ${t}`) return mappedType } diff --git a/scripts/generate-op-types.ts b/scripts/generate-op-types.ts index fd964972..38ad64bb 100644 --- a/scripts/generate-op-types.ts +++ b/scripts/generate-op-types.ts @@ -76,9 +76,9 @@ import { Account, Application, Asset } from './reference' } } function* emitArgType(argType: AlgoTsType) { - if (hasFlags(argType, AlgoTsType.Application)) yield 'Application | uint64' + if (hasFlags(argType, AlgoTsType.Application)) yield 'Application' if (hasFlags(argType, AlgoTsType.Account)) yield 'Account' - if (hasFlags(argType, AlgoTsType.Asset)) yield 'Asset | uint64' + if (hasFlags(argType, AlgoTsType.Asset)) yield 'Asset' if (hasFlags(argType, AlgoTsType.Uint64)) yield 'uint64' if (hasFlags(argType, AlgoTsType.Bytes)) yield 'bytes' if (hasFlags(argType, AlgoTsType.Boolean)) yield 'boolean' diff --git a/src/awst_build/eb/op-module-builder.ts b/src/awst_build/eb/op-module-builder.ts index 7216e6a6..a6a5935f 100644 --- a/src/awst_build/eb/op-module-builder.ts +++ b/src/awst_build/eb/op-module-builder.ts @@ -4,13 +4,15 @@ import type { SourceLocation } from '../../awst/source-location' import { CodeError, InternalError } from '../../errors' import { enumerate, invariant } from '../../util' import type { IntrinsicOpGrouping, IntrinsicOpMapping } from '../op-metadata' -import { OP_METADATA, VOID_OPS } from '../op-metadata' +import { OP_METADATA } from '../op-metadata' import type { PType } from '../ptypes' import { IntrinsicEnumType, IntrinsicFunctionGroupType, IntrinsicFunctionType, stringPType } from '../ptypes' import { typeRegistry } from '../type-registry' import { FunctionBuilder, InstanceExpressionBuilder, NodeBuilder } from './index' import { requestConstantOfType, requestExpressionOfType } from './util' +const VOID_OPS = ['itxn_begin', 'itxn_next', 'itxn_submit'] + export class IntrinsicOpGroupBuilder extends NodeBuilder { private opGrouping: IntrinsicOpGrouping public readonly ptype: IntrinsicFunctionGroupType diff --git a/src/awst_build/op-metadata.ts b/src/awst_build/op-metadata.ts index ffd626ab..3d76c558 100644 --- a/src/awst_build/op-metadata.ts +++ b/src/awst_build/op-metadata.ts @@ -1,6 +1,6 @@ /* THIS FILE IS GENERATED BY ~/scripts/generate-op-metadata.ts - DO NOT MODIFY DIRECTLY */ -import type { Expression } from '../awst/nodes' import * as ptypes from './ptypes' +import type { Expression } from '../awst/nodes' export type ImmediateArgMapping = { name: string @@ -4774,5 +4774,3 @@ export const OP_METADATA: Record