hindiscript-lang
Version:
HindiScript – A tiny Hindi-first toy language that runs .hs files. Created by Atikin Verse.
23 lines (20 loc) • 812 B
JavaScript
import vm from 'node:vm';
import { color } from './utils.js';
// Execute internal code in a restricted sandbox with a provided runtime.
export async function runInSandbox(code, { runtime, filename = 'stdin' } = {}) {
const context = vm.createContext(runtime.__globals, {
name: 'HindiScriptContext',
codeGeneration: { strings: true, wasm: false }
});
const script = new vm.Script(code, { filename, displayErrors: true });
try {
const result = script.runInContext(context, { timeout: 1000 });
// Support top-level async via returned Promise
if (result && typeof result.then === 'function') {
await result;
}
} catch (e) {
const msg = e && e.message ? e.message : String(e);
throw new Error(`${color.red('HindiScript error')}: ${msg}`);
}
}