~technomancy/fennel-lang.org

ref: c9a7db72fe3fd06b5eec936984713f034acd9511 fennel-lang.org/repl-worker.lua -rw-r--r-- 1.6 KiB
c9a7db72Phil Hagelberg Remove broken contributors link until we can get it fixed. 2 months ago
                                                                                
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package.path = "./?.lua"
local js = require "js"

local _G = _G
local pack = table.pack
local tostring = tostring

-- just make a few things not blow up
_G.os.exit = function() end
_G.os.getenv = function() return nil end

-- require-macros depends on io.open; we splice in a hacky replacement
io={open=function(filename)
    return {
        read = function(_, all)
            assert(all=="*all", "Can only read *all.")
            local xhr = js.new(js.global.XMLHttpRequest)
            xhr:open("GET", filename, false)
            xhr:send()
            assert(xhr.status == 200, xhr.status .. ": " .. xhr.statusText)
            return tostring(xhr.response)
        end,
        close = function() end,
    }
end}

function postContent(functionName, lines)
    for i = 1, lines.n do
        js.global:postMessage(functionName .. "|append|" .. tostring(lines[i]))
    end
    js.global:postMessage(functionName .. "|dispatch|")
end

_G.printLuacode = function(...)
    postContent("printLuacode", pack(...))
end

_G.print = function(...)
    postContent("print", pack(...))
end

_G.narrate = function(...)
    postContent("narrate", pack(...))
end

_G.printError = function(...)
    postContent("printError", pack(...))
end

local fennel = require("fennel/fennel")
package.loaded.fennel = fennel

repl = coroutine.create(fennel.dofile("repl.fnl"))

local welcome = "Welcome to Fennel " .. fennel.version
    .. ", running on Fengari (" .. _VERSION .. ")"

_G.print(welcome)

_G.printLuacode("Compiled Lua code")

js.global:postMessage("loaded|loaded|" .. welcome)

assert(coroutine.resume(repl))

js.global.onmessage = function(_, event)
    coroutine.resume(repl, event.data)
end