0
0
Fork 0
mirror of https://github.com/denoland/rusty_v8.git synced 2024-11-21 15:04:33 -05:00

Expose from_function_callback_info (#1016)

This commit is contained in:
Divy Srivastava 2022-06-28 16:49:37 +05:30 committed by GitHub
parent b9d44e31a2
commit 95f2ba399c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 46 additions and 6 deletions

View file

@ -132,8 +132,10 @@ pub struct ReturnValue<'cb>(*mut Value, PhantomData<&'cb ()>);
/// and for our purposes we currently don't need
/// other types. So for now it's a simplified version.
impl<'cb> ReturnValue<'cb> {
fn from_function_callback_info(info: *const FunctionCallbackInfo) -> Self {
let slot = unsafe { v8__FunctionCallbackInfo__GetReturnValue(info) };
pub unsafe fn from_function_callback_info(
info: *const FunctionCallbackInfo,
) -> Self {
let slot = v8__FunctionCallbackInfo__GetReturnValue(info);
Self(slot, PhantomData)
}
@ -209,7 +211,7 @@ pub struct FunctionCallbackArguments<'s> {
}
impl<'s> FunctionCallbackArguments<'s> {
pub(crate) fn from_function_callback_info(
pub unsafe fn from_function_callback_info(
info: *const FunctionCallbackInfo,
) -> Self {
Self {
@ -328,8 +330,9 @@ where
fn mapping() -> Self {
let f = |info: *const FunctionCallbackInfo| {
let scope = &mut unsafe { CallbackScope::new(&*info) };
let args = FunctionCallbackArguments::from_function_callback_info(info);
let rv = ReturnValue::from_function_callback_info(info);
let args =
unsafe { FunctionCallbackArguments::from_function_callback_info(info) };
let rv = unsafe { ReturnValue::from_function_callback_info(info) };
(F::get())(scope, args, rv);
};
f.to_c_fn()

View file

@ -173,7 +173,8 @@ where
F: UnitType + Fn(&mut HandleScope, Local<Value>, WasmStreaming),
{
let scope = &mut unsafe { CallbackScope::new(&*info) };
let args = FunctionCallbackArguments::from_function_callback_info(info);
let args =
unsafe { FunctionCallbackArguments::from_function_callback_info(info) };
let data = args.data().unwrap(); // Always present.
let data = &*data as *const Value;
let zero = null_mut();

View file

@ -2277,6 +2277,42 @@ fn nested_builder<'a>(
.build(scope);
}
#[test]
fn function_builder_raw() {
let _setup_guard = setup();
let isolate = &mut v8::Isolate::new(Default::default());
{
let scope = &mut v8::HandleScope::new(isolate);
let context = v8::Context::new(scope);
let scope = &mut v8::ContextScope::new(scope, context);
let global = context.global(scope);
let recv: v8::Local<v8::Value> = global.into();
extern "C" fn callback(info: *const v8::FunctionCallbackInfo) {
let scope = unsafe { &mut v8::CallbackScope::new(&*info) };
let args = unsafe {
v8::FunctionCallbackArguments::from_function_callback_info(info)
};
assert!(args.length() == 1);
assert!(args.get(0).is_string());
let mut rv =
unsafe { v8::ReturnValue::from_function_callback_info(info) };
rv.set(
v8::String::new(scope, "Hello from function!")
.unwrap()
.into(),
);
}
let func = v8::Function::new_raw(scope, callback).unwrap();
let arg0 = v8::String::new(scope, "Hello").unwrap();
let value = func.call(scope, recv, &[arg0.into()]).unwrap();
assert!(value.is_string());
assert_eq!(value.to_rust_string_lossy(scope), "Hello from function!");
}
}
#[test]
fn return_value() {
let _setup_guard = setup();