Language reference · section 24

24. Workers

Feature: Workers

  Scenario: A worker function runs in a Web Worker
    Given a function with @worker
    When the caller runs it
    Then the call runs in a Web Worker and returns a Promise

  Scenario: Arguments and results are copied with the structured clone algorithm
    Given a @worker call with an object argument
    Then the worker receives a copy of the object

  Scenario: Worker arguments must be an array
    Given a @worker call whose arguments are not an array
    Then the compiler reports an error "worker arguments must be an array"

  Scenario: A pool sets the number of workers
    Given the decorator @worker(pool: N)
    When N is a whole number from 1 to 32
    Then the module can run up to N workers at once

  Scenario: A pool size outside 1 to 32 is an error
    Given the decorator @worker(pool: 40)
    Then the compiler reports an error "pool must be a whole number from 1 to 32, not 40"

  Scenario: pool is given once
    Given the decorator @worker(pool: 2, pool: 3)
    Then the compiler reports an error "`pool` is given more than once"

  Scenario: Every worker function in one file has the same pool size
    Given two @worker functions in one file with different pool sizes
    Then the compiler reports an error "every @worker function in one file must use the same pool size"

  Scenario: A call goes to an idle worker first
    Given a pool with an idle worker
    When a call arrives
    Then the call goes to the idle worker

  Scenario: A new worker starts when the pool has room
    Given a pool with no idle worker and room for another worker
    When a call arrives
    Then a new worker starts for the call

  Scenario: A call joins the least busy worker when the pool is full
    Given a full pool with no idle worker
    When a call arrives
    Then the call joins the least busy worker

  Scenario: A crashed worker fails only its own calls
    Given a pool where one worker crashes
    Then only the calls on that worker reject, and the next call that needs a worker starts a new one

  Scenario: A thrown error keeps its class and message
    Given a @worker function that throws a RangeError with a message
    When the caller awaits the call
    Then the caller gets a RangeError with the same message

  Scenario: A thrown value that is not an error becomes an Error
    Given a @worker function that throws the text "bad input"
    When the caller awaits the call
    Then the caller gets an Error whose message is "bad input"

  Scenario: A @worker function cannot take a mut parameter
    Given a @worker function with a mut parameter
    Then the compiler reports an error "`mut` parameters are not allowed on @worker functions"