Space Lua: Align arithmetical error messages with Lua 5.4 (#1793)
* Space Lua: `<close>` attribute, `__close` metamethod and extensive tests Signed-off-by: Matouš Jan Fialka <mjf@mjf.cz> * Space Lua: Align arithmetical error messages with Lua 5.4 **Fixes:** - Tighten `tonumber` semantics to accept hex floats like `0x1.2` while keeping plain hex as integers. - Improve Lua compatibility by fixing relational `>`/`>=` operand ordering to preserve correct error type direction. **Changes:** - Update concatenation errors to include the operand type. - Add `LuaStringCoercionError` from numeric coercion and handle it in `luaOp()` to emit correct messages (or fallback). - Raise > `bad argument #2 to 'tonumber' (base out of range)` for invalid bases. - Update test suite **Notes:** - The `arithmetic_test.lua` test suite now passes if run with Lua 5.4. Signed-off-by: Matouš Jan Fialka <mjf@mjf.cz> * Run `make fmt` Signed-off-by: Matouš Jan Fialka <mjf@mjf.cz> * Rename `assert_throws` to `assertThrows` in Lua test suite Signed-off-by: Matouš Jan Fialka <mjf@mjf.cz> * Replace `LuaStringCoercionError` class with sentinel constant Signed-off-by: Matouš Jan Fialka <mjf@mjf.cz> --------- Signed-off-by: Matouš Jan Fialka <mjf@mjf.cz>
This commit is contained in:
@@ -4,7 +4,7 @@ local function assert_eq(actual, expected, message)
|
||||
end
|
||||
end
|
||||
|
||||
local function assert_throws(msg_substr, fn)
|
||||
local function assertThrows(msg_substr, fn)
|
||||
local ok, err = pcall(fn)
|
||||
|
||||
if ok then
|
||||
@@ -320,7 +320,7 @@ assert_eq('1'+2, 3, 'str-num: "1"+2 == 3')
|
||||
assert_eq(' -2 ' * '3', -6, 'str-num: " -2 " * "3" == -6')
|
||||
assert_eq('0x10' + 1, 17, 'str-num: hex int string + 1 == 17')
|
||||
assert_eq('0x1p4' + 0, 16, 'str-num: hex float string + 0 == 16')
|
||||
assert_throws('attempt to perform arithmetic on a non-number',
|
||||
assertThrows("attempt to add a 'string' with a 'number'",
|
||||
function()
|
||||
return 'x1'+1
|
||||
end
|
||||
@@ -342,7 +342,7 @@ assert_eq(1/-rec_zero(5) == 1/0, true, 'recursive: -(rec_zero) (+Inf)')
|
||||
-- 16. Modulo and integer division by zero
|
||||
|
||||
-- 16.1. Modulo by zero
|
||||
assert_throws('modulo by zero',
|
||||
assertThrows("attempt to perform 'n%0'",
|
||||
function()
|
||||
return 1%0
|
||||
end
|
||||
@@ -370,7 +370,7 @@ val = pcall(
|
||||
assert_eq(val, true, 'mixed (int,float) mod by zero ok (NaN)')
|
||||
|
||||
-- 16.2. Integer division by zero
|
||||
assert_throws('divide by zero',
|
||||
assertThrows('divide by zero',
|
||||
function()
|
||||
return 1//0
|
||||
end
|
||||
@@ -467,83 +467,237 @@ assert_eq(0^0 == 1, true, 'pow: 0^0 == 1')
|
||||
assert_eq((-0.0)^0 == 1, true, 'pow: (-0.0)^0 == 1')
|
||||
|
||||
-- 19. Error tests
|
||||
assert_throws('has no integer representation',
|
||||
assertThrows('has no integer representation',
|
||||
function()
|
||||
return ~0.5
|
||||
end
|
||||
)
|
||||
|
||||
assert_throws('attempt to perform arithmetic on a non-number',
|
||||
assertThrows("attempt to add a 'string' with a 'number'",
|
||||
function()
|
||||
return 'a'+1
|
||||
end
|
||||
)
|
||||
|
||||
assert_throws('attempt to perform arithmetic on a non-number',
|
||||
assertThrows('attempt to perform arithmetic on a table value',
|
||||
function()
|
||||
return -{}
|
||||
end
|
||||
)
|
||||
|
||||
-- 19.1. Bitwise on non-integers should error
|
||||
assert_throws('has no integer representation',
|
||||
assertThrows('has no integer representation',
|
||||
function()
|
||||
return 1.5&1
|
||||
end
|
||||
)
|
||||
|
||||
assert_throws('attempt to perform arithmetic on a non-number',
|
||||
assertThrows(
|
||||
"attempt to perform bitwise operation on a string value (constant '3')",
|
||||
function()
|
||||
return '3'|1
|
||||
end
|
||||
)
|
||||
|
||||
assert_throws('has no integer representation',
|
||||
assertThrows('has no integer representation',
|
||||
function()
|
||||
return 1~1.2
|
||||
end
|
||||
)
|
||||
|
||||
assert_throws('has no integer representation',
|
||||
assertThrows('has no integer representation',
|
||||
function()
|
||||
return 1<<0.1
|
||||
end
|
||||
)
|
||||
|
||||
-- 19.2. Relational type error
|
||||
assert_throws('attempt to compare number with string',
|
||||
assertThrows('attempt to compare number with string',
|
||||
function()
|
||||
return 1<'1'
|
||||
end
|
||||
)
|
||||
|
||||
-- 19.3. Additional negative tests
|
||||
assert_throws('attempt to perform arithmetic on a non-number',
|
||||
assertThrows('attempt to perform arithmetic on a table value',
|
||||
function()
|
||||
return 1+{}
|
||||
end
|
||||
)
|
||||
|
||||
assert_throws('attempt to perform arithmetic on a non-number',
|
||||
assertThrows("attempt to unm a 'string' with a 'string'",
|
||||
function()
|
||||
return -'x'
|
||||
end
|
||||
)
|
||||
|
||||
assert_throws('attempt to perform arithmetic on a non-number',
|
||||
assertThrows(
|
||||
"attempt to perform bitwise operation on a string value (constant '1')",
|
||||
function()
|
||||
return ~'1'
|
||||
end
|
||||
)
|
||||
|
||||
assert_throws('attempt to compare string with number',
|
||||
assertThrows('attempt to compare string with number',
|
||||
function()
|
||||
return '1'<1
|
||||
end
|
||||
)
|
||||
|
||||
assert_throws('attempt to compare number with object',
|
||||
assertThrows('attempt to compare number with table',
|
||||
function()
|
||||
return 1<{}
|
||||
end
|
||||
)
|
||||
|
||||
-- 19.4. String arithmetic: exact verb mapping
|
||||
assertThrows("attempt to sub a 'string' with a 'number'",
|
||||
function()
|
||||
return 'x' - 1
|
||||
end
|
||||
)
|
||||
|
||||
assertThrows("attempt to mul a 'string' with a 'number'",
|
||||
function()
|
||||
return 'x' * 2
|
||||
end
|
||||
)
|
||||
|
||||
assertThrows("attempt to div a 'string' with a 'number'",
|
||||
function()
|
||||
return 'x' / 2
|
||||
end
|
||||
)
|
||||
|
||||
assertThrows("attempt to idiv a 'string' with a 'number'",
|
||||
function()
|
||||
return 'x' // 2
|
||||
end
|
||||
)
|
||||
|
||||
assertThrows("attempt to mod a 'string' with a 'number'",
|
||||
function()
|
||||
return 'x' % 2
|
||||
end
|
||||
)
|
||||
|
||||
assertThrows("attempt to pow a 'string' with a 'number'",
|
||||
function()
|
||||
return 'x' ^ 2
|
||||
end
|
||||
)
|
||||
|
||||
-- 19.4.1. String arithmetic: type pairing and string-vs-string cases
|
||||
assertThrows("attempt to add a 'string' with a 'string'",
|
||||
function()
|
||||
return 'x' + 'y'
|
||||
end
|
||||
)
|
||||
|
||||
assertThrows("attempt to mul a 'number' with a 'string'",
|
||||
function()
|
||||
return 2 * 'x'
|
||||
end
|
||||
)
|
||||
|
||||
-- 19.5. Bitwise: exact type errors
|
||||
assertThrows("attempt to perform bitwise operation on a table value",
|
||||
function()
|
||||
return {} & 1
|
||||
end
|
||||
)
|
||||
|
||||
assertThrows("attempt to perform bitwise operation on a nil value",
|
||||
function()
|
||||
return nil | 1
|
||||
end
|
||||
)
|
||||
|
||||
assertThrows("attempt to perform bitwise operation on a boolean value",
|
||||
function()
|
||||
return true ~ 1
|
||||
end
|
||||
)
|
||||
|
||||
-- 19.6. Bitwise shifts: non-integer RHS
|
||||
assertThrows("number has no integer representation",
|
||||
function()
|
||||
return 1 << 0.5
|
||||
end
|
||||
)
|
||||
|
||||
assertThrows("number has no integer representation",
|
||||
function()
|
||||
return 8 >> 0.25
|
||||
end
|
||||
)
|
||||
|
||||
-- 19.7. Concatenation: exact messages
|
||||
assertThrows("attempt to concatenate a nil value",
|
||||
function()
|
||||
return nil .. "x"
|
||||
end
|
||||
)
|
||||
|
||||
assertThrows("attempt to concatenate a nil value",
|
||||
function()
|
||||
return "x" .. nil
|
||||
end
|
||||
)
|
||||
|
||||
assertThrows("attempt to concatenate a table value",
|
||||
function()
|
||||
return "x" .. {}
|
||||
end
|
||||
)
|
||||
|
||||
-- 19.8. Length operator: exact message
|
||||
assertThrows("attempt to get length of a number value",
|
||||
function()
|
||||
return #1
|
||||
end
|
||||
)
|
||||
|
||||
assertThrows("attempt to get length of a nil value",
|
||||
function()
|
||||
return #nil
|
||||
end
|
||||
)
|
||||
|
||||
-- 19.9. Relational mismatches
|
||||
assertThrows("attempt to compare number with string",
|
||||
function()
|
||||
return 1 <= '1'
|
||||
end
|
||||
)
|
||||
|
||||
assertThrows("attempt to compare number with string",
|
||||
function()
|
||||
return '1' >= 1
|
||||
end
|
||||
)
|
||||
|
||||
assertThrows("attempt to compare table with number",
|
||||
function()
|
||||
return 1 > {}
|
||||
end
|
||||
)
|
||||
|
||||
assertThrows("attempt to compare table with number",
|
||||
function()
|
||||
return {} < 1
|
||||
end
|
||||
)
|
||||
|
||||
-- 19.10. Explicit int-path division/modulo by zero through expr
|
||||
assertThrows("attempt to perform 'n%0'",
|
||||
function()
|
||||
return 1 % (1-1)
|
||||
end
|
||||
)
|
||||
|
||||
assertThrows("attempt to divide by zero",
|
||||
function()
|
||||
return 1 // (1-1)
|
||||
end
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user