Skip to content

Commit

Permalink
fix: avoid updating op-metadata file directly as it is generated by a…
Browse files Browse the repository at this point in the history
… script
  • Loading branch information
boblat committed Nov 6, 2024
1 parent 8d69152 commit 796995f
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 13 deletions.
31 changes: 24 additions & 7 deletions scripts/build-op-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,23 @@ const TYPE_MAP: Record<string, AlgoTsType> = {
any: AlgoTsType.Uint64 | AlgoTsType.Bytes,
}

const INPUT_TYPE_MAP: Record<string, AlgoTsType> = {
application: AlgoTsType.Application | AlgoTsType.Uint64,
asset: AlgoTsType.Asset | AlgoTsType.Uint64,
}

const INPUT_ALGOTS_TYPE_MAP: Record<AlgoTsType, AlgoTsType> = {
[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 => ({
Expand Down Expand Up @@ -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) {
Expand All @@ -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),
}),
Expand Down Expand Up @@ -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')
Expand All @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions scripts/generate-op-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
4 changes: 3 additions & 1 deletion src/awst_build/eb/op-module-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions src/awst_build/op-metadata.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -4774,5 +4774,3 @@ export const OP_METADATA: Record<string, IntrinsicOpMapping | IntrinsicOpGroupin
],
},
}

export const VOID_OPS = ['itxn_begin', 'itxn_next', 'itxn_submit']

0 comments on commit 796995f

Please sign in to comment.