lua

package
v0.2.4 Latest Latest
Warning

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

Go to latest
Published: Feb 25, 2021 License: MIT Imports: 7 Imported by: 1

README

Lua

An easy-to-use wrapper for gopher-lua that provides easy interoperability between Lua and Go.

Usage

package main

import "github.com/spy16/pkg/lua"

func main() {
	luaState, _ := lua.New(
		lua.Path("/Users/bob/lua-lib"),
		lua.Module("http", httpClient{}),
	)

	_ = luaState.Execute(`print("hello")`)
}

Documentation

Overview

Package lua provides a wrapper around gopher-lua to easily enable Lua scripting support in Go applications. See New() for initialisation of Lua instance.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Lua

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

Lua is a wrapper around lua-state and provides functions for managing state and executing lua code.

func New

func New(opts ...Option) (*Lua, error)

New returns a new Lua instance initialised. Options can be provided to bind globals, register modules etc.

func (*Lua) Call

func (l *Lua) Call(name string, args ...interface{}) (lua.LValue, error)

Call a lua function by its name. Args are automatically converted to appropriate types using the Luar library

func (*Lua) Destroy

func (l *Lua) Destroy()

Destroy releases all resources held by the lua state and marks the instance closed for usage.

func (*Lua) Execute

func (l *Lua) Execute(src string) error

Execute the given lua script string. Use Call() for calling a function for result.

func (*Lua) ExecuteFile

func (l *Lua) ExecuteFile(fileName string) error

ExecuteFile reads and executes the lua file. Use Call() for calling a function for result.

func (*Lua) State added in v0.2.3

func (l *Lua) State() *lua.LState

State returns the internal LState.

type Option

type Option func(l *Lua) error

Option can be provided to New() to customise initialization of Lua state.

func Context added in v0.2.2

func Context(ctx context.Context) Option

Context sets a context to be used by the lua state.

func Globals

func Globals(vals map[string]interface{}) Option

Globals sets all values in the map as global variables in the lua state. See Module() to create a module from the map.

func Module

func Module(name string, structOrMap interface{}) Option

Module defines a Lua module with public struct fields and methods exported. Use Globals() if you need to set the struct or the map as a single value.

func Path

func Path(dirs ...string) Option

Path appends the given directories to LUA_PATH in correct format.

Jump to

Keyboard shortcuts

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