Class: Buffer::Array

Inherits:
Object
  • Object
show all
Includes:
Enumerable, Native
Defined in:
lib/opal/typed-array/array.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(buffer, bits, type) ⇒ Array

Returns a new instance of Array.



23
24
25
26
27
28
29
30
31
# File 'lib/opal/typed-array/array.rb', line 23

def initialize (buffer, bits, type)
	klass = Array.for(bits, type)

	super(`new klass(#{buffer.to_native})`)

	@buffer = buffer
	@bits   = `#@native.BYTES_PER_ELEMENT * 8`
	@type   = type
end

Instance Attribute Details

#bitsObject (readonly)

Returns the value of attribute bits.



21
22
23
# File 'lib/opal/typed-array/array.rb', line 21

def bits
  @bits
end

#bufferObject (readonly)

Returns the value of attribute buffer.



21
22
23
# File 'lib/opal/typed-array/array.rb', line 21

def buffer
  @buffer
end

#typeObject (readonly)

Returns the value of attribute type.



21
22
23
# File 'lib/opal/typed-array/array.rb', line 21

def type
  @type
end

Class Method Details

.for(bits, type) ⇒ Object



14
15
16
# File 'lib/opal/typed-array/array.rb', line 14

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

Instance Method Details

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



33
34
35
# File 'lib/opal/typed-array/array.rb', line 33

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

#[]=(index, value) ⇒ Object



37
38
39
# File 'lib/opal/typed-array/array.rb', line 37

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

#bytesizeObject



41
42
43
# File 'lib/opal/typed-array/array.rb', line 41

def bytesize
	`#@native.byteLength`
end

#eachObject



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/opal/typed-array/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 'lib/opal/typed-array/array.rb', line 57

def length
	`#@native.length`
end

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



61
62
63
# File 'lib/opal/typed-array/array.rb', line 61

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