Skip to content

Instantly share code, notes, and snippets.

@caridy
Last active February 12, 2020 07:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save caridy/f623d0dc6f8d71578f5a533cad99bcd0 to your computer and use it in GitHub Desktop.
Save caridy/f623d0dc6f8d71578f5a533cad99bcd0 to your computer and use it in GitHub Desktop.
evaluator api proposal
interface EvaluatorInit {
importHook(name, referrer): Promise<ParsedModule>;
isDirectEvalHook(evalFunctionRef: any): boolean;
randomHook(): number; // Use for Math.random()
nowHook(): number; // Use for both Date.now() and new Date(),
localeHook(): string; // e.g.: 'fr-FR' - Affects appropriate ECMA-402 APIs within Realm
LocalTZAHook(): string; // This is important to be able to override for deterministic testing and such
canCompileStringsHook(source: String): boolean; // mimic CSP including nounces
thisValue?: object;
}
interface EvaluateOptions {
// the type of source to be evaluated, "program" or "expression"
type: string;
// object or proxy used to resolve bindings on the global execution context
globalContour?: object;
}
interface Evaluator {
intrinsics(): Record<string, IntrinsicObject>; // current realm's intrinsics + those redefined by the evaluator (Function, eval, Realm, etc)
evaluate(sourceText: string, options: EvaluateOptions): CompletionRecord;
import(specifier: string): Promise<ModuleNamespace>;
}
declare var Evaluator: {
prototype: Evaluator;
new(options?: EvaluatorInit): Evaluator;
parseModule(sourceText: String): ParsedModule;
};
// inspired by https://gist.github.com/dherman/1555894a09ca188d4d3ca0c96fb44311
interface ParsedModule {
link(specifier: string, parsedModule: ParsedModule);
requestedNames(): string[];
}
// Other potential hooks (dialect explained here: https://gist.github.com/dherman/9146568)
// - transformSourceHook(source: string, type: Script | Module | ...): string;
// - importMetaHook(parsedModule: ParsedModule): ImportMetaObject;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment