Skip to content

Commit

Permalink
remove tobuffer
Browse files Browse the repository at this point in the history
  • Loading branch information
vlorc committed Dec 12, 2018
1 parent d81d463 commit eaeefa7
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 13 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func main() {
+ [dns](https://github.com/vlorc/lua-vm/blob/master/demo/dns.lua)
+ [http](https://github.com/vlorc/lua-vm/blob/master/demo/http.lua)
+ [time](https://github.com/vlorc/lua-vm/blob/master/demo/time.lua)
+ [hash](https://github.com/vlorc/lua-vm/blob/master/demo/hash.lua)
+ [crypto](https://github.com/vlorc/lua-vm/blob/master/demo/crypto.lua)
+ [regexp](https://github.com/vlorc/lua-vm/blob/master/demo/regexp.lua)
+ [bit](https://github.com/vlorc/lua-vm/blob/master/demo/bit.lua)
+ [file](https://github.com/vlorc/lua-vm/blob/master/demo/file.lua)
2 changes: 1 addition & 1 deletion README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func main() {
+ [dns](https://github.com/vlorc/lua-vm/blob/master/demo/dns.lua)
+ [http](https://github.com/vlorc/lua-vm/blob/master/demo/http.lua)
+ [time](https://github.com/vlorc/lua-vm/blob/master/demo/time.lua)
+ [hash](https://github.com/vlorc/lua-vm/blob/master/demo/hash.lua)
+ [crypto](https://github.com/vlorc/lua-vm/blob/master/demo/crypto.lua)
+ [regexp](https://github.com/vlorc/lua-vm/blob/master/demo/regexp.lua)
+ [bit](https://github.com/vlorc/lua-vm/blob/master/demo/bit.lua)
+ [file](https://github.com/vlorc/lua-vm/blob/master/demo/file.lua)
4 changes: 4 additions & 0 deletions base/bit.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,7 @@ func (BitFactory) Or(v, r uint64) uint64 {
func (BitFactory) Not(v uint64) uint64 {
return ^v
}

func (BitFactory) Test(v, r uint64) bool {
return 0 != (v >> r)&1
}
5 changes: 3 additions & 2 deletions base/buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package base

import (
"bytes"
"layeh.com/gopher-luar"
"unicode/utf8"
"unsafe"
)
Expand All @@ -18,8 +19,8 @@ func (BufferFactory) Alloc(length int, args ...int) Buffer {
return __allocBuffer(length, args...)
}

func (BufferFactory) Form(str string) Buffer {
return Buffer(str)
func (BufferFactory) Form(L *luar.LState) int {
return toBuffer(L)
}

func (BufferFactory) FormNumber(val ...int) Buffer {
Expand Down
51 changes: 44 additions & 7 deletions base/tobuffer.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,57 @@
package base

import (
"bytes"
"encoding"
"encoding/json"
"github.com/yuin/gopher-lua"
"layeh.com/gopher-luar"
"reflect"
"unsafe"
)

func ToBuffer(L *lua.LState) int {
buf := __toBuffer(L)
L.Push(luar.New(L, buf))
func toBuffer(L *luar.LState) int {
buf := __toBuffer(L.LState)
L.Push(luar.New(L.LState, buf))
return 1
}

func __toBuffer(L *lua.LState) Buffer {
top := L.GetTop()
if top > 1 {
return __newBufferN(L)
}
if 1 == top {
return __newBuffer(L.Get(1))
return __newBuffer1(L.Get(1))
}
return nil
}

func __newBufferN(L *lua.LState) Buffer {
v := L.Get(1)
switch v.Type() {
case lua.LTString:
r := make([]string, L.GetTop())
for i := L.GetTop(); i > 0; i-- {
r[i-1] = string(L.Get(i).(lua.LString))
}
return __stringBuffer(r...)
case lua.LTNumber:
r := make([]int, L.GetTop())
for i := L.GetTop(); i > 0; i-- {
r[i-1] = int(L.Get(i).(lua.LNumber))
}
return __numberBuffer(r...)
case lua.LTUserData:
r := make([]interface{}, L.GetTop())
for i := L.GetTop(); i > 0; i-- {
r[i-1] = int(L.Get(i).(lua.LNumber))
}
return __writeBuffer(r...)
}
return nil
}

func __newBuffer(v lua.LValue) Buffer {
func __newBuffer1(v lua.LValue) Buffer {
switch v.Type() {
case lua.LTString:
__stringBuffer(string(v.(lua.LString)))
Expand All @@ -36,7 +64,6 @@ func __newBuffer(v lua.LValue) Buffer {
}

func __dataBuffer(v interface{}) Buffer {
reflect.TypeOf(v)
switch r := v.(type) {
case *[]byte:
if nil != r {
Expand Down Expand Up @@ -102,3 +129,13 @@ func __numberBuffer(val ...int) Buffer {
}
return buf
}

func __writeBuffer(val ...interface{}) Buffer {
var buf bytes.Buffer
for _, v := range val {
if b := __dataBuffer(v); len(b) > 0 {
buf.Write(b)
}
}
return Buffer(buf.Bytes())
}
5 changes: 5 additions & 0 deletions demo/buffer.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
local buffer = require("buffer")

local buf = buffer:form(1,2,3,4,5,6,7,8,9)

print(buf:toHash())
3 changes: 1 addition & 2 deletions main/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ func main() {
network := driver.DirectDriver{}
filesystem := fs.NewRelativeFileFactory(".", fs.NativeFileFactory{})
p := pool.NewLuaPool().Preload(
pool.Value("tobuffer", base.ToBuffer),
pool.Module("net.tcp", tcp.NewTCPFactory(network)),
pool.Module("net.udp", udp.NewUDPFactory(network)),
pool.Module("net.http", http.NewHTTPFactory(network)),
Expand All @@ -49,7 +48,7 @@ func main() {
)

now := time.Now()
err := p.DoFile("demo/dns.lua")
err := p.DoFile("demo/buffer.lua")
if nil != err {
println("error: ", err.Error())
}
Expand Down

0 comments on commit eaeefa7

Please sign in to comment.