typeof.lua 841 B

12345678910111213141516171819202122232425262728293031323334
  1. --------------------------------------------------------------------------------
  2. -- Copyright (c) 2015 - 2016 , 蒙占志(topameng) topameng@gmail.com
  3. -- All rights reserved.
  4. -- Use, modification and distribution are subject to the "MIT License"
  5. --------------------------------------------------------------------------------
  6. local type = type
  7. local types = {}
  8. local _typeof = tolua.typeof
  9. local _findtype = tolua.findtype
  10. function typeof(obj)
  11. local t = type(obj)
  12. local ret = nil
  13. if t == "table" then
  14. ret = types[obj]
  15. if ret == nil then
  16. ret = _typeof(obj)
  17. types[obj] = ret
  18. end
  19. elseif t == "string" then
  20. ret = types[obj]
  21. if ret == nil then
  22. ret = _findtype(obj)
  23. types[obj] = ret
  24. end
  25. else
  26. error(debug.traceback("attemp to call typeof on type "..t))
  27. end
  28. return ret
  29. end