Skip to content
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

fix: u256 and ns for unity bindgen #2635

Merged
merged 2 commits into from
Nov 5, 2024

Conversation

Larkooo
Copy link
Collaborator

@Larkooo Larkooo commented Nov 5, 2024

Summary by CodeRabbit

  • New Features

    • Enhanced type mappings for BigInteger and improved model class generation in the UnityPlugin.
    • Added support for handling composite types, specifically for U256, generating separate instances for high and low values.
  • Bug Fixes

    • Improved identification and processing of model structs and dependencies.
  • Documentation

    • Updated method signatures for better clarity and functionality.

Copy link

coderabbitai bot commented Nov 5, 2024

Walkthrough

Ohayo, sensei! This pull request introduces several modifications to the UnityPlugin implementation in mod.rs. Key updates include new type mappings for BigInteger, restructuring of the format_model function to include a namespace parameter, and enhancements to model class generation. Additionally, the handle_model function has been refined for better struct processing, and the handle_arg_recursive function now manages CompositeType::Unknown for U256 types, generating two FieldElement instances.

Changes

File Path Change Summary
crates/dojo/bindgen/src/plugins/unity/mod.rs - Updated format_model to include a namespace parameter.
- Restructured model class naming.
- Modified handle_model for better struct identification.
- Enhanced handle_arg_recursive for CompositeType::Unknown with U256 handling.
- Updated method signatures for format_model and generate_code.

Possibly related PRs

  • refactor: update & fix unity bindgen #2631: The changes in this PR also modify the UnityPlugin implementation in mod.rs, specifically enhancing the handle_model function, which is directly related to the changes made in the main PR regarding model struct processing and formatting.

Suggested reviewers

  • glihm

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Outside diff range and nitpick comments (1)
crates/dojo/bindgen/src/plugins/unity/mod.rs (1)

206-215: Ohayo, sensei! Remove empty 'Start' and 'Update' methods if unnecessary

The Start() and Update() methods in the model class are empty. If the model class does not require Unity's lifecycle methods, consider removing them to simplify the code and improve readability.

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 447ba4f and ba3c117.

📒 Files selected for processing (1)
  • crates/dojo/bindgen/src/plugins/unity/mod.rs (4 hunks)
🔇 Additional comments (2)
crates/dojo/bindgen/src/plugins/unity/mod.rs (2)

107-107: Ohayo, sensei! Addition of 'BigInteger' using statement is appropriate

Including using BigInteger = System.Numerics.BigInteger; ensures that BigInteger is correctly referenced for i128, u128, and u256 type mappings. This enhances type handling in both contract_imports and model_imports.

Also applies to: 120-120


347-358: Ohayo, sensei! Verify the handling of 'U256' composite type

The handle_arg_recursive function now handles CompositeType::Unknown for U256 by decomposing it into high and low fields and converting them to FieldElements. Please ensure that:

  • The U256 type is defined with accessible high and low fields.
  • This approach aligns with how U256 should be serialized in the system.

You can run the following script to verify the definition of U256:

✅ Verification successful

Ohayo, sensei! The U256 handling is correctly implemented

Based on the codebase analysis, the handling of U256 with high and low components is verified to be correct. The code uses multiple U256 implementations:

  1. alloy_primitives::U256 - Used extensively in Katana, with proper high/low bit manipulation:
let low_u128: u128 = (value & U256::from(u128::MAX)).to();
let high_u128: u128 = U256::from(value >> 128).to();
  1. starknet::core::types::U256 - Used in Torii with proper high/low field access:
U256::from_words(value.low, value.high)

The approach in the review code correctly decomposes U256 into high and low FieldElements, which aligns with both implementations' serialization patterns.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify that 'U256' is defined with 'high' and 'low' fields.

# Test: Search for the definition of 'U256' struct with 'high' and 'low' fields.
ast-grep --pattern $'struct U256 {{
  high: $_,
  low: $_,
  $$$
}}'

Length of output: 69


Script:

#!/bin/bash
# Let's try a broader search for U256 definitions and usages

# Search for U256 struct definition in Rust files
rg "struct U256" -A 5

# Search for U256 type imports or uses
rg "use.*U256"

# Search for any mentions of U256 with high or low
rg "U256.*(?:high|low)|(?:high|low).*U256" -p

Length of output: 6296


Script:

#!/bin/bash
# Let's also check if U256 might be defined in dependencies
# by looking at Cargo.toml files and their dependencies

fd Cargo.toml --exec cat {}

Length of output: 54307


Script:

#!/bin/bash
# Check if U256 might be coming from common crypto/numeric libraries
rg "primitive-types|uint|num-bigint" Cargo.toml

Length of output: 70


Script:

#!/bin/bash
# Let's check for U256 in alloy-primitives since we see it's used in many imports
rg "use alloy_primitives::U256" -A 3

# Also check for any U256 implementations or methods with high/low
rg "impl.*U256|fn.*U256.*high|fn.*U256.*low" -A 5

Length of output: 4389


Script:

#!/bin/bash
# Let's also check the starknet crate's U256 usage since it's imported in many places
rg "use starknet::.*U256" -A 3

Length of output: 2582

Comment on lines +206 to +215
// Model definition for `{}` model
public class {}_{} : ModelInstance {{
{}

// Start is called before the first frame update
void Start() {{
}}

// Update is called once per frame
void Update() {{
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Ohayo, sensei! Consider using C# namespaces instead of prefixing class names

Currently, the model class name includes the namespace appended with an underscore (e.g., namespace_ModelName). In C#, it's conventional to use the namespace keyword to define namespaces, which improves code organization and adheres to best practices.

 public class {}_{} : ModelInstance {{
+// Suggested change:
+namespace {} {{
+    public class {} : ModelInstance {{
         {}
     }}
+}}

Committable suggestion skipped: line range outside the PR's diff.

Copy link

codecov bot commented Nov 5, 2024

Codecov Report

Attention: Patch coverage is 0% with 25 lines in your changes missing coverage. Please review.

Project coverage is 56.76%. Comparing base (447ba4f) to head (ba3c117).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
crates/dojo/bindgen/src/plugins/unity/mod.rs 0.00% 25 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2635      +/-   ##
==========================================
- Coverage   56.79%   56.76%   -0.03%     
==========================================
  Files         397      397              
  Lines       49560    49573      +13     
==========================================
- Hits        28148    28142       -6     
- Misses      21412    21431      +19     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

// Update is called once per frame
void Update() {{
}}
// Model definition for `{}` model
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
// Model definition for `{}` model
// Model definition for `{}_{}` model

@Larkooo Larkooo merged commit 14211a9 into dojoengine:main Nov 5, 2024
12 of 14 checks passed
@Larkooo Larkooo deleted the fix-bindgen-final branch November 5, 2024 19:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants