Class: Buffer

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

Overview

backtick_javascript: true

Defined Under Namespace

Classes: Array, View

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Native::Wrapper

included, #to_n

Constructor Details

#initialize(size, bits = 8) ⇒ Buffer

Returns a new instance of Buffer.



23
24
25
26
27
28
29
# File 'opal/stdlib/buffer.rb', line 23

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



14
15
16
17
18
19
20
21
# File 'opal/stdlib/buffer.rb', line 14

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

.supported?Boolean

Returns:



10
11
12
# File 'opal/stdlib/buffer.rb', line 10

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

Instance Method Details

#lengthObject Also known as: size



31
32
33
# File 'opal/stdlib/buffer.rb', line 31

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

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



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

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

#to_sObject



43
44
45
# File 'opal/stdlib/buffer.rb', line 43

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

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



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

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