Skip to content

Commit

Permalink
fix: improve golang externs (#1133)
Browse files Browse the repository at this point in the history
* fix: improve golang externs
  • Loading branch information
ajewellamz authored Dec 17, 2024
1 parent 983f752 commit b6ee16e
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
os "os"
"path/filepath"

"github.com/dafny-lang/DafnyRuntimeGo/v4/dafny"
_dafny "github.com/dafny-lang/DafnyRuntimeGo/v4/dafny"
)

Expand All @@ -28,8 +29,7 @@ func (_static CompanionStruct_Default___) INTERNAL_ReadBytesFromFile(path _dafny

dat, err := ioutil.ReadFile(p)
if err != nil {
errAsSequence := _dafny.UnicodeSeqOfUtf8Bytes(err.Error())
return true, _dafny.EmptySeq, errAsSequence
return true, _dafny.EmptySeq, _dafny.SeqOfChars([]dafny.Char(err.Error())...)
}
datAsSequence := _dafny.SeqOfBytes(dat)
return false, datAsSequence, _dafny.EmptySeq
Expand All @@ -53,8 +53,7 @@ func (_static CompanionStruct_Default___) INTERNAL_WriteBytesToFile(path _dafny.
bytesArray := _dafny.ToByteArray(bytes)
err := ioutil.WriteFile(p, bytesArray, 0644)
if err != nil {
errAsSequence := _dafny.UnicodeSeqOfUtf8Bytes(err.Error())
return true, errAsSequence
return true, _dafny.SeqOfChars([]dafny.Char(err.Error())...)
}
return false, _dafny.EmptySeq
}
Expand All @@ -73,21 +72,24 @@ func (_static CompanionStruct_Default___) INTERNAL_AppendBytesToFile(path _dafny
}()

// Create directories
os.MkdirAll(filepath.Dir(p), os.ModePerm)
err := os.MkdirAll(filepath.Dir(p), os.ModePerm)
if err != nil {
return true, _dafny.SeqOfChars([]dafny.Char(err.Error())...)
}

bytesArray := _dafny.ToByteArray(bytes)

f, err := os.OpenFile(p, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return true, _dafny.UnicodeSeqOfUtf8Bytes(err.Error())
return true, _dafny.SeqOfChars([]dafny.Char(err.Error())...)
}

if _, err := f.Write(bytesArray); err != nil {
return true, _dafny.UnicodeSeqOfUtf8Bytes(err.Error())
return true, _dafny.SeqOfChars([]dafny.Char(err.Error())...)
}

if err := f.Close(); err != nil {
return true, _dafny.UnicodeSeqOfUtf8Bytes(err.Error())
return true, _dafny.SeqOfChars([]dafny.Char(err.Error())...)
}

return false, _dafny.EmptySeq
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ func (CompanionStruct_Default___) GetProcessCpuTimeMillis() int64 {
}

func GetProcessCpuTimeMillis() int64 {
usage := new(syscall.Rusage)
syscall.Getrusage(syscall.RUSAGE_SELF, usage)
var usage syscall.Rusage
err := syscall.Getrusage(syscall.RUSAGE_SELF, &usage)
if err != nil {
return 0
}
return (usage.Utime.Nano() + usage.Stime.Nano()) / 1000000
}
3 changes: 0 additions & 3 deletions StandardLibrary/test/GetOpt.dfy
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,6 @@ module GetOptTest {
]);
var x :- expect GetOptions(MyOptions, ["cmd", "--help"]);
var y :- expect NeedsHelp(MyOptions, x);
print "\n", y, "\n";
}

method {:test} TestHelpFail() {
Expand Down Expand Up @@ -196,11 +195,9 @@ module GetOptTest {

x :- expect GetOptions(MyOptions, ["MyProg", "--help"]);
var y :- expect NeedsHelp(MyOptions, x);
print "\n", y, "\n";

x :- expect GetOptions(MyOptions, ["MyProg", "command", "--help"]);
y :- expect NeedsHelp(MyOptions, x);
print "\n", y, "\n";
}

method {:test} TestDefault() {
Expand Down
8 changes: 8 additions & 0 deletions StandardLibrary/test/TestString.dfy
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,12 @@ module TestStrings {
expect y == [1,2,3,4,5];
}

// ensure that FileIO error are returned properly, and not a panic! or the like
// This fails to fail on Windows+Dotnet, becasue \ instead of /
// method {:test} TestBadFileIO()
// {
// var x := WriteBytesToFile("/../../MyFile", [1,2,3,4,5]);
// expect x.Failure?;
// }

}

0 comments on commit b6ee16e

Please sign in to comment.