Skip to content

Commit

Permalink
Rewrite TextEncoder::Encode to use typed array API. Fixes #15504
Browse files Browse the repository at this point in the history
  • Loading branch information
absoludity committed Feb 12, 2017
1 parent 89dcbec commit bb7833a
Showing 1 changed file with 5 additions and 9 deletions.
14 changes: 5 additions & 9 deletions components/script/dom/textencoder.rs
Expand Up @@ -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]
Expand Down Expand Up @@ -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())
}
}

0 comments on commit bb7833a

Please sign in to comment.