1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
//! VapourSynth callable functions.

use std::marker::PhantomData;
use std::ops::{Deref, DerefMut};
use std::os::raw::c_void;
use std::ptr::NonNull;
use std::{mem, panic, process};
use vapoursynth_sys as ffi;

use crate::api::API;
use crate::core::CoreRef;
use crate::map::{Map, MapRef, MapRefMut};

/// Holds a reference to a function that may be called.
#[derive(Debug)]
pub struct Function<'core> {
    handle: NonNull<ffi::VSFuncRef>,
    _owner: PhantomData<&'core ()>,
}

unsafe impl<'core> Send for Function<'core> {}
unsafe impl<'core> Sync for Function<'core> {}

impl<'core> Drop for Function<'core> {
    #[inline]
    fn drop(&mut self) {
        unsafe {
            API::get_cached().free_func(self.handle.as_ptr());
        }
    }
}

impl<'core> Clone for Function<'core> {
    #[inline]
    fn clone(&self) -> Self {
        let handle = unsafe { API::get_cached().clone_func(self.handle.as_ptr()) };
        Self {
            handle: unsafe { NonNull::new_unchecked(handle) },
            _owner: PhantomData,
        }
    }
}

impl<'core> Function<'core> {
    /// Wraps `handle` in a `Function`.
    ///
    /// # Safety
    /// The caller must ensure `handle` and the lifetime are valid and API is cached.
    #[inline]
    pub(crate) unsafe fn from_ptr(handle: *mut ffi::VSFuncRef) -> Self {
        Self {
            handle: NonNull::new_unchecked(handle),
            _owner: PhantomData,
        }
    }

    /// Returns the underlying pointer.
    #[inline]
    pub(crate) fn ptr(&self) -> *mut ffi::VSFuncRef {
        self.handle.as_ptr()
    }

    /// Creates a new function.
    ///
    /// To indicate an error from the callback, set an error on the output map.
    #[inline]
    pub fn new<F>(api: API, core: CoreRef<'core>, callback: F) -> Self
    where
        F: Fn(API, CoreRef<'core>, &Map<'core>, &mut Map<'core>) + Send + Sync + 'core,
    {
        unsafe extern "system" fn c_callback<'core, F>(
            in_: *const ffi::VSMap,
            out: *mut ffi::VSMap,
            user_data: *mut c_void,
            core: *mut ffi::VSCore,
            _vsapi: *const ffi::VSAPI,
        ) where
            F: Fn(API, CoreRef<'core>, &Map<'core>, &mut Map<'core>) + Send + Sync + 'core,
        {
            let closure = move || {
                let api = API::get_cached();
                let core = CoreRef::from_ptr(core);
                let in_ = MapRef::from_ptr(in_);
                let mut out = MapRefMut::from_ptr(out);
                let callback = Box::from_raw(user_data as *mut F);

                callback(api, core, &in_, &mut out);

                mem::forget(callback);
            };

            if panic::catch_unwind(closure).is_err() {
                process::abort();
            }
        }

        unsafe extern "system" fn c_free<F>(user_data: *mut c_void) {
            drop(Box::from_raw(user_data as *mut F))
        }

        let data = Box::new(callback);

        let handle = unsafe {
            API::get_cached().create_func(
                c_callback::<'core, F>,
                Box::into_raw(data) as _,
                Some(c_free::<F>),
                core.ptr(),
            )
        };

        Self {
            handle: unsafe { NonNull::new_unchecked(handle) },
            _owner: PhantomData,
        }
    }

    /// Calls the function. If the call fails `out` will have an error set.
    #[inline]
    pub fn call(&self, in_: &Map<'core>, out: &mut Map<'core>) {
        unsafe { API::get_cached().call_func(self.handle.as_ptr(), in_.deref(), out.deref_mut()) };
    }
}