Skip to content

CLI: new `-m` parameter for module selection

E. Rivas requested to merge er433/cli/module_parameter into dev

Motivation and Context

In certain cases, it can be handy to compile not an entrypoint in top-level, but instead an entrypoint inside a module. In particular, this can be useful in the setting of !2379 (merged) .

Description

This MR enables the usage of a module_path for compiling modules. E.g.

$ cat review/inside.mligo 
type storage = int

type parameter =
  Increment of int
| Decrement of int
| Reset

// Two entrypoints

let add (store : storage) (delta : int) = store + delta
let sub (store : storage) (delta : int) = store - delta

module Foo = struct
  let main (action : parameter) (store : storage) : operation list * storage =
   [],    // No operations
   (match action with
     Increment (n) -> add store n
   | Decrement (n) -> sub store n
   | Reset         -> 0)
  [@view] let foo (i : int) (store : storage) : int = i + store
  let bar (i : int) (store : storage) : int = 1 + i + store
end

[@view] let bar (i : int) (store : storage) : int = 1 + i + store

module X = Foo

module Bar = struct
  module Foo = X
  module C = Foo
end

module C = Bar.C

let kkmain = C.main

(* Tests for main access point *)

let initial_storage = 42

let test_initial_storage =
 let (taddr, _, _) = Test.originate C.main initial_storage 0tez in
 assert (Test.get_storage taddr = initial_storage)

let test_increment =
 let (taddr, _, _) = Test.originate C.main initial_storage 0tez in
 let contr = Test.to_contract taddr in
 let _ = Test.transfer_to_contract_exn contr (Increment 1) 1mutez in
 assert (Test.get_storage taddr = initial_storage + 1)

$ ligo compile contract review/inside.mligo -m Bar.C
{ parameter (or (or (int %decrement) (int %increment)) (unit %reset)) ;
  storage int ;
  code { UNPAIR ;
         IF_LEFT { IF_LEFT { SWAP ; SUB } { ADD } } { DROP 2 ; PUSH int 0 } ;
         NIL operation ;
         PAIR } ;
  view "foo" int int { UNPAIR ; ADD } }

Component

  • compiler
  • website
  • webide
  • vscode-plugin
  • debugger

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Performance improvement (non-breaking change that improves performance)
  • None (change with no changelog)

Changelog

A new argument is available when compiling a contract: -m, which allows to select a module path where the entrypoint (and views) is located.

Example file increment.jsligo

type storage = int;

type parameter =
  ["Increment", int]
| ["Decrement", int]
| ["Reset"];

type ret = [list<operation>, storage];

const add = (store : storage, delta : int) : storage => store + delta;
const sub = (store : storage, delta : int) : storage => store - delta;

namespace C {

  const main = (action : parameter, store : storage) : ret => {
   return [list([]) as list<operation>,
   match (action, {
    Increment:(n: int) => add (store, n),
    Decrement:(n: int) => sub (store, n),
    Reset    :()  => 0})]
  };

}

Compilation using -m

$ ligo compile contract increment.jsligo -m C
{ parameter (or (or (int %decrement) (int %increment)) (unit %reset)) ;
  storage int ;
  code { UNPAIR ;
         IF_LEFT { IF_LEFT { SWAP ; SUB } { ADD } } { DROP 2 ; PUSH int 0 } ;
         NIL operation ;
         PAIR } }

Checklist:

  • Changes follow the existing coding style (use dune @fmt to check).
  • Tests for the changes have been added (for bug fixes / feature).
  • Documentation has been updated.
  • Changelog description has been added (if appropriate).
  • Start titles under ## Changelog section with #### (if appropriate).
  • There is no image or uploaded file in changelog
  • Examples in changed behaviour have been added to the changelog (for breaking change / feature).
Edited by E. Rivas

Merge request reports