@@ -16,9 +16,12 @@ use mozjs::jsapi::*;
1616use mozjs:: jsval:: ObjectValue ;
1717use mozjs:: jsval:: UndefinedValue ;
1818use mozjs:: rooted;
19- use mozjs:: rust:: wrappers:: { Construct1 , JS_GetProperty , JS_SetProperty } ;
19+ use mozjs:: rust:: wrappers2:: {
20+ Call , Construct1 , JS_DefineFunction , JS_GetProperty , JS_NewGlobalObject , JS_NewPlainObject ,
21+ JS_SetProperty , NewArrayBufferWithUserOwnedContents ,
22+ } ;
2023use mozjs:: rust:: SIMPLE_GLOBAL_CLASS ;
21- use mozjs:: rust:: { IntoHandle , JSEngine , RealmOptions , Runtime } ;
24+ use mozjs:: rust:: { HandleValue , IntoHandle , JSEngine , RealmOptions , Runtime } ;
2225use mozjs_sys:: jsgc:: ValueArray ;
2326
2427#[ repr( align( 8 ) ) ]
@@ -47,36 +50,37 @@ unsafe extern "C" fn bar(_cx: *mut JSContext, argc: u32, vp: *mut Value) -> bool
4750 true
4851}
4952
50- fn run ( rt : Runtime ) {
53+ fn run ( mut rt : Runtime ) {
5154 let options = RealmOptions :: default ( ) ;
52- rooted ! ( in( rt. cx( ) ) let global = unsafe {
53- JS_NewGlobalObject ( rt. cx( ) , & SIMPLE_GLOBAL_CLASS , ptr:: null_mut( ) ,
55+ let cx = rt. cx ( ) ;
56+ rooted ! ( & in( cx) let global = unsafe {
57+ JS_NewGlobalObject ( cx, & SIMPLE_GLOBAL_CLASS , ptr:: null_mut( ) ,
5458 OnNewGlobalHookOption :: FireOnNewGlobalHook ,
5559 & * options)
5660 } ) ;
57- let _ac = JSAutoRealm :: new ( rt . cx ( ) , global. get ( ) ) ;
61+ let _ac = JSAutoRealm :: new ( unsafe { cx . raw_cx ( ) } , global. get ( ) ) ;
5862
5963 // Get WebAssembly.Module and WebAssembly.Instance constructors.
60- rooted ! ( in( rt . cx ( ) ) let mut wasm = UndefinedValue ( ) ) ;
61- rooted ! ( in( rt . cx ( ) ) let mut wasm_module = UndefinedValue ( ) ) ;
62- rooted ! ( in( rt . cx ( ) ) let mut wasm_instance = UndefinedValue ( ) ) ;
64+ rooted ! ( & in( cx ) let mut wasm = UndefinedValue ( ) ) ;
65+ rooted ! ( & in( cx ) let mut wasm_module = UndefinedValue ( ) ) ;
66+ rooted ! ( & in( cx ) let mut wasm_instance = UndefinedValue ( ) ) ;
6367
6468 unsafe {
6569 assert ! ( JS_GetProperty (
66- rt . cx ( ) ,
70+ cx ,
6771 global. handle( ) ,
6872 c"WebAssembly" . as_ptr( ) ,
6973 wasm. handle_mut( )
7074 ) ) ;
71- rooted ! ( in( rt . cx ( ) ) let mut wasm_obj = wasm. to_object( ) ) ;
75+ rooted ! ( & in( cx ) let mut wasm_obj = wasm. to_object( ) ) ;
7276 assert ! ( JS_GetProperty (
73- rt . cx ( ) ,
77+ cx ,
7478 wasm_obj. handle( ) ,
7579 c"Module" . as_ptr( ) ,
7680 wasm_module. handle_mut( )
7781 ) ) ;
7882 assert ! ( JS_GetProperty (
79- rt . cx ( ) ,
83+ cx ,
8084 wasm_obj. handle( ) ,
8185 c"Instance" . as_ptr( ) ,
8286 wasm_instance. handle_mut( )
@@ -86,86 +90,83 @@ fn run(rt: Runtime) {
8690 assert ! ( HI_WASM . 0 . as_ptr( ) as usize % 8 == 0 ) ;
8791
8892 // Construct Wasm module from bytes.
89- rooted ! ( in( rt . cx ( ) ) let mut module = null_mut:: <JSObject >( ) ) ;
93+ rooted ! ( & in( cx ) let mut module = null_mut:: <JSObject >( ) ) ;
9094 {
91- let array_buffer = JS :: NewArrayBufferWithUserOwnedContents (
92- rt. cx ( ) ,
93- HI_WASM . 0 . len ( ) ,
94- HI_WASM . 0 . as_ptr ( ) as _ ,
95- ) ;
95+ let array_buffer =
96+ NewArrayBufferWithUserOwnedContents ( cx, HI_WASM . 0 . len ( ) , HI_WASM . 0 . as_ptr ( ) as _ ) ;
9697 assert ! ( !array_buffer. is_null( ) ) ;
9798
98- rooted ! ( in( rt . cx ( ) ) let val = ObjectValue ( array_buffer) ) ;
99+ rooted ! ( & in( cx ) let val = ObjectValue ( array_buffer) ) ;
99100 let args = HandleValueArray :: from ( val. handle ( ) . into_handle ( ) ) ;
100101
101102 assert ! ( Construct1 (
102- rt . cx ( ) ,
103+ cx ,
103104 wasm_module. handle( ) ,
104105 & args,
105106 module. handle_mut( )
106107 ) )
107108 }
108109
109110 // Construct Wasm module instance with required imports.
110- rooted ! ( in( rt . cx ( ) ) let mut instance = null_mut:: <JSObject >( ) ) ;
111+ rooted ! ( & in( cx ) let mut instance = null_mut:: <JSObject >( ) ) ;
111112 {
112113 // Build "env" imports object.
113- rooted ! ( in( rt . cx ( ) ) let mut env_import_obj = JS_NewPlainObject ( rt . cx ( ) ) ) ;
114+ rooted ! ( & in( cx ) let mut env_import_obj = JS_NewPlainObject ( cx ) ) ;
114115 assert ! ( !env_import_obj. is_null( ) ) ;
115116 let function = JS_DefineFunction (
116- rt . cx ( ) ,
117+ cx ,
117118 env_import_obj. handle ( ) . into ( ) ,
118119 c"bar" . as_ptr ( ) ,
119120 Some ( bar) ,
120121 1 ,
121122 0 ,
122123 ) ;
123124 assert ! ( !function. is_null( ) ) ;
124- rooted ! ( in( rt . cx ( ) ) let mut env_import = ObjectValue ( env_import_obj. get( ) ) ) ;
125+ rooted ! ( & in( cx ) let mut env_import = ObjectValue ( env_import_obj. get( ) ) ) ;
125126 // Build imports bag.
126- rooted ! ( in( rt . cx ( ) ) let mut imports = JS_NewPlainObject ( rt . cx ( ) ) ) ;
127+ rooted ! ( & in( cx ) let mut imports = JS_NewPlainObject ( cx ) ) ;
127128 assert ! ( !imports. is_null( ) ) ;
128129 assert ! ( JS_SetProperty (
129- rt . cx ( ) ,
130+ cx ,
130131 imports. handle( ) ,
131132 c"env" . as_ptr( ) ,
132133 env_import. handle( )
133134 ) ) ;
134135
135- rooted ! ( in( rt . cx ( ) ) let mut args = ValueArray :: new( [ ObjectValue ( module. get( ) ) , ObjectValue ( imports. get( ) ) ] ) ) ;
136+ rooted ! ( & in( cx ) let mut args = ValueArray :: new( [ ObjectValue ( module. get( ) ) , ObjectValue ( imports. get( ) ) ] ) ) ;
136137
137138 assert ! ( Construct1 (
138- rt . cx ( ) ,
139+ cx ,
139140 wasm_instance. handle( ) ,
140141 & HandleValueArray :: from( & args) ,
141142 instance. handle_mut( )
142143 ) ) ;
143144 }
144145
145146 // Find `foo` method in exports.
146- rooted ! ( in( rt . cx ( ) ) let mut exports = UndefinedValue ( ) ) ;
147+ rooted ! ( & in( cx ) let mut exports = UndefinedValue ( ) ) ;
147148
148149 assert ! ( JS_GetProperty (
149- rt . cx ( ) ,
150+ cx ,
150151 instance. handle( ) ,
151152 c"exports" . as_ptr( ) ,
152153 exports. handle_mut( )
153154 ) ) ;
154155
155- rooted ! ( in( rt . cx ( ) ) let mut exports_obj = exports. to_object( ) ) ;
156- rooted ! ( in( rt . cx ( ) ) let mut foo = UndefinedValue ( ) ) ;
156+ rooted ! ( & in( cx ) let mut exports_obj = exports. to_object( ) ) ;
157+ rooted ! ( & in( cx ) let mut foo = UndefinedValue ( ) ) ;
157158 assert ! ( JS_GetProperty (
158- rt . cx ( ) ,
159+ cx ,
159160 exports_obj. handle( ) ,
160161 c"foo" . as_ptr( ) ,
161162 foo. handle_mut( )
162163 ) ) ;
163164
164165 // call foo and get its result
165- rooted ! ( in( rt . cx ( ) ) let mut rval = UndefinedValue ( ) ) ;
166+ rooted ! ( & in( cx ) let mut rval = UndefinedValue ( ) ) ;
166167 assert ! ( Call (
167- rt . cx ( ) ,
168- JS :: UndefinedHandleValue ,
168+ cx ,
169+ HandleValue :: undefined ( ) ,
169170 foo. handle( ) . into( ) ,
170171 & HandleValueArray :: empty( ) ,
171172 rval. handle_mut( ) . into( )
@@ -180,7 +181,6 @@ fn run(rt: Runtime) {
180181fn main ( ) {
181182 let engine = JSEngine :: init ( ) . expect ( "failed to initalize JS engine" ) ;
182183 let runtime = Runtime :: new ( engine. handle ( ) ) ;
183- assert ! ( !runtime. cx( ) . is_null( ) , "failed to create JSContext" ) ;
184184 run ( runtime) ;
185185}
186186
0 commit comments