diff --git a/README.md b/README.md index 8cb30fe..f25c265 100644 --- a/README.md +++ b/README.md @@ -47,5 +47,5 @@ send me a pull request! ## You can register classes? -Yes. At the moment, bitser supports MiddleClass, SECL, hump.class and Slither (and +Yes. At the moment, bitser supports MiddleClass, SECL, hump.class, Slither and Moonscript classes (and probably some other class libraries by accident). diff --git a/bitser.lua b/bitser.lua index e91d341..4e05842 100644 --- a/bitser.lua +++ b/bitser.lua @@ -176,7 +176,8 @@ local function write_table(value, seen) local classname = (class_name_registry[value.class] -- MiddleClass or class_name_registry[value.__baseclass] -- SECL or class_name_registry[getmetatable(value)] -- hump.class - or class_name_registry[value.__class__]) -- Slither + or class_name_registry[value.__class__] -- Slither + or class_name_registry[value.__class]) -- Moonscript class if classname then classkey = classkey_registry[classname] Buffer_write_byte(242) @@ -354,6 +355,10 @@ local function deserialize_Slither(instance, class) return getmetatable(class).allocate(instance) end +local function deserialize_Moonscript(instance, class) + return setmetatable(instance, class.__base) +end + return {dumps = function(value) serialize(value) return ffi.string(buf, buf_pos) @@ -381,7 +386,7 @@ end, unregister = function(name) end, registerClass = function(name, class, classkey, deserializer) if not class then class = name - name = class.__name__ or class.name + name = class.__name__ or class.name or class.__name end if not classkey then if class.__instanceDict then @@ -391,7 +396,7 @@ end, registerClass = function(name, class, classkey, deserializer) -- assume SECL classkey = '__baseclass' end - -- assume hump.class, Slither, or something else that doesn't store the + -- assume hump.class, Slither, Moonscript class or something else that doesn't store the -- class directly on the instance end if not deserializer then @@ -407,6 +412,9 @@ end, registerClass = function(name, class, classkey, deserializer) elseif class.__name__ then -- assume Slither deserializer = deserialize_Slither + elseif class.__base then + -- assume Moonscript class + deserializer = deserialize_Moonscript else error("no deserializer given for unsupported class library") end diff --git a/spec/bitser_spec.lua b/spec/bitser_spec.lua index 899260b..ce5d8a4 100644 --- a/spec/bitser_spec.lua +++ b/spec/bitser_spec.lua @@ -163,6 +163,37 @@ describe("bitser", function() assert.is_true(class.isinstance(serdeser(bojack), Horse)) bitser.unregisterClass('Horse') end) + it("serializes Moonscript class instances", function() + local Horse + do + local _class_0 + local _base_0 = {} + _base_0.__index = _base_0 + _class_0 = setmetatable({ + __init = function(self, name) + self.name = name + self[1] = 'instance can be sequence' + end, + __base = _base_0, + __name = "Horse"}, { + __index = _base_0, + __call = function(cls, ...) + local _self_0 = setmetatable({}, _base_0) + cls.__init(_self_0, ...) + return _self_0 + end + }) + _base_0.__class = _class_0 + Horse = _class_0 + end + assert.are.same(Horse.__name, "Horse") -- to shut coveralls up + bitser.registerClass(Horse) + local bojack = Horse('Bojack Horseman') + test_serdeser(bojack) + local new_bojack = serdeser(bojack) + assert.are.equal(new_bojack.__class, Horse) + bitser.unregisterClass('Horse') + end) it("serializes custom class instances", function() local Horse_mt = bitser.registerClass('Horse', {__index = {}}, nil, setmetatable) local function Horse(name)