From 1acfabe2e763c0e8edef113a3fe16d563365e9b0 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Wed, 25 Mar 2026 22:44:43 +0300 Subject: [PATCH] examples: update wasm_codegen for current wasmer (fixes #22607) --- examples/wasm_codegen/add.v | 13 ------ examples/wasm_codegen/factorial.v | 33 -------------- examples/wasm_codegen/functions.v | 29 ------------- examples/wasm_codegen/main.v | 71 +++++++++++++++++++++++++++++++ 4 files changed, 71 insertions(+), 75 deletions(-) delete mode 100644 examples/wasm_codegen/add.v delete mode 100644 examples/wasm_codegen/factorial.v delete mode 100644 examples/wasm_codegen/functions.v create mode 100644 examples/wasm_codegen/main.v diff --git a/examples/wasm_codegen/add.v b/examples/wasm_codegen/add.v deleted file mode 100644 index 09e2ae429..000000000 --- a/examples/wasm_codegen/add.v +++ /dev/null @@ -1,13 +0,0 @@ -import wasm - -fn main() { - mut m := wasm.Module{} - mut func := m.new_function('add', [.i32_t, .i32_t], [.i32_t]) - { - func.local_get(0) - func.local_get(1) - func.add(.i32_t) - } - m.commit(func, true) // `export: true` - print(m.compile().bytestr()) -} diff --git a/examples/wasm_codegen/factorial.v b/examples/wasm_codegen/factorial.v deleted file mode 100644 index 41c3c372e..000000000 --- a/examples/wasm_codegen/factorial.v +++ /dev/null @@ -1,33 +0,0 @@ -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) - bif := fac.c_if([], [.i64_t]) - { - fac.i64_const(1) - } - fac.c_else(bif) - { - { - 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(bif) - } - m.commit(fac, true) - print(m.compile().bytestr()) - - // v run factorial.v > a.wasm - // wasmer a.wasm -i fac 5 -} diff --git a/examples/wasm_codegen/functions.v b/examples/wasm_codegen/functions.v deleted file mode 100644 index 478040fe8..000000000 --- a/examples/wasm_codegen/functions.v +++ /dev/null @@ -1,29 +0,0 @@ -import wasm - -fn main() { - mut m := wasm.Module{} - mut pyth := m.new_function('pythagoras', [.f32_t, .f32_t], [ - .f32_t, - ]) - { - pyth.local_get(0) - pyth.local_get(0) - pyth.mul(.f32_t) - pyth.local_get(1) - pyth.local_get(1) - pyth.mul(.f32_t) - pyth.add(.f32_t) - pyth.sqrt(.f32_t) - pyth.cast(.f32_t, true, .f64_t) - } - m.commit(pyth, true) - mut test := m.new_function('test', [.f32_t], [.f64_t]) - { - test.local_get(0) - test.f32_const(10.0) - test.call('pythagoras') - test.cast(.f32_t, true, .f64_t) - } - m.commit(test, true) - print(m.compile().bytestr()) -} diff --git a/examples/wasm_codegen/main.v b/examples/wasm_codegen/main.v new file mode 100644 index 000000000..0808be492 --- /dev/null +++ b/examples/wasm_codegen/main.v @@ -0,0 +1,71 @@ +import os +import wasm + +// Install Wasmer: +// $ curl https://get.wasmer.io -sSfL | sh +// $ wasmer --version +// +// Create `examples/wasm_codegen/math.wasm`: +// $ v run examples/wasm_codegen/main.v +// +// Call entrypoints with the current Wasmer CLI: +// $ wasmer run examples/wasm_codegen/math.wasm --invoke add 3 5 +// 8 +// $ wasmer run examples/wasm_codegen/math.wasm --invoke factorial 10 +// 3628800 +// $ wasmer run examples/wasm_codegen/math.wasm --invoke pythagoras 30 40 +// 50 +fn main() { + mut math := Math{} + math.function_add('add') + math.function_factorial('factorial') + math.function_pythagoras('pythagoras') + math.save(os.join_path(@DIR, 'math.wasm'))! +} + +struct Math { + wasm.Module +} + +fn (mut math Math) function_add(name string) { + mut func := math.new_function(name, [.i32_t, .i32_t], [.i32_t]) + func.local_get(0) + func.local_get(1) + func.add(.i32_t) + math.commit(func, true) +} + +fn (mut math Math) function_factorial(name string) { + mut func := math.new_function(name, [.i64_t], [.i64_t]) + func.local_get(0) + func.eqz(.i64_t) + block := func.c_if([], [.i64_t]) + func.i64_const(1) + func.c_else(block) + func.local_get(0) + func.local_get(0) + func.i64_const(1) + func.sub(.i64_t) + func.call(name) + func.mul(.i64_t) + func.c_end(block) + math.commit(func, true) +} + +fn (mut math Math) function_pythagoras(name string) { + mut func := math.new_function(name, [.f32_t, .f32_t], [.f64_t]) + func.local_get(0) + func.local_get(0) + func.mul(.f32_t) + func.local_get(1) + func.local_get(1) + func.mul(.f32_t) + func.add(.f32_t) + func.sqrt(.f32_t) + func.cast(.f32_t, true, .f64_t) + math.commit(func, true) +} + +fn (mut math Math) save(path string) ! { + os.write_file_array(path, math.compile())! +} -- 2.39.5