mirror of
https://github.com/MihailRis/voxelcore.git
synced 2026-10-11 22:11:51 +00:00
Merge pull request #779 from MihailRis/fix-bytearray-view-classes
fix bytearray view classes
This commit is contained in:
commit
1ae2ec62d8
1 changed files with 15 additions and 14 deletions
|
|
@ -231,44 +231,45 @@ local function FFIBytearray_as_string(bytes)
|
||||||
end
|
end
|
||||||
|
|
||||||
local function create_FFIview_class(name, typename, typesize)
|
local function create_FFIview_class(name, typename, typesize)
|
||||||
local FFIU16view_mt = {
|
local ptrtype = typename .. "*"
|
||||||
|
local FFIview_mt = {
|
||||||
__index = function(self, key)
|
__index = function(self, key)
|
||||||
if key <= 0 or key > self.size then
|
if key == 'size' then
|
||||||
|
return self.bytes.size / typesize
|
||||||
|
end
|
||||||
|
if key <= 0 or key > self.bytes.size / typesize then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
return self.ptr[key - 1]
|
local ptr = FFI.cast(ptrtype, self.bytes.bytes)
|
||||||
|
return ptr[key - 1]
|
||||||
end,
|
end,
|
||||||
__newindex = function(self, key, value)
|
__newindex = function(self, key, value)
|
||||||
if key == self.size + 1 then
|
if key <= 0 or key > self.bytes.size / typesize then
|
||||||
return append(self, value)
|
|
||||||
elseif key <= 0 or key > self.size then
|
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
self.ptr[key - 1] = value
|
local ptr = FFI.cast(ptrtype, self.bytes.bytes)
|
||||||
|
ptr[key - 1] = value
|
||||||
end,
|
end,
|
||||||
__len = function(self)
|
__len = function(self)
|
||||||
return self.size
|
return self.bytes.size / typesize
|
||||||
end,
|
end,
|
||||||
__tostring = function(self)
|
__tostring = function(self)
|
||||||
return string.format(name .. "[%s]{...}", tonumber(self.size))
|
return string.format(name .. "[%s]{...}", tonumber(self.bytes.size / typesize))
|
||||||
end,
|
end,
|
||||||
__ipairs = function(self)
|
__ipairs = function(self)
|
||||||
local i = 0
|
local i = 0
|
||||||
return function()
|
return function()
|
||||||
i = i + 1
|
i = i + 1
|
||||||
if i <= self.size then
|
if i <= self.bytes.size / typesize then
|
||||||
return i, self.ptr[i - 1]
|
return i, self.ptr[i - 1]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
}
|
}
|
||||||
return function (bytes)
|
return function (bytes)
|
||||||
local ptr = FFI.cast(typename .. "*", bytes.bytes)
|
|
||||||
local x = setmetatable({
|
local x = setmetatable({
|
||||||
bytes=bytes,
|
bytes=bytes,
|
||||||
ptr=ptr,
|
}, FFIview_mt)
|
||||||
size=math.floor(bytes.size / typesize),
|
|
||||||
}, FFIU16view_mt)
|
|
||||||
return x
|
return x
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue