summaryrefslogtreecommitdiff
path: root/demo/index.html
blob: 7a1b082906db4b3bc79dae6863f27be80121308d (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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<!DOCTYPE html>
<head>
  <script src="libwasm-debug.js"></script>
</head>
<style>
  textarea {
    width: 100%;
    height: 400px;
    min-height: 400px;
    border: 1px solid #ccc;
    border-radius: 5px;
    margin: 0;
    padding: 5px;
  }

  textarea:focus {
    outline: none;
    border-color: #777;
    box-shadow: 0 0 4px rgba(0, 0, 0, 0.5);
  }

  section {
    position: relative;
    width: 80%;
    margin: auto;
    box-sizing: border-box;
  }

</style>
<body>
  <section>
    <textarea id="input" autofocus autocomplete="off" autocorrect="off"
        autocapitalize="off" spellcheck="false"></textarea>
  </section>
  <pre id="output">
    errors:
  </pre>
  <script>
    var defaultIndent = '  ';
    function insertTextAtSelection(input, src) {
      var selectionStart = input.selectionStart;
      var selectionEnd = input.selectionEnd;
      var oldValue = input.value;
      input.value = oldValue.slice(0, selectionStart) + src +
                    oldValue.slice(selectionEnd);
      input.selectionStart = input.selectionEnd = selectionStart + src.length;
    }

    function onInputKeyDown(e) {
      if (e.keyCode == 9) {  // tab
        insertTextAtSelection(this, defaultIndent);
        e.preventDefault();
      } else if (e.keyCode == 13) {  // newline
        // count nesting depth
        var parens = 0;
        var lastOpen = -1;
        var indent = '';
        for (var i = this.selectionStart - 1; i >= 0; --i) {
          var c = this.value[i];
          if (c == '(') {
            if (--parens < 0) {
              if (lastOpen != -1)
                i = lastOpen;
              else
                indent = defaultIndent;
              break;
            // find first sibling "(", if any
            } else if (parens == 0 && lastOpen == -1) {
              lastOpen = i;
            }
          } else if (c == ')') {
            parens++;
          }
        }
        // get column of current nesting
        var col = 0;
        for (; i > 0; --i) {
          var c = this.value[i];
          if (c == '\n') {
            col--;  // went back too far
            break;
          } else {
            col++;
          }
        }
        // write newline, plus indentation
        insertTextAtSelection(this, '\n' + ' '.repeat(col) + indent);
        e.preventDefault();
      }
    }

    function onError(loc, error, sourceLine, sourceLineColumnOffset) {
      var lines = [
        loc.filename + ':' + loc.line + ':' + loc.firstColumn,
        error
      ];
      if (sourceLine.length > 0) {
        var numSpaces = loc.firstColumn - 1 - sourceLineColumnOffset;
        var numCarets = loc.lastColumn - loc.firstColumn;
        lines.push(sourceLine);
        lines.push(' '.repeat(numSpaces) + '^'.repeat(numCarets));
      }
      output.textContent += lines.join('\n') + '\n';
    }

    var allocator = wasm.LibcAllocator;
    var eh = new wasm.SourceErrorHandler(onError, 80);

    function compile(text) {
      output.textContent = '';
      var buf = wasm.Buffer.fromString(text);
      var lexer = wasm.Lexer.fromBuffer(allocator, 'test.wast', buf);
      try {
        var script = wasm.parse(lexer, eh);
        wasm.checkAst(lexer, script, eh);

        try {
          var memoryWriter = new wasm.MemoryWriter(allocator);
          var writer = memoryWriter.base;
          var options = new wasm.WriteBinaryOptions({logWrites: true});
          wasm.writeBinaryScript(allocator, writer, script, options);

          output.textContent = 'done';
        } catch (e) {
        } finally {
          options.$destroy();
          memoryWriter.$destroy();
        }

      } catch (e) {
        // swallow error
      } finally {
        if (script)
          script.$destroy();
      }
      lexer.$destroy();
      buf.$destroy();
    }

    function onInputInput(e) {
      compile(input.value);
    }

    var input = document.getElementById('input');
    input.addEventListener('keydown', onInputKeyDown);
    input.addEventListener('input', onInputInput);

    var output = document.getElementById('output');
  </script>
</body>