diff --git a/base/bit.go b/base/bit.go index 20f945a..93b6720 100644 --- a/base/bit.go +++ b/base/bit.go @@ -112,4 +112,4 @@ func (BitFactory) Reverse32(v uint32) uint32 { func (BitFactory) Reverse64(v uint64) uint64 { return bits.Reverse64(v) -} \ No newline at end of file +} diff --git a/base/buffer.go b/base/buffer.go index 8cb1c99..bdaaed9 100644 --- a/base/buffer.go +++ b/base/buffer.go @@ -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 { diff --git a/base/tobuffer.go b/base/tobuffer.go index f51abb4..8372a35 100644 --- a/base/tobuffer.go +++ b/base/tobuffer.go @@ -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 } @@ -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) } diff --git a/crypto/aes/aes.go b/crypto/aes/aes.go new file mode 100644 index 0000000..c0985b5 --- /dev/null +++ b/crypto/aes/aes.go @@ -0,0 +1 @@ +package aes diff --git a/crypto/rand/rand.go b/crypto/rand/rand.go index 93c1f56..3af7069 100644 --- a/crypto/rand/rand.go +++ b/crypto/rand/rand.go @@ -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() } diff --git a/io/reader.go b/io/reader.go index 2fe75e9..6289064 100644 --- a/io/reader.go +++ b/io/reader.go @@ -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" ) @@ -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 { diff --git a/io/writer.go b/io/writer.go index f6896c0..32aaf48 100644 --- a/io/writer.go +++ b/io/writer.go @@ -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{} @@ -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) @@ -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: @@ -51,15 +90,20 @@ 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 { @@ -67,7 +111,6 @@ func (w *StreamWriter) Endian(big bool) { } else { w.order = binary.LittleEndian } - w.writer.Size() } func (w *StreamWriter) Len() int { return w.writer.Buffered() @@ -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 @@ -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[:]) +} diff --git a/os/os.go b/os/os.go new file mode 100644 index 0000000..79d9d9a --- /dev/null +++ b/os/os.go @@ -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 +}