comparison src/luan/modules/Luan.luan @ 1260:4b5b84853f6f

replace Table.as_table with Luan.to_table
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 23 Sep 2018 22:32:34 -0600
parents e8020216dee7
children 198d6af7330a
comparison
equal deleted inserted replaced
1259:e8020216dee7 1260:4b5b84853f6f
41 41
42 local error = Luan.error 42 local error = Luan.error
43 local type = Luan.type or error() 43 local type = Luan.type or error()
44 local pairs = Luan.pairs or error() 44 local pairs = Luan.pairs or error()
45 45
46 -- tries to convert to luan 46 Luan.to_table = LuanJava.toTable
47 function Luan.java_to_luan(obj)
48 return LuanJava.toTable(obj) or obj
49 end
50 47
51 function Luan.to_luan(obj,java_to_luan) 48 local function to_luan(obj,to_table)
52 if type(obj) ~= "java" then 49 if type(obj) ~= "java" then
53 return obj 50 return obj
54 end 51 end
55 java_to_luan = java_to_luan or Luan.java_to_luan 52 obj = to_table(obj) or error("can't convert type "..obj.getClass().getName().." to luan")
56 obj = java_to_luan(obj)
57 type(obj) ~= "java" or error("can't convert type "..obj.getClass().getName().." to luan")
58 if type(obj) ~= "table" then
59 return obj
60 end
61 local tbl = {} 53 local tbl = {}
62 for key, value in pairs(obj) do 54 for key, value in pairs(obj) do
63 key = Luan.to_luan(key,java_to_luan) 55 key = to_luan(key,to_table)
64 value = Luan.to_luan(value,java_to_luan) 56 value = to_luan(value,to_table)
65 tbl[key] = value 57 tbl[key] = value
66 end 58 end
67 return tbl 59 return tbl
68 end 60 end
69 61
62 function Luan.to_luan(obj,to_table)
63 to_table = to_table or Luan.to_table
64 return to_luan(obj,to_table)
65 end
66
70 return Luan 67 return Luan