Documentation
¶
Overview ¶
Package compiler translates the annotated AST produced by package syntax into bytecode.Function templates.
The compiler consumes the scope annotations directly: every identifier already points at its Binding (or nil for a global), bindings know whether they are captured by inner functions and whether reads need a TDZ check, and each scope lists the hoisted function declarations to instantiate on entry. Un-captured bindings live in registers, captured ones in closure environments; module and script top-level bindings always live in the top-level environment so that exports are live and the register file stays small.
Index ¶
- func CompileEval(e *syntax.Eval) (*bytecode.Function, error)
- func CompileModule(m *syntax.Module) (*bytecode.Function, error)
- func CompileScript(s *syntax.Script) (*bytecode.Function, error)
- type Error
- type Hook
- func (Hook) CompileEval(name, src string, scope *bytecode.EvalScope, stop func() error) (*bytecode.Function, error)
- func (Hook) CompileFunction(name, params, body string, generator, async bool, stop func() error) (fn *bytecode.Function, err error)
- func (Hook) CompileScript(name, src string, stop func() error) (*bytecode.Function, error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CompileEval ¶
CompileEval compiles parsed eval code into the arrow function template the engine calls in the environment of the eval (the call site's Env for a direct eval, none for an indirect one) with its this value; the call returns the completion value. The global var and function declarations of sloppy eval code are listed in Extra.Globals (Eval set), for the engine's EvalDeclarationInstantiation before the call; the eval code itself creates those that go to a function's variable environment.
func CompileModule ¶
CompileModule compiles a parsed module into its top-level function template (Kind == KindModule). The module's bindings become the slots of the function's own environment; Function.Module maps export names to those slots and lists what the module imports. A module with top-level await compiles as an async function body (Function.Async): the call returns the promise of its evaluation.
func CompileScript ¶
CompileScript compiles a script into a KindScript function whose return value is the completion value of the script. The script's declarations are global (GlobalDeclarationInstantiation, run by the engine before the function): its var and function names are properties of the global object and its let, const and class names bindings of the realm's global declarative environment, both read and written by name. The script is sloppy mode code unless its directive prologue says "use strict".
Types ¶
type Hook ¶
type Hook struct{}
Hook compiles the source text the engine evaluates at run time: eval code, the functions of the Function constructors and Realm.EvalScript's scripts. It satisfies engine.Compiler (engine.SetCompiler), which the root package installs, so that the engine needs neither the parser nor the compiler. Its methods call stop, when not nil, before every 1024th statement of each statement list they parse, resolve and compile, the first included, and return the error it returns as is.
func (Hook) CompileEval ¶
func (Hook) CompileEval(name, src string, scope *bytecode.EvalScope, stop func() error) (*bytecode.Function, error)
CompileEval parses and compiles eval code for the direct eval call site scope describes, or an indirect eval when scope is nil.
func (Hook) CompileFunction ¶
func (Hook) CompileFunction(name, params, body string, generator, async bool, stop func() error) (fn *bytecode.Function, err error)
CompileFunction compiles the function CreateDynamicFunction (ES2025 20.2.1.1.1) builds from the parameter list params and body: the source text is `function anonymous(params\n) {\nbody\n}` (with the prefix of the kind), which must parse as that one function with params and body exactly where they were put, so that neither can end the other early. The template it returns is a script that declares nothing and returns the function, created in the global environment: the name anonymous is not bound.