Skip to content

Executes code in a (unsafe) sandboxed environment, instruction by instruction, with configurable speed.

Notifications You must be signed in to change notification settings

academia-de-codigo/step-interpreter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

step-interpreter

Javascript interpreter that is able to run code with configurable speed.

TODO README

Example usage:

const Interpreter = require('../src/interpreter');

const code = `
    const a = 1;

    for (let i = 0; i < 5; i++) {
        console.log(sum(a, i));
    }
    
    this.printer = function(string) {
        console.log(string);
    };

    function sum(a, b) {
        return a + b;
    }

    console.log('done inside interpreter');
`;

async function run() {
    const interpreter = new Interpreter();
    const unsubscribe = interpreter.addStepper(code => console.log('going to run....', code));

    interpreter.expose({
        console,
    });

    setTimeout(() => {
        console.log('pausing interpreter...');
        interpreter.pause();
    }, 1500);

    setTimeout(() => {
        console.log('unsubscribing stepper...');
        unsubscribe();
        console.log('resuming interpreter...');
        interpreter.resume();
        console.log('setting step interval to 10');
        interpreter.setStepInterval(10);
    }, 3000);

    await interpreter.run(code);
    interpreter.read('printer')('OUTSIDE INTERPRETER: DONE!');
}

run();

About

Executes code in a (unsafe) sandboxed environment, instruction by instruction, with configurable speed.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published