File: mod_notionflux.lua

package info (click to toggle)
notion 4.0.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 4,656 kB
  • sloc: ansic: 47,365; sh: 2,093; makefile: 594; perl: 270
file content (45 lines) | stat: -rw-r--r-- 1,293 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
if package.loaded["mod_notionflux"] then return end
if not ioncore.load_module("mod_notionflux") then
  return
end

local mod_notionflux=_G.mod_notionflux
if not mod_notionflux then
    return
end

-- takes input from mod_notionflux socket as a function (as produced by
-- loadstring), calls it with modified _G.print, returns formatted return value
-- or forwards an error
function mod_notionflux.run_lua(fn, idx)
    local function newprint(...)
        -- collecting strings in a table and using table.concat produces less garbage
        local out = {}
        for i,v in ipairs{...} do
            if i>1 then
                table.insert(out, "\t")
            end
            local s=tostring(v)
            if type(s) ~= "string" then
                error "'tostring' must return a string to 'print'"
            end
            table.insert(out, s)
        end
        table.insert(out, "\n")

        mod_notionflux.xwrite(idx, table.concat(out))
    end
    local oldprint=_G.print
    _G.print=newprint
    local ok, res=pcall(fn)
    _G.print=oldprint
    if not ok then error(res) end

    -- format a string result using %q
    res = type(res) == "string"
        and string.format("%q", res)
        or  tostring(res)
    return res
end

package.loaded["mod_notionflux"]=true