5
5
// except according to those terms.
6
6
7
7
use super :: { JSObject , JSString } ;
8
- use crate :: { sys, JSContext , JSException , JSValue } ;
8
+ use crate :: { sys, JSException , JSValue } ;
9
9
use std:: ops:: Deref ;
10
10
use std:: ptr;
11
11
@@ -144,7 +144,6 @@ impl JSObject {
144
144
/// let pow = math.get_property("pow").as_object().unwrap();
145
145
///
146
146
/// let result = pow.call_as_function(
147
- /// &ctx,
148
147
/// None,
149
148
/// &[JSValue::new_number(&ctx, 2.), JSValue::new_number(&ctx, 3.)],
150
149
/// ).unwrap();
@@ -153,7 +152,6 @@ impl JSObject {
153
152
/// ```
154
153
pub fn call_as_function (
155
154
& self ,
156
- context : & JSContext ,
157
155
this : Option < & JSObject > ,
158
156
arguments : & [ JSValue ] ,
159
157
) -> Result < JSValue , JSException > {
@@ -162,10 +160,11 @@ impl JSObject {
162
160
. map ( |argument| argument. raw )
163
161
. collect :: < Vec < _ > > ( ) ;
164
162
let mut exception: sys:: JSValueRef = ptr:: null_mut ( ) ;
163
+ let context = self . value . ctx ;
165
164
166
165
let result = unsafe {
167
166
sys:: JSObjectCallAsFunction (
168
- context. raw ,
167
+ context,
169
168
self . raw ,
170
169
this. map ( |this| this. raw ) . unwrap_or_else ( ptr:: null_mut) ,
171
170
arguments. len ( ) ,
@@ -178,23 +177,31 @@ impl JSObject {
178
177
return Err ( JSException {
179
178
value : JSValue {
180
179
raw : exception,
181
- ctx : context. raw ,
180
+ ctx : context,
182
181
} ,
183
182
} ) ;
184
183
}
185
184
186
185
if result. is_null ( ) {
187
186
return Err ( JSException {
188
- value : JSValue :: new_string (
189
- context,
190
- "Cannot call this object as a function: it is not a valid function" ,
191
- ) ,
187
+ value : JSValue {
188
+ raw : unsafe {
189
+ sys:: JSValueMakeString (
190
+ context,
191
+ JSString :: from (
192
+ "Cannot call this object as a function: it is not a valid function" ,
193
+ )
194
+ . raw ,
195
+ )
196
+ } ,
197
+ ctx : context,
198
+ } ,
192
199
} ) ;
193
200
}
194
201
195
202
Ok ( JSValue {
196
203
raw : result,
197
- ctx : context. raw ,
204
+ ctx : context,
198
205
} )
199
206
}
200
207
}
@@ -297,7 +304,6 @@ mod tests {
297
304
let pow = math. get_property ( "pow" ) . as_object ( ) ?;
298
305
299
306
let result = pow. call_as_function (
300
- & ctx,
301
307
None ,
302
308
& [ JSValue :: new_number ( & ctx, 2. ) , JSValue :: new_number ( & ctx, 3. ) ] ,
303
309
) ?;
@@ -307,7 +313,7 @@ mod tests {
307
313
// Not a function, it's a constant.
308
314
let e = math. get_property ( "E" ) . as_object ( ) ?;
309
315
310
- assert ! ( e. call_as_function( & ctx , None , & [ ] ) . is_err( ) ) ;
316
+ assert ! ( e. call_as_function( None , & [ ] ) . is_err( ) ) ;
311
317
312
318
Ok ( ( ) )
313
319
}
0 commit comments