Skip to content

Commit

Permalink
Implement part of BinIO structure
Browse files Browse the repository at this point in the history
  • Loading branch information
minoki committed Oct 15, 2023
1 parent 0e87872 commit 588addf
Show file tree
Hide file tree
Showing 9 changed files with 448 additions and 1 deletion.
39 changes: 38 additions & 1 deletion doc/BasisLibrary.md
Original file line number Diff line number Diff line change
Expand Up @@ -1144,6 +1144,44 @@ structure TextIO : sig
end
```

## structure BinIO - partial

```sml
structure BinIO :> sig
(* IMPERATIVE_IO *)
(* structure StreamIO : STREAM_IO *)
type vector = Word8Vector.vector
type elem = Word8.word
type instream
type outstream
val input : instream -> vector
val input1 : instream -> elem option
val inputN : instream * int -> vector
val inputAll : instream -> vector
(* val canInput : instream * int -> int option *)
(* val lookahead : instream -> elem option *)
val closeIn : instream -> unit
val endOfStream : instream -> bool
val output : outstream * vector -> unit
val output1 : outstream * elem -> unit
val flushOut : outstream -> unit
val closeOut : outstream -> unit
(* val mkInstream : StreamIO.instream -> instream *)
(* val getInstream : instream -> StreamIO.instream *)
(* val setInstream : instream * StreamIO.instream -> unit *)
(* val mkOutstream : StreamIO.outstream -> outstream *)
(* val getOutstream : outstream -> StreamIO.outstream *)
(* val setOutstream : outstream * StreamIO.outstream -> unit *)
(* val getPosOut : outstream -> StreamIO.out_pos *)
(* val setPosOut : outstream * StreamIO.out_pos -> unit *)
(* BIN_IO *)
val openIn : string -> instream
val openOut : string -> outstream
val openAppend : string -> outstream
end
```

## structure OS - partial

```sml
Expand Down Expand Up @@ -1350,7 +1388,6 @@ The GC time returned by this structure is always zero.

```sml
signature BIN_IO
structure BinIO :> BIN_IO
signature IMPERATIVE_IO
signature PRIM_IO
structure BinPrimIO :> PRIM_IO where ...
Expand Down
1 change: 1 addition & 0 deletions lib/lunarml/ml/basis/basis.mlb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ signature VECTOR_SLICE
signature WORD
structure Array
structure ArraySlice
structure BinIO
structure Bool
structure BoolArray
structure BoolArraySlice
Expand Down
115 changes: 115 additions & 0 deletions lib/lunarml/ml/basis/lua/bin-io.sml
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
structure BinIO :> sig
type vector = Word8Vector.vector
type elem = Word8.word
type instream
type outstream
val input : instream -> vector
val input1 : instream -> elem option
val inputN : instream * int -> vector
val inputAll : instream -> vector
val closeIn : instream -> unit
val endOfStream : instream -> bool
val output : outstream * vector -> unit
val output1 : outstream * elem -> unit
val flushOut : outstream -> unit
val closeOut : outstream -> unit
val openIn : string -> instream
val openOut : string -> outstream
val openAppend : string -> outstream
end = struct
local
val io = LunarML.assumeDiscardable Lua.global "io"
val io_open = LunarML.assumeDiscardable Lua.field (io, "open")
structure Instream :> sig
type instream
type vector = Word8Vector.vector
type elem = Word8.word
val input : instream -> vector
val input1 : instream -> elem option
val inputN : instream * int -> vector
val inputAll : instream -> vector
(* val canInput *)
(* val lookahead *)
val closeIn : instream -> unit
val endOfStream : instream -> bool
val openIn : string -> instream
end = struct
type instream = Lua.value
type vector = Word8Vector.vector
type elem = Word8.word
fun input f = let val result = Vector.sub (Lua.method (f, "read") #[Lua.fromInt 1024], 0)
in if Lua.isFalsy result then
Word8Vector.fromList []
else
Lua.unsafeFromValue result : vector
end
fun input1 f = let val result = Vector.sub (Lua.method (f, "read") #[Lua.fromInt 1], 0)
in if Lua.isNil result then
NONE
else
SOME (Word8Vector.sub (Lua.unsafeFromValue result : vector, 0))
end
fun inputN (f, n : int) = if n < 0 then
raise Size
else
let val result = Vector.sub (Lua.method (f, "read") #[Lua.fromInt n], 0)
in if Lua.isNil result then
Word8Vector.fromList []
else
Lua.unsafeFromValue result : vector
end
fun inputAll f = let val result = Vector.sub (Lua.method (f, "read") #[Lua.fromString "a"], 0)
in Lua.unsafeFromValue result : vector
end
fun closeIn f = (Lua.method (f, "close") #[]; ())
fun endOfStream f = let val result = Vector.sub (Lua.method (f, "read") #[Lua.fromInt 0], 0)
in if Lua.isFalsy result then
true
else
false
end
(* BIN_IO *)
fun openIn f = let val (r0, message) = Lua.call2 io_open #[Lua.fromString f, Lua.fromString "rb"]
in if Lua.isNil r0 then
raise IO.Io { name = f, function = "BinIO.openIn", cause = Fail (Lua.unsafeFromValue message) } (* TODO: cause *)
else
r0
end
end
structure Outstream :> sig
type outstream
type vector = Word8Vector.vector
type elem = Word8.word
val output : outstream * vector -> unit
val output1 : outstream * elem -> unit
val flushOut : outstream -> unit
val closeOut : outstream -> unit
val openOut : string -> outstream
val openAppend : string -> outstream
end = struct
type outstream = Lua.value
type vector = Word8Vector.vector
type elem = Word8.word
fun output (f, s : Word8Vector.vector) = (Lua.method (f, "write") #[Lua.unsafeToValue s]; ())
fun output1 (f, c : Word8.word) = (Lua.method (f, "write") #[Lua.fromString (String.str (Byte.byteToChar c))]; ())
fun flushOut f = (Lua.method (f, "flush") #[]; ())
fun closeOut f = (Lua.method (f, "close") #[]; ())
(* BIN_IO *)
fun openOut f = let val (r0, message) = Lua.call2 io_open #[Lua.fromString f, Lua.fromString "wb"]
in if Lua.isNil r0 then
raise IO.Io { name = f, function = "BinIO.openOut", cause = Fail (Lua.unsafeFromValue message) } (* TODO: cause *)
else
r0
end
fun openAppend f = let val (r0, message) = Lua.call2 io_open #[Lua.fromString f, Lua.fromString "ab"]
in if Lua.isNil r0 then
raise IO.Io { name = f, function = "BinIO.openAppend", cause = Fail (Lua.unsafeFromValue message) } (* TODO: cause *)
else
r0
end
end
in
open Instream
open Outstream
end (* local *)
end; (* structure BinIO *)
Loading

0 comments on commit 588addf

Please sign in to comment.