Skip to content

Commit

Permalink
Merge branch 'develop' of https://github.com/skycoinproject/cx into d…
Browse files Browse the repository at this point in the history
…evelop_opcodes
  • Loading branch information
asahi3g committed Dec 30, 2019
2 parents 08c3e34 + fc9ea87 commit 3ec997f
Show file tree
Hide file tree
Showing 5 changed files with 204 additions and 51 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
- Increased opengl version from 2.1 to 3.2.
- Added opengl bindings : glClearBufferI, glClearBufferUI, glClearBufferF, glBlendFuncSeparate, glDrawBuffers.
- Added opengl constants : GL_NONE, GL_RED, GL_RGBA16F, GL_HALF_FLOAT, GL_UNSIGNED_INT_24_8, GL_R8.
- Added opcodes for reading binary files : os.Seek, os.ReadUI16, os.ReadUI32, os.ReadF32, os.ReadUI16Slice, os.ReadUI32Slice, os.ReadF32Slice.
* Changes
* Removed cx-games as a module. It was just confusing as users would be
redirected to an outdated version of the repo and the games are already
Expand Down
9 changes: 4 additions & 5 deletions cx/op_gltext.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@ func op_gltext_LoadTrueType(prgrm *CXProgram) {
fp := prgrm.GetFramePointer()

inp1, inp2, inp3, inp4, inp5, inp6 := expr.Inputs[0], expr.Inputs[1], expr.Inputs[2], expr.Inputs[3], expr.Inputs[4], expr.Inputs[5]

if theFont, err := gltext.LoadTruetype(openFiles[ReadStr(fp, inp2)], ReadI32(fp, inp3), rune(ReadI32(fp, inp4)), rune(ReadI32(fp, inp5)), gltext.Direction(ReadI32(fp, inp6))); err == nil {
fonts[ReadStr(fp, inp1)] = theFont
} else {
panic(err)
if file := validFile(ReadI32(fp, inp1)); file != nil {
if theFont, err := gltext.LoadTruetype(file, ReadI32(fp, inp3), rune(ReadI32(fp, inp4)), rune(ReadI32(fp, inp5)), gltext.Direction(ReadI32(fp, inp6))); err == nil {
fonts[ReadStr(fp, inp2)] = theFont
}
}
}

Expand Down
201 changes: 168 additions & 33 deletions cx/op_os.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@ import (
"bytes"
"encoding/binary"
//"fmt"
"github.com/amherag/skycoin/src/cipher/encoder"
"io/ioutil"
"math"
"os"
"os/exec"
"strings"
"syscall"
"time"

"github.com/amherag/skycoin/src/cipher/encoder"
)

const (
Expand All @@ -23,93 +22,229 @@ const (
OS_SEEK_END
)

var openFiles map[string]*os.File = make(map[string]*os.File, 0)
var openFiles []*os.File
var freeFiles []int32

// helper function used to validate json handle from expr
func validFileFromExpr(expr *CXExpression, fp int) *os.File {
handle := ReadI32(fp, expr.Inputs[0])
return validFile(handle)
}

// helper function used to validate file handle from i32
func validFile(handle int32) *os.File {
if handle >= 0 && handle < int32(len(openFiles)) && openFiles[handle] != nil {
return openFiles[handle]
}
return nil
}

func op_os_ReadAllText(prgrm *CXProgram) {
expr := prgrm.GetExpr()
fp := prgrm.GetFramePointer()

var success bool
if byts, err := ioutil.ReadFile(ReadStr(fp, expr.Inputs[0])); err == nil {
WriteObject(GetFinalOffset(fp, expr.Outputs[0]), encoder.Serialize(string(byts)))
} else {
panic(err)
success = true
}

WriteBool(GetFinalOffset(fp, expr.Outputs[1]), success)
}

func op_os_Open(prgrm *CXProgram) {
expr := prgrm.GetExpr()
fp := prgrm.GetFramePointer()

inp1 := expr.Inputs[0]
name := ReadStr(fp, inp1)
if file, err := os.Open(name); err == nil {
openFiles[name] = file
} else {
panic(err)
handle := int32(-1)

if file, err := os.Open(ReadStr(fp, expr.Inputs[0])); err == nil {
freeCount := len(freeFiles)
if freeCount > 0 {
freeCount--
handle = int32(freeFiles[freeCount])
freeFiles = freeFiles[:freeCount]
} else {
handle = int32(len(openFiles))
openFiles = append(openFiles, nil)
}

if handle < 0 || handle >= int32(len(openFiles)) {
panic("internal error")
}

openFiles[handle] = file
}

WriteI32(GetFinalOffset(fp, expr.Outputs[0]), int32(handle))
}

func op_os_Close(prgrm *CXProgram) {
expr := prgrm.GetExpr()
fp := prgrm.GetFramePointer()

inp1 := expr.Inputs[0]
name := ReadStr(fp, inp1)
if file, ok := openFiles[name]; ok {
if err := file.Close(); err != nil {
panic(err)
success := false

handle := ReadI32(fp, expr.Inputs[0])
if file := validFile(handle); file != nil {
if err := file.Close(); err == nil {
success = true
}

openFiles[handle] = nil
freeFiles = append(freeFiles, handle)
}

WriteBool(GetFinalOffset(fp, expr.Outputs[0]), success)
}

func op_os_Seek(prgrm *CXProgram) {
expr := prgrm.GetExpr()
fp := prgrm.GetFramePointer()

file := openFiles[ReadStr(fp, expr.Inputs[0])]
file.Seek(ReadI64(fp, expr.Inputs[1]), int(ReadI32(fp, expr.Inputs[2])))
offset := int64(-1)
if file := validFileFromExpr(expr, fp); file != nil {
var err error
if offset, err = file.Seek(ReadI64(fp, expr.Inputs[1]), int(ReadI32(fp, expr.Inputs[2]))); err != nil {
offset = -1
}
}
WriteI64(GetFinalOffset(fp, expr.Outputs[0]), offset)
}

func op_os_ReadF32(prgrm *CXProgram) {
expr := prgrm.GetExpr()
fp := prgrm.GetFramePointer()

file := openFiles[ReadStr(fp, expr.Inputs[0])]
var value float32
err := binary.Read(file, binary.LittleEndian, &value)
if err != nil {
panic(err)
var success bool

if file := validFileFromExpr(expr, fp); file != nil {
if err := binary.Read(file, binary.LittleEndian, &value); err == nil {
success = true
}
}

WriteF32(GetFinalOffset(fp, expr.Outputs[0]), value)
WriteBool(GetFinalOffset(fp, expr.Outputs[1]), success)
}

func op_os_ReadF32Slice(prgrm *CXProgram) {
expr := prgrm.GetExpr()
fp := prgrm.GetFramePointer()

var success bool

outputSlicePointer := GetFinalOffset(fp, expr.Outputs[0])
outputSliceOffset := GetPointerOffset(int32(outputSlicePointer))
count := ReadI32(fp, expr.Inputs[1])
if count > 0 {
if file := validFileFromExpr(expr, fp); file != nil {
values := make([]float32, count)
if err := binary.Read(file, binary.LittleEndian, values); err == nil {
success = true
outputSliceOffset = int32(sliceResize(outputSliceOffset, count, 4))
outputSliceData := GetSliceData(outputSliceOffset, 4)
for i := int32(0); i < count; i++ {
WriteMemF32(outputSliceData, int(i*4), values[i])
}
}
}
}

WriteMemory(GetFinalOffset(fp, expr.Outputs[0]), FromF32(value))
WriteI32(outputSlicePointer, outputSliceOffset)
WriteBool(GetFinalOffset(fp, expr.Outputs[1]), success)

}

func op_os_ReadUI32(prgrm *CXProgram) {
expr := prgrm.GetExpr()
fp := prgrm.GetFramePointer()

file := openFiles[ReadStr(fp, expr.Inputs[0])]
var value uint32
err := binary.Read(file, binary.LittleEndian, &value)
if err != nil {
panic(err)
var success bool

if file := validFileFromExpr(expr, fp); file != nil {
if err := binary.Read(file, binary.LittleEndian, &value); err == nil {
success = true
}
}

WriteUI32(GetFinalOffset(fp, expr.Outputs[0]), value)
WriteBool(GetFinalOffset(fp, expr.Outputs[1]), success)
}

func op_os_ReadUI32Slice(prgrm *CXProgram) {
expr := prgrm.GetExpr()
fp := prgrm.GetFramePointer()

var success bool

outputSlicePointer := GetFinalOffset(fp, expr.Outputs[0])
outputSliceOffset := GetPointerOffset(int32(outputSlicePointer))
count := ReadI32(fp, expr.Inputs[1])
if count > 0 {
if file := validFileFromExpr(expr, fp); file != nil {
values := make([]uint32, count)
if err := binary.Read(file, binary.LittleEndian, values); err == nil {
success = true
outputSliceOffset = int32(sliceResize(outputSliceOffset, count, 4))
outputSliceData := GetSliceData(outputSliceOffset, 4)
for i := int32(0); i < count; i++ {
WriteMemUI32(outputSliceData, int(i*4), values[i])
}
}
}
}

WriteMemory(GetFinalOffset(fp, expr.Outputs[0]), FromUI32(value))
WriteI32(outputSlicePointer, outputSliceOffset)
WriteBool(GetFinalOffset(fp, expr.Outputs[1]), success)

}

func op_os_ReadUI16(prgrm *CXProgram) {
expr := prgrm.GetExpr()
fp := prgrm.GetFramePointer()

file := openFiles[ReadStr(fp, expr.Inputs[0])]
var value uint16
err := binary.Read(file, binary.LittleEndian, &value)
if err != nil {
panic(err)
var success bool

if file := validFileFromExpr(expr, fp); file != nil {
if err := binary.Read(file, binary.LittleEndian, &value); err == nil {
success = true
}
}

WriteMemory(GetFinalOffset(fp, expr.Outputs[0]), FromUI16(value))
WriteUI16(GetFinalOffset(fp, expr.Outputs[0]), value)
WriteBool(GetFinalOffset(fp, expr.Outputs[1]), success)
}

func op_os_ReadUI16Slice(prgrm *CXProgram) {
expr := prgrm.GetExpr()
fp := prgrm.GetFramePointer()

var success bool

outputSlicePointer := GetFinalOffset(fp, expr.Outputs[0])
outputSliceOffset := GetPointerOffset(int32(outputSlicePointer))
count := ReadI32(fp, expr.Inputs[1])
if count > 0 {
if file := validFileFromExpr(expr, fp); file != nil {
values := make([]uint16, count)
if err := binary.Read(file, binary.LittleEndian, values); err == nil {
success = true
outputSliceOffset = int32(sliceResize(outputSliceOffset, count, 2))
outputSliceData := GetSliceData(outputSliceOffset, 2)
for i := int32(0); i < count; i++ {
WriteMemUI16(outputSliceData, int(i*2), values[i])
}
}
}
}

WriteI32(outputSlicePointer, outputSliceOffset)
WriteBool(GetFinalOffset(fp, expr.Outputs[1]), success)

}

func op_os_GetWorkingDirectory(prgrm *CXProgram) {
Expand Down
42 changes: 30 additions & 12 deletions cx/opcodes_base.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ const (
OP_OS_READ_F32
OP_OS_READ_UI32
OP_OS_READ_UI16
OP_OS_READ_F32_SLICE
OP_OS_READ_UI32_SLICE
OP_OS_READ_UI16_SLICE
OP_OS_RUN
OP_OS_EXIT

Expand Down Expand Up @@ -73,25 +76,34 @@ func init() {
[]*CXArgument{newOpPar(TYPE_STR, false)})
AddOpCode(OP_OS_OPEN, "os.Open",
[]*CXArgument{newOpPar(TYPE_STR, false)},
[]*CXArgument{})
[]*CXArgument{newOpPar(TYPE_I32, false)})
AddOpCode(OP_OS_CLOSE, "os.Close",
[]*CXArgument{newOpPar(TYPE_STR, false)},
[]*CXArgument{})
[]*CXArgument{newOpPar(TYPE_I32, false)},
[]*CXArgument{newOpPar(TYPE_BOOL, false)})
AddOpCode(OP_OS_SEEK, "os.Seek",
[]*CXArgument{newOpPar(TYPE_STR, false), newOpPar(TYPE_I64, false), newOpPar(TYPE_I32, false)},
[]*CXArgument{})
[]*CXArgument{newOpPar(TYPE_I32, false), newOpPar(TYPE_I64, false), newOpPar(TYPE_I32, false)},
[]*CXArgument{newOpPar(TYPE_I64, false)})
AddOpCode(OP_OS_READ_F32, "os.ReadF32",
[]*CXArgument{newOpPar(TYPE_STR, false)},
[]*CXArgument{newOpPar(TYPE_F32, false)})
[]*CXArgument{newOpPar(TYPE_I32, false)},
[]*CXArgument{newOpPar(TYPE_F32, false), newOpPar(TYPE_BOOL, false)})
AddOpCode(OP_OS_READ_UI32, "os.ReadUI32",
[]*CXArgument{newOpPar(TYPE_STR, false)},
[]*CXArgument{newOpPar(TYPE_UI32, false)})
[]*CXArgument{newOpPar(TYPE_I32, false)},
[]*CXArgument{newOpPar(TYPE_UI32, false), newOpPar(TYPE_BOOL, false)})
AddOpCode(OP_OS_READ_UI16, "os.ReadUI16",
[]*CXArgument{newOpPar(TYPE_STR, false)},
[]*CXArgument{newOpPar(TYPE_UI16, false)})
[]*CXArgument{newOpPar(TYPE_I32, false)},
[]*CXArgument{newOpPar(TYPE_UI16, false), newOpPar(TYPE_BOOL, false)})
AddOpCode(OP_OS_READ_F32_SLICE, "os.ReadF32Slice",
[]*CXArgument{newOpPar(TYPE_I32, false), newOpPar(TYPE_I32, false)},
[]*CXArgument{newOpPar(TYPE_F32, true), newOpPar(TYPE_BOOL, false)})
AddOpCode(OP_OS_READ_UI32_SLICE, "os.ReadUI32Slice",
[]*CXArgument{newOpPar(TYPE_I32, false), newOpPar(TYPE_I32, false)},
[]*CXArgument{newOpPar(TYPE_UI32, true), newOpPar(TYPE_BOOL, false)})
AddOpCode(OP_OS_READ_UI16_SLICE, "os.ReadUI16Slice",
[]*CXArgument{newOpPar(TYPE_I32, false), newOpPar(TYPE_I32, false)},
[]*CXArgument{newOpPar(TYPE_UI16, true), newOpPar(TYPE_BOOL, false)})
AddOpCode(OP_OS_READ_ALL_TEXT, "os.ReadAllText",
[]*CXArgument{newOpPar(TYPE_STR, false)},
[]*CXArgument{newOpPar(TYPE_STR, false)})
[]*CXArgument{newOpPar(TYPE_STR, false), newOpPar(TYPE_BOOL, false)})
AddOpCode(OP_OS_RUN, "os.Run",
[]*CXArgument{newOpPar(TYPE_STR, false), newOpPar(TYPE_I32, false), newOpPar(TYPE_I32, false), newOpPar(TYPE_STR, false)},
[]*CXArgument{newOpPar(TYPE_I32, false), newOpPar(TYPE_I32, false), newOpPar(TYPE_STR, false)})
Expand Down Expand Up @@ -172,6 +184,12 @@ func handleBaseOpcode(opCode int) opcodeHandler {
return op_os_ReadUI32
case OP_OS_READ_UI16:
return op_os_ReadUI16
case OP_OS_READ_F32_SLICE:
return op_os_ReadF32Slice
case OP_OS_READ_UI32_SLICE:
return op_os_ReadUI32Slice
case OP_OS_READ_UI16_SLICE:
return op_os_ReadUI16Slice
case OP_OS_READ_ALL_TEXT:
return op_os_ReadAllText
case OP_OS_RUN:
Expand Down
2 changes: 1 addition & 1 deletion cx/opcodes_opengl.go
Original file line number Diff line number Diff line change
Expand Up @@ -720,7 +720,7 @@ func init() {

// gltext
AddOpCode(OP_GLTEXT_LOAD_TRUE_TYPE, "gltext.LoadTrueType",
[]*CXArgument{newOpPar(TYPE_STR, false), newOpPar(TYPE_STR, false), newOpPar(TYPE_I32, false), newOpPar(TYPE_I32, false), newOpPar(TYPE_I32, false), newOpPar(TYPE_I32, false)},
[]*CXArgument{newOpPar(TYPE_I32, false), newOpPar(TYPE_STR, false), newOpPar(TYPE_I32, false), newOpPar(TYPE_I32, false), newOpPar(TYPE_I32, false), newOpPar(TYPE_I32, false)},
[]*CXArgument{})
AddOpCode(OP_GLTEXT_PRINTF, "gltext.Printf",
[]*CXArgument{newOpPar(TYPE_STR, false), newOpPar(TYPE_F32, false), newOpPar(TYPE_F32, false), newOpPar(TYPE_STR, false)},
Expand Down

0 comments on commit 3ec997f

Please sign in to comment.