Class: Buffer

Inherits:
Native show all
Includes:
Native
Defined in:
opal/stdlib/buffer.rb,
opal/stdlib/buffer/view.rb,
opal/stdlib/buffer/array.rb

Defined Under Namespace

Classes: Array, View

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Native

call, convert, included, is_a?, #to_n, try_convert

Constructor Details

#initialize(size, bits = 8) ⇒ Buffer

Returns a new instance of Buffer



20
21
22
23
24
25
26
# File 'opal/stdlib/buffer.rb', line 20

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

Class Method Details

.name_for(bits, type) ⇒ Object



12
13
14
15
16
17
18
# File 'opal/stdlib/buffer.rb', line 12

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

.supported?Boolean

Returns:



8
9
10
# File 'opal/stdlib/buffer.rb', line 8

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

Instance Method Details

#lengthObject Also known as: size



28
29
30
# File 'opal/stdlib/buffer.rb', line 28

def length
  `#@native.byteLength`
end

#to_a(bits = 8, type = :unsigned) ⇒ Object



34
35
36
# File 'opal/stdlib/buffer.rb', line 34

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

#view(offset = nil, length = nil) ⇒ Object



38
39
40
# File 'opal/stdlib/buffer.rb', line 38

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