mirror of
https://github.com/servo/servo.git
synced 2025-08-06 14:10:11 +01:00
add str ToJSValConvertible for str type
This commit is contained in:
parent
37a97f3273
commit
6f569dee92
1 changed files with 14 additions and 1 deletions
|
@ -44,7 +44,7 @@ pub trait IDLInterface {
|
|||
}
|
||||
|
||||
/// A trait to convert Rust types to `JSVal`s.
|
||||
pub trait ToJSValConvertible {
|
||||
pub trait ToJSValConvertible for Sized? {
|
||||
/// Convert `self` to a `JSVal`. JSAPI failure causes a task failure.
|
||||
fn to_jsval(&self, cx: *mut JSContext) -> JSVal;
|
||||
}
|
||||
|
@ -232,6 +232,19 @@ impl FromJSValConvertible<()> for f64 {
|
|||
}
|
||||
}
|
||||
|
||||
impl ToJSValConvertible for str {
|
||||
fn to_jsval(&self, cx: *mut JSContext) -> JSVal {
|
||||
unsafe {
|
||||
let string_utf16: Vec<u16> = self.utf16_units().collect();
|
||||
let jsstr = JS_NewUCStringCopyN(cx, string_utf16.as_ptr(), string_utf16.len() as libc::size_t);
|
||||
if jsstr.is_null() {
|
||||
panic!("JS_NewUCStringCopyN failed");
|
||||
}
|
||||
StringValue(&*jsstr)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ToJSValConvertible for DOMString {
|
||||
fn to_jsval(&self, cx: *mut JSContext) -> JSVal {
|
||||
unsafe {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue