diff --git a/.changeset/honest-geckos-refuse.md b/.changeset/honest-geckos-refuse.md new file mode 100644 index 0000000000..b8b8932dd1 --- /dev/null +++ b/.changeset/honest-geckos-refuse.md @@ -0,0 +1,5 @@ +--- +"@latticexyz/store": patch +--- + +Updated codegen to not render `push` and `pop` methods for static arrays. The `length` method now returns the hardcoded known length instead of calculating it like with a dynamic array. diff --git a/e2e/packages/contracts/src/codegen/tables/StaticArray.sol b/e2e/packages/contracts/src/codegen/tables/StaticArray.sol index 8a2b55e49e..c1a9d358f5 100644 --- a/e2e/packages/contracts/src/codegen/tables/StaticArray.sol +++ b/e2e/packages/contracts/src/codegen/tables/StaticArray.sol @@ -169,49 +169,29 @@ library StaticArray { /** * @notice Get the length of value. */ - function lengthValue() internal view returns (uint256) { - bytes32[] memory _keyTuple = new bytes32[](0); - - uint256 _byteLength = StoreSwitch.getDynamicFieldLength(_tableId, _keyTuple, 0); - unchecked { - return _byteLength / 32; - } + function lengthValue() internal pure returns (uint256) { + return 2; } /** * @notice Get the length of value. */ - function _lengthValue() internal view returns (uint256) { - bytes32[] memory _keyTuple = new bytes32[](0); - - uint256 _byteLength = StoreCore.getDynamicFieldLength(_tableId, _keyTuple, 0); - unchecked { - return _byteLength / 32; - } + function _lengthValue() internal pure returns (uint256) { + return 2; } /** * @notice Get the length of value. */ - function length() internal view returns (uint256) { - bytes32[] memory _keyTuple = new bytes32[](0); - - uint256 _byteLength = StoreSwitch.getDynamicFieldLength(_tableId, _keyTuple, 0); - unchecked { - return _byteLength / 32; - } + function length() internal pure returns (uint256) { + return 2; } /** * @notice Get the length of value. */ - function _length() internal view returns (uint256) { - bytes32[] memory _keyTuple = new bytes32[](0); - - uint256 _byteLength = StoreCore.getDynamicFieldLength(_tableId, _keyTuple, 0); - unchecked { - return _byteLength / 32; - } + function _length() internal pure returns (uint256) { + return 2; } /** @@ -266,78 +246,6 @@ library StaticArray { } } - /** - * @notice Push an element to value. - */ - function pushValue(uint256 _element) internal { - bytes32[] memory _keyTuple = new bytes32[](0); - - StoreSwitch.pushToDynamicField(_tableId, _keyTuple, 0, abi.encodePacked((_element))); - } - - /** - * @notice Push an element to value. - */ - function _pushValue(uint256 _element) internal { - bytes32[] memory _keyTuple = new bytes32[](0); - - StoreCore.pushToDynamicField(_tableId, _keyTuple, 0, abi.encodePacked((_element))); - } - - /** - * @notice Push an element to value. - */ - function push(uint256 _element) internal { - bytes32[] memory _keyTuple = new bytes32[](0); - - StoreSwitch.pushToDynamicField(_tableId, _keyTuple, 0, abi.encodePacked((_element))); - } - - /** - * @notice Push an element to value. - */ - function _push(uint256 _element) internal { - bytes32[] memory _keyTuple = new bytes32[](0); - - StoreCore.pushToDynamicField(_tableId, _keyTuple, 0, abi.encodePacked((_element))); - } - - /** - * @notice Pop an element from value. - */ - function popValue() internal { - bytes32[] memory _keyTuple = new bytes32[](0); - - StoreSwitch.popFromDynamicField(_tableId, _keyTuple, 0, 32); - } - - /** - * @notice Pop an element from value. - */ - function _popValue() internal { - bytes32[] memory _keyTuple = new bytes32[](0); - - StoreCore.popFromDynamicField(_tableId, _keyTuple, 0, 32); - } - - /** - * @notice Pop an element from value. - */ - function pop() internal { - bytes32[] memory _keyTuple = new bytes32[](0); - - StoreSwitch.popFromDynamicField(_tableId, _keyTuple, 0, 32); - } - - /** - * @notice Pop an element from value. - */ - function _pop() internal { - bytes32[] memory _keyTuple = new bytes32[](0); - - StoreCore.popFromDynamicField(_tableId, _keyTuple, 0, 32); - } - /** * @notice Update an element of value at `_index`. */ diff --git a/packages/cli/contracts/src/codegen/tables/Dynamics1.sol b/packages/cli/contracts/src/codegen/tables/Dynamics1.sol index a749ee0b67..308952a97f 100644 --- a/packages/cli/contracts/src/codegen/tables/Dynamics1.sol +++ b/packages/cli/contracts/src/codegen/tables/Dynamics1.sol @@ -153,27 +153,15 @@ library Dynamics1 { /** * @notice Get the length of staticB32. */ - function lengthStaticB32(bytes32 key) internal view returns (uint256) { - bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; - - uint256 _byteLength = StoreSwitch.getDynamicFieldLength(_tableId, _keyTuple, 0); - unchecked { - return _byteLength / 32; - } + function lengthStaticB32(bytes32 key) internal pure returns (uint256) { + return 1; } /** * @notice Get the length of staticB32. */ - function _lengthStaticB32(bytes32 key) internal view returns (uint256) { - bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; - - uint256 _byteLength = StoreCore.getDynamicFieldLength(_tableId, _keyTuple, 0); - unchecked { - return _byteLength / 32; - } + function _lengthStaticB32(bytes32 key) internal pure returns (uint256) { + return 1; } /** @@ -204,46 +192,6 @@ library Dynamics1 { } } - /** - * @notice Push an element to staticB32. - */ - function pushStaticB32(bytes32 key, bytes32 _element) internal { - bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; - - StoreSwitch.pushToDynamicField(_tableId, _keyTuple, 0, abi.encodePacked((_element))); - } - - /** - * @notice Push an element to staticB32. - */ - function _pushStaticB32(bytes32 key, bytes32 _element) internal { - bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; - - StoreCore.pushToDynamicField(_tableId, _keyTuple, 0, abi.encodePacked((_element))); - } - - /** - * @notice Pop an element from staticB32. - */ - function popStaticB32(bytes32 key) internal { - bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; - - StoreSwitch.popFromDynamicField(_tableId, _keyTuple, 0, 32); - } - - /** - * @notice Pop an element from staticB32. - */ - function _popStaticB32(bytes32 key) internal { - bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; - - StoreCore.popFromDynamicField(_tableId, _keyTuple, 0, 32); - } - /** * @notice Update an element of staticB32 at `_index`. */ @@ -315,27 +263,15 @@ library Dynamics1 { /** * @notice Get the length of staticI32. */ - function lengthStaticI32(bytes32 key) internal view returns (uint256) { - bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; - - uint256 _byteLength = StoreSwitch.getDynamicFieldLength(_tableId, _keyTuple, 1); - unchecked { - return _byteLength / 4; - } + function lengthStaticI32(bytes32 key) internal pure returns (uint256) { + return 2; } /** * @notice Get the length of staticI32. */ - function _lengthStaticI32(bytes32 key) internal view returns (uint256) { - bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; - - uint256 _byteLength = StoreCore.getDynamicFieldLength(_tableId, _keyTuple, 1); - unchecked { - return _byteLength / 4; - } + function _lengthStaticI32(bytes32 key) internal pure returns (uint256) { + return 2; } /** @@ -366,46 +302,6 @@ library Dynamics1 { } } - /** - * @notice Push an element to staticI32. - */ - function pushStaticI32(bytes32 key, int32 _element) internal { - bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; - - StoreSwitch.pushToDynamicField(_tableId, _keyTuple, 1, abi.encodePacked((_element))); - } - - /** - * @notice Push an element to staticI32. - */ - function _pushStaticI32(bytes32 key, int32 _element) internal { - bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; - - StoreCore.pushToDynamicField(_tableId, _keyTuple, 1, abi.encodePacked((_element))); - } - - /** - * @notice Pop an element from staticI32. - */ - function popStaticI32(bytes32 key) internal { - bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; - - StoreSwitch.popFromDynamicField(_tableId, _keyTuple, 1, 4); - } - - /** - * @notice Pop an element from staticI32. - */ - function _popStaticI32(bytes32 key) internal { - bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; - - StoreCore.popFromDynamicField(_tableId, _keyTuple, 1, 4); - } - /** * @notice Update an element of staticI32 at `_index`. */ @@ -477,27 +373,15 @@ library Dynamics1 { /** * @notice Get the length of staticU128. */ - function lengthStaticU128(bytes32 key) internal view returns (uint256) { - bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; - - uint256 _byteLength = StoreSwitch.getDynamicFieldLength(_tableId, _keyTuple, 2); - unchecked { - return _byteLength / 16; - } + function lengthStaticU128(bytes32 key) internal pure returns (uint256) { + return 3; } /** * @notice Get the length of staticU128. */ - function _lengthStaticU128(bytes32 key) internal view returns (uint256) { - bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; - - uint256 _byteLength = StoreCore.getDynamicFieldLength(_tableId, _keyTuple, 2); - unchecked { - return _byteLength / 16; - } + function _lengthStaticU128(bytes32 key) internal pure returns (uint256) { + return 3; } /** @@ -528,46 +412,6 @@ library Dynamics1 { } } - /** - * @notice Push an element to staticU128. - */ - function pushStaticU128(bytes32 key, uint128 _element) internal { - bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; - - StoreSwitch.pushToDynamicField(_tableId, _keyTuple, 2, abi.encodePacked((_element))); - } - - /** - * @notice Push an element to staticU128. - */ - function _pushStaticU128(bytes32 key, uint128 _element) internal { - bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; - - StoreCore.pushToDynamicField(_tableId, _keyTuple, 2, abi.encodePacked((_element))); - } - - /** - * @notice Pop an element from staticU128. - */ - function popStaticU128(bytes32 key) internal { - bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; - - StoreSwitch.popFromDynamicField(_tableId, _keyTuple, 2, 16); - } - - /** - * @notice Pop an element from staticU128. - */ - function _popStaticU128(bytes32 key) internal { - bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; - - StoreCore.popFromDynamicField(_tableId, _keyTuple, 2, 16); - } - /** * @notice Update an element of staticU128 at `_index`. */ @@ -639,27 +483,15 @@ library Dynamics1 { /** * @notice Get the length of staticAddrs. */ - function lengthStaticAddrs(bytes32 key) internal view returns (uint256) { - bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; - - uint256 _byteLength = StoreSwitch.getDynamicFieldLength(_tableId, _keyTuple, 3); - unchecked { - return _byteLength / 20; - } + function lengthStaticAddrs(bytes32 key) internal pure returns (uint256) { + return 4; } /** * @notice Get the length of staticAddrs. */ - function _lengthStaticAddrs(bytes32 key) internal view returns (uint256) { - bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; - - uint256 _byteLength = StoreCore.getDynamicFieldLength(_tableId, _keyTuple, 3); - unchecked { - return _byteLength / 20; - } + function _lengthStaticAddrs(bytes32 key) internal pure returns (uint256) { + return 4; } /** @@ -690,46 +522,6 @@ library Dynamics1 { } } - /** - * @notice Push an element to staticAddrs. - */ - function pushStaticAddrs(bytes32 key, address _element) internal { - bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; - - StoreSwitch.pushToDynamicField(_tableId, _keyTuple, 3, abi.encodePacked((_element))); - } - - /** - * @notice Push an element to staticAddrs. - */ - function _pushStaticAddrs(bytes32 key, address _element) internal { - bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; - - StoreCore.pushToDynamicField(_tableId, _keyTuple, 3, abi.encodePacked((_element))); - } - - /** - * @notice Pop an element from staticAddrs. - */ - function popStaticAddrs(bytes32 key) internal { - bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; - - StoreSwitch.popFromDynamicField(_tableId, _keyTuple, 3, 20); - } - - /** - * @notice Pop an element from staticAddrs. - */ - function _popStaticAddrs(bytes32 key) internal { - bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; - - StoreCore.popFromDynamicField(_tableId, _keyTuple, 3, 20); - } - /** * @notice Update an element of staticAddrs at `_index`. */ @@ -801,27 +593,15 @@ library Dynamics1 { /** * @notice Get the length of staticBools. */ - function lengthStaticBools(bytes32 key) internal view returns (uint256) { - bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; - - uint256 _byteLength = StoreSwitch.getDynamicFieldLength(_tableId, _keyTuple, 4); - unchecked { - return _byteLength / 1; - } + function lengthStaticBools(bytes32 key) internal pure returns (uint256) { + return 5; } /** * @notice Get the length of staticBools. */ - function _lengthStaticBools(bytes32 key) internal view returns (uint256) { - bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; - - uint256 _byteLength = StoreCore.getDynamicFieldLength(_tableId, _keyTuple, 4); - unchecked { - return _byteLength / 1; - } + function _lengthStaticBools(bytes32 key) internal pure returns (uint256) { + return 5; } /** @@ -852,46 +632,6 @@ library Dynamics1 { } } - /** - * @notice Push an element to staticBools. - */ - function pushStaticBools(bytes32 key, bool _element) internal { - bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; - - StoreSwitch.pushToDynamicField(_tableId, _keyTuple, 4, abi.encodePacked((_element))); - } - - /** - * @notice Push an element to staticBools. - */ - function _pushStaticBools(bytes32 key, bool _element) internal { - bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; - - StoreCore.pushToDynamicField(_tableId, _keyTuple, 4, abi.encodePacked((_element))); - } - - /** - * @notice Pop an element from staticBools. - */ - function popStaticBools(bytes32 key) internal { - bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; - - StoreSwitch.popFromDynamicField(_tableId, _keyTuple, 4, 1); - } - - /** - * @notice Pop an element from staticBools. - */ - function _popStaticBools(bytes32 key) internal { - bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; - - StoreCore.popFromDynamicField(_tableId, _keyTuple, 4, 1); - } - /** * @notice Update an element of staticBools at `_index`. */ diff --git a/packages/cli/contracts/src/codegen/tables/Singleton.sol b/packages/cli/contracts/src/codegen/tables/Singleton.sol index 430df832f9..7581b13a14 100644 --- a/packages/cli/contracts/src/codegen/tables/Singleton.sol +++ b/packages/cli/contracts/src/codegen/tables/Singleton.sol @@ -175,25 +175,15 @@ library Singleton { /** * @notice Get the length of v2. */ - function lengthV2() internal view returns (uint256) { - bytes32[] memory _keyTuple = new bytes32[](0); - - uint256 _byteLength = StoreSwitch.getDynamicFieldLength(_tableId, _keyTuple, 0); - unchecked { - return _byteLength / 4; - } + function lengthV2() internal pure returns (uint256) { + return 2; } /** * @notice Get the length of v2. */ - function _lengthV2() internal view returns (uint256) { - bytes32[] memory _keyTuple = new bytes32[](0); - - uint256 _byteLength = StoreCore.getDynamicFieldLength(_tableId, _keyTuple, 0); - unchecked { - return _byteLength / 4; - } + function _lengthV2() internal pure returns (uint256) { + return 2; } /** @@ -222,42 +212,6 @@ library Singleton { } } - /** - * @notice Push an element to v2. - */ - function pushV2(uint32 _element) internal { - bytes32[] memory _keyTuple = new bytes32[](0); - - StoreSwitch.pushToDynamicField(_tableId, _keyTuple, 0, abi.encodePacked((_element))); - } - - /** - * @notice Push an element to v2. - */ - function _pushV2(uint32 _element) internal { - bytes32[] memory _keyTuple = new bytes32[](0); - - StoreCore.pushToDynamicField(_tableId, _keyTuple, 0, abi.encodePacked((_element))); - } - - /** - * @notice Pop an element from v2. - */ - function popV2() internal { - bytes32[] memory _keyTuple = new bytes32[](0); - - StoreSwitch.popFromDynamicField(_tableId, _keyTuple, 0, 4); - } - - /** - * @notice Pop an element from v2. - */ - function _popV2() internal { - bytes32[] memory _keyTuple = new bytes32[](0); - - StoreCore.popFromDynamicField(_tableId, _keyTuple, 0, 4); - } - /** * @notice Update an element of v2 at `_index`. */ @@ -323,25 +277,15 @@ library Singleton { /** * @notice Get the length of v3. */ - function lengthV3() internal view returns (uint256) { - bytes32[] memory _keyTuple = new bytes32[](0); - - uint256 _byteLength = StoreSwitch.getDynamicFieldLength(_tableId, _keyTuple, 1); - unchecked { - return _byteLength / 4; - } + function lengthV3() internal pure returns (uint256) { + return 2; } /** * @notice Get the length of v3. */ - function _lengthV3() internal view returns (uint256) { - bytes32[] memory _keyTuple = new bytes32[](0); - - uint256 _byteLength = StoreCore.getDynamicFieldLength(_tableId, _keyTuple, 1); - unchecked { - return _byteLength / 4; - } + function _lengthV3() internal pure returns (uint256) { + return 2; } /** @@ -370,42 +314,6 @@ library Singleton { } } - /** - * @notice Push an element to v3. - */ - function pushV3(uint32 _element) internal { - bytes32[] memory _keyTuple = new bytes32[](0); - - StoreSwitch.pushToDynamicField(_tableId, _keyTuple, 1, abi.encodePacked((_element))); - } - - /** - * @notice Push an element to v3. - */ - function _pushV3(uint32 _element) internal { - bytes32[] memory _keyTuple = new bytes32[](0); - - StoreCore.pushToDynamicField(_tableId, _keyTuple, 1, abi.encodePacked((_element))); - } - - /** - * @notice Pop an element from v3. - */ - function popV3() internal { - bytes32[] memory _keyTuple = new bytes32[](0); - - StoreSwitch.popFromDynamicField(_tableId, _keyTuple, 1, 4); - } - - /** - * @notice Pop an element from v3. - */ - function _popV3() internal { - bytes32[] memory _keyTuple = new bytes32[](0); - - StoreCore.popFromDynamicField(_tableId, _keyTuple, 1, 4); - } - /** * @notice Update an element of v3 at `_index`. */ @@ -471,25 +379,15 @@ library Singleton { /** * @notice Get the length of v4. */ - function lengthV4() internal view returns (uint256) { - bytes32[] memory _keyTuple = new bytes32[](0); - - uint256 _byteLength = StoreSwitch.getDynamicFieldLength(_tableId, _keyTuple, 2); - unchecked { - return _byteLength / 4; - } + function lengthV4() internal pure returns (uint256) { + return 1; } /** * @notice Get the length of v4. */ - function _lengthV4() internal view returns (uint256) { - bytes32[] memory _keyTuple = new bytes32[](0); - - uint256 _byteLength = StoreCore.getDynamicFieldLength(_tableId, _keyTuple, 2); - unchecked { - return _byteLength / 4; - } + function _lengthV4() internal pure returns (uint256) { + return 1; } /** @@ -518,42 +416,6 @@ library Singleton { } } - /** - * @notice Push an element to v4. - */ - function pushV4(uint32 _element) internal { - bytes32[] memory _keyTuple = new bytes32[](0); - - StoreSwitch.pushToDynamicField(_tableId, _keyTuple, 2, abi.encodePacked((_element))); - } - - /** - * @notice Push an element to v4. - */ - function _pushV4(uint32 _element) internal { - bytes32[] memory _keyTuple = new bytes32[](0); - - StoreCore.pushToDynamicField(_tableId, _keyTuple, 2, abi.encodePacked((_element))); - } - - /** - * @notice Pop an element from v4. - */ - function popV4() internal { - bytes32[] memory _keyTuple = new bytes32[](0); - - StoreSwitch.popFromDynamicField(_tableId, _keyTuple, 2, 4); - } - - /** - * @notice Pop an element from v4. - */ - function _popV4() internal { - bytes32[] memory _keyTuple = new bytes32[](0); - - StoreCore.popFromDynamicField(_tableId, _keyTuple, 2, 4); - } - /** * @notice Update an element of v4 at `_index`. */ diff --git a/packages/store/ts/codegen/field.ts b/packages/store/ts/codegen/field.ts index e24f5403a6..5eb2db1db1 100644 --- a/packages/store/ts/codegen/field.ts +++ b/packages/store/ts/codegen/field.ts @@ -51,7 +51,7 @@ export function renderFieldMethods(options: RenderTableOptions) { } return ${renderDecodeFieldSingle(field)}; } - ` + ` ) ); } @@ -80,29 +80,50 @@ export function renderFieldMethods(options: RenderTableOptions) { if (field.isDynamic) { const portionData = fieldPortionData(field); const dynamicSchemaIndex = schemaIndex - options.staticFields.length; + const { typeWrappingData } = field; if (options.withGetters) { - result += renderWithFieldSuffix(options.withSuffixlessFieldMethods, field.name, (_methodNameSuffix) => - renderWithStore( - storeArgument, - ({ _typedStore, _store, _commentSuffix, _methodNamePrefix }) => ` - /** - * @notice Get the length of ${field.name}${_commentSuffix}. - */ - function ${_methodNamePrefix}length${_methodNameSuffix}(${renderArguments([ - _typedStore, - _typedTableId, - _typedKeyArgs, - ])}) internal view returns (uint256) { - ${_keyTupleDefinition} - uint256 _byteLength = ${_store}.getDynamicFieldLength(_tableId, _keyTuple, ${dynamicSchemaIndex}); - unchecked { - return _byteLength / ${portionData.elementLength}; + 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}; } - } - ` - ) - ); + ` + ) + ); + } else { + result += renderWithFieldSuffix(options.withSuffixlessFieldMethods, field.name, (_methodNameSuffix) => + renderWithStore( + storeArgument, + ({ _typedStore, _store, _commentSuffix, _methodNamePrefix }) => ` + /** + * @notice Get the length of ${field.name}${_commentSuffix}. + */ + function ${_methodNamePrefix}length${_methodNameSuffix}(${renderArguments([ + _typedStore, + _typedTableId, + _typedKeyArgs, + ])}) internal view returns (uint256) { + ${_keyTupleDefinition} + uint256 _byteLength = ${_store}.getDynamicFieldLength(_tableId, _keyTuple, ${dynamicSchemaIndex}); + unchecked { + return _byteLength / ${portionData.elementLength}; + } + } + ` + ) + ); + } result += renderWithFieldSuffix(options.withSuffixlessFieldMethods, field.name, (_methodNameSuffix) => renderWithStore( @@ -118,14 +139,14 @@ export function renderFieldMethods(options: RenderTableOptions) { _typedKeyArgs, "uint256 _index", ])}) internal view returns (${portionData.typeWithLocation}) { - ${_keyTupleDefinition} - unchecked { - bytes memory _blob = ${_store}.getDynamicFieldSlice( - _tableId, - _keyTuple, - ${dynamicSchemaIndex}, - _index * ${portionData.elementLength}, - (_index + 1) * ${portionData.elementLength} + ${_keyTupleDefinition} + unchecked { + bytes memory _blob = ${_store}.getDynamicFieldSlice( + _tableId, + _keyTuple, + ${dynamicSchemaIndex}, + _index * ${portionData.elementLength}, + (_index + 1) * ${portionData.elementLength} ); return ${portionData.decoded}; } @@ -135,44 +156,46 @@ export function renderFieldMethods(options: RenderTableOptions) { ); } - result += renderWithFieldSuffix(options.withSuffixlessFieldMethods, field.name, (_methodNameSuffix) => - renderWithStore( - storeArgument, - ({ _typedStore, _store, _commentSuffix, _methodNamePrefix }) => ` + if (!typeWrappingData || typeWrappingData.kind !== "staticArray") { + result += renderWithFieldSuffix(options.withSuffixlessFieldMethods, field.name, (_methodNameSuffix) => + renderWithStore( + storeArgument, + ({ _typedStore, _store, _commentSuffix, _methodNamePrefix }) => ` /** * @notice Push ${portionData.title} to ${field.name}${_commentSuffix}. */ function ${_methodNamePrefix}push${_methodNameSuffix}(${renderArguments([ - _typedStore, - _typedTableId, - _typedKeyArgs, - `${portionData.typeWithLocation} ${portionData.name}`, - ])}) internal { - ${_keyTupleDefinition} - ${_store}.pushToDynamicField(_tableId, _keyTuple, ${dynamicSchemaIndex}, ${portionData.encoded}); - } + _typedStore, + _typedTableId, + _typedKeyArgs, + `${portionData.typeWithLocation} ${portionData.name}`, + ])}) internal { + ${_keyTupleDefinition} + ${_store}.pushToDynamicField(_tableId, _keyTuple, ${dynamicSchemaIndex}, ${portionData.encoded}); + } ` - ) - ); + ) + ); - result += renderWithFieldSuffix(options.withSuffixlessFieldMethods, field.name, (_methodNameSuffix) => - renderWithStore( - storeArgument, - ({ _typedStore, _store, _commentSuffix, _methodNamePrefix }) => ` - /** - * @notice Pop ${portionData.title} from ${field.name}${_commentSuffix}. - */ - function ${_methodNamePrefix}pop${_methodNameSuffix}(${renderArguments([ - _typedStore, - _typedTableId, - _typedKeyArgs, - ])}) internal { - ${_keyTupleDefinition} - ${_store}.popFromDynamicField(_tableId, _keyTuple, ${dynamicSchemaIndex}, ${portionData.elementLength}); - } - ` - ) - ); + result += renderWithFieldSuffix(options.withSuffixlessFieldMethods, field.name, (_methodNameSuffix) => + renderWithStore( + storeArgument, + ({ _typedStore, _store, _commentSuffix, _methodNamePrefix }) => ` + /** + * @notice Pop ${portionData.title} from ${field.name}${_commentSuffix}. + */ + function ${_methodNamePrefix}pop${_methodNameSuffix}(${renderArguments([ + _typedStore, + _typedTableId, + _typedKeyArgs, + ])}) internal { + ${_keyTupleDefinition} + ${_store}.popFromDynamicField(_tableId, _keyTuple, ${dynamicSchemaIndex}, ${portionData.elementLength}); + } + ` + ) + ); + } result += renderWithFieldSuffix(options.withSuffixlessFieldMethods, field.name, (_methodNameSuffix) => renderWithStore(storeArgument, ({ _typedStore, _store, _commentSuffix, _methodNamePrefix }) => {