package module
Version: v0.0.0-...-950ecff Latest Latest

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

Go to latest
Published: Dec 21, 2020 License: MIT Imports: 13 Imported by: 0


LuaSocket for GopherLua

A native Go implementation of LuaSocket for the GopherLua VM.


Loading Modules
import (

// Bring up a GopherLua VM
L := lua.NewState()
defer L.Close()

// Preload LuaSocket modules
Read lines from a socket
script := `
  local client = require 'socket'.connect('', 8000)
  local line1 = client:receive('*l')
  local line2 = client:receive('*l')
  return line1, line2`
line1 := L.ToString(-2)
line2 := L.ToString(-1)
Get system time
L.DoString("return require 'socket'.gettime()")
gettimeValue := float64(L.ToNumber(-1))


$ go test
ok	0.045s
?	[no test files]
?	[no test files]
ok	0.040s
?	[no test files]
ok	0.269s
?	[no test files]
?	[no test files]
?	[no test files]
?	[no test files]
?	[no test files]
?	[no test files]
?	[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.


Divergence from LuaSocket source codes
1. Unit test calls to os.exit() replaced with error()

This was necessary in order to detect and report errors from a test runner. Filed LuaSocket Issue #227.

2. Finalized Exceptions moved to pure-Lua

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.




This section is empty.


This section is empty.


func Preload

func Preload(L *lua.LState)


This section is empty.

Source Files

Jump to

Keyboard shortcuts

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