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



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

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



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

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:



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

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

Instance Method Details

#lengthObject Also known as: size



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

def length
  `#@native.byteLength`
end

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



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

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

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



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

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