forked from opal/opal
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuffer.rb
More file actions
42 lines (34 loc) · 746 Bytes
/
buffer.rb
File metadata and controls
42 lines (34 loc) · 746 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
require 'native'
require 'buffer/array'
require 'buffer/view'
class Buffer
include Native::Wrapper
def self.supported?
!$$[:ArrayBuffer].nil?
end
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
def initialize(size, bits = 8)
if native?(size)
super(size)
else
super(`new ArrayBuffer(size * (bits / 8))`)
end
end
def length
`#{@native}.byteLength`
end
alias size length
def to_a(bits = 8, type = :unsigned)
Array.new(self, bits, type)
end
def view(offset = nil, length = nil)
View.new(self, offset, length)
end
end