mirror of
https://github.com/servo/servo.git
synced 2025-08-03 12:40:06 +01:00
auto merge of #1077 : kmcallister/servo/jsstr, r=jdm
This doesn't resolve the big questions of how Servo will represent strings; it's just about doing the conversion correctly for our existing types.
This commit is contained in:
commit
ebe1c1353c
5 changed files with 32 additions and 25 deletions
|
@ -14,17 +14,17 @@ use std::libc;
|
||||||
use std::ptr;
|
use std::ptr;
|
||||||
use std::ptr::{null, to_unsafe_ptr};
|
use std::ptr::{null, to_unsafe_ptr};
|
||||||
use std::str;
|
use std::str;
|
||||||
|
use std::vec;
|
||||||
use std::unstable::raw::Box;
|
use std::unstable::raw::Box;
|
||||||
use js::glue::*;
|
use js::glue::*;
|
||||||
use js::glue::{DefineFunctionWithReserved, GetObjectJSClass, RUST_OBJECT_TO_JSVAL};
|
use js::glue::{DefineFunctionWithReserved, GetObjectJSClass, RUST_OBJECT_TO_JSVAL};
|
||||||
use js::glue::{js_IsObjectProxyClass, js_IsFunctionProxyClass, IsProxyHandlerFamily};
|
use js::glue::{js_IsObjectProxyClass, js_IsFunctionProxyClass, IsProxyHandlerFamily};
|
||||||
use js::jsapi::{JS_AlreadyHasOwnProperty, JS_NewObject, JS_NewFunction, JS_GetGlobalObject};
|
use js::jsapi::{JS_AlreadyHasOwnProperty, JS_NewObject, JS_NewFunction, JS_GetGlobalObject};
|
||||||
use js::jsapi::{JS_DefineProperties, JS_WrapValue, JS_ForwardGetPropertyTo};
|
use js::jsapi::{JS_DefineProperties, JS_WrapValue, JS_ForwardGetPropertyTo};
|
||||||
use js::jsapi::{JS_EncodeString, JS_free, JS_GetStringCharsAndLength};
|
use js::jsapi::{JS_GetClass, JS_LinkConstructorAndPrototype, JS_GetStringCharsAndLength};
|
||||||
use js::jsapi::{JS_GetClass, JS_LinkConstructorAndPrototype};
|
|
||||||
use js::jsapi::{JS_GetFunctionPrototype, JS_InternString, JS_GetFunctionObject};
|
use js::jsapi::{JS_GetFunctionPrototype, JS_InternString, JS_GetFunctionObject};
|
||||||
use js::jsapi::{JS_HasPropertyById, JS_GetPrototype, JS_GetGlobalForObject};
|
use js::jsapi::{JS_HasPropertyById, JS_GetPrototype, JS_GetGlobalForObject};
|
||||||
use js::jsapi::{JS_NewStringCopyN, JS_DefineFunctions, JS_DefineProperty};
|
use js::jsapi::{JS_NewUCStringCopyN, JS_DefineFunctions, JS_DefineProperty};
|
||||||
use js::jsapi::{JS_ValueToString, JS_GetReservedSlot, JS_SetReservedSlot};
|
use js::jsapi::{JS_ValueToString, JS_GetReservedSlot, JS_SetReservedSlot};
|
||||||
use js::jsapi::{JSContext, JSObject, JSBool, jsid, JSClass, JSNative, JSTracer};
|
use js::jsapi::{JSContext, JSObject, JSBool, jsid, JSClass, JSNative, JSTracer};
|
||||||
use js::jsapi::{JSFunctionSpec, JSPropertySpec, JSVal, JSPropertyDescriptor};
|
use js::jsapi::{JSFunctionSpec, JSPropertySpec, JSVal, JSPropertyDescriptor};
|
||||||
|
@ -205,25 +205,27 @@ pub fn jsval_to_str(cx: *JSContext, v: JSVal) -> Result<~str, ()> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let strbuf = JS_EncodeString(cx, jsstr);
|
let length = 0;
|
||||||
let buf = str::raw::from_c_str(strbuf);
|
let chars = JS_GetStringCharsAndLength(cx, jsstr, &length);
|
||||||
JS_free(cx, strbuf as *libc::c_void);
|
do vec::raw::buf_as_slice(chars, length as uint) |char_vec| {
|
||||||
Ok(buf)
|
Ok(str::from_utf16(char_vec))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[fixed_stack_segment]
|
#[fixed_stack_segment]
|
||||||
pub unsafe fn domstring_to_jsval(cx: *JSContext, string: &DOMString) -> JSVal {
|
pub unsafe fn domstring_to_jsval(cx: *JSContext, string: &DOMString) -> JSVal {
|
||||||
match string {
|
match string {
|
||||||
&None => {
|
&None => JSVAL_NULL,
|
||||||
JSVAL_NULL
|
&Some(ref s) => do s.to_utf16().as_imm_buf |buf, len| {
|
||||||
}
|
let jsstr = JS_NewUCStringCopyN(cx, buf, len as libc::size_t);
|
||||||
&Some(ref s) => {
|
if jsstr.is_null() {
|
||||||
do s.as_imm_buf |buf, len| {
|
// FIXME: is there something else we should do on failure?
|
||||||
let cbuf = cast::transmute(buf);
|
JSVAL_NULL
|
||||||
RUST_STRING_TO_JSVAL(JS_NewStringCopyN(cx, cbuf, len as libc::size_t))
|
} else {
|
||||||
|
RUST_STRING_TO_JSVAL(jsstr)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -19,6 +19,7 @@ use std::cast;
|
||||||
use std::cell::Cell;
|
use std::cell::Cell;
|
||||||
use std::comm;
|
use std::comm;
|
||||||
use std::comm::{Port, SharedChan};
|
use std::comm::{Port, SharedChan};
|
||||||
|
use std::str;
|
||||||
use std::str::eq_slice;
|
use std::str::eq_slice;
|
||||||
use std::task;
|
use std::task;
|
||||||
use std::from_str::FromStr;
|
use std::from_str::FromStr;
|
||||||
|
@ -102,7 +103,7 @@ macro_rules! handle_element_base(
|
||||||
|
|
||||||
|
|
||||||
pub struct JSFile {
|
pub struct JSFile {
|
||||||
data: ~[u8],
|
data: ~str,
|
||||||
url: Url
|
url: Url
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -222,11 +223,11 @@ fn js_script_listener(to_parent: SharedChan<HtmlDiscoveryMessage>,
|
||||||
|
|
||||||
let bytes = result_port.recv();
|
let bytes = result_port.recv();
|
||||||
if bytes.is_some() {
|
if bytes.is_some() {
|
||||||
result_vec.push(JSFile { data: bytes.unwrap(), url: url_clone });
|
result_vec.push(JSFile { data: str::from_utf8(bytes.unwrap()), url: url_clone });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
JSTaskNewInlineScript(data, url) => {
|
JSTaskNewInlineScript(data, url) => {
|
||||||
result_vec.push(JSFile { data: data.into_bytes(), url: url });
|
result_vec.push(JSFile { data: data, url: url });
|
||||||
}
|
}
|
||||||
JSTaskExit => {
|
JSTaskExit => {
|
||||||
break;
|
break;
|
||||||
|
|
|
@ -30,6 +30,7 @@ use std::comm;
|
||||||
use std::comm::{Port, SharedChan};
|
use std::comm::{Port, SharedChan};
|
||||||
use std::io::read_whole_file;
|
use std::io::read_whole_file;
|
||||||
use std::ptr;
|
use std::ptr;
|
||||||
|
use std::str;
|
||||||
use std::task::{spawn_sched, SingleThreaded};
|
use std::task::{spawn_sched, SingleThreaded};
|
||||||
use std::util::replace;
|
use std::util::replace;
|
||||||
use dom::window::TimerData;
|
use dom::window::TimerData;
|
||||||
|
@ -578,7 +579,7 @@ impl ScriptTask {
|
||||||
Ok(bytes) => {
|
Ok(bytes) => {
|
||||||
compartment.define_functions(debug_fns);
|
compartment.define_functions(debug_fns);
|
||||||
cx.evaluate_script(compartment.global_obj,
|
cx.evaluate_script(compartment.global_obj,
|
||||||
bytes,
|
str::from_utf8(bytes),
|
||||||
url.path.clone(),
|
url.path.clone(),
|
||||||
1);
|
1);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1 +1 @@
|
||||||
Subproject commit 1a9a5c3c1462bd3b3e7905ee2834a5ba906f0214
|
Subproject commit dc50fb7958312e8ee5b4a03db516bcafd6df3d51
|
|
@ -24,8 +24,10 @@ function check_collection(obj, num, classes, name) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function check_tag(tagname, num, classes) {
|
function check_tag(tagname, num, classes, tagname_upper) {
|
||||||
check_collection(document.getElementsByTagName(tagname), num, classes, tagname.toUpperCase());
|
if (tagname_upper === undefined)
|
||||||
|
tagname_upper = tagname.toUpperCase();
|
||||||
|
check_collection(document.getElementsByTagName(tagname), num, classes, tagname_upper);
|
||||||
}
|
}
|
||||||
|
|
||||||
check_collection(document.links, 1, [HTMLAnchorElement], "A");
|
check_collection(document.links, 1, [HTMLAnchorElement], "A");
|
||||||
|
@ -73,8 +75,9 @@ check_tag("track", 1, [HTMLTrackElement]);
|
||||||
check_tag("audio", 1, [HTMLMediaElement, HTMLAudioElement]);
|
check_tag("audio", 1, [HTMLMediaElement, HTMLAudioElement]);
|
||||||
check_tag("video", 1, [HTMLMediaElement, HTMLVideoElement]);
|
check_tag("video", 1, [HTMLMediaElement, HTMLVideoElement]);
|
||||||
|
|
||||||
// FIXME: Test non-ASCII tag names
|
// Test non-ASCII tag names. Uppercasing is ASCII-only per spec:
|
||||||
check_tag("foo", 1, [HTMLUnknownElement]);
|
// http://dom.spec.whatwg.org/#dom-element-tagname
|
||||||
|
check_tag("foo-á", 1, [HTMLUnknownElement], "FOO-á");
|
||||||
|
|
||||||
finish();
|
finish();
|
||||||
</script>
|
</script>
|
||||||
|
@ -134,7 +137,7 @@ finish();
|
||||||
<track></track>
|
<track></track>
|
||||||
</video>
|
</video>
|
||||||
|
|
||||||
<foo>hi</foo>
|
<foo-á>hi</foo-á>
|
||||||
|
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue