Imports in WebAssembly
The module states its needs; the host supplies them or no deal.
(module
;; Imports are the module's stated needs: the host supplies this
;; function at instantiation or the module refuses to start.
(import "env" "log_i32" (func $log (param i32)))
(global $reports (mut i32) (i32.const 0))
(func (export "report") (param $reading i32)
(global.set $reports
(i32.add (global.get $reports) (i32.const 1)))
(call $log (local.get $reading))
(call $log (global.get $reports))))
How it works
(import "env" "log_i32" (func $log ...))is a two-level name.- The import binds to
$logand calls like any local function. - Instantiation fails outright if the host offers nothing matching.
Keywords and builtins used here
callexportfuncglobali32importmodulemutparam
The run, in numbers
- Lines
- 12
- Characters to type
- 408
- Tokens
- 79
- Three-star pace
- 65 tpm
At the three-star pace of 65 tokens a minute, this run takes about 73 seconds.
Step 3 of 3 in Globals & startup, step 22 of 28 in Language basics.