mirror of
https://github.com/servo/servo.git
synced 2025-08-05 05:30:08 +01:00
Remove some usage of unsafe code in HTMLCanvasElement
This commit is contained in:
parent
d1282dc8cc
commit
1d92796b03
1 changed files with 26 additions and 33 deletions
|
@ -28,7 +28,7 @@ use crate::dom::webgl2renderingcontext::WebGL2RenderingContext;
|
||||||
use crate::dom::webglrenderingcontext::{
|
use crate::dom::webglrenderingcontext::{
|
||||||
LayoutCanvasWebGLRenderingContextHelpers, WebGLRenderingContext,
|
LayoutCanvasWebGLRenderingContextHelpers, WebGLRenderingContext,
|
||||||
};
|
};
|
||||||
use crate::script_runtime::JSContext as SafeJSContext;
|
use crate::script_runtime::JSContext;
|
||||||
use base64;
|
use base64;
|
||||||
use canvas_traits::canvas::{CanvasId, CanvasMsg, FromScriptMsg};
|
use canvas_traits::canvas::{CanvasId, CanvasMsg, FromScriptMsg};
|
||||||
use canvas_traits::webgl::{GLContextAttributes, WebGLVersion};
|
use canvas_traits::webgl::{GLContextAttributes, WebGLVersion};
|
||||||
|
@ -39,7 +39,6 @@ use image::png::PNGEncoder;
|
||||||
use image::ColorType;
|
use image::ColorType;
|
||||||
use ipc_channel::ipc::IpcSharedMemory;
|
use ipc_channel::ipc::IpcSharedMemory;
|
||||||
use js::error::throw_type_error;
|
use js::error::throw_type_error;
|
||||||
use js::jsapi::JSContext;
|
|
||||||
use js::rust::HandleValue;
|
use js::rust::HandleValue;
|
||||||
use profile_traits::ipc;
|
use profile_traits::ipc;
|
||||||
use script_layout_interface::{HTMLCanvasData, HTMLCanvasDataSource};
|
use script_layout_interface::{HTMLCanvasData, HTMLCanvasDataSource};
|
||||||
|
@ -207,10 +206,9 @@ impl HTMLCanvasElement {
|
||||||
Some(context)
|
Some(context)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(unsafe_code)]
|
fn get_or_init_webgl_context(
|
||||||
unsafe fn get_or_init_webgl_context(
|
|
||||||
&self,
|
&self,
|
||||||
cx: *mut JSContext,
|
cx: JSContext,
|
||||||
options: HandleValue,
|
options: HandleValue,
|
||||||
) -> Option<DomRoot<WebGLRenderingContext>> {
|
) -> Option<DomRoot<WebGLRenderingContext>> {
|
||||||
if let Some(ctx) = self.context() {
|
if let Some(ctx) = self.context() {
|
||||||
|
@ -227,10 +225,9 @@ impl HTMLCanvasElement {
|
||||||
Some(context)
|
Some(context)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(unsafe_code)]
|
fn get_or_init_webgl2_context(
|
||||||
unsafe fn get_or_init_webgl2_context(
|
|
||||||
&self,
|
&self,
|
||||||
cx: *mut JSContext,
|
cx: JSContext,
|
||||||
options: HandleValue,
|
options: HandleValue,
|
||||||
) -> Option<DomRoot<WebGL2RenderingContext>> {
|
) -> Option<DomRoot<WebGL2RenderingContext>> {
|
||||||
if !pref!(dom.webgl2.enabled) {
|
if !pref!(dom.webgl2.enabled) {
|
||||||
|
@ -260,20 +257,19 @@ impl HTMLCanvasElement {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(unsafe_code)]
|
#[allow(unsafe_code)]
|
||||||
unsafe fn get_gl_attributes(
|
fn get_gl_attributes(cx: JSContext, options: HandleValue) -> Option<GLContextAttributes> {
|
||||||
cx: *mut JSContext,
|
unsafe {
|
||||||
options: HandleValue,
|
match WebGLContextAttributes::new(cx, options) {
|
||||||
) -> Option<GLContextAttributes> {
|
Ok(ConversionResult::Success(ref attrs)) => Some(From::from(attrs)),
|
||||||
match WebGLContextAttributes::new(SafeJSContext::from_ptr(cx), options) {
|
Ok(ConversionResult::Failure(ref error)) => {
|
||||||
Ok(ConversionResult::Success(ref attrs)) => Some(From::from(attrs)),
|
throw_type_error(*cx, &error);
|
||||||
Ok(ConversionResult::Failure(ref error)) => {
|
None
|
||||||
throw_type_error(cx, &error);
|
},
|
||||||
None
|
_ => {
|
||||||
},
|
debug!("Unexpected error on conversion of WebGLContextAttributes");
|
||||||
_ => {
|
None
|
||||||
debug!("Unexpected error on conversion of WebGLContextAttributes");
|
},
|
||||||
None
|
}
|
||||||
},
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -329,10 +325,9 @@ impl HTMLCanvasElementMethods for HTMLCanvasElement {
|
||||||
make_uint_setter!(SetHeight, "height", DEFAULT_HEIGHT);
|
make_uint_setter!(SetHeight, "height", DEFAULT_HEIGHT);
|
||||||
|
|
||||||
// https://html.spec.whatwg.org/multipage/#dom-canvas-getcontext
|
// https://html.spec.whatwg.org/multipage/#dom-canvas-getcontext
|
||||||
#[allow(unsafe_code)]
|
|
||||||
fn GetContext(
|
fn GetContext(
|
||||||
&self,
|
&self,
|
||||||
cx: SafeJSContext,
|
cx: JSContext,
|
||||||
id: DOMString,
|
id: DOMString,
|
||||||
options: HandleValue,
|
options: HandleValue,
|
||||||
) -> Option<RenderingContext> {
|
) -> Option<RenderingContext> {
|
||||||
|
@ -340,14 +335,12 @@ impl HTMLCanvasElementMethods for HTMLCanvasElement {
|
||||||
"2d" => self
|
"2d" => self
|
||||||
.get_or_init_2d_context()
|
.get_or_init_2d_context()
|
||||||
.map(RenderingContext::CanvasRenderingContext2D),
|
.map(RenderingContext::CanvasRenderingContext2D),
|
||||||
"webgl" | "experimental-webgl" => unsafe {
|
"webgl" | "experimental-webgl" => self
|
||||||
self.get_or_init_webgl_context(*cx, options)
|
.get_or_init_webgl_context(cx, options)
|
||||||
.map(RenderingContext::WebGLRenderingContext)
|
.map(RenderingContext::WebGLRenderingContext),
|
||||||
},
|
"webgl2" | "experimental-webgl2" => self
|
||||||
"webgl2" | "experimental-webgl2" => unsafe {
|
.get_or_init_webgl2_context(cx, options)
|
||||||
self.get_or_init_webgl2_context(*cx, options)
|
.map(RenderingContext::WebGL2RenderingContext),
|
||||||
.map(RenderingContext::WebGL2RenderingContext)
|
|
||||||
},
|
|
||||||
_ => None,
|
_ => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -355,7 +348,7 @@ impl HTMLCanvasElementMethods for HTMLCanvasElement {
|
||||||
// https://html.spec.whatwg.org/multipage/#dom-canvas-todataurl
|
// https://html.spec.whatwg.org/multipage/#dom-canvas-todataurl
|
||||||
fn ToDataURL(
|
fn ToDataURL(
|
||||||
&self,
|
&self,
|
||||||
_context: SafeJSContext,
|
_context: JSContext,
|
||||||
_mime_type: Option<DOMString>,
|
_mime_type: Option<DOMString>,
|
||||||
_quality: HandleValue,
|
_quality: HandleValue,
|
||||||
) -> Fallible<USVString> {
|
) -> Fallible<USVString> {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue