|
|
|
@ -415,6 +415,13 @@ impl Executor for Interpreter {
|
|
|
|
|
let val_obj = self.run(obj)?; |
|
|
|
|
val_obj.borrow().set_field(field.clone(), val.clone()); |
|
|
|
|
} |
|
|
|
|
ExprDef::GetField(ref obj, ref field) => { |
|
|
|
|
let val_obj = self.run(obj)?; |
|
|
|
|
let val_field = self.run(field)?; |
|
|
|
|
val_obj |
|
|
|
|
.borrow() |
|
|
|
|
.set_field(val_field.to_string(), val.clone()); |
|
|
|
|
} |
|
|
|
|
_ => (), |
|
|
|
|
} |
|
|
|
|
Ok(val) |
|
|
|
@ -703,4 +710,45 @@ mod tests {
|
|
|
|
|
|
|
|
|
|
assert_eq!(exec(scenario), pass); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#[test] |
|
|
|
|
fn object_field_set() { |
|
|
|
|
let scenario = r#" |
|
|
|
|
let m = {}; |
|
|
|
|
m['key'] = 22; |
|
|
|
|
m['key'] |
|
|
|
|
"#; |
|
|
|
|
assert_eq!(exec(scenario), String::from("22")); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#[test] |
|
|
|
|
fn array_field_set() { |
|
|
|
|
let element_changes = r#" |
|
|
|
|
let m = [1, 2, 3]; |
|
|
|
|
m[1] = 5; |
|
|
|
|
m[1] |
|
|
|
|
"#; |
|
|
|
|
assert_eq!(exec(element_changes), String::from("5")); |
|
|
|
|
|
|
|
|
|
let length_changes = r#" |
|
|
|
|
let m = [1, 2, 3]; |
|
|
|
|
m[10] = 52; |
|
|
|
|
m.length |
|
|
|
|
"#; |
|
|
|
|
assert_eq!(exec(length_changes), String::from("11")); |
|
|
|
|
|
|
|
|
|
let negative_index_wont_affect_length = r#" |
|
|
|
|
let m = [1, 2, 3]; |
|
|
|
|
m[-11] = 5; |
|
|
|
|
m.length |
|
|
|
|
"#; |
|
|
|
|
assert_eq!(exec(negative_index_wont_affect_length), String::from("3")); |
|
|
|
|
|
|
|
|
|
let non_num_key_wont_affect_length = r#" |
|
|
|
|
let m = [1, 2, 3]; |
|
|
|
|
m["magic"] = 5; |
|
|
|
|
m.length |
|
|
|
|
"#; |
|
|
|
|
assert_eq!(exec(non_num_key_wont_affect_length), String::from("3")); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|