From 088590c5eed157cd538f9042ea23bb5353f8a15c Mon Sep 17 00:00:00 2001 From: Lars Bergstrom Date: Tue, 4 Mar 2014 15:57:17 -0800 Subject: [PATCH 1/3] Rust upgrade --- global.rs | 4 ++-- js.rc | 20 +++++++++++--------- rust.rs | 17 ++++++++++------- trace.rs | 2 +- 4 files changed, 24 insertions(+), 19 deletions(-) diff --git a/global.rs b/global.rs index b78f54413..5e16c5fbf 100644 --- a/global.rs +++ b/global.rs @@ -105,7 +105,7 @@ pub extern fn debug(cx: *JSContext, argc: c_uint, vp: *mut JSVal) -> JSBool { unsafe { let argv = JS_ARGV(cx, &*vp); for i in range(0, argc as int) { - let jsstr = JS_ValueToString(cx, *ptr::offset(argv, i)); + let jsstr = JS_ValueToString(cx, *argv.offset(i)); debug!("{:s}", jsval_to_rust_str(cx, jsstr)); } JS_SET_RVAL(cx, &*vp, UndefinedValue()); @@ -128,7 +128,7 @@ pub extern fn assert(cx: *JSContext, argc: c_uint, vp: *mut JSVal) -> JSBool { let argument = match argc { 0 => UndefinedValue(), - _ => *ptr::offset(argv, 0) + _ => *argv.offset(0) }; let result = 0; diff --git a/js.rc b/js.rc index ff1095590..ea88aa98e 100644 --- a/js.rc +++ b/js.rc @@ -9,12 +9,12 @@ #[allow(non_uppercase_statics)]; -extern mod extra; -extern mod green; +extern crate extra; +extern crate green; +extern crate serialize; use std::libc; use std::cast; -use std::ptr; use std::ptr::null; use std::result::{Result, Ok, Err}; use std::libc::c_uint; @@ -115,7 +115,7 @@ pub fn result_obj(o: jsobj) -> Result { #[inline(always)] pub unsafe fn JS_ARGV(_cx: *JSContext, vp: *JSVal) -> *JSVal { - ptr::offset(vp, 2) + vp.offset(2) } pub unsafe fn JS_SET_RVAL(_cx: *JSContext, vp: *JSVal, v: JSVal) { @@ -126,10 +126,10 @@ pub unsafe fn JS_SET_RVAL(_cx: *JSContext, vp: *JSVal, v: JSVal) { #[inline(alwyas)] pub unsafe fn JS_THIS_OBJECT(cx: *JSContext, vp: *mut JSVal) -> *JSObject { let r = - if (*ptr::offset(&*vp, 1)).is_primitive() { + if (*(vp.offset(1))).is_primitive() { JS_ComputeThis(cx, &*vp) } else { - *ptr::offset(&*vp, 1) + *(vp.offset(1)) }; r.to_object_or_null() } @@ -142,6 +142,7 @@ pub unsafe fn JS_CALLEE(_cx: *JSContext, vp: *JSVal) -> JSVal { // This is a duplication of the shadow stuff from jsfriendapi.h. Here // there be dragons! mod shadow { + use std::cast; use std::libc; use std::ptr; use jsapi::{JSObject, JSClass, jsid}; @@ -178,7 +179,8 @@ mod shadow { #[inline(always)] pub unsafe fn fixedSlots(&self) -> *JSVal { - (ptr::offset(ptr::to_unsafe_ptr(&self), 1)) as *JSVal + let x: *JSVal = cast::transmute(&self); + (x.offset(1)) as *JSVal } // Like slotRef, but just returns the value, not a reference @@ -187,9 +189,9 @@ mod shadow { unsafe { let nfixed : libc::size_t = self.numFixedSlots(); if slot < nfixed { - return *ptr::offset(self.fixedSlots(), slot as int) + return *self.fixedSlots().offset(slot as int) } - return *ptr::offset(self.slots, (slot - nfixed) as int) + return *self.slots.offset((slot - nfixed) as int) } } } diff --git a/rust.rs b/rust.rs index 29175b3bb..8e7241b54 100644 --- a/rust.rs +++ b/rust.rs @@ -6,7 +6,7 @@ use std::libc::types::os::arch::c95::{size_t, c_uint}; use std::libc::{c_char, uintptr_t}; -use std::num; +use std::cmp; use std::rc; use jsapi::*; use jsval::{JSVal, NullValue}; @@ -67,10 +67,12 @@ extern fn gc_callback(rt: *JSRuntime, _status: JSGCStatus) { unsafe { let mut task = Local::borrow(None::); let green_task: ~GreenTask = task.get().maybe_take_runtime().unwrap(); - let c = green_task.coroutine.get_ref(); - let start = c.current_stack_segment.start() as uintptr_t; - let end = c.current_stack_segment.end() as uintptr_t; - JS_SetNativeStackBounds(rt, num::min(start, end), num::max(start, end)); + { + let c = green_task.coroutine.get_ref(); + let start = c.current_stack_segment.start() as uintptr_t; + let end = c.current_stack_segment.end() as uintptr_t; + JS_SetNativeStackBounds(rt, cmp::min(start, end), cmp::max(start, end)); + } task.get().put_runtime(green_task); } } @@ -185,7 +187,7 @@ impl Cx { if ERR == JS_EvaluateUCScript(self.ptr, glob.borrow().ptr, script_utf16.as_ptr(), script_utf16.len() as c_uint, filename_cstr, line_num as c_uint, - ptr::to_unsafe_ptr(&rval)) { + &rval) { debug!("...err!"); Err(()) } else { @@ -290,7 +292,7 @@ pub struct jsobj_rsrc { impl Drop for jsobj_rsrc { fn drop(&mut self) { unsafe { - JS_RemoveObjectRoot(self.cxptr, ptr::to_unsafe_ptr(&self.ptr)); + JS_RemoveObjectRoot(self.cxptr, &self.ptr); } } } @@ -324,6 +326,7 @@ impl to_jsstr for ~str { #[cfg(test)] pub mod test { use super::rt; + use super::{CxUtils, RtUtils}; use super::super::global; use super::super::jsapi::{JS_GC, JS_GetRuntime}; diff --git a/trace.rs b/trace.rs index e2ecd72db..3706cbfe4 100644 --- a/trace.rs +++ b/trace.rs @@ -4,7 +4,7 @@ use jsapi::JSTracer; -use extra::serialize::Encoder; +use serialize::Encoder; impl Encoder for JSTracer { fn emit_nil(&mut self) {} From a884e044c38127c1954d73781b199bbe90389051 Mon Sep 17 00:00:00 2001 From: Josh Matthews Date: Fri, 14 Mar 2014 12:54:03 -0400 Subject: [PATCH 2/3] Avoid scanning stack guard page, and remove unnecessary global object rooting. --- rust.rs | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/rust.rs b/rust.rs index 8e7241b54..43e69cc5f 100644 --- a/rust.rs +++ b/rust.rs @@ -8,6 +8,7 @@ use std::libc::types::os::arch::c95::{size_t, c_uint}; use std::libc::{c_char, uintptr_t}; use std::cmp; use std::rc; +use std::rt::Runtime; use jsapi::*; use jsval::{JSVal, NullValue}; use default_stacksize; @@ -67,12 +68,11 @@ extern fn gc_callback(rt: *JSRuntime, _status: JSGCStatus) { unsafe { let mut task = Local::borrow(None::); let green_task: ~GreenTask = task.get().maybe_take_runtime().unwrap(); - { - let c = green_task.coroutine.get_ref(); - let start = c.current_stack_segment.start() as uintptr_t; - let end = c.current_stack_segment.end() as uintptr_t; - JS_SetNativeStackBounds(rt, cmp::min(start, end), cmp::max(start, end)); - } + let (start, end) = green_task.stack_bounds(); + // libgreen adds a guard page to the stack which causes SpiderMonkey to + // to choke, so we need to skip over that. + let start = start + ::std::os::page_size(); + JS_SetNativeStackBounds(rt, cmp::min(start, end), cmp::max(start, end)); task.get().put_runtime(green_task); } } @@ -132,7 +132,7 @@ impl CxUtils for rc::Rc { result(JS_InitStandardClasses(ptr, globobj)).and_then(|_ok| { Ok(rc::Rc::new(Compartment { cx: self.clone(), - global_obj: self.rooted_obj(globobj), + global_obj: globobj, })) }) } @@ -141,7 +141,7 @@ impl CxUtils for rc::Rc { fn new_compartment_with_global(&self, global: *JSObject) -> Result,()> { Ok(rc::Rc::new(Compartment { cx: self.clone(), - global_obj: self.rooted_obj(global), + global_obj: global, })) } } @@ -177,14 +177,14 @@ impl Cx { } } - pub fn evaluate_script(&self, glob: jsobj, script: ~str, filename: ~str, line_num: uint) + pub fn evaluate_script(&self, glob: *JSObject, script: ~str, filename: ~str, line_num: uint) -> Result<(),()> { let script_utf16 = script.to_utf16(); filename.to_c_str().with_ref(|filename_cstr| { let rval: JSVal = NullValue(); debug!("Evaluating script from {:s} with content {}", filename, script); unsafe { - if ERR == JS_EvaluateUCScript(self.ptr, glob.borrow().ptr, + if ERR == JS_EvaluateUCScript(self.ptr, glob, script_utf16.as_ptr(), script_utf16.len() as c_uint, filename_cstr, line_num as c_uint, &rval) { @@ -232,21 +232,21 @@ pub extern fn reportError(_cx: *JSContext, msg: *c_char, report: *JSErrorReport) pub struct Compartment { cx: rc::Rc, - global_obj: jsobj, + global_obj: *JSObject, } impl Compartment { pub fn define_functions(&self, specvec: &'static [JSFunctionSpec]) -> Result<(),()> { unsafe { result(JS_DefineFunctions(self.cx.borrow().ptr, - self.global_obj.borrow().ptr, + self.global_obj, specvec.as_ptr())) } } pub fn define_properties(&self, specvec: &'static [JSPropertySpec]) -> Result<(),()> { unsafe { result(JS_DefineProperties(self.cx.borrow().ptr, - self.global_obj.borrow().ptr, + self.global_obj, specvec.as_ptr())) } } @@ -259,7 +259,7 @@ impl Compartment { unsafe { name.to_c_str().with_ref(|name| { result(JS_DefineProperty(self.cx.borrow().ptr, - self.global_obj.borrow().ptr, + self.global_obj, name, value, Some(getter), @@ -342,7 +342,7 @@ pub mod test { comp.borrow().define_functions(global::DEBUG_FNS); let s = ~"debug(22);"; - cx.borrow().evaluate_script(comp.borrow().global_obj.clone(), s, ~"test", 1u) + cx.borrow().evaluate_script(comp.borrow().global_obj, s, ~"test", 1u) }); } From c94949f8c567e17c1594931ec62c7857ed24b047 Mon Sep 17 00:00:00 2001 From: Josh Matthews Date: Fri, 14 Mar 2014 15:06:47 -0400 Subject: [PATCH 3/3] Warning police. --- global.rs | 1 - js.rc | 3 +-- jsapi.rs | 2 +- rust.rs | 3 +-- 4 files changed, 3 insertions(+), 6 deletions(-) diff --git a/global.rs b/global.rs index 5e16c5fbf..b41a5f4ec 100644 --- a/global.rs +++ b/global.rs @@ -16,7 +16,6 @@ use std::libc::{c_uint, c_void}; use std::str::raw::from_c_str; use std::cast::transmute; use std::ptr::null; -use std::ptr; use jsapi; use jsapi::{JSClass, JSContext, JSFunctionSpec, JSBool, JSNativeWrapper}; use jsapi::{JS_EncodeString, JS_free, JS_ValueToBoolean, JS_ValueToString}; diff --git a/js.rc b/js.rc index ea88aa98e..9e71b117f 100644 --- a/js.rc +++ b/js.rc @@ -7,7 +7,7 @@ #[feature(globs, managed_boxes)]; -#[allow(non_uppercase_statics)]; +#[allow(non_uppercase_statics, non_camel_case_types)]; extern crate extra; extern crate green; @@ -144,7 +144,6 @@ pub unsafe fn JS_CALLEE(_cx: *JSContext, vp: *JSVal) -> JSVal { mod shadow { use std::cast; use std::libc; - use std::ptr; use jsapi::{JSObject, JSClass, jsid}; use jsval::JSVal; diff --git a/jsapi.rs b/jsapi.rs index 3643ab4f3..c2e88c5e1 100644 --- a/jsapi.rs +++ b/jsapi.rs @@ -4,7 +4,7 @@ /* automatically generated by rust-bindgen */ -#[allow(non_uppercase_statics)]; +#[allow(non_uppercase_statics, non_camel_case_types)]; use std::libc::*; use jsfriendapi::JSJitInfo; diff --git a/rust.rs b/rust.rs index 43e69cc5f..80a3423ee 100644 --- a/rust.rs +++ b/rust.rs @@ -5,7 +5,7 @@ #[doc = "Rust wrappers around the raw JS apis"]; use std::libc::types::os::arch::c95::{size_t, c_uint}; -use std::libc::{c_char, uintptr_t}; +use std::libc::c_char; use std::cmp; use std::rc; use std::rt::Runtime; @@ -17,7 +17,6 @@ use JSOPTION_VAROBJFIX; use JSOPTION_METHODJIT; use JSOPTION_TYPE_INFERENCE; use ERR; -use std::ptr; use std::ptr::null; use result; use result_obj;