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

array: implement array.slice #180

Merged
merged 2 commits into from
Oct 28, 2019
Merged

array: implement array.slice #180

merged 2 commits into from
Oct 28, 2019

Conversation

muskuloes
Copy link
Contributor

array.slice per #177

/// length is the length of the array.
/// <https://tc39.es/ecma262/#sec-array.prototype.slice>
pub fn slice(this: &Value, args: &[Value], interpreter: &mut Interpreter) -> ResultValue {
let new_array = make_array(&to_value(Object::default()), &[], interpreter)?;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After creating an object, you need to mark it as an array and set it's prototype. Check here for sample code on how to do it.


let start = match args.get(0) {
Some(v) => from_value(v.clone()).expect("failed to parse argument for Array method"),
None => return Ok(this.clone()),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be returning the new array here. this.clone() will only copy the reference. Test case:

var animals = ['ant', 'bison', 'camel', 'duck', 'elephant'];
var animals2 = animals.splice();
animals.push(5);
console.log(animals, animals2);
> Array ["ant", "bison", "camel", "duck", "elephant", 5] Array []


let span = max(to.wrapping_sub(from), 0);
for i in from..from.wrapping_add(span) {
add_to_array_object(&new_array, &[this.get_field(&i.to_string())])?;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's better to just call this.set_field rather than constructing a slice and calling add_to_array_object.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@muskuloes Sorry, not this.set_field but new_array.set_field

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants