numbl can be used as an npm library in both Node.js and browser applications.
npm install numbl
import { executeCode } from "numbl";
const result = executeCode("disp(2 + 3)");
console.log(result.output); // ["5\n"]
executeCode(source, options?, workspaceFiles?, mainFileName?)Parses and executes .m source code. Returns an ExecResult:
output -- array of printed linesreturnValue -- the final ans valuevariableValues -- all workspace variables after executionplotInstructions -- graphics commands (for rendering plots)Pass an ExecOptions object to control execution:
const result = executeCode(code, {
onOutput: text => console.log(text), // streaming output callback
displayResults: true, // show expression results
optimization: 1, // 0=interpreter only, 1=JIT (default)
});
Define custom .m functions by passing them as workspace files:
const result = executeCode("y = myfunc(3);", {}, [
{ name: "myfunc.m", source: "function r = myfunc(x)\nr = x^2;\nend" },
]);
console.log(result.variableValues.y); // 9
Errors throw a RuntimeError with file and line information:
import { executeCode, RuntimeError } from "numbl";
try {
executeCode("error('something went wrong')");
} catch (e) {
if (e instanceof RuntimeError) {
console.error(`${e.file}:${e.line}: ${e.message}`);
}
}