Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable obj[key] = value syntax. #161

Merged
merged 2 commits into from
Oct 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
48 changes: 48 additions & 0 deletions src/lib/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,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)
Expand Down Expand Up @@ -675,4 +682,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"));
}
}
2 changes: 1 addition & 1 deletion src/lib/js/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ impl Object {
}
}

#[derive(Trace, Finalize, Clone, Debug)]
#[derive(Trace, Finalize, Clone, Debug, Eq, PartialEq)]
pub enum ObjectKind {
Function,
Array,
Expand Down
11 changes: 11 additions & 0 deletions src/lib/js/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,17 @@ impl ValueData {
pub fn set_field(&self, field: String, val: Value) -> Value {
match *self {
ValueData::Object(ref obj) => {
if obj.borrow().kind == ObjectKind::Array {
letmutx marked this conversation as resolved.
Show resolved Hide resolved
if let Ok(num) = field.parse::<usize>() {
if num > 0 {
let len: i32 = from_value(self.get_field_slice("length"))
.expect("Could not convert argument to i32");
if len < (num + 1) as i32 {
self.set_field_slice("length", to_value(num + 1));
}
}
}
}
obj.borrow_mut()
.properties
.insert(field, Property::default().value(val.clone()));
Expand Down