Language reference · section 22

22. WebAssembly (@wasm)

@wasm compiles a function to WebAssembly. The subset is numeric. The types are int (32-bit integer), float (64-bit float), and boolean. A numeric literal with a . is a float. Any other numeric literal is an int.

Feature: WebAssembly

  Scenario: @wasm compiles a function to WebAssembly
    Given a function with @wasm on the line before fn
    Then the compiler emits the function as WebAssembly

  Scenario: Arithmetic needs int or float operands
    Given a @wasm function that adds two booleans
    Then the compiler reports an error "arithmetic needs int or float operands"

  Scenario: Both operands must have the same type
    Given a @wasm function that adds an int and a float
    Then the compiler reports an error "operands must have the same type, got int and float"

  Scenario: % needs int operands
    Given a @wasm function that uses % on float values
    Then the compiler reports an error "% needs int operands"

  Scenario: Ordering needs int or float operands
    Given a @wasm function that compares two booleans with <
    Then the compiler reports an error "ordering needs int or float operands"

  Scenario: and and or need boolean operands
    Given a @wasm function that uses and on ints
    Then the compiler reports an error "and/or need boolean operands"

  Scenario: ** is not supported in wasm
    Given a @wasm function that uses **
    Then the compiler reports an error "** is not supported in @wasm functions"

  Scenario: ?? and in are not supported in wasm
    Given a @wasm function that uses ?? or in
    Then the compiler reports an error

  Scenario: Only calls by name to other @wasm functions are allowed
    Given a @wasm function that calls a function g that is not @wasm
    Then the compiler reports an error "`g` is not a @wasm function; @wasm code can only call other @wasm functions"

  Scenario: A name must be a parameter, a local binding, or a @wasm function
    Given a @wasm function that reads an unknown name n
    Then the compiler reports an error "unknown name `n`; @wasm functions can only use parameters, local bindings, and other @wasm functions"

  Scenario: Strings, arrays, and objects are not allowed in wasm
    Given a @wasm function that uses a string
    Then the compiler reports an error "strings is not supported in @wasm functions"

  Scenario: Loops over collections are not allowed in wasm
    Given a @wasm function with a for loop over a collection
    Then the compiler reports an error "loops over collections are not supported yet; use while"

  Scenario: A nested function is not allowed in wasm
    Given a @wasm function that declares a nested function g
    Then the compiler reports an error "nested function g is not allowed"

  Scenario: A return value must match the return type
    Given a @wasm function that returns an int where boolean is declared
    Then the compiler reports an error "expected boolean but got int"

  Scenario: A wasm function is imported like any other function
    Given the code 'import { fib } from "./math.pin"'
    Then fib runs in WebAssembly
@wasm
fn fib(n: int): int:
  if n < 2:
    return n

  return fib(n - 1) + fib(n - 2)