aboutsummaryrefslogtreecommitdiffstats
path: root/lib/init.server.moon
blob: 198c64e74fd5ddf71305eae8b43d4198febf1694 (plain)
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
export MODE, print, warn, relative, on_client
MODE = 'SERVER'

deep_tostring = (tbl, space='') ->
  buf = space .. tostring tbl

  return buf unless 'table' == type tbl

  buf = buf .. ' {\n'
  for k,v in pairs tbl
    buf = buf .. "#{space} [#{k}]: #{deep_tostring v, space .. '  '}\n"
  buf = buf .. "#{space}}"
  buf

print = do
  _print = print
  (...) ->
    contents = [deep_tostring v for v in *{ ... } ]
    _print table.unpack contents

-- warning messages
warn = (...) ->
  contents = [deep_tostring v for v in *{ ... } ]
  io.stderr\write table.concat contents, '\t'
  io.stderr\write '\n'

-- relative imports
relative = do
  _require = require

  (base, sub) ->
    sub = 0 unless 'number' == type sub

    for i=1, sub
      base = base\match '^(.*)%.%w+$'

    (name, x) ->
      name = base .. name if '.' == name\sub 1, 1
      _require name

import compile, insert_loader from require 'lib.duct_tape'
insert_loader!

on_client = (fn, ...) ->
  args = {...}
  -- warn code
  "<script type=\"application/lua\">
    local fn = #{compile fn}
    fn(#{table.concat [string.format '%q', v for v in *args ], ', '})
  </script>"

{
  -- access / flush the appended data
  flush: ->
    with x = buffer
      buffer = ''
}