Wed, 25 Sep 2013 15:00:13 +0100
examples/luastate.lua: Fix variable name (thanks Stephen Q)
9
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
1 | -- Copyright (c) 2011-2012 by Robert G. Jakabosky <bobby@neoawareness.com> |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
2 | -- |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
3 | -- Permission is hereby granted, free of charge, to any person obtaining a copy |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
4 | -- of this software and associated documentation files (the "Software"), to deal |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
5 | -- in the Software without restriction, including without limitation the rights |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
6 | -- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
7 | -- copies of the Software, and to permit persons to whom the Software is |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
8 | -- furnished to do so, subject to the following conditions: |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
9 | -- |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
10 | -- The above copyright notice and this permission notice shall be included in |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
11 | -- all copies or substantial portions of the Software. |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
12 | -- |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
13 | -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
14 | -- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
15 | -- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
16 | -- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
17 | -- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
18 | -- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
19 | -- THE SOFTWARE. |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
20 | |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
21 | local ltraverse = require"traverse" |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
22 | local sformat = string.format |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
23 | local have_getsize = false |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
24 | local getsize = function() |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
25 | return 0 |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
26 | end |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
27 | if not jit then |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
28 | getsize = require"getsize" |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
29 | have_getsize = true |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
30 | end |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
31 | |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
32 | local _M = {} |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
33 | |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
34 | function _M.dump_stats(file) |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
35 | local type_cnts = {} |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
36 | local function type_inc(t) |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
37 | type_cnts[t] = (type_cnts[t] or 0) + 1 |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
38 | end |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
39 | local type_mem = {} |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
40 | local function type_mem_inc(v) |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
41 | if not have_getsize then return end |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
42 | local t = type(v) |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
43 | local s = getsize(v) |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
44 | type_mem[t] = (type_mem[t] or 0) + s |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
45 | end |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
46 | -- build metatable->type map for userdata type detection. |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
47 | local ud_types = {} |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
48 | local reg = debug.getregistry() |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
49 | for k,v in pairs(reg) do |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
50 | if type(k) == 'string' and type(v) == 'table' then |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
51 | ud_types[v] = k |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
52 | end |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
53 | end |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
54 | local function ud_type(ud) |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
55 | return ud_types[debug.getmetatable(ud)] or "<unknown>" |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
56 | end |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
57 | local str_data = 0 |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
58 | local funcs = { |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
59 | ["edge"] = function(from, to, how, name) |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
60 | type_inc"edges" |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
61 | end, |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
62 | ["table"] = function(v) |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
63 | type_inc"table" |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
64 | type_mem_inc(v) |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
65 | end, |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
66 | ["string"] = function(v) |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
67 | type_inc"string" |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
68 | str_data = str_data + #v |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
69 | type_mem_inc(v) |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
70 | end, |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
71 | ["userdata"] = function(v) |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
72 | type_inc"userdata" |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
73 | type_inc(ud_type(v)) |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
74 | type_mem_inc(v) |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
75 | end, |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
76 | ["cdata"] = function(v) |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
77 | type_inc"cdata" |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
78 | end, |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
79 | ["func"] = function(v) |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
80 | type_inc"function" |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
81 | type_mem_inc(v) |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
82 | end, |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
83 | ["thread"] = function(v) |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
84 | type_inc"thread" |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
85 | type_mem_inc(v) |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
86 | end, |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
87 | } |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
88 | local ignores = {} |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
89 | for k,v in pairs(funcs) do |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
90 | ignores[#ignores + 1] = k |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
91 | ignores[#ignores + 1] = v |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
92 | end |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
93 | ignores[#ignores + 1] = type_cnts |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
94 | ignores[#ignores + 1] = funcs |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
95 | ignores[#ignores + 1] = ignores |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
96 | |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
97 | ltraverse.traverse(funcs, ignores) |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
98 | |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
99 | local fd = file |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
100 | if type(file) == 'string' then |
10
cafaa46928c8
examples/luastate.lua: Fix variable name (thanks Stephen Q)
Matthew Wild <mwild1@gmail.com>
parents:
9
diff
changeset
|
101 | fd = io.open(file, "w") |
9
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
102 | end |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
103 | fd:write(sformat("memory = %i bytes\n", collectgarbage"count" * 1024)) |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
104 | fd:write(sformat("str_data = %i\n", str_data)) |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
105 | fd:write(sformat("object type counts:\n")) |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
106 | for t,cnt in pairs(type_cnts) do |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
107 | fd:write(sformat(" %9s = %9i\n", t, cnt)) |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
108 | end |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
109 | fd:write("\n") |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
110 | if have_getsize then |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
111 | fd:write(sformat("per type memory usage:\n")) |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
112 | local total = 0 |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
113 | for t,mem in pairs(type_mem) do |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
114 | total = total + mem |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
115 | fd:write(sformat(" %9s = %9i bytes\n", t, mem)) |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
116 | end |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
117 | fd:write(sformat("total: %9i bytes\n", total)) |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
118 | fd:write("\n") |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
119 | end |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
120 | --[[ |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
121 | fd:write("LUA_REGISTRY dump:\n") |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
122 | for k,v in pairs(reg) do |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
123 | fd:write(tostring(k),'=', tostring(v),'\n') |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
124 | end |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
125 | --]] |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
126 | if type(file) == 'string' then |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
127 | fd:close() |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
128 | end |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
129 | end |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
130 | |
cefa0be5f567
Add examples/luastate.lua from http://article.gmane.org/gmane.comp.lang.lua.general/90539
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
131 | return _M |