mirror of
https://github.com/servo/servo.git
synced 2025-08-07 14:35:33 +01:00
wip: integrate js
This commit is contained in:
parent
6d27ee5e0e
commit
7ca571148b
3 changed files with 62 additions and 8 deletions
|
@ -1,6 +1,7 @@
|
||||||
export msg, ping;
|
export msg, ping;
|
||||||
export content;
|
export content;
|
||||||
|
|
||||||
|
import result::extensions;
|
||||||
import dom::rcu::writer_methods;
|
import dom::rcu::writer_methods;
|
||||||
import dom=dom::base;
|
import dom=dom::base;
|
||||||
import layout::layout;
|
import layout::layout;
|
||||||
|
@ -56,8 +57,17 @@ fn content(to_layout: chan<layout::msg>) -> chan<msg> {
|
||||||
execute(filename) {
|
execute(filename) {
|
||||||
#debug["content: Received filename `%s` to execute", filename];
|
#debug["content: Received filename `%s` to execute", filename];
|
||||||
|
|
||||||
|
alt io::read_whole_file(filename) {
|
||||||
|
result::err(msg) {
|
||||||
|
io::println(#fmt["Error opening %s: %s", filename, msg]);
|
||||||
|
}
|
||||||
|
result::ok(bytes) {
|
||||||
let cx = rt.cx();
|
let cx = rt.cx();
|
||||||
let _glob = cx.new_global(jsglobal::global_class());
|
cx.new_global(jsglobal::global_class()).chain { |glob|
|
||||||
|
cx.evaluate_script(glob, bytes, filename, 1u)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
exit {
|
exit {
|
||||||
to_layout.send(layout::exit);
|
to_layout.send(layout::exit);
|
||||||
|
|
|
@ -2,6 +2,7 @@ import jsapi::*;
|
||||||
import jsapi::bindgen::*;
|
import jsapi::bindgen::*;
|
||||||
import ptr::{null, addr_of};
|
import ptr::{null, addr_of};
|
||||||
import result::{result, ok, err, extensions};
|
import result::{result, ok, err, extensions};
|
||||||
|
import libc::c_char;
|
||||||
|
|
||||||
export rt;
|
export rt;
|
||||||
export methods;
|
export methods;
|
||||||
|
@ -37,7 +38,8 @@ fn rt() -> rt {
|
||||||
|
|
||||||
impl methods for rt {
|
impl methods for rt {
|
||||||
fn cx() -> cx {
|
fn cx() -> cx {
|
||||||
@cx_rsrc({ptr: JS_NewContext(self.ptr, default_stacksize)})
|
@cx_rsrc({ptr: JS_NewContext(self.ptr, default_stacksize),
|
||||||
|
rt: self})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -45,14 +47,14 @@ impl methods for rt {
|
||||||
// contexts
|
// contexts
|
||||||
|
|
||||||
type cx = @cx_rsrc;
|
type cx = @cx_rsrc;
|
||||||
resource cx_rsrc(self: {ptr: *JSContext}) {
|
resource cx_rsrc(self: {ptr: *JSContext, rt: rt}) {
|
||||||
JS_DestroyContext(self.ptr);
|
JS_DestroyContext(self.ptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
impl methods for cx {
|
impl methods for cx {
|
||||||
fn rooted_obj(obj: *JSObject) -> jsobj {
|
fn rooted_obj(obj: *JSObject) -> jsobj {
|
||||||
let jsobj = @jsobj_rsrc({cx: self.ptr, obj: obj});
|
let jsobj = @jsobj_rsrc({cx: self, cxptr: self.ptr, ptr: obj});
|
||||||
JS_AddObjectRoot(self.ptr, ptr::addr_of(jsobj.obj));
|
JS_AddObjectRoot(self.ptr, ptr::addr_of(jsobj.ptr));
|
||||||
jsobj
|
jsobj
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -66,6 +68,29 @@ impl methods for cx {
|
||||||
ok(self.rooted_obj(globobj))
|
ok(self.rooted_obj(globobj))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn evaluate_script(glob: jsobj, bytes: [u8], filename: str,
|
||||||
|
line_num: uint) -> result<(),()> {
|
||||||
|
vec::as_buf(bytes) { |bytes_ptr|
|
||||||
|
str::as_c_str(filename) { |filename_cstr|
|
||||||
|
let bytes_ptr = bytes_ptr as *c_char;
|
||||||
|
let v: jsval = 0_u64;
|
||||||
|
#debug["Evaluating script from %s", filename];
|
||||||
|
if JS_EvaluateScript(self.ptr, glob.ptr,
|
||||||
|
bytes_ptr, bytes.len() as uintN,
|
||||||
|
filename_cstr, line_num as uintN,
|
||||||
|
ptr::addr_of(v)) == ERR {
|
||||||
|
#debug["...err!"];
|
||||||
|
err(())
|
||||||
|
} else {
|
||||||
|
// we could return the script result but then we'd have
|
||||||
|
// to root it and so forth and, really, who cares?
|
||||||
|
#debug["...ok!"];
|
||||||
|
ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ___________________________________________________________________________
|
// ___________________________________________________________________________
|
||||||
|
@ -73,7 +98,25 @@ impl methods for cx {
|
||||||
|
|
||||||
type jsobj = @jsobj_rsrc;
|
type jsobj = @jsobj_rsrc;
|
||||||
|
|
||||||
resource jsobj_rsrc(self: {cx: *JSContext, obj: *JSObject}) {
|
resource jsobj_rsrc(self: {cx: cx, cxptr: *JSContext, ptr: *JSObject}) {
|
||||||
JS_RemoveObjectRoot(self.cx, ptr::addr_of(self.obj));
|
JS_RemoveObjectRoot(self.cxptr, ptr::addr_of(self.ptr));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod test {
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn dummy() {
|
||||||
|
let rt = rt();
|
||||||
|
let cx = rt.cx();
|
||||||
|
let gc = jsglobal::global_class();
|
||||||
|
cx.new_global(gc).chain {
|
||||||
|
|glob|
|
||||||
|
str::bytes("x = 1;") {
|
||||||
|
|bytes|
|
||||||
|
cx.evaluate_script(glob, bytes, "test", 1u);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
1
test.js
Normal file
1
test.js
Normal file
|
@ -0,0 +1 @@
|
||||||
|
print("Hello, world!");
|
Loading…
Add table
Add a link
Reference in a new issue