feat: 支持 Infinity 常量

This commit is contained in:
爱特每羊队
2026-06-07 23:11:26 +08:00
parent 501a14b3af
commit 850f9f12ed
4 changed files with 62 additions and 0 deletions
+1
View File
@@ -184,6 +184,7 @@ impl CompileCtx {
let builtins = [ let builtins = [
"NaN", "NaN",
"undefined", "undefined",
"Infinity",
"Object", "Object",
"Array", "Array",
"String", "String",
@@ -376,3 +376,15 @@ fn compile_this_member_read_ok() {
let module = compile_source("function f() { return this.x; }"); let module = compile_source("function f() { return this.x; }");
assert!(!module.bytecode.is_empty()); assert!(!module.bytecode.is_empty());
} }
#[test]
fn compile_infinity_ok() {
let module = compile_source("Infinity");
assert!(!module.bytecode.is_empty());
}
#[test]
fn compile_neg_infinity_ok() {
let module = compile_source("1 / -Infinity");
assert!(!module.bytecode.is_empty());
}
+7
View File
@@ -88,6 +88,7 @@ impl OxideKernel {
let si_nan = string_forge.intern("NaN").0; let si_nan = string_forge.intern("NaN").0;
let si_undef = string_forge.intern("undefined").0; let si_undef = string_forge.intern("undefined").0;
let si_infinity = string_forge.intern("Infinity").0;
let nan_shape = shape_forge.make_shape(EMPTY_SHAPE_ID, si_nan); let nan_shape = shape_forge.make_shape(EMPTY_SHAPE_ID, si_nan);
global_obj.set_shape_id(nan_shape); global_obj.set_shape_id(nan_shape);
@@ -101,6 +102,12 @@ impl OxideKernel {
.ensure_hash_props() .ensure_hash_props()
.push(Box::new(JsValue::undefined())); .push(Box::new(JsValue::undefined()));
let inf_shape = shape_forge.make_shape(undef_shape, si_infinity);
global_obj.set_shape_id(inf_shape);
global_obj
.ensure_hash_props()
.push(Box::new(JsValue::float(f64::INFINITY)));
let global_object = P::new(global_obj); let global_object = P::new(global_obj);
Self { Self {
@@ -37,3 +37,45 @@ fn typeof_undefined_is_undefined() {
let result = eval("typeof undefined").unwrap(); let result = eval("typeof undefined").unwrap();
assert!(result.is_string()); assert!(result.is_string());
} }
#[test]
fn eval_infinity_returns_infinity() {
let result = eval("Infinity").unwrap();
assert!(result.is_double());
assert!(result.as_double().is_infinite());
assert!(result.as_double().is_sign_positive());
}
#[test]
fn eval_neg_infinity_via_division() {
let result = eval("1 / Infinity").unwrap();
assert!(result.is_double());
assert_eq!(result.as_double(), 0.0);
}
#[test]
fn typeof_infinity_is_number() {
let result = eval("typeof Infinity").unwrap();
assert!(result.is_string());
}
#[test]
fn infinity_ne_nan() {
let result = eval("Infinity !== NaN").unwrap();
assert!(result.is_bool());
assert!(result.as_bool());
}
#[test]
fn infinity_gt_large_number() {
let result = eval("Infinity > 1e308").unwrap();
assert!(result.is_bool());
assert!(result.as_bool());
}
#[test]
fn division_by_zero_gives_infinity() {
let result = eval("1 / 0").unwrap();
assert!(result.is_double());
assert!(result.as_double().is_infinite());
}