Mercurial Hosting > luan
diff 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 |
line wrap: on
line diff
--- a/src/luan/modules/Table.luan Sun Sep 23 22:32:34 2018 -0600 +++ b/src/luan/modules/Table.luan Mon Sep 24 13:09:16 2018 -0600 @@ -15,4 +15,38 @@ Table.sort = TableLuan.sort Table.unpack = TableLuan.unpack + +local Luan = require "luan:Luan.luan" +local error = Luan.error +local type = Luan.type or error() +local pairs = Luan.pairs or error() +local LuanJava = require "java:luan.Luan" +local toTable = LuanJava.toTable or error() + +function Table.java_to_table_shallow(obj) + return toTable(obj) or error("can't convert type "..obj.getClass().getName().." to table") +end + +local to_luan, deepen + +function to_luan(obj,java_to_table_shallow) + return type(obj)=="java" and deepen(java_to_table_shallow(obj),java_to_table_shallow) or obj +end + +function deepen(tbl,java_to_table_shallow) + local rtn = {} + for key, value in pairs(tbl) do + key = to_luan(key,java_to_table_shallow) + value = to_luan(value,java_to_table_shallow) + rtn[key] = value + end + return rtn +end + +function Table.java_to_table_deep(obj,java_to_table_shallow) + java_to_table_shallow = java_to_table_shallow or Table.java_to_table_shallow + return deepen(java_to_table_shallow(obj),java_to_table_shallow) +end + + return Table