The start function in WebAssembly
Initialization that runs before any export can be called.
(module
;; The start function runs once at instantiation, before any
;; export can be called -- initialization without a host contract.
(global $seeded (mut i32) (i32.const 0))
(memory 1)
(func $init
(i32.store (i32.const 0) (i32.const 1408))
(global.set $seeded (i32.const 1)))
(start $init)
(func (export "elevation") (result i32)
(i32.load (i32.const 0)))
(func (export "ready") (result i32)
(global.get $seeded)))
How it works
(start $init)nominates one function to run at instantiation.- It seeds memory and flips a (mut i32) flag.
- By the time the host calls
elevation, the store already happened.
Keywords and builtins used here
exportfuncglobali32memorymodulemutresultstart
The run, in numbers
- Lines
- 15
- Characters to type
- 419
- Tokens
- 85
- Three-star pace
- 65 tpm
At the three-star pace of 65 tokens a minute, this run takes about 78 seconds.
Step 2 of 3 in Globals & startup, step 21 of 28 in Language basics.