Comments (1)
The method mentioned in the "Alternative" section using a binary string with pack
and unpack
is the method used in wasmtime-rb.
These are Memory#read(offset, size)
and Memory#write(offset, value)
. I think this is a very easy-to-use and highly versatile method. Therefore, it might be better to adopt this method rather than adding a new View.
I am a little unsure if the code below is completely correct, but it should work like this.
Write:
memory.write(
addr,
case type
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: #{type}"
end
)
Read:
case type
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: #{type}"
end
Of course, the same thing can be done with the current version of wasmer-ruby using uint8_view
.
Write:
uint8_view = memory.uint8_view(addr)
arg_uint8 = case type
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: #{type}"
end.unpack('C*')
arg_uint8.each_with_index do |a, i|
uint8_view[i] = a
end
Read:
uint8_view = memory.uint8_view(addr)
case type
when 'int8' then Array.new(len * 1) { |i| uint8_view[i] }.pack('C*').unpack('c*')
when 'uint8' then Array.new(len * 1) { |i| uint8_view[i] }.pack('C*').unpack('C*')
when 'int16' then Array.new(len * 2) { |i| uint8_view[i] }.pack('C*').unpack('s*')
when 'uint16' then Array.new(len * 2) { |i| uint8_view[i] }.pack('C*').unpack('S*')
when 'int32' then Array.new(len * 4) { |i| uint8_view[i] }.pack('C*').unpack('l*')
when 'uint32' then Array.new(len * 4) { |i| uint8_view[i] }.pack('C*').unpack('L*')
when 'int64' then Array.new(len * 8) { |i| uint8_view[i] }.pack('C*').unpack('q*')
when 'uint64' then Array.new(len * 8) { |i| uint8_view[i] }.pack('C*').unpack('Q*')
when 'float32' then Array.new(len * 4) { |i| uint8_view[i] }.pack('C*').unpack('e*')
when 'float64' then Array.new(len * 8) { |i| uint8_view[i] }.pack('C*').unpack('E*')
else raise "unsupported type: #{type}"
end
from wasmer-ruby.
Related Issues (20)
- Export Wasm globals ? HOT 4
- WASI / import function support ? HOT 4
- Failed to install this gem HOT 7
- gem install fails with Rust 1.46.0 HOT 3
- Too much rust too little Ruby HOT 1
- Install fails on Ruby 3.0.1 HOT 1
- More parity with other languages HOT 5
- Seg fault with statically linked version of Ruby HOT 4
- Migrate `rutie-derive` and `rutie-derive-macros` to `rutie HOT 1
- Cannot install on macOS with statically distributed Ruby version HOT 12
- Release version 1.0.1 HOT 1
- [More Examples] Could more examples be added? HOT 1
- Integer conversion raising en exception
- Challenges in Running JS Interpreter via wasmer-ruby HOT 4
- Update to latest wasmer 2.3 (or 3.0 when released)
- Redirecting STDOUT and STDERR HOT 9
- Support WCGI?
- `undefining the allocator of T_DATA class ...` warning on Ruby 3.2
- gem install fails with Rust 1.75.0 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from wasmer-ruby.