rtea
rtea (Rust Tcl Extension Architecture) is a library for writing Tcl extensions
in Rust. It wraps the Tcl stubs interface so that extensions can be written
without unsafe code or direct knowledge of Tcl's C API.
Setup
Your crate must be a cdylib:
[]
= ["cdylib"]
[]
= "0.3"
Writing an Extension
Initialization
Use the #[module_init] macro to define the entry point Tcl calls when loading
your extension. The macro generates the correctly named and typed extern "C"
symbol, validates the interpreter, and registers the package version.
use *;
The module name passed to the macro must match Tcl's naming conventions: first
letter capitalised, all others lowercase (e.g. Example for a library named
libexample). The generated symbol is Example_Init.
If the extension should also be loadable into a safe interpreter, provide a
second entry point with #[module_safe_init], which generates Example_SafeInit.
Unloading
#[module_safe_unload] generates the corresponding Example_SafeUnload symbol.
Commands
Stateless commands
Commands that do not need to carry state take the form:
interp.create_command?;
Return Err(message) to signal a Tcl error; the message becomes the
interpreter's error result automatically.
Stateful commands
StatefulCommand associates owned data with a command. The data is dropped
automatically when the command is deleted from the interpreter.
use RefCell;
new
.attach_command?;
Object commands
Commands that work with typed Tcl objects use create_obj_command:
interp.create_obj_command?;
Custom Object Types
The TclObjectType derive macro registers a Rust struct as a native Tcl object
type, allowing values to be passed between Tcl and Rust without repeated string
parsing.
use *;
After calling interp.register_obj_type::<Point>(), the type is available to
Tcl. Use Point::from_object(&obj) to borrow the inner value and
point.into_object() (or the From impl) to wrap one in an Object.
Evaluating Tcl
let result = interp.eval?;
println!; // "2"
eval returns Ok(Object) on success and Err(Object) on error, where the
object holds the interpreter's result or error message respectively.
Maintenance
stubParser.tcl regenerates the Tcl stubs vtable in src/interpreter.rs from
a Tcl source tree. Run it whenever updating the Tcl version:
tclsh stubParser.tcl /path/to/tcl/generic/tcl.decls