Skip to content
Merged
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
43 changes: 12 additions & 31 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
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
18 changes: 0 additions & 18 deletions tooling/noir_codegen/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,10 @@ import { expect } from 'chai';
import {
exported_function_foo,
exported_function_baz,
exported_function_parent,
MyStruct,
u64,
u32,
ForeignCallHandler,
Parent,
Child,
} from './codegen/index.js';

it('codegens a callable function', async () => {
Expand Down Expand Up @@ -119,18 +116,3 @@ it('codegens a callable argless function', async () => {
const val: u64 = await exported_function_baz();
expect(val).to.be.eq('0x01');
});

it('codegens types for generic arguments', async () => {
const a: Parent<Child> = {
inner: {
value: '1',
},
counter: '2',
};
const b: Parent<u32> = {
inner: '3',
counter: '4',
};
const val: u32 = await exported_function_parent(a, b);
expect(val).to.be.eq('0x0a'); // 1 + 2 + 3 + 4 = 10
});
14 changes: 0 additions & 14 deletions tooling/noir_codegen/test/test_lib/src/lib.nr
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,3 @@ fn exported_function_bar(my_struct: NestedStruct<1, 2, 3, u64>) -> (u64) {
fn exported_function_baz() -> u64 {
1
}

pub struct Parent<T> {
inner: T,
counter: u32,
}

pub struct Child {
value: u32,
}

#[export]
fn exported_function_parent(a: Parent<Child>, b: Parent<u32>) -> u32 {
a.counter + a.inner.value + b.counter + b.inner
}
Loading