summaryrefslogtreecommitdiff
path: root/boot.js
diff options
context:
space:
mode:
Diffstat (limited to 'boot.js')
-rw-r--r--boot.js103
1 files changed, 103 insertions, 0 deletions
diff --git a/boot.js b/boot.js
new file mode 100644
index 0000000..161a4d9
--- /dev/null
+++ b/boot.js
@@ -0,0 +1,103 @@
+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: {
+ 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));
+ }
+ },
+ event: {
+ keyboardCode(event) {
+ return event.code;
+ }
+ },
+ canvas: {
+ getContext(elem, type) {
+ return elem.getContext(type);
+ },
+ setFillColor(context, color) {
+ context.fillStyle = color;
+ },
+ clearRect(context, x, y, w, h) {
+ context.clearRect(x, y, w, h);
+ },
+ fillRect(context, x, y, w, h) {
+ context.fillRect(x, y, w, h);
+ },
+ 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);
+ }
+ }
+ });
+ const scheme = await mod.reflect();
+ scheme.init_module(mod);
+}
+window.addEventListener("load", load);