feat: 支持 this 语法

This commit is contained in:
2026-06-07 23:07:50 +08:00
parent 7739c7a145
commit 74f4668e28
4 changed files with 65 additions and 0 deletions
+38
View File
@@ -148,3 +148,41 @@ fn fd_returns_new_array() {
let result = eval(&mut vm, "function f() { return new Array(3); } f()").unwrap();
assert!(result.is_object());
}
// --- this expression ---
#[test]
fn this_in_function_reads_value() {
let mut vm = Vm::new();
let result = eval(&mut vm, "function f() { return this; } f()").unwrap();
assert!(
result.is_object(),
"expected object (function as this), got: {:?}",
result
);
}
#[test]
fn this_in_function_assign_member() {
let mut vm = Vm::new();
let result = eval(&mut vm, "function f(m) { this.message = m; } f('hello'); 1").unwrap();
assert_eq!(result.as_int(), 1);
}
#[test]
fn this_member_access() {
let mut vm = Vm::new();
let result = eval(&mut vm, "function f() { this.x = 42; return this.x; } f()").unwrap();
assert_eq!(result.as_int(), 42);
}
#[test]
fn this_in_constructor_sets_proto() {
let mut vm = Vm::new();
let result = eval(
&mut vm,
"function Ctor(m) { this.message = m; } Ctor.prototype = new Object(); 1",
)
.unwrap();
assert_eq!(result.as_int(), 1);
}