~technomancy/fennel-lang.org

ref: c9a7db72fe3fd06b5eec936984713f034acd9511 fennel-lang.org/shim.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
-- We tricked the Fennel repl into running inside a browser using Fengari and
-- init.lua+repl.fnl. Now we must trick our trick into running back inside
-- a normal shell, because debugging in the browser is kinda miserable.

local noop = function() return {} end

local createElement = function()
   return {
      style={},
      appendChild=table.insert,
      getAttribute=noop,
      setAttribute=noop,
      dispatchEvent=noop,
   }
end

local p = print
local open = io.open
local console = createElement()

-- print gets replaced with some DOM stuff, so make fake DOM stuff that actually
-- prints instead of inserting child elements:
function console.appendChild(_, line)
   p(table.concat(line, " "))
end

-- io.open gets replaced with XHR stuff, so create a fake XHR that knows how
-- to secretly io.open:
local open = function(x, _, filename)
   local f = assert(open(filename))
   x.response = f:read("*all")
   f:close()
end

package.loaded.js = {
   new = function(x) return x end,
   global = {
      XMLHttpRequest = {open=open, send=noop, status=200, statusText=""},
      document = {
         createTextNode=function(_, x) return x end,
         getElementById=function(_, id)
            return id == "fengari-console" and console or createElement()
         end,
         createElement=createElement,
         createEvent=function() return {initEvent=noop} end,
      },
   }
}

local coro = require("init")

coroutine.resume(coro, "(print :abc)")
coroutine.resume(coro, "(var x 1)")
coroutine.resume(coro, "(set x 2)")
coroutine.resume(coro, "(print x :better-equal 2)")