-
-
Notifications
You must be signed in to change notification settings - Fork 407
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
Conversation
src/lib/js/array.rs
Outdated
/// 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)?; |
There was a problem hiding this comment.
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.
src/lib/js/array.rs
Outdated
|
||
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()), |
There was a problem hiding this comment.
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 []
src/lib/js/array.rs
Outdated
|
||
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())])?; |
There was a problem hiding this comment.
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
.
There was a problem hiding this comment.
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
array.slice per #177