lmcode-cpp host authoring skill
Purpose
Guide the creation of a small C++ host that exposes a typed schema to lmscript and loads and runs bytecode with lmcode-cpp.
When to use it
Use this when implementing a native application that should expose a controlled set of host objects, methods and functions to DSL scripts.
Install
Include dslvm.hpp from the SDK and compile as C++17. The standard catalog lmcode_std_v3 (226 signatures, including the REST and CLI interfaces RestGet, RestPost, RestPut, RestDelete, RestPatch, RestHead, RestOptions, RestHandler, CliCommand and CliOption with rest_config, rest_route, rest_serve, rest_stop, rest_cli and the RestExchange methods) is included by each Registry; add callbacks only for host-specific functions and methods. Use a compiler that emits the SDK's current bytecode version.
Commands
g++ -std=c++17 -Iinclude host.cpp -o host
lmscript --lmcode-signatures schema.json
./host --lmcode-signatures
./host --lmcode scene.dslb
Inputs and outputs
- Input: binary bytecode and the exact JSON schema bytes used to compile it.
- Host setup: registry callbacks plus context pointers to host-owned objects.
- Output: host-defined effects and, for command-line programs, one standard JSON result line.
Safety rules
- Treat bytecode as trusted: the VM does not impose runtime limits or dynamic call checks.
- Pass exact schema bytes and bind each host-specific context, function and method; standard functions are included by the Registry.
- Keep objects and references returned by methods alive for all subsequent script calls.
- Check signature parity during development. Matching arity and return type are part of the schema contract.
- Expose collections as host object types with len() and at(i); DSL list/map value kinds do not exist.
Worked examples
- Create one Registry and register a context object type.
- Bind each method callback with argument and return type strings that exactly match the schema.
- Load bytecode and schema bytes with Program::load.
- Create host objects before Context::set; keep owners in scope through Program::run.
- For a REST or CLI program, let annotated rule entries implement the standard interfaces, call rest_route and rest_serve in the body, or dispatch an argument list with rest_cli; see the reference page for RestExchange.
- Use standard functions without host registration; bind only host-specific functions and methods.
- For collections, return typed object values and implement the indexed access behavior in the host.
Errors and exit codes
Catch dslvm::Error around load and run. Standard CLI conventions use exit 2 for compilation/argument errors, 3 for bytecode load or run errors, and 4 for application failures. Include the error code and source location when present.
How to verify
- Compile a tiny DSL chunk against the host schema.
- Diff the compiler's canonical signatures against Registry::signatures().
- Test a valid run and representative load/runtime failures.
- Confirm the host passes the exact schema bytes used by the compiler.
- Test object ownership and collection access over the complete script run.
lmcode-cpp is the C++ member of a five-language SDK family with Rust, Go, C#, and Java siblings.