Skip to content

How to pass JavaScript functions from V8 to Rust? #19231

Closed Answered by bartlomieju
adhamsalama asked this question in Q&A
Discussion options

You must be logged in to vote

Example:

#[op(v8)]
fn op_set_promise_reject_callback<'a>(
scope: &mut v8::HandleScope<'a>,
cb: serde_v8::Value,
) -> Result<Option<serde_v8::Value<'a>>, Error> {
let cb = to_v8_fn(scope, cb)?;
let context_state_rc = JsRealm::state_from_scope(scope);
let old = context_state_rc
.borrow_mut()
.js_promise_reject_cb
.replace(Rc::new(cb));
let old = old.map(|v| v8::Local::new(scope, &*v));
Ok(old.map(|v| from_v8(scope, v.into()).unwrap()))
}
.

That said - this is limited to only synchronous ops. You won't be able to pass and call a JS function in an asynchronou…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@adhamsalama
Comment options

@adhamsalama
Comment options

Answer selected by adhamsalama
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants