luarunner

package module
v1.1.2 Latest Latest
Warning

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

Go to latest
Published: Jun 25, 2025 License: GPL-3.0 Imports: 6 Imported by: 0

README

Go wrapper for LuaJIT with built-in extensions

Lua extensions

hash library
  1. hash.postgres - pg_hash_bytes from PostgreSQL sources
  2. hash.murmur3 - MurmurHash3_x86_32 by Austin Appleby
  3. hash.crc32 - from libiberty
cjson library

Original library, documentation

  1. cjson
  2. cjson_safe
yyjson

A fast and non-destructive Lua library for working with JSON. The JSON document is stored in internal yyjson structures and is not marshalled or unmarshalled to Lua values. Creating JSON arrays is not supported.

  1. yyjson.load - parses JSON into a read-only readonly object
  2. yyjson.load_mut - parses JSON into a mutable object
  3. yyjson.new - creates a new mutable JSON object
  4. yyjson.null - null constant
  5. tostring(v) - serializes a mutable JSON object to a string
examples
j = yyjson.new() -- create an empty JSON object
j = yyjson.load(json_string) -- load JSON for read-only access (faster)
j = yyjson.load_mut(json_string) -- load JSON for modification

-- access fields
print(j.int32)
print(j.object.nested_object.inner_float)

-- modify fields
j.uuid = nil -- delete
j.boolean = yyjson.null -- overwrite with null
j.newobject = {test = 1, test1 = {test2 = "test"}}
j.array[4] = 8
j.copy = j.object -- copy subtrees

-- serialize to json
tostring(j) -- full document
tostring(j.object) -- document part

Documentation

Overview

luarunner — a package for interacting with LuaJIT.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type LuaRunner

type LuaRunner struct {
	L *C.lua_State
}

LuaRunner represents Lua VM

func New

func New() (*LuaRunner, error)

New initializes and returns a new Lua VM

func (*LuaRunner) AddPackageCPath

func (lua *LuaRunner) AddPackageCPath(pattern string)

AddPackageCPath adds a pattern to the file search path for require (package.cpath)

func (*LuaRunner) AddPackagePath

func (lua *LuaRunner) AddPackagePath(pattern string)

AddPackagePath adds a pattern to the file search path for require (package.path)

func (*LuaRunner) Call

func (lua *LuaRunner) Call(in, out int) error

Call calls the function at the top of the stack To set the function, use GetGlobal in - number of input parameters out - number of return values, -1 for a variable number

func (*LuaRunner) CheckFunction

func (lua *LuaRunner) CheckFunction(funcName string) bool

CheckFunction checks for the existence of a global function in the current Lua VM

func (*LuaRunner) Close

func (lua *LuaRunner) Close()

Close releases all resources associated with the Lua VM

func (*LuaRunner) Get

func (lua *LuaRunner) Get(i int) (any, error)

Get gets a value in Lua stack by index

func (*LuaRunner) GetGlobal

func (lua *LuaRunner) GetGlobal(name string)

GetGlobal pushes the value of the global variable with the name 'name' onto the stack

func (*LuaRunner) GetStackSize

func (lua *LuaRunner) GetStackSize() int

GetStackSize returns the current depth of the Lua VM stack It is used to determine the number of return values after a function call and for debugging memory leaks

func (*LuaRunner) Load

func (lua *LuaRunner) Load(code string) error

Load loads a Lua script and pushes the code onto the top of the stack. To execute it, you need to call Run(). code - Lua code

func (*LuaRunner) LoadFile

func (lua *LuaRunner) LoadFile(name string) error

Load loads a Lua script from a file and pushes the code onto the top of the stack. To execute it, you need to call Run(). name - path to the file

func (*LuaRunner) Pop

func (lua *LuaRunner) Pop() (any, error)

Pop pops a value from Lua stack

func (*LuaRunner) Push

func (lua *LuaRunner) Push(valueAny any)

Push pushes a value onto the Lua VM stack.

string and []byte are converted to a string (LUA_TSTRING); int, int32, int64, float32, float64 are converted to a number (LUA_TNUMBER), beware of truncating int64 to 53 bits; bool is converted to a boolean (LUA_TBOOLEAN); uintptr is converted to LUA_TLIGHTUSERDATA; nil is converted to LUA_TLIGHTUSERDATA(NULL) if it is a table value, otherwise to nil (LUA_TNIL).

map[string]any is converted to a table (LUA_TTABLE), and it's the only map type that is efficiently passed to Lua. Other map types will use reflection. On one hand, this allows using arbitrary key and value types supported by Lua; on the other hand, reflection is less efficient. Another limitation is that this package does not allow retrieving a Lua table with key types other than string or number.

Using reflection: pointers are dereferenced; slices and arrays are passed as tables with integer keys; structs and maps with arbitrary key/value types are passed as tables.

If an unsupported data type is encountered, nil or LUA_TLIGHTUSERDATA(NULL) is pushed onto the stack, depending on whether it's a simple scalar or a table element.

func (*LuaRunner) Run

func (lua *LuaRunner) Run() error

Run calls the function at the top of the stack with no parameters and a variable number of return values To execute the script after Load and LoadFile

func (*LuaRunner) SetGlobal

func (lua *LuaRunner) SetGlobal(name string)

SetGlobal pops a value from the stack and sets it as the global variable with the name 'name'

func (*LuaRunner) StrictRead

func (lua *LuaRunner) StrictRead()

StrictRead prevents reading uninitialized variables, protecting against typos.

func (*LuaRunner) StrictWrite

func (lua *LuaRunner) StrictWrite()

StrictWrite prevents writing to undeclared global variables; It is useful to call it after initializing the script.

Jump to

Keyboard shortcuts

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