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

feat: Builder pattern for VirtualMachine #820

Merged
merged 1 commit into from
Feb 24, 2023

Conversation

zarboq
Copy link
Contributor

@zarboq zarboq commented Feb 4, 2023

Builder pattern for VirtualMachine

Description

Added a builder pattern for VirtualMachine (VirtualMachineBuilder) to allow creating VirtualMachine with customized fields.

Linked to issue #819

Checklist

  • Linked to Github Issue
  • Unit tests added
  • Integration tests added.
  • This change requires new documentation.
    • Documentation has been added/updated.

}

pub fn segments(mut self, segments: MemorySegmentManager) -> VirtualMachineBuilder {
// Set the name on the builder itself, and return the builder by value.
Copy link
Contributor

Choose a reason for hiding this comment

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

You copy pasted this comment on every setter method.
Yon can remove it every time

Comment on lines 1110 to 1112
// If we can get away with not consuming the Builder here, that is an
// advantage. It means we can use the FooBuilder as a template for constructing
// many Foos.
Copy link
Contributor

Choose a reason for hiding this comment

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

Remove this comment

Comment on lines 1114 to 1115
// Create a Foo from the FooBuilder, applying all settings in FooBuilder
// to Foo.
Copy link
Contributor

Choose a reason for hiding this comment

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

and this one

}

impl VirtualMachineBuilder {
pub fn new(trace_enabled: bool) -> VirtualMachineBuilder {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is trace enable treated differently than the other fields?
Couldn't it also have it setter method?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It also has one but I just reproduced VirtualMachine constructor behavior. Don't know if it's the right way of doing it or if it should be created empty.

Copy link
Contributor

Choose a reason for hiding this comment

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

You can ignore the current VirtualMachine::new and go with your own. By default it will be None, and if the function with_trace (or smthing else) is called, it will internally be set to Some(Vec::<TraceEntry>::new())

@zarboq zarboq force-pushed the virtual_machine_builder branch from d161a24 to 1ff9260 Compare February 16, 2023 15:03
@codecov
Copy link

codecov bot commented Feb 16, 2023

Codecov Report

Merging #820 (04b9321) into main (abaf215) will increase coverage by 0.01%.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##             main     #820      +/-   ##
==========================================
+ Coverage   96.82%   96.84%   +0.01%     
==========================================
  Files          69       69              
  Lines       28835    29017     +182     
==========================================
+ Hits        27920    28102     +182     
  Misses        915      915              
Impacted Files Coverage Δ
src/vm/vm_core.rs 97.54% <100.00%> (+0.14%) ⬆️

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@tdelabro
Copy link
Contributor

also it's breaking the CI

@zarboq zarboq force-pushed the virtual_machine_builder branch 3 times, most recently from 356bd99 to 34ee83d Compare February 18, 2023 23:36
@zarboq zarboq requested a review from tdelabro February 20, 2023 17:04
Comment on lines 1003 to 1006
impl Default for VirtualMachineBuilder {
fn default() -> Self {
Self::new()
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Perso si j'ai default et new qui font la même chose je préfère autant n'avoir que default.
C'est pas un red flag, mais c'est plus concis, respectueux des principe DRY


#[test]
fn builder_test() {
let run_context = RunContext {
Copy link
Contributor

Choose a reason for hiding this comment

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

inline RunContext { ... } in the VitualMachine { ... } instanciation

fp: 0,
};

let virtual_machine = VirtualMachine {
Copy link
Contributor

Choose a reason for hiding this comment

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

call it expected_virtual_machine

@zarboq zarboq force-pushed the virtual_machine_builder branch from 34ee83d to dc87676 Compare February 20, 2023 22:08
Comment on lines +4159 to +4142
.run_finished(true)
.current_step(12)
Copy link
Contributor

Choose a reason for hiding this comment

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

Add a call to each one of your setters function here

@zarboq zarboq force-pushed the virtual_machine_builder branch 2 times, most recently from 8f8b003 to 6feafeb Compare February 21, 2023 16:12
@zarboq zarboq requested a review from tdelabro February 21, 2023 17:31
let _virtual_machine_from_builder: VirtualMachine = VirtualMachineBuilder::default()
.hooks(crate::vm::hooks::Hooks::new(
None,
Some(Arc::new(pre_step_hook)),
Copy link
Contributor

Choose a reason for hiding this comment

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

Some(std::sync::Arc::new(pre_step_hook)), will spare you the import of line 4200

ap: Relocatable::from((0, 1)),
fp: Relocatable::from((0, 1)),
}]))
.build();
Copy link
Contributor

Choose a reason for hiding this comment

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

About the test of the hook.

Don't call .build() immediately. Assign the builder to a variable instead.

Then behind #[cfg(feature = "hooks")] call hooks on this variable.

Then call build() and store the vm_from_builder as you did.

Then do all your assert, then behind #[cfg(feature = "hooks")] add one more assert for the hooks

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Smart !

@zarboq zarboq force-pushed the virtual_machine_builder branch from 6feafeb to 04b9321 Compare February 22, 2023 11:49
@zarboq zarboq marked this pull request as ready for review February 22, 2023 16:27
Copy link
Contributor

@fmoletta fmoletta left a comment

Choose a reason for hiding this comment

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

Looks good! Thanks for the PR!

@tdelabro
Copy link
Contributor

@Oppen would you like to be the second approver?

Copy link
Contributor

@Jrigada Jrigada left a comment

Choose a reason for hiding this comment

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

LGTM! Thanks

@Jrigada Jrigada added this pull request to the merge queue Feb 24, 2023
Merged via the queue into lambdaclass:main with commit 71e7140 Feb 24, 2023
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.

4 participants