scraggy
Version:
A safe JavaScript expression evaluator
96 lines (89 loc) • 3.21 kB
TypeScript
import * as acorn from 'acorn';
declare class Scope {
private variables;
private parent;
private children;
private refCount;
private functions;
constructor(parentScope?: Scope | null, initial?: Record<string, any>);
getParent(): Scope | null;
defineFromObject(scope: Record<string, any>): void;
trackFunction(func: RuntimeFunction): void;
untrackFunction(func: RuntimeFunction): void;
addRef(): void;
release(): void;
private addChild;
private removeChild;
private cleanup;
define(name: string, value: any): void;
assign(name: string, value: any): boolean;
lookup(name: string): any;
getVariables(): Map<string, any>;
}
declare class ReturnValue extends Error {
value: any;
constructor(value: any);
}
declare class BreakValue extends Error {
label?: string | undefined;
constructor(label?: string | undefined);
}
declare class ContinueValue extends Error {
label?: string | undefined;
constructor(label?: string | undefined);
}
type FunctionParameter = {
name: string;
isRest: boolean;
isDestructuring: boolean;
destructuringPattern?: acorn.Pattern;
};
declare class RuntimeFunction {
private params;
private body;
private scope;
private evaluator;
private isAsync;
private ownerScope;
private destroyed;
constructor(params: FunctionParameter[], body: acorn.BlockStatement | acorn.Expression, scope: Scope, evaluator: (node: acorn.Expression | acorn.Statement, scope: Scope) => Promise<any>, isAsync?: boolean);
destroy(): void;
/**
* Process a destructuring pattern for function parameters
* @param pattern The destructuring pattern (ObjectPattern or ArrayPattern)
* @param value The value to destructure
* @param scope The scope to define variables in
*/
private processDestructuringParameter;
call(thisArg: any, args: any[]): Promise<any>;
}
/**
* Gets the associated RuntimeFunction for a function
*/
declare function getRuntimeFunction(func: Function): RuntimeFunction | undefined;
type GlobalObject = Record<string, any>;
/**
* Evaluates JavaScript code without using the built-in eval function
* @param globalObj The global object context to use when evaluating
* @param script The JavaScript code to evaluate
* @returns The result of evaluating the script
*/
declare function evaluate<T>(globalObj: GlobalObject, script: string): Promise<T>;
declare function evaluateAST(globalObj: GlobalObject, ast: acorn.Program, originalScript?: string): Promise<any>;
declare class MemoryTracker {
private static instance;
private activeScopes;
private activeFunctions;
private constructor();
static getInstance(): MemoryTracker;
trackScope(scope: Scope): void;
untrackScope(scope: Scope): void;
trackFunction(func: RuntimeFunction): void;
untrackFunction(func: RuntimeFunction): void;
getStats(): {
activeScopes: number;
activeFunctions: number;
};
reset(): void;
}
export { BreakValue, ContinueValue, type FunctionParameter, type GlobalObject, MemoryTracker, ReturnValue, RuntimeFunction, Scope, evaluate, evaluateAST, getRuntimeFunction };