make it possible for content to (successfully) invoke JS

This commit is contained in:
Niko Matsakis 2012-05-24 16:46:16 -07:00
parent f12af3c539
commit b7315ad9d0
4 changed files with 23 additions and 15 deletions

View file

@ -63,7 +63,10 @@ fn content(to_layout: chan<layout::msg>) -> chan<msg> {
} }
result::ok(bytes) { result::ok(bytes) {
let cx = rt.cx(); let cx = rt.cx();
cx.set_default_options_and_version();
cx.set_logging_error_reporter();
cx.new_compartment(jsglobal::global_class).chain { |comp| cx.new_compartment(jsglobal::global_class).chain { |comp|
comp.define_functions(jsglobal::global_fns);
cx.evaluate_script(comp.global_obj, bytes, filename, 1u) cx.evaluate_script(comp.global_obj, bytes, filename, 1u)
}; };
} }

View file

@ -74,6 +74,10 @@ impl methods for cx {
JS_SetVersion(self.ptr, v); JS_SetVersion(self.ptr, v);
} }
fn set_logging_error_reporter() {
JS_SetErrorReporter(self.ptr, reportError);
}
fn set_error_reporter(reportfn: *u8) { fn set_error_reporter(reportfn: *u8) {
JS_SetErrorReporter(self.ptr, reportfn); JS_SetErrorReporter(self.ptr, reportfn);
} }
@ -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 // compartment
@ -152,24 +168,12 @@ resource jsobj_rsrc(self: {cx: cx, cxptr: *JSContext, ptr: *JSObject}) {
#[cfg(test)] #[cfg(test)]
mod 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] #[test]
fn dummy() { fn dummy() {
let rt = rt(); let rt = rt();
let cx = rt.cx(); let cx = rt.cx();
cx.set_default_options_and_version(); cx.set_default_options_and_version();
cx.set_error_reporter(reportError); cx.set_logging_error_reporter();
cx.new_compartment(jsglobal::global_class).chain { |comp| cx.new_compartment(jsglobal::global_class).chain { |comp|
comp.define_functions(jsglobal::global_fns); comp.define_functions(jsglobal::global_fns);

View file

@ -63,7 +63,7 @@ fn global_class(np: name_pool) -> JSClass {
null(), null(), null(), null(), null())} // 40 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; import io::writer_util;
#debug["debug() called with %? arguments", argc]; #debug["debug() called with %? arguments", argc];
@ -78,6 +78,7 @@ crust fn debug(cx: *JSContext, argc: uintN, vp: *jsval) {
#debug["%s", str]; #debug["%s", str];
} }
JS_SET_RVAL(cx, vp, JSVAL_NULL); JS_SET_RVAL(cx, vp, JSVAL_NULL);
ret 1_i32;
} }
} }

View file

@ -1 +1 @@
print("Hello, world!"); debug("Hello, world!");