module_safe_init

Attribute Macro module_safe_init 

Source
#[module_safe_init]
Expand description

Helper for creating the “safe” initialization function for Tcl extensions.

This macro will automatically create the appropriate wrapper to validate the interpreter and “provide” the package to the interpreter. The prototype of the wrapped function should be

type init_fn = fn(interp: &rtea::Interpreter) -> Result<rtea::TclStatus, String>;

and one or two attributes should be passed to the macro. The first must be the module’s name with a capital first letter and all others lowercase (this is a Tcl requirement). The second, optional attribute, is the version which by Tcl convention should be in accordance with semver.

§Example

#[module_safe_init(Example, "1.0.0")]
fn init(interp: &Interpreter) -> Result<TclStatus, String> {
    interp.eval("Initializing module...")
}

The above example will create a function named Example_SafeInit (with the no_mangle attribute) which Tcl will use as the initialization routine. This assumes that your files final library name matches the expectation of -lexample for the C linker (which is the case if used in a “cdylib” crate named “example”).

§Warning

This initialization routine is intended to be safe to use from untrusted code. Users must take care that the functionality they expose to Tcl scripts from here is truly “safe” (in the destroy a system sense, not Rust’s crash a program sense). It is highly recommended you read about Safe Tcl before using this macro.