Sha256: 4bc6bade39d9f4fb2341270a8eefcff5ba9eebdfa4123857f32e133dbf18d056

Contents?: true

Size: 1.04 KB

Versions: 49

Compression:

Stored size: 1.04 KB

Contents

class Buffer < Native

class Array < Native
  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

49 entries across 49 versions & 3 rubygems

Version Path
opal-0.10.6 stdlib/buffer/array.rb
opal-0.10.6.beta stdlib/buffer/array.rb
opal-0.10.5 stdlib/buffer/array.rb
opal-0.10.4 stdlib/buffer/array.rb
opal-0.11.0.rc1 stdlib/buffer/array.rb
opal-0.10.3 stdlib/buffer/array.rb
opal-0.10.2 stdlib/buffer/array.rb
opal-0.10.1 stdlib/buffer/array.rb
opal-0.10.0 stdlib/buffer/array.rb
opal-0.10.0.rc2 stdlib/buffer/array.rb
opal-0.9.4 stdlib/buffer/array.rb
opal-0.9.3 stdlib/buffer/array.rb
opal-0.10.0.rc1 stdlib/buffer/array.rb
opal-0.10.0.beta5 stdlib/buffer/array.rb
opal-0.10.0.beta4 stdlib/buffer/array.rb
opal-0.10.0.beta3 stdlib/buffer/array.rb
opal-0.10.0.beta2 stdlib/buffer/array.rb
opal-0.10.0.beta1 stdlib/buffer/array.rb
opal-0.9.2 stdlib/buffer/array.rb
opal-0.9.0 stdlib/buffer/array.rb