diff --git a/src/function.rs b/src/function.rs index 4e7ae9cf..9f50b71e 100644 --- a/src/function.rs +++ b/src/function.rs @@ -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() diff --git a/src/wasm.rs b/src/wasm.rs index baeb97f2..91cf0ff3 100644 --- a/src/wasm.rs +++ b/src/wasm.rs @@ -173,7 +173,8 @@ where F: UnitType + Fn(&mut HandleScope, Local, 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(); diff --git a/tests/test_api.rs b/tests/test_api.rs index c910f41f..573571d1 100644 --- a/tests/test_api.rs +++ b/tests/test_api.rs @@ -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 = 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();