mirror of
https://github.com/servo/servo.git
synced 2025-08-02 12:10:29 +01:00
Implement TextDecoder#decode.
This commit is contained in:
parent
c0d4e27d79
commit
e2929403ef
8 changed files with 37 additions and 142 deletions
|
@ -7,16 +7,21 @@ use dom::bindings::codegen::Bindings::TextDecoderBinding::TextDecoderMethods;
|
|||
use dom::bindings::error::{Error, Fallible};
|
||||
use dom::bindings::global::GlobalRef;
|
||||
use dom::bindings::js::{JSRef, Temporary};
|
||||
use dom::bindings::str::USVString;
|
||||
use dom::bindings::trace::JSTraceable;
|
||||
use dom::bindings::utils::{Reflector, reflect_dom_object};
|
||||
|
||||
use util::str::DOMString;
|
||||
|
||||
use encoding::Encoding;
|
||||
use encoding::types::EncodingRef;
|
||||
use encoding::types::{EncodingRef, DecoderTrap};
|
||||
use encoding::label::encoding_from_whatwg_label;
|
||||
use js::jsapi::{JSContext, JSObject};
|
||||
use js::jsfriendapi::bindgen::JS_GetObjectAsArrayBufferView;
|
||||
|
||||
use std::borrow::ToOwned;
|
||||
use std::ptr;
|
||||
use std::slice;
|
||||
|
||||
#[dom_struct]
|
||||
pub struct TextDecoder {
|
||||
|
@ -62,4 +67,33 @@ impl<'a> TextDecoderMethods for JSRef<'a, TextDecoder> {
|
|||
fn Fatal(self) -> bool {
|
||||
self.fatal
|
||||
}
|
||||
|
||||
#[allow(unsafe_code)]
|
||||
fn Decode(self, cx: *mut JSContext, input: Option<*mut JSObject>)
|
||||
-> Fallible<USVString> {
|
||||
let input = match input {
|
||||
Some(input) => input,
|
||||
None => return Ok(USVString("".to_owned())),
|
||||
};
|
||||
|
||||
let mut length = 0;
|
||||
let mut data = ptr::null_mut();
|
||||
if unsafe { JS_GetObjectAsArrayBufferView(cx, input, &mut length, &mut data).is_null() } {
|
||||
return Err(Error::Type("Argument to TextDecoder.decode is not an ArrayBufferView".to_owned()));
|
||||
}
|
||||
|
||||
let buffer = unsafe {
|
||||
slice::from_raw_parts(data as *const _, length as usize)
|
||||
};
|
||||
let trap = if self.fatal {
|
||||
DecoderTrap::Strict
|
||||
} else {
|
||||
DecoderTrap::Replace
|
||||
};
|
||||
match self.encoding.decode(buffer, trap) {
|
||||
Ok(s) => Ok(USVString(s)),
|
||||
Err(_) => Err(Error::Type("Decoding failed".to_owned())),
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -16,4 +16,6 @@ interface TextDecoder {
|
|||
readonly attribute boolean fatal;
|
||||
//readonly attribute boolean ignoreBOM;
|
||||
//USVString decode(optional BufferSource input, optional TextDecodeOptions options);
|
||||
[Throws]
|
||||
USVString decode(optional object input);
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue