Make typed_array_or_sequence_to_vec support typed arrays.

This commit is contained in:
Josh Matthews 2017-02-02 12:02:13 -05:00
parent 6c4ce7247f
commit baf8c5462a
2 changed files with 32 additions and 22 deletions

2
Cargo.lock generated
View file

@ -1288,7 +1288,7 @@ dependencies = [
[[package]] [[package]]
name = "js" name = "js"
version = "0.1.4" version = "0.1.4"
source = "git+https://github.com/servo/rust-mozjs#4ad9326fc70d13294c77f9f0988fda6abfd0023b" source = "git+https://github.com/servo/rust-mozjs#cc9185025d2655074b29cc0a4bf5ee450356ac5f"
dependencies = [ dependencies = [
"cmake 0.1.20 (registry+https://github.com/rust-lang/crates.io-index)", "cmake 0.1.20 (registry+https://github.com/rust-lang/crates.io-index)",
"heapsize 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", "heapsize 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)",

View file

@ -9,7 +9,7 @@ use dom::bindings::codegen::Bindings::WebGLRenderingContextBinding::{self, WebGL
use dom::bindings::codegen::Bindings::WebGLRenderingContextBinding::WebGLRenderingContextConstants as constants; use dom::bindings::codegen::Bindings::WebGLRenderingContextBinding::WebGLRenderingContextConstants as constants;
use dom::bindings::codegen::Bindings::WebGLRenderingContextBinding::WebGLRenderingContextMethods; use dom::bindings::codegen::Bindings::WebGLRenderingContextBinding::WebGLRenderingContextMethods;
use dom::bindings::codegen::UnionTypes::ImageDataOrHTMLImageElementOrHTMLCanvasElementOrHTMLVideoElement; use dom::bindings::codegen::UnionTypes::ImageDataOrHTMLImageElementOrHTMLCanvasElementOrHTMLVideoElement;
use dom::bindings::conversions::{ArrayBufferViewContents, ConversionResult, FromJSValConvertible, ToJSValConvertible}; use dom::bindings::conversions::{ConversionResult, FromJSValConvertible, ToJSValConvertible};
use dom::bindings::error::{Error, Fallible}; use dom::bindings::error::{Error, Fallible};
use dom::bindings::inheritance::Castable; use dom::bindings::inheritance::Castable;
use dom::bindings::js::{JS, LayoutJS, MutNullableJS, Root}; use dom::bindings::js::{JS, LayoutJS, MutNullableJS, Root};
@ -37,8 +37,9 @@ use dom::window::Window;
use euclid::size::Size2D; use euclid::size::Size2D;
use ipc_channel::ipc::{self, IpcSender}; use ipc_channel::ipc::{self, IpcSender};
use js::conversions::ConversionBehavior; use js::conversions::ConversionBehavior;
use js::jsapi::{JSContext, JSObject, JS_GetArrayBufferViewType, Type}; use js::jsapi::{JSContext, JSObject, JS_GetArrayBufferViewType, Type, Rooted};
use js::jsval::{BooleanValue, DoubleValue, Int32Value, JSVal, NullValue, UndefinedValue}; use js::jsval::{BooleanValue, DoubleValue, Int32Value, JSVal, NullValue, UndefinedValue};
use js::typedarray::{TypedArray, TypedArrayElement, Float32, Int32};
use net_traits::image::base::PixelFormat; use net_traits::image::base::PixelFormat;
use net_traits::image_cache_thread::ImageResponse; use net_traits::image_cache_thread::ImageResponse;
use offscreen_gl_context::{GLContextAttributes, GLLimits}; use offscreen_gl_context::{GLContextAttributes, GLLimits};
@ -775,15 +776,24 @@ impl Drop for WebGLRenderingContext {
#[allow(unsafe_code)] #[allow(unsafe_code)]
unsafe fn typed_array_or_sequence_to_vec<T>(cx: *mut JSContext, unsafe fn typed_array_or_sequence_to_vec<T>(cx: *mut JSContext,
sequence_or_abv: *mut JSObject, sequence_or_abv: *mut JSObject,
config: <T as FromJSValConvertible>::Config) -> Result<Vec<T>, Error> config: <T::Element as FromJSValConvertible>::Config)
where T: ArrayBufferViewContents + FromJSValConvertible, -> Result<Vec<T::Element>, Error>
<T as FromJSValConvertible>::Config: Clone, where T: TypedArrayElement,
T::Element: FromJSValConvertible + Clone,
<T::Element as FromJSValConvertible>::Config: Clone,
{ {
// TODO(servo/rust-mozjs#330): replace this with a macro that supports generic types.
let mut typed_array_root = Rooted::new_unrooted();
let typed_array: Option<TypedArray<T>> =
TypedArray::from(cx, &mut typed_array_root, sequence_or_abv).ok();
if let Some(mut typed_array) = typed_array {
return Ok(typed_array.as_slice().to_vec());
}
assert!(!sequence_or_abv.is_null()); assert!(!sequence_or_abv.is_null());
rooted!(in(cx) let mut val = UndefinedValue()); rooted!(in(cx) let mut val = UndefinedValue());
sequence_or_abv.to_jsval(cx, val.handle_mut()); sequence_or_abv.to_jsval(cx, val.handle_mut());
match Vec::<T>::from_jsval(cx, val.handle(), config) { match Vec::<T::Element>::from_jsval(cx, val.handle(), config) {
Ok(ConversionResult::Success(v)) => Ok(v), Ok(ConversionResult::Success(v)) => Ok(v),
Ok(ConversionResult::Failure(error)) => Err(Error::Type(error.into_owned())), Ok(ConversionResult::Failure(error)) => Err(Error::Type(error.into_owned())),
// FIXME: What to do here? Generated code only aborts the execution of // FIXME: What to do here? Generated code only aborts the execution of
@ -2318,7 +2328,7 @@ impl WebGLRenderingContextMethods for WebGLRenderingContext {
uniform: Option<&WebGLUniformLocation>, uniform: Option<&WebGLUniformLocation>,
data: *mut JSObject) -> Fallible<()> { data: *mut JSObject) -> Fallible<()> {
assert!(!data.is_null()); assert!(!data.is_null());
let data_vec = try!(typed_array_or_sequence_to_vec::<i32>(cx, data, ConversionBehavior::Default)); let data_vec = try!(typed_array_or_sequence_to_vec::<Int32>(cx, data, ConversionBehavior::Default));
if self.validate_uniform_parameters(uniform, UniformSetterType::Int, &data_vec) { if self.validate_uniform_parameters(uniform, UniformSetterType::Int, &data_vec) {
self.ipc_renderer self.ipc_renderer
@ -2336,7 +2346,7 @@ impl WebGLRenderingContextMethods for WebGLRenderingContext {
uniform: Option<&WebGLUniformLocation>, uniform: Option<&WebGLUniformLocation>,
data: *mut JSObject) -> Fallible<()> { data: *mut JSObject) -> Fallible<()> {
assert!(!data.is_null()); assert!(!data.is_null());
let data_vec = try!(typed_array_or_sequence_to_vec::<f32>(cx, data, ())); let data_vec = try!(typed_array_or_sequence_to_vec::<Float32>(cx, data, ()));
if self.validate_uniform_parameters(uniform, UniformSetterType::Float, &data_vec) { if self.validate_uniform_parameters(uniform, UniformSetterType::Float, &data_vec) {
self.ipc_renderer self.ipc_renderer
@ -2365,7 +2375,7 @@ impl WebGLRenderingContextMethods for WebGLRenderingContext {
uniform: Option<&WebGLUniformLocation>, uniform: Option<&WebGLUniformLocation>,
data: *mut JSObject) -> Fallible<()> { data: *mut JSObject) -> Fallible<()> {
assert!(!data.is_null()); assert!(!data.is_null());
let data_vec = try!(typed_array_or_sequence_to_vec::<f32>(cx, data, ())); let data_vec = try!(typed_array_or_sequence_to_vec::<Float32>(cx, data, ()));
if self.validate_uniform_parameters(uniform, if self.validate_uniform_parameters(uniform,
UniformSetterType::FloatVec2, UniformSetterType::FloatVec2,
@ -2398,7 +2408,7 @@ impl WebGLRenderingContextMethods for WebGLRenderingContext {
uniform: Option<&WebGLUniformLocation>, uniform: Option<&WebGLUniformLocation>,
data: *mut JSObject) -> Fallible<()> { data: *mut JSObject) -> Fallible<()> {
assert!(!data.is_null()); assert!(!data.is_null());
let data_vec = try!(typed_array_or_sequence_to_vec::<i32>(cx, data, ConversionBehavior::Default)); let data_vec = try!(typed_array_or_sequence_to_vec::<Int32>(cx, data, ConversionBehavior::Default));
if self.validate_uniform_parameters(uniform, if self.validate_uniform_parameters(uniform,
UniformSetterType::IntVec2, UniformSetterType::IntVec2,
@ -2431,7 +2441,7 @@ impl WebGLRenderingContextMethods for WebGLRenderingContext {
uniform: Option<&WebGLUniformLocation>, uniform: Option<&WebGLUniformLocation>,
data: *mut JSObject) -> Fallible<()> { data: *mut JSObject) -> Fallible<()> {
assert!(!data.is_null()); assert!(!data.is_null());
let data_vec = try!(typed_array_or_sequence_to_vec::<f32>(cx, data, ())); let data_vec = try!(typed_array_or_sequence_to_vec::<Float32>(cx, data, ()));
if self.validate_uniform_parameters(uniform, if self.validate_uniform_parameters(uniform,
UniformSetterType::FloatVec3, UniformSetterType::FloatVec3,
@ -2464,7 +2474,7 @@ impl WebGLRenderingContextMethods for WebGLRenderingContext {
uniform: Option<&WebGLUniformLocation>, uniform: Option<&WebGLUniformLocation>,
data: *mut JSObject) -> Fallible<()> { data: *mut JSObject) -> Fallible<()> {
assert!(!data.is_null()); assert!(!data.is_null());
let data_vec = try!(typed_array_or_sequence_to_vec::<i32>(cx, data, ConversionBehavior::Default)); let data_vec = try!(typed_array_or_sequence_to_vec::<Int32>(cx, data, ConversionBehavior::Default));
if self.validate_uniform_parameters(uniform, if self.validate_uniform_parameters(uniform,
UniformSetterType::IntVec3, UniformSetterType::IntVec3,
@ -2498,7 +2508,7 @@ impl WebGLRenderingContextMethods for WebGLRenderingContext {
uniform: Option<&WebGLUniformLocation>, uniform: Option<&WebGLUniformLocation>,
data: *mut JSObject) -> Fallible<()> { data: *mut JSObject) -> Fallible<()> {
assert!(!data.is_null()); assert!(!data.is_null());
let data_vec = try!(typed_array_or_sequence_to_vec::<i32>(cx, data, ConversionBehavior::Default)); let data_vec = try!(typed_array_or_sequence_to_vec::<Int32>(cx, data, ConversionBehavior::Default));
if self.validate_uniform_parameters(uniform, if self.validate_uniform_parameters(uniform,
UniformSetterType::IntVec4, UniformSetterType::IntVec4,
@ -2531,7 +2541,7 @@ impl WebGLRenderingContextMethods for WebGLRenderingContext {
uniform: Option<&WebGLUniformLocation>, uniform: Option<&WebGLUniformLocation>,
data: *mut JSObject) -> Fallible<()> { data: *mut JSObject) -> Fallible<()> {
assert!(!data.is_null()); assert!(!data.is_null());
let data_vec = try!(typed_array_or_sequence_to_vec::<f32>(cx, data, ())); let data_vec = try!(typed_array_or_sequence_to_vec::<Float32>(cx, data, ()));
if self.validate_uniform_parameters(uniform, if self.validate_uniform_parameters(uniform,
UniformSetterType::FloatVec4, UniformSetterType::FloatVec4,
@ -2552,7 +2562,7 @@ impl WebGLRenderingContextMethods for WebGLRenderingContext {
transpose: bool, transpose: bool,
data: *mut JSObject) -> Fallible<()> { data: *mut JSObject) -> Fallible<()> {
assert!(!data.is_null()); assert!(!data.is_null());
let data_vec = try!(typed_array_or_sequence_to_vec::<f32>(cx, data, ())); let data_vec = try!(typed_array_or_sequence_to_vec::<Float32>(cx, data, ()));
if self.validate_uniform_parameters(uniform, if self.validate_uniform_parameters(uniform,
UniformSetterType::FloatMat2, UniformSetterType::FloatMat2,
&data_vec) { &data_vec) {
@ -2572,7 +2582,7 @@ impl WebGLRenderingContextMethods for WebGLRenderingContext {
transpose: bool, transpose: bool,
data: *mut JSObject) -> Fallible<()> { data: *mut JSObject) -> Fallible<()> {
assert!(!data.is_null()); assert!(!data.is_null());
let data_vec = try!(typed_array_or_sequence_to_vec::<f32>(cx, data, ())); let data_vec = try!(typed_array_or_sequence_to_vec::<Float32>(cx, data, ()));
if self.validate_uniform_parameters(uniform, if self.validate_uniform_parameters(uniform,
UniformSetterType::FloatMat3, UniformSetterType::FloatMat3,
&data_vec) { &data_vec) {
@ -2592,7 +2602,7 @@ impl WebGLRenderingContextMethods for WebGLRenderingContext {
transpose: bool, transpose: bool,
data: *mut JSObject) -> Fallible<()> { data: *mut JSObject) -> Fallible<()> {
assert!(!data.is_null()); assert!(!data.is_null());
let data_vec = try!(typed_array_or_sequence_to_vec::<f32>(cx, data, ())); let data_vec = try!(typed_array_or_sequence_to_vec::<Float32>(cx, data, ()));
if self.validate_uniform_parameters(uniform, if self.validate_uniform_parameters(uniform,
UniformSetterType::FloatMat4, UniformSetterType::FloatMat4,
&data_vec) { &data_vec) {
@ -2632,7 +2642,7 @@ impl WebGLRenderingContextMethods for WebGLRenderingContext {
#[allow(unsafe_code)] #[allow(unsafe_code)]
unsafe fn VertexAttrib1fv(&self, cx: *mut JSContext, indx: u32, data: *mut JSObject) -> Fallible<()> { unsafe fn VertexAttrib1fv(&self, cx: *mut JSContext, indx: u32, data: *mut JSObject) -> Fallible<()> {
assert!(!data.is_null()); assert!(!data.is_null());
let data_vec = try!(typed_array_or_sequence_to_vec::<f32>(cx, data, ())); let data_vec = try!(typed_array_or_sequence_to_vec::<Float32>(cx, data, ()));
if data_vec.len() < 1 { if data_vec.len() < 1 {
return Ok(self.webgl_error(InvalidOperation)); return Ok(self.webgl_error(InvalidOperation));
} }
@ -2649,7 +2659,7 @@ impl WebGLRenderingContextMethods for WebGLRenderingContext {
#[allow(unsafe_code)] #[allow(unsafe_code)]
unsafe fn VertexAttrib2fv(&self, cx: *mut JSContext, indx: u32, data: *mut JSObject) -> Fallible<()> { unsafe fn VertexAttrib2fv(&self, cx: *mut JSContext, indx: u32, data: *mut JSObject) -> Fallible<()> {
assert!(!data.is_null()); assert!(!data.is_null());
let data_vec = try!(typed_array_or_sequence_to_vec::<f32>(cx, data, ())); let data_vec = try!(typed_array_or_sequence_to_vec::<Float32>(cx, data, ()));
if data_vec.len() < 2 { if data_vec.len() < 2 {
return Ok(self.webgl_error(InvalidOperation)); return Ok(self.webgl_error(InvalidOperation));
} }
@ -2666,7 +2676,7 @@ impl WebGLRenderingContextMethods for WebGLRenderingContext {
#[allow(unsafe_code)] #[allow(unsafe_code)]
unsafe fn VertexAttrib3fv(&self, cx: *mut JSContext, indx: u32, data: *mut JSObject) -> Fallible<()> { unsafe fn VertexAttrib3fv(&self, cx: *mut JSContext, indx: u32, data: *mut JSObject) -> Fallible<()> {
assert!(!data.is_null()); assert!(!data.is_null());
let data_vec = try!(typed_array_or_sequence_to_vec::<f32>(cx, data, ())); let data_vec = try!(typed_array_or_sequence_to_vec::<Float32>(cx, data, ()));
if data_vec.len() < 3 { if data_vec.len() < 3 {
return Ok(self.webgl_error(InvalidOperation)); return Ok(self.webgl_error(InvalidOperation));
} }
@ -2683,7 +2693,7 @@ impl WebGLRenderingContextMethods for WebGLRenderingContext {
#[allow(unsafe_code)] #[allow(unsafe_code)]
unsafe fn VertexAttrib4fv(&self, cx: *mut JSContext, indx: u32, data: *mut JSObject) -> Fallible<()> { unsafe fn VertexAttrib4fv(&self, cx: *mut JSContext, indx: u32, data: *mut JSObject) -> Fallible<()> {
assert!(!data.is_null()); assert!(!data.is_null());
let data_vec = try!(typed_array_or_sequence_to_vec::<f32>(cx, data, ())); let data_vec = try!(typed_array_or_sequence_to_vec::<Float32>(cx, data, ()));
if data_vec.len() < 4 { if data_vec.len() < 4 {
return Ok(self.webgl_error(InvalidOperation)); return Ok(self.webgl_error(InvalidOperation));
} }