Class: Dewasm::Pozeiden::WasmModule::Rt::Memory

Inherits:
Object
  • Object
show all
Defined in:
lib/dewasm/pozeiden/wasm_module.rb

Constant Summary collapse

PAGE_SIZE =
65536

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(min_pages, max_pages) ⇒ Memory

Returns a new instance of Memory.



252
253
254
255
256
257
258
259
260
261
262
# File 'lib/dewasm/pozeiden/wasm_module.rb', line 252

def initialize(min_pages, max_pages)
	@size = min_pages * PAGE_SIZE
	saved = Warning[:experimental]
	begin
		Warning[:experimental] = false
		@buffer = IO::Buffer.new(@size)
	ensure
		Warning[:experimental] = saved
	end
	@max_pages = max_pages && max_pages < 65536 ? max_pages : 65536
end

Instance Attribute Details

#bufferObject (readonly)

Returns the value of attribute buffer.



248
249
250
# File 'lib/dewasm/pozeiden/wasm_module.rb', line 248

def buffer
  @buffer
end

Instance Method Details

#check(addr, len) ⇒ Object



264
265
266
# File 'lib/dewasm/pozeiden/wasm_module.rb', line 264

def check(addr, len)
	Rt.trap("out of bounds memory access") if addr + len > @size
end

#copy(dst, src, len) ⇒ Object



268
269
270
271
272
273
# File 'lib/dewasm/pozeiden/wasm_module.rb', line 268

def copy(dst, src, len)
	check(dst, len)
	check(src, len)
	return if len == 0
	@buffer.copy(@buffer, dst, len, src)
end

#f32_load(a) ⇒ Object

The bit path preserves a NaN's sign and payload.



276
# File 'lib/dewasm/pozeiden/wasm_module.rb', line 276

def f32_load(a) = Rt.f32_from_bits(i32_load(a))

#f32_store(a, v) ⇒ Object



278
# File 'lib/dewasm/pozeiden/wasm_module.rb', line 278

def f32_store(a, v) = i32_store(a, Rt.f32_bits(v))

#f64_load(a) ⇒ Object



280
# File 'lib/dewasm/pozeiden/wasm_module.rb', line 280

def f64_load(a) = (Rt.trap("out of bounds memory access") if a + 8 > @size; @buffer.get_value(:f64, a))

#f64_store(a, v) ⇒ Object



282
# File 'lib/dewasm/pozeiden/wasm_module.rb', line 282

def f64_store(a, v) = (Rt.trap("out of bounds memory access") if a + 8 > @size; @buffer.set_value(:f64, a, v))

#fill(dst, val, len) ⇒ Object



284
285
286
287
288
# File 'lib/dewasm/pozeiden/wasm_module.rb', line 284

def fill(dst, val, len)
	check(dst, len)
	return if len == 0
	@buffer.clear(val & 0xff, dst, len)
end

#grow(delta) ⇒ Object



290
291
292
293
294
295
296
# File 'lib/dewasm/pozeiden/wasm_module.rb', line 290

def grow(delta)
	old = size
	return M32 if old + delta > @max_pages
	@size += delta * PAGE_SIZE
	@buffer.resize(@size) # zero-fills the new tail
	old
end

#i32_load(a) ⇒ Object



298
# File 'lib/dewasm/pozeiden/wasm_module.rb', line 298

def i32_load(a) = (Rt.trap("out of bounds memory access") if a + 4 > @size; @buffer.get_value(:u32, a))

#i32_load16_u(a) ⇒ Object



300
# File 'lib/dewasm/pozeiden/wasm_module.rb', line 300

def i32_load16_u(a) = (Rt.trap("out of bounds memory access") if a + 2 > @size; @buffer.get_value(:u16, a))

#i32_load8_s(a) ⇒ Object



302
# File 'lib/dewasm/pozeiden/wasm_module.rb', line 302

def i32_load8_s(a) = (Rt.trap("out of bounds memory access") if a + 1 > @size; @buffer.get_value(:S8, a) & M32)

#i32_load8_u(a) ⇒ Object



304
# File 'lib/dewasm/pozeiden/wasm_module.rb', line 304

def i32_load8_u(a) = (Rt.trap("out of bounds memory access") if a + 1 > @size; @buffer.get_value(:U8, a))

#i32_store(a, v) ⇒ Object



306
# File 'lib/dewasm/pozeiden/wasm_module.rb', line 306

def i32_store(a, v) = (Rt.trap("out of bounds memory access") if a + 4 > @size; @buffer.set_value(:u32, a, v))

#i32_store16(a, v) ⇒ Object



308
# File 'lib/dewasm/pozeiden/wasm_module.rb', line 308

def i32_store16(a, v) = (Rt.trap("out of bounds memory access") if a + 2 > @size; @buffer.set_value(:u16, a, v & 0xffff))

#i32_store8(a, v) ⇒ Object



310
# File 'lib/dewasm/pozeiden/wasm_module.rb', line 310

def i32_store8(a, v) = (Rt.trap("out of bounds memory access") if a + 1 > @size; @buffer.set_value(:U8, a, v & 0xff))

#i64_load(a) ⇒ Object



312
# File 'lib/dewasm/pozeiden/wasm_module.rb', line 312

def i64_load(a) = (Rt.trap("out of bounds memory access") if a + 8 > @size; @buffer.get_value(:u64, a))

#i64_load16_u(a) ⇒ Object



314
# File 'lib/dewasm/pozeiden/wasm_module.rb', line 314

def i64_load16_u(a) = (Rt.trap("out of bounds memory access") if a + 2 > @size; @buffer.get_value(:u16, a))

#i64_load32_u(a) ⇒ Object



316
# File 'lib/dewasm/pozeiden/wasm_module.rb', line 316

def i64_load32_u(a) = (Rt.trap("out of bounds memory access") if a + 4 > @size; @buffer.get_value(:u32, a))

#i64_load8_u(a) ⇒ Object



318
# File 'lib/dewasm/pozeiden/wasm_module.rb', line 318

def i64_load8_u(a) = (Rt.trap("out of bounds memory access") if a + 1 > @size; @buffer.get_value(:U8, a))

#i64_store(a, v) ⇒ Object



320
# File 'lib/dewasm/pozeiden/wasm_module.rb', line 320

def i64_store(a, v) = (Rt.trap("out of bounds memory access") if a + 8 > @size; @buffer.set_value(:u64, a, v))

#i64_store16(a, v) ⇒ Object



322
# File 'lib/dewasm/pozeiden/wasm_module.rb', line 322

def i64_store16(a, v) = (Rt.trap("out of bounds memory access") if a + 2 > @size; @buffer.set_value(:u16, a, v & 0xffff))

#i64_store32(a, v) ⇒ Object



324
# File 'lib/dewasm/pozeiden/wasm_module.rb', line 324

def i64_store32(a, v) = (Rt.trap("out of bounds memory access") if a + 4 > @size; @buffer.set_value(:u32, a, v & M32))

#i64_store8(a, v) ⇒ Object



326
# File 'lib/dewasm/pozeiden/wasm_module.rb', line 326

def i64_store8(a, v) = (Rt.trap("out of bounds memory access") if a + 1 > @size; @buffer.set_value(:U8, a, v & 0xff))

#init(dst, data, src, len) ⇒ Object

Also used to initialize active data segments at instantiation time.



329
330
331
332
333
334
# File 'lib/dewasm/pozeiden/wasm_module.rb', line 329

def init(dst, data, src, len)
	Rt.trap("out of bounds memory access") if src + len > data.bytesize
	check(dst, len)
	return if len == 0
	@buffer.set_string(data, dst, len, src)
end

#read_string(ptr, len) ⇒ Object



336
337
338
339
# File 'lib/dewasm/pozeiden/wasm_module.rb', line 336

def read_string(ptr, len)
	check(ptr, len)
	@buffer.get_string(ptr, len)
end

#sizeObject



341
342
343
# File 'lib/dewasm/pozeiden/wasm_module.rb', line 341

def size
	@size / PAGE_SIZE
end

#wasm_kindObject



250
# File 'lib/dewasm/pozeiden/wasm_module.rb', line 250

def wasm_kind = :memory