Skip to main content

function

Macro function 

Source
macro_rules! function {
    {// #0
        $name:ident ($($arguments:tt)+) $(-> $return_type:ty)? $body:block
    } => { ... };
    (// #1
        $name:ident use $func:path
    ) => { ... };
    (// #2
        @Name $name:ident
    ) => { ... };
    (// #3
        @Return $return_type:ty
    ) => { ... };
    (// #4
        @Return
    ) => { ... };
    {// #5
        @Parameters [$f:ident, $d:ident, $a:ident $(,)?]
        $frt:ident, $data:ident, $args:ident $(,)?
    } => { ... };
    {// #6
        @Parameters [$f:ident, $d:ident, $a:ident $(,)?]
        $frt:ident, $data:ident, _ $(,)?
    } => { ... };
    {// #7
        @Parameters [$f:ident, $d:ident, $a:ident $(,)?]
        $frt:ident, _, $args:ident $(,)?
    } => { ... };
    {// #8
        @Parameters [$f:ident, $d:ident, $a:ident $(,)?]
        _, $data:ident, $args:ident $(,)?
    } => { ... };
    {// #9
        @Parameters [$f:ident, $d:ident, $a:ident $(,)?]
        _, _, $args:ident $(,)?
    } => { ... };
    {// #10
        @Parameters [$f:ident, $d:ident, $a:ident $(,)?]
        _, $data:ident, _ $(,)?
    } => { ... };
    {// #11
        @Parameters [$f:ident, $d:ident, $a:ident $(,)?]
        $frt:ident, _, _ $(,)?
    } => { ... };
    {// #12
        @Parameters [$f:ident, $d:ident, $a:ident $(,)?]
        _, _, _ $(,)?
    } => { ... };
    (// #13
        @Unwind [$frt:expr_2021, $catched:expr_2021]
    ) => { ... };
}
Expand description

Defines a function intended for context registration by generating its ABI-compatible wrapper and binding it to a Rust implementation.

Expands to a &'static constant of type FunctionDefinition, intended to be added to a FunctionSet.

§Panic Handling

Any panic occurring within the function body is intercepted via std::panic::catch_unwind. Instead of unwinding across the FFI boundary, an ErrorObject containing the captured panic details is constructed and returned to the Flash Runtime.

This fallback return value is NOT constrained by the return type declared in the macro invocation. On the ActionScript side, the result may either be expected and handled as an Error, or not. In the latter case, if an ErrorObject is returned, casting it to a non-error type yields null and may lead to runtime exceptions.

When the ErrorObject is properly handled, the Flash Runtime remains stable. However, care must be taken to avoid leaving the native extension in an inconsistent state; resources should be managed reliably even in the presence of panics.

§Full Example

mod lib {
    use fre_rs::prelude::*;
    fre_rs::function! {
        method_name (frt, data, args) -> Option<Object> {
            return frt.current_context().get_actionscript_data().ok();
        }
    }
    fre_rs::function! (method_name2 use method_implementation);
    fn method_implementation <'a> (frt: &FlashRuntime<'a>, data: Option<&mut dyn Any>, args: &[Object<'a>]) -> Object<'a> {null}
}

§Minimal Example

mod lib {
    fre_rs::function! {
        method_name (_, _, _) {}
    }
}