litecode

package module
v0.0.0-...-e4d66d4 Latest Latest
Warning

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

Go to latest
Published: Mar 4, 2025 License: Unlicense Imports: 16 Imported by: 0

README

Deterministic Luau virtual machine and library toolkit

Documentation

Overview

Package litecode is a deterministic Luau virtual machine and standard library toolkit.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ToString

func ToString(a any) string

ToString returns a string representation of any value.

Types

type Args

type Args struct {
	// Co is the coroutine that the function is running.
	Co *Coroutine
	// List is the list of all arguments passed to the function.
	List []any
	// contains filtered or unexported fields
}

Args represents the arguments passed to a user-defined native function.

A number of helper functions are provided to extract arguments from the list. If these functions fail to extract the argument, the coroutine yields an invalid/missing argument error.

func (*Args) CheckNextArg

func (a *Args) CheckNextArg()

CheckNextArg ensures that there is at least one more argument to be read. If there isn't, the coroutine yields a missing argument error.

func (*Args) GetAny

func (a *Args) GetAny(optV ...any) (arg any)

GetAny returns the next argument.

func (*Args) GetBool

func (a *Args) GetBool(optV ...bool) bool

GetBool returns the next argument as a boolean value. An optional value can be passed if the argument is not required.

func (*Args) GetBuffer

func (a *Args) GetBuffer(optV ...*Buffer) *Buffer

GetBuffer returns the next argument as a buffer value. An optional value can be passed if the argument is not required.

func (*Args) GetCoroutine

func (a *Args) GetCoroutine(optV ...*Coroutine) *Coroutine

GetCoroutine returns the next argument as a coroutine value. An optional value can be passed if the argument is not required.

func (*Args) GetFunction

func (a *Args) GetFunction(optV ...Function) Function

GetFunction returns the next argument as a function value. An optional value can be passed if the argument is not required.

func (*Args) GetNumber

func (a *Args) GetNumber(optV ...float64) float64

GetNumber returns the next argument as a float64 number value. An optional value can be passed if the argument is not required.

func (*Args) GetString

func (a *Args) GetString(optV ...string) string

GetString returns the next argument as a string value. An optional value can be passed if the argument is not required.

func (*Args) GetTable

func (a *Args) GetTable(optV ...*Table) *Table

GetTable returns the next argument as a table value. An optional value can be passed if the argument is not required.

func (*Args) GetVector

func (a *Args) GetVector(optV ...Vector) Vector

GetVector returns the next argument as a vector value. An optional value can be passed if the argument is not required.

type Buffer

type Buffer []byte

Buffer represents a Luau byte buffer. Luau type`buffer`

type Compiler

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

Compiler allows programs to be compiled and deserialised with a cache and given optimisation level.

func NewCompiler

func NewCompiler(o uint8) Compiler

NewCompiler creates a new compiler with the given optimisation level.

func (Compiler) Compile

func (c Compiler) Compile(path string) (p compiled, err error)

Compile compiles a program at a specific path to bytecode and returns its deserialised form.

type Coroutine

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

Coroutine represents a Luau coroutine, including the main coroutine. Luau type `thread`

func (*Coroutine) Error

func (co *Coroutine) Error(err error)

Error yields an error to the coroutine, killing it shortly after.

func (*Coroutine) Resume

func (co *Coroutine) Resume(args ...any) (r Rets, err error)

Resume executes the coroutine with the provided arguments, starting it with the given arguments if it is not already started, otherwise resuming it and passing the argument values back to the yielded function.

type Env

type Env map[any]any

Env represents a global Luau environment.

func (*Env) AddFn

func (e *Env) AddFn(f Function)

AddFn adds a function to the environment.

type Function

type Function struct {
	// Run is the native body of the function. Its coroutine argument is used to run the function in a coroutine.
	Run *func(co *Coroutine, args ...any) (r Rets, err error)
	// contains filtered or unexported fields
}

Function represents a native or wrapped Luau function. Luau type `function`

func MakeFn

func MakeFn(name string, f func(args Args) (r Rets, err error)) Function

MakeFn creates a new function with a given name and body. Functions created by MakeFn can be added to a library using NewLib.

type Rets

type Rets []any

Rets represents the return values of a user-defined native function.

type Status

type Status uint8

Status represents the status of a coroutine.

const (
	CoSuspended Status = iota
	CoRunning
	CoNormal
	CoDead
)

Coroutine stati

type Table

type Table struct {
	Array []any
	Hash  map[any]any
	// contains filtered or unexported fields
}

Table represents a Luau table, with resizeable array and hash parts. Luau type `table`

func NewLib

func NewLib(functions []Function, other ...map[string]any) *Table

NewLib creates a new library with a given table of functions and other values, such as constants. Functions can be created using MakeFn.

func (*Table) ForceSet

func (t *Table) ForceSet(k, v any)

ForceSet sets a table value at a key, regardless of whether the table is readonly.

func (*Table) Get

func (t *Table) Get(k any) any

Get returns a value at a key in the table.

func (*Table) GetHash

func (t *Table) GetHash(k any) any

GetHash returns a value at a key, only searching the hash part of the table.

func (*Table) Iter

func (t *Table) Iter() iter.Seq2[any, any]

Iter returns an iterator over the table, yielding key-value pairs in a deterministic order.

func (*Table) Len

func (t *Table) Len() int

Len returns the length of the array part of the table (the length of the array up until the first nil).

func (*Table) Set

func (t *Table) Set(k, v any) error

Set sets a table value at a key, returning an error if the table is readonly.

func (*Table) SetArray

func (t *Table) SetArray(i int, v any)

SetArray sets a value at an integer index, plcing it into the Array part or the Hash part and resizing each as appropriate.

func (*Table) SetHash

func (t *Table) SetHash(k, v any)

SetHash updates or deletes a key-value pair in the hash part of the table.

type Vector

type Vector [4]float32

Vector represents a 3-wide or 4-wide vector value. Luau type `vector`

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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