plugin
Loading Brass code built separately as a shared library (a plugin) and calling its exported functions.
A plugin is a Brass file whose entry points are marked export fn, built with brass build --plugin file.brass -o file.so. The host program loads it with plugin.load(&files, &path), which needs a file permission like any file read (a plugin’s code runs with the program’s full trust, so where it may come from is limited the same way) and calls an export with lib.call("name", args...). call is checked at run time: every export carries a fingerprint of its signature and of every type reachable from it, so a wrong name, wrong argument or result types, a plugin built by another compiler, or a missing file is a PluginError, never a crash.
The plugin side. An export cannot be generic or declare errors, and a plugin file has no top-level statements:
export fn version() -> Str { return "1.2"}
export fn scale(x: i64, factor: i64) -> i64 { return x * factor}Build it:
brass build --plugin tools.brass -o tools.soThe host side. call has no fixed result type; it takes the type the caller expects, so give the variable a type:
import fsimport plugin
let lib = try plugin.load(&fs.files(), &"./tools.so")let v: Str = try lib.call("version")let n: i64 = try lib.call("scale", 6, 7)print(f"{v} {n}")Handle a plugin that is missing or does not match:
import fsimport plugin
let lib = plugin.load(&fs.files(), &"./tools.so") catch { plugin.PluginError(e) => { print(f"cannot load {e.path}: {e.message}") return }}let n: i64 = lib.call("scale", 6, 7) catch { plugin.PluginError(e) => { print(e.message) 0 }}print(n)For anything richer than a few functions, share a trait. Put the trait (and its error types) in a module that both the host and the plugin import, have the plugin export a function that returns Box<dyn Trait>, and call the trait’s methods from the host. Trait methods may declare errors even though exports cannot:
because in tool.brass, imported by both sidespub trait Tool { fn run(&mut self, input: &Str) -> Str ! ToolError}
because in the pluginexport fn create() -> Box<dyn tool.Tool> { return Box.new(WordCount { calls: 0 })}
because in the hostlet mut t: Box<dyn tool.Tool> = try lib.call("create")print(t.run(&"hello brass plugins") catch "?")Values move freely between host and plugin: both use the same allocator and panic state, and error types match by module and type name.
Reference
Section titled “Reference”import pluginFunctions
Section titled “Functions”pub fn load(files: &fs.Files, path: &Str) -> Library ! PluginErrorLoads a plugin built with brass build --plugin; its code runs with the program’s full trust.
Loading runs the plugin’s native code with the full rights of the program; there is no sandbox. Load only plugins you would run as programs. The path is passed to the system loader as is, so use ./name.so (or an absolute path) for a file in the current folder; a bare name.so may be searched for in the system library folders instead.
Plugins are never unloaded, because values they handed out point into their code. Loading the same path again gives the already loaded code; it does not pick up a rebuilt file.
PluginError
Section titled “PluginError”pub struct PluginError { pub path: Str pub message: Str}path is the path given to load. message says what went wrong, for example “the plugin has no export fn nope” or “version has a different type in the plugin”.
Library
Section titled “Library”pub struct Library {}A loaded plugin stays loaded until the program ends, since its code backs values it handed out.
It also has 2 private field(s), so outside this module it is made only by this module’s functions, which check what goes in.