script-eval-js
v1.0.7
Published
eval with context, execute script block, pass parameters, global function variables
Downloads
5
Maintainers
Readme
Script Runner
Make your eval pass parameters and specify this context when executing the script, and you can register global functions and variables for him.
License: MIT
install
npm install script-eval-js
import { ScriptRunner } from 'script-eval-js';
examples
const result = ScriptRunner.eval(`return 1+1;`);
console.log(result);
const result = ScriptRunner.eval(`console.log(this);`);
console.log(result);
const result = ScriptRunner.eval(`console.log(id+name);`, { id: 100, name: 'script runner' });
console.log(result);
const func = ScriptRunner.buildFunction(`console.log(this); return id + name;`, ['id','name'],this);
const result = func(100,'script runner');
console.log(result);
typescript typing
/**
* global context
*/
export declare class ScriptEvalContext {
constructor(baseContext?: ScriptEvalContext, initLib?: boolean);
clear(): void;
register(name: string, object: any): void;
remove(name: string): void;
get(name: string): any;
get names(): string[];
get values(): any[];
}
export declare class ScriptRunner {
static buildFunction(script: string, argNames?: string[], thisContext?: Object, globalContext?: ScriptEvalContext): Function;
static eval<TResult>(script: string, parameters: Record<string, any>, thisContext?: Object, globalContext?: ScriptEvalContext): TResult;
}