From baf8c5462a45dfa6dbcc0f41140d1be66290f613 Mon Sep 17 00:00:00 2001 From: Josh Matthews Date: Thu, 2 Feb 2017 12:02:13 -0500 Subject: [PATCH] Make typed_array_or_sequence_to_vec support typed arrays. --- Cargo.lock | 2 +- .../script/dom/webglrenderingcontext.rs | 52 +++++++++++-------- 2 files changed, 32 insertions(+), 22 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index de3c8529c3af..b9380dc554dc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1288,7 +1288,7 @@ dependencies = [ [[package]] name = "js" version = "0.1.4" -source = "git+https://github.com/servo/rust-mozjs#4ad9326fc70d13294c77f9f0988fda6abfd0023b" +source = "git+https://github.com/servo/rust-mozjs#cc9185025d2655074b29cc0a4bf5ee450356ac5f" dependencies = [ "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)", diff --git a/components/script/dom/webglrenderingcontext.rs b/components/script/dom/webglrenderingcontext.rs index f4de68e2addc..6c9aa68db9bd 100644 --- a/components/script/dom/webglrenderingcontext.rs +++ b/components/script/dom/webglrenderingcontext.rs @@ -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::WebGLRenderingContextMethods; 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::inheritance::Castable; use dom::bindings::js::{JS, LayoutJS, MutNullableJS, Root}; @@ -37,8 +37,9 @@ use dom::window::Window; use euclid::size::Size2D; use ipc_channel::ipc::{self, IpcSender}; 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::typedarray::{TypedArray, TypedArrayElement, Float32, Int32}; use net_traits::image::base::PixelFormat; use net_traits::image_cache_thread::ImageResponse; use offscreen_gl_context::{GLContextAttributes, GLLimits}; @@ -775,15 +776,24 @@ impl Drop for WebGLRenderingContext { #[allow(unsafe_code)] unsafe fn typed_array_or_sequence_to_vec(cx: *mut JSContext, sequence_or_abv: *mut JSObject, - config: ::Config) -> Result, Error> - where T: ArrayBufferViewContents + FromJSValConvertible, - ::Config: Clone, + config: ::Config) + -> Result, Error> + where T: TypedArrayElement, + T::Element: FromJSValConvertible + Clone, + ::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::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()); rooted!(in(cx) let mut val = UndefinedValue()); sequence_or_abv.to_jsval(cx, val.handle_mut()); - match Vec::::from_jsval(cx, val.handle(), config) { + match Vec::::from_jsval(cx, val.handle(), config) { Ok(ConversionResult::Success(v)) => Ok(v), Ok(ConversionResult::Failure(error)) => Err(Error::Type(error.into_owned())), // FIXME: What to do here? Generated code only aborts the execution of @@ -2318,7 +2328,7 @@ impl WebGLRenderingContextMethods for WebGLRenderingContext { uniform: Option<&WebGLUniformLocation>, data: *mut JSObject) -> Fallible<()> { assert!(!data.is_null()); - let data_vec = try!(typed_array_or_sequence_to_vec::(cx, data, ConversionBehavior::Default)); + let data_vec = try!(typed_array_or_sequence_to_vec::(cx, data, ConversionBehavior::Default)); if self.validate_uniform_parameters(uniform, UniformSetterType::Int, &data_vec) { self.ipc_renderer @@ -2336,7 +2346,7 @@ impl WebGLRenderingContextMethods for WebGLRenderingContext { uniform: Option<&WebGLUniformLocation>, data: *mut JSObject) -> Fallible<()> { assert!(!data.is_null()); - let data_vec = try!(typed_array_or_sequence_to_vec::(cx, data, ())); + let data_vec = try!(typed_array_or_sequence_to_vec::(cx, data, ())); if self.validate_uniform_parameters(uniform, UniformSetterType::Float, &data_vec) { self.ipc_renderer @@ -2365,7 +2375,7 @@ impl WebGLRenderingContextMethods for WebGLRenderingContext { uniform: Option<&WebGLUniformLocation>, data: *mut JSObject) -> Fallible<()> { assert!(!data.is_null()); - let data_vec = try!(typed_array_or_sequence_to_vec::(cx, data, ())); + let data_vec = try!(typed_array_or_sequence_to_vec::(cx, data, ())); if self.validate_uniform_parameters(uniform, UniformSetterType::FloatVec2, @@ -2398,7 +2408,7 @@ impl WebGLRenderingContextMethods for WebGLRenderingContext { uniform: Option<&WebGLUniformLocation>, data: *mut JSObject) -> Fallible<()> { assert!(!data.is_null()); - let data_vec = try!(typed_array_or_sequence_to_vec::(cx, data, ConversionBehavior::Default)); + let data_vec = try!(typed_array_or_sequence_to_vec::(cx, data, ConversionBehavior::Default)); if self.validate_uniform_parameters(uniform, UniformSetterType::IntVec2, @@ -2431,7 +2441,7 @@ impl WebGLRenderingContextMethods for WebGLRenderingContext { uniform: Option<&WebGLUniformLocation>, data: *mut JSObject) -> Fallible<()> { assert!(!data.is_null()); - let data_vec = try!(typed_array_or_sequence_to_vec::(cx, data, ())); + let data_vec = try!(typed_array_or_sequence_to_vec::(cx, data, ())); if self.validate_uniform_parameters(uniform, UniformSetterType::FloatVec3, @@ -2464,7 +2474,7 @@ impl WebGLRenderingContextMethods for WebGLRenderingContext { uniform: Option<&WebGLUniformLocation>, data: *mut JSObject) -> Fallible<()> { assert!(!data.is_null()); - let data_vec = try!(typed_array_or_sequence_to_vec::(cx, data, ConversionBehavior::Default)); + let data_vec = try!(typed_array_or_sequence_to_vec::(cx, data, ConversionBehavior::Default)); if self.validate_uniform_parameters(uniform, UniformSetterType::IntVec3, @@ -2498,7 +2508,7 @@ impl WebGLRenderingContextMethods for WebGLRenderingContext { uniform: Option<&WebGLUniformLocation>, data: *mut JSObject) -> Fallible<()> { assert!(!data.is_null()); - let data_vec = try!(typed_array_or_sequence_to_vec::(cx, data, ConversionBehavior::Default)); + let data_vec = try!(typed_array_or_sequence_to_vec::(cx, data, ConversionBehavior::Default)); if self.validate_uniform_parameters(uniform, UniformSetterType::IntVec4, @@ -2531,7 +2541,7 @@ impl WebGLRenderingContextMethods for WebGLRenderingContext { uniform: Option<&WebGLUniformLocation>, data: *mut JSObject) -> Fallible<()> { assert!(!data.is_null()); - let data_vec = try!(typed_array_or_sequence_to_vec::(cx, data, ())); + let data_vec = try!(typed_array_or_sequence_to_vec::(cx, data, ())); if self.validate_uniform_parameters(uniform, UniformSetterType::FloatVec4, @@ -2552,7 +2562,7 @@ impl WebGLRenderingContextMethods for WebGLRenderingContext { transpose: bool, data: *mut JSObject) -> Fallible<()> { assert!(!data.is_null()); - let data_vec = try!(typed_array_or_sequence_to_vec::(cx, data, ())); + let data_vec = try!(typed_array_or_sequence_to_vec::(cx, data, ())); if self.validate_uniform_parameters(uniform, UniformSetterType::FloatMat2, &data_vec) { @@ -2572,7 +2582,7 @@ impl WebGLRenderingContextMethods for WebGLRenderingContext { transpose: bool, data: *mut JSObject) -> Fallible<()> { assert!(!data.is_null()); - let data_vec = try!(typed_array_or_sequence_to_vec::(cx, data, ())); + let data_vec = try!(typed_array_or_sequence_to_vec::(cx, data, ())); if self.validate_uniform_parameters(uniform, UniformSetterType::FloatMat3, &data_vec) { @@ -2592,7 +2602,7 @@ impl WebGLRenderingContextMethods for WebGLRenderingContext { transpose: bool, data: *mut JSObject) -> Fallible<()> { assert!(!data.is_null()); - let data_vec = try!(typed_array_or_sequence_to_vec::(cx, data, ())); + let data_vec = try!(typed_array_or_sequence_to_vec::(cx, data, ())); if self.validate_uniform_parameters(uniform, UniformSetterType::FloatMat4, &data_vec) { @@ -2632,7 +2642,7 @@ impl WebGLRenderingContextMethods for WebGLRenderingContext { #[allow(unsafe_code)] unsafe fn VertexAttrib1fv(&self, cx: *mut JSContext, indx: u32, data: *mut JSObject) -> Fallible<()> { assert!(!data.is_null()); - let data_vec = try!(typed_array_or_sequence_to_vec::(cx, data, ())); + let data_vec = try!(typed_array_or_sequence_to_vec::(cx, data, ())); if data_vec.len() < 1 { return Ok(self.webgl_error(InvalidOperation)); } @@ -2649,7 +2659,7 @@ impl WebGLRenderingContextMethods for WebGLRenderingContext { #[allow(unsafe_code)] unsafe fn VertexAttrib2fv(&self, cx: *mut JSContext, indx: u32, data: *mut JSObject) -> Fallible<()> { assert!(!data.is_null()); - let data_vec = try!(typed_array_or_sequence_to_vec::(cx, data, ())); + let data_vec = try!(typed_array_or_sequence_to_vec::(cx, data, ())); if data_vec.len() < 2 { return Ok(self.webgl_error(InvalidOperation)); } @@ -2666,7 +2676,7 @@ impl WebGLRenderingContextMethods for WebGLRenderingContext { #[allow(unsafe_code)] unsafe fn VertexAttrib3fv(&self, cx: *mut JSContext, indx: u32, data: *mut JSObject) -> Fallible<()> { assert!(!data.is_null()); - let data_vec = try!(typed_array_or_sequence_to_vec::(cx, data, ())); + let data_vec = try!(typed_array_or_sequence_to_vec::(cx, data, ())); if data_vec.len() < 3 { return Ok(self.webgl_error(InvalidOperation)); } @@ -2683,7 +2693,7 @@ impl WebGLRenderingContextMethods for WebGLRenderingContext { #[allow(unsafe_code)] unsafe fn VertexAttrib4fv(&self, cx: *mut JSContext, indx: u32, data: *mut JSObject) -> Fallible<()> { assert!(!data.is_null()); - let data_vec = try!(typed_array_or_sequence_to_vec::(cx, data, ())); + let data_vec = try!(typed_array_or_sequence_to_vec::(cx, data, ())); if data_vec.len() < 4 { return Ok(self.webgl_error(InvalidOperation)); }