classic.lua 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. --
  2. -- classic
  3. --
  4. -- Copyright (c) 2014, rxi
  5. -- Copyright (c) 2020, Astie Teddy (TSnake41)
  6. --
  7. -- This module is free software; you can redistribute it and/or modify it under
  8. -- the terms of the MIT license. See LICENSE for details.
  9. --
  10. -- Lynx version, with some modifications.
  11. --
  12. local Object = {}
  13. Object.__index = Object
  14. function Object:new()
  15. end
  16. function Object:extend()
  17. local cls = {}
  18. for k, v in pairs(self) do
  19. if k:find("__") == 1 then
  20. cls[k] = v
  21. end
  22. end
  23. cls.__index = cls
  24. cls.super = self
  25. return setmetatable(cls, self)
  26. end
  27. function Object:implement(...)
  28. for _, cls in pairs { ... } do
  29. for k, v in pairs(cls) do
  30. if self[k] == nil then
  31. self[k] = v
  32. end
  33. end
  34. end
  35. end
  36. function Object:is(T)
  37. local mt = getmetatable(self)
  38. while mt do
  39. if mt == T then
  40. return true
  41. end
  42. mt = getmetatable(mt)
  43. end
  44. return false
  45. end
  46. function Object:__tostring()
  47. return "Object"
  48. end
  49. function Object:__call(...)
  50. local obj = setmetatable({}, self)
  51. obj:new(...)
  52. return obj
  53. end
  54. return Object