Sha256: 49fb3d07d23b210a9b1f677df3acfcacc619f0d034ea425abf54a6e504c7a039

Contents?: true

Size: 1.19 KB

Versions: 6

Compression:

Stored size: 1.19 KB

Contents

# backtick_javascript: true

class Buffer
  class Array
    include Native::Wrapper

    def self.for(bits, type)
      $$["#{Buffer.name_for bits, type}Array"]
    end

    include Enumerable

    attr_reader :buffer, :type

    def initialize(buffer, bits = nil, type = nil)
      if Native == buffer
        super(buffer)
      else
        %x{
          var klass = #{Array.for(bits, type)};

          #{super(`new klass(#{buffer.to_n})`)}
        }
      end

      @buffer = buffer
      @type   = type
    end

    def bits
      `#{@native}.BYTES_PER_ELEMENT * 8`
    end

    def [](index, offset = nil)
      offset ? `#{@native}.subarray(index, offset)` : `#{@native}[index]`
    end

    def []=(index, value)
      `#{@native}[index] = value`
    end

    def bytesize
      `#{@native}.byteLength`
    end

    def each
      return enum_for :each unless block_given?

      %x{
        for (var i = 0, length = #{@native}.length; i < length; i++) {
          #{yield `#{@native}[i]`}
        }
      }

      self
    end

    def length
      `#{@native}.length`
    end

    def merge!(other, offset = undefined)
      `#{@native}.set(#{other.to_n}, offset)`
    end

    alias size length
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
opal-1.8.3.rc1 stdlib/buffer/array.rb
opal-1.8.2 stdlib/buffer/array.rb
opal-1.8.1 stdlib/buffer/array.rb
opal-1.8.0 stdlib/buffer/array.rb
opal-1.8.0.beta1 stdlib/buffer/array.rb
opal-1.8.0.alpha1 stdlib/buffer/array.rb