comparison src/luan/modules/Table.luan @ 1261:198d6af7330a

rename Luan.to_table to Table.java_to_table_shallow and Luan.to_luan to Table.java_to_table_deep
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 24 Sep 2018 13:09:16 -0600
parents 4b5b84853f6f
children 81d3a01fbd09
comparison
equal deleted inserted replaced
1260:4b5b84853f6f 1261:198d6af7330a
13 Table.remove = TableLuan.remove 13 Table.remove = TableLuan.remove
14 Table.size = TableLuan.size 14 Table.size = TableLuan.size
15 Table.sort = TableLuan.sort 15 Table.sort = TableLuan.sort
16 Table.unpack = TableLuan.unpack 16 Table.unpack = TableLuan.unpack
17 17
18
19 local Luan = require "luan:Luan.luan"
20 local error = Luan.error
21 local type = Luan.type or error()
22 local pairs = Luan.pairs or error()
23 local LuanJava = require "java:luan.Luan"
24 local toTable = LuanJava.toTable or error()
25
26 function Table.java_to_table_shallow(obj)
27 return toTable(obj) or error("can't convert type "..obj.getClass().getName().." to table")
28 end
29
30 local to_luan, deepen
31
32 function to_luan(obj,java_to_table_shallow)
33 return type(obj)=="java" and deepen(java_to_table_shallow(obj),java_to_table_shallow) or obj
34 end
35
36 function deepen(tbl,java_to_table_shallow)
37 local rtn = {}
38 for key, value in pairs(tbl) do
39 key = to_luan(key,java_to_table_shallow)
40 value = to_luan(value,java_to_table_shallow)
41 rtn[key] = value
42 end
43 return rtn
44 end
45
46 function Table.java_to_table_deep(obj,java_to_table_shallow)
47 java_to_table_shallow = java_to_table_shallow or Table.java_to_table_shallow
48 return deepen(java_to_table_shallow(obj),java_to_table_shallow)
49 end
50
51
18 return Table 52 return Table