Language reference · section 11

11. Shared bindings in worker files

A file that uses @wasm or @worker has two modules. The implementation module declares every top-level name once. The main module reads those names.

Feature: Shared bindings

  Scenario: Main-thread code reads a shared binding
    Given a shared binding total
    When main-thread code reads total
    Then the code gets the current value

  Scenario: Main-thread code cannot change a shared binding
    Given a shared binding total
    When main-thread code assigns total
    Then the compiler reports an error "`total` is a top-level binding shared with the implementation module and cannot be changed from main-thread code; move the change into a function"

  Scenario: A function can change a shared mut binding through a mut parameter
    Given a shared mut binding total
    When a function in the implementation module passes total to a mut parameter
    Then the function changes total

  Scenario: A file with a pool cannot declare a top-level mut binding
    Given a file with @worker(pool: 2) and a top-level "mut q = 1"
    Then the compiler reports an error "`q` is mutable top-level state, and each worker in a pool keeps its own copy; make it immutable, or remove `pool`"