Expand description
code-native — Helper crate for writing Code language native modules in Rust.
This crate eliminates the boilerplate required to create native .so
modules for the Code language. It re-exports all C-ABI types, provides
safe value constructors and field-access helpers, and ships the
code_module! macro that generates the required #[no_mangle]
entry-points (code_module_abi_version + code_module_init).
§Quick Start
ⓘ
use code_native::*;
unsafe extern "C" fn handle_add(particle: CodeValue) -> CodeValue {
let a = read_field_number(&particle, "a");
let b = read_field_number(&particle, "b");
code_object(vec![
code_field("_class", code_string("AddResult")),
code_field("result", code_number(a + b)),
])
}
code_module! {
vars: [
"PI" => code_number(3.14159),
],
types: [
"Add" [("a", "Number"), ("b", "Number")],
],
handlers: [
"Add" => handle_add,
],
emissions: [],
}Compile with:
cargo build -p code-native
rustc --edition 2021 --crate-type cdylib \
--extern code_native=target/debug/libcode_native.rlib \
-o mymodule.so mymodule.rsMacros§
- code_
module - Declare an Code native module.
Structs§
- Code
Emission - Emission declaration exported by a native module.
- Code
Export Handler - Exported handler: class name + handler function.
- Code
Export Type - Exported type declaration.
- Code
Export Var - Exported variable: name + constant value.
- Code
Field - A key-value pair for object fields.
- Code
Module Desc - Top-level module descriptor returned by
code_module_init(). - Code
Type Field - Field descriptor for type declarations.
- Code
Value - A single Code value in C-ABI representation.
Constants§
- CODE_
ABI_ VERSION - Current ABI version. Native modules must return this from
code_module_abi_version. - CODE_
EMIT_ TARGET_ BASE - Emission target: dispatch to the linking module’s base handlers.
- CODE_
TAG_ ARRAY - CODE_
TAG_ BOOLEAN - CODE_
TAG_ NULL - CODE_
TAG_ NUMBER - CODE_
TAG_ OBJECT - CODE_
TAG_ STRING
Functions§
- code_
array - Create an Array value from a
Vecof elements. - code_
boolean - Create a Boolean value.
- code_
emit - Emit a particle to the host runtime.
- code_
emit_ exception - Build and emit an
Exceptionparticle. - code_
emit_ log - Build and emit a
Logparticle. - code_
field - Create a single object field. The name is leaked.
- code_
null - Create a Null value.
- code_
number - Create a Number value.
- code_
object - Create an Object value from a
Vecof fields. - code_
string - Create a String value. The string is leaked into a C-compatible pointer that lives for the remainder of the process — fine for return values and module descriptors.
- code_
string_ raw - Create a String value from a raw
*const c_charpointer. - leak_
str - Leak a Rust
&strinto a*const c_charthat lives forever. - read_
boolean - Read a boolean from an
CodeValue, returningfalseif the tag is wrong. - read_
field ⚠ - Look up a field by name inside an Object
CodeValue. - read_
field_ ⚠bool - Convenience: read a boolean field from an object by name.
Returns
falseif the field doesn’t exist or isn’t a boolean. - read_
field_ ⚠number - Convenience: read a number field from an object by name.
Returns
0.0if the field doesn’t exist or isn’t a number. - read_
field_ ⚠str - Convenience: read a string field from an object by name.
Returns
""if the field doesn’t exist or isn’t a string. - read_
number - Read a number from an
CodeValue, returning0.0if the tag is wrong. - read_
str ⚠ - Read a string from an
CodeValue, returning""if the tag is wrong or the pointer is null.
Type Aliases§
- Code
Emit Fn - Callback signature for native-module emission.
- Code
Native Handler Fn - Native handler signature:
fn(particle) -> CodeValue.