Skip to content
velt/generate exports

velt/generate

API reference

What a source generator is written with. A library declares a generic function without a body, and a generator, a Velt function that reads a description of the types, writes the body for each type the program uses the function with. For a walkthrough, read the guide.

import { InstanceRequest, Output } from "velt/generate";

Exports

Re-exported from velt/generate/output

function quote(s: string): string

s as a double-quoted Velt (and JavaScript) string literal.

Re-exported from velt/generate/serve

function serve(generators: GeneratorEntry[]): void

The host's loop (the compiler builds a program whose main calls it with the generators it needs): reads the requests from standard input, runs each generator, and writes the bodies, the further instances they ask for and their diagnostics to standard output.

Re-exported from velt/generate/enum_member

A member of an enum: its name and its value as JavaScript source.

name: string = ""No doc comment yet
value: string = ""No doc comment yet

Re-exported from velt/generate/field

A field of an object, class or struct type.

name: string = ""No doc comment yet
type: i64 = 0No doc comment yet
optional: bool = falseNo doc comment yet
readonly: bool = falseNo doc comment yet
isPrivate: bool = false

private, or an ES private field (#x, whose name keeps the #).

isEsPrivate: bool = falseNo doc comment yet
hasDefault: bool = falseNo doc comment yet
isGetter: bool = false

An object literal's getter (get k() { … }): type is what it returns.

stdPrivate: bool = false

A private field a std type, or a type of a package with a native library, declares (its runtime state: a handle), also in a class extending it.

via: i64 = -1

A field the type's objects hold for an extension (interface E extends T with optional fields, whose objects are T's): code reads it from the value seen as type via (-1: a field of the type itself).

at: i64 = -1No doc comment yet

Re-exported from velt/generate/model

Every type a request reaches.

types: Type[] = []No doc comment yet
type(id: i64): Type

The type with id id.

Re-exported from velt/generate/output

Where a generator writes the instance's body, helpers and diagnostics.

constructor(session: Session, name: string)No doc comment yet
body(code: string): void

Appends code to the body.

line(code: string): void

Appends one line of code to the body.

helper(code: string): void

Adds a top-level declaration (a helper function or constant) next to the instance.

fresh(base: string): string

A name for a helper that no other instance uses.

instance(fn: string, args: i64[]): string

The name of the function generated for instance fn<args…> of a generated function of the same generator module (generated in the same run when it is new): code calls it with the arguments fn takes.

typeName(id: i64): string

How generated code writes model type id.

literal(s: string): string

s as a Velt string literal.

error(message: string, at: i64, labels: [i64, string][] = []): void

Reports an error at at (a type's or a field's at; -1: at each call that needs the instance). The first line of message is the error, each further line a note; labels point at other places of the model ([at, text], such as a field's declaration).

takeBody(): string

The body written so far.

takeHelpers(): stringNo doc comment yet

Re-exported from velt/generate/instance_request

What the compiler asks a generator for: the body of one instance.

fn: string = ""

The generated function's name, and the instance's type arguments (model ids).

args: i64[] = []No doc comment yet
params: string[] = []

The names of the instance's parameters, in order.

model: Model = new Model()No doc comment yet

Re-exported from velt/generate/method

A method of a class that std's operations call by name (toJSON, toString, the inspect hook keyed by Symbol.for("nodejs.util.inspect.custom")), when it can be called that way: its name, how many parameters it takes and its result type.

name: string = ""No doc comment yet
params: i64 = 0No doc comment yet
ret: i64 = 0No doc comment yet

Re-exported from velt/generate/session

One run of the host: the model, the instances generated and queued, the output.

model: Model = new Model()No doc comment yet
prefix: string = ""No doc comment yet
key: string = ""

The generator key of the instance being generated.

names: Map<string, string> = new Map()No doc comment yet
params: Map<string, string[]> = new Map()No doc comment yet
queue: Pending[] = []No doc comment yet
next: i64 = 0No doc comment yet
instance(fn: string, args: i64[]): stringNo doc comment yet
report(kind: string, name: string, at: i64, message: string): voidNo doc comment yet

Re-exported from velt/generate/type

One type of the model: what a generator may know about it.

id: i64 = 0No doc comment yet
kind: string = ""

number, boolean, string, void, never, symbol, literal, enum, array, tuple, option, union, object, class, struct, interface, map, record, set, shared, date, function (also a function with properties), generator, any, unknown, promise or opaque.

display: string = ""

As messages show it (User, number[]).

qualifiedName: string = ""

<module path>::<name> of a declared type, else "".

spelling: string = ""

How generated code writes the type ("" when it cannot be named).

repr: string = ""

f64, i32, … for numbers.

args: i64[] = []

Element (array, set), payload (option, promise, shared), key and value (map, record), tuple elements or type arguments, by kind.

arms: i64[] = []

The members of a union, in order.

fields: Field[] = []

Fields of objects, classes and structs, inherited ones first.

base: i64 = -1

A class's base class (-1: none) and the classes of the program that extend it.

subclasses: i64[] = []No doc comment yet
members: EnumMember[] = []

Members of an enum.

literal: string = ""

The value of a literal type as JavaScript source ("a", 1, true); a string is also its JSON text. For an option: what it holds when empty (null, undefined or null | undefined).

methods: Method[] = []

The methods std's operations call by name (toJSON, toString, the inspect hook keyed by Symbol.for("nodejs.util.inspect.custom")).

mayBeProxy: bool = false

Whether a value of this type may be a typed Proxy standing for it (a class or object type in a program that uses Proxy): only then does a generator need a path for proxies.

hasProperties: bool = false

A function with properties (Object.assign(f, { … })).

classValue: bool = false

A class used as a value (typeof C: its constructor and statics).

dynamic: bool = false

A dynamic type: any, unknown, and also object and Function (kind interface), whose values say at run time what they hold.

privateField: string = ""

For a std type, or a type of a package with a native library, whose structure the model does not show (opaque, set): the private field that keeps its state (a runtime handle), else "".

ctorAccess: string = ""

A struct's or class's constructor when it is not public (private, protected), else "": code that builds a value without running the constructor (__construct) bypasses it.

factories: string[] = []

The public static methods of a class that return one (its factories), sorted.

at: i64 = -1

Where it is declared, for Output.error (-1: nowhere).

type

GeneratorEntry

#std/generate/serve.vlt:15

Re-exported from velt/generate/serve

type GeneratorEntry = { key: string; fn: string; run: (req: InstanceRequest, out: Output) => void throws Error }

A generated function the host can generate: its generator's key, its name, the generator.