summaryrefslogtreecommitdiff
path: root/chapter-7/propagators.js
blob: 1477a8611546311df7af8c45a3ef8b85325f9de5 (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
window.addEventListener("load", async () => {
  await Scheme.load_main("propagators.wasm", {
    reflect_wasm_dir: "hoot",
    user_imports: {
      window: {
        scrollTo: window.scrollTo,
        setTimeout: setTimeout,
        getWindow: function() {
          return window;
        },
      },
      document: {
        makeTextNode: Document.prototype.createTextNode.bind(document),
        makeElement: Document.prototype.createElement.bind(document),
        getElementById: Document.prototype.getElementById.bind(document),
        getBody: function() {
          return document.body;
        },
      },
      element: {
        firstChild: (elem) => elem.firstChild,
        nextSibling: (elem) => elem.nextSibling,
        appendChild: function(parent, child) {
          return parent.appendChild(child);
        },
        setAttribute: function(elem, attr, value) {
          return elem.setAttribute(attr, value);
        },
        getAttribute: function(elem, attr) {
          return elem.getAttribute(attr);
        },
        getProperty: function(elem, property, dflt) {
          // The little + '' ensures the value is a string
          return value = elem[property] + '';
        },
        setProperty: function(elem, property, new_value) {
          elem[property] = new_value;
        },
        getValue: (elem) => elem.value,
        setValue: (elem, val) => elem.value = val,
        remove: function(elem) {
          elem.remove();
        },
        replaceWith: (oldElem, newElem) => oldElem.replaceWith(newElem),
        addEventListener: function(elem, name, f) {
          elem.addEventListener(name, f);
        },
      },
      websocket: {
        create: function(uri) {
          ws = new WebSocket(uri);
          return ws;
        },
        registerHandler: function(sock, handler, f) {
          sock[handler] = function (event) {
            if (handler == "onmessage") {
              f(event.data);
            } else {
              f(event);
            }
          };
        },
        send: function(sock, message) {
          sock.send(message);
        },
      },
      notification: {
        create: function(text) {
          return new Notification(text);
        },
        permission: function() {
          return Notification.permission;
        },
        requestPermission: Notification.requestPermission,
      },
      promise: {
        on: function(promise, f) {
          promise.then(f);
        },
      },
      localStorage: {
        getItem: (key) => localStorage.getItem(key) || "#f",
        setItem: (key, value) => localStorage.setItem(key, value)
      },
      location: {
        protocol: () => location.protocol,
        hostname: () => location.hostname
      }
    }
  });
});