diff options
author | Ali Mohammad Pur <ali.mpfard@gmail.com> | 2021-05-04 07:36:59 +0430 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2021-05-21 00:15:23 +0100 |
commit | b3c13c3e8ae4e65310714283980dd42b2da30be8 (patch) | |
tree | 79bbcd193b255d3dc124d7f91982e2bfa51765ce /Tests | |
parent | ba2fce14d31e134901898bbf235771c5b6a7fb01 (diff) | |
download | serenity-b3c13c3e8ae4e65310714283980dd42b2da30be8.zip |
LibWasm+Meta: Add test-wasm and optionally test the conformance tests
This only tests "can it be parsed", but the goal of this commit is to
provide a test framework that can be built upon :)
The conformance tests are downloaded, compiled* and installed only if
the INCLUDE_WASM_SPEC_TESTS cmake option is enabled.
(*) Since we do not yet have a wast parser, the compilation is delegated
to an external tool from binaryen, `wasm-as`, which is required for the
test suite download/install to succeed.
This *does* run the tests in CI, but it currently does not include the
spec conformance tests.
Diffstat (limited to 'Tests')
-rw-r--r-- | Tests/CMakeLists.txt | 1 | ||||
-rw-r--r-- | Tests/LibWasm/CMakeLists.txt | 2 | ||||
-rw-r--r-- | Tests/LibWasm/test-wasm.cpp | 70 |
3 files changed, 73 insertions, 0 deletions
diff --git a/Tests/CMakeLists.txt b/Tests/CMakeLists.txt index 514bfb1a68..855bb5d531 100644 --- a/Tests/CMakeLists.txt +++ b/Tests/CMakeLists.txt @@ -11,5 +11,6 @@ add_subdirectory(LibM) add_subdirectory(LibPthread) add_subdirectory(LibRegex) add_subdirectory(LibSQL) +add_subdirectory(LibWasm) add_subdirectory(LibWeb) add_subdirectory(UserspaceEmulator) diff --git a/Tests/LibWasm/CMakeLists.txt b/Tests/LibWasm/CMakeLists.txt new file mode 100644 index 0000000000..0f16647d2e --- /dev/null +++ b/Tests/LibWasm/CMakeLists.txt @@ -0,0 +1,2 @@ +serenity_testjs_test(test-wasm.cpp test-wasm LIBS LibWasm) +install(TARGETS test-wasm RUNTIME DESTINATION bin) diff --git a/Tests/LibWasm/test-wasm.cpp b/Tests/LibWasm/test-wasm.cpp new file mode 100644 index 0000000000..c8f48a3002 --- /dev/null +++ b/Tests/LibWasm/test-wasm.cpp @@ -0,0 +1,70 @@ +/* + * Copyright (c) 2021, Ali Mohammad Pur <mpfard@serenityos.org> + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include <LibCore/File.h> +#include <LibTest/JavaScriptTestRunner.h> +#include <LibWasm/AbstractMachine/Interpreter.h> +#include <LibWasm/Types.h> + +TEST_ROOT("Userland/Libraries/LibWasm/Tests"); + +TESTJS_GLOBAL_FUNCTION(read_binary_wasm_file, readBinaryWasmFile) +{ + auto filename = vm.argument(0).to_string(global_object); + if (vm.exception()) + return {}; + auto file = Core::File::open(filename, Core::OpenMode::ReadOnly); + if (file.is_error()) { + vm.throw_exception<JS::TypeError>(global_object, file.error()); + return {}; + } + auto contents = file.value()->read_all(); + auto array = JS::Uint8Array::create(global_object, contents.size()); + contents.span().copy_to(array->data()); + return array; +} + +TESTJS_GLOBAL_FUNCTION(parse_webassembly_module, parseWebAssemblyModule) +{ + auto object = vm.argument(0).to_object(global_object); + if (vm.exception()) + return {}; + if (!is<JS::Uint8Array>(object)) { + vm.throw_exception<JS::TypeError>(global_object, "Expected a Uint8Array argument to parse_webassembly_module"); + return {}; + } + auto& array = static_cast<JS::Uint8Array&>(*object); + InputMemoryStream stream { array.data() }; + auto result = Wasm::Module::parse(stream); + if (result.is_error()) { + vm.throw_exception<JS::SyntaxError>(global_object, Wasm::parse_error_to_string(result.error())); + return {}; + } + if (stream.handle_any_error()) + return JS::js_undefined(); + return JS::js_null(); +} + +TESTJS_GLOBAL_FUNCTION(compare_typed_arrays, compareTypedArrays) +{ + auto lhs = vm.argument(0).to_object(global_object); + if (vm.exception()) + return {}; + if (!is<JS::TypedArrayBase>(lhs)) { + vm.throw_exception<JS::TypeError>(global_object, "Expected a TypedArray"); + return {}; + } + auto& lhs_array = static_cast<JS::TypedArrayBase&>(*lhs); + auto rhs = vm.argument(1).to_object(global_object); + if (vm.exception()) + return {}; + if (!is<JS::TypedArrayBase>(rhs)) { + vm.throw_exception<JS::TypeError>(global_object, "Expected a TypedArray"); + return {}; + } + auto& rhs_array = static_cast<JS::TypedArrayBase&>(*rhs); + return JS::Value(lhs_array.viewed_array_buffer()->buffer() == rhs_array.viewed_array_buffer()->buffer()); +} |