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

Create a Runtime struct. #142

Merged
merged 1 commit into from Apr 10, 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

Create a Runtime struct.

  • Loading branch information
Ms2ger committed Apr 10, 2015
commit 402b7b2db8816ffeccacfa9a8d316f4487e96ba0
@@ -12,6 +12,7 @@ use std::ffi;
use std::rc;
use std::str;
use std::string;
use std::u32;
use jsapi::*;
use jsapi::JSVersion::JSVERSION_LATEST;
use jsval::{JSVal, NullValue};
@@ -27,6 +28,58 @@ use ERR;
// ___________________________________________________________________________
// friendly Rustic API to runtimes

/// A wrapper for the `JSRuntime` and `JSContext` structures in SpiderMonkey.
pub struct Runtime {
pub rt: rt,
pub cx: rc::Rc<Cx>,
}

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()
});

// Unconstrain the runtime's threshold on nominal heap size, to avoid
// triggering GC too often if operating continuously near an arbitrary
// finite threshold. This leaves the maximum-JS_malloc-bytes threshold
// still in effect to cause periodical, and we hope hygienic,
// last-ditch GCs from within the GC's allocator.
unsafe {
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()
});
js_context.set_default_options_and_version();
js_context.set_logging_error_reporter();
unsafe {
JS_SetGCZeal((*js_context).ptr, 0, JS_DEFAULT_ZEAL_FREQ);
}

Runtime {
rt: js_runtime,
cx: js_context,
}
}

/// Returns the `JSRuntime` object.
pub fn rt(&self) -> *mut JSRuntime {
self.rt.ptr
}

/// Returns the `JSContext` object.
pub fn cx(&self) -> *mut JSContext {
self.cx.ptr
}
}

pub type rt = rc::Rc<rt_rsrc>;

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