NethermindEth / cairo-vm-go

A virtual machine for Cairo written in Go
MIT License
79 stars 49 forks source link

[feat] added a default scope manager #382

Closed fishonamos closed 3 months ago

fishonamos commented 3 months ago

Modified the code so when the context is created, the Scope Manager is also initialised. This will solve #345

TAdev0 commented 3 months ago

@fishonamos hi, your code will not compile because NewScopeManager takes a map[string]any as argument. What you can do instead is calling *hinter.DefaultNewScopeManager() , and DefaultNewScopeManager will call NewScopeManager for you passing an empty array as argument.

TAdev0 commented 3 months ago

Basically, you have 2 possibilities:

fishonamos commented 3 months ago

Basically, you have 2 possibilities:

  • manually defining the ScopeManager field of ctx
  ctx := &hinter.HintRunnerContext{
          ScopeManager: *hinter.DefaultNewScopeManager(),
      }
  • creating an uninitialized ctx an calling InitializeScopeManager , passing ctx and an empty array of string to initialize the ScopeManager field:
      ctx := &hinter.HintRunnerContext{}
      hinter.InitializeScopeManager(ctx, make(map[string]any))

Thank you @TAdev0 . I used the Uninitialized ctx option which creates an empty HintRunnerContext first, then initializes the ScopeManager using InitializeScopeManager(). The code now compiles.

TAdev0 commented 3 months ago

LGTM!

@cicr99 should we merge this like this and open another PR to modify every test to be consistent with the default initialized scope manager?

TAdev0 commented 3 months ago

there is a unit test failing after merging the main branch.

This is becasue of UsortVerify test that uses InitializeScopeManager to create a scope with positions_dict . But as we already call InitializeScopeManager in the general test setting, it wont work.

Can you modify the UsortVerify so that it calls ctx.ScopeManager.EnterScope instead?

har777 commented 3 months ago

there is a unit test failing after merging the main branch.

This is becasue of UsortVerify test that uses InitializeScopeManager to create a scope with positions_dict . But as we already call InitializeScopeManager in the general test setting, it wont work.

Can you modify the UsortVerify so that it calls ctx.ScopeManager.EnterScope instead?

Shouldn't he just call AssignVariable? Given a default scope exists now?

TAdev0 commented 3 months ago

yes AssignVariable is enough, you are right. I'll do a PR to update all tests configuration once this one is merged anyway

cicr99 commented 3 months ago

LGTM!

@cicr99 should we merge this like this and open another PR to modify every test to be consistent with the default initialized scope manager?

I was thinking to do the updating of all tests in this pr as well, but it might take some time and effort, specially with all the merges happening at the moment. I think doing it separately would be the best way to go. Once the tests are passing @fishonamos, we can merge. The PR looks good like this!

fishonamos commented 3 months ago

I modified "Usort Verify" to just call Assign Variable. I think we need to do for all others too, such as UsortVerifyMultiplicityAssert.

TAdev0 commented 3 months ago

hi @fishonamos , yes i will update all other tests in another PR to make sure we never create unnecessary scopes :)

TAdev0 commented 3 months ago

@fishonamos your unit test dont pass. You should use make build and make unit to run tests locally and make sure they pass. you need to check the return value with AssignVariable

fishonamos commented 3 months ago

They are now passing

Screenshot 2024-05-04 at 02 03 04
TAdev0 commented 3 months ago

you need to check the return value with AssigneVariable


err = ctx.ScopeManager.AssignVariable("...", ...)
 if err != nil {
       return err
 }
fishonamos commented 3 months ago

All tests have passed.

TAdev0 commented 3 months ago

lgtm!

TAdev0 commented 3 months ago

well we should probably have used

        if err != nil {
                        t.Fatal(err)
                    }

in unit tests, but anyway it doesnt matter, and i'm pushing soon a pr that will update this