@@ -18,9 +18,9 @@ use std::cell::UnsafeCell;
18
18
use std:: marker:: PhantomData ;
19
19
use consts:: { JSCLASS_RESERVED_SLOTS_MASK , JSCLASS_GLOBAL_SLOT_COUNT , JSCLASS_IS_GLOBAL } ;
20
20
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 } ;
24
24
use jsapi:: { SetWarningReporter , Evaluate2 , JSErrorReport } ;
25
25
use jsapi:: { JS_SetGCParameter , JSGCParamKey } ;
26
26
use jsapi:: { Heap , HeapObjectPostBarrier , HeapValuePostBarrier } ;
@@ -103,79 +103,76 @@ impl ToResult for bool {
103
103
// ___________________________________________________________________________
104
104
// friendly Rustic API to runtimes
105
105
106
- /// A wrapper for the `JSRuntime` and ` JSContext` structures in SpiderMonkey.
106
+ /// A wrapper for the `JSContext` structure in SpiderMonkey.
107
107
pub struct Runtime {
108
- rt : * mut JSRuntime ,
109
108
cx : * mut JSContext ,
110
109
}
111
110
112
111
impl Runtime {
113
- /// Creates a new `JSRuntime` and ` JSContext`.
112
+ /// Creates a new `JSContext`.
114
113
pub fn new ( ) -> Runtime {
115
114
unsafe {
116
- struct TopRuntime ( * mut JSRuntime ) ;
117
- unsafe impl Sync for TopRuntime { }
115
+ struct TopContext ( * mut JSContext ) ;
116
+ unsafe impl Sync for TopContext { }
118
117
119
118
lazy_static ! {
120
- static ref PARENT : TopRuntime = {
119
+ static ref PARENT : TopContext = {
121
120
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 (
124
132
default_heapsize, ChunkSize as u32 , ptr:: null_mut( ) ) ;
125
- assert!( !runtime. is_null( ) ) ;
126
- let context = JS_GetContext ( runtime) ;
127
133
assert!( !context. is_null( ) ) ;
128
134
InitSelfHostedCode ( context) ;
129
- TopRuntime ( runtime )
135
+ TopContext ( context )
130
136
}
131
137
} ;
132
138
}
133
139
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( ) ) ;
137
143
138
144
// Unconstrain the runtime's threshold on nominal heap size, to avoid
139
145
// triggering GC too often if operating continuously near an arbitrary
140
146
// finite threshold. This leaves the maximum-JS_malloc-bytes threshold
141
147
// still in effect to cause periodical, and we hope hygienic,
142
148
// last-ditch GCs from within the GC's allocator.
143
149
JS_SetGCParameter (
144
- js_runtime , JSGCParamKey :: JSGC_MAX_BYTES , u32:: MAX ) ;
150
+ js_context , JSGCParamKey :: JSGC_MAX_BYTES , u32:: MAX ) ;
145
151
146
152
JS_SetNativeStackQuota (
147
- js_runtime ,
153
+ js_context ,
148
154
STACK_QUOTA ,
149
155
STACK_QUOTA - SYSTEM_CODE_BUFFER ,
150
156
STACK_QUOTA - SYSTEM_CODE_BUFFER - TRUSTED_SCRIPT_BUFFER ) ;
151
157
152
- let js_context = JS_GetContext ( js_runtime) ;
153
- assert ! ( !js_context. is_null( ) ) ;
154
-
155
158
InitSelfHostedCode ( js_context) ;
156
159
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 ) ;
161
164
162
- SetWarningReporter ( js_runtime , Some ( report_warning) ) ;
165
+ SetWarningReporter ( js_context , Some ( report_warning) ) ;
163
166
164
167
JS_BeginRequest ( js_context) ;
165
168
166
169
Runtime {
167
- rt : js_runtime,
168
170
cx : js_context,
169
171
}
170
172
}
171
173
}
172
174
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.
179
176
pub fn cx ( & self ) -> * mut JSContext {
180
177
self . cx
181
178
}
@@ -215,7 +212,7 @@ impl Drop for Runtime {
215
212
fn drop ( & mut self ) {
216
213
unsafe {
217
214
JS_EndRequest ( self . cx ) ;
218
- JS_DestroyRuntime ( self . rt ) ;
215
+ JS_DestroyContext ( self . cx ) ;
219
216
}
220
217
}
221
218
}
@@ -285,14 +282,26 @@ impl<T> Rooted<T> {
285
282
}
286
283
}
287
284
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
+
288
297
pub unsafe fn add_to_root_stack ( & mut self , cx : * mut JSContext ) where T : RootKind {
289
298
let ctxfriend: & mut ContextFriendFields = mem:: transmute ( cx) ;
290
299
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 _ ;
294
303
295
- ctxfriend . roots . stackRoots_ [ kind ] = self as * mut _ as usize as _ ;
304
+ * stack = self as * mut _ as usize as _ ;
296
305
}
297
306
298
307
pub unsafe fn remove_from_root_stack ( & mut self ) {
0 commit comments