Giter Club home page Giter Club logo

Comments (1)

kojix2 avatar kojix2 commented on June 4, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.