Class: IO

Inherits:
Object show all
Defined in:
lib/rlang/lib/io.rb

Constant Summary collapse

@@num_bytes_written =
0
@@num_bytes_read =
0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fd) ⇒ IO

Returns a new instance of IO.



15
16
17
# File 'lib/rlang/lib/io.rb', line 15

def initialize(fd)
  @fd = fd
end

Instance Attribute Details

#fdObject

Returns the value of attribute fd.



10
11
12
# File 'lib/rlang/lib/io.rb', line 10

def fd
  @fd
end

Instance Method Details



28
29
30
# File 'lib/rlang/lib/io.rb', line 28

def print(stg)
  self.write(stg)
end

#puts(stg) ⇒ Object



32
33
34
35
36
37
38
39
40
# File 'lib/rlang/lib/io.rb', line 32

def puts(stg)
  arg stg: :String
  result :none
  ciovec = WASI::CIOVec.new(2)
  ciovec << stg
  ciovec << "\n"
  errno = WASI.fd_write(@fd, ciovec.ciovs.ptr, 2, @@num_bytes_written.addr)
  ciovec.free
end

#readObject



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/rlang/lib/io.rb', line 42

def read
  result :String
  local stg: :String
  iovec = WASI::IOVec.new(1)
  errno = WASI.fd_read(@fd, iovec.iovs.ptr, 1, @@num_bytes_read.addr)
  # -1 below because of \0 terminated string
  stg = String.new(iovec.iovs[0], @@num_bytes_read-1) 
  # Nullify the iovs entry used by the String object so it is not freed
  iovec.iovs[0] = 0
  iovec.free
  stg
end

#write(stg) ⇒ Object



19
20
21
22
23
24
25
26
# File 'lib/rlang/lib/io.rb', line 19

def write(stg)
  arg stg: :String
  ciovec = WASI::CIOVec.new(1)
  ciovec << stg
  errno = WASI.fd_write(@fd, ciovec.ciovs.ptr, 1, @@num_bytes_written.addr)
  ciovec.free
  errno
end