embedlua

package module
v0.0.0-...-7041fb1 Latest Latest
Warning

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

Go to latest
Published: Jul 9, 2019 License: MIT Imports: 5 Imported by: 0

README

embedlua

GoDoc Go Report Card

A thin wrapper around yuin/gopher-lua package for embedding lua in Go applications

Usage

package main

import (
	"fmt"
	"math"
	"strings"

	"github.com/spy16/canister/embedlua"
)

const sampleLua = `
function sayHelloWorld(name)
  fmt_printf("'Hello %s' from Lua using fmt.Printf() of Go!", name)
  return 10
end
`

func main() {
	wr := embedlua.New()
	wr.Bind("fmt_printf", func(format string, args ...interface{}) {
		format = strings.Trim(format, "\n") + "\n"
		fmt.Printf(format, args...)
	})
	wr.BindAll(map[string]interface{}{
		"e":   math.E,
		"Pi":  math.Pi,
		"Phi": math.Phi,
	})
	wr.Execute(sampleLua)

	fmt.Println(wr.Call("sayHelloWorld", "John"))
}

Documentation

Overview

Package embedlua provides a simple convinience wrapper around `github.com/yuin/gopher-lua` package for embedding lua scripting backend

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Wrapper

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

Wrapper is a thin wrapper around gopher-lua defined lua state

func New

func New(paths ...string) *Wrapper

New initializes a new wrapper with empty Lua state

func (*Wrapper) Bind

func (wr *Wrapper) Bind(name string, v interface{})

Bind creates a global variable with given value and name thus exposing the value to the lua script

func (*Wrapper) BindAll

func (wr *Wrapper) BindAll(vals map[string]interface{})

BindAll exposes all the values in the map to the lua scripts by iteratively calling Bind with key and value

func (*Wrapper) Call

func (wr *Wrapper) 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 (*Wrapper) Execute

func (wr *Wrapper) Execute(src string) error

Execute the given lua script string

func (*Wrapper) ExecuteFile

func (wr *Wrapper) ExecuteFile(fileName string) error

ExecuteFile reads and executes the lua file

func (*Wrapper) Reset

func (wr *Wrapper) Reset()

Reset the lua state. Same as creating new instance of Wrapper

func (*Wrapper) State

func (wr *Wrapper) State() *lua.LState

State returns the actual lua state object

Jump to

Keyboard shortcuts

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