Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge the context and runtime structs into Runtime. #149

Merged
merged 4 commits into from May 10, 2015
Merged
Changes from 1 commit
Commits
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.

Always

Just for now

Move evaluate_script to Runtime.

  • Loading branch information
Ms2ger committed May 10, 2015
commit 774a764770b196ed64e4973744f9d2ff247412b1
@@ -89,6 +89,40 @@ impl Runtime {
pub fn cx(&self) -> *mut JSContext {
self.cx.ptr
}

pub fn evaluate_script(&self, global: *mut JSObject, script: String,
filename: String, line_num: usize)
-> Result<(), ()> {
let script_utf16: Vec<u16> = script.utf16_units().collect();
let filename_cstr = ffi::CString::new(filename.as_bytes()).unwrap();
debug!("Evaluating script from {} with content {}", filename, script);

// SpiderMonkey does not approve of null pointers.
let (ptr, len) = if script_utf16.len() == 0 {
static empty: &'static [u16] = &[];
(empty.as_ptr(), 0)
} else {
(script_utf16.as_ptr(), script_utf16.len() as c_uint)
};
assert!(!ptr.is_null());

let mut rval: JSVal = NullValue();
let result = unsafe {
JS_EvaluateUCScript(self.cx(), global, ptr, len,
filename_cstr.as_ptr(), line_num as c_uint,
&mut rval)
};

if result == 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(())
}
}
}

pub type rt = rc::Rc<rt_rsrc>;
@@ -121,37 +155,6 @@ impl Drop for Cx {
}
}

impl Cx {
pub fn evaluate_script(&self, glob: *mut JSObject, script: String, filename: String, line_num: usize)
-> Result<(),()> {
let script_utf16: Vec<u16> = script.utf16_units().collect();
let filename_cstr = ffi::CString::new(filename.as_bytes()).unwrap();
let mut rval: JSVal = NullValue();
debug!("Evaluating script from {} with content {}", filename, script);
// SpiderMonkey does not approve of null pointers.
let (ptr, len) = if script_utf16.len() == 0 {
static empty: &'static [u16] = &[];
(empty.as_ptr(), 0)
} else {
(script_utf16.as_ptr(), script_utf16.len() as c_uint)
};
assert!(!ptr.is_null());
unsafe {
if ERR == JS_EvaluateUCScript(self.ptr, glob, ptr, len,
filename_cstr.as_ptr(), line_num as c_uint,
&mut rval) {
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(())
}
}
}
}

pub unsafe extern fn reportError(_cx: *mut JSContext, msg: *const c_char, report: *mut JSErrorReport) {
let fnptr = (*report).filename;
let fname = if !fnptr.is_null() {
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.