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

Move code from TypedArray::create and Add TypedArray::update #333

Merged
merged 1 commit into from Feb 7, 2017

Conversation

@yysung1123
Copy link
Contributor

yysung1123 commented Feb 4, 2017

For servo/servo#15350

TypedArray::update is used to be replace update_array_buffer_view


This change is Reviewable

Copy link
Member

jdm left a comment

We will definitely want a testcase added to tests/typedarray.rs showing how this new API works. Run it with cargo test.

/// Update an existed JS typed array
pub unsafe fn update(length: u32,
data: Option<&[T::Element]>,
result: MutableHandleObject) {

This comment has been minimized.

@jdm

jdm Feb 4, 2017

Member

Rather than making a static method like this, I recommend pub fn update(&mut self, data: &[T::Element]).

This comment has been minimized.

@yysung1123

yysung1123 Feb 4, 2017

Author Contributor

But if it isn't a static method, I can't call it from TypedArray::create.

This comment has been minimized.

@jdm

jdm Feb 4, 2017

Member

I argue that we can create a update_raw that encapsulates the relevant bit, and call that from both create and update (passing in a mutable destination buffer and an immutable source buffer).

@yysung1123 yysung1123 force-pushed the yysung1123:typedarray-update branch from d18260c to bd866f0 Feb 5, 2017
@yysung1123
Copy link
Contributor Author

yysung1123 commented Feb 5, 2017

Should I replace all data: Option<&[T::Element]> with data: <&[T::Element]>?

@jdm
Copy link
Member

jdm commented Feb 5, 2017

Yes, that would lead to a cleaner API.

@yysung1123 yysung1123 force-pushed the yysung1123:typedarray-update branch 2 times, most recently from f712f19 to 648ff71 Feb 5, 2017
@@ -124,22 +124,31 @@ impl<'a, T: TypedArrayElementCreator + TypedArrayElement> TypedArray<'a, T> {
/// be copied into the newly-allocated buffer. Returns the new JS reflector.
pub unsafe fn create(cx: *mut JSContext,
length: u32,
data: Option<&[T::Element]>,

This comment has been minimized.

@jdm

jdm Feb 5, 2017

Member

We want to keep the Option here, and only call update_raw if it's Some.

TypedArray::<T>::update_raw(data, self.object.handle_mut());
}

pub unsafe fn update_raw(data: &[T::Element],

This comment has been minimized.

@jdm

jdm Feb 5, 2017

Member

This should not be public.

}

pub unsafe fn update_raw(data: &[T::Element],
result: MutableHandleObject) {

This comment has been minimized.

@jdm

jdm Feb 5, 2017

Member

This can be a HandleObject instead.


typedarray!(in(cx) let mut array: Uint32Array = rval.get());
array.as_mut().unwrap().update(Some(&[0, 2, 4, 6]));
assert_eq!(array.unwrap().as_slice(), &[0, 2, 4, 6, 0][..]);
}

This comment has been minimized.

@jdm

jdm Feb 5, 2017

Member

Let's add another test in this file that uses #[should_panic] that calls update with an argument that is longer than the typed array that it is replacing, too.

@jdm
Copy link
Member

jdm commented Feb 5, 2017

Not sure why my last comment is marked outdated, but please look at it as well.

@yysung1123 yysung1123 force-pushed the yysung1123:typedarray-update branch 2 times, most recently from 9017800 to eed397f Feb 5, 2017
@yysung1123
Copy link
Contributor Author

yysung1123 commented Feb 6, 2017

@jdm

About panic test? I have added it already!

Copy link
Member

jdm left a comment

The test looks good. A couple small stylistic changes and this should be ready to go!

}

This comment has been minimized.

@jdm

jdm Feb 6, 2017

Member

nit: remove the trailing spaces on this line.

let buf = T::get_data(result.get());
ptr::copy_nonoverlapping(data.as_ptr(), buf, data.len());
if data.is_some() {
TypedArray::<T>::update_raw(data.unwrap(), result.handle());

This comment has been minimized.

@jdm

jdm Feb 6, 2017

Member

We can use if let here instead and avoid the need to unwrap the value.

@yysung1123 yysung1123 force-pushed the yysung1123:typedarray-update branch 2 times, most recently from 1f2e497 to 7388337 Feb 6, 2017
@yysung1123 yysung1123 force-pushed the yysung1123:typedarray-update branch from 7388337 to af187fb Feb 7, 2017
@yysung1123
Copy link
Contributor Author

yysung1123 commented Feb 7, 2017

Conflicts resolved

@jdm
Copy link
Member

jdm commented Feb 7, 2017

@bors-servo: r+
Thanks!

@bors-servo
Copy link
Contributor

bors-servo commented Feb 7, 2017

📌 Commit af187fb has been approved by jdm

@bors-servo
Copy link
Contributor

bors-servo commented Feb 7, 2017

Testing commit af187fb with merge 4c4e850...

bors-servo added a commit that referenced this pull request Feb 7, 2017
Move code from TypedArray::create and Add TypedArray::update

For servo/servo#15350

```TypedArray::update``` is used to be replace ```update_array_buffer_view```

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/rust-mozjs/333)
<!-- Reviewable:end -->
@bors-servo
Copy link
Contributor

bors-servo commented Feb 7, 2017

☀️ Test successful - status-appveyor, status-travis
Approved by: jdm
Pushing 4c4e850 to master...

@bors-servo bors-servo merged commit af187fb into servo:master Feb 7, 2017
2 checks passed
2 checks passed
continuous-integration/appveyor/pr AppVeyor build succeeded
Details
homu Test successful
Details
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

3 participants
You can’t perform that action at this time.