Debian Codegraph @

demo.html

raw
1 <!DOCTYPE html>
2 <html>
3 <body>
4 <div>
5 <label>Regex: <input id="regex" value="a -> b"></label><br>
6 <label>Input: <input id="input" value="aaa"></label><br>
7 <button onclick="apply('up')">Up</button>
8 <button onclick="apply('down')">Down</button>
9 <br>
10 <label>Result: <pre id="output"></pre></label>
11 </div>
12
13 <script>
14 var Module = { // Emscripten looks for global object named 'Module'
15 onRuntimeInitialized: function() { // When webassembly is loaded
16 window.foma = {
17 parseRegex: Module.cwrap('fsm_parse_regex', 'number', ['string', 'number', 'number']),
18 applyInit: Module.cwrap('apply_init', 'number', ['number']),
19 applyUp: Module.cwrap('apply_up', 'string', ['number', 'string']),
20 applyDown: Module.cwrap('apply_down', 'string', ['number', 'string']),
21 destroy: Module.cwrap('fsm_destroy', null, ['number']),
22 applyClear: Module.cwrap('apply_clear', null, ['number'])
23 };
24 }
25 };
26
27 function apply(dir) {
28 let result;
29 if (window.fsm) {
30 foma.applyClear(window.handle);
31 foma.destroy(window.fsm);
32 }
33
34 window.fsm = foma.parseRegex(document.getElementById('regex').value, 0, 0);
35 window.handle = foma.applyInit(window.fsm);
36 if (dir === `up`) {
37 result = foma.applyUp(window.handle, document.getElementById('input').value);
38
39 } else {
40 result = foma.applyDown(window.handle, document.getElementById('input').value);
41 }
42
43 document.getElementById('output').textContent = result || '(no output)';
44 }
45 </script>
46 <script src="libfoma.js"></script>
47 </body>
48 </html>