npm Integration Guide
The autolang-compiler package bundles the Autolang VM as a WebAssembly module, so you can embed it in any JavaScript or TypeScript project โ browser or Node.js โ without any native build steps.
๐ฆ Installation
Install the package from npm. The WASM binary is bundled โ no native compilation required.
npm install autolang-compiler@latest๐ Basic Usage
Create a compiler instance with ACompiler.create(), run a script, and read the output. That's the core loop for most use cases.
import { ACompiler } from 'autolang-compiler';
const compiler = await ACompiler.create();
// compileAndRun will refresh automatically after run vm
// if you want to run multiple times with same vm instance,
// then you can use compile and run separately
await compiler.compileAndRun("main.atl", `
println("Hello from Autolang!")
println("1 + 1 = " + (1 + 1))
`);
console.log(compiler.getOutput());
// Hello from Autolang!
// 1 + 1 = 2How output works
print and println calls internally. Use compiler.getOutput() after execution to retrieve the full buffer. Call compiler.clearOutput() to reset it before the next run.โ๏ธ Compile and Run Separately
When you need to validate code before running it โ or run the same compiled bytecode multiple times โ split the two steps. This is especially useful for agent pipelines where you want to check for errors before actually executing.
import { ACompiler } from 'autolang-compiler';
const compiler = await ACompiler.create();
const code = `println("Running!")`;
// Compile only โ does not execute
const ok = compiler.compile("main.atl", code);
if (compiler.hasCompilerError()) {
console.error("Compilation failed!");
} else {
// Run once
compiler.run();
console.log(compiler.getOutput());
// Run again without recompiling
compiler.clearOutput();
compiler.run();
console.log(compiler.getOutput());
}
// Reset everything before the next compilation
compiler.refresh();๐ข Opcode Execution Limit
Autolang tracks how many VM instructions each run executes. You can set a hard cap to stop runaway scripts โ the default is 100,000 opcodes. This is one of the primary safety mechanisms for AI-generated code.
import { ACompiler } from 'autolang-compiler';
const compiler = await ACompiler.create();
// Custom limit
compiler.setLimitOpcodeCount(50_000);
// Or effectively disable it (INT32_MAX)
compiler.setLimitOpcodeCount(2_147_483_647);
await compiler.compileAndRun("main.atl", `
var i = 0
while (true) {
i = i + 1
if (i >= 10) break
}
println("i = " + i)
`);
console.log(compiler.getOutput()); // i = 10Keep a limit when running untrusted code
๐ฅ Handling Runtime Exceptions
Autolang distinguishes between compile-time errors (wrong types, undefined variables) and runtime exceptions (thrown with throw, or triggered by divide-by-zero etc.). Runtime exceptions don't crash the host โ they're captured and accessible from the outside.
import { ACompiler } from 'autolang-compiler';
const compiler = await ACompiler.create();
await compiler.compileAndRun("main.atl", `
throw Exception("Something went wrong!")
`);
if (compiler.hasException()) {
const err = compiler.getException();
// AException = { message: string }
console.error("Runtime exception:", err?.message);
// Runtime exception: Something went wrong!
}
// You can also throw from the host side:
// compiler.throwException("Triggered from JavaScript");Compile errors vs runtime exceptions
- Compile errors โ caught at compile time. Check with
hasCompilerError()or use thesetOnError()callback. - Runtime exceptions โ thrown during execution. Check with
hasException()/getException().
Need to expose your own functions to scripts? See Native Libraries.
๐ค AI Reference Guide
If you are building an AI agent or assistant that needs to generate Autolang code, you can feed our pre-packaged markdown guide into the LLM system prompt. This ensures the model writes syntactically valid code and adheres to compiler constraints.
Download or copy the prompt instructions at the AI Reference page.
