Skip to content

Commit

Permalink
Remove cdata support
Browse files Browse the repository at this point in the history
  • Loading branch information
gvx committed Dec 29, 2024
1 parent f423938 commit c95cb0d
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 65 deletions.
27 changes: 1 addition & 26 deletions bitser.lua
Original file line number Diff line number Diff line change
Expand Up @@ -230,23 +230,7 @@ local function write_table(value, seen)
end
end

local function write_cdata(value, seen)
local ty = ffi.typeof(value)
if ty == value then
-- ctype
Buffer_write_byte(251)
serialize_value(tostring(ty):sub(7, -2), seen)
return
end
-- cdata
Buffer_write_byte(252)
serialize_value(ty, seen)
local len = ffi.sizeof(value)
write_number(len)
Buffer_write_raw(ffi.typeof('$[1]', ty)(value), len)
end

local types = {number = write_number, string = write_string, table = write_table, boolean = write_boolean, ["nil"] = write_nil, cdata = write_cdata}
local types = {number = write_number, string = write_string, table = write_table, boolean = write_boolean, ["nil"] = write_nil}

serialize_value = function(value, seen)
if seen[value] then
Expand Down Expand Up @@ -393,15 +377,6 @@ local function deserialize_value(seen)
elseif t == 250 then
--short int
return Buffer_read_data("int16_t[1]", 2)[0]
elseif t == 251 then
--ctype
return ffi.typeof(deserialize_value(seen))
elseif t == 252 then
local ctype = deserialize_value(seen)
local len = deserialize_value(seen)
local read_into = ffi.typeof('$[1]', ctype)()
Buffer_read_raw(read_into, len)
return ctype(read_into[0])
else
error("unsupported serialized type " .. t)
end
Expand Down
42 changes: 3 additions & 39 deletions spec/bitser_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,6 @@ local function test_serdeser(value)
assert.are.same(serdeser(value), value)
end

local function test_serdeser_idempotent(value)
assert.are.same(bitser.dumps(serdeser(value)), bitser.dumps(value))
end

describe("bitser", function()
it("serializes simple values", function()
test_serdeser(true)
Expand Down Expand Up @@ -81,7 +77,7 @@ describe("bitser", function()
bitser.unregister("temp_resource")
end)
it("serializes many resources", function()
local max = 1000
local max = 100
local t = {}
for i = 1, max do
bitser.register(tostring(i), i)
Expand All @@ -93,7 +89,7 @@ describe("bitser", function()
end
end)
it("serializes deeply nested tables", function()
local max = 1000
local max = 100
local t = {}
for _ = 1, max do
t.t = {}
Expand Down Expand Up @@ -239,7 +235,7 @@ describe("bitser", function()
test_serdeser(t)
end)
it("serializes many references", function()
local max = 1000
local max = 100
local t = {}
local t2 = {}
for i = 1, max do
Expand Down Expand Up @@ -302,38 +298,6 @@ describe("bitser", function()
bitser.dumpLoveFile("some_file_name", v)
assert.are.same(v, bitser.loadLoveFile("some_file_name"))
end)
it("can read and write simple cdata", function()
test_serdeser(ffi.new('double', 42.5))
end)
it("can read and write cdata with a registered ctype", function()
pcall(ffi.cdef,[[
struct some_struct {
int a;
double b;
};
]])
local value = ffi.new('struct some_struct', 42, 1.25)
bitser.register('struct_type', ffi.typeof(value))
test_serdeser_idempotent(value)
bitser.unregister('struct_type')
end)
it("can read and write cdata without registering its ctype", function()
pcall(ffi.cdef,[[
struct some_struct {
int a;
double b;
};
]])
local value = ffi.new('struct some_struct', 42, 1.25)
test_serdeser_idempotent(value)
end)
it("cannot read from anonymous structs", function()
local v = bitser.dumps(ffi.new('struct { int a; }'))
assert.has_error(function() bitser.loads(v) end)
end)
it("can read and write simple multiple cdata of the same ctype without getting confused", function()
test_serdeser({ffi.new('double', 42.5), ffi.new('double', 12), ffi.new('double', 0.01)})
end)
it("can read and write metatables by default", function()
local t = setmetatable({foo="foo"}, {__index = {bar="bar"}})
test_serdeser(t)
Expand Down

0 comments on commit c95cb0d

Please sign in to comment.