Class: Buffer::Array

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Enumerable

#to_json, #to_set

Methods included from Native

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

Constructor Details

#initialize(buffer, bits = nil, type = nil) ⇒ Array

Returns a new instance of Array


14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'opal/stdlib/buffer/array.rb', line 14

def initialize(buffer, bits = nil, type = nil)
  if Native == buffer
    super(buffer)
  else
    %x{
      var klass = #{Array.for(bits, type)};

      #{super(`new klass(#{buffer.to_n})`)}
    }
  end

  @buffer = buffer
  @type   = type
end

Instance Attribute Details

#bufferObject (readonly)

Returns the value of attribute buffer


12
13
14
# File 'opal/stdlib/buffer/array.rb', line 12

def buffer
  @buffer
end

#typeObject (readonly)

Returns the value of attribute type


12
13
14
# File 'opal/stdlib/buffer/array.rb', line 12

def type
  @type
end

Class Method Details

.for(bits, type) ⇒ Object


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

def self.for(bits, type)
  $$["#{Buffer.name_for bits, type}Array"]
end

Instance Method Details

#[](index, offset = nil) ⇒ Object


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

def [](index, offset = nil)
  offset ? `#@native.subarray(index, offset)` : `#@native[index]`
end

#[]=(index, value) ⇒ Object


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

def []=(index, value)
  `#@native[index] = value`
end

#bitsObject


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

def bits
  `#@native.BYTES_PER_ELEMENT * 8`
end

#bytesizeObject


41
42
43
# File 'opal/stdlib/buffer/array.rb', line 41

def bytesize
  `#@native.byteLength`
end

#eachObject


45
46
47
48
49
50
51
52
53
54
55
# File 'opal/stdlib/buffer/array.rb', line 45

def each
  return enum_for :each unless block_given?

  %x{
    for (var i = 0, length = #@native.length; i < length; i++) {
      #{yield `#@native[i]`}
    }
  }

  self
end

#lengthObject Also known as: size


57
58
59
# File 'opal/stdlib/buffer/array.rb', line 57

def length
  `#@native.length`
end

#merge!(other, offset = undefined) ⇒ Object


61
62
63
# File 'opal/stdlib/buffer/array.rb', line 61

def merge!(other, offset = undefined)
  `#@native.set(#{other.to_n}, offset)`
end