theduke / quickjs-rs

Rust wrapper for the quickjs Javascript engine.
MIT License
570 stars 48 forks source link

[Req] Add support for JsValue as add_callback closure parameter #109

Closed monoclex closed 3 years ago

monoclex commented 3 years ago

It would be nice if closures in add_callback could optionally take a JsValue. The use cases for doing such would be possibly compatibility with other JS apis, but in my specific use case, I happen to want to convert a Range<usize> into a JsValue so I can blackbox the conversion logic of Range<usize> -> JsValue (as I truncate the range into two u32s and store them into a single f64 in the name of micro optimizations, and I don't want to expose that ugly logic anywhere).

Example of code that would work after this change:

fn prime(context: &mut Context) {
    context.add_callback("asdf", |a: JsValue| JsValue::Undefined);
}