native

package
v0.0.0-...-c2c195c Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 6, 2026 License: BSD-3-Clause Imports: 5 Imported by: 1

Documentation

Overview

Package native provides types to implement native variables, constants, functions, types and packages that can be imported or used as builtins in programs and templates.

Index

Constants

This section is empty.

Variables

View Source
var StopLookup = errors.New("stop lookup")

StopLookup is used as return value from a LookupFunc function to indicate that the lookup should be stopped.

Functions

func FunctionCallArgumentAt

func FunctionCallArgumentAt[T any](arguments FunctionCallArguments, index int) T

FunctionCallArgumentAt returns a variadic-view argument with its exact element type.

func FunctionCallFrameArg

func FunctionCallFrameArg[T any](frame FunctionCallFrame, index int) T

FunctionCallFrameArg returns an argument with its exact declared type.

func FunctionCallFrameReceiver

func FunctionCallFrameReceiver[T any](frame FunctionCallFrame) T

FunctionCallFrameReceiver returns the receiver assignable to T.

func FunctionCallFrameVariadicArg

func FunctionCallFrameVariadicArg[T any](frame FunctionCallFrame, index int) T

FunctionCallFrameVariadicArg returns an expanded variadic argument with its exact element type.

Types

type AdaptiveFunc

type AdaptiveFunc struct {
	// Impl is the runtime implementation. Must be a reflect.Func.
	// Argument count and concrete assignability are validated against
	// its declared signature; its declared return type is replaced by
	// what ReturnType produces.
	Impl any

	// LambdaParams supplies the parameter types of a lambda argument, so a
	// caller can write `filter(items, e => e.Ready)` and have `e` typed from
	// what `items` is.
	//
	// It is called once per call site, for each argument that is a lambda with
	// no declared parameter types, with the static types of the arguments
	// already resolved to its left (later ones are nil). Return nil to leave
	// the lambda uninferable, which surfaces as a "cannot infer" diagnostic at
	// the lambda rather than a confusing error inside its body.
	//
	// The lambda's RESULT type is never supplied here — the checker infers it
	// from the returned expression.
	LambdaParams func(argIndex int, resolved []reflect.Type) []reflect.Type

	// ReturnType is the static-type computation. Called once per call
	// site by the type-checker. argTypes contains the static types
	// of the call's actual arguments, in order. The returned
	// reflect.Type becomes the call expression's static return type.
	// Return (nil, err) to surface err as the call site's compile
	// diagnostic.
	ReturnType func(argTypes []reflect.Type) (reflect.Type, error)
}

AdaptiveFunc declares a native function whose static return type at each call site is computed by a closure from the static types of the call's arguments. Use this for slice-operation filters that should preserve the element type of their input — without this, the declared concrete return type (typically `[]any` for polymorphic filters) erases useful type information at the call boundary.

Behaviour:

  • Argument count and concrete assignability are checked against Impl's reflect.Func signature, exactly as for a plain native function. The closure does NOT influence argument validation.
  • The static return type at each call site is the value returned by ReturnType(argTypes), where argTypes is the list of the call's actual argument static types. Impl's declared return type is ignored for this purpose.
  • At render time Impl is invoked with the call's arguments boxed in `any`. Impl should use reflection to construct a return value matching the static type ReturnType promised at type-check time. Returning a mismatched type at runtime triggers a panic in Scriggo's call-result handling.
  • If ReturnType returns a non-nil error, the type-check fails at the call site with that error as the diagnostic.

Example:

// shard_slice — return type matches the input slice's static type.
var ShardSlice = native.AdaptiveFunc{
    Impl: func(items any, shardIndex, totalShards int) any { /* … */ },
    ReturnType: func(argTypes []reflect.Type) (reflect.Type, error) {
        return argTypes[0], nil
    },
}

type CSS

type CSS string

CSS is the css type in templates.

type CSSEnvStringer

type CSSEnvStringer interface {
	CSS(Env) CSS
}

CSSEnvStringer is like CSSStringer where the CSS method takes an Env parameter.

type CSSStringer

type CSSStringer interface {
	CSS() CSS
}

CSSStringer is implemented by values that are not escaped in CSS context.

type CombinedImporter

type CombinedImporter []Importer

CombinedImporter combines multiple importers into one importer.

func (CombinedImporter) Import

func (importers CombinedImporter) Import(path string) (ImportablePackage, error)

Import calls the Import method of each importer and returns as soon as an importer returns a package.

type CombinedPackage

type CombinedPackage []ImportablePackage

CombinedPackage implements an ImportablePackage by combining multiple packages into one package with name the name of the first package and as declarations the declarations of all packages.

The ImportablePackage.Lookup method calls the Lookup methods of each package in order and returns as soon as a package returns a not nil value.

func (CombinedPackage) Lookup

func (packages CombinedPackage) Lookup(name string) Declaration

Lookup calls the Lookup method of each package in order and returns as soon as a combined package returns a declaration.

func (CombinedPackage) LookupFunc

func (packages CombinedPackage) LookupFunc(f LookupFunc) error

LookupFunc calls the LookupFunc method of each package in order. As soon as f returns StopLookup, LookupFunc returns. If the same declaration name is in multiple packages, f is only called with its first occurrence.

func (CombinedPackage) PackageName

func (packages CombinedPackage) PackageName() string

PackageName returns the package name of the first combined package.

type Converter

type Converter = func(src []byte, out io.Writer) error

Converter is implemented by format converters.

type Declaration

type Declaration any

Declaration represents a declaration.

type Declarations

type Declarations map[string]Declaration

Declarations represents a set of variables, constants, functions, types and packages declarations and can be used for template globals and package declarations.

The key is the declaration's name and the element is its value.

type Env

type Env interface {

	// CallPath returns the path, relative to the root, of the call site of
	// the caller function. If it is not called by the main goroutine, the
	// returned value is not significant.
	CallPath() string

	// CallLine returns the 1-based line, within CallPath's file, of the call
	// site of the caller function, or 0 if unknown. Like CallPath, it is only
	// significant when called by the main goroutine.
	CallLine() int

	// Context returns the context of the execution.
	// It is the context passed as an option for execution.
	Context() context.Context

	// Fatal exits the execution and then panics with value v. Deferred
	// functions are not called and started goroutines are not terminated.
	Fatal(v any)

	// MarkdownConverter returns the Markdown converter provided to the
	// BuildTemplate function, if one was set.
	MarkdownConverter() Converter

	// Print calls the print built-in function with args as argument.
	Print(args ...any)

	// Println calls the println built-in function with args as argument.
	Println(args ...any)

	// Stop stops the execution with the given error. Deferred functions are
	// not called and started goroutines are not terminated.
	Stop(err error)

	// TypeOf is like reflect.TypeOf but if v has a Scriggo type it returns
	// its Scriggo reflect type instead of the reflect type of the proxy.
	TypeOf(v reflect.Value) reflect.Type
}

Env represents an execution environment.

Each execution creates an Env value. This value is passed as the first argument to calls to native functions and methods that have Env as the type of the first parameter.

type EnvStringer

type EnvStringer interface {
	String(Env) string
}

EnvStringer is like fmt.Stringer where the String method takes an Env parameter.

type FunctionCallArguments

type FunctionCallArguments struct {
	// contains filtered or unexported fields
}

FunctionCallArguments provides revoked-frame-checked access to variadic arguments.

func (FunctionCallArguments) Len

func (arguments FunctionCallArguments) Len() int

Len returns the number of arguments in the view.

func (FunctionCallArguments) Value

func (arguments FunctionCallArguments) Value(index int) reflect.Value

Value returns an argument as a reflect.Value of the variadic element type.

type FunctionCallFrame

type FunctionCallFrame struct {
	// contains filtered or unexported fields
}

FunctionCallFrame provides allocation-free access to one native call's VM registers.

func (FunctionCallFrame) ArgBool

func (frame FunctionCallFrame) ArgBool(index int) bool

ArgBool returns a bool argument.

func (FunctionCallFrame) ArgEnv

func (frame FunctionCallFrame) ArgEnv(index int) Env

ArgEnv returns an Env argument.

func (FunctionCallFrame) ArgFloat

func (frame FunctionCallFrame) ArgFloat(index int) float64

ArgFloat returns a floating-point argument.

func (FunctionCallFrame) ArgInt

func (frame FunctionCallFrame) ArgInt(index int) int64

ArgInt returns a signed integer argument.

func (FunctionCallFrame) ArgString

func (frame FunctionCallFrame) ArgString(index int) string

ArgString returns a string argument.

func (FunctionCallFrame) ArgUint

func (frame FunctionCallFrame) ArgUint(index int) uint64

ArgUint returns an unsigned integer argument.

func (FunctionCallFrame) ArgValue

func (frame FunctionCallFrame) ArgValue(index int) reflect.Value

ArgValue returns an argument as a reflect.Value of its declared type.

func (FunctionCallFrame) Receiver

func (frame FunctionCallFrame) Receiver() reflect.Value

Receiver returns the bound receiver of a method-value call.

func (FunctionCallFrame) SetResultBool

func (frame FunctionCallFrame) SetResultBool(index int, value bool)

SetResultBool stores a bool result.

func (FunctionCallFrame) SetResultFloat

func (frame FunctionCallFrame) SetResultFloat(index int, value float64)

SetResultFloat stores a floating-point result.

func (FunctionCallFrame) SetResultInt

func (frame FunctionCallFrame) SetResultInt(index int, value int64)

SetResultInt stores a signed integer result.

func (FunctionCallFrame) SetResultString

func (frame FunctionCallFrame) SetResultString(index int, value string)

SetResultString stores a string result.

func (FunctionCallFrame) SetResultUint

func (frame FunctionCallFrame) SetResultUint(index int, value uint64)

SetResultUint stores an unsigned integer result.

func (FunctionCallFrame) SetResultValue

func (frame FunctionCallFrame) SetResultValue(index int, value reflect.Value)

SetResultValue stores a result assignable to its declared type.

func (FunctionCallFrame) SetResultZero

func (frame FunctionCallFrame) SetResultZero(index int)

SetResultZero stores the zero value of a result's declared type.

func (FunctionCallFrame) Type

func (frame FunctionCallFrame) Type() reflect.Type

Type returns the exact function type invoked by this frame.

func (FunctionCallFrame) VariadicArguments

func (frame FunctionCallFrame) VariadicArguments() FunctionCallArguments

VariadicArguments returns a view over expanded or directly passed variadic arguments.

func (FunctionCallFrame) VariadicBool

func (frame FunctionCallFrame) VariadicBool(index int) bool

VariadicBool returns an expanded bool argument.

func (FunctionCallFrame) VariadicFloat

func (frame FunctionCallFrame) VariadicFloat(index int) float64

VariadicFloat returns an expanded floating-point argument.

func (FunctionCallFrame) VariadicInt

func (frame FunctionCallFrame) VariadicInt(index int) int64

VariadicInt returns an expanded signed integer argument.

func (FunctionCallFrame) VariadicLen

func (frame FunctionCallFrame) VariadicLen() int

VariadicLen returns the number of expanded variadic arguments, or -1 when a slice was passed directly.

func (FunctionCallFrame) VariadicString

func (frame FunctionCallFrame) VariadicString(index int) string

VariadicString returns an expanded string argument.

func (FunctionCallFrame) VariadicUint

func (frame FunctionCallFrame) VariadicUint(index int) uint64

VariadicUint returns an expanded unsigned integer argument.

func (FunctionCallFrame) VariadicValue

func (frame FunctionCallFrame) VariadicValue(index int) reflect.Value

VariadicValue returns an expanded argument as a reflect.Value of its declared element type.

type FunctionCallFrameAccess

type FunctionCallFrameAccess interface {
	FunctionCallFrameValid(uint64) bool
	FunctionCallFrameReceiver(uint64) reflect.Value
	FunctionCallFrameEnv(uint64, int) Env
	FunctionCallFrameBool(uint64, int, int) bool
	FunctionCallFrameInt(uint64, int, int) int64
	FunctionCallFrameUint(uint64, int, int) uint64
	FunctionCallFrameFloat(uint64, int, int) float64
	FunctionCallFrameString(uint64, int, int) string
	FunctionCallFrameValue(uint64, int, int, reflect.Type) reflect.Value
	FunctionCallFrameSliceLen(uint64, int) int
	FunctionCallFrameSliceValue(uint64, int, int, reflect.Type) reflect.Value
	FunctionCallFrameSetBool(uint64, int, bool)
	FunctionCallFrameSetInt(uint64, int, int64)
	FunctionCallFrameSetUint(uint64, int, uint64)
	FunctionCallFrameSetFloat(uint64, int, float64)
	FunctionCallFrameSetString(uint64, int, string)
	FunctionCallFrameSetValue(uint64, int, reflect.Value)
}

FunctionCallFrameAccess connects a FunctionCallFrame to one active runtime call.

type FunctionCallFrameSite

type FunctionCallFrameSite struct {
	// contains filtered or unexported fields
}

FunctionCallFrameSite is an immutable, pre-authenticated frame invocation target.

func (*FunctionCallFrameSite) Invoke

func (site *FunctionCallFrameSite) Invoke(
	access FunctionCallFrameAccess,
	generation uint64,
	variadicCount int,
)

Invoke binds the active runtime access and calls the authenticated frame implementation.

func (*FunctionCallFrameSite) Type

func (site *FunctionCallFrameSite) Type() reflect.Type

Type returns the exact function type accepted by the site.

type FunctionTrampoline

type FunctionTrampoline struct {
	// contains filtered or unexported fields
}

FunctionTrampoline associates an exact function value with a direct implementation.

func MakeFunctionTrampoline

func MakeFunctionTrampoline(
	typ reflect.Type,
	call func([]reflect.Value) []reflect.Value,
) *FunctionTrampoline

MakeFunctionTrampoline creates a function value and a direct invocation path for it.

func MakeFunctionTrampolineFor

func MakeFunctionTrampolineFor(
	value reflect.Value,
	call func([]reflect.Value) []reflect.Value,
) *FunctionTrampoline

MakeFunctionTrampolineFor creates a direct invocation path for an existing function value.

func MakeFunctionTrampolineForWithFrame

func MakeFunctionTrampolineForWithFrame(
	value reflect.Value,
	call func([]reflect.Value) []reflect.Value,
	callFrame func(FunctionCallFrame),
) *FunctionTrampoline

MakeFunctionTrampolineForWithFrame adds a VM-register call path for an existing function value.

func MakeFunctionTrampolineWithFrame

func MakeFunctionTrampolineWithFrame(
	typ reflect.Type,
	call func([]reflect.Value) []reflect.Value,
	callFrame func(FunctionCallFrame),
) *FunctionTrampoline

MakeFunctionTrampolineWithFrame adds a VM-register call path to a function trampoline.

func MakeMethodTrampoline

func MakeMethodTrampoline(
	receiver reflect.Type,
	methodName string,
	call func([]reflect.Value) []reflect.Value,
) *FunctionTrampoline

MakeMethodTrampoline creates a direct invocation path for an exact method target.

func MakeMethodTrampolineWithFrame

func MakeMethodTrampolineWithFrame(
	receiver reflect.Type,
	methodName string,
	call func([]reflect.Value) []reflect.Value,
	callFrame func(FunctionCallFrame),
) *FunctionTrampoline

MakeMethodTrampolineWithFrame adds a VM-register call path for an exact method target.

func (*FunctionTrampoline) Call

func (trampoline *FunctionTrampoline) Call(args []reflect.Value) []reflect.Value

Call invokes the implementation without reflect.Value.Call.

func (*FunctionTrampoline) CallFrame

func (trampoline *FunctionTrampoline) CallFrame(frame FunctionCallFrame)

CallFrame invokes the VM-register implementation of trampoline.

func (*FunctionTrampoline) FunctionCallFrameSite

func (trampoline *FunctionTrampoline) FunctionCallFrameSite() *FunctionCallFrameSite

FunctionCallFrameSite returns the immutable frame target after validating the trampoline.

func (*FunctionTrampoline) MethodTarget

func (trampoline *FunctionTrampoline) MethodTarget() (reflect.Type, string, bool)

MethodTarget reports the exact method receiver and name for a method trampoline.

func (*FunctionTrampoline) NewFunctionCallFrame

func (trampoline *FunctionTrampoline) NewFunctionCallFrame(
	access FunctionCallFrameAccess,
	generation uint64,
	variadicCount int,
) FunctionCallFrame

NewFunctionCallFrame binds an active runtime call to this trampoline's exact type.

func (*FunctionTrampoline) SupportsFunctionCallFrame

func (trampoline *FunctionTrampoline) SupportsFunctionCallFrame() bool

SupportsFunctionCallFrame reports whether trampoline has a VM-register call path.

func (*FunctionTrampoline) Value

func (trampoline *FunctionTrampoline) Value() reflect.Value

Value returns the function value represented by trampoline.

type HTML

type HTML string

HTML is the html type in templates.

type HTMLEnvStringer

type HTMLEnvStringer interface {
	HTML(Env) HTML
}

HTMLEnvStringer is like HTMLStringer where the HTML method takes a Env parameter.

type HTMLStringer

type HTMLStringer interface {
	HTML() HTML
}

HTMLStringer is implemented by values that are not escaped in HTML context.

type ImportablePackage

type ImportablePackage interface {

	// PackageName returns the name of the package.
	// It is a Go identifier but not the empty identifier.
	PackageName() string

	// Lookup searches for an exported declaration, named name, in the
	// package. If the declaration does not exist, it returns nil.
	Lookup(name string) Declaration

	// LookupFunc calls f for each package declaration stopping if f returns
	// an error. Lookup order is undefined.
	LookupFunc(f LookupFunc) error
}

ImportablePackage represents an importable package.

type Importer

type Importer interface {
	Import(path string) (ImportablePackage, error)
}

Importer represents a package importer; Import returns the native package with the given package path.

If an error occurs it returns the error, if the package does not exist it returns nil and nil.

type JS

type JS string

JS is the js type in templates.

type JSEnvStringer

type JSEnvStringer interface {
	JS(Env) JS
}

JSEnvStringer is like JSStringer where the JS method takes an Env parameter.

type JSON

type JSON string

JSON is the json type in templates.

type JSONEnvStringer

type JSONEnvStringer interface {
	JSON(Env) JSON
}

JSONEnvStringer is like JSONStringer where the JSON method takes an Env parameter.

type JSONStringer

type JSONStringer interface {
	JSON() JSON
}

JSONStringer is implemented by values that are not escaped in JSON context.

type JSStringer

type JSStringer interface {
	JS() JS
}

JSStringer is implemented by values that are not escaped in JavaScript context.

type LookupFunc

type LookupFunc func(name string, decl Declaration) error

LookupFunc is the type of the function called by ImportablePackage.LookupFunc to read each package declaration. If the function returns an error, ImportablePackage.LookupFunc stops and returns the error or nil if the error is StopLookup.

type Markdown

type Markdown string

Markdown is the markdown type in templates.

type MarkdownEnvStringer

type MarkdownEnvStringer interface {
	Markdown(Env) Markdown
}

MarkdownEnvStringer is like MarkdownStringer where the Markdown method takes a Env parameter.

type MarkdownStringer

type MarkdownStringer interface {
	Markdown() Markdown
}

MarkdownStringer is implemented by values that are not escaped in Markdown context.

type Package

type Package struct {
	// Name of the package.
	Name string
	// Declarations of the package.
	Declarations Declarations
}

Package implements ImportablePackage given its name and declarations.

func (Package) Lookup

func (p Package) Lookup(name string) Declaration

Lookup returns the declaration named name in the package or nil if no such declaration exists.

func (Package) LookupFunc

func (p Package) LookupFunc(f LookupFunc) error

LookupFunc calls f for each package declaration stopping if f returns an error. Lookup order is undefined.

func (Package) PackageName

func (p Package) PackageName() string

PackageName returns the name of the package.

type Packages

type Packages map[string]ImportablePackage

Packages implements Importer using a map of ImportablePackage.

func (Packages) Import

func (pp Packages) Import(path string) (ImportablePackage, error)

Import returns an ImportablePackage.

type SynchronousDeclaration

type SynchronousDeclaration struct {
	Declaration Declaration
	Members     []string
}

SynchronousDeclaration marks callable native behavior as safe for batched template execution. Calls must finish before returning and must not retain Env values or Scriggo function values. Members contains callable field or method paths exposed by a variable, type, or function result. Members of a multiple-result function start with their zero-based result index, such as "[0].Milliseconds". A '*' path segment matches every struct field at that level, not callable names.

func Synchronous

func Synchronous(declaration Declaration, members ...string) SynchronousDeclaration

Synchronous marks callable native behavior as safe for batched template execution. A function declaration or function-typed variable is certified directly. Callable fields and methods require explicit member paths. Members of a multiple-result function require an exact zero-based result index. Function-valued results cannot be certified and make a batch fail closed.

type TextFragment

type TextFragment interface {
	io.WriterTo
}

TextFragment is immutable, replayable text that can be streamed without first materializing it as a string. It is showable only in text context.

type TextFragmentWriter

type TextFragmentWriter interface {
	WriteTextFragment(TextFragment) error
}

TextFragmentWriter accepts a fragment directly and may retain it after the template execution returns. Scriggo uses it only for a text-format root without source maps; its output must match TextFragment.WriteTo.

type UntypedBooleanConst

type UntypedBooleanConst bool

UntypedBooleanConst represents an untyped boolean constant.

type UntypedNumericConst

type UntypedNumericConst string

UntypedNumericConst represents an untyped numeric constant.

type UntypedStringConst

type UntypedStringConst string

UntypedStringConst represents an untyped string constant.

type VectorAuthority

type VectorAuthority struct {
	// contains filtered or unexported fields
}

VectorAuthority authenticates a vector execution controller to an Env. Each vector run accepts only the exact authority configured by its caller.

func NewVectorAuthority

func NewVectorAuthority() *VectorAuthority

NewVectorAuthority returns a unique vector execution authority.

type VectorBoundary

type VectorBoundary struct {
	// contains filtered or unexported fields
}

VectorBoundary marks VM-native vector item boundaries.

func NewVectorBoundary

func NewVectorBoundary() *VectorBoundary

NewVectorBoundary returns a unique VM-native vector boundary.

func (*VectorBoundary) Begin

func (boundary *VectorBoundary) Begin(int)

Begin starts a VM-native vector item.

func (*VectorBoundary) End

func (boundary *VectorBoundary) End(int)

End finishes a VM-native vector item.

func (*VectorBoundary) Valid

func (boundary *VectorBoundary) Valid() bool

Valid reports whether boundary was created by NewVectorBoundary.

type VectorEnv

type VectorEnv interface {
	Env
	ActivateVectorIndex(*VectorAuthority, int) error
	RevokeVectorIndex(*VectorAuthority) error
	LoadVectorRange(
		*VectorAuthority,
		int,
		map[string]any,
		[]context.Context,
		[]*FunctionTrampoline,
	) error
	// LoadVectorRangeOwned transfers freshly allocated columns and contexts to the VM.
	LoadVectorRangeOwned(
		*VectorAuthority,
		int,
		map[string]any,
		[]context.Context,
		[]*FunctionTrampoline,
	) error
}

VectorEnv is implemented by an Env while a vector execution is configured.

type VectorFiberBoundary

type VectorFiberBoundary struct {
	// contains filtered or unexported fields
}

VectorFiberBoundary marks VM-native child boundaries inside one vector item.

func NewVectorFiberBoundary

func NewVectorFiberBoundary() *VectorFiberBoundary

NewVectorFiberBoundary returns a unique VM-native vector fiber boundary.

func (*VectorFiberBoundary) BeginChild

func (boundary *VectorFiberBoundary) BeginChild(int, int)

BeginChild starts one child of the active vector item.

func (*VectorFiberBoundary) EndChild

func (boundary *VectorFiberBoundary) EndChild(int, int)

EndChild finishes one child of the active vector item.

func (*VectorFiberBoundary) Valid

func (boundary *VectorFiberBoundary) Valid() bool

Valid reports whether boundary was created by NewVectorFiberBoundary.

type VectorFiberLifecycle

type VectorFiberLifecycle interface {
	BeginChild(int, int) (context.Context, error)
	EndChild(int, int) error
}

VectorFiberLifecycle receives authenticated child transitions. BeginChild returns the exact context owned by that child.

type VectorLifecycle

type VectorLifecycle interface {
	Begin(context.Context, int) error
	Finish(int) error
	Commit(int) error
	Abort(int, error)
}

VectorLifecycle receives authenticated VM-native vector item transitions.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL