Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
So this turns out to be less easy than I thought a few hours ago Flatbuffers is pretty bad, the lua side hasn't been touched for 3 years yet I found a silly typo just flipping through the code: google/flatbuffers#8251 In particular, they seem to assume string.pack/string.unpack will exist, but in 5.1 it doesn't, with LuaJIT they use ffi which means that works out for most 5.1 users Solution: we're going to shim our own go implementation of the flatbuffers lua library
- Loading branch information
Showing
4 changed files
with
294 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package pua | ||
|
||
import ( | ||
"github.com/yuin/gopher-lua" | ||
) | ||
|
||
/* | ||
local m = {} | ||
m.Builder = require("flatbuffers.builder").New | ||
m.N = require("flatbuffers.numTypes") | ||
m.view = require("flatbuffers.view") | ||
m.binaryArray = require("flatbuffers.binaryarray") | ||
return m | ||
*/ | ||
|
||
func requireHelper(ls *lua.LState, m *lua.LTable, require lua.LValue, name string, path string) { | ||
ls.Push(require) | ||
ls.Push(lua.LString(path)) | ||
ls.Call(1, 1) | ||
ls.SetField(m, name, ls.Get(-1)) | ||
ls.Pop(1) | ||
} | ||
|
||
func FlatBuffers_Loader(ls *lua.LState) int { | ||
m := ls.NewTable() | ||
require := ls.GetGlobal("require") | ||
ls.Push(require) | ||
ls.Push(lua.LString("flatbuffers.builder")) | ||
ls.Call(1, 1) | ||
builder := ls.GetTable(ls.Get(-1), lua.LString("New")) | ||
ls.SetField(m, "builder", builder) | ||
ls.Pop(1) | ||
|
||
requireHelper(ls, m, require, "N", "flatbuffers.numTypes") | ||
requireHelper(ls, m, require, "view", "flatbuffers.view") | ||
requireHelper(ls, m, require, "binaryArray", "flatbuffers.binaryarray") | ||
|
||
ls.Push(m) | ||
return 1 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
package pua | ||
|
||
import ( | ||
"github.com/yuin/gopher-lua" | ||
) | ||
|
||
type BinaryArray struct { | ||
data []byte | ||
} | ||
|
||
var LuaBinaryArray = LuaUserDataType[BinaryArray]{Name: "flatbuffers_binaryarray"} | ||
|
||
func FlatBuffers_BinaryArray_Loader(ls *lua.LState) int { | ||
m := ls.NewTable() | ||
ls.SetField(m, "New", ls.NewFunction(BinaryArrayNew)) | ||
ls.SetField(m, "Pack", ls.NewFunction(BinaryArrayPack)) | ||
ls.SetField(m, "Unpack", ls.NewFunction(BinaryArrayUnpack)) | ||
|
||
mt := ls.NewTable() | ||
ls.SetField(mt, "__index", ls.NewFunction(BinaryArrayIndex)) | ||
ls.SetField(mt, "__len", ls.NewFunction(BinaryArrayLen)) | ||
ls.SetField(mt, "Slice", ls.NewFunction(BinaryArraySlice)) | ||
ls.SetField(mt, "Grow", ls.NewFunction(BinaryArrayGrow)) | ||
ls.SetField(mt, "Pad", ls.NewFunction(BinaryArrayPad)) | ||
ls.SetField(mt, "Set", ls.NewFunction(BinaryArraySet)) | ||
|
||
ls.Push(m) | ||
return 1 | ||
} | ||
|
||
func BinaryArrayNew(ls *lua.LState) int { | ||
lval := ls.Get(-1) | ||
var ba BinaryArray | ||
switch val := lval.(type) { | ||
case lua.LString: | ||
ba = BinaryArray{ | ||
data: []byte(val), | ||
} | ||
case lua.LNumber: | ||
ba = BinaryArray{ | ||
data: make([]byte, int(val)), | ||
} | ||
default: | ||
ls.Error(lua.LString("Expect a integer size value or string to construct a binary array"), 0) | ||
return 0 | ||
} | ||
ls.Push(LuaBinaryArray.New(ls, ba)) | ||
return 1 | ||
} | ||
|
||
func BinaryArrayLen(ls *lua.LState) int { | ||
ba := LuaBinaryArray.StartMeta(ls) | ||
ls.Push(lua.LNumber(len(ba.data))) | ||
return 1 | ||
} | ||
|
||
func BinaryArrayIndex(ls *lua.LState) int { | ||
ba, key := LuaBinaryArray.StartIndex(ls) | ||
switch key { | ||
case "size": | ||
ls.Push(lua.LNumber(len(ba.data))) | ||
case "str": | ||
ls.Push(lua.LString(ba.data)) | ||
case "data": | ||
ls.Error(lua.LString("binaryArray data property inaccessible"), 0) | ||
return 0 | ||
default: | ||
ls.Push(ls.GetField(LuaBinaryArray.Metatable(ls), key)) | ||
} | ||
return 1 | ||
} | ||
|
||
func BinaryArraySlice(ls *lua.LState) int { | ||
var startPos, endPos int | ||
ba := LuaBinaryArray.StartMeta(ls) | ||
if luaStartPos, ok := ls.Get(2).(lua.LNumber); ok { | ||
startPos = max(int(luaStartPos), 0) | ||
} else { | ||
startPos = 0 | ||
} | ||
if luaEndPos, ok := ls.Get(3).(lua.LNumber); ok { | ||
endPos = min(int(luaEndPos), len(ba.data)) | ||
} else { | ||
endPos = len(ba.data) | ||
} | ||
ls.Push(lua.LString(ba.data[startPos:endPos])) | ||
return 1 | ||
} | ||
|
||
func BinaryArrayGrow(ls *lua.LState) int { | ||
ba := LuaBinaryArray.StartMeta(ls) | ||
newsize := int(ls.CheckNumber(2)) | ||
if newsize > len(ba.data) { | ||
newdata := make([]byte, newsize) | ||
copy(newdata[newsize-len(ba.data):], ba.data) | ||
ba.data = newdata | ||
} | ||
return 0 | ||
} | ||
|
||
func BinaryArrayPad(ls *lua.LState) int { | ||
ba := LuaBinaryArray.StartMeta(ls) | ||
n := int(ls.CheckNumber(2)) | ||
startPos := int(ls.CheckNumber(3)) | ||
for i := range n { | ||
ba.data[startPos+i] = 0 | ||
} | ||
return 0 | ||
} | ||
|
||
func BinaryArraySet(ls *lua.LState) int { | ||
ba := LuaBinaryArray.StartMeta(ls) | ||
idx := int(ls.CheckNumber(3)) | ||
value := ls.Get(2) | ||
if num, ok := value.(lua.LNumber); ok { | ||
ba.data[idx] = byte(num) | ||
} | ||
if str, ok := value.(lua.LString); ok { | ||
ba.data[idx] = str[0] | ||
} | ||
return 0 | ||
} | ||
|
||
// (fmt, ...) | ||
// return string.pack(fmt, ...) | ||
func BinaryArrayPack(ls *lua.LState) int { | ||
panic("TODO") | ||
} | ||
|
||
// fmt, s, pos | ||
// return string.unpack(fmt, ba.data, pos+1) | ||
func BinaryArrayUnpack(ls *lua.LState) int { | ||
panic("TODO") | ||
} |
Oops, something went wrong.