object

package
v2.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 10, 2026 License: Apache-2.0 Imports: 19 Imported by: 0

Documentation

Overview

Package object re-exports error types from the errors package for convenience.

Package object provides the standard set of Risor object types.

For external users of Risor, often an object.Object interface will be type asserted to a specific object type, such as *object.Float.

For example:

switch obj := obj.(type) {
case *object.String:
	// do something with obj.Value()
case *object.Float:
	// do something with obj.Value()
}

The Type() method of each object may also be used to get a string name of the object type, such as "string" or "float".

Index

Constants

View Source
const (
	ErrSyntax  = errors.ErrSyntax
	ErrType    = errors.ErrType
	ErrName    = errors.ErrName
	ErrValue   = errors.ErrValue
	ErrRuntime = errors.ErrRuntime
	ErrImport  = errors.ErrImport
)

Re-export error kind constants

Variables

View Source
var (
	FormatStackTrace    = errors.FormatStackTrace
	NewEvalError        = errors.NewEvalError
	NewArgsErrorType    = errors.NewArgsError
	NewTypeError        = errors.NewTypeError
	NewValueError       = errors.NewValueError
	NewIndexError       = errors.NewIndexError
	NewStructuredError  = errors.NewStructuredError
	NewStructuredErrorf = errors.NewStructuredErrorf
)

Re-export functions for convenience

View Source
var (
	Nil   = &NilType{}
	True  = &Bool{value: true}
	False = &Bool{value: false}
)

Functions

func Arg

func Arg[T Object](args []Object, index int, methodName string) (T, error)

Arg extracts and type-asserts an argument from the args slice. Returns a descriptive error if the index is out of bounds or the type doesn't match.

func AsBool

func AsBool(obj Object) (bool, error)

func AsByte

func AsByte(obj Object) (byte, error)

func AsBytes

func AsBytes(obj Object) ([]byte, error)

func AsFloat

func AsFloat(obj Object) (float64, error)

func AsInt

func AsInt(obj Object) (int64, error)

func AsObjects

func AsObjects(m map[string]any) (map[string]Object, error)

AsObjects transforms a map containing arbitrary Go types to a map of Risor objects, using the default registry. If an item in the map is of a type that can't be converted, an error is returned.

func AsObjectsWithRegistry

func AsObjectsWithRegistry(m map[string]any, registry *TypeRegistry) (map[string]Object, error)

AsObjectsWithRegistry transforms a map containing arbitrary Go types to a map of Risor objects using the specified registry.

func AsReader

func AsReader(obj Object) (io.Reader, error)

func AsString

func AsString(obj Object) (string, error)

func AsStringSlice

func AsStringSlice(obj Object) ([]string, error)

func AsTime

func AsTime(obj Object) (time.Time, error)

func AsWriter

func AsWriter(obj Object) (io.Writer, error)

func AttrNames

func AttrNames(attrs []AttrSpec) []string

AttrNames returns just the attribute names from a slice of AttrSpec. This is a convenience helper for common use cases.

func CompareTypes

func CompareTypes(a, b Object) int

func Equals

func Equals(a, b Object) bool

func IsError

func IsError(obj Object) bool

func Keys

func Keys(m map[string]Object) []string

Keys returns the keys of an object map as a sorted slice of strings.

func ObjectToRune

func ObjectToRune(obj Object) (rune, error)

ObjectToRune converts a Risor Object to a rune.

func PrintableValue

func PrintableValue(obj Object) interface{}

PrintableValue returns a value that should be used when printing an object.

func RegisterType

func RegisterType(t Type, description string, attrsFn func() []AttrSpec)

RegisterType registers a type with its documentation. This should be called from init() functions in type implementation files.

func ResolveIndex

func ResolveIndex(idx int64, size int64) (int64, error)

ResolveIndex checks that the index is inbounds and transforms a negative index into the corresponding positive index. If the index is out of bounds, an error is returned.

func ResolveIntSlice

func ResolveIntSlice(slice Slice, size int64) (start int64, stop int64, err error)

ResolveIntSlice checks that the slice start and stop indices are inbounds and transforms negative indices into the corresponding positive indices. If the slice is out of bounds, an error is returned.

func TypeDocsMap

func TypeDocsMap() map[string]TypeSpec

TypeDocsMap returns documentation for all registered types as a map.

func WithCallFunc

func WithCallFunc(ctx context.Context, fn CallFunc) context.Context

WithCallFunc stores a CallFunc in the context. Called by the VM during initialization to enable Closure.Call() to execute bytecode.

Types

type ArgsError

type ArgsError = errors.ArgsError

Re-export types from errors package for convenience

type AttrBuilder

type AttrBuilder[T any] struct {
	// contains filtered or unexported fields
}

AttrBuilder provides a fluent API for defining a single attribute.

func (*AttrBuilder[T]) Arg

func (b *AttrBuilder[T]) Arg(name string) *AttrBuilder[T]

Arg adds a required argument by name (for methods). Panics if called after OptionalArg (required args must come first).

func (*AttrBuilder[T]) Args

func (b *AttrBuilder[T]) Args(names ...string) *AttrBuilder[T]

Args adds multiple required arguments (for methods). Panics if called after OptionalArg (required args must come first).

func (*AttrBuilder[T]) Doc

func (b *AttrBuilder[T]) Doc(doc string) *AttrBuilder[T]

Doc sets the attribute's documentation string.

func (*AttrBuilder[T]) Getter

func (b *AttrBuilder[T]) Getter(fn func(T) Object)

Getter sets the property getter and registers the attribute. Use this for read-only properties that return a value directly. Panics if an attribute with the same name is already registered.

func (*AttrBuilder[T]) Impl

func (b *AttrBuilder[T]) Impl(fn func(T, context.Context, ...Object) (Object, error))

Impl sets the method implementation and registers the attribute. Use this for callable methods that take arguments. Panics if an attribute with the same name is already registered.

func (*AttrBuilder[T]) OptionalArg

func (b *AttrBuilder[T]) OptionalArg(name string) *AttrBuilder[T]

OptionalArg adds an optional argument by name (for methods). Optional args must come after all required args.

func (*AttrBuilder[T]) Returns

func (b *AttrBuilder[T]) Returns(typ string) *AttrBuilder[T]

Returns sets the return type (for documentation/tooling).

type AttrDef

type AttrDef[T any] struct {
	Spec       AttrSpec
	IsProperty bool
	MinArgs    int // Minimum required arguments (for optional arg support)
	// For methods:
	MethodImpl func(self T, ctx context.Context, args ...Object) (Object, error)
	// For properties:
	PropertyImpl func(self T) Object
}

AttrDef combines an attribute's specification with its implementation. It supports both properties (direct value access) and methods (callable).

type AttrRegistry

type AttrRegistry[T any] struct {
	// contains filtered or unexported fields
}

AttrRegistry holds all attributes (properties and methods) for a given object type.

func NewAttrRegistry

func NewAttrRegistry[T any](typeName string) *AttrRegistry[T]

NewAttrRegistry creates a registry for the given type name.

func NewMethodRegistry

func NewMethodRegistry[T any](typeName string) *AttrRegistry[T]

func (*AttrRegistry[T]) Define

func (r *AttrRegistry[T]) Define(name string) *AttrBuilder[T]

Define starts building a new attribute definition. Returns an AttrBuilder for fluent configuration.

func (*AttrRegistry[T]) GetAttr

func (r *AttrRegistry[T]) GetAttr(self T, name string) (Object, bool)

GetAttr returns the named attribute bound to self. For properties, returns the value directly. For methods, returns a Builtin wrapper. Returns nil, false if the attribute doesn't exist.

func (*AttrRegistry[T]) Specs

func (r *AttrRegistry[T]) Specs() []AttrSpec

Specs returns a copy of all registered attribute specifications in registration order.

type AttrResolver

type AttrResolver interface {
	ResolveAttr(ctx context.Context, name string) (Object, error)
}

AttrResolver is an interface used to resolve dynamic attributes on an object.

type AttrSpec

type AttrSpec struct {
	// Name is the attribute name (e.g., "split", "append").
	Name string

	// Doc is a short description of what the attribute does.
	Doc string

	// Args lists parameter names (e.g., ["sep"] or ["old", "new"]).
	// Empty for attributes that take no arguments.
	Args []string

	// Returns describes the return type (e.g., "list", "string", "bool").
	Returns string
}

AttrSpec describes an attribute available on an object. This provides metadata for introspection, documentation, and tooling.

func FindAttr

func FindAttr(attrs []AttrSpec, name string) (AttrSpec, bool)

FindAttr searches for an attribute by name in a slice of AttrSpec. Returns the AttrSpec and true if found, or zero value and false if not.

type Bool

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

Bool wraps bool and implements Object and Hashable interface.

func NewBool

func NewBool(value bool) *Bool

func Not

func Not(b *Bool) *Bool

func (*Bool) Attrs

func (b *Bool) Attrs() []AttrSpec

func (*Bool) Compare

func (b *Bool) Compare(other Object) (int, error)

func (*Bool) Equals

func (b *Bool) Equals(other Object) bool

func (*Bool) GetAttr

func (b *Bool) GetAttr(name string) (Object, bool)

func (*Bool) Inspect

func (b *Bool) Inspect() string

func (*Bool) Interface

func (b *Bool) Interface() interface{}

func (*Bool) IsTruthy

func (b *Bool) IsTruthy() bool

func (*Bool) MarshalJSON

func (b *Bool) MarshalJSON() ([]byte, error)

func (*Bool) RunOperation

func (b *Bool) RunOperation(opType op.BinaryOpType, right Object) (Object, error)

func (*Bool) SetAttr

func (b *Bool) SetAttr(name string, value Object) error

func (*Bool) String

func (b *Bool) String() string

func (*Bool) Type

func (b *Bool) Type() Type

func (*Bool) Value

func (b *Bool) Value() bool

type Builtin

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

Builtin wraps func and implements Object interface.

func NewBuiltin

func NewBuiltin(name string, fn BuiltinFunction) *Builtin

NewBuiltin creates a new builtin function with the given name and function. Use the builder methods InModule() or WithModule() to associate the builtin with a module.

func NewNoopBuiltin

func NewNoopBuiltin(name string) *Builtin

NewNoopBuiltin creates a builtin function that has no effect. Use WithModule() to associate it with a module if needed.

func (*Builtin) Attrs

func (b *Builtin) Attrs() []AttrSpec

func (*Builtin) Call

func (b *Builtin) Call(ctx context.Context, args ...Object) (Object, error)

func (*Builtin) Equals

func (b *Builtin) Equals(other Object) bool

func (*Builtin) GetAttr

func (b *Builtin) GetAttr(name string) (Object, bool)

func (*Builtin) InModule

func (b *Builtin) InModule(moduleName string) *Builtin

InModule sets the module name for this builtin. This is used for the Key() method which returns the fully-qualified name (e.g., "math.sqrt").

func (*Builtin) Inspect

func (b *Builtin) Inspect() string

func (*Builtin) Interface

func (b *Builtin) Interface() interface{}

func (*Builtin) IsTruthy

func (b *Builtin) IsTruthy() bool

func (*Builtin) Key

func (b *Builtin) Key() string

Returns a string that uniquely identifies this builtin function.

func (*Builtin) MarshalJSON

func (b *Builtin) MarshalJSON() ([]byte, error)

func (*Builtin) Name

func (b *Builtin) Name() string

func (*Builtin) RunOperation

func (b *Builtin) RunOperation(opType op.BinaryOpType, right Object) (Object, error)

func (*Builtin) SetAttr

func (b *Builtin) SetAttr(name string, value Object) error

func (*Builtin) String

func (b *Builtin) String() string

func (*Builtin) Type

func (b *Builtin) Type() Type

func (*Builtin) Value

func (b *Builtin) Value() BuiltinFunction

func (*Builtin) WithModule

func (b *Builtin) WithModule(module *Module) *Builtin

WithModule sets the module for this builtin. The module name is derived from the module's name. Use this when you have a module reference.

type BuiltinFunction

type BuiltinFunction func(ctx context.Context, args ...Object) (Object, error)

BuiltinFunction holds the type of a built-in function.

type Byte

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

Byte wraps byte and implements Object and Hashable interface.

func NewByte

func NewByte(value byte) *Byte

func (*Byte) Attrs

func (b *Byte) Attrs() []AttrSpec

func (*Byte) Compare

func (b *Byte) Compare(other Object) (int, error)

func (*Byte) Equals

func (b *Byte) Equals(other Object) bool

func (*Byte) GetAttr

func (b *Byte) GetAttr(name string) (Object, bool)

func (*Byte) Inspect

func (b *Byte) Inspect() string

func (*Byte) Interface

func (b *Byte) Interface() interface{}

func (*Byte) IsTruthy

func (b *Byte) IsTruthy() bool

func (*Byte) MarshalJSON

func (b *Byte) MarshalJSON() ([]byte, error)

func (*Byte) RunOperation

func (b *Byte) RunOperation(opType op.BinaryOpType, right Object) (Object, error)

func (*Byte) SetAttr

func (b *Byte) SetAttr(name string, value Object) error

func (*Byte) String

func (b *Byte) String() string

func (*Byte) Type

func (b *Byte) Type() Type

func (*Byte) Value

func (b *Byte) Value() byte

type Bytes

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

func NewBytes

func NewBytes(value []byte) *Bytes

func (*Bytes) Attrs

func (b *Bytes) Attrs() []AttrSpec

func (*Bytes) Clone

func (b *Bytes) Clone() *Bytes

func (*Bytes) Compare

func (b *Bytes) Compare(other Object) (int, error)

func (*Bytes) Contains

func (b *Bytes) Contains(obj Object) *Bool

func (*Bytes) ContainsAny

func (b *Bytes) ContainsAny(obj Object) (Object, error)

func (*Bytes) ContainsRune

func (b *Bytes) ContainsRune(obj Object) (Object, error)

func (*Bytes) Count

func (b *Bytes) Count(obj Object) (Object, error)

func (*Bytes) DelItem

func (b *Bytes) DelItem(key Object) *Error

func (*Bytes) Enumerate

func (b *Bytes) Enumerate(ctx context.Context, fn func(key, value Object) bool)

func (*Bytes) Equals

func (b *Bytes) Equals(other Object) bool

func (*Bytes) GetAttr

func (b *Bytes) GetAttr(name string) (Object, bool)

func (*Bytes) GetItem

func (b *Bytes) GetItem(key Object) (Object, *Error)

func (*Bytes) GetSlice

func (b *Bytes) GetSlice(slice Slice) (Object, *Error)

func (*Bytes) HasPrefix

func (b *Bytes) HasPrefix(obj Object) (Object, error)

func (*Bytes) HasSuffix

func (b *Bytes) HasSuffix(obj Object) (Object, error)

func (*Bytes) Index

func (b *Bytes) Index(obj Object) (Object, error)

func (*Bytes) IndexAny

func (b *Bytes) IndexAny(obj Object) (Object, error)

func (*Bytes) IndexByte

func (b *Bytes) IndexByte(obj Object) (Object, error)

func (*Bytes) IndexRune

func (b *Bytes) IndexRune(obj Object) (Object, error)

func (*Bytes) Inspect

func (b *Bytes) Inspect() string

func (*Bytes) Integers

func (b *Bytes) Integers() []Object

func (*Bytes) Interface

func (b *Bytes) Interface() interface{}

func (*Bytes) IsTruthy

func (b *Bytes) IsTruthy() bool

func (*Bytes) Len

func (b *Bytes) Len() *Int

func (*Bytes) MarshalJSON

func (b *Bytes) MarshalJSON() ([]byte, error)

func (*Bytes) Repeat

func (b *Bytes) Repeat(obj Object) (Object, error)

func (*Bytes) Replace

func (b *Bytes) Replace(old, new, count Object) (Object, error)

func (*Bytes) ReplaceAll

func (b *Bytes) ReplaceAll(old, new Object) (Object, error)

func (*Bytes) Reversed

func (b *Bytes) Reversed() *Bytes

func (*Bytes) RunOperation

func (b *Bytes) RunOperation(opType op.BinaryOpType, right Object) (Object, error)

func (*Bytes) SetAttr

func (b *Bytes) SetAttr(name string, value Object) error

func (*Bytes) SetItem

func (b *Bytes) SetItem(key, value Object) *Error

func (*Bytes) String

func (b *Bytes) String() string

func (*Bytes) Type

func (b *Bytes) Type() Type

func (*Bytes) Value

func (b *Bytes) Value() []byte

type CallFunc

type CallFunc func(ctx context.Context, fn *Closure, args []Object) (Object, error)

CallFunc is a function type for invoking closures. The VM registers its implementation via WithCallFunc, and Closure.Call() retrieves it to execute the closure's bytecode.

External code should use the Callable interface instead of CallFunc directly. Both *Builtin and *Closure implement Callable, providing a uniform way to invoke functions without knowing their concrete type.

func GetCallFunc

func GetCallFunc(ctx context.Context) (CallFunc, bool)

GetCallFunc retrieves the CallFunc from the context. Used internally by Closure.Call() to execute the closure's bytecode. External code should use the Callable interface instead.

type Callable

type Callable interface {
	// Call invokes the callable with the given arguments and returns the result.
	Call(ctx context.Context, args ...Object) (Object, error)
}

Callable is an interface for objects that can be invoked as functions. Both *Builtin and *Closure implement this interface, allowing code to call functions without knowing their concrete type.

For closures, Call() uses the CallFunc stored in the context (set by the VM) to execute the closure's bytecode. For builtins, Call() invokes the wrapped Go function directly.

List methods like Map, Filter, Each, and Reduce accept any Callable, enabling both builtins and closures to be used as callbacks.

type Cell

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

Internal: do not use. Cell is an implementation detail for closure variable capture.

func NewCell

func NewCell(value *Object) *Cell

func (*Cell) Attrs

func (c *Cell) Attrs() []AttrSpec

func (*Cell) Equals

func (c *Cell) Equals(other Object) bool

func (*Cell) GetAttr

func (c *Cell) GetAttr(name string) (Object, bool)

func (*Cell) Inspect

func (c *Cell) Inspect() string

func (*Cell) Interface

func (c *Cell) Interface() interface{}

func (*Cell) IsTruthy

func (c *Cell) IsTruthy() bool

func (*Cell) MarshalJSON

func (c *Cell) MarshalJSON() ([]byte, error)

func (*Cell) RunOperation

func (c *Cell) RunOperation(opType op.BinaryOpType, right Object) (Object, error)

func (*Cell) Set

func (c *Cell) Set(value Object)

func (*Cell) SetAttr

func (c *Cell) SetAttr(name string, value Object) error

func (*Cell) String

func (c *Cell) String() string

func (*Cell) Type

func (c *Cell) Type() Type

func (*Cell) Value

func (c *Cell) Value() Object

type Closure

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

Closure is a runtime function instance with captured variables. It references an immutable bytecode.Function for its signature and code, and holds runtime state like default values (as Objects) and free variables.

func CloneWithCaptures

func CloneWithCaptures(c *Closure, freeVars []*Cell) *Closure

CloneWithCaptures creates a new closure from an existing closure with captured variables. The defaults slice is shared (not copied) since it's immutable after construction. The freeVars slice is copied to ensure the closure owns its captures.

func NewClosure

func NewClosure(fn *bytecode.Function) *Closure

NewClosure creates a Closure from a bytecode.Function template. The defaults are converted from Go types to Object types.

func (*Closure) Attrs

func (f *Closure) Attrs() []AttrSpec

func (*Closure) BytecodeFunction

func (f *Closure) BytecodeFunction() *bytecode.Function

BytecodeFunction returns the underlying bytecode.Function.

func (*Closure) Call

func (f *Closure) Call(ctx context.Context, args ...Object) (Object, error)

func (*Closure) Code

func (f *Closure) Code() *bytecode.Code

Code returns the bytecode for this function's body.

func (*Closure) Default

func (f *Closure) Default(index int) Object

Default returns the default parameter value at the given index.

func (*Closure) DefaultCount

func (f *Closure) DefaultCount() int

DefaultCount returns the number of default parameter values.

func (*Closure) Equals

func (f *Closure) Equals(other Object) bool

func (*Closure) FreeVar

func (f *Closure) FreeVar(index int) *Cell

FreeVar returns the captured variable at the given index.

func (*Closure) FreeVarCount

func (f *Closure) FreeVarCount() int

FreeVarCount returns the number of captured variables.

func (*Closure) GetAttr

func (f *Closure) GetAttr(name string) (Object, bool)

func (*Closure) HasRestParam

func (f *Closure) HasRestParam() bool

HasRestParam returns true if the function has a rest parameter.

func (*Closure) Inspect

func (f *Closure) Inspect() string

func (*Closure) Interface

func (f *Closure) Interface() interface{}

func (*Closure) IsTruthy

func (f *Closure) IsTruthy() bool

func (*Closure) LocalsCount

func (f *Closure) LocalsCount() int

LocalsCount returns the number of local variables.

func (*Closure) MarshalJSON

func (f *Closure) MarshalJSON() ([]byte, error)

func (*Closure) Name

func (f *Closure) Name() string

Name returns the function name (delegates to bytecode.Function).

func (*Closure) Parameter

func (f *Closure) Parameter(index int) string

Parameter returns the parameter name at the given index (delegates to bytecode.Function).

func (*Closure) ParameterCount

func (f *Closure) ParameterCount() int

ParameterCount returns the number of parameters (delegates to bytecode.Function).

func (*Closure) RequiredArgsCount

func (f *Closure) RequiredArgsCount() int

RequiredArgsCount returns the minimum number of arguments required.

func (*Closure) RestParam

func (f *Closure) RestParam() string

RestParam returns the rest parameter name (delegates to bytecode.Function).

func (*Closure) RunOperation

func (f *Closure) RunOperation(opType op.BinaryOpType, right Object) (Object, error)

func (*Closure) SetAttr

func (f *Closure) SetAttr(name string, value Object) error

func (*Closure) String

func (f *Closure) String() string

func (*Closure) Type

func (f *Closure) Type() Type

type Color

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

func NewColor

func NewColor(c color.Color) *Color

func (*Color) Attrs

func (c *Color) Attrs() []AttrSpec

func (*Color) Equals

func (c *Color) Equals(other Object) bool

func (*Color) GetAttr

func (c *Color) GetAttr(name string) (Object, bool)

func (*Color) Inspect

func (c *Color) Inspect() string

func (*Color) Interface

func (c *Color) Interface() interface{}

func (*Color) IsTruthy

func (c *Color) IsTruthy() bool

func (*Color) MarshalJSON

func (c *Color) MarshalJSON() ([]byte, error)

func (*Color) RunOperation

func (c *Color) RunOperation(opType op.BinaryOpType, right Object) (Object, error)

func (*Color) SetAttr

func (c *Color) SetAttr(name string, value Object) error

func (*Color) String

func (c *Color) String() string

func (*Color) Type

func (c *Color) Type() Type

func (*Color) Value

func (c *Color) Value() color.Color

type Comparable

type Comparable interface {
	Compare(other Object) (int, error)
}

Comparable is an interface used to compare two objects.

-1 if this < other
 0 if this == other
 1 if this > other

type Container

type Container interface {
	Enumerable

	// GetItem implements the [key] operator for a container type.
	GetItem(key Object) (Object, *Error)

	// GetSlice implements the [start:stop] operator for a container type.
	GetSlice(s Slice) (Object, *Error)

	// SetItem implements the [key] = value operator for a container type.
	SetItem(key, value Object) *Error

	// DelItem implements the del [key] operator for a container type.
	DelItem(key Object) *Error

	// Contains returns true if the given item is found in this container.
	Contains(item Object) *Bool

	// Len returns the number of items in this container.
	Len() *Int
}

type DynamicAttr

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

DynamicAttr is an Object that represents an attribute that can be dynamically resolved to a concrete Object at runtime.

func NewDynamicAttr

func NewDynamicAttr(name string, fn ResolveAttrFunc) *DynamicAttr

func (*DynamicAttr) Attrs

func (d *DynamicAttr) Attrs() []AttrSpec

func (*DynamicAttr) Equals

func (d *DynamicAttr) Equals(other Object) bool

func (*DynamicAttr) GetAttr

func (d *DynamicAttr) GetAttr(name string) (Object, bool)

func (*DynamicAttr) Inspect

func (d *DynamicAttr) Inspect() string

func (*DynamicAttr) Interface

func (d *DynamicAttr) Interface() interface{}

func (*DynamicAttr) IsTruthy

func (d *DynamicAttr) IsTruthy() bool

func (*DynamicAttr) MarshalJSON

func (d *DynamicAttr) MarshalJSON() ([]byte, error)

func (*DynamicAttr) ResolveAttr

func (d *DynamicAttr) ResolveAttr(ctx context.Context, name string) (Object, error)

func (*DynamicAttr) RunOperation

func (d *DynamicAttr) RunOperation(opType op.BinaryOpType, right Object) (Object, error)

func (*DynamicAttr) SetAttr

func (d *DynamicAttr) SetAttr(name string, value Object) error

func (*DynamicAttr) String

func (d *DynamicAttr) String() string

func (*DynamicAttr) Type

func (d *DynamicAttr) Type() Type

type Enumerable

type Enumerable interface {
	Enumerate(ctx context.Context, fn func(key, value Object) bool)
}

Enumerable is an interface for types that can be iterated with a callback. The callback receives the key and value for each element. Return false to stop.

type Error

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

Error wraps a Go error interface and implements Object.

Errors are values. You can create them, inspect them, store them, and pass them around like any other value. Use "throw" to trigger exception handling.

Example:

let err = error("something went wrong")
print(err.message())     // inspect
print(`Error: ${err}`)   // stringify
throw err                // only throw triggers exception handling

func ArgsErrorf

func ArgsErrorf(format string, args ...interface{}) *Error

ArgsErrorf returns a Risor Error object containing an arguments error.

func AsError

func AsError(obj Object) (*Error, error)

func Errorf

func Errorf(format string, a ...interface{}) *Error

func EvalErrorf

func EvalErrorf(format string, args ...interface{}) *Error

EvalErrorf returns a Risor Error object containing an eval error.

func IndexErrorf

func IndexErrorf(format string, args ...interface{}) *Error

IndexErrorf returns a Risor Error object containing an index error.

func NewArgsError

func NewArgsError(fn string, takes, given int) *Error

func NewArgsRangeError

func NewArgsRangeError(fn string, takesMin, takesMax, given int) *Error

func NewError

func NewError(err error) *Error

func NewErrorFromStructured

func NewErrorFromStructured(se *StructuredError) *Error

NewErrorFromStructured creates a new Error from a StructuredError.

func Require

func Require(funcName string, count int, args []Object) *Error

func RequireRange

func RequireRange(funcName string, min, max int, args []Object) *Error

func Sort

func Sort(items []Object) *Error

Sort a list in place. If the list contains a non-comparable object, an error is returned.

func TypeErrorf

func TypeErrorf(format string, args ...interface{}) *Error

TypeErrorf returns a Risor Error object containing a type error.

func ValueErrorf

func ValueErrorf(format string, args ...interface{}) *Error

ValueErrorf returns a Risor Error object containing a value error.

func (*Error) Attrs

func (e *Error) Attrs() []AttrSpec

func (*Error) Compare

func (e *Error) Compare(other Object) (int, error)

func (*Error) Equals

func (e *Error) Equals(other Object) bool

func (*Error) Error

func (e *Error) Error() string

func (*Error) FriendlyErrorMessage

func (e *Error) FriendlyErrorMessage() string

FriendlyErrorMessage returns a human-friendly error message if the error has structured data, otherwise returns the standard error string.

func (*Error) GetAttr

func (e *Error) GetAttr(name string) (Object, bool)

func (*Error) Inspect

func (e *Error) Inspect() string

func (*Error) Interface

func (e *Error) Interface() interface{}

func (*Error) IsTruthy

func (e *Error) IsTruthy() bool

func (*Error) MarshalJSON

func (e *Error) MarshalJSON() ([]byte, error)

func (*Error) Message

func (e *Error) Message() *String

func (*Error) RunOperation

func (e *Error) RunOperation(opType op.BinaryOpType, right Object) (Object, error)

func (*Error) SetAttr

func (e *Error) SetAttr(name string, value Object) error

func (*Error) String

func (e *Error) String() string

func (*Error) Structured

func (e *Error) Structured() *StructuredError

Structured returns the underlying StructuredError if present.

func (*Error) Type

func (e *Error) Type() Type

func (*Error) Unwrap

func (e *Error) Unwrap() error

func (*Error) Value

func (e *Error) Value() error

type ErrorKind

type ErrorKind = errors.ErrorKind

Re-export types from errors package for convenience

type EvalError

type EvalError = errors.EvalError

Re-export types from errors package for convenience

type Float

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

Float wraps float64 and implements Object and Hashable interfaces.

func NewFloat

func NewFloat(value float64) *Float

func (*Float) Attrs

func (f *Float) Attrs() []AttrSpec

func (*Float) Compare

func (f *Float) Compare(other Object) (int, error)

func (*Float) Equals

func (f *Float) Equals(other Object) bool

func (*Float) GetAttr

func (f *Float) GetAttr(name string) (Object, bool)

func (*Float) Inspect

func (f *Float) Inspect() string

func (*Float) Interface

func (f *Float) Interface() interface{}

func (*Float) IsTruthy

func (f *Float) IsTruthy() bool

func (*Float) MarshalJSON

func (f *Float) MarshalJSON() ([]byte, error)

func (*Float) RunOperation

func (f *Float) RunOperation(opType op.BinaryOpType, right Object) (Object, error)

func (*Float) SetAttr

func (f *Float) SetAttr(name string, value Object) error

func (*Float) String

func (f *Float) String() string

func (*Float) Type

func (f *Float) Type() Type

func (*Float) Value

func (f *Float) Value() float64

type FriendlyError

type FriendlyError = errors.FriendlyError

Re-export types from errors package for convenience

type FromGoFunc

type FromGoFunc func(v any) (Object, error)

FromGoFunc converts a Go value to a Risor Object.

type FuncSpec

type FuncSpec struct {
	// Name is the function name (e.g., "len", "sorted").
	Name string

	// Doc is a short description of what the function does.
	Doc string

	// Args lists parameter names (e.g., ["obj"] or ["items", "key"]).
	Args []string

	// Returns describes the return type (e.g., "int", "list").
	Returns string

	// Example shows a short usage example (optional).
	Example string
}

FuncSpec describes a builtin function. This provides metadata for introspection, documentation, and tooling.

type GoFunc

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

GoFunc wraps an arbitrary Go function for use in Risor. It uses reflection to handle argument conversion and function calls.

func NewGoFunc

func NewGoFunc(fn reflect.Value, name string, registry *TypeRegistry) *GoFunc

NewGoFunc creates a new GoFunc wrapping the given Go function. The registry is used for converting arguments and return values.

func (*GoFunc) Attrs

func (g *GoFunc) Attrs() []AttrSpec

func (*GoFunc) Call

func (g *GoFunc) Call(ctx context.Context, args ...Object) (result Object, err error)

Call invokes the wrapped Go function with the given Risor arguments. It handles context injection, argument conversion, variadic args, and error returns.

func (*GoFunc) Equals

func (g *GoFunc) Equals(other Object) bool

func (*GoFunc) GetAttr

func (g *GoFunc) GetAttr(name string) (Object, bool)

func (*GoFunc) Inspect

func (g *GoFunc) Inspect() string

func (*GoFunc) Interface

func (g *GoFunc) Interface() any

func (*GoFunc) IsTruthy

func (g *GoFunc) IsTruthy() bool

func (*GoFunc) Name

func (g *GoFunc) Name() string

Name returns the name of the wrapped function.

func (*GoFunc) RunOperation

func (g *GoFunc) RunOperation(opType op.BinaryOpType, right Object) (Object, error)

func (*GoFunc) SetAttr

func (g *GoFunc) SetAttr(name string, value Object) error

func (*GoFunc) String

func (g *GoFunc) String() string

func (*GoFunc) Type

func (g *GoFunc) Type() Type

type GoStruct

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

GoStruct wraps a Go struct for use in Risor. It exposes struct fields and methods via GetAttr/SetAttr.

func NewGoStruct

func NewGoStruct(ptrVal reflect.Value, registry *TypeRegistry) *GoStruct

NewGoStruct creates a new GoStruct wrapping the given Go struct pointer. The value must be a pointer to a struct.

func (*GoStruct) Attrs

func (g *GoStruct) Attrs() []AttrSpec

func (*GoStruct) CallMethod

func (g *GoStruct) CallMethod(ctx context.Context, name string, args ...Object) (Object, error)

CallMethod calls a method on the struct by name with the given arguments. This is a convenience method that combines GetAttr and Call.

func (*GoStruct) Equals

func (g *GoStruct) Equals(other Object) bool

func (*GoStruct) GetAttr

func (g *GoStruct) GetAttr(name string) (Object, bool)

func (*GoStruct) Inspect

func (g *GoStruct) Inspect() string

func (*GoStruct) Interface

func (g *GoStruct) Interface() any

func (*GoStruct) IsTruthy

func (g *GoStruct) IsTruthy() bool

func (*GoStruct) RunOperation

func (g *GoStruct) RunOperation(opType op.BinaryOpType, right Object) (Object, error)

func (*GoStruct) SetAttr

func (g *GoStruct) SetAttr(name string, value Object) error

func (*GoStruct) String

func (g *GoStruct) String() string

func (*GoStruct) StructType

func (g *GoStruct) StructType() reflect.Type

StructType returns the type of the underlying struct.

func (*GoStruct) Type

func (g *GoStruct) Type() Type

func (*GoStruct) Value

func (g *GoStruct) Value() reflect.Value

Value returns the underlying Go struct pointer as a reflect.Value.

type IndexError

type IndexError = errors.IndexError

Re-export types from errors package for convenience

type Int

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

Int wraps int64 and implements Object and Hashable interfaces. Int is immutable: the value is set at construction and cannot be changed.

func NewInt

func NewInt(value int64) *Int

NewInt returns an *Int for the given value. Small integers (-10 to 255) are returned from a pre-allocated cache, so the same pointer may be returned for equal values. This is safe because Int is immutable.

func (*Int) Attrs

func (i *Int) Attrs() []AttrSpec

func (*Int) Compare

func (i *Int) Compare(other Object) (int, error)

func (*Int) Equals

func (i *Int) Equals(other Object) bool

func (*Int) GetAttr

func (i *Int) GetAttr(name string) (Object, bool)

func (*Int) Inspect

func (i *Int) Inspect() string

func (*Int) Interface

func (i *Int) Interface() interface{}

func (*Int) IsTruthy

func (i *Int) IsTruthy() bool

func (*Int) MarshalJSON

func (i *Int) MarshalJSON() ([]byte, error)

func (*Int) RunOperation

func (i *Int) RunOperation(opType op.BinaryOpType, right Object) (Object, error)

func (*Int) SetAttr

func (i *Int) SetAttr(name string, value Object) error

func (*Int) String

func (i *Int) String() string

func (*Int) Type

func (i *Int) Type() Type

func (*Int) Value

func (i *Int) Value() int64

type Introspectable

type Introspectable interface {
	// Attrs returns the attribute specifications for this object.
	Attrs() []AttrSpec
}

Introspectable is implemented by objects that can describe their attributes. This enables tooling like :methods, risor doc, and autocomplete.

type Iter

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

Iter is a lazy iterator that wraps a generator function. It implements Enumerable so it can be used with spread, list(), etc.

func NewIter

func NewIter(desc string, gen func(ctx context.Context, fn func(key, value Object) bool)) *Iter

NewIter creates a new iterator with a description and generator function.

func NewMapItemIter

func NewMapItemIter(m *Map) *Iter

NewMapItemIter creates an iterator over map [key, value] pairs.

func NewMapKeyIter

func NewMapKeyIter(m *Map) *Iter

NewMapKeyIter creates an iterator over map keys.

func NewMapValueIter

func NewMapValueIter(m *Map) *Iter

NewMapValueIter creates an iterator over map values.

func (*Iter) Attrs

func (it *Iter) Attrs() []AttrSpec

func (*Iter) Enumerate

func (it *Iter) Enumerate(ctx context.Context, fn func(key, value Object) bool)

Enumerate implements Enumerable, allowing Iter to be used with spread, list(), etc.

func (*Iter) Equals

func (it *Iter) Equals(other Object) bool

func (*Iter) GetAttr

func (it *Iter) GetAttr(name string) (Object, bool)

func (*Iter) Inspect

func (it *Iter) Inspect() string

func (*Iter) Interface

func (it *Iter) Interface() any

func (*Iter) IsTruthy

func (it *Iter) IsTruthy() bool

func (*Iter) RunOperation

func (it *Iter) RunOperation(opType op.BinaryOpType, right Object) (Object, error)

func (*Iter) SetAttr

func (it *Iter) SetAttr(name string, value Object) error

func (*Iter) String

func (it *Iter) String() string

func (*Iter) Type

func (it *Iter) Type() Type

type List

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

List of objects

func AsList

func AsList(obj Object) (*List, error)

func NewList

func NewList(items []Object) *List

func NewStringList

func NewStringList(s []string) *List

func (*List) Append

func (ls *List) Append(obj Object)

Append adds an item at the end of the list.

func (*List) Attrs

func (ls *List) Attrs() []AttrSpec

func (*List) Clear

func (ls *List) Clear()

Clear removes all the items from the list.

func (*List) Compare

func (ls *List) Compare(other Object) (int, error)

func (*List) Contains

func (ls *List) Contains(item Object) *Bool

Contains returns true if the given item is found in this container.

func (*List) Copy

func (ls *List) Copy() *List

Copy returns a shallow copy of the list.

func (*List) Count

func (ls *List) Count(obj Object) int64

Count returns the number of items with the specified value.

func (*List) DelItem

func (ls *List) DelItem(key Object) *Error

DelItem implements the del [key] operator for a container type.

func (*List) Each

func (ls *List) Each(ctx context.Context, fn Object) (Object, error)

func (*List) Enumerate

func (ls *List) Enumerate(ctx context.Context, fn func(key, value Object) bool)

func (*List) Equals

func (ls *List) Equals(other Object) bool

func (*List) Extend

func (ls *List) Extend(other *List)

Extend adds the items of a list to the end of the current list.

func (*List) Filter

func (ls *List) Filter(ctx context.Context, fn Object) (Object, error)

func (*List) GetAttr

func (ls *List) GetAttr(name string) (Object, bool)

func (*List) GetItem

func (ls *List) GetItem(key Object) (Object, *Error)

func (*List) GetSlice

func (ls *List) GetSlice(s Slice) (Object, *Error)

GetSlice implements the [start:stop] operator for a container type.

func (*List) Index

func (ls *List) Index(obj Object) int64

Index returns the index of the first item with the specified value.

func (*List) Insert

func (ls *List) Insert(index int64, obj Object)

Insert adds an item at the specified position.

func (*List) Inspect

func (ls *List) Inspect() string

func (*List) Interface

func (ls *List) Interface() interface{}

func (*List) IsTruthy

func (ls *List) IsTruthy() bool

func (*List) Keys

func (ls *List) Keys() Object

func (*List) Len

func (ls *List) Len() *Int

Len returns the number of items in this container.

func (*List) Map

func (ls *List) Map(ctx context.Context, fn Object) (Object, error)

func (*List) MarshalJSON

func (ls *List) MarshalJSON() ([]byte, error)

func (*List) Pop

func (ls *List) Pop(index int64) (Object, error)

Pop removes the item at the specified position.

func (*List) Reduce

func (ls *List) Reduce(ctx context.Context, initial Object, fn Object) (Object, error)

func (*List) Remove

func (ls *List) Remove(obj Object)

Remove removes the first item with the specified value.

func (*List) Reverse

func (ls *List) Reverse()

Reverse reverses the order of the list.

func (*List) Reversed

func (ls *List) Reversed() *List

func (*List) RunOperation

func (ls *List) RunOperation(opType op.BinaryOpType, right Object) (Object, error)

func (*List) SetAttr

func (ls *List) SetAttr(name string, value Object) error

func (*List) SetItem

func (ls *List) SetItem(key, value Object) *Error

SetItem implements the [key] = value operator for a container type.

func (*List) Size

func (ls *List) Size() int

func (*List) String

func (ls *List) String() string

func (*List) Type

func (ls *List) Type() Type

func (*List) Value

func (ls *List) Value() []Object

type Map

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

func AsMap

func AsMap(obj Object) (*Map, error)

func NewMap

func NewMap(m map[string]Object) *Map

func (*Map) Attrs

func (m *Map) Attrs() []AttrSpec

func (*Map) Clear

func (m *Map) Clear()

func (*Map) Contains

func (m *Map) Contains(key Object) *Bool

Contains returns true if the given item is found in this container.

func (*Map) Copy

func (m *Map) Copy() *Map

func (*Map) DelItem

func (m *Map) DelItem(key Object) *Error

DelItem deletes the item with the given key from the map.

func (*Map) Delete

func (m *Map) Delete(key string) Object

func (*Map) Enumerate

func (m *Map) Enumerate(ctx context.Context, fn func(key, value Object) bool)

func (*Map) Equals

func (m *Map) Equals(other Object) bool

func (*Map) Get

func (m *Map) Get(key string) Object

func (*Map) GetAttr

func (m *Map) GetAttr(name string) (Object, bool)

func (*Map) GetItem

func (m *Map) GetItem(key Object) (Object, *Error)

func (*Map) GetSlice

func (m *Map) GetSlice(s Slice) (Object, *Error)

GetSlice implements the [start:stop] operator for a container type.

func (*Map) GetWithDefault

func (m *Map) GetWithDefault(key string, defaultValue Object) Object

func (*Map) GetWithObject

func (m *Map) GetWithObject(key *String) Object

func (*Map) Inspect

func (m *Map) Inspect() string

func (*Map) Interface

func (m *Map) Interface() interface{}

func (*Map) IsTruthy

func (m *Map) IsTruthy() bool

func (*Map) Keys

func (m *Map) Keys() *List

func (*Map) Len

func (m *Map) Len() *Int

Len returns the number of items in this container.

func (*Map) ListItems

func (m *Map) ListItems() *List

func (*Map) MarshalJSON

func (m *Map) MarshalJSON() ([]byte, error)

func (*Map) Pop

func (m *Map) Pop(key string, def Object) Object

func (*Map) RunOperation

func (m *Map) RunOperation(opType op.BinaryOpType, right Object) (Object, error)

func (*Map) Set

func (m *Map) Set(key string, value Object)

func (*Map) SetAttr

func (m *Map) SetAttr(name string, value Object) error

func (*Map) SetDefault

func (m *Map) SetDefault(key string, value Object) Object

func (*Map) SetItem

func (m *Map) SetItem(key, value Object) *Error

SetItem assigns a value to the given key in the map.

func (*Map) Size

func (m *Map) Size() int

func (*Map) SortedKeys

func (m *Map) SortedKeys() []string

func (*Map) String

func (m *Map) String() string

func (*Map) StringKeys

func (m *Map) StringKeys() []string

func (*Map) Type

func (m *Map) Type() Type

func (*Map) Update

func (m *Map) Update(other *Map)

func (*Map) Value

func (m *Map) Value() map[string]Object

func (*Map) Values

func (m *Map) Values() *List

type MethodBuilder

type MethodBuilder[T any] = AttrBuilder[T]

Aliases for backward compatibility during migration

type MethodDef

type MethodDef[T any] = AttrDef[T]

Aliases for backward compatibility during migration

type MethodRegistry

type MethodRegistry[T any] = AttrRegistry[T]

Aliases for backward compatibility during migration

type Module

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

func NewBuiltinsModule

func NewBuiltinsModule(name string, contents map[string]Object, callableOption ...BuiltinFunction) *Module

func NewModule

func NewModule(name string, code *bytecode.Code) *Module

func (*Module) Attrs

func (m *Module) Attrs() []AttrSpec

func (*Module) Call

func (m *Module) Call(ctx context.Context, args ...Object) (Object, error)

func (*Module) Code

func (m *Module) Code() *bytecode.Code

func (*Module) Compare

func (m *Module) Compare(other Object) (int, error)

func (*Module) Equals

func (m *Module) Equals(other Object) bool

func (*Module) GetAttr

func (m *Module) GetAttr(name string) (Object, bool)

func (*Module) Inspect

func (m *Module) Inspect() string

func (*Module) Interface

func (m *Module) Interface() interface{}

func (*Module) IsTruthy

func (m *Module) IsTruthy() bool

func (*Module) MarshalJSON

func (m *Module) MarshalJSON() ([]byte, error)

func (*Module) Name

func (m *Module) Name() *String

func (*Module) Override

func (m *Module) Override(name string, value Object) error

Override provides a mechanism to modify module attributes after loading. Whether or not this is exposed to Risor scripts changes the security posture of reusing modules. By default, this is not exposed to scripting. Overriding with a value of nil is equivalent to deleting the attribute.

func (*Module) RunOperation

func (m *Module) RunOperation(opType op.BinaryOpType, right Object) (Object, error)

func (*Module) SetAttr

func (m *Module) SetAttr(name string, value Object) error

func (*Module) String

func (m *Module) String() string

func (*Module) Type

func (m *Module) Type() Type

func (*Module) UseGlobals

func (m *Module) UseGlobals(globals []Object)

type NilType

type NilType struct{}

func (*NilType) Attrs

func (n *NilType) Attrs() []AttrSpec

func (*NilType) Compare

func (n *NilType) Compare(other Object) (int, error)

func (*NilType) Equals

func (n *NilType) Equals(other Object) bool

func (*NilType) GetAttr

func (n *NilType) GetAttr(name string) (Object, bool)

func (*NilType) Inspect

func (n *NilType) Inspect() string

func (*NilType) Interface

func (n *NilType) Interface() interface{}

func (*NilType) IsTruthy

func (n *NilType) IsTruthy() bool

func (*NilType) MarshalJSON

func (n *NilType) MarshalJSON() ([]byte, error)

func (*NilType) RunOperation

func (n *NilType) RunOperation(opType op.BinaryOpType, right Object) (Object, error)

func (*NilType) SetAttr

func (n *NilType) SetAttr(name string, value Object) error

func (*NilType) String

func (n *NilType) String() string

func (*NilType) Type

func (n *NilType) Type() Type

type Object

type Object interface {
	// Type of the object.
	Type() Type

	// Inspect returns a string representation of the given object.
	Inspect() string

	// Interface converts the given object to a native Go value.
	Interface() interface{}

	// Returns true if the given object is equal to this object.
	Equals(other Object) bool

	// Attrs returns the attribute specifications for this object type.
	// Used for introspection, documentation, and tooling (autocomplete, etc.).
	// Returns nil for types with no attributes.
	Attrs() []AttrSpec

	// GetAttr returns the attribute with the given name from this object.
	GetAttr(name string) (Object, bool)

	// SetAttr sets the attribute with the given name on this object.
	SetAttr(name string, value Object) error

	// IsTruthy returns true if the object is considered "truthy".
	IsTruthy() bool

	// RunOperation runs an operation on this object with the given
	// right-hand side object.
	RunOperation(opType op.BinaryOpType, right Object) (Object, error)
}

Object is the interface that all object types in Risor must implement.

func BinaryOp

func BinaryOp(opType op.BinaryOpType, a, b Object) (Object, error)

BinaryOp performs a binary operation on two objects, given an operator.

func Compare

func Compare(opType op.CompareOpType, a, b Object) (Object, error)

Compare two objects using the given comparison operator. An error is returned if either of the objects is not comparable.

func FromGoType

func FromGoType(obj interface{}) Object

FromGoType converts a Go value to a Risor Object using the default registry. On error, returns an *Error object (for backward compatibility). Prefer using TypeRegistry.FromGo for new code.

func RuneToObject

func RuneToObject(r rune) Object

RuneToObject converts a rune to a Risor String object.

type Partial

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

Partial is a partially applied function

func NewPartial

func NewPartial(fn Object, args []Object) *Partial

func (*Partial) Args

func (p *Partial) Args() []Object

func (*Partial) Attrs

func (p *Partial) Attrs() []AttrSpec

func (*Partial) Equals

func (p *Partial) Equals(other Object) bool

func (*Partial) Function

func (p *Partial) Function() Object

func (*Partial) GetAttr

func (p *Partial) GetAttr(name string) (Object, bool)

func (*Partial) Inspect

func (p *Partial) Inspect() string

func (*Partial) Interface

func (p *Partial) Interface() interface{}

func (*Partial) IsTruthy

func (p *Partial) IsTruthy() bool

func (*Partial) MarshalJSON

func (p *Partial) MarshalJSON() ([]byte, error)

func (*Partial) RunOperation

func (p *Partial) RunOperation(opType op.BinaryOpType, right Object) (Object, error)

func (*Partial) SetAttr

func (p *Partial) SetAttr(name string, value Object) error

func (*Partial) Type

func (p *Partial) Type() Type

type Range

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

Range represents a lazy sequence of integers, similar to Python's range. It stores start, stop, and step values and generates integers on demand.

func NewRange

func NewRange(start, stop, step int64) *Range

NewRange creates a new Range object. Panics if step is zero.

func (*Range) Attrs

func (r *Range) Attrs() []AttrSpec

func (*Range) Each added in v2.1.0

func (r *Range) Each(ctx context.Context, fn Object) (Object, error)

func (*Range) Enumerate

func (r *Range) Enumerate(ctx context.Context, fn func(key, value Object) bool)

Enumerate iterates over the range values.

func (*Range) Equals

func (r *Range) Equals(other Object) bool

func (*Range) Filter added in v2.1.0

func (r *Range) Filter(ctx context.Context, fn Object) (Object, error)

func (*Range) GetAttr

func (r *Range) GetAttr(name string) (Object, bool)

func (*Range) Inspect

func (r *Range) Inspect() string

func (*Range) Interface

func (r *Range) Interface() any

func (*Range) IsTruthy

func (r *Range) IsTruthy() bool

func (*Range) Map added in v2.1.0

func (r *Range) Map(ctx context.Context, fn Object) (Object, error)

func (*Range) RunOperation

func (r *Range) RunOperation(opType op.BinaryOpType, right Object) (Object, error)

func (*Range) SetAttr

func (r *Range) SetAttr(name string, value Object) error

func (*Range) Start

func (r *Range) Start() int64

Start returns the start value.

func (*Range) Step

func (r *Range) Step() int64

Step returns the step value.

func (*Range) Stop

func (r *Range) Stop() int64

Stop returns the stop value.

func (*Range) Type

func (r *Range) Type() Type

type RegistryBuilder

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

RegistryBuilder constructs a TypeRegistry with custom converters.

func NewRegistryBuilder

func NewRegistryBuilder() *RegistryBuilder

NewRegistryBuilder creates a builder starting from the default registry.

func (*RegistryBuilder) Build

func (b *RegistryBuilder) Build() *TypeRegistry

Build creates an immutable TypeRegistry.

func (*RegistryBuilder) RegisterFromGo

func (b *RegistryBuilder) RegisterFromGo(typ reflect.Type, fn FromGoFunc) *RegistryBuilder

RegisterFromGo adds a converter for Go -> Risor.

func (*RegistryBuilder) RegisterToGo

func (b *RegistryBuilder) RegisterToGo(typ reflect.Type, fn ToGoFunc) *RegistryBuilder

RegisterToGo adds a converter for Risor -> Go.

type ResolveAttrFunc

type ResolveAttrFunc func(ctx context.Context, name string) (Object, error)

type RisorValuer

type RisorValuer interface {
	RisorValue() Object
}

RisorValuer is implemented by Go types that know how to become Risor Objects. When converting Go values to Risor Objects, the TypeRegistry checks for this interface first, allowing custom types to define their own conversion.

type Slice

type Slice struct {
	Start Object
	Stop  Object
}

Slice is used to specify a range or slice of items in a container.

type SourceLocation

type SourceLocation = errors.SourceLocation

Re-export types from errors package for convenience

type StackFrame

type StackFrame = errors.StackFrame

Re-export types from errors package for convenience

type String

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

func NewString

func NewString(s string) *String

func (*String) Attrs

func (s *String) Attrs() []AttrSpec

func (*String) Compare

func (s *String) Compare(other Object) (int, error)

func (*String) Contains

func (s *String) Contains(obj Object) *Bool

func (*String) Count

func (s *String) Count(obj Object) (Object, error)

func (*String) DelItem

func (s *String) DelItem(key Object) *Error

func (*String) Enumerate

func (s *String) Enumerate(ctx context.Context, fn func(key, value Object) bool)

func (*String) Equals

func (s *String) Equals(other Object) bool

func (*String) Fields

func (s *String) Fields() Object

func (*String) GetAttr

func (s *String) GetAttr(name string) (Object, bool)

func (*String) GetItem

func (s *String) GetItem(key Object) (Object, *Error)

func (*String) GetSlice

func (s *String) GetSlice(slice Slice) (Object, *Error)

func (*String) HasPrefix

func (s *String) HasPrefix(obj Object) (Object, error)

func (*String) HasSuffix

func (s *String) HasSuffix(obj Object) (Object, error)

func (*String) Index

func (s *String) Index(obj Object) (Object, error)

func (*String) Inspect

func (s *String) Inspect() string

func (*String) Interface

func (s *String) Interface() interface{}

func (*String) IsTruthy

func (s *String) IsTruthy() bool

func (*String) Join

func (s *String) Join(obj Object) (Object, error)

func (*String) LastIndex

func (s *String) LastIndex(obj Object) (Object, error)

func (*String) Len

func (s *String) Len() *Int

func (*String) MarshalJSON

func (s *String) MarshalJSON() ([]byte, error)

func (*String) Repeat

func (s *String) Repeat(obj Object) (Object, error)

func (*String) ReplaceAll

func (s *String) ReplaceAll(old, new Object) (Object, error)

func (*String) Reversed

func (s *String) Reversed() *String

func (*String) RunOperation

func (s *String) RunOperation(opType op.BinaryOpType, right Object) (Object, error)

func (*String) Runes

func (s *String) Runes() []Object

func (*String) SetAttr

func (s *String) SetAttr(name string, value Object) error

func (*String) SetItem

func (s *String) SetItem(key, value Object) *Error

func (*String) Split

func (s *String) Split(obj Object) (Object, error)

func (*String) String

func (s *String) String() string

func (*String) ToLower

func (s *String) ToLower() Object

func (*String) ToUpper

func (s *String) ToUpper() Object

func (*String) Trim

func (s *String) Trim(obj Object) (Object, error)

func (*String) TrimPrefix

func (s *String) TrimPrefix(obj Object) (Object, error)

func (*String) TrimSpace

func (s *String) TrimSpace() Object

func (*String) TrimSuffix

func (s *String) TrimSuffix(obj Object) (Object, error)

func (*String) Type

func (s *String) Type() Type

func (*String) Value

func (s *String) Value() string

type StructuredError

type StructuredError = errors.StructuredError

Re-export types from errors package for convenience

type Time

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

func NewTime

func NewTime(t time.Time) *Time

func (*Time) AddDate

func (t *Time) AddDate(ctx context.Context, args ...Object) (Object, error)

func (*Time) After

func (t *Time) After(ctx context.Context, args ...Object) (Object, error)

func (*Time) Attrs

func (t *Time) Attrs() []AttrSpec

func (*Time) Before

func (t *Time) Before(ctx context.Context, args ...Object) (Object, error)

func (*Time) Compare

func (t *Time) Compare(other Object) (int, error)

func (*Time) Equals

func (t *Time) Equals(other Object) bool

func (*Time) Format

func (t *Time) Format(ctx context.Context, args ...Object) (Object, error)

func (*Time) GetAttr

func (t *Time) GetAttr(name string) (Object, bool)

func (*Time) Inspect

func (t *Time) Inspect() string

func (*Time) Interface

func (t *Time) Interface() interface{}

func (*Time) IsTruthy

func (t *Time) IsTruthy() bool

func (*Time) MarshalJSON

func (t *Time) MarshalJSON() ([]byte, error)

func (*Time) RunOperation

func (t *Time) RunOperation(opType op.BinaryOpType, right Object) (Object, error)

func (*Time) SetAttr

func (t *Time) SetAttr(name string, value Object) error

func (*Time) String

func (t *Time) String() string

func (*Time) Type

func (t *Time) Type() Type

func (*Time) UTC

func (t *Time) UTC(ctx context.Context, args ...Object) (Object, error)

func (*Time) Unix

func (t *Time) Unix(ctx context.Context, args ...Object) (Object, error)

func (*Time) Value

func (t *Time) Value() time.Time

type ToGoFunc

type ToGoFunc func(obj Object, targetType reflect.Type) (any, error)

ToGoFunc converts a Risor Object to a Go value of a specific type.

type Type

type Type string

Type of an object as a string.

const (
	BOOL          Type = "bool"
	BUILTIN       Type = "builtin"
	BYTE          Type = "byte"
	BYTES         Type = "bytes"
	CELL          Type = "cell"
	COLOR         Type = "color"
	COMPLEX       Type = "complex"
	COMPLEX_SLICE Type = "complex_slice"
	DYNAMIC_ATTR  Type = "dynamic_attr"
	ERROR         Type = "error"
	FLOAT         Type = "float"
	FUNCTION      Type = "function"
	INT           Type = "int"
	LIST          Type = "list"
	MAP           Type = "map"
	MODULE        Type = "module"
	NIL           Type = "null"
	PARTIAL       Type = "partial"
	RANGE         Type = "range"
	RESULT        Type = "result"
	STRING        Type = "string"
	TIME          Type = "time"
	GOFUNC        Type = "go_func"
	GOSTRUCT      Type = "go_struct"
)

Type constants

const ITER Type = "iter"

ITER type constant

type TypeError

type TypeError = errors.TypeError

Re-export types from errors package for convenience

type TypeRegistry

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

TypeRegistry handles conversion between Go values and Risor Objects. It is immutable after construction and safe for concurrent use.

func DefaultRegistry

func DefaultRegistry() *TypeRegistry

DefaultRegistry returns a TypeRegistry with converters for all built-in types.

func (*TypeRegistry) FromGo

func (r *TypeRegistry) FromGo(v any) (Object, error)

FromGo converts a Go value to a Risor Object.

func (*TypeRegistry) ToGo

func (r *TypeRegistry) ToGo(obj Object, targetType reflect.Type) (any, error)

ToGo converts a Risor Object to a Go value of the specified type.

type TypeSpec

type TypeSpec struct {
	// Name is the type name (e.g., "string", "list").
	Name string

	// Doc is a description of the type.
	Doc string

	// Attrs lists the attributes/methods available on this type.
	Attrs []AttrSpec
}

TypeSpec describes a Risor type.

func TypeDoc

func TypeDoc(t Type) (TypeSpec, bool)

TypeDoc returns documentation for a specific type.

func TypeDocs

func TypeDocs() []TypeSpec

TypeDocs returns documentation for all registered types.

type ValueError

type ValueError = errors.ValueError

Re-export types from errors package for convenience

Jump to

Keyboard shortcuts

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