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
5 changes: 5 additions & 0 deletions .changeset/lovely-knives-leave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@latticexyz/world": patch
---

Fix static array arguments in system libraries.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
} from "@latticexyz/common/codegen";
import { RenderSystemLibraryOptions } from "./types";
import { ContractInterfaceError } from "@latticexyz/common/codegen";
import { stringToHex } from "viem";

export function renderSystemLibrary(options: RenderSystemLibraryOptions) {
const {
Expand Down Expand Up @@ -289,6 +290,8 @@ function functionInterfaceName(contractFunction: ContractInterfaceFunction) {
const paramTypes = parameters
.map((param) => param.split(" ")[0])
.map((type) => type.replace("[]", "Array"))
// Static arrays may contain multiple disallowed symbols, for name uniqueness toHex is easier than escaping
.map((type) => type.replace(/\[.+\]/, (match) => stringToHex(match)))
.join("_");
return `_${name}${paramTypes.length === 0 ? "" : `_${paramTypes}`}`;
}
Expand Down
7 changes: 7 additions & 0 deletions test/system-libraries/src/codegen/world/IASystem.sol

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

21 changes: 21 additions & 0 deletions test/system-libraries/src/namespaces/a/ASystem.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Value } from "./codegen/tables/Value.sol";
import { PositionValue } from "./codegen/tables/PositionValue.sol";
import { AddressValue } from "./codegen/tables/AddressValue.sol";
import { ASystemThing, Position } from "./ASystemTypes.sol";
import { THREE } from "./ASystemConstants.sol";

contract ASystem is System {
function setValue(ASystemThing memory value) external {
Expand Down Expand Up @@ -43,4 +44,24 @@ contract ASystem is System {
AddressValue.set(addr);
return addr;
}

function setValuesStaticArray(uint256[1] memory values) external {
Value.set(values[0]);
}

function setValuesStaticArray(uint256[2] memory values) external {
Value.set(values[1]);
}

function setValuesStaticArray(uint256[THREE] memory values) external {
Value.set(values[2]);
}

/*
// TODO: support this case
// (see flattenTypeName in contractToInterface.ts)
function setValuesStaticArray(uint256[1 - 0 * 2 + THREE] memory values) external {
Value.set(values[3]);
}
*/
}
4 changes: 4 additions & 0 deletions test/system-libraries/src/namespaces/a/ASystemConstants.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.28;

uint256 constant THREE = 3;

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

7 changes: 7 additions & 0 deletions test/system-libraries/test/Libraries.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ contract LibrariesTest is MudTest {
assertEq(PositionValue.getX(), 4);
assertEq(PositionValue.getY(), 5);
assertEq(PositionValue.getZ(), 6);

aSystem.setValuesStaticArray([uint256(1)]);
assertEq(Value.get(), 1);
aSystem.setValuesStaticArray([uint256(1), 2]);
assertEq(Value.get(), 2);
aSystem.setValuesStaticArray([uint256(1), 2, 3]);
assertEq(Value.get(), 3);
}

function testCanCallSystemFromOtherSystem() public {
Expand Down
Loading