Skip to content

Commit e5dc3df

Browse files
committed
Expanding LuaSlice.
1 parent 5d24510 commit e5dc3df

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

src/lua/fileffi-cdef.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,11 @@ LuaFile* dupFile(LuaFile*);
8989

9090
LuaFile* zReader(LuaFile*, int64_t size, bool raw);
9191

92+
LuaSlice* createEmptySlice();
9293
uint64_t getSliceSize(LuaSlice*);
9394
const void* getSliceData(LuaSlice*);
95+
void* getSliceMutableData(LuaSlice*);
96+
void resizeSlice(LuaSlice* slice, uint32_t size);
9497
void destroySlice(LuaSlice*);
9598

9699
LuaFile* mem4g();

src/lua/fileffimeta.lua

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,28 @@ local sliceMeta = {
2727
return buffer[index]
2828
elseif index == 'data' then
2929
return C.getSliceData(slice._wrapper)
30+
elseif index == 'mutable' then
31+
return C.getSliceMutableData(slice._wrapper)
3032
elseif index == 'size' then
3133
return tonumber(C.getSliceSize(slice._wrapper))
34+
elseif index == 'resize' then
35+
return function(slice, size)
36+
C.resizeSlice(slice._wrapper, size)
37+
end
3238
end
3339
error('Unknown index `' .. index .. '` for LuaSlice')
3440
end,
35-
__newindex = function(slice, index, value) end,
41+
__newindex = function(slice, index, value)
42+
if type(index) == 'number' and index >= 0 and index < C.getSliceSize(slice._wrapper) then
43+
local data = C.getSliceMutableData(slice._wrapper)
44+
local buffer = ffi.cast('uint8_t*', data)
45+
buffer[index] = value
46+
elseif index == 'size' then
47+
C.resizeSlice(slice._wrapper, value)
48+
else
49+
error('Unknown or immutable index `' .. index .. '` for LuaSlice')
50+
end
51+
end,
3652
}
3753

3854
local function createSliceWrapper(wrapper)
@@ -71,5 +87,6 @@ local LuaBuffer = ffi.metatype('LuaBuffer', bufferMeta)
7187

7288
Support.File._LuaBuffer = LuaBuffer
7389
Support.File._createSliceWrapper = createSliceWrapper
90+
Support.File.createEmptySlice = function() return createSliceWrapper(C.createEmptySlice()) end
7491

7592
-- )EOF"

src/lua/luafile.cc

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,10 +167,11 @@ LuaFile* zReader(LuaFile* wrapper, int64_t size, bool raw) {
167167
: new PCSX::ZReader(wrapper->file, size));
168168
}
169169

170+
PCSX::Slice* createEmptySlice() { return new PCSX::Slice(); }
170171
uint64_t getSliceSize(PCSX::Slice* slice) { return slice->size(); }
171-
172172
const void* getSliceData(PCSX::Slice* slice) { return slice->data(); }
173-
173+
void* getSliceMutableData(PCSX::Slice* slice) { return slice->mutableData(); }
174+
void resizeSlice(PCSX::Slice* slice, uint32_t size) { slice->resize(size); }
174175
void destroySlice(PCSX::Slice* slice) { delete slice; }
175176

176177
int readFileUserData(PCSX::Lua L) {
@@ -343,8 +344,11 @@ static void registerAllSymbols(PCSX::Lua L) {
343344

344345
REGISTER(L, zReader);
345346

347+
REGISTER(L, createEmptySlice);
346348
REGISTER(L, getSliceSize);
347349
REGISTER(L, getSliceData);
350+
REGISTER(L, getSliceMutableData);
351+
REGISTER(L, resizeSlice);
348352
REGISTER(L, destroySlice);
349353

350354
REGISTER(L, mem4g);

0 commit comments

Comments
 (0)