| 1 | module main |
| 2 | |
| 3 | import os |
| 4 | |
| 5 | const pid = os.getpid() |
| 6 | |
| 7 | const 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 | |
| 12 | fn 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. |
| 29 | pub 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 | |