Skip to content

Commit 55f2867

Browse files
committed
fix: Remove &JSContext from JSObject::call_as_function.
This patch removes the `&JSContext` argument from `JSObject::call_as_function`. All the other methods adopt this style, and I believe it makes total sense.
1 parent 4fafe4a commit 55f2867

File tree

1 file changed

+18
-12
lines changed

1 file changed

+18
-12
lines changed

src/object.rs

+18-12
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// except according to those terms.
66

77
use super::{JSObject, JSString};
8-
use crate::{sys, JSContext, JSException, JSValue};
8+
use crate::{sys, JSException, JSValue};
99
use std::ops::Deref;
1010
use std::ptr;
1111

@@ -144,7 +144,6 @@ impl JSObject {
144144
/// let pow = math.get_property("pow").as_object().unwrap();
145145
///
146146
/// let result = pow.call_as_function(
147-
/// &ctx,
148147
/// None,
149148
/// &[JSValue::new_number(&ctx, 2.), JSValue::new_number(&ctx, 3.)],
150149
/// ).unwrap();
@@ -153,7 +152,6 @@ impl JSObject {
153152
/// ```
154153
pub fn call_as_function(
155154
&self,
156-
context: &JSContext,
157155
this: Option<&JSObject>,
158156
arguments: &[JSValue],
159157
) -> Result<JSValue, JSException> {
@@ -162,10 +160,11 @@ impl JSObject {
162160
.map(|argument| argument.raw)
163161
.collect::<Vec<_>>();
164162
let mut exception: sys::JSValueRef = ptr::null_mut();
163+
let context = self.value.ctx;
165164

166165
let result = unsafe {
167166
sys::JSObjectCallAsFunction(
168-
context.raw,
167+
context,
169168
self.raw,
170169
this.map(|this| this.raw).unwrap_or_else(ptr::null_mut),
171170
arguments.len(),
@@ -178,23 +177,31 @@ impl JSObject {
178177
return Err(JSException {
179178
value: JSValue {
180179
raw: exception,
181-
ctx: context.raw,
180+
ctx: context,
182181
},
183182
});
184183
}
185184

186185
if result.is_null() {
187186
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+
},
192199
});
193200
}
194201

195202
Ok(JSValue {
196203
raw: result,
197-
ctx: context.raw,
204+
ctx: context,
198205
})
199206
}
200207
}
@@ -297,7 +304,6 @@ mod tests {
297304
let pow = math.get_property("pow").as_object()?;
298305

299306
let result = pow.call_as_function(
300-
&ctx,
301307
None,
302308
&[JSValue::new_number(&ctx, 2.), JSValue::new_number(&ctx, 3.)],
303309
)?;
@@ -307,7 +313,7 @@ mod tests {
307313
// Not a function, it's a constant.
308314
let e = math.get_property("E").as_object()?;
309315

310-
assert!(e.call_as_function(&ctx, None, &[]).is_err());
316+
assert!(e.call_as_function(None, &[]).is_err());
311317

312318
Ok(())
313319
}

0 commit comments

Comments
 (0)