Posts for h4rdc0ded

Joined: 4/20/2011
Posts: 1
Location: Munich
I dont think there is any officially documentation (searched myself :( ). But Desmume's tostring function is modified, and allows you to retrieve *some* information about most of the exposed functions and their parameters. I've a small script which gathers all this information; the code is not pretty, but the generated html file is quite useful. Just copy the following code into a file and let Desmume run it.
--[[
Utility script for printing tables as html.
]]
local f = io.open('doc.html', 'wb')

local function pathAppend(path, key)
  assert(type(key) == 'string' or type(key) == 'number', 'Cant deal with ' .. type(key) .. ' as key.')
  if type(key) == 'string' then
    if not key:find'^[%w_]+$' then
      return string.format('%s[%q]')
    else
      if path ~= '' then
        return path .. '.' .. key
      else
        return key
      end
    end
  else
    return path .. '[' .. tostring(key) .. ']'
  end
end

local function elementSortLesserThan(a, b)
  if type(b[2]) == 'table' then
    if type(a[2]) == 'table' then
      return a[1]<b[1]
    else
      return true
    end
  else
    if type(a[2]) == 'table' then
      return false
    else
      return a[1]<b[1]
    end
  end
end

local function applyRecursive(t, func, depth, visited, path)
  depth = depth or 0
  visited = visited or {}
  path = path or ''
  assert(type(t) == 'table')
  assert(type(func) == 'function')
  assert(type(depth) == 'number')
  assert(type(visited) == 'table')
  assert(type(path) == 'string')
  
  local tmp = {}
  for k, v in pairs(t) do
    table.insert(tmp, {k, v})
  end
  table.sort(tmp, elementSortLesserThan)
  
  for _, t in ipairs(tmp) do
    if type(t[2]) == 'table' then
      if visited[t[2]] then
        func(t[1], t[2], depth, visited[t[2]], false)
      else
        visited[t[2]] = pathAppend(path, t[1])
        func(t[1], t[2], depth, visited[t[2]], true)
        applyRecursive(t[2], func, depth+1, visited, visited[t[2]])
      end
    else
      func(t[1], t[2], depth, path, true)
    end
  end
end

local printFunction = function(name, func)
  local s = tostring(func)
  if s:find(':', 1, true) then
    f:write('<a class="keyword">function</a> <a class="funcname">' .. name .. '</a>: <a class="undocumented">undocumented</a>')
  else
    f:write('<a class="keyword">function</a> <a class="funcname">' .. name .. '</a>')
    
    for leading, param in s:gmatch'([%,%[%]%(%)%.])%s*([%w_]*)%s*' do
      f:write(leading)
      if #param > 0 then f:write('<a class="param">'..param..'</a>') end
    end
  end
  f:write'<br />\n'
end

local prettyprint
do
  local ld = 0
  prettyprint = function(k, v, depth, path, firstVisit)
    if depth > ld then
      ld = depth
      f:write(string.format('<p style="margin-left:%ipx;">', 20*depth))
    elseif depth < ld then
      ld = depth
      f:write'</p>\n'
      if ld > 0 then
        f:write(string.format('<p style="margin-left:%ipx;">\n', 20*depth))
      end
    end
    
    if type(v) == 'table' then
      if firstVisit then
        f:write(string.format('<a class="table" name="%s">%s</a>:<br />\n', path, k))
      else
        f:write(string.format('<a class="table" href="#%s">%s</a>:<br />\n', path, k))
      end
    end
    
    if type(v) == 'function' then
      printFunction(k, v)
    end
  end
end

f:write[[
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
            "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head><style type="text/css">
body {
  font-family:"Courier New";
}
.keyword {
  color:blue;
  font-weight:bold;
}
.funcname {
  font-weight:bold;
}
.table {
  font-size:18px;
  color:rgb(0,128,192);
  font-weight:bold;
}
.undocumented {
  background-color:rgb(255,0,0);
  color:rgb(255,255,255);
}
.param {
  
}
</style></head>
<body>
]]

applyRecursive(package.loaded, prettyprint, 0, {[_G] = '', [package.loaded] = ''})

f:write'</body></html>'
f:close()
--edit: just realized that the thread is over a year old (was still on the first page). Well, maybe this helps someone else...