mirror of
https://github.com/servo/servo.git
synced 2025-08-06 14:10:11 +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 content;
|
||||
|
||||
import result::extensions;
|
||||
import dom::rcu::writer_methods;
|
||||
import dom=dom::base;
|
||||
import layout::layout;
|
||||
|
@ -56,8 +57,17 @@ fn content(to_layout: chan<layout::msg>) -> chan<msg> {
|
|||
execute(filename) {
|
||||
#debug["content: Received filename `%s` to execute", filename];
|
||||
|
||||
let cx = rt.cx();
|
||||
let _glob = cx.new_global(jsglobal::global_class());
|
||||
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();
|
||||
cx.new_global(jsglobal::global_class()).chain { |glob|
|
||||
cx.evaluate_script(glob, bytes, filename, 1u)
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
exit {
|
||||
to_layout.send(layout::exit);
|
||||
|
|
|
@ -2,6 +2,7 @@ import jsapi::*;
|
|||
import jsapi::bindgen::*;
|
||||
import ptr::{null, addr_of};
|
||||
import result::{result, ok, err, extensions};
|
||||
import libc::c_char;
|
||||
|
||||
export rt;
|
||||
export methods;
|
||||
|
@ -37,7 +38,8 @@ fn rt() -> rt {
|
|||
|
||||
impl methods for rt {
|
||||
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
|
||||
|
||||
type cx = @cx_rsrc;
|
||||
resource cx_rsrc(self: {ptr: *JSContext}) {
|
||||
resource cx_rsrc(self: {ptr: *JSContext, rt: rt}) {
|
||||
JS_DestroyContext(self.ptr);
|
||||
}
|
||||
|
||||
impl methods for cx {
|
||||
fn rooted_obj(obj: *JSObject) -> jsobj {
|
||||
let jsobj = @jsobj_rsrc({cx: self.ptr, obj: obj});
|
||||
JS_AddObjectRoot(self.ptr, ptr::addr_of(jsobj.obj));
|
||||
let jsobj = @jsobj_rsrc({cx: self, cxptr: self.ptr, ptr: obj});
|
||||
JS_AddObjectRoot(self.ptr, ptr::addr_of(jsobj.ptr));
|
||||
jsobj
|
||||
}
|
||||
|
||||
|
@ -66,6 +68,29 @@ impl methods for cx {
|
|||
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;
|
||||
|
||||
resource jsobj_rsrc(self: {cx: *JSContext, obj: *JSObject}) {
|
||||
JS_RemoveObjectRoot(self.cx, ptr::addr_of(self.obj));
|
||||
resource jsobj_rsrc(self: {cx: cx, cxptr: *JSContext, ptr: *JSObject}) {
|
||||
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