This repository has been archived on 2026-09-15. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
OxideJS/crates/oxide_vm/tests/arithmetic_tests.rs
T
pjh123 c4961fae29 feat: 二元幂运算 opcode 支持 number 与 BigInt
- 新增 EXP opcode(0x94)与 emit 层 Exponential 分支,二元 a ** b 不再 compile error
- dispatch_exp 与 dispatch_compound_exp 共享 exp_result:IEEE-754 powf 快路径 +
  BigInt 幂(负/超 u32 指数 RangeError)+ 混合 BigInt/Number TypeError
- 复合 **= 补 BigInt 语义(此前走 f64 近似),与二元 ** 行为一致
2026-08-15 21:17:49 +08:00

232 lines
6.1 KiB
Rust

use oxide_compiler::compiler::Compiler;
use oxide_parser::Allocator;
use oxide_vm::vm::Vm;
fn eval(source: &str) -> String {
let allocator = Allocator::default();
let program = match oxide_parser::parse(&allocator, source) {
Ok(p) => p,
Err(e) => return format!("parse error: {}", e[0].message),
};
let module = match Compiler::new().compile(&program) {
Ok(m) => m,
Err(e) => return format!("compile error: {e}"),
};
let mut vm = Vm::new();
match vm.run(&module) {
Ok(result) => format!("{result}"),
Err(e) => format!("vm error: {e}"),
}
}
/// 求值并取字符串结果内容(字符串值走 lookup_str,非字符串走 Display)。
fn eval_str(source: &str) -> String {
let allocator = Allocator::default();
let program = oxide_parser::parse(&allocator, source).expect("parse ok");
let module = Compiler::new().compile(&program).expect("compile ok");
let mut vm = Vm::new();
let val = vm.run(&module).expect("run ok");
if val.is_string() {
vm.lookup_str(val).unwrap_or_default()
} else {
format!("{val}")
}
}
#[test]
fn eval_arithmetic_add() {
assert_eq!(eval("1 + 2"), "3");
}
#[test]
fn eval_arithmetic_sub() {
assert_eq!(eval("5 - 3"), "2");
}
#[test]
fn eval_arithmetic_mul() {
assert_eq!(eval("3 * 4"), "12");
}
#[test]
fn eval_arithmetic_div() {
assert_eq!(eval("10 / 2"), "5");
}
#[test]
fn eval_arithmetic_mod() {
assert_eq!(eval("7 % 3"), "1");
}
#[test]
fn eval_precedence() {
assert_eq!(eval("1 * 2 + 3"), "5");
}
#[test]
fn eval_negation() {
assert_eq!(eval("-5"), "-5");
}
#[test]
fn eval_division_by_zero() {
assert_eq!(eval("1 / 0"), "Infinity");
}
#[test]
fn eval_string_concat() {
assert_eq!(eval("1 + 2 + 3"), "6");
}
#[test]
fn eval_string_concat_negative_double_appends() {
// 非空缓冲 + ryu 路径负数:负号紧跟当前数值追加,不插到整个结果最前。
assert_eq!(eval_str("\"a\" + -1.5"), "a-1.5");
assert_eq!(eval_str("\"a\" + (-1e21)"), "a-1e+21");
// 整数负数(int 直写)与整数 double 快路径负数同样追加正确。
assert_eq!(eval_str("\"a\" + -42"), "a-42");
assert_eq!(eval_str("\"a\" + (-42.0)"), "a-42");
// ≥2^53 可精确表示整数负数(2^53+2)落 ryu 定点路径,追加语义保持一致。
assert_eq!(eval_str("\"a\" + -9007199254740994"), "a-9007199254740994");
}
#[test]
fn eval_compound_add() {
assert_eq!(eval("var x=5; x+=3; x"), "8");
}
#[test]
fn eval_compound_sub() {
assert_eq!(eval("var x=10; x-=4; x"), "6");
}
#[test]
fn eval_compound_mul() {
assert_eq!(eval("var x=2; x*=3; x"), "6");
}
#[test]
fn eval_compound_div() {
assert_eq!(eval("var x=10; x/=2; x"), "5");
}
#[test]
fn eval_compound_mod() {
assert_eq!(eval("var x=7; x%=3; x"), "1");
}
#[test]
fn eval_compound_exp() {
assert_eq!(eval("var x=2; x**=3; x"), "8");
}
#[test]
fn eval_binary_exp() {
assert_eq!(eval("2 ** 3"), "8");
assert_eq!(eval("2 ** 10"), "1024");
assert_eq!(eval("2 ** -1"), "0.5");
assert_eq!(eval("4 ** 0.5"), "2");
}
#[test]
fn eval_exp_edge_cases() {
assert_eq!(eval("0 ** 0"), "1");
assert_eq!(eval("(-2) ** 3"), "-8");
assert_eq!(eval("(-2) ** 2"), "4");
assert_eq!(eval("(-2) ** 0.5"), "NaN");
assert_eq!(eval("NaN ** 0"), "1");
assert_eq!(eval("2 ** Infinity"), "Infinity");
assert_eq!(eval("2.5 ** 2"), "6.25");
}
#[test]
fn eval_exp_bigint() {
assert_eq!(eval("2n ** 3n"), "BigInt(8)");
assert_eq!(eval("2n ** 0n"), "BigInt(1)");
assert_eq!(eval("0n ** 0n"), "BigInt(1)");
}
#[test]
fn eval_exp_bigint_negative_exponent_throws() {
// BigInt::exponentiate 禁止负指数,抛 RangeError(规范 §6.1.6.1.19)。
let err = eval("2n ** -1n");
assert!(err.contains("RangeError"), "expected RangeError, got: {err}");
}
#[test]
fn eval_exp_mixed_bigint_number_throws() {
// ToNumeric 类型不一致:Number 与 BigInt 混合幂抛 TypeError(与 ADD/SUB 同)。
let err = eval("2 ** 2n");
assert!(err.contains("TypeError"), "expected TypeError, got: {err}");
let err2 = eval("2n ** 2");
assert!(err2.contains("TypeError"), "expected TypeError, got: {err2}");
}
#[test]
fn eval_exp_compound_bigint() {
// 复合 **= 与二元 ** 共享幂语义(BigInt 结果保持 BigInt,不再降 f64)。
assert_eq!(eval("var x = 2n; x **= 3n; x"), "BigInt(8)");
let err = eval("var x = 2n; x **= 3; x");
assert!(err.contains("TypeError"), "expected TypeError, got: {err}");
}
#[test]
fn eval_compound_add_expr_value() {
assert_eq!(eval("var x=5; x+=3"), "8");
}
#[test]
fn eval_compound_undefined() {
assert_eq!(eval("var x; x+=1; x"), "NaN");
}
#[test]
fn eval_bitwise_direct_ops() {
assert_eq!(eval("5 & 3"), "1");
assert_eq!(eval("5 | 2"), "7");
assert_eq!(eval("5 ^ 1"), "4");
assert_eq!(eval("1 << 3"), "8");
assert_eq!(eval("-8 >> 1"), "-4");
assert_eq!(eval("-1 >>> 0"), "4294967295");
assert_eq!(eval("~0"), "-1");
}
#[test]
fn eval_bitwise_coercion() {
assert_eq!(eval("'3' | 0"), "3");
assert_eq!(eval("'x' | 0"), "0");
assert_eq!(eval("undefined | 0"), "0");
assert_eq!(eval("NaN | 0"), "0");
assert_eq!(eval("1.9 | 0"), "1");
assert_eq!(eval("-1.9 | 0"), "-1");
}
#[test]
fn eval_shift_masking() {
assert_eq!(eval("1 << 32"), "1");
assert_eq!(eval("1 << 33"), "2");
assert_eq!(eval("8 >> 33"), "4");
}
#[test]
fn eval_ushr_boundaries() {
assert_eq!(eval("2147483647 >>> 0"), "2147483647");
assert_eq!(eval("2147483648 >>> 0"), "2147483648");
assert_eq!(eval("-1 >>> 1"), "2147483647");
}
#[test]
fn eval_compound_bitwise_ops() {
assert_eq!(eval("var x=5; x &= 3; x"), "1");
assert_eq!(eval("var x=5; x |= 2; x"), "7");
assert_eq!(eval("var x=5; x ^= 1; x"), "4");
assert_eq!(eval("var x=1; x <<= 5; x"), "32");
assert_eq!(eval("var x=-8; x >>= 1; x"), "-4");
assert_eq!(eval("var x=-1; x >>>= 0; x"), "4294967295");
}
#[test]
fn eval_bitwise_in_control_flow() {
assert_eq!(eval("var x=3; if ((x & 1) == 1) { 7 } else { 9 }"), "7");
}