Skip to content

Commit f6d87ed

Browse files
dk1avdrg
andauthored
fix(world): fix static array arguments in system libraries (#3661)
Co-authored-by: V <[email protected]>
1 parent fc10a27 commit f6d87ed

File tree

7 files changed

+123
-0
lines changed

7 files changed

+123
-0
lines changed

.changeset/lovely-knives-leave.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@latticexyz/world": patch
3+
---
4+
5+
Fix static array arguments in system libraries.

packages/world/ts/node/render-solidity/renderSystemLibrary.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
} from "@latticexyz/common/codegen";
88
import { RenderSystemLibraryOptions } from "./types";
99
import { ContractInterfaceError } from "@latticexyz/common/codegen";
10+
import { stringToHex } from "viem";
1011

1112
export function renderSystemLibrary(options: RenderSystemLibraryOptions) {
1213
const {
@@ -289,6 +290,8 @@ function functionInterfaceName(contractFunction: ContractInterfaceFunction) {
289290
const paramTypes = parameters
290291
.map((param) => param.split(" ")[0])
291292
.map((type) => type.replace("[]", "Array"))
293+
// Static arrays may contain multiple disallowed symbols, for name uniqueness toHex is easier than escaping
294+
.map((type) => type.replace(/\[.+\]/, (match) => stringToHex(match)))
292295
.join("_");
293296
return `_${name}${paramTypes.length === 0 ? "" : `_${paramTypes}`}`;
294297
}

test/system-libraries/src/codegen/world/IASystem.sol

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/system-libraries/src/namespaces/a/ASystem.sol

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { Value } from "./codegen/tables/Value.sol";
66
import { PositionValue } from "./codegen/tables/PositionValue.sol";
77
import { AddressValue } from "./codegen/tables/AddressValue.sol";
88
import { ASystemThing, Position } from "./ASystemTypes.sol";
9+
import { THREE } from "./ASystemConstants.sol";
910

1011
contract ASystem is System {
1112
function setValue(ASystemThing memory value) external {
@@ -43,4 +44,24 @@ contract ASystem is System {
4344
AddressValue.set(addr);
4445
return addr;
4546
}
47+
48+
function setValuesStaticArray(uint256[1] memory values) external {
49+
Value.set(values[0]);
50+
}
51+
52+
function setValuesStaticArray(uint256[2] memory values) external {
53+
Value.set(values[1]);
54+
}
55+
56+
function setValuesStaticArray(uint256[THREE] memory values) external {
57+
Value.set(values[2]);
58+
}
59+
60+
/*
61+
// TODO: support this case
62+
// (see flattenTypeName in contractToInterface.ts)
63+
function setValuesStaticArray(uint256[1 - 0 * 2 + THREE] memory values) external {
64+
Value.set(values[3]);
65+
}
66+
*/
4667
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// SPDX-License-Identifier: MIT
2+
pragma solidity >=0.8.28;
3+
4+
uint256 constant THREE = 3;

test/system-libraries/src/namespaces/a/codegen/systems/ASystemLib.sol

Lines changed: 76 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/system-libraries/test/Libraries.t.sol

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,13 @@ contract LibrariesTest is MudTest {
5353
assertEq(PositionValue.getX(), 4);
5454
assertEq(PositionValue.getY(), 5);
5555
assertEq(PositionValue.getZ(), 6);
56+
57+
aSystem.setValuesStaticArray([uint256(1)]);
58+
assertEq(Value.get(), 1);
59+
aSystem.setValuesStaticArray([uint256(1), 2]);
60+
assertEq(Value.get(), 2);
61+
aSystem.setValuesStaticArray([uint256(1), 2, 3]);
62+
assertEq(Value.get(), 3);
5663
}
5764

5865
function testCanCallSystemFromOtherSystem() public {

0 commit comments

Comments
 (0)