Class: Wardite::WasiSnapshotPreview1

Inherits:
Object
  • Object
show all
Defined in:
lib/wardite/wasi.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeWasiSnapshotPreview1

Returns a new instance of WasiSnapshotPreview1.



6
7
8
9
10
11
12
# File 'lib/wardite/wasi.rb', line 6

def initialize
  @fd_table = [
    STDIN,
    STDOUT,
    STDERR,
  ]
end

Instance Attribute Details

#fd_tableObject

: Array



4
5
6
# File 'lib/wardite/wasi.rb', line 4

def fd_table
  @fd_table
end

Instance Method Details

#fd_write(store, args) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/wardite/wasi.rb', line 17

def fd_write(store, args)
  iargs = args.map do |elm|
    if elm.is_a?(Integer)
      elm
    else
      raise Wardite::ArgumentError, "invalid type of args: #{args.inspect}"
    end
  end #: Array[Integer]
  fd, iovs, iovs_len, rp = *iargs
  if !fd || !iovs || !iovs_len || !rp
    raise Wardite::ArgumentError, "args too short"
  end
  file = self.fd_table[fd]
  memory = store.memories[0]
  nwritten = 0
  iovs_len.times do
    start = unpack_le_int(memory.data[iovs...(iovs+4)])
    iovs += 4
    slen = unpack_le_int(memory.data[iovs...(iovs+4)])
    iovs += 4
    # TODO: parallel write?
    nwritten += file.write(memory.data[start...(start+slen)])
  end

  memory.data[rp...(rp+4)] = [nwritten].pack("I")

  0
end

#to_moduleObject



47
48
49
50
51
# File 'lib/wardite/wasi.rb', line 47

def to_module
  {
    fd_write: lambda{|store, args| self.fd_write(store, args) },
  }
end