From 134978255ba674870f67d7a65a051a8edc73b131 Mon Sep 17 00:00:00 2001 From: Daniel Silverstone Date: Sun, 5 Aug 2012 11:46:13 +0100 Subject: EXAMPLE: Show pairs, ipairs and next working, and allow lprint() to show simple tabular returns too --- example/simple-example.lua | 48 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/example/simple-example.lua b/example/simple-example.lua index c385474..a7ca3d4 100644 --- a/example/simple-example.lua +++ b/example/simple-example.lua @@ -49,7 +49,15 @@ local function lprint(...) print "Error encountered:" end for i = 2, foo.n do - print(foo[i]) + if type(foo[i]) == "table" then + print("{") + for k, v in pairs(foo[i]) do + print("",k,v) + end + print("}") + else + print(foo[i]) + end end print() end @@ -107,3 +115,41 @@ supple.host.set_limits { count = 100 } lprint(supple.host.run(long_run, "@long-code")) supple.host.set_limits { memory = 1000 } lprint(supple.host.run(long_run, "@big-code")) + + +-- next we demonstrate that ipairs works on proxied tables + +local summing = [[ + local t = ... + local tot = 0 + for i, v in ipairs(t) do + tot = tot + v + end + return tot +]] + +lprint(supple.host.run(summing, "@summing", { 10, 14, 3 })) + +-- next we demonstrate that next works on proxied tables + +local isempty = [[ + local t = ... + return next(t) == nil +]] + +lprint(supple.host.run(isempty, "@isempty", {})) +lprint(supple.host.run(isempty, "@isempty", {"bar"})) + +-- And now that pairs works on proxied tables + +local keys = [[ + local t = ... + local ret = {} + for k, v in pairs(t) do + ret[#ret+1] = k + end + ret.ret = ret + return ret +]] + +lprint(supple.host.run(keys, "@keys", { foo="bar", baz="meta" })) -- cgit v1.2.1