mirror of
https://github.com/servo/servo.git
synced 2025-08-03 20:50:07 +01:00
Rewrite TextEncoder::Encode to use typed array API. Fixes #15504
This commit is contained in:
parent
89dcbec0ba
commit
bb7833a7fc
1 changed files with 5 additions and 9 deletions
|
@ -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())
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue