Embedded Atom application
Save as embed.c. This host registers an Atom builtin, compiles and runs a short module, roots its exported function and calls it. An embedding application that stores an encoded program can substitute atom_decode for atom_compile and execute it through the same atom_run call.
Load the exported Atom function
The host registers a C function, runs the script, and looks up its answer export. It roots that function so it can still call it after freeing the program handle.
AtomStatus status = atom_register(vm, "host_double", host_double, NULL);
if (status == ATOM_OK) status = atom_compile(vm, &source, &program);
if (status == ATOM_OK) status = atom_run(vm, program, &exports);
if (status == ATOM_OK) status = atom_module_get(vm, exports, "answer", &function);
if (status == ATOM_OK) {
callback = atom_root_new(function);
if (!callback) status = ATOM_NO_MEMORY;
}
Show full source 70 lines
#include <atom.h>
#include <inttypes.h>
#include <stdio.h>
static bool console(void *context, const char *bytes, size_t length)
{
return fwrite(bytes, 1, length, context) == length;
}
static AtomStatus host_double(AtomVM *vm, void *context, size_t argc,
const AtomValue *argv, AtomValue *out)
{
(void)context;
if (argc != 1 || argv[0].type != ATOM_INT)
return atom_error(vm, "host_double expects a signed integer");
int64_t input = argv[0].as.integer;
if (input < INT64_MIN / 2 || input > INT64_MAX / 2)
return atom_error(vm, "host_double overflow");
*out = atom_int(input * 2);
return ATOM_OK;
}
int main(void)
{
const char *modules[] = {"math"};
AtomConfig config = {
.write = console,
.write_context = stdout,
.modules = modules,
.module_count = 1,
.instruction_limit = 100000,
.heap_limit = 8 * 1024 * 1024,
};
AtomVM *vm = atom_vm_new(&config);
if (!vm) return 1;
const char script[] =
"use math\n"
"export func answer(x):\n"
" print(math.abs(x))\n"
" return host_double(x)\n";
AtomSource source = {"embedded.atom", script, sizeof(script) - 1};
AtomProgram *program = NULL;
AtomRoot *callback = NULL;
AtomValue exports, function;
AtomStatus status = atom_register(vm, "host_double", host_double, NULL);
if (status == ATOM_OK) status = atom_compile(vm, &source, &program);
if (status == ATOM_OK) status = atom_run(vm, program, &exports);
if (status == ATOM_OK) status = atom_module_get(vm, exports, "answer", &function);
if (status == ATOM_OK) {
callback = atom_root_new(function);
if (!callback) status = ATOM_NO_MEMORY;
}
atom_program_free(program);
AtomValue input = atom_int(21), result;
if (status == ATOM_OK)
status = atom_call(vm, atom_root_get(callback), 1, &input, &result);
if (status == ATOM_OK && result.type == ATOM_INT) {
printf("Answer: %" PRId64 "\n", result.as.integer);
} else {
if (status == ATOM_OK) status = atom_error(vm, "answer must return a signed integer");
const AtomDiagnostic *error = atom_diagnostic(vm);
fprintf(stderr, "%s\n", error && error->message ? error->message : "Atom failed");
if (error && error->trace) fputs(error->trace, stderr);
}
if (callback) atom_root_free(callback);
atom_vm_free(vm);
return status == ATOM_OK ? 0 : 1;
}
Save as CMakeLists.txt next to embed.c. Configure with cmake -S . -B build -DCMAKE_PREFIX_PATH=/path/to/atom-sdk, then cmake --build build. Use Atom::Shared in target_link_libraries to build against the shared SDK.
cmake_minimum_required(VERSION 3.25)
project(EmbeddedAtom LANGUAGES C)
find_package(Atom CONFIG REQUIRED)
add_executable(embed embed.c)
set_target_properties(embed PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
target_link_libraries(embed PRIVATE Atom::Static)