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

Various cleanup. #145

Merged
merged 3 commits into from May 9, 2015
Merged
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.

Always

Just for now

@@ -64,6 +64,7 @@ mod ValueTag {
}

#[cfg(target_pointer_width = "64")]
#[allow(dead_code)]
mod ValueShiftedTag {
use super::{JSVAL_TAG_MAX_DOUBLE, JSVAL_TAG_SHIFT, ValueTag};

@@ -5,9 +5,9 @@
#![crate_name = "js"]
#![crate_type = "rlib"]

#![feature(link_args, unsafe_destructor,collections)]
#![feature(link_args, collections)]

#![allow(non_upper_case_globals, non_camel_case_types, non_snake_case, improper_ctypes)]
#![allow(non_upper_case_globals, non_camel_case_types, non_snake_case, improper_ctypes, raw_pointer_derive)]

extern crate libc;
#[macro_use]
@@ -34,10 +34,11 @@ pub struct Runtime {
impl Runtime {
/// Creates a new `JSRuntime` and `JSContext`.
pub fn new() -> Runtime {
let js_runtime = rt();
assert!({
let ptr: *mut JSRuntime = (*js_runtime).ptr;
!ptr.is_null()
let js_runtime = unsafe { JS_Init(default_heapsize) };
assert!(!js_runtime.is_null());

let js_runtime = rc::Rc::new(rt_rsrc {
ptr: js_runtime
});

// Unconstrain the runtime's threshold on nominal heap size, to avoid
@@ -49,10 +50,14 @@ impl Runtime {
JS_SetGCParameter(js_runtime.ptr, JSGC_MAX_BYTES, u32::MAX);
}

let js_context = js_runtime.cx();
assert!({
let ptr: *mut JSContext = (*js_context).ptr;
!ptr.is_null()
let js_context = unsafe {
JS_NewContext(js_runtime.ptr, default_stacksize as size_t)
};
assert!(!js_context.is_null());

let js_context = rc::Rc::new(Cx {
ptr: js_context,
rt: js_runtime.clone(),
});
js_context.set_default_options_and_version();
js_context.set_logging_error_reporter();
@@ -91,32 +96,6 @@ impl Drop for rt_rsrc {
}
}

pub fn new_runtime(p: *mut JSRuntime) -> rt {
return rc::Rc::new(rt_rsrc {
ptr: p
})
}

pub trait RtUtils {
fn cx(&self) -> rc::Rc<Cx>;
}

impl RtUtils for rc::Rc<rt_rsrc> {
fn cx(&self) -> rc::Rc<Cx> {
unsafe {
new_context(JS_NewContext(self.ptr,
default_stacksize as size_t), self.clone())
}
}
}

pub fn rt() -> rt {
unsafe {
let runtime = JS_Init(default_heapsize);
return new_runtime(runtime);
}
}

// ___________________________________________________________________________
// contexts

@@ -133,13 +112,6 @@ impl Drop for Cx {
}
}

pub fn new_context(ptr: *mut JSContext, rt: rt) -> rc::Rc<Cx> {
return rc::Rc::new(Cx {
ptr: ptr,
rt: rt,
})
}

impl Cx {
pub fn set_default_options_and_version(&self) {
self.set_options(JSOPTION_VAROBJFIX | JSOPTION_METHODJIT |
@@ -228,15 +200,13 @@ pub fn with_compartment<R, F: FnMut() -> R>(cx: *mut JSContext, object: *mut JSO

#[cfg(test)]
pub mod test {
use super::rt;
use super::RtUtils;
use super::Runtime;

#[test]
pub fn dummy() {
let rt = rt();
let cx = rt.cx();
cx.deref().set_default_options_and_version();
cx.deref().set_logging_error_reporter();
let rt = Runtime::new();
rt.cx.set_default_options_and_version();
rt.cx.set_logging_error_reporter();
}

}
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.