Skip to content

Commit

Permalink
os
Browse files Browse the repository at this point in the history
  • Loading branch information
vlorc committed Dec 17, 2018
1 parent 0d304b3 commit 537ab93
Show file tree
Hide file tree
Showing 8 changed files with 167 additions and 14 deletions.
2 changes: 1 addition & 1 deletion base/bit.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,4 @@ func (BitFactory) Reverse32(v uint32) uint32 {

func (BitFactory) Reverse64(v uint64) uint64 {
return bits.Reverse64(v)
}
}
2 changes: 1 addition & 1 deletion base/buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func (BufferFactory) Alloc(length int, args ...int) Buffer {
}

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

func (BufferFactory) FormNumber(val ...int) Buffer {
Expand Down
8 changes: 4 additions & 4 deletions base/tobuffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import (
"unsafe"
)

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

Expand Down Expand Up @@ -56,7 +56,7 @@ func __newBuffer1(v lua.LValue) Buffer {
case lua.LTString:
return __stringBuffer(string(v.(lua.LString)))
case lua.LTNumber:
return __allocBuffer(int(v.(lua.LNumber)), 0)
return __allocBuffer(int(v.(lua.LNumber)))
case lua.LTUserData:
return __dataBuffer(v.(*lua.LUserData).Value)
}
Expand Down
1 change: 1 addition & 0 deletions crypto/aes/aes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package aes
6 changes: 5 additions & 1 deletion crypto/rand/rand.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,14 @@ func (RandFactory) Uint8() uint {
return uint(__rand.Int31n(256))
}

func (RandFactory) Uint32() uint {
func (RandFactory) Uint16() uint {
return uint(__rand.Int31n(65536))
}

func (RandFactory) Uint32() uint {
return uint(__rand.Uint32())
}

func (RandFactory) Uint64() uint64 {
return __rand.Uint64()
}
Expand Down
21 changes: 20 additions & 1 deletion io/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import (
"bytes"
"encoding/binary"
"github.com/vlorc/lua-vm/base"
"github.com/yuin/gopher-lua"
"io"
"io/ioutil"
"layeh.com/gopher-luar"
"strings"
"unsafe"
)
Expand All @@ -31,7 +33,24 @@ type StreamReader struct {
}
type ReaderFactory struct{}

func (f ReaderFactory) New(b interface{}) Reader {
func (f ReaderFactory) New(L luar.LState) int {
v := L.Get(-1)
w := f.__newReader(v)
L.Push(luar.New(L.LState, w))
return 1
}

func (f ReaderFactory) __newReader(val lua.LValue) Reader {
switch val.Type() {
case lua.LTString:
return f.FormString(string(val.(lua.LString)))
case lua.LTUserData:
return f.__toReader(val.(*lua.LUserData).Value)
}
return nil
}

func (f ReaderFactory) __toReader(b interface{}) Reader {
switch r := b.(type) {
case *[]byte:
if nil != r {
Expand Down
122 changes: 116 additions & 6 deletions io/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import (
"bytes"
"encoding/binary"
"github.com/vlorc/lua-vm/base"
"github.com/yuin/gopher-lua"
"io"
"layeh.com/gopher-luar"
)

type WriterFactory struct{}
Expand All @@ -18,6 +20,7 @@ type Writer interface {
Len() int
Size() int
ReadFrom(io.Reader) (int64, error)
WriteTo(io.Writer) (int64, error)
WriteString(string) (int, error)
WriteLine(string) (int, error)
WriteInt8(int) (int, error)
Expand All @@ -31,8 +34,44 @@ type StreamWriter struct {
order binary.ByteOrder
}

func (f WriterFactory) New(b interface{}) Writer {
type BufferWriter struct {
writer *bytes.Buffer
order binary.ByteOrder
}

func (f WriterFactory) New(L luar.LState) int {
v := L.Get(-1)
w := f.__toWriter(v)
L.Push(luar.New(L.LState, w))
return 1
}

func (f WriterFactory) __newWriter(val lua.LValue) Writer {
switch val.Type() {
case lua.LTString:
return f.FormString(string(val.(lua.LString)))
case lua.LTNumber:
return f.FormSize(int(val.(lua.LNumber)))
case lua.LTUserData:
return f.__toWriter(val.(*lua.LUserData).Value)
}
return nil
}

func (f WriterFactory) __toWriter(b interface{}) Writer {
switch r := b.(type) {
case *[]byte:
if nil != r {
return f.FormBuffer(base.Buffer(*r))
}
case *base.Buffer:
if nil != r {
return f.FormBuffer(base.Buffer(*r))
}
case *string:
if nil != r {
return f.FormString(*r)
}
case []byte:
return f.FormBuffer(base.Buffer(r))
case base.Buffer:
Expand All @@ -51,23 +90,27 @@ func (WriterFactory) FormStream(w io.Writer) Writer {
order: binary.LittleEndian,
}
}

func (f WriterFactory) FormSize(size int) Writer {
return f.FormStream(bytes.NewBuffer(make([]byte, size)))
return f.FormBuffer(make([]byte, size))
}
func (f WriterFactory) FormString(str string) Writer {
return f.FormStream(bytes.NewBufferString(str))
return &BufferWriter{
writer: bytes.NewBufferString(str),
order: binary.LittleEndian,
}
}
func (f WriterFactory) FormBuffer(buf base.Buffer) Writer {
return f.FormStream(bytes.NewBuffer(buf))
return &BufferWriter{
writer: bytes.NewBuffer(buf),
order: binary.LittleEndian,
}
}
func (w *StreamWriter) Endian(big bool) {
if big {
w.order = binary.BigEndian
} else {
w.order = binary.LittleEndian
}
w.writer.Size()
}
func (w *StreamWriter) Len() int {
return w.writer.Buffered()
Expand All @@ -81,6 +124,9 @@ func (w *StreamWriter) Reset(ww io.Writer) {
func (w *StreamWriter) Flush() error {
return w.writer.Flush()
}
func (w *StreamWriter) WriteTo(io.Writer) (int64, error) {
return 0, nil
}
func (w *StreamWriter) WriteLine(str string) (n int, err error) {
if n, err = w.writer.WriteString(str); nil != err {
return 0, err
Expand Down Expand Up @@ -117,3 +163,67 @@ func (w *StreamWriter) WriteInt64(v int) (int, error) {
w.order.PutUint64(buf[:], uint64(v))
return w.writer.Write(buf[:])
}

func (w *BufferWriter) Endian(big bool) {
if big {
w.order = binary.BigEndian
} else {
w.order = binary.LittleEndian
}
}
func (w *BufferWriter) Len() int {
return w.writer.Len()
}
func (w *BufferWriter) Size() int {
return w.writer.Cap()
}
func (w *BufferWriter) Reset(ww io.Writer) {
iw, ok := ww.(*bytes.Buffer)
if ok {
w.writer = iw
} else {
w.writer.Reset()
}
}
func (w *BufferWriter) Flush() error {
return nil
}
func (w *BufferWriter) WriteTo(ww io.Writer) (int64, error) {
return w.writer.WriteTo(ww)
}
func (w *BufferWriter) WriteLine(str string) (n int, err error) {
if n, err = w.writer.WriteString(str); nil != err {
return 0, err
}
return n + 1, w.writer.WriteByte(byte('\n'))
}
func (w *BufferWriter) WriteString(str string) (n int, err error) {
return w.writer.WriteString(str)
}
func (w *BufferWriter) Write(p []byte) (n int, err error) {
return w.writer.Write(p)
}
func (w *BufferWriter) ReadFrom(r io.Reader) (int64, error) {
return w.writer.ReadFrom(r)
}
func (w *BufferWriter) WriteRune(v rune) (int, error) {
return w.writer.WriteRune(v)
}
func (w *BufferWriter) WriteInt8(v int) (int, error) {
return 1, w.writer.WriteByte(byte(v & 0xff))
}
func (w *BufferWriter) WriteInt16(v int) (int, error) {
var buf [2]byte
w.order.PutUint16(buf[:], uint16(v&0xffff))
return w.writer.Write(buf[:])
}
func (w *BufferWriter) WriteInt32(v int) (int, error) {
var buf [4]byte
w.order.PutUint32(buf[:], uint32(v&0xffffffff))
return w.writer.Write(buf[:])
}
func (w *BufferWriter) WriteInt64(v int) (int, error) {
var buf [8]byte
w.order.PutUint64(buf[:], uint64(v))
return w.writer.Write(buf[:])
}
19 changes: 19 additions & 0 deletions os/os.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package os

import (
"runtime"
)

type OSFactory struct{}

func (OSFactory) Version() string {
return runtime.Version()
}

func (OSFactory) Name() string {
return runtime.GOOS
}

func (OSFactory) Arch() string {
return runtime.GOARCH
}

0 comments on commit 537ab93

Please sign in to comment.