diff --git a/tooling/noir_codegen/package.json b/tooling/noir_codegen/package.json index a16b35ca085..86c9ee556a2 100644 --- a/tooling/noir_codegen/package.json +++ b/tooling/noir_codegen/package.json @@ -35,7 +35,7 @@ "dev": "tsc-multi --watch", "build": "tsc", "test": "yarn test:codegen && yarn test:node && yarn test:clean", - "test:codegen": "${NARGO:-nargo} export --program-dir=./test/test_lib && tsx src/main.ts ./test/test_lib/export/** --out-dir ./test/codegen", + "test:codegen": "rm -rf ./test/test_lib/export && ${NARGO:-nargo} export --program-dir=./test/test_lib && tsx src/main.ts ./test/test_lib/export/** --out-dir ./test/codegen", "test:node": "mocha --timeout 25000 --exit --config ./.mocharc.json", "test:clean": "rm -rf ./test/codegen ./test/test_lib/export", "prettier": "prettier 'src/**/*.ts'", diff --git a/tooling/noir_codegen/src/utils/abi_type_with_generics.ts b/tooling/noir_codegen/src/utils/abi_type_with_generics.ts index 6e8b5a17a8e..123e1b2774b 100644 --- a/tooling/noir_codegen/src/utils/abi_type_with_generics.ts +++ b/tooling/noir_codegen/src/utils/abi_type_with_generics.ts @@ -60,63 +60,44 @@ export type AbiTypeWithGenerics = * Maps an ABI type to an ABI type with generics. * This performs pure type conversion, and does not generate any bindings. */ -export function mapAbiTypeToAbiTypeWithGenerics( - abiType: AbiType, - allTypes: AbiTypeWithGenerics[], -): AbiTypeWithGenerics { - let returnedType: AbiTypeWithGenerics; +export function mapAbiTypeToAbiTypeWithGenerics(abiType: AbiType): AbiTypeWithGenerics { switch (abiType.kind) { case 'field': case 'boolean': case 'string': case 'integer': - returnedType = abiType; - break; - case 'array': { - const type = mapAbiTypeToAbiTypeWithGenerics(abiType.type, allTypes); - returnedType = { + return abiType; + case 'array': + return { kind: 'array', length: abiType.length, - type, + type: mapAbiTypeToAbiTypeWithGenerics(abiType.type), }; - break; - } case 'struct': { const structType = { path: abiType.path, - fields: abiType.fields.map(function (field) { - const type = mapAbiTypeToAbiTypeWithGenerics(field.type, allTypes); - return { - name: field.name, - type, - }; - }), + fields: abiType.fields.map((field) => ({ + name: field.name, + type: mapAbiTypeToAbiTypeWithGenerics(field.type), + })), generics: [], }; - returnedType = { + return { kind: 'struct', structType, args: [], }; - break; } case 'tuple': - returnedType = { + return { kind: 'tuple', - fields: abiType.fields.map(function (field) { - const type = mapAbiTypeToAbiTypeWithGenerics(field, allTypes); - allTypes.push(type); - return type; - }), + fields: abiType.fields.map(mapAbiTypeToAbiTypeWithGenerics), }; - break; default: { const exhaustiveCheck: never = abiType; throw new Error(`Unhandled abi type: ${exhaustiveCheck}`); } } - allTypes.push(returnedType); - return returnedType; } /** @@ -150,9 +131,12 @@ export function findAllStructsInType(abiType: AbiTypeWithGenerics): Struct[] { let lastStructs = findStructsInType(abiType); while (lastStructs.length > 0) { allStructs = allStructs.concat(lastStructs); - lastStructs = lastStructs.flatMap((struct) => - struct.structType.fields.flatMap((field) => findStructsInType(field.type)), - ); + lastStructs = lastStructs.flatMap(function (struct) { + const fieldStructTypes = struct.structType.fields.flatMap((field) => findStructsInType(field.type)); + const argsStructTypes = struct.args.flatMap(findAllStructsInType); + const structTypes = fieldStructTypes.concat(argsStructTypes); + return structTypes; + }); } return allStructs; } diff --git a/tooling/noir_codegen/src/utils/typings_generator.ts b/tooling/noir_codegen/src/utils/typings_generator.ts index 886ae8b7c55..ccdaf1dc0a0 100644 --- a/tooling/noir_codegen/src/utils/typings_generator.ts +++ b/tooling/noir_codegen/src/utils/typings_generator.ts @@ -99,11 +99,13 @@ export class TypingsGenerator { // Map all the types used in the ABIs to the demonomorphized types for (const { abi, circuitName, artifact } of circuits) { const params = abi.parameters.map((param) => { - const type = mapAbiTypeToAbiTypeWithGenerics(param.type, this.allTypes); + const type = mapAbiTypeToAbiTypeWithGenerics(param.type); + this.allTypes.push(type); return { name: param.name, type }; }); if (abi.return_type) { - const returnType = mapAbiTypeToAbiTypeWithGenerics(abi.return_type.abi_type, this.allTypes); + const returnType = mapAbiTypeToAbiTypeWithGenerics(abi.return_type.abi_type); + this.allTypes.push(returnType); this.demonomorphizedAbis.push({ circuitName, params, returnType, artifact }); } else { this.demonomorphizedAbis.push({ circuitName, params, artifact }); diff --git a/tooling/noir_codegen/test/index.test.ts b/tooling/noir_codegen/test/index.test.ts index 2442dd3a761..abf63a36ae8 100644 --- a/tooling/noir_codegen/test/index.test.ts +++ b/tooling/noir_codegen/test/index.test.ts @@ -6,11 +6,11 @@ import { exported_function_baz, exported_function_parent, MyStruct, + Parent, + Child, u64, u32, ForeignCallHandler, - Parent, - Child, } from './codegen/index.js'; it('codegens a callable function', async () => {