glua

package module
v0.4.7 Latest Latest
Warning

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

Go to latest
Published: Apr 21, 2023 License: MIT Imports: 11 Imported by: 1

README

glua

embed lua script language for Go

go luajit glue,very very fast, support lua coroutine

Demo

easy use, like:

package main

import (
	"context"
	"fmt"
	"time"

	"github.com/RyouZhang/go-lua"
)

func main() {

	ts := time.Now()
	res, err := glua.NewAction().WithScript(`
	function fib(n)
		if n == 0 then
			return 0
		elseif n == 1 then
			return 1
		end
		return fib(n-1) + fib(n-2)
	end
	`).WithEntrypoint("fib").AddParam(35).Execute(context.Background())

	fmt.Println("cost:", time.Now().Sub(ts))
	fmt.Println(res, err)
}

Benchmark

fib(35) fibt(35) Language (Type)
wassmer-go 57ms `` Wasm
glua 110ms 1.5ms Luajit(VM)
Tengo 2,315ms 3ms Tengo (VM)
go-lua 4,028ms 3ms Lua (VM)
GopherLua 4,409ms 3ms Lua (VM)
goja 5,194ms 4ms JavaScript (VM)
starlark-go 6,954ms 3ms Starlark (Interpreter)
gpython 11,324ms 4ms Python (Interpreter)
Yaegi 4,715ms 10ms Yaegi (Interpreter)
otto 48,539ms 6ms JavaScript (Interpreter)
Anko 52,821ms 6ms Anko (Interpreter)
- - - -
Go 47ms 2ms Go (Native)
Lua 756ms 2ms Lua (Native)
Python 1,907ms 14ms Python2 (Native)

* fib(35): Fibonacci(35)
* fibt(35): tail-call version of Fibonacci(35)
* Go does not read the source code from file, while all other cases do

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GlobalOptions

func GlobalOptions(opts *Options)

func LuaNumberToFloat32

func LuaNumberToFloat32(value interface{}) (float32, error)

func LuaNumberToFloat64

func LuaNumberToFloat64(value interface{}) (float64, error)

func LuaNumberToInt

func LuaNumberToInt(value interface{}) (int, error)

func LuaNumberToInt32

func LuaNumberToInt32(value interface{}) (int32, error)

func LuaNumberToInt64

func LuaNumberToInt64(value interface{}) (int64, error)

func RegisterExternMethod

func RegisterExternMethod(methodName string, method LuaExternFunc) error

Types

type Action

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

func NewAction

func NewAction() *Action

func (*Action) AddFunc

func (a *Action) AddFunc(methodName string, method LuaExternFunc) *Action

func (*Action) AddParam

func (a *Action) AddParam(params ...interface{}) *Action

func (*Action) Execute

func (a *Action) Execute(ctx context.Context) (interface{}, error)

func (*Action) WithEntrypoint

func (a *Action) WithEntrypoint(entrypoint string) *Action

func (*Action) WithScript

func (a *Action) WithScript(script string) *Action

func (*Action) WithScriptMD5

func (a *Action) WithScriptMD5(md5 string) *Action

func (*Action) WithScriptPath

func (a *Action) WithScriptPath(scriptPath string) *Action

type LuaExternFunc

type LuaExternFunc func(context.Context, ...interface{}) (interface{}, error)

type Metric added in v0.3.5

type Metric interface {
	Counter(name string, value int64, labels map[string]string)
	Gauge(name string, value int64, labels map[string]string)
}

type Options

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

func NewOptions

func NewOptions() *Options

func (*Options) SetMetric added in v0.3.5

func (opt *Options) SetMetric(handle Metric) *Options

func (*Options) SetPreloadScripeMethod

func (opt *Options) SetPreloadScripeMethod(method func() string) *Options

func (*Options) WithMaxVMSize

func (opt *Options) WithMaxVMSize(maxVmSize int) *Options

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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