Language reference · section 25

25. Transfer

transfer(value) moves an ArrayBuffer or a typed array to a worker, or back from one. The sender's copy is detached. Its length becomes 0.

Feature: Transfer

  Scenario: transfer moves a buffer to a worker
    Given the code "fill(transfer(buf))" with a @worker function fill
    When the call runs
    Then the worker receives buf and the caller's copy of buf has length 0

  Scenario: transfer needs a buffer
    Given the code "transfer(1)" as a direct argument of a @worker call
    Then the compiler reports an error "transfer needs an ArrayBuffer or a typed array"

  Scenario: transfer must be a direct argument or a direct result
    Given transfer used inside another value in a @worker call
    Then the compiler reports an error "transfer(...) must be a direct argument or result of a @worker call, not nested in another value"

  Scenario: transfer outside a @worker call is passed through
    Given transfer used in an ordinary expression
    Then the value is passed through unchanged

  Scenario: A program can define its own transfer function
    Given a program that defines transfer itself
    Then the program keeps its own function
@worker
fn fill(buf: Uint8Array): Uint8Array:
  return transfer(buf)

buf = new Uint8Array(1024)

fill(transfer(buf)) # buf is now detached