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 [$c:ident, $d:ident, $a:ident $(,)?]
        $ctx:ident, $data:ident, $args:ident $(,)?
    } => { ... };
    {// #6
        @Parameters [$c:ident, $d:ident, $a:ident $(,)?]
        $ctx:ident, $data:ident, _ $(,)?
    } => { ... };
    {// #7
        @Parameters [$c:ident, $d:ident, $a:ident $(,)?]
        $ctx:ident, _, $args:ident $(,)?
    } => { ... };
    {// #8
        @Parameters [$c:ident, $d:ident, $a:ident $(,)?]
        _, $data:ident, $args:ident $(,)?
    } => { ... };
    {// #9
        @Parameters [$c:ident, $d:ident, $a:ident $(,)?]
        _, _, $args:ident $(,)?
    } => { ... };
    {// #10
        @Parameters [$c:ident, $d:ident, $a:ident $(,)?]
        _, $data:ident, _ $(,)?
    } => { ... };
    {// #11
        @Parameters [$c:ident, $d:ident, $a:ident $(,)?]
        $ctx:ident, _, _ $(,)?
    } => { ... };
    {// #12
        @Parameters [$c:ident, $d:ident, $a:ident $(,)?]
        _, _, _ $(,)?
    } => { ... };
    (// #13
        @Unwind [$ctx: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 FunctionImplementation, intended to be added to a FunctionSet.

This macro supports two forms: one that defines a function inline, and another that binds to an existing Function using the use keyword.

For larger or more complex implementations, the latter can provide better IDE support. However, it introduces two distinct items with the same semantics, so naming should be chosen carefully to avoid confusion.

§Panic Handling

Any panic occurring within the function body is intercepted via std::panic::catch_unwind. Instead of unwinding across the FFI boundary, an as3::Error 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 as3::Error is returned, casting it to a non-error type yields null and may lead to runtime exceptions.

When the as3::Error 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 (ctx, data, args) -> as3::Object {
            return ctx.get_actionscript_data();
        }
    }
    fre_rs::function! (method_name2 use method_implementation);
    fn method_implementation <'a> (ctx: &CurrentContext<'a>, data: Option<&mut dyn Any>, args: &[as3::Object<'a>]) -> as3::Object<'a> {as3::null}
}

§Minimal Example

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