Implement WebIDL codegen for ByteString.

This commit is contained in:
Ms2ger 2014-05-01 22:02:53 +02:00
parent 541d581c32
commit 8c879c8bf8
6 changed files with 100 additions and 2 deletions

View file

@ -687,6 +687,24 @@ def getJSToNativeConversionTemplate(type, descriptorProvider, failureCode=None,
return handleOptional(getConversionCode(), CGGeneric(declType), isOptional)
if type.isByteString():
assert not isEnforceRange and not isClamp
conversionCode = (
"match FromJSValConvertible::from_jsval(cx, ${val}, ()) {\n"
" Ok(strval) => strval,\n"
" Err(_) => { %s },\n"
"}" % exceptionCode)
declType = CGGeneric("ByteString")
if type.nullable():
declType = CGWrapper(declType, pre="Option<", post=">")
conversionCode = handleDefaultNull(conversionCode, "None")
else:
assert defaultValue is None
return handleOptional(conversionCode, declType, isOptional)
if type.isEnum():
assert not isEnforceRange and not isClamp
@ -996,6 +1014,11 @@ def getRetvalDeclarationForType(returnType, descriptorProvider):
if returnType.nullable():
result = CGWrapper(result, pre="Option<", post=">")
return result
if returnType.isByteString():
result = CGGeneric("ByteString")
if returnType.nullable():
result = CGWrapper(result, pre="Option<", post=">")
return result
if returnType.isEnum():
result = CGGeneric(returnType.unroll().inner.identifier.name)
if returnType.nullable():
@ -4283,11 +4306,12 @@ class CGBindingRoot(CGThing):
'dom::bindings::error::{FailureUnknown, Fallible, Error, ErrorResult}',
'dom::bindings::error::{throw_method_failed_with_details}',
'dom::bindings::error::throw_type_error',
'script_task::JSPageInfo',
'dom::bindings::proxyhandler',
'dom::bindings::proxyhandler::{_obj_toString, defineProperty}',
'dom::bindings::proxyhandler::{FillPropertyDescriptor, GetExpandoObject}',
'dom::bindings::proxyhandler::{getPropertyDescriptor}',
'dom::bindings::str::ByteString',
'script_task::JSPageInfo',
'libc',
'servo_util::str::DOMString',
'servo_util::vec::zip_copies',

View file

@ -3,6 +3,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use dom::bindings::js::JS;
use dom::bindings::str::ByteString;
use dom::bindings::utils::Reflectable;
use dom::bindings::utils::jsstring_to_str;
use dom::bindings::utils::unwrap_jsmanaged;
@ -12,7 +13,8 @@ use js::jsapi::{JSBool, JSContext};
use js::jsapi::{JS_ValueToUint64, JS_ValueToInt64};
use js::jsapi::{JS_ValueToECMAUint32, JS_ValueToECMAInt32};
use js::jsapi::{JS_ValueToUint16, JS_ValueToNumber, JS_ValueToBoolean};
use js::jsapi::{JS_NewUCStringCopyN, JS_ValueToString};
use js::jsapi::{JS_ValueToString, JS_GetStringCharsAndLength};
use js::jsapi::{JS_NewUCStringCopyN, JS_NewStringCopyN};
use js::jsapi::{JS_WrapValue};
use js::jsval::JSVal;
use js::jsval::{UndefinedValue, NullValue, BooleanValue, Int32Value, UInt32Value};
@ -20,6 +22,7 @@ use js::jsval::{StringValue, ObjectValue};
use js::glue::RUST_JS_NumberValue;
use libc;
use std::default::Default;
use std::slice;
use dom::bindings::codegen::PrototypeList;
@ -253,6 +256,43 @@ impl FromJSValConvertible<StringificationBehavior> for DOMString {
}
}
impl ToJSValConvertible for ByteString {
fn to_jsval(&self, cx: *JSContext) -> JSVal {
unsafe {
let slice = self.as_slice();
let jsstr = JS_NewStringCopyN(cx, slice.as_ptr() as *libc::c_char,
slice.len() as libc::size_t);
if jsstr.is_null() {
fail!("JS_NewStringCopyN failed");
}
StringValue(&*jsstr)
}
}
}
impl FromJSValConvertible<()> for ByteString {
fn from_jsval(cx: *JSContext, value: JSVal, _option: ()) -> Result<ByteString, ()> {
unsafe {
let string = JS_ValueToString(cx, value);
if string.is_null() {
debug!("JS_ValueToString failed");
return Err(());
}
let mut length = 0;
let chars = JS_GetStringCharsAndLength(cx, string, &mut length as *mut _ as *_);
slice::raw::buf_as_slice(chars, length as uint, |char_vec| {
if char_vec.iter().any(|&c| c > 0xFF) {
// XXX Throw
Err(())
} else {
Ok(ByteString::new(char_vec.iter().map(|&c| c as u8).collect()))
}
})
}
}
}
impl<T: Reflectable> ToJSValConvertible for JS<T> {
fn to_jsval(&self, cx: *JSContext) -> JSVal {
let obj = self.reflector().get_jsobject();

View file

@ -0,0 +1,16 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
pub struct ByteString(Vec<u8>);
impl ByteString {
pub fn new(value: Vec<u8>) -> ByteString {
ByteString(value)
}
pub fn as_slice<'a>(&'a self) -> &'a [u8] {
let ByteString(ref vector) = *self;
vector.as_slice()
}
}