Class: Cry::Wasmtime

Inherits:
Object
  • Object
show all
Defined in:
lib/cry/wasmtime.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(wasm_bytes = nil) ⇒ Wasmtime

Returns a new instance of Wasmtime.



7
8
9
# File 'lib/cry/wasmtime.rb', line 7

def initialize(wasm_bytes = nil)
  load_wasm(wasm_bytes) if wasm_bytes
end

Instance Attribute Details

#instanceObject (readonly)

Returns the value of attribute instance.



5
6
7
# File 'lib/cry/wasmtime.rb', line 5

def instance
  @instance
end

Instance Method Details

#function(name) ⇒ Object



30
31
32
# File 'lib/cry/wasmtime.rb', line 30

def function(name)
  instance.export(name.to_s).to_func
end

#get_view(_addr, _type) ⇒ Object



65
66
67
# File 'lib/cry/wasmtime.rb', line 65

def get_view(_addr, _type)
  raise 'not implemented'
end

#invoke(name, *args) ⇒ Object



34
35
36
# File 'lib/cry/wasmtime.rb', line 34

def invoke(name, *args)
  instance.invoke(name.to_s, *args)
end

#load_wasm(wasm_bytes) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/cry/wasmtime.rb', line 11

def load_wasm(wasm_bytes)
  engine = ::Wasmtime::Engine.new
  mod = ::Wasmtime::Module.new(engine, wasm_bytes)

  linker = ::Wasmtime::Linker.new(engine, wasi: true)

  wasi_ctx = ::Wasmtime::WasiCtxBuilder
             .new.set_stdin_string('hi!')
             .inherit_stdout
             .inherit_stderr
             .set_argv(ARGV)
             .set_env(ENV)

  store = ::Wasmtime::Store.new(engine, wasi_ctx: wasi_ctx)

  @instance = linker.instantiate(store, mod)
  self
end

#memoryObject



42
43
44
# File 'lib/cry/wasmtime.rb', line 42

def memory
  instance.export('memory').to_memory
end

#read_memory(addr, t2, len) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/cry/wasmtime.rb', line 69

def read_memory(addr, t2, len)
  case t2
  when 'int8'    then memory.read(addr, len * 1).unpack('c*')
  when 'uint8'   then memory.read(addr, len * 1).unpack('C*')
  when 'int16'   then memory.read(addr, len * 2).unpack('s*')
  when 'uint16'  then memory.read(addr, len * 2).unpack('S*')
  when 'int32'   then memory.read(addr, len * 4).unpack('l*')
  when 'uint32'  then memory.read(addr, len * 4).unpack('L*')
  when 'int64'   then memory.read(addr, len * 8).unpack('q*')
  when 'uint64'  then memory.read(addr, len * 8).unpack('Q*')
  when 'float32' then memory.read(addr, len * 4).unpack('e*')
  when 'float64' then memory.read(addr, len * 8).unpack('E*')
  else raise "unsupported type: #{t2}"
  end
end

#startObject



38
39
40
# File 'lib/cry/wasmtime.rb', line 38

def start
  invoke('_start')
end

#write_memory(addr, t2, arg) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/cry/wasmtime.rb', line 46

def write_memory(addr, t2, arg)
  memory.write(
    addr,
    case t2
    when 'int8'    then arg.pack('c*')
    when 'uint8'   then arg.pack('C*')
    when 'int16'   then arg.pack('s*')
    when 'uint16'  then arg.pack('S*')
    when 'int32'   then arg.pack('l*')
    when 'uint32'  then arg.pack('L*')
    when 'int64'   then arg.pack('q*')
    when 'uint64'  then arg.pack('Q*')
    when 'float32' then arg.pack('e*')
    when 'float64' then arg.pack('E*')
    else raise "unsupported type: #{t2}"
    end
  )
end