view src/luan/modules/Table.luan @ 1267:9fa8b8389578

add LuanTable.luan; support metatable __gc(); add luan.sql;
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 12 Nov 2018 02:10:41 -0700
parents 81d3a01fbd09
children 8d95711f6615
line wrap: on
line source

java()
local TableLuan = require "java:luan.modules.TableLuan"

local Table = {}

Table.clear = TableLuan.clear
Table.concat = TableLuan.concat
Table.copy = TableLuan.copy
Table.hash_value = TableLuan.hash_value
Table.insert = TableLuan.insert
Table.is_empty = TableLuan.is_empty
Table.pack = TableLuan.pack
Table.remove = TableLuan.remove
Table.size = TableLuan.size
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 toTable = TableLuan.toTable or error()

function Table.java_to_table_shallow(obj)
	local rtn = toTable(obj)
	if rtn ~= nil then
		return rtn
	end
	local tp = type(obj)
	if tp == "java" then
		tp = obj.getClass().getName()
	end
	error("can't convert type "..tp.." 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