lua

package module
v1.5.6 Latest Latest
Warning

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

Go to latest
Published: Feb 21, 2024 License: MIT Imports: 15 Imported by: 1

README

Concurrent LUA Executor

Go Report Card GoDoc

This repository contains a concurrent LUA executor that is designed to keep running a same (but updateable) set of scripts over a long period of time. The design of the library is quite opinionated, as it requires the main() function to be present in the script in order to run it. It also maintains a pool of VMs per Script instance to increase throughput per script.

Under the hood, it uses gopher-lua library but abstracts it away, in order to be easily replaced in future if required.

Usage Example

Below is the usage example which runs the fibonacci LUA script with input 10.

// Load the script
s, err := FromString("test.lua", `
    function main(n)
        if n < 2 then return 1 end
        return main(n - 2) + main(n - 1)
    end
`)

// Run the main() function with 10 as argument
result, err := s.Run(context.Background(), 10)
println(result.String()) // Output: 89

The library also supports passing complex data types, thanks to gopher-luar. In the example below we create a Person struct and update its name in LUA as a side-effect of the script. It also returns the updated name back as a string.

// Load the script
s, err := FromString("test.lua", `
    function main(input)
        input.Name = "Updated"
        return input.Name
    end
`)

input := &Person{ Name: "Roman" }
out, err := s.Run(context.Background(), input)
println(out)         // Outputs: "Updated"
println(input.Name)  // Outputs: "Updated"

Native Modules

This library also supports and abstracts modules, which allows you to provide one or multiple native libraries which can be used by the script. These things are just ensembles of functions which are implemented in pure Go.

Such functions must comply to a specific interface - they should have their arguments as the library's values (e.g. Number, String or Bool) and the result can be either a value and error or just an error. Here's an example of such function:

func hash(s lua.String) (lua.Number, error) {
	h := fnv.New32a()
	h.Write([]byte(s))

	return lua.Number(h.Sum32()), nil
}

In order to use it, the functions should be registered into a NativeModule which then is loaded when script is created.

// Create a test module which provides hash function
module := &NativeModule{
    Name:    "test",
    Version: "1.0.0",
}
module.Register("hash", hash)

// Load the script
s, err := FromString("test.lua", `
    local api = require("test")

    function main(input)
        return api.hash(input)
    end
`, module) // <- attach the module

out, err := s.Run(context.Background(), "abcdef")
println(out) // Output: 4282878506

Script Modules

Similarly to native modules, the library also supports LUA script modules. In order to use it, first you need to create a script which contains a module and returns a table with the functions. Then, create a ScriptModule which points to the script with Name which can be used in the require statement.

moduleCode, err := FromString("module.lua", `
    local demo_mod = {} -- The main table

    function demo_mod.Mult(a, b)
        return a * b
    end

    return demo_mod
`)

// Create a test module which provides hash function
module := &ScriptModule{
    Script:  moduleCode,
    Name:    "demo_mod",
    Version: "1.0.0",
}

Finally, attach the module to the script as with native modules.

// Load the script
s, err := FromString("test.lua", `
    local demo = require("demo_mod")

    function main(input)
        return demo.Mult(5, 5)
    end
`, module) // <- attach the module

out, err := s.Run(context.Background())
println(out) // Output: 25

Benchmarks

Benchmark_Serial/fib-8         	 5870025	       203 ns/op	      16 B/op	       2 allocs/op
Benchmark_Serial/empty-8       	 8592448	       137 ns/op	       0 B/op	       0 allocs/op
Benchmark_Serial/update-8      	 1000000	      1069 ns/op	     224 B/op	      14 allocs/op

Documentation

Index

Constants

View Source
const (
	TypeNil = Type(iota)
	TypeBool
	TypeNumber
	TypeString
	TypeBools
	TypeNumbers
	TypeStrings
	TypeTable
	TypeArray
	TypeValue
)

Various supported types

Variables

This section is empty.

Functions

This section is empty.

Types

type Array added in v1.3.0

type Array []Value

Array represents the array of values

func (Array) Native added in v1.3.0

func (v Array) Native() any

Native returns value casted to native type

func (Array) String added in v1.3.0

func (v Array) String() string

String returns the string representation of the value

func (Array) Type added in v1.3.0

func (v Array) Type() Type

Type returns the type of the value

type Bool

type Bool bool

Bool represents the boolean value

func (Bool) Native added in v1.1.0

func (v Bool) Native() any

Native returns value casted to native type

func (Bool) String

func (v Bool) String() string

String returns the string representation of the value

func (Bool) Type

func (v Bool) Type() Type

Type returns the type of the value

type Bools added in v0.0.4

type Bools []bool

Bools represents the boolean array value

func (Bools) Native added in v1.1.0

func (v Bools) Native() any

Native returns value casted to native type

func (Bools) String added in v0.0.4

func (v Bools) String() string

String returns the string representation of the value

func (Bools) Type added in v0.0.4

func (v Bools) Type() Type

Type returns the type of the value

type Module

type Module interface {
	// contains filtered or unexported methods
}

Module represents a loadable module.

type NativeModule added in v0.0.3

type NativeModule struct {
	Name    string // The name of the module
	Version string // The module version string
	// contains filtered or unexported fields
}

NativeModule represents a loadable native module.

func (*NativeModule) Register added in v0.0.3

func (m *NativeModule) Register(name string, function any) error

Register registers a function into the module.

func (*NativeModule) Unregister added in v0.0.3

func (m *NativeModule) Unregister(name string)

Unregister unregisters a function from the module.

type Nil

type Nil struct{}

Nil represents the nil value

func (Nil) Native added in v1.1.0

func (v Nil) Native() any

Native returns value casted to native type

func (Nil) String

func (v Nil) String() string

String returns the string representation of the value

func (Nil) Type

func (v Nil) Type() Type

Type returns the type of the value

type Number

type Number float64

Number represents the numerical value

func (Number) Native added in v1.1.0

func (v Number) Native() any

Native returns value casted to native type

func (Number) String

func (v Number) String() string

String returns the string representation of the value

func (Number) Type

func (v Number) Type() Type

Type returns the type of the value

type Numbers added in v0.0.4

type Numbers []float64

Numbers represents the number array value

func (Numbers) Native added in v1.1.0

func (v Numbers) Native() any

Native returns value casted to native type

func (Numbers) String added in v0.0.4

func (v Numbers) String() string

String returns the string representation of the value

func (Numbers) Type added in v0.0.4

func (v Numbers) Type() Type

Type returns the type of the value

type Script

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

Script represents a LUA script

func FromReader

func FromReader(name string, r io.Reader, modules ...Module) (*Script, error)

FromReader reads a script fron an io.Reader

func FromString

func FromString(name, code string, modules ...Module) (*Script, error)

FromString reads a script fron a string

func New added in v1.5.2

func New(name string, source io.Reader, concurrency int, modules ...Module) (*Script, error)

New creates a new script from an io.Reader

func (*Script) Close

func (s *Script) Close() error

Close closes the script and cleanly disposes of its resources.

func (*Script) Concurrency added in v1.5.2

func (s *Script) Concurrency() int

Concurrency returns the concurrency setting of the script

func (*Script) Name added in v0.0.2

func (s *Script) Name() string

Name returns the name of the script

func (*Script) Run

func (s *Script) Run(ctx context.Context, args ...any) (Value, error)

Run runs the main function of the script with arguments.

func (*Script) Update

func (s *Script) Update(r io.Reader) (err error)

Update updates the content of the script.

type ScriptModule added in v0.0.3

type ScriptModule struct {
	Script  *Script // The script that contains the module
	Name    string  // The name of the module
	Version string  // The module version string
}

ScriptModule represents a loadable module written in LUA itself.

type String

type String string

String represents the string value

func (String) Native added in v1.1.0

func (v String) Native() any

Native returns value casted to native type

func (String) String

func (v String) String() string

String returns the string representation of the value

func (String) Type

func (v String) Type() Type

Type returns the type of the value

type Strings added in v0.0.4

type Strings []string

Strings represents the string array value

func (Strings) Native added in v1.1.0

func (v Strings) Native() any

Native returns value casted to native type

func (Strings) String added in v0.0.4

func (v Strings) String() string

String returns the string representation of the value

func (Strings) Type added in v0.0.4

func (v Strings) Type() Type

Type returns the type of the value

type Table added in v0.0.7

type Table map[string]Value

Table represents a map of string to value

func (Table) Native added in v1.1.0

func (v Table) Native() any

Native returns value casted to native type

func (Table) String added in v0.0.7

func (v Table) String() string

String returns the string representation of the value

func (Table) Type added in v0.0.7

func (v Table) Type() Type

Type returns the type of the value

func (*Table) UnmarshalJSON added in v1.2.0

func (v *Table) UnmarshalJSON(b []byte) error

UnmarshalJSON unmarshals the type from JSON

type Type

type Type byte

Type represents a type of the value

type Value

type Value interface {
	fmt.Stringer
	Type() Type
	Native() any
	// contains filtered or unexported methods
}

Value represents a returned

func ValueOf

func ValueOf(v any) Value

ValueOf converts the type to our value

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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