Skip to content

Embedding Guide

Derek Snider edited this page May 19, 2026 · 1 revision

Embedding Guide

madc can be embedded in your C/C++ applications via libmadc — a shared library that exposes madc's compiler and runtime as a C++ API.

Quick example

#include <libmadc/api.h>

int main() {
    madc::program pgm;
    pgm.exec_file("script.mad");
    return 0;
}

API surface

madc::program

The main entry point. A pimpl wrapper over the internal Program class.

madc::program pgm;

// Compile and run a file
pgm.exec_file("script.mad");

// Compile and run a string
pgm.exec_string("puts(\"hello\");");

// Compile a file without running
pgm.compile_file("script.mad");

// Evaluate an expression
madc::value result;
pgm.eval("2 + 2", result);

Calling script functions from C++

madc::value result;
pgm.call("my_function", {42, 3.14, "hello"}, result);

call() supports up to four arguments on the scalar/C-string subset (void, bool, int64, double, const char *, std::string).

Global variable access

madc::value val;
pgm.get_global("my_var", val);
pgm.set_global("my_var", 42);

Registering host functions

pgm.register_function("my_callback", my_native_func, signature);

Register C++ functions that scripts can call.

Convenience wrappers

For simple use cases that don't need a persistent program state:

madc::exec_file("script.mad");
madc::exec_string("puts(\"hello\");");

madc::value result;
madc::eval("2 + 2", result);

These create a temporary madc::program internally.

Security and limits

madc::program pgm;

// Lock down capabilities
pgm.set_security_policy(madc::authority_mode::system_locked);

// Set execution limits
madc::invoke_limits limits;
limits.cpu_time_ms = 5000;
limits.memory_bytes = 64 * 1024 * 1024;
pgm.set_invoke_limits(limits);

system_locked mode disables process builtins, dynamic loading, and other sensitive operations.

Building with libmadc

# Install libmadc
sudo make -C src install-libmadc

# Compile your program
g++ -std=c++11 myapp.cpp -lmadc -L/usr/local/lib -I/usr/local/include -o myapp

# Run
LD_LIBRARY_PATH=/usr/local/lib ./myapp

What's next?

Clone this wiki locally