Improved Promise polyfill for MicroPython only (#1517)

This commit is contained in:
Andrea Giammarchi
2023-06-09 12:53:31 +02:00
committed by GitHub
parent 50b1a1d7c5
commit 818614b798
4 changed files with 10 additions and 44 deletions

View File

@@ -1,21 +1,7 @@
import { getBuffer } from "../fetch-utils.js";
import { absoluteURL, defineProperty } from "../utils.js";
import "@ungap/with-resolvers";
// REQUIRES INTEGRATION TEST
/* c8 ignore start */
// TODO: this should *NOT* be needed as the polyfill
// already patches on demand the Promise object
const { withResolvers } = Promise;
defineProperty(globalThis, "Promise", {
configurable: true,
value: class extends Promise {
withResolvers() {
return withResolvers.call(this);
}
},
});
/* c8 ignore stop */
import { getBuffer } from "../fetch-utils.js";
import { absoluteURL } from "../utils.js";
/**
* Trim code only if it's a single line that prettier or other tools might have modified.

View File

@@ -10,12 +10,19 @@ import {
const type = "micropython";
let patchPromise = true;
// REQUIRES INTEGRATION TEST
/* c8 ignore start */
export default {
type: [type, "mpy"],
module: () => `http://localhost:8080/micropython/micropython.mjs`,
async engine({ loadMicroPython }, config, url) {
// @bug https://github.com/micropython/micropython/issues/11749
if (patchPromise) {
patchPromise = false;
globalThis.Promise = class extends Promise {};
}
const { stderr, stdout, get } = stdio();
url = url.replace(/\.m?js$/, ".wasm");
const runtime = await get(loadMicroPython({ stderr, stdout, url }));

View File

@@ -50,6 +50,6 @@
"coincident": "^0.2.3"
},
"worker": {
"blob": "sha256-s/Qe9L8Mxmdjzk/TnVwmMYA5AvEemsRkOeqtZG6uBLM="
"blob": "sha256-m8uF2bNB691FtVIpuDfAT/avUolqZRQwO9tD3r88+6A="
}
}

View File

@@ -40,34 +40,7 @@
w = XWorker('./input.lua', type='lua')
w.sync.input = handle_input
</script>
<input type="text" placeholder="loading ..." required disabled />
<input type="submit" mpy-click="handle_result(event)" disabled />
<!--script type="mpy">
from js import XWorker, Array, Promise, Reflect, document
resolve = None
def trap_resolve(res, rej):
global resolve
resolve = res
def handle_result(event):
input = document.querySelector('input[type="text"]')
resolve(input.value)
def handle_input(data):
input = document.querySelector('input[type="text"]')
input.placeholder = data
input.disabled = False
# TODO: Promise.new(trap_resolve) does not work in MicroPython
# but the following throws in Pyodide 😅 so fallback to deferred
return Reflect.construct(Promise, Array(trap_resolve))
# TODO: Proxy do not work in MicroPython so type='py' it is
w = XWorker('./input.py', type='py')
w.sync.input = handle_input
</script-->
</body>
</html>