Skip to content

Commit

Permalink
refactor(store): make static array length a constant (#2410)
Browse files Browse the repository at this point in the history
yonadaa authored Mar 14, 2024
1 parent 7052103 commit 144c0d8
Showing 6 changed files with 40 additions and 153 deletions.
5 changes: 5 additions & 0 deletions .changeset/sixty-queens-brake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@latticexyz/store": minor
---

Replaced the static array length getters in table libraries with constants.
30 changes: 4 additions & 26 deletions e2e/packages/contracts/src/codegen/tables/StaticArray.sol

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

75 changes: 10 additions & 65 deletions packages/cli/contracts/src/codegen/tables/Dynamics1.sol

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 6 additions & 39 deletions packages/cli/contracts/src/codegen/tables/Singleton.sol

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 8 additions & 8 deletions packages/cli/contracts/test/Tablegen.t.sol
Original file line number Diff line number Diff line change
@@ -67,11 +67,11 @@ contract TablegenTest is Test, StoreMock {
assertEq(abi.encode(Dynamics2.get(key)), abi.encode(data2));

// test length getters
assertEq(Dynamics1.lengthStaticB32(key), staticB32.length);
assertEq(Dynamics1.lengthStaticI32(key), staticI32.length);
assertEq(Dynamics1.lengthStaticU128(key), staticU128.length);
assertEq(Dynamics1.lengthStaticAddrs(key), staticAddrs.length);
assertEq(Dynamics1.lengthStaticBools(key), staticBools.length);
assertEq(Dynamics1.lengthStaticB32, staticB32.length);
assertEq(Dynamics1.lengthStaticI32, staticI32.length);
assertEq(Dynamics1.lengthStaticU128, staticU128.length);
assertEq(Dynamics1.lengthStaticAddrs, staticAddrs.length);
assertEq(Dynamics1.lengthStaticBools, staticBools.length);
assertEq(Dynamics2.lengthU64(key), u64.length);
assertEq(Dynamics2.lengthStr(key), bytes(str).length);
assertEq(Dynamics2.lengthB(key), b.length);
@@ -131,17 +131,17 @@ contract TablegenTest is Test, StoreMock {
assertEq(Singleton.getV1(), -10);

assertEq(abi.encode(Singleton.getV2()), abi.encode([uint32(1), 2]));
assertEq(Singleton.lengthV2(), 2);
assertEq(Singleton.lengthV2, 2);
assertEq(Singleton.getItemV2(0), 1);
assertEq(Singleton.getItemV2(1), 2);

assertEq(abi.encode(Singleton.getV3()), abi.encode([uint32(3), 4]));
assertEq(Singleton.lengthV3(), 2);
assertEq(Singleton.lengthV3, 2);
assertEq(Singleton.getItemV3(0), 3);
assertEq(Singleton.getItemV3(1), 4);

assertEq(abi.encode(Singleton.getV4()), abi.encode([uint32(5)]));
assertEq(Singleton.lengthV4(), 1);
assertEq(Singleton.lengthV4, 1);
assertEq(Singleton.getItemV4(0), 5);
vm.expectRevert(abi.encodeWithSelector(IStoreErrors.Store_IndexOutOfBounds.selector, 4, 4));
assertEq(Singleton.getItemV4(1), 0);
22 changes: 7 additions & 15 deletions packages/store/ts/codegen/field.ts
Original file line number Diff line number Diff line change
@@ -89,22 +89,14 @@ export function renderFieldMethods(options: RenderTableOptions): string {

if (options.withGetters) {
if (typeWrappingData && typeWrappingData.kind === "staticArray") {
result += renderWithFieldSuffix(options.withSuffixlessFieldMethods, field.name, (_methodNameSuffix) =>
renderWithStore(
storeArgument,
({ _typedStore, _commentSuffix, _methodNamePrefix }) => `
/**
* @notice Get the length of ${field.name}${_commentSuffix}.
*/
function ${_methodNamePrefix}length${_methodNameSuffix}(${renderArguments([
_typedStore,
_typedTableId,
_typedKeyArgs,
])}) internal pure returns (uint256) {
return ${typeWrappingData.staticLength};
}
result += renderWithFieldSuffix(
options.withSuffixlessFieldMethods,
field.name,
(_methodNameSuffix) =>
`
// The length of ${field.name}
uint256 constant length${_methodNameSuffix} = ${typeWrappingData.staticLength};
`,
),
);
} else {
result += renderWithFieldSuffix(options.withSuffixlessFieldMethods, field.name, (_methodNameSuffix) =>

0 comments on commit 144c0d8

Please sign in to comment.