mirror of
https://github.com/denoland/rusty_v8.git
synced 2024-11-24 15:19:31 -05:00
Expose from_function_callback_info
(#1016)
This commit is contained in:
parent
b9d44e31a2
commit
95f2ba399c
3 changed files with 46 additions and 6 deletions
|
@ -132,8 +132,10 @@ pub struct ReturnValue<'cb>(*mut Value, PhantomData<&'cb ()>);
|
||||||
/// and for our purposes we currently don't need
|
/// and for our purposes we currently don't need
|
||||||
/// other types. So for now it's a simplified version.
|
/// other types. So for now it's a simplified version.
|
||||||
impl<'cb> ReturnValue<'cb> {
|
impl<'cb> ReturnValue<'cb> {
|
||||||
fn from_function_callback_info(info: *const FunctionCallbackInfo) -> Self {
|
pub unsafe fn from_function_callback_info(
|
||||||
let slot = unsafe { v8__FunctionCallbackInfo__GetReturnValue(info) };
|
info: *const FunctionCallbackInfo,
|
||||||
|
) -> Self {
|
||||||
|
let slot = v8__FunctionCallbackInfo__GetReturnValue(info);
|
||||||
Self(slot, PhantomData)
|
Self(slot, PhantomData)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -209,7 +211,7 @@ pub struct FunctionCallbackArguments<'s> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'s> FunctionCallbackArguments<'s> {
|
impl<'s> FunctionCallbackArguments<'s> {
|
||||||
pub(crate) fn from_function_callback_info(
|
pub unsafe fn from_function_callback_info(
|
||||||
info: *const FunctionCallbackInfo,
|
info: *const FunctionCallbackInfo,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
Self {
|
Self {
|
||||||
|
@ -328,8 +330,9 @@ where
|
||||||
fn mapping() -> Self {
|
fn mapping() -> Self {
|
||||||
let f = |info: *const FunctionCallbackInfo| {
|
let f = |info: *const FunctionCallbackInfo| {
|
||||||
let scope = &mut unsafe { CallbackScope::new(&*info) };
|
let scope = &mut unsafe { CallbackScope::new(&*info) };
|
||||||
let args = FunctionCallbackArguments::from_function_callback_info(info);
|
let args =
|
||||||
let rv = ReturnValue::from_function_callback_info(info);
|
unsafe { FunctionCallbackArguments::from_function_callback_info(info) };
|
||||||
|
let rv = unsafe { ReturnValue::from_function_callback_info(info) };
|
||||||
(F::get())(scope, args, rv);
|
(F::get())(scope, args, rv);
|
||||||
};
|
};
|
||||||
f.to_c_fn()
|
f.to_c_fn()
|
||||||
|
|
|
@ -173,7 +173,8 @@ where
|
||||||
F: UnitType + Fn(&mut HandleScope, Local<Value>, WasmStreaming),
|
F: UnitType + Fn(&mut HandleScope, Local<Value>, WasmStreaming),
|
||||||
{
|
{
|
||||||
let scope = &mut unsafe { CallbackScope::new(&*info) };
|
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 = args.data().unwrap(); // Always present.
|
||||||
let data = &*data as *const Value;
|
let data = &*data as *const Value;
|
||||||
let zero = null_mut();
|
let zero = null_mut();
|
||||||
|
|
|
@ -2277,6 +2277,42 @@ fn nested_builder<'a>(
|
||||||
.build(scope);
|
.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]
|
#[test]
|
||||||
fn return_value() {
|
fn return_value() {
|
||||||
let _setup_guard = setup();
|
let _setup_guard = setup();
|
||||||
|
|
Loading…
Reference in a new issue