diff --git a/components/script/dom/textencoder.rs b/components/script/dom/textencoder.rs index 1bb354187145..7cb0443d17a7 100644 --- a/components/script/dom/textencoder.rs +++ b/components/script/dom/textencoder.rs @@ -14,8 +14,7 @@ use encoding::EncoderTrap; use encoding::Encoding; use encoding::all::UTF_8; use js::jsapi::{JSContext, JSObject}; -use js::jsapi::{JS_GetUint8ArrayData, JS_NewUint8Array}; -use libc::uint8_t; +use js::typedarray::Uint8Array; use std::ptr; #[dom_struct] @@ -52,13 +51,10 @@ impl TextEncoderMethods for TextEncoder { // https://encoding.spec.whatwg.org/#dom-textencoder-encode unsafe fn Encode(&self, cx: *mut JSContext, input: USVString) -> NonZero<*mut JSObject> { let encoded = UTF_8.encode(&input.0, EncoderTrap::Strict).unwrap(); - let length = encoded.len() as u32; - rooted!(in(cx) let js_object = JS_NewUint8Array(cx, length)); - assert!(!js_object.is_null()); - let mut is_shared = false; - let js_object_data: *mut uint8_t = JS_GetUint8ArrayData(js_object.get(), &mut is_shared, ptr::null()); - assert!(!is_shared); - ptr::copy_nonoverlapping(encoded.as_ptr(), js_object_data, length as usize); + + rooted!(in(cx) let mut js_object = ptr::null_mut()); + assert!(Uint8Array::create(cx, encoded.len() as u32, Some(encoded.as_slice()), js_object.handle_mut()).is_ok()); + NonZero::new(js_object.get()) } }