开源软件名称:Shopify/go-lua开源软件地址:https://github.com/Shopify/go-lua开源编程语言:Go 99.8%开源软件介绍:A Lua VM in pure Gogo-lua is a port of the Lua 5.2 VM to pure Go. It is compatible with binary files dumped by The motivation is to enable simple scripting of Go applications. For example, it is used to describe flows in Shopify's load generation tool, Genghis. Usagego-lua is intended to be used as a Go package. It does not include a command to run the interpreter. To start using the library, run: go get github.com/Shopify/go-lua To develop & test go-lua, you'll also need the lua-tests submodule checked out: git submodule update --init You can then develop with the usual Go commands, e.g.: go build
go test -cover A simple example that loads & runs a Lua script is: package main
import "github.com/Shopify/go-lua"
func main() {
l := lua.NewState()
lua.OpenLibraries(l)
if err := lua.DoFile(l, "hello.lua"); err != nil {
panic(err)
}
} Statusgo-lua has been used in production in Shopify's load generation tool, Genghis, since May 2014, and is also part of Shopify's resiliency tooling. The core VM and compiler has been ported and tested. The compiler is able to correctly process all Lua source files from the Lua test suite. The VM has been tested to correctly execute over a third of the Lua test cases. Most core Lua libraries are at least partially implemented. Prominent exceptions are regular expressions, coroutines and Weak reference tables are not and will not be supported. go-lua uses the Go heap for Lua objects, and Go does not support weak references. BenchmarksBenchmark results shown here are taken from a Mid 2012 MacBook Pro Retina with a 2.6 GHz Core i7 CPU running OS X 10.10.2, go 1.4.2 and Lua 5.2.2. The Fibonacci function can be written a few different ways to evaluate different performance characteristics of a language interpreter. The simplest way is as a recursive function: function fib(n)
if n == 0 then
return 0
elseif n == 1 then
return 1
end
return fib(n-1) + fib(n-2)
end This exercises the call stack implementation. When computing
The recursive Fibonacci function can be transformed into a tail-recursive variant: function fibt(n0, n1, c)
if c == 0 then
return n0
else if c == 1 then
return n1
end
return fibt(n1, n0+n1, c-1)
end
function fib(n)
fibt(0, 1, n)
end The Lua interpreter detects and optimizes tail calls. This exhibits similar relative performance between the 3 interpreters, though gopher-lua edges ahead a little due to its simpler stack model and reduced bookkeeping.
Finally, we can write an explicitly iterative implementation: function fib(n)
if n == 0 then
return 0
else if n == 1 then
return 1
end
local n0, n1 = 0, 1
for i = n, 2, -1 do
local tmp = n0 + n1
n0 = n1
n1 = tmp
end
return n1
end This exercises more of the bytecode interpreter’s inner loop. Here we see the performance impact of Go’s
Licensego-lua is licensed under the MIT license. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论