Skip to content
This repository was archived by the owner on Nov 12, 2022. It is now read-only.

Commit 75ace6b

Browse files
committed
Update friendly rustic API for the great rt -> cx refactor and rooting stacks move to Zone
1 parent bbf907b commit 75ace6b

File tree

2 files changed

+50
-41
lines changed

2 files changed

+50
-41
lines changed

src/jsglue.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -734,10 +734,10 @@ static size_t MallocSizeOf(const void* aPtr)
734734
}
735735

736736
bool
737-
CollectServoSizes(JSRuntime *rt, JS::ServoSizes *sizes)
737+
CollectServoSizes(JSContext* cx, JS::ServoSizes *sizes)
738738
{
739739
mozilla::PodZero(sizes);
740-
return JS::AddServoSizeOf(rt, MallocSizeOf,
740+
return JS::AddServoSizeOf(cx, MallocSizeOf,
741741
/* ObjectPrivateVisitor = */ nullptr, sizes);
742742
}
743743

src/rust.rs

Lines changed: 48 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ use std::cell::UnsafeCell;
1818
use std::marker::PhantomData;
1919
use consts::{JSCLASS_RESERVED_SLOTS_MASK, JSCLASS_GLOBAL_SLOT_COUNT, JSCLASS_IS_GLOBAL};
2020
use jsapi;
21-
use jsapi::{JS_Init, JS_GetContext, JS_NewRuntime, JS_DestroyRuntime};
22-
use jsapi::{JSContext, JSRuntime, JSObject, JSFlatString, JSFunction, JSString, Symbol, JSScript, jsid, Value};
23-
use jsapi::{RuntimeOptionsRef, ReadOnlyCompileOptions};
21+
use jsapi::{InitWithFailureDiagnostic, JS_NewContext, JS_DestroyContext};
22+
use jsapi::{JSContext, JSObject, JSFlatString, JSFunction, JSString, Symbol, JSScript, jsid, Value};
23+
use jsapi::{ContextOptionsRef, ReadOnlyCompileOptions};
2424
use jsapi::{SetWarningReporter, Evaluate2, JSErrorReport};
2525
use jsapi::{JS_SetGCParameter, JSGCParamKey};
2626
use jsapi::{Heap, HeapObjectPostBarrier, HeapValuePostBarrier};
@@ -103,79 +103,76 @@ impl ToResult for bool {
103103
// ___________________________________________________________________________
104104
// friendly Rustic API to runtimes
105105

106-
/// A wrapper for the `JSRuntime` and `JSContext` structures in SpiderMonkey.
106+
/// A wrapper for the `JSContext` structure in SpiderMonkey.
107107
pub struct Runtime {
108-
rt: *mut JSRuntime,
109108
cx: *mut JSContext,
110109
}
111110

112111
impl Runtime {
113-
/// Creates a new `JSRuntime` and `JSContext`.
112+
/// Creates a new `JSContext`.
114113
pub fn new() -> Runtime {
115114
unsafe {
116-
struct TopRuntime(*mut JSRuntime);
117-
unsafe impl Sync for TopRuntime {}
115+
struct TopContext(*mut JSContext);
116+
unsafe impl Sync for TopContext {}
118117

119118
lazy_static! {
120-
static ref PARENT: TopRuntime = {
119+
static ref PARENT: TopContext = {
121120
unsafe {
122-
assert!(JS_Init());
123-
let runtime = JS_NewRuntime(
121+
// Yes, this is asserting that this call returns *null*
122+
// (not non-null) on purpose. Unlike the rest of JSAPI,
123+
// this function returns non-null on failure and null on
124+
// success.
125+
assert!(InitWithFailureDiagnostic(if cfg!(feature = "debugmozjs") {
126+
true
127+
} else {
128+
false
129+
}).is_null());
130+
131+
let context = JS_NewContext(
124132
default_heapsize, ChunkSize as u32, ptr::null_mut());
125-
assert!(!runtime.is_null());
126-
let context = JS_GetContext(runtime);
127133
assert!(!context.is_null());
128134
InitSelfHostedCode(context);
129-
TopRuntime(runtime)
135+
TopContext(context)
130136
}
131137
};
132138
}
133139

134-
let js_runtime =
135-
JS_NewRuntime(default_heapsize, ChunkSize as u32, PARENT.0);
136-
assert!(!js_runtime.is_null());
140+
let js_context =
141+
JS_NewContext(default_heapsize, ChunkSize as u32, PARENT.0);
142+
assert!(!js_context.is_null());
137143

138144
// Unconstrain the runtime's threshold on nominal heap size, to avoid
139145
// triggering GC too often if operating continuously near an arbitrary
140146
// finite threshold. This leaves the maximum-JS_malloc-bytes threshold
141147
// still in effect to cause periodical, and we hope hygienic,
142148
// last-ditch GCs from within the GC's allocator.
143149
JS_SetGCParameter(
144-
js_runtime, JSGCParamKey::JSGC_MAX_BYTES, u32::MAX);
150+
js_context, JSGCParamKey::JSGC_MAX_BYTES, u32::MAX);
145151

146152
JS_SetNativeStackQuota(
147-
js_runtime,
153+
js_context,
148154
STACK_QUOTA,
149155
STACK_QUOTA - SYSTEM_CODE_BUFFER,
150156
STACK_QUOTA - SYSTEM_CODE_BUFFER - TRUSTED_SCRIPT_BUFFER);
151157

152-
let js_context = JS_GetContext(js_runtime);
153-
assert!(!js_context.is_null());
154-
155158
InitSelfHostedCode(js_context);
156159

157-
let runtimeopts = RuntimeOptionsRef(js_runtime);
158-
(*runtimeopts).set_baseline_(true);
159-
(*runtimeopts).set_ion_(true);
160-
(*runtimeopts).set_nativeRegExp_(true);
160+
let opts = ContextOptionsRef(js_context);
161+
(*opts).set_baseline_(true);
162+
(*opts).set_ion_(true);
163+
(*opts).set_nativeRegExp_(true);
161164

162-
SetWarningReporter(js_runtime, Some(report_warning));
165+
SetWarningReporter(js_context, Some(report_warning));
163166

164167
JS_BeginRequest(js_context);
165168

166169
Runtime {
167-
rt: js_runtime,
168170
cx: js_context,
169171
}
170172
}
171173
}
172174

173-
/// Returns the `JSRuntime` object.
174-
pub fn rt(&self) -> *mut JSRuntime {
175-
self.rt
176-
}
177-
178-
/// Returns the `JSContext` object.
175+
/// Returns the underlying `JSContext` object.
179176
pub fn cx(&self) -> *mut JSContext {
180177
self.cx
181178
}
@@ -215,7 +212,7 @@ impl Drop for Runtime {
215212
fn drop(&mut self) {
216213
unsafe {
217214
JS_EndRequest(self.cx);
218-
JS_DestroyRuntime(self.rt);
215+
JS_DestroyContext(self.cx);
219216
}
220217
}
221218
}
@@ -285,14 +282,26 @@ impl<T> Rooted<T> {
285282
}
286283
}
287284

285+
unsafe fn get_root_stack(cx: &mut ContextFriendFields)
286+
-> *mut *mut Rooted<*mut ::std::os::raw::c_void>
287+
where T: RootKind
288+
{
289+
let kind = T::rootKind() as usize;
290+
if let Some(zone) = cx.zone_.as_mut() {
291+
&mut zone.stackRoots_[kind] as *mut _ as *mut _
292+
} else {
293+
&mut cx._base.roots.stackRoots_[kind] as *mut _ as *mut _
294+
}
295+
}
296+
288297
pub unsafe fn add_to_root_stack(&mut self, cx: *mut JSContext) where T: RootKind {
289298
let ctxfriend: &mut ContextFriendFields = mem::transmute(cx);
290299

291-
let kind = T::rootKind() as usize;
292-
self.stack = &mut ctxfriend.roots.stackRoots_[kind] as *mut _ as *mut _;
293-
self.prev = ctxfriend.roots.stackRoots_[kind] as *mut _;
300+
self.stack = Self::get_root_stack(ctxfriend);
301+
let stack = self.stack.as_mut().unwrap();
302+
self.prev = *stack as *mut _;
294303

295-
ctxfriend.roots.stackRoots_[kind] = self as *mut _ as usize as _;
304+
*stack = self as *mut _ as usize as _;
296305
}
297306

298307
pub unsafe fn remove_from_root_stack(&mut self) {

0 commit comments

Comments
 (0)