Class: Relaton::Un::Wasm::Memory

Inherits:
Object
  • Object
show all
Defined in:
lib/relaton/un/wasm/memory.rb

Constant Summary collapse

PAGE =
65_536

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(initial_pages:, max_pages: nil) ⇒ Memory

Returns a new instance of Memory.



11
12
13
14
15
# File 'lib/relaton/un/wasm/memory.rb', line 11

def initialize(initial_pages:, max_pages: nil)
  @pages = initial_pages
  @max_pages = max_pages
  @buf = String.new("\x00".b * (initial_pages * PAGE), encoding: Encoding::ASCII_8BIT)
end

Instance Attribute Details

#max_pagesObject (readonly)

Returns the value of attribute max_pages.



9
10
11
# File 'lib/relaton/un/wasm/memory.rb', line 9

def max_pages
  @max_pages
end

Instance Method Details

#grow(delta) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/relaton/un/wasm/memory.rb', line 21

def grow(delta)
  new_pages = @pages + delta
  return -1 if @max_pages && new_pages > @max_pages

  old = @pages
  @pages = new_pages
  @buf << ("\x00".b * (delta * PAGE))
  old
end

#load_i16(addr) ⇒ Object



57
58
59
60
# File 'lib/relaton/un/wasm/memory.rb', line 57

def load_i16(addr)
  v = load_u16(addr)
  v >= 0x8000 ? v - 0x10000 : v
end

#load_i32(addr) ⇒ Object



67
68
69
70
# File 'lib/relaton/un/wasm/memory.rb', line 67

def load_i32(addr)
  v = load_u32(addr)
  v >= 0x8000_0000 ? v - 0x1_0000_0000 : v
end

#load_i64(addr) ⇒ Object



77
78
79
80
# File 'lib/relaton/un/wasm/memory.rb', line 77

def load_i64(addr)
  v = load_u64(addr)
  v >= 0x8000_0000_0000_0000 ? v - 0x1_0000_0000_0000_0000 : v
end

#load_i8(addr) ⇒ Object



47
48
49
50
# File 'lib/relaton/un/wasm/memory.rb', line 47

def load_i8(addr)
  v = load_u8(addr)
  v >= 0x80 ? v - 0x100 : v
end

#load_u16(addr) ⇒ Object



52
53
54
55
# File 'lib/relaton/un/wasm/memory.rb', line 52

def load_u16(addr)
  bounds!(addr, 2)
  @buf.byteslice(addr, 2).unpack1("v")
end

#load_u32(addr) ⇒ Object



62
63
64
65
# File 'lib/relaton/un/wasm/memory.rb', line 62

def load_u32(addr)
  bounds!(addr, 4)
  @buf.byteslice(addr, 4).unpack1("V")
end

#load_u64(addr) ⇒ Object



72
73
74
75
# File 'lib/relaton/un/wasm/memory.rb', line 72

def load_u64(addr)
  bounds!(addr, 8)
  @buf.byteslice(addr, 8).unpack1("Q<")
end

#load_u8(addr) ⇒ Object



42
43
44
45
# File 'lib/relaton/un/wasm/memory.rb', line 42

def load_u8(addr)
  bounds!(addr, 1)
  @buf.getbyte(addr)
end

#read(addr, len) ⇒ Object



31
32
33
34
# File 'lib/relaton/un/wasm/memory.rb', line 31

def read(addr, len)
  bounds!(addr, len)
  @buf.byteslice(addr, len)
end

#size_pagesObject



17
18
19
# File 'lib/relaton/un/wasm/memory.rb', line 17

def size_pages
  @pages
end

#store_u16(addr, val) ⇒ Object



87
88
89
90
# File 'lib/relaton/un/wasm/memory.rb', line 87

def store_u16(addr, val)
  bounds!(addr, 2)
  @buf.bytesplice(addr, 2, [val & 0xffff].pack("v"))
end

#store_u32(addr, val) ⇒ Object



92
93
94
95
# File 'lib/relaton/un/wasm/memory.rb', line 92

def store_u32(addr, val)
  bounds!(addr, 4)
  @buf.bytesplice(addr, 4, [val & 0xffff_ffff].pack("V"))
end

#store_u64(addr, val) ⇒ Object



97
98
99
100
# File 'lib/relaton/un/wasm/memory.rb', line 97

def store_u64(addr, val)
  bounds!(addr, 8)
  @buf.bytesplice(addr, 8, [val & 0xffff_ffff_ffff_ffff].pack("Q<"))
end

#store_u8(addr, val) ⇒ Object



82
83
84
85
# File 'lib/relaton/un/wasm/memory.rb', line 82

def store_u8(addr, val)
  bounds!(addr, 1)
  @buf.setbyte(addr, val & 0xff)
end

#write(addr, bytes) ⇒ Object



36
37
38
39
40
# File 'lib/relaton/un/wasm/memory.rb', line 36

def write(addr, bytes)
  bytes = bytes.b
  bounds!(addr, bytes.bytesize)
  @buf.bytesplice(addr, bytes.bytesize, bytes)
end