Sha256: 29f3451b9064c5a307e2e5c5e028a5b3929b1f385d2d142be22f3836ddb42ade

Contents?: true

Size: 789 Bytes

Versions: 16

Compression:

Stored size: 789 Bytes

Contents

require 'native'
require 'buffer/array'
require 'buffer/view'

class Buffer
  include Native::Wrapper

  def self.supported?
    !$$[:ArrayBuffer].nil?
  end

  def self.name_for(bits, type)
    part = case type
           when :unsigned then 'Uint'
           when :signed   then 'Int'
           when :float    then 'Float'
           end
    "#{part}#{bits}"
  end

  def initialize(size, bits = 8)
    if native?(size)
      super(size)
    else
      super(`new ArrayBuffer(size * (bits / 8))`)
    end
  end

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

  def to_a(bits = 8, type = :unsigned)
    Array.new(self, bits, type)
  end

  def view(offset = nil, length = nil)
    View.new(self, offset, length)
  end

  def to_s
    to_a.to_a.pack('c*')
  end

  alias size length
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
opal-1.7.4 stdlib/buffer.rb
opal-1.7.3 stdlib/buffer.rb
opal-1.7.2 stdlib/buffer.rb
opal-1.7.1 stdlib/buffer.rb
opal-1.7.0 stdlib/buffer.rb
opal-1.7.0.rc1 stdlib/buffer.rb
opal-1.6.1 stdlib/buffer.rb
opal-1.6.0 stdlib/buffer.rb
opal-1.6.0.rc1 stdlib/buffer.rb
opal-1.6.0.alpha1 stdlib/buffer.rb
opal-1.5.1 stdlib/buffer.rb
opal-1.5.0 stdlib/buffer.rb
opal-1.5.0.rc1 stdlib/buffer.rb
opal-1.4.1 stdlib/buffer.rb
opal-1.4.0 stdlib/buffer.rb
opal-1.4.0.alpha1 stdlib/buffer.rb