Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion tooling/noir_codegen/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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'",
Expand Down
52 changes: 18 additions & 34 deletions tooling/noir_codegen/src/utils/abi_type_with_generics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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;
}
6 changes: 4 additions & 2 deletions tooling/noir_codegen/src/utils/typings_generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand Down
4 changes: 2 additions & 2 deletions tooling/noir_codegen/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down
Loading