Sablejs
đī¸ The safer and faster ECMA5.1 interpreter written by JavaScript
Install / Use
/learn @sablejs/SablejsREADME

<a href="https://www.npmjs.com/package/sablejs"><img src="https://img.shields.io/npm/v/sablejs.svg?sanitize=true" alt="Version"></a>
đ sablejs 2.0 will be opening all the code, please click to learn more about our milestone and goals.
English | įŽäŊ䏿
The safer and faster ECMA5.1 interpreter written by JavaScript, it can be used:
- Sandbox (like Figma Plugin Sandbox, but better and easier to use);
- Protect JavaScript source code via AOT compiling to opcode.
sablejs covered ~95% test262 es5-tests cases, it can be safely used in production.
Quick Start
sablejs includes the Compiler and Interpreter independently, so we removed the related dynamic api from the spec (see Limits 1). In short, you need to compile your JavaScript code with sablejs cli before you run it.
Example
Suppose we have the following code in fib.js:
function fib(n) {
return n < 2 ? n : fib(n - 1) + fib(n - 2);
}
var start = Date.now();
console.log("[INFO] fib: " + fib(30));
console.log("[INFO] time consuming: " + (Date.now() - start) + "ms");
Compile It!
> npm i sablejs -g
> sablejs -i fib.js -o output # get output file that contains base64 string
sablejs cli includes the following commands:
Usage: sablejs [options]
Options:
-v, --vers output the current version
-i, --input <path> compile input filepath
-o, --output <path> compile output filepath
-j --json don't do Base64 compress, output simple json result
-s, --slient don't output log
-h, --help
Run It!
> npm install sablejs --save
or you can import to your html directly
<script src="https://cdn.jsdelivr.net/npm/sablejs@1.0.8/runtime.js"></script>
Browser
const VM = require("sablejs/runtime")();
// import console.log function to vm call
const vm = new VM();
const vGlobal = vm.getGlobal();
const vConsole = vm.createObject();
const vLog = vm.createFunction("log", function () {
const temp = [];
for (let i = 0; i < arguments.length; i++) {
temp.push(vm.asString(arguments[i]));
}
console.log(...temp);
return vm.createUndefined();
});
vm.setProperty(vConsole, "log", vLog);
vm.setProperty(vGlobal, "console", vConsole);
(async () => {
const resp = await fetch("<output url>");
const data = await resp.text();
vm.run(data);
vm.destroy();
})();
Node
const VM = require("sablejs/runtime")();
const fs = require("fs");
// import console.log function to vm call
const vm = new VM();
const vGlobal = vm.getGlobal();
const vConsole = vm.createObject();
const vLog = vm.createFunction("log", function () {
const temp = [];
for (let i = 0; i < arguments.length; i++) {
temp.push(vm.asString(arguments[i]));
}
console.log(...temp);
return vm.createUndefined();
});
vm.setProperty(vConsole, "log", vLog);
vm.setProperty(vGlobal, "console", vConsole);
// please run: sablejs -i fib.js -o output
vm.run(fs.readFileSync("./output").toString());
vm.destroy();
APIs
- VM.prototype.run(source, isSimpleJSON)
- source: String - the compiled result via sablejs compiler
- isSimpleJSON: Boolean - if be true, you should use
-jto make compiler output simple json result, default false. return:undefined
Initialize the VM and execute the compiled source code.
const VM = require('sablejs/runtime')();
const vm = new VM();
// source should be base64 string via sablejs compiling
vm.run(`<compiled source string>`);
- VM.prototype.getGlobal()
return:Value
Returns the global in the VM, which is similar to the window in browser and the global in Node.js.
const global = vm.getGlobal();
- VM.prototype.createUndefined()
returnValue
Create an undefined boxed type.
const vUndefined = vm.createUndefined();
- VM.prototype.createNull()
return:Value
Create an null boxed type.
const vNull = vm.createNull();
- VM.prototype.createBoolean(bool)
- bool: Boolean
returnValue
Create an bool boxed type.
const vBoolean = vm.createBoolean(true);
- VM.prototype.createNumber(num)
- num: Number
returnValue
Create an number boxed type.
const vNumber = vm.createNumber(1024);
- VM.prototype.createString(str)
- str: String
returnValue
Create an string boxed type.
const vString = vm.createString('Hello World!');
- VM.prototype.createObject()
returnValue
Create an object boxed type.
const vObject = vm.createObject();
- VM.prototype.createArray(length)
- length: Number | undefined
returnValue
Create an array boxed type.
const vArray1 = vm.createArray();
// or
const vArray2 = vm.createArray(128);
- VM.prototype.createFunction(name, func)
- name: String
- func: Function
returnValue
Create an funcntion boxed type. It receives a function name and the specific implementation of the function. Both the function parameter and this are boxed types in func.
const vFunction = vm.createFunction("trim", function(str) {
// this is the undefined or new's instannce boxed type
// str maybe the string boxed type, we need to check it
});
- VM.prototype.createError(message)
- message: String | undefined
returnValue
Create an error boxed type.
const vError1 = vm.createError();
// or
const vError2 = vm.createError("unknown error");
- VM.prototype.createRegExp(pattern, flags)
- pattern: String
- flags: String | undefined
returnValue
Create an regexp boxed type.
const vRegExp = vm.createRegExp("\\w+", "ig");
- VM.prototype.createDate()
returnValue
Create an date boxed type.
const vDate = vm.createDate();
- VM.prototype.isUndefined(value)
- value: Value
returnBoolean
Used to determine if the type is undefinend.
const vUndefined = vm.createUndefined();
if(vm.isUndefined(vUndefined)) {
// ...
}
- VM.prototype.isNull(value)
- value: Value
returnBoolean
Used to determine if the type is null.
const vNull = vm.createNull();
if(vm.isNull(vNull)) {
// ...
}
- VM.prototype.isBoolean(value)
- value: Value
returnBoolean
Used to determine if the type is bool.
const vBoolean = vm.createBoolean(true);
if(vm.isBoolean(vBoolean)) {
// ...
}
- VM.prototype.isNumber(value)
- value: Value
returnBoolean
Used to determine if the type is number.
const vNumber = vm.createNumber(1024);
if(vm.isNumber(vNumber)) {
// ...
}
- VM.prototype.isString(value)
- value: Value
returnBoolean
Used to determine if the type is string.
const vString = vm.createString("Hello World!");
if(vm.isString(vString)) {
// ...
}
- VM.prototype.isObject(value)
- value: Value
returnBoolean
Used to determine if the type is object.
const vObject = vm.createObject();
const vArray = vm.createArray();
if(vm.isObject(vObject) && vm.isObject(vArray)) {
// ...
}
- VM.prototype.isArray(value)
- value: Value
returnBoolean
Used to determine if the type is array.
const vArray = vm.createArray();
if(vm.isArray(vArray)) {
// ...
}
- VM.prototype.isFunction(value)
- value: Value
returnBoolean
Used to determine if the type is function.
const vFunction = vm.createFunction("log", function(){});
if(vm.isFunction(vFunction)){
// ...
}
- VM.prototype.isError(value)
- value: Value
returnBoolean
Used to determine if the type is error.
const vError = vm.createError('unknown error');
if(vm.isError(vError)){
// ...
}
- VM.prototype.isRegExp(value)
- value: Value
returnBoolean
Used to determine if the type is regexp.
const vRegExp = vm.createRegExp("\\w+", "ig");
if(vm.isRegExp(vRegExp)){
// ...
}
- VM.prototype.isDate(value)
- value: Value
returnBoolean
Used to determine if the type is date.
const vDate = vm.createDate();
if(vm.isDate(vDate)){
// ...
}
- VM.prototype.asUndefined(value)
- value: Value
returnundefined
Converting undefined boxed type to plain undefined value.
const vUndefined = vm.createUndefined();
vm.asUndefined(vUndefined) === undefined;
- VM.prototype.asNull(value)
- value: Value
returnnull
Converting null boxed type to plain null value.
const vNull = vm.createNull();
vm.asNull(vNull) === null;
- VM.prototype.asBoolean(value)
- value: Value
returnBoolean
Converting bool boxed type to plain bool value.
const vBoolean = vm.createBoolean(true);
const boolean = vm.asBoolean(vBoolean);
if(boolean === true) {
// ...
}
- VM.prototype.asNumber(value)
- value: Value
returnNumber
Converting number boxed type to plain number value.
const vNumber = vm.createNumber(1024);
const number = vm.asNumber(vNumber);
if(number === 102
Related Skills
node-connect
336.2kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
82.8kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
336.2kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
commit-push-pr
82.8kCommit, push, and open a PR
