v2 / vlib / wasm / tests / common.v
40 lines · 35 sloc · 1.47 KB · e2e5cf8db56f3562c7baa735061690be936bdf3e
Raw
1module main
2
3import os
4
5const pid = os.getpid()
6
7const wasm_validate_exe = find_wasm_validate() or {
8 println('>>> Skipping test, since wasm-validate could not be found, error: ${err}')
9 exit(0)
10}
11
12fn find_wasm_validate() !string {
13 // Prefer to find our own version first, if it was installed already
14 // through install_wabt.vsh, since it is more likely to be known, recent, and stable:
15 thirdpart_wasm_validate_folder := os.join_path(@VEXEROOT, 'thirdparty', 'wabt', 'bin')
16 extension := $if windows { '.exe' } $else { '' }
17 wasm_validate_executable := os.join_path(thirdpart_wasm_validate_folder,
18 'wasm-validate${extension}')
19 if os.exists(wasm_validate_executable) {
20 return wasm_validate_executable
21 }
22 if path := os.find_abs_path_of_executable('wasm-validate') {
23 return path
24 }
25 return error('could not find wasm-validate executable in thirdparty/ as well, try first `v run cmd/tools/install_wabt.vsh`')
26}
27
28// validate validates the given wasm code using `wasm-validate` executable.
29pub fn validate(code []u8) ! {
30 println('validating using: ${wasm_validate_exe}')
31 outfile := os.join_path(os.temp_dir(), 'code_${pid}.wasm')
32 os.write_file(outfile, code.bytestr())!
33 validation_cmd := '${os.quoted_path(wasm_validate_exe)} ${os.quoted_path(outfile)}'
34 res := os.execute(validation_cmd)
35 if res.exit_code != 0 {
36 eprintln('failed exit code: ${res.exit_code} | command:\n${validation_cmd}')
37 return error('wasm-validate exited with a non zero exit code: ${res.exit_code}')
38 }
39 os.rm(outfile)!
40}
41