Skip to content

Latest commit

 

History

History
120 lines (87 loc) · 1.83 KB

API.md

File metadata and controls

120 lines (87 loc) · 1.83 KB

back to README.md

API Reference

Module exports

{
    VM,
    Script,
}

new VM( context )

Create a virtual machine context.

Example usage

let vm = new VM({
    "say": "Hello world",
});

<VM>.context( ctx )

Get or extend the context variables.

Example usage

let vm = new VM({
    "say": "Hello world",
});

vm.context()
// { say: "Hello world" }

vm.context({ say: undefined, name: "Agent Smith" });
// { name: "Agent Smith" }

<VM>.run( code[, ...input_args] )

Run the given code in this VM's context.

  • When code is a function, input_args are passed to that function.

Example usage

let vm = new VM({
    square ( n ) {
        return n**2;
    }
});
vm.run(`square( 2 );`)

new Script( code, default_ctx )

Create a pre-compiled script.

Example usage

let script = new Script(`square( 2 )`);

Code input can be a function, string, or instance of vm.Script.

  • When code is a function, input_args are passed to that function.

Code as a function

let ctx = {
    square (n) {
        return n**2;
    },
};

let script = new Script( (num) => {
    return square( num );
});

let result = script.run( ctx, 2 );
// 4

Code as a string

let script = new Script(`square( 2 )`);

Code as instance of vm.Script

import vm from 'vm';

let vm_script = new vm.Script(`square( 2 )`);
let script = new Script( vm_script );

<Script>.script : vm.Script

Get the underlying instance of vm.Script.

<Script>.run( ctx[, ...input_args] )

Run this Script's code using the given context.

  • ctx - (required) can be an instance of VM or an object
    • default_ctx is applied, then ctx

Example usage

let script = new Script(`square( 2 )`);

script.run({
    square ( n ) {
        return n**2;
    }
});