Class: Buffer

Inherits:
Object show all
Includes:
Native::Wrapper
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::Wrapper

included, #to_n

Constructor Details

#initialize(size, bits = 8) ⇒ Buffer

Returns a new instance of Buffer.



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

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
19
# File 'opal/stdlib/buffer.rb', line 12

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:



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

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

Instance Method Details

#lengthObject Also known as: size



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

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