-
Notifications
You must be signed in to change notification settings - Fork 288
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Object::call_with()
#879
Object::call_with()
#879
Conversation
I like
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dherman This looks great! Other than the small suggestion to simplify, my only open question is on naming.
I'm torn between call_with
and call_method
.
I personally prefer
I do agree it's a little unwieldy, but maybe not a deal breaker. |
@mathieulj Thanks for the feedback!
Apologies if this seems like too much of a nit, but the two methods return the same type, |
No worries, my thoughts are in flux, I value differences in opinions and I don't feel particularly strongly about it. The two both return |
The inconsistencies both in arguments types and in fallibility of the return type bothers me too, @mathieulj. And I feel like without the word "method" it sounds too much like we're calling the object as if it were a function. So I think I'm pretty anti- @kjvalencik I'm sympathetic to |
I'm sufficiently convinced that // I don't mind it at all when chaining.
let n: Handle<JsNumber> = obj
- .call_method(&mut cx, "getN")?
+ .call_method_with(&mut cx, "getN")?
.arg(cx.number(42))
.apply(&mut cx)?;
// It's a little awkward on one line, but not terrible
- let n: Handle<JsNumber> = obj.call_method(&mut cx, "getN")?.applyt(&mut cx)?;
+ let n: Handle<JsNumber> = obj.call_method_with(&mut cx, "getN")?.applyt(&mut cx)?; |
Thanks for the example, @kjvalencik! I'm leaning towards |
@dherman I like that plan! |
Co-authored-by: K.J. Valencik <kjvalencik@gmail.com>
use crate::types::function::CallOptions; | ||
use crate::types::utf8::Utf8; | ||
use crate::types::{build, JsUndefined, JsValue, Value}; | ||
use crate::types::{build, JsFunction, JsUndefined, JsValue, Value}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit, these additions are throwing unused warnings on the legacy backend. Probably not a big deal because that's getting removed very soon.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
death to legacy…
K: PropertyKey, | ||
{ | ||
let mut options = self.get::<JsFunction, _, _>(cx, method)?.call_with(cx); | ||
options.this(JsValue::new_internal(self.to_raw())); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Flagging this for follow-up. JsValue::new_internal
should be marked unsafe
since it allows arbitrarily extending a lifetime.
This usage of it is safe because it's bound by a new Context
which will always be the shortest possible.
@dherman This looks fantastic and is ready to merge! I filed a nit and a follow-up, but no action is needed. |
Add
Object::call_with()
convenience method to call a method on an object.Example:
Closes #873.
Design questions
JsFunction::call_with()
?date.call_with()
vsdate.call_method()
vsdate.call_method_with()