From b33079617253f844db5d7352280504b8f1851483 Mon Sep 17 00:00:00 2001 From: David Thompson Date: Fri, 5 Jul 2024 10:38:45 -0400 Subject: Add propagator experiment. --- chapter-7/propagators.js | 91 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 chapter-7/propagators.js (limited to 'chapter-7/propagators.js') diff --git a/chapter-7/propagators.js b/chapter-7/propagators.js new file mode 100644 index 0000000..1477a86 --- /dev/null +++ b/chapter-7/propagators.js @@ -0,0 +1,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 + } + } + }); +}); -- cgit v1.2.3