goluaez

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

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

Go to latest
Published: Jan 16, 2016 License: MIT Imports: 6 Imported by: 3

README

luaez

GoDoc Build Status Coverage Status

Easy embedding of lua in go.

goluaez wraps gopher-lua and gopher-luar. gopher-luar does a nice job of converting go values to lua. This package uses that and also converts lua values to go and converts go slices to lua tables.

This makes it easy to have small 1 or 2 line user-defined functions in a go application that embeds lua.

Example usage:

package main

import (
	"fmt"
	"log"

	"github.com/brentp/goluaez"
)

func check(e error) {
	if e != nil {
		log.Fatal(e)
	}
}

func main() {
	L, err := goluaez.NewState(`
adder = function(a, b)
    return a + b
end`)
	check(err)
	defer L.Close()

	// Run uses a mutex so can be run in a goroutine.
	result, err := L.Run("adder(x, y)", map[string]interface{}{"x": 12, "y": "23"})
	check(err)
	fmt.Println(result.(float64))
	// 35

	result, err = L.Run("'hello' .. ' world'")
	check(err)
	fmt.Println(result.(string))
	// hello world

	L.DoString("a = {}; a['a'] = 22; a['b'] = 33;")
	result, err = L.Run("a")
	check(err)
	fmt.Println(result.(map[string]interface{}))
	// map[b:33 a:22]

}

Prelude

The embedded lua engine is populated with the lua functions in data/prelude.lua So far, this consists of:

  • string:split(sep) (split a string by a delim)
  • string:strip() (remove whitespace from a string)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Go2LValue

func Go2LValue(v interface{}) (lua.LValue, error)

func LValue2Go

func LValue2Go(v lua.LValue) (interface{}, error)

Types

type State

type State struct {
	*lua.LState
	// contains filtered or unexported fields
}

func NewState

func NewState(code ...string) (*State, error)

NewState creates a new State object optionally initialized with some code.

func (*State) Run

func (s *State) Run(code string, values ...map[string]interface{}) (interface{}, error)

Run code given some values. This is thread-safe.

func (*State) SetGlobal

func (s *State) SetGlobal(name string, val interface{}) error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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