summaryrefslogtreecommitdiff
path: root/boot.js
blob: fbb2468ffac49a518c69313e10bf3b41a8dd509e (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
151
152
153
154
155
156
157
async function load() {
  const procMap = new WeakMap();

  function wrapProc(obj) {
    function makeWrapper() {
      const proc = scheme.to_js(obj);
      function wrapper (...args) {
        return proc.call(...args);
      }
      procMap.set(obj, wrapper);
      return wrapper;
    }

    return procMap.get(obj) || makeWrapper();
  }

  const mod = await SchemeModule.fetch_and_instantiate("game.wasm", {}, {
    window: {
      get() {
        return window;
      },
      innerWidth() {
        return window.innerWidth;
      },
      innerHeight() {
        return window.innerHeight;
      },
      requestAnimationFrame(proc) {
        window.requestAnimationFrame(wrapProc(proc));
      },
      setTimeout(proc, delay) {
        window.setTimeout(wrapProc(proc), delay);
      }
    },
    document: {
      get() { return document; },
      body() { return document.body; },
      getElementById: Document.prototype.getElementById.bind(document),
      createTextNode: Document.prototype.createTextNode.bind(document),
      createElement: Document.prototype.createElement.bind(document)
    },
    element: {
      value(elem) {
        return elem.value;
      },
      setValue(elem, value) {
        elem.value = value;
      },
      setWidth(elem, width) {
        elem.width = width;
      },
      setHeight(elem, height) {
        elem.height = height;
      },
      appendChild(parent, child) {
        return parent.appendChild(child);
      },
      setAttribute(elem, name, value) {
        elem.setAttribute(name, value);
      },
      removeAttribute(elem, name) {
        elem.removeAttribute(name);
      },
      remove(elem) {
        elem.remove();
      },
      replaceWith(oldElem, newElem) {
        oldElem.replaceWith(newElem);
      },
      addEventListener(elem, name, proc) {
        elem.addEventListener(name, wrapProc(proc));
      },
      removeEventListener(elem, name, proc) {
        elem.removeEventListener(name, wrapProc(proc));
      },
      clone(elem) {
        return elem.cloneNode();
      }
    },
    event: {
      preventDefault(event) {
        event.preventDefault();
      },
      keyboardCode(event) {
        return event.code;
      }
    },
    canvas: {
      getContext(elem, type) {
        return elem.getContext(type);
      },
      setFillColor(context, color) {
        context.fillStyle = color;
      },
      setFont(context, font) {
        context.font = font;
      },
      setTextAlign(context, align) {
        context.textAlign = align;
      },
      clearRect(context, x, y, w, h) {
        context.clearRect(x, y, w, h);
      },
      fillRect(context, x, y, w, h) {
        context.fillRect(x, y, w, h);
      },
      fillText(context, text, x, y) {
        context.fillText(text, x, y);
      },
      drawImage(context, image, sx, sy, sWidth, sHeight, dx, dy, dWidth, dHeight) {
        context.drawImage(image, sx, sy, sWidth, sHeight, dx, dy, dWidth, dHeight);
      },
      setScale(context, sx, sy) {
        context.scale(sx, sy);
      },
      setTransform(context, a, b, c, d, e, f) {
        context.setTransform(a, b, c, d, e, f);
      },
      setImageSmoothingEnabled(context, enabled) {
        context.imageSmoothingEnabled = (enabled == 1);
      }
    },
    audio: {
      new(src) {
        return new Audio(src);
      },
      play(audio) {
        audio.play();
      },
      pause(audio) {
        audio.pause();
      },
      volume(audio) {
        return audio.volume;
      },
      setVolume(audio, vol) {
        audio.volume = vol;
      },
      setLoop(audio, loop) {
        audio.loop = (loop == 1);
      },
      seek(audio, time) {
        audio.currentTime = time;
      }
    },
    image: {
      new(src) {
        const img = new Image();
        img.src = src;
        return img;
      }
    }
  });
  const scheme = await mod.reflect();
  scheme.init_module(mod);
}
window.addEventListener("load", load);