-
Notifications
You must be signed in to change notification settings - Fork 217
feat(world): find systems based on inheritance #3649
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
0c43567
Support systems without suffix by adding them to MUD config
vdrg 31ae7d6
Create small-horses-own.md
vdrg ab0ad91
Use system labels from namespaces config
vdrg eb7ef49
Merge remote-tracking branch 'origin/main' into vdrg/systems-without-…
frolic d85b80c
parse system source
frolic a858cb3
rm log
frolic c8d47e2
clarify the strategy
frolic 2b0053b
update changeset
frolic b498a08
update snapshots
frolic a7d9a0c
gas report
frolic 1d087e9
Update small-horses-own.md
frolic File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
--- | ||
"@latticexyz/world": patch | ||
"@latticexyz/cli": patch | ||
--- | ||
|
||
`mud` CLI commands will now recognize systems if they inherit directly from the base `System` imported from `@latticexyz/world/src/System.sol`, allowing you to write systems without a `System` suffix. | ||
|
||
```solidity | ||
import {System} from "@latticexyz/world/src/System.sol"; | ||
|
||
contract EntityProgram is System { | ||
... | ||
} | ||
``` | ||
|
||
If you have contracts that inherit from the base `System` that aren't meant to be deployed, you can mark them as `abstract contract` or [disable the system's deploy via config](https://mud.dev/config/reference). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { visit } from "@solidity-parser/parser"; | ||
import type { ContractDefinition, SourceUnit } from "@solidity-parser/parser/dist/src/ast-types"; | ||
|
||
export function findContractNode(ast: SourceUnit, contractName: string): ContractDefinition | undefined { | ||
let contract: ContractDefinition | undefined = undefined; | ||
|
||
visit(ast, { | ||
ContractDefinition(node) { | ||
if (node.name === contractName) { | ||
contract = node; | ||
} | ||
}, | ||
}); | ||
|
||
return contract; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { visit } from "@solidity-parser/parser"; | ||
import type { SourceUnit } from "@solidity-parser/parser/dist/src/ast-types"; | ||
|
||
export interface SymbolImport { | ||
symbol: string; | ||
path: string; | ||
} | ||
|
||
/** | ||
* Get import for given symbol. | ||
* | ||
* To avoid circular dependencies of interfaces on their implementations, | ||
* symbols used for args/returns must always be imported from an auxiliary file. | ||
* To avoid parsing the entire project to build dependencies, | ||
* symbols must be imported with an explicit `import { symbol } from ...` | ||
*/ | ||
export function findSymbolImport(ast: SourceUnit, symbol: string): SymbolImport | undefined { | ||
let symbolImport: SymbolImport | undefined; | ||
|
||
visit(ast, { | ||
ImportDirective({ path, symbolAliases }) { | ||
if (symbolAliases) { | ||
for (const symbolAndAlias of symbolAliases) { | ||
// either check the alias, or the original symbol if there's no alias | ||
const symbolAlias = symbolAndAlias[1] ?? symbolAndAlias[0]; | ||
if (symbol === symbolAlias) { | ||
symbolImport = { | ||
// always use the original symbol for interface imports | ||
symbol: symbolAndAlias[0], | ||
path, | ||
}; | ||
return; | ||
} | ||
} | ||
} | ||
}, | ||
}); | ||
|
||
return symbolImport; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from "./contractToInterface"; | ||
export * from "./format"; | ||
export * from "./formatAndWrite"; | ||
export * from "./parseSystem"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { parse, visit } from "@solidity-parser/parser"; | ||
|
||
import { findContractNode } from "./findContractNode"; | ||
import { findSymbolImport } from "./findSymbolImport"; | ||
|
||
const baseSystemName = "System"; | ||
const baseSystemPath = "@latticexyz/world/src/System.sol"; | ||
|
||
export function parseSystem( | ||
source: string, | ||
contractName: string, | ||
): undefined | { contractType: "contract" | "abstract" } { | ||
const ast = parse(source); | ||
const contractNode = findContractNode(ast, contractName); | ||
if (!contractNode) return; | ||
|
||
const contractType = contractNode.kind; | ||
// skip libraries and interfaces | ||
// we allow abstract systems here so that we can create system libraries from them but without deploying them | ||
if (contractType !== "contract" && contractType !== "abstract") return; | ||
|
||
const isSystem = ((): boolean => { | ||
// if using the System suffix, assume its a system | ||
if (contractName.endsWith("System") && contractName !== baseSystemName) return true; | ||
|
||
// otherwise check if we're inheriting from the base system | ||
let extendsBaseSystem = false; | ||
visit(contractNode, { | ||
InheritanceSpecifier(node) { | ||
if (node.baseName.namePath === baseSystemName) { | ||
extendsBaseSystem = true; | ||
} | ||
}, | ||
}); | ||
return extendsBaseSystem && findSymbolImport(ast, baseSystemName)?.path === baseSystemPath; | ||
})(); | ||
|
||
if (isSystem) { | ||
return { contractType }; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this has to be done serially now that we're parsing all
*.sol
source files in worldgen to determine if a source file is a system, otherwise we get a bunch of "file does not exist" errors from tablegen deleting/creating files