C API and Native Extensions
How to write a native C module, embed the Solan interpreter in a C program, use SolanValue, and understand FFI limits and permissions.
Native C module
Building and binding a C module
A native module defines C callbacks with the shared function signature and exports solan_module_init(SolanModule *m). Register functions with solan_module_add_function and values with solan_module_add_value.
#include "solan.h"
static int add(int argc, const SolanValue *argv, SolanValue *res, void *user) {
(void)user; (void)argc;
if (!solan_is_number(argv[0]) || !solan_is_number(argv[1]))
return solan_throw("جمع: يتطلب رقمين");
*res = solan_number(solan_to_number(argv[0]) + solan_to_number(argv[1]));
return 0;
}
int solan_module_init(SolanModule *m) {
solan_module_add_function(m, "جمع", add, 2, 2, NULL);
solan_module_add_value(m, "الاصدار", solan_number(1));
return 0;
}- Build a shared object with
-fPIC -sharedand-I include; the README example outputshello.so. - Import the module from Solan using a relative path; the module name is derived from the filename without extension.
- The executable exports
solan_*symbols with the appropriate dynamic-linker flag; README says not to link the extension againstlibsolanor the executable.
Values and API
Values and public API
| Group | API names documented in README |
|---|---|
| Value construction | solan_nil, solan_bool, solan_number, solan_string, solan_list_new, solan_map_new. |
| Checks and conversion | solan_is_*, solan_to_bool, solan_to_number, solan_to_cstr. |
| Lists and maps | solan_list_len/get/push, solan_map_set/get. |
| Errors and GC roots | solan_throw, solan_pin, solan_unpin to keep values rooted from garbage collection. |
| Embedding | solan_init, solan_register_function, solan_eval, solan_call, solan_last_error, solan_eval_file, solan_abi_version. |
This is a concise name index, not a substitute for type definitions and signatures in include/solan.h. Check the header for arguments, return types, and value lifetimes.
Embedding
Embedding Solan in a C program
Build as a shared library with -DSOLAN_LIBRARY, then call solan_init() from C, optionally register a native function, pass source to solan_eval, or invoke a Solan function with solan_call. README examples use solan_last_error() to read failures.
cc -O2 -fPIC -shared -DSOLAN_LIBRARY -Wno-format-truncation -o libsolan.so solan.c -lm -ldl
cc -I include -o demo demo.c -L. -lsolan -Wl,-rpath,.Security and platform
FFI, constraints, and permissions
- Direct
ffibinding associates a C symbol with a signature such asd(d); README lists type codesi,l,p,d,s,b, andv. - FFI supports x86-64 and AArch64, up to 6 integer and 8 floating-point arguments; no float, varargs, or callbacks.
- Direct FFI and native-module loading require the
اصليpermission, which is inherently unsafe. Apply runtime permission policy before executing third-party native code.
Examples are quoted or summarized from README and were not compiled or run in this task. ABI behavior varies by platform/compiler; do not infer ownership or binary compatibility from this page alone.
Source files
Direct links pinned to the source snapshot used by this reference; the main branch may change later.