Skip to content

Commit

Permalink
fix #209 "Calling Array with one argument" (#328)
Browse files Browse the repository at this point in the history
* fix issue 209 "Calling Array with one argument"

* Update boa/src/builtins/array/mod.rs

Co-Authored-By: Iban Eguia <razican@protonmail.ch>

* Changed from unimplemented to panic in array

Co-authored-by: Iban Eguia <razican@protonmail.ch>
  • Loading branch information
HalidOdat and Razican authored Apr 29, 2020
1 parent 84b4da5 commit dd0f967
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 12 deletions.
40 changes: 28 additions & 12 deletions boa/src/builtins/array/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,15 +101,6 @@ pub fn make_array(this: &Value, args: &[Value], ctx: &mut Interpreter) -> Result
// Make a new Object which will internally represent the Array (mapping
// between indices and values): this creates an Object with no prototype

// Create length
let length = Property::new()
.value(to_value(args.len() as i32))
.writable(true)
.configurable(false)
.enumerable(false);

this.set_prop("length".to_string(), length);

// Set Prototype
let array_prototype = ctx
.realm
Expand All @@ -122,11 +113,36 @@ pub fn make_array(this: &Value, args: &[Value], ctx: &mut Interpreter) -> Result
// to its Javascript Identifier (global constructor method name)
this.set_kind(ObjectKind::Array);

// And finally add our arguments in
for (n, value) in args.iter().enumerate() {
this.set_field_slice(&n.to_string(), value.clone());
// add our arguments in
let mut length = args.len() as i32;
match args.len() {
1 if args[0].is_integer() => {
length = from_value::<i32>(args[0].clone()).expect("Could not convert argument to i32");
// TODO: It should not create an array of undefineds, but an empty array ("holy" array in V8) with length `n`.
for n in 0..length {
this.set_field_slice(&n.to_string(), Gc::new(ValueData::Undefined));
}
}
1 if args[0].is_double() => {
// TODO: throw `RangeError`.
panic!("RangeError: invalid array length");
}
_ => {
for (n, value) in args.iter().enumerate() {
this.set_field_slice(&n.to_string(), value.clone());
}
}
}

// finally create length property
let length = Property::new()
.value(to_value(length))
.writable(true)
.configurable(false)
.enumerable(false);

this.set_prop("length".to_string(), length);

Ok(this.clone())
}

Expand Down
22 changes: 22 additions & 0 deletions boa/src/builtins/array/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -791,3 +791,25 @@ fn some() {
assert_eq!(del_array_length, "3");
assert_eq!(result, "true");
}

#[test]
fn call_array_constructor_with_one_argument() {
let realm = Realm::create();
let mut engine = Executor::new(realm);
let init = r#"
var empty = new Array(0);
var five = new Array(5);
var one = new Array("Hello, world!");
"#;
forward(&mut engine, init);
let result = forward(&mut engine, "empty.length");
assert_eq!(result, "0");

let result = forward(&mut engine, "five.length");
assert_eq!(result, "5");

let result = forward(&mut engine, "one.length");
assert_eq!(result, "1");
}

0 comments on commit dd0f967

Please sign in to comment.