66 lines
2.5 KiB
WebAssembly Text Format
66 lines
2.5 KiB
WebAssembly Text Format
(module
|
|
(type $heap-object (sub (struct (field (mut i32)))))
|
|
(type $open-procedure (func (param i32)))
|
|
(type $closure (sub $heap-object
|
|
(struct (field (mut i32))
|
|
(field (ref $open-procedure)))))
|
|
(type $cont-stack-type (array (mut (ref null $open-procedure))))
|
|
(type $arg-array-type (array (mut eqref)))
|
|
(type (func (result (ref eq))))
|
|
(global $cont-stack-top (mut i32) (i32.const 0))
|
|
(global $cont-stack (ref $cont-stack-type)
|
|
(array.new_default $cont-stack-type (i32.const 128)))
|
|
(global $arg-array (ref $arg-array-type)
|
|
(array.new_default $arg-array-type (i32.const 32)))
|
|
(global (mut eqref) (ref.null eq))
|
|
(elem declare funcref (ref.func 1))
|
|
(func $halt (param i32)
|
|
(local (ref eq) (ref eq) (ref eq) (ref eq) (ref eq))
|
|
(global.set 3
|
|
(ref.as_non_null
|
|
(array.get $arg-array-type
|
|
(global.get $arg-array)
|
|
(i32.const 0)))))
|
|
(func $f1 (param i32)
|
|
(local (ref eq) (ref eq) (ref eq) (ref eq) (ref eq))
|
|
;; pop arg 0
|
|
(local.set
|
|
1
|
|
(ref.as_non_null
|
|
(array.get $arg-array-type
|
|
(global.get $arg-array)
|
|
(i32.const 0))))
|
|
;; push arg 0
|
|
(array.set $arg-array-type
|
|
(global.get $arg-array)
|
|
(i32.const 0)
|
|
(local.get 1))
|
|
;; pop continuation
|
|
(return_call_ref
|
|
$open-procedure
|
|
(i32.const 1)
|
|
(ref.as_non_null (array.get $cont-stack-type
|
|
(global.get $cont-stack)
|
|
(global.get $cont-stack-top)))
|
|
(global.set $cont-stack-top
|
|
(i32.sub
|
|
(global.get $cont-stack-top)
|
|
(i32.const 1)))))
|
|
(func $f2 (type $open-procedure) (param i32)
|
|
(local (ref eq) (ref eq) (ref eq) (ref eq) (ref eq))
|
|
(local.set 1
|
|
(struct.new $closure
|
|
(i32.const 0)
|
|
(ref.func $f1)))
|
|
(array.set $arg-array-type
|
|
(global.get $arg-array)
|
|
(i32.const 0)
|
|
(local.get 1))
|
|
(i32.const 1)
|
|
(return_call $f1))
|
|
(func $main (export "main") (result (ref eq))
|
|
(local (ref eq) (ref eq) (ref eq) (ref eq) (ref eq))
|
|
(call $f2 (i32.const 0))
|
|
(ref.as_non_null
|
|
(global.get 3))))
|