mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
vlib: add a wasm module, implementing a pure V webassembly seralisation library (#17909)
This commit is contained in:
33
examples/wasm_codegen/factorial.v
Normal file
33
examples/wasm_codegen/factorial.v
Normal file
@ -0,0 +1,33 @@
|
||||
import wasm
|
||||
|
||||
fn main() {
|
||||
mut m := wasm.Module{}
|
||||
mut fac := m.new_function('fac', [.i64_t], [.i64_t])
|
||||
{
|
||||
fac.local_get(0)
|
||||
fac.eqz(.i64_t)
|
||||
fac.c_if([], [.i64_t])
|
||||
{
|
||||
fac.i64_const(1)
|
||||
}
|
||||
fac.c_else()
|
||||
{
|
||||
{
|
||||
fac.local_get(0)
|
||||
}
|
||||
{
|
||||
fac.local_get(0)
|
||||
fac.i64_const(1)
|
||||
fac.sub(.i64_t)
|
||||
fac.call('fac')
|
||||
}
|
||||
fac.mul(.i64_t)
|
||||
}
|
||||
fac.c_end_if()
|
||||
}
|
||||
m.commit(fac, true)
|
||||
print(m.compile().bytestr())
|
||||
|
||||
// v run factorial.v > a.wasm
|
||||
// wasmer a.wasm -i fac 5
|
||||
}
|
Reference in New Issue
Block a user