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

TextDecoder's Decode now receives a BufferSource as input #20413

Merged
merged 1 commit into from Mar 24, 2018
Merged
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.

Always

Just for now

TextDecoder's Decode now receives a BufferSource as input

  • Loading branch information
christianpoveda committed Mar 24, 2018
commit 2c47e7e1db2d31e6f05b0fbe83a73e7d2dd94595
@@ -3,15 +3,15 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

use dom::bindings::codegen::Bindings::TextDecoderBinding;
use dom::bindings::codegen::Bindings::TextDecoderBinding::TextDecoderMethods;
use dom::bindings::codegen::Bindings::TextDecoderBinding::{TextDecoderMethods, TextDecodeOptions};
use dom::bindings::codegen::UnionTypes::ArrayBufferViewOrArrayBuffer;
use dom::bindings::error::{Error, Fallible};
use dom::bindings::reflector::{Reflector, reflect_dom_object};
use dom::bindings::root::DomRoot;
use dom::bindings::str::{DOMString, USVString};
use dom::globalscope::GlobalScope;
use dom_struct::dom_struct;
use encoding_rs::Encoding;
use js::jsapi::{JSContext, JSObject};
use std::borrow::ToOwned;

#[dom_struct]
@@ -65,32 +65,30 @@ impl TextDecoderMethods for TextDecoder {
self.fatal
}

#[allow(unsafe_code)]
// https://encoding.spec.whatwg.org/#dom-textdecoder-decode
unsafe 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())),
};

typedarray!(in(_cx) let data_res: ArrayBufferView = input);
let mut data = match data_res {
Ok(data) => data,
Err(_) => {
return Err(Error::Type("Argument to TextDecoder.decode is not an ArrayBufferView".to_owned()));
fn Decode(
&self,
input: Option<ArrayBufferViewOrArrayBuffer>,
_options: &TextDecodeOptions
) -> Fallible<USVString> {
match input {
Some(arr) => {
let vec: Vec<u8> = match arr {
ArrayBufferViewOrArrayBuffer::ArrayBufferView(mut a) => a.to_vec(),
ArrayBufferViewOrArrayBuffer::ArrayBuffer(mut a) => a.to_vec()
};
let s = if self.fatal {
match self.encoding.decode_without_bom_handling_and_without_replacement(&vec) {
Some(s) => s,
None => return Err(Error::Type("Decoding failed".to_owned())),
}
} else {
let (s, _has_errors) = self.encoding.decode_without_bom_handling(&vec);
s
};
Ok(USVString(s.into_owned()))
}
};

let s = if self.fatal {
match self.encoding.decode_without_bom_handling_and_without_replacement(data.as_slice()) {
Some(s) => s,
None => return Err(Error::Type("Decoding failed".to_owned())),
}
} else {
let (s, _has_errors) = self.encoding.decode_without_bom_handling(data.as_slice());
s
};
Ok(USVString(s.into_owned()))
None => Ok(USVString("".to_owned()))
}
}
}
@@ -5,15 +5,18 @@
// https://encoding.spec.whatwg.org/#interface-textdecoder
dictionary TextDecoderOptions {
boolean fatal = false;
//boolean ignoreBOM = false;
// boolean ignoreBOM = false;
};

dictionary TextDecodeOptions {
// boolean stream = false;
};

[Constructor(optional DOMString label = "utf-8", optional TextDecoderOptions options), Exposed=(Window,Worker)]
interface TextDecoder {
readonly attribute DOMString encoding;
readonly attribute boolean fatal;
//readonly attribute boolean ignoreBOM;
//USVString decode(optional BufferSource input, optional TextDecodeOptions options);
// readonly attribute boolean ignoreBOM;
[Throws]
USVString decode(optional object input);
USVString decode(optional BufferSource input, optional TextDecodeOptions options);
};

This file was deleted.

ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.