0
0
Fork 0
mirror of https://github.com/denoland/rusty_v8.git synced 2024-11-24 15:19:31 -05:00

feat: add call_with_context to v8::Function (#1350)

This commit is contained in:
Matt Mastracci 2023-10-26 12:28:14 -06:00 committed by GitHub
parent be0e017cd5
commit 01d82405e1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 50 additions and 0 deletions

View file

@ -850,6 +850,7 @@ impl Function {
Self::builder_raw(callback).build(scope)
}
/// Call a function in a context scope.
#[inline]
pub fn call<'s>(
&self,
@ -867,6 +868,34 @@ impl Function {
}
}
/// Call a function in a given context.
#[inline]
pub fn call_with_context<'s>(
&self,
scope: &mut HandleScope<'s, ()>,
context: Local<Context>,
recv: Local<Value>,
args: &[Local<Value>],
) -> Option<Local<'s, Value>> {
let args = Local::slice_into_raw(args);
let argc = int::try_from(args.len()).unwrap();
let argv = args.as_ptr();
unsafe {
let ret = v8__Function__Call(
self,
context.as_non_null().as_ptr(),
&*recv,
argc,
argv,
);
if ret.is_null() {
None
} else {
scope.cast_local(|_| ret)
}
}
}
#[inline(always)]
pub fn new_instance<'s>(
&self,

View file

@ -3897,6 +3897,27 @@ fn function() {
assert!(value.is_boolean());
assert!(value.boolean_value(scope));
}
{
let mut root_scope = v8::HandleScope::new(isolate);
let context = v8::Context::new(&mut root_scope);
let mut scope = v8::ContextScope::new(&mut root_scope, context);
let function: v8::Local<v8::Function> =
eval(&mut scope, "function a() { return 1; }; a")
.unwrap()
.try_into()
.unwrap();
drop(scope);
let recv = v8::undefined(&mut root_scope).into();
let ret = function
.call_with_context(&mut root_scope, context, recv, &[])
.unwrap();
let integer: v8::Local<v8::Integer> = ret.try_into().unwrap();
let mut scope = v8::ContextScope::new(&mut root_scope, context);
assert_eq!(integer.int32_value(&mut scope).unwrap(), 1);
}
}
#[test]