A native Go implementation of LuaSocket for the GopherLua VM.
import (
"github.com/teejaded/gluasocket"
)
// Bring up a GopherLua VM
L := lua.NewState()
defer L.Close()
// Preload LuaSocket modules
gluasocket.Preload(L)script := `
local client = require 'socket'.connect('127.0.0.1', 8000)
local line1 = client:receive('*l')
local line2 = client:receive('*l')
client:close()
return line1, line2`
L.DoString(script)
line1 := L.ToString(-2)
line2 := L.ToString(-1)L.DoString("return require 'socket'.gettime()")
gettimeValue := float64(L.ToNumber(-1))$ go test github.com/teejaded/gluasocket...
ok github.com/teejaded/gluasocket 0.045s
? github.com/teejaded/gluasocket/ltn12 [no test files]
? github.com/teejaded/gluasocket/mime [no test files]
ok github.com/teejaded/gluasocket/mimecore 0.040s
? github.com/teejaded/gluasocket/socket [no test files]
ok github.com/teejaded/gluasocket/socketcore 0.269s
? github.com/teejaded/gluasocket/socketexcept [no test files]
? github.com/teejaded/gluasocket/socketftp [no test files]
? github.com/teejaded/gluasocket/socketheaders [no test files]
? github.com/teejaded/gluasocket/sockethttp [no test files]
? github.com/teejaded/gluasocket/socketsmtp [no test files]
? github.com/teejaded/gluasocket/sockettp [no test files]
? github.com/teejaded/gluasocket/socketurl [no test files]Some original Lua-based LuaSocket unit tests are used and wrapped in Go unit test functions. Tests that perform os.exit() are modified to perform error() instead so that errors are made detectable.
This was necessary in order to detect and report errors from a test runner. Filed LuaSocket Issue #227.
LuaSocket's exception handling is based on Diego's Finalized Exceptions whitepaper.
After struggling to port the C-based socket.newtry() and socket.protect() functions to GopherLua, an easier path emerged when I discovered the pure-Lua implementations found in the unmerged LuaSocket Pull Request #161, which introduces a new socket.except module, and makes tiny modifications to the socket module in order to use it. This served the purposes of this project perfectly.