mirror of
https://github.com/gvx/bitser.git
synced 2025-01-16 03:24:19 +00:00
Remove cdata support
See https://github.com/gvx/bitser/issues/28#issuecomment-2564723498
This commit is contained in:
parent
f423938ec0
commit
c95cb0d77a
27
bitser.lua
27
bitser.lua
@ -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
|
||||
@ -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
|
||||
|
@ -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)
|
||||
@ -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)
|
||||
@ -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 = {}
|
||||
@ -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
|
||||
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user