Add support for basic assertions

This commit is contained in:
Rerumu 2022-05-26 21:00:52 -04:00
parent 08d2108a21
commit 7577135976
2 changed files with 85 additions and 7 deletions

View File

@ -0,0 +1,74 @@
local loaded = {}
local linked = {}
local LUA_NAN_ARITHMETIC = 0 / 0
local LUA_NAN_CANONICAL = 0 / 0
local LUA_NAN_DEFAULT = 0 / 0
local function is_number_equality(lhs, rhs)
if type(lhs) ~= "number" or type(rhs) ~= "number" then
return false
elseif lhs ~= lhs and rhs ~= rhs then
return lhs ~= rhs
end
return math.abs(lhs - rhs) < 0.000001
end
local function assert_eq(lhs, rhs, message, level)
if lhs == rhs or is_number_equality(lhs, rhs) then
return
end
if message then
message = ": " .. message
else
message = ""
end
lhs = tostring(lhs)
rhs = tostring(rhs)
level = (level or 1) + 1
error(lhs .. " ~= " .. rhs .. message, level)
end
local function assert_neq(lhs, rhs, message, level)
if lhs ~= rhs and not is_number_equality(lhs, rhs) then
return
end
if message then
message = ": " .. message
else
message = ""
end
lhs = tostring(lhs)
rhs = tostring(rhs)
level = (level or 1) + 1
error(lhs .. " == " .. rhs .. message, level)
end
local function raw_invoke(func, ...)
return func(...)
end
local function assert_trap(func, ...)
if pcall(func, ...) then
error("Failed to trap", 2)
end
end
local function assert_return(data, wanted)
for i, v in ipairs(data) do
assert_eq(v, wanted[i], "Result mismatch at " .. i, 2)
end
end
local function assert_exhaustion(func, ...)
if pcall(func, ...) then
error("Failed to exhaust", 2)
end
end

View File

@ -17,6 +17,8 @@ use wast::{
use wasm_ast::builder::TypeInfo;
static ASSERTION: &str = include_str!("assertion.lua");
macro_rules! write_assert_number {
($name:ident, $generic:ty, $reader:ty) => {
fn $name(data: &NanPattern<$generic>, w: &mut dyn Write) -> IResult<()> {
@ -199,9 +201,8 @@ impl Target for LuaJIT {
fn write_runtime(w: &mut dyn Write) -> IResult<()> {
let runtime = codegen_luajit::RUNTIME;
writeln!(w, "local rt = (function() {runtime} end)()")?;
writeln!(w, "local loaded = {{}}")?;
writeln!(w, "local linked = {{}}")
writeln!(w, "{ASSERTION}")?;
writeln!(w, "local rt = (function() {runtime} end)()")
}
fn write_module(typed: &TypedModule, w: &mut dyn Write) -> IResult<()> {
@ -245,9 +246,8 @@ impl Target for Luau {
fn write_runtime(w: &mut dyn Write) -> IResult<()> {
let runtime = codegen_luau::RUNTIME;
writeln!(w, "local rt = (function() {runtime} end)()")?;
writeln!(w, "local loaded = {{}}")?;
writeln!(w, "local linked = {{}}")
writeln!(w, "{ASSERTION}")?;
writeln!(w, "local rt = (function() {runtime} end)()")
}
fn write_module(typed: &TypedModule, w: &mut dyn Write) -> IResult<()> {
@ -362,9 +362,13 @@ impl<T: Target> Tester<T> {
}
}
static DO_NOT_RUN: [&str; 4] = [
static DO_NOT_RUN: [&str; 8] = [
"binary-leb128.wast",
"conversions.wast",
"float_exprs.wast",
"float_literals.wast",
"float_memory.wast",
"float_misc.wast",
"names.wast",
"skip-stack-guard-page.wast",
];