docs: 补充注释与格式化

This commit is contained in:
2026-08-06 23:10:27 +08:00
parent e36a69976e
commit 31e28a03a6
156 changed files with 2393 additions and 1687 deletions
+21 -22
View File
@@ -113,32 +113,31 @@ fn primitive_property_write_throws_clean_type_error() {
}
}
// --- Phase 14.0.1: type-conversion infrastructure (ENG-13) ---
// SC-1..SC-8 from ROADMAP. eval() returns the Display of the final value;
// every assertion below targets a bool/number result to avoid string-quoting
// ambiguity.
// --- 类型转换基础设施测试 ---
// eval() 返回最终值的 Display;以下断言都针对 bool/number 结果,
// 避免字符串引用的歧义。
// SC-1 ToPrimitive: boxed Number unwraps via valueOf (was "NaN" before).
// ToPrimitive:装箱 Number 经 valueOf 解包。
#[test]
fn to_primitive_number_boxed() {
assert_eq!(eval("+new Number(42)"), "42");
}
// SC-1 ToPrimitive: boxed String coerces to its primitive in == (object vs string).
// ToPrimitive:装箱 String 在 == 中强转为原始值(对象 vs 字符串)。
#[test]
fn to_primitive_string_boxed() {
assert_eq!(eval("new String('x') == 'x'"), "true");
}
// SC-1 ToPrimitive: obj[Symbol.toPrimitive] is consulted (callback runs, returns 7).
// ToPrimitive:优先调用 obj[Symbol.toPrimitive](回调执行并返回 7)。
#[test]
fn to_primitive_symbol_hint_invoked() {
assert_eq!(eval("var o = {}; o[Symbol.toPrimitive] = function (h) { return 7; }; +o"), "7");
}
// SC-2 ToNumber: to_number_full throws TypeError on a Symbol (per D-02).
// Note: the unary-plus operator path uses coerce_number_bounded (no symbol
// throw) so `+Symbol()` yields NaN at the operator level; see test below.
// ToNumber:to_number_full 对 Symbol 抛 TypeError。
// 注意:一元加号路径用 coerce_number_bounded(对 Symbol 不抛),因此运算符层的
// +Symbol() 得到 NaN;见下个测试。
#[test]
fn to_number_full_symbol_throws() {
let mut vm = Vm::new();
@@ -148,73 +147,73 @@ fn to_number_full_symbol_throws() {
assert!(result.unwrap_err().contains("Symbol"), "expected a Symbol TypeError message");
}
// SC-2/SC-7 operator-level reality: +Symbol is NaN (symbol-throw not wired into arithmetic).
// 运算符层实况:+Symbol 为 NaN(Symbol 抛出未接入算术路径)。
#[test]
fn unary_plus_symbol_is_nan() {
assert_eq!(eval("+Symbol('x')"), "NaN");
}
// SC-3 Abstract Equality: string vs number coerces.
// 抽象相等:字符串与数字互相强转。
#[test]
fn abstract_eq_string_number() {
assert_eq!(eval("'1' == 1"), "true");
}
// SC-3 Abstract Equality: bool->number recursion + string->number.
// 抽象相等:布尔转数字的递归 + 字符串转数字。
#[test]
fn abstract_eq_bool_number() {
assert_eq!(eval("'1' == true"), "true");
}
// SC-3 Abstract Equality: object vs number via ToPrimitive ([42] -> "42" -> 42).
// 抽象相等:对象 vs 数字经 ToPrimitive([42] -> "42" -> 42)。
#[test]
fn abstract_eq_object_primitive() {
assert_eq!(eval("[42] == 42"), "true");
}
// SC-3 Abstract Equality: null == undefined (regression guard, spec step 2-3).
// 抽象相等:null == undefined(回归锚,规范第 2-3 步)。
#[test]
fn abstract_eq_null_undefined() {
assert_eq!(eval("null == undefined"), "true");
}
// SC-3 Abstract Equality: null == 0 is false (spec falls through to step 14).
// 抽象相等:null == 0 为 false(规范落到第 14 步)。
#[test]
fn abstract_eq_null_zero_mismatch() {
assert_eq!(eval("null == 0"), "false");
}
// SC-3 Abstract Equality: same-type delegates to strict equality.
// 抽象相等:同类型委托给严格相等。
#[test]
fn abstract_eq_same_type_string() {
assert_eq!(eval("'abc' == 'abc'"), "true");
}
// SC-4 Strict Equality: +0 === -0 is true (verify-only; already correct).
// 严格相等:+0 === -0 为 true(仅验证)。
#[test]
fn strict_eq_signed_zero_eval() {
assert_eq!(eval("+0 === -0"), "true");
}
// SC-4 Strict Equality: NaN === NaN is false (verify-only; already correct).
// 严格相等:NaN === NaN 为 false(仅验证)。
#[test]
fn strict_eq_nan_eval() {
assert_eq!(eval("NaN === NaN"), "false");
}
// SC-5 Arithmetic +: object operand coerces via valueOf.
// 算术 +:对象操作数经 valueOf 强转。
#[test]
fn add_object_valueof_coercion() {
assert_eq!(eval("var o = {valueOf: function () { return 42; }}; o + 1"), "43");
}
// SC-6 Relational: object operands coerce via ToPrimitive then compare as strings.
// 关系比较:对象操作数经 ToPrimitive 后按字符串比较。
#[test]
fn relational_object_coercion() {
assert_eq!(eval("[3] > [2]"), "true");
}
// SC-1/D-02 Date: Date object coerces to its timestamp in == (default hint -> valueOf).
// Date 对象在 == 中强转为自身时间戳(默认 hint -> valueOf)。
#[test]
fn date_object_equals_timestamp() {
assert_eq!(eval("new Date(0) == 0"), "true");