Skip to content

Conversation

0xrusowsky
Copy link
Contributor

@0xrusowsky 0xrusowsky commented Sep 11, 2025

Motivation

Historical limitation in vm.parseJson where JSON object fields had to be sorted alphabetically, breaking compatibility with Solidity struct field order. This required developers to work around the limitation by manually ordering JSON fields.

Solution

Integrate Solar's compiler output into the multi-runners. The PR introduces a CheatcodeAnalysis architecture that provides the cheatcode inspector access to Solar's compiler output, enabling order-preserving JSON parsing.

JSON cheats improvements

  • parseJson and parseJsonType now use struct definitions to preserve field order
  • fallback to alphabetical ordering when unavailable (backwards compatible)

CheatcodeAnalysis Architecture

New CheatcodeAnalysis struct that provides cached, on-demand access to Solar's compiler output:

pub struct CheatcodeAnalysis {
    pub compiler: Arc<Compiler>,
    struct_defs: OnceLock<Result<StructDefinitions, AnalysisError>>,
}

the impl aims to be:

  • easily extensible by only having to modify the CheatcodeAnalysis struct with the new fields and getter methods
  • performant thanks to the lazy (on-demand) evaluation + caching

integrating a new analysis tool only requires setting up a new getter method (besides its impl):

/// Lazily initializes and returns the struct definitions.
pub fn struct_defs(&self) -> Result<&StructDefinitions, &AnalysisError> {
    self.struct_defs
        .get_or_init(|| {
            self.compiler.enter(|compiler| {
                let gcx = compiler.gcx();

                StructDefinitionResolver::new(gcx).process()
            })
        })
        .as_ref()
}

note: StructDefinitionResolver is a HIR visitor


old disc:

@0xrusowsky 0xrusowsky changed the title feat(cheats): sorted JSON params feat(cheats): preserve struct order when parsing JSON objects Sep 17, 2025
@grandizzy grandizzy added this to the v1.5.0 milestone Sep 22, 2025
@0xrusowsky 0xrusowsky marked this pull request as ready for review September 23, 2025 19:28
let value = serialize_value_as_json(value)?;
let value = serialize_value_as_json(
value,
state.analysis.as_ref().and_then(|analysis| analysis.struct_defs().ok()),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can make this a fn on state

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

output: &ProjectCompileOutput,
mut report: CoverageReport,
config: Arc<Config>,
config_and_project: ConfigAndProject,
Copy link
Member

@DaniPopes DaniPopes Sep 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is not needed, you can't clone it you should be able to re-use output's compiler in build

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@0xrusowsky 0xrusowsky moved this to Ready For Review in Foundry Sep 24, 2025
@0xrusowsky 0xrusowsky marked this pull request as draft September 24, 2025 09:45
@0xrusowsky 0xrusowsky moved this from Ready For Review to In Progress in Foundry Sep 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: In Progress
Development

Successfully merging this pull request may close these issues.

3 participants