Skip to content

Commit

Permalink
make it possible for content to (successfully) invoke JS
Browse files Browse the repository at this point in the history
  • Loading branch information
nikomatsakis committed May 31, 2012
1 parent f12af3c commit b7315ad
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 15 deletions.
3 changes: 3 additions & 0 deletions src/servo/content.rs
Expand Up @@ -63,7 +63,10 @@ fn content(to_layout: chan<layout::msg>) -> chan<msg> {
}
result::ok(bytes) {
let cx = rt.cx();
cx.set_default_options_and_version();
cx.set_logging_error_reporter();
cx.new_compartment(jsglobal::global_class).chain { |comp|
comp.define_functions(jsglobal::global_fns);
cx.evaluate_script(comp.global_obj, bytes, filename, 1u)
};
}
Expand Down
30 changes: 17 additions & 13 deletions src/servo/content/js.rs
Expand Up @@ -74,6 +74,10 @@ impl methods for cx {
JS_SetVersion(self.ptr, v);
}

fn set_logging_error_reporter() {
JS_SetErrorReporter(self.ptr, reportError);
}

fn set_error_reporter(reportfn: *u8) {
JS_SetErrorReporter(self.ptr, reportfn);
}
Expand Down Expand Up @@ -119,6 +123,18 @@ impl methods for cx {
}
}

crust fn reportError(_cx: *JSContext,
msg: *c_char,
report: *JSErrorReport) {
unsafe {
let fnptr = (*report).filename;
let fname = if fnptr.is_not_null() {from_c_str(fnptr)} else {"none"};
let lineno = (*report).lineno;
let msg = from_c_str(msg);
#error["Error at %s:%?: %s\n", fname, lineno, msg];
}
}

// ___________________________________________________________________________
// compartment

Expand Down Expand Up @@ -152,24 +168,12 @@ resource jsobj_rsrc(self: {cx: cx, cxptr: *JSContext, ptr: *JSObject}) {
#[cfg(test)]
mod test {

crust fn reportError(_cx: *JSContext,
msg: *c_char,
report: *JSErrorReport) {
unsafe {
let fnptr = (*report).filename;
let fname = if fnptr.is_not_null() {from_c_str(fnptr)} else {"none"};
let lineno = (*report).lineno;
let msg = from_c_str(msg);
#error["Error at %s:%?: %s\n", fname, lineno, msg];
}
}

#[test]
fn dummy() {
let rt = rt();
let cx = rt.cx();
cx.set_default_options_and_version();
cx.set_error_reporter(reportError);
cx.set_logging_error_reporter();
cx.new_compartment(jsglobal::global_class).chain { |comp|
comp.define_functions(jsglobal::global_fns);

Expand Down
3 changes: 2 additions & 1 deletion src/servo/content/jsglobal.rs
Expand Up @@ -63,7 +63,7 @@ fn global_class(np: name_pool) -> JSClass {
null(), null(), null(), null(), null())} // 40
}

crust fn debug(cx: *JSContext, argc: uintN, vp: *jsval) {
crust fn debug(cx: *JSContext, argc: uintN, vp: *jsval) -> JSBool {
import io::writer_util;

#debug["debug() called with %? arguments", argc];
Expand All @@ -78,6 +78,7 @@ crust fn debug(cx: *JSContext, argc: uintN, vp: *jsval) {
#debug["%s", str];
}
JS_SET_RVAL(cx, vp, JSVAL_NULL);
ret 1_i32;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/test/test.js
@@ -1 +1 @@
print("Hello, world!");
debug("Hello, world!");

0 comments on commit b7315ad

Please sign in to comment.