Native libraries

native loads a C ABI dynamic library into the Radlang process. This is useful for platform APIs exposed through a small C, Objective-C, or Swift shim. Libraries remain loaded until the process exits.

NativeLib lib = native.open("bin/librad-image.dylib")
string result = lib.call("rad_image_generate", json.from(request))
Future<string> pending = lib.callAsync("rad_image_generate", json.from(request))
string image = sys.blockOn(pending)

call runs on the calling thread. callAsync copies its input and runs the native function on the runtime worker pool, then resolves Future<string> on the scheduler. Use callAsync for long operations in HTTP handlers. A library function may be called concurrently; it must protect shared state itself. For progress, expose a separate native function that returns current state and poll it with callAsync.

Each callable symbol has this C signature and a paired <symbol>_free function:

char *rad_image_generate(const char *json_input);
void rad_image_generate_free(char *result);

The input is a borrowed, null-terminated UTF-8 string valid for the duration of the call. The library returns an allocated, null-terminated UTF-8 string. Radlang copies the output, then calls the paired free function exactly once. The free symbol is required, even when the result uses malloc; implement it with free(result) in that case. JSON input and output are a convention rather than a runtime requirement. Binary data and embedded null bytes need encoding such as base64. A null result makes synchronous call throw an Error; for callAsync it resolves to the JSON string "null".

native.open reports load failures as catchable Errors. Symbols are resolved on first call, and a missing call or free symbol also throws before the worker job starts. The library and its symbols remain loaded for the process lifetime. Because calls run in the same process, a crash or out-of-memory failure in the library also affects the Radlang application.

Build the library separately and ship it beside the executable. native.open tries the given path first, then resolves a relative path beside the executable. For example, place bin/librad-image.dylib under the executable’s directory. On macOS, set dependent dylibs’ install names/rpaths relative to the library with @loader_path; on Linux, use $ORIGIN in the library’s rpath. The Radlang binary does not embed the library.

Binary calls

lib.callBytes(string symbol, byte[] input): byte[] is a synchronous, copying binary ABI. It supports embedded nulls and does not reinterpret bytes as JSON:

int transform(const uint8_t *input, int64_t length,
              uint8_t **output, int64_t *output_length);
void transform_free(uint8_t *output);

The input is borrowed only until return and must not be retained. Set both output fields on every return. Status0 returns copied bytes; any other status returns UTF-8 error bytes which become a catchable Error with that code. A nonnull output must be owned by the plugin and accepted by its paired free function, called once after copying or on result/allocation error. Empty output may be null. Negative or unaddressable lengths and nonempty null outputs throw. Plugin free functions must not throw; C++ plugins must catch all exceptions before returning through the ABI.

String and binary exports have different signatures. Use different symbol names; the runtime rejects a symbol already cached through another ABI. Existing string call/callAsync behavior remains unchanged. No asynchronous binary ABI is added.

math.f64From(float[]): byte[] and math.f64To(byte[]): float[] copy native-endian IEEE-754 64-bit storage without text formatting or narrowing. They preserve bits, including signed zero, and require whole 8-byte values when decoding. These buffers are native interop data, not an endian-independent file/network format. The optional rtorch package uses this path for owned tensors.