1
0
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:
l-m
2023-04-09 14:55:02 +10:00
committed by GitHub
parent 9658d20f03
commit d2f69472b2
14 changed files with 1966 additions and 0 deletions

View File

@@ -0,0 +1,56 @@
import wasm
import os
const exe = os.find_abs_path_of_executable('wasm-validate') or { exit(0) }
fn validate(mod []u8) ! {
mut proc := os.new_process(exe)
proc.set_args(['-'])
proc.set_redirect_stdio()
proc.run()
{
os.fd_write(proc.stdio_fd[0], mod.bytestr())
os.fd_close(proc.stdio_fd[0])
}
proc.wait()
if proc.status != .exited {
return error('wasm-validate exited abormally')
}
if proc.code != 0 {
return error('wasm-validate exited with a non zero exit code')
}
proc.close()
}
fn test_add() {
mut m := wasm.Module{}
mut a1 := m.new_function('add', [.i32_t, .i32_t], [.i32_t])
{
a1.local_get(0)
a1.local_get(1)
a1.add(.i32_t)
}
m.commit(a1, true)
mut a2 := m.new_function('sub', [.i32_t, .i32_t], [.i32_t])
{
a2.local_get(0)
a2.local_get(1)
a2.sub(.i32_t)
}
m.commit(a2, true)
mut a3 := m.new_function('mul', [.i32_t, .i32_t], [.i32_t])
{
a3.local_get(0)
a3.local_get(1)
a3.mul(.i32_t)
}
m.commit(a3, true)
mut a4 := m.new_function('div', [.i32_t, .i32_t], [.i32_t])
{
a4.local_get(0)
a4.local_get(1)
a4.div(.i32_t, true)
}
m.commit(a4, true)
validate(m.compile()) or { panic(err) }
}

View File

@@ -0,0 +1,143 @@
import wasm
import os
const exe = os.find_abs_path_of_executable('wasm-validate') or { exit(0) }
fn validate(mod []u8) ! {
mut proc := os.new_process(exe)
proc.set_args(['-'])
proc.set_redirect_stdio()
proc.run()
{
os.fd_write(proc.stdio_fd[0], mod.bytestr())
os.fd_close(proc.stdio_fd[0])
}
proc.wait()
if proc.status != .exited {
return error('wasm-validate exited abormally')
}
if proc.code != 0 {
return error('wasm-validate exited with a non zero exit code')
}
proc.close()
}
fn test_add() {
mut m := wasm.Module{}
mut a1 := m.new_function('param', [], [.i32_t])
{
a1.i32_const(1)
blk := a1.c_block([.i32_t], [.i32_t])
{
a1.i32_const(2)
a1.add(.i32_t)
}
a1.c_end(blk)
}
m.commit(a1, true)
mut a2 := m.new_function('params', [], [.i32_t])
{
a2.i32_const(1)
a2.i32_const(2)
blk := a2.c_block([.i32_t, .i32_t], [.i32_t])
{
a2.add(.i32_t)
}
a2.c_end(blk)
}
m.commit(a2, true)
mut a3 := m.new_function('params-id', [], [.i32_t])
{
a3.i32_const(1)
a3.i32_const(2)
blk := a3.c_block([.i32_t, .i32_t], [.i32_t, .i32_t])
{
}
a3.c_end(blk)
a3.add(.i32_t)
}
m.commit(a3, true)
mut b1 := m.new_function('param-break', [], [.i32_t])
{
b1.i32_const(1)
blk := b1.c_block([.i32_t], [.i32_t])
{
b1.i32_const(2)
b1.add(.i32_t)
b1.c_br(blk)
}
b1.c_end(blk)
}
m.commit(b1, true)
mut b2 := m.new_function('params-break', [], [.i32_t])
{
b2.i32_const(1)
b2.i32_const(2)
blk := b2.c_block([.i32_t, .i32_t], [.i32_t])
{
b2.add(.i32_t)
b2.c_br(blk)
}
b2.c_end(blk)
}
m.commit(b2, true)
mut b3 := m.new_function('params-id-break', [], [.i32_t])
{
b3.i32_const(1)
b3.i32_const(2)
blk := b3.c_block([.i32_t, .i32_t], [.i32_t, .i32_t])
{
b3.c_br(blk)
}
b3.c_end(blk)
b3.add(.i32_t)
}
m.commit(b3, true)
mut dummy := m.new_function('dummy', [], [])
{
}
m.commit(dummy, false)
mut c1 := m.new_function('singular', [], [.i32_t])
{
blk1 := c1.c_block([], [])
{
c1.nop()
}
c1.c_end(blk1)
blk2 := c1.c_block([], [.i32_t])
{
c1.i32_const(7)
}
c1.c_end(blk2)
}
m.commit(c1, true)
mut c2 := m.new_function('nested', [], [.i32_t])
{
blk := c2.c_block([], [.i32_t])
{
blk1 := c2.c_block([], [])
{
c2.call('dummy')
blk2 := c2.c_block([], [])
{
}
c2.c_end(blk2)
c2.nop()
}
c2.c_end(blk1)
blk2 := c2.c_block([], [.i32_t])
{
c2.call('dummy')
c2.i32_const(9)
}
c2.c_end(blk2)
}
c2.c_end(blk)
}
m.commit(c2, true)
validate(m.compile()) or { panic(err) }
}

106
vlib/wasm/tests/call_test.v Normal file
View File

@@ -0,0 +1,106 @@
import wasm
import os
const exe = os.find_abs_path_of_executable('wasm-validate') or { exit(0) }
fn validate(mod []u8) ! {
mut proc := os.new_process(exe)
proc.set_args(['-'])
proc.set_redirect_stdio()
proc.run()
{
os.fd_write(proc.stdio_fd[0], mod.bytestr())
os.fd_close(proc.stdio_fd[0])
}
proc.wait()
if proc.status != .exited {
return error('wasm-validate exited abormally')
}
if proc.code != 0 {
return error('wasm-validate exited with a non zero exit code')
}
proc.close()
}
fn test_add() {
mut m := wasm.Module{}
mut a1 := m.new_function('const-i32', [], [.i32_t])
{
a1.i32_const(0x132)
}
m.commit(a1, false)
mut a2 := m.new_function('const-i64', [], [.i64_t])
{
a2.i64_const(0x164)
}
m.commit(a2, false)
mut a3 := m.new_function('const-f32', [], [.f32_t])
{
a3.f32_const(0.2)
}
m.commit(a3, false)
mut a4 := m.new_function('const-f64', [], [.f64_t])
{
a4.f64_const(0.4)
}
m.commit(a4, false)
mut a5 := m.new_function('const-i32-i64', [], [.i32_t, .i64_t])
{
a5.i32_const(0x132)
a5.i64_const(0x164)
}
m.commit(a5, false)
mut b1 := m.new_function('type-i32', [], [.i32_t])
{
b1.call('const-i32')
}
m.commit(b1, true)
mut b2 := m.new_function('type-i64', [], [.i64_t])
{
b2.call('const-i64')
}
m.commit(b2, true)
mut b3 := m.new_function('type-f32', [], [.f32_t])
{
b3.call('const-f32')
}
m.commit(b3, true)
mut b4 := m.new_function('type-f64', [], [.f64_t])
{
b4.call('const-f64')
}
m.commit(b4, true)
mut b5 := m.new_function('type-i32-i64', [], [.i32_t, .i64_t])
{
b5.call('const-i32-i64')
}
m.commit(b5, true)
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)
validate(m.compile()) or { panic(err) }
}