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.9.0.rc1 stdlib/buffer/array.rb
opal-0.9.0.beta2 stdlib/buffer/array.rb
opal-0.9.0.beta1 stdlib/buffer/array.rb
opal-0.8.1 stdlib/buffer/array.rb
opal-0.8.1.rc1 stdlib/buffer/array.rb
opal-wedge-0.9.0.dev stdlib/buffer/array.rb
opal-0.8.0 stdlib/buffer/array.rb
opal-0.8.0.rc3 stdlib/buffer/array.rb
opal-0.8.0.rc2 stdlib/buffer/array.rb
opal-0.8.0.rc1 stdlib/buffer/array.rb
opal-0.8.0.beta1 stdlib/buffer/array.rb
opal-0.7.2 stdlib/buffer/array.rb
opal-0.7.1 stdlib/buffer/array.rb
opal-0.7.0 stdlib/buffer/array.rb
opal-0.7.0.rc1 stdlib/buffer/array.rb
opal-0.7.0.beta3 stdlib/buffer/array.rb
opal-0.6.3 stdlib/buffer/array.rb
opal-0.7.0.beta2 stdlib/buffer/array.rb
opal-cj-0.7.0.beta2 stdlib/buffer/array.rb
opal-cj-0.7.0.beta1 stdlib/buffer/array.rb