Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement TextDecoder #5058

Closed
wants to merge 5 commits into from
Closed
Changes from 1 commit
Commits
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.

Always

Just for now

Prev

Fixed decode method and added match statement to encoding method

  • Loading branch information
KiChjang committed Mar 1, 2015
commit 69dc71b268e2e5b112538a7cbf314a5154c4aca5
@@ -15,12 +15,13 @@ use encoding::types::EncodingRef;
use encoding::{Encoding, DecoderTrap};
use encoding::label::encoding_from_whatwg_label;

use js::jsfriendapi::bindgen::{JS_NewUint8Array, JS_GetUint8ArrayData};
use js::jsfriendapi::bindgen::{JS_GetUint8ArrayData, JS_GetArrayBufferByteLength};
use js::jsapi::JSTracer;

use util::str::DOMString;

use std::borrow::ToOwned;
use std::slice::from_raw_parts;

#[dom_struct]
pub struct TextDecoder {
@@ -60,14 +61,18 @@ impl TextDecoder {
}

impl<'a> TextDecoderMethods for JSRef<'a, TextDecoder> {
pub fn Decode(self, cx: *mut JSContext, input: *mut JSObject) -> DOMString {
pub fn Decode(self, cx: *mut JSContext, input: *mut JSObject) -> Fallible<DOMString> {
let length: usize = JS_GetArrayBufferByteLength(input, cx) as usize;
let stream: *const uint8_t = JS_GetUint8ArrayData(input, cx) as *const uint8_t;
let trap = if self.fatal { DecoderTrap::Strict } else { DecoderTrap::Replace };
unsafe { self.encoding.decode(stream as &[u8], trap).unwrap() }
unsafe { self.encoding.decode(from_raw_parts(stream, length), trap) }
}

fn Encoding(self) -> DOMString {
self.encoding.whatwg_name().unwrap().to_owned()
match self.encoding.whatwg_name() {
Some(enc) => enc.to_owned(),
None => "Unknown"
}
}

fn Fatal(self) -> bool {
@@ -24,5 +24,5 @@ interface TextDecoder {
readonly attribute boolean ignoreBOM;
// FIXME: decode should return a USVString instead, and ArrayBuffer should really be BufferSource
[Throws]
DOMString decode(optional ArrayBuffer input, optional TextDecodeOptions options);
DOMString decode(optional ArrayBuffer input/*, optional TextDecodeOptions options*/);
};
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.