Repository: pyscript/pyscript
File: pyscript.core/esm/runtime/_utils.js (JavaScript)

Code before:
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.
 * @param {string} code code that might be a single line
 * @returns {strong}
 */
export const clean = (code) =>
    code.replace(/^[^\r\n]+$/, (line) => line.trim());

// REQUIRES INTEGRATION TEST
/* c8 ignore start */
export const io = new WeakMap();
export const stdio = (init) => {
    const context = init || console;
    const localIO = {
        stderr: (context.stderr || console.error).bind(context),
        stdout: (context.stdout || console.log).bind(context),
    };
    return {
        stderr: (...args) => localIO.stderr(...args),
        stdout: (...args) => localIO.stdout(...args),
        async get(engine) {
            const runtime = await engine;
            io.set(runtime, localIO);
            return runtime;
        },
    };
};
/* c8 ignore stop */

// This should be the only helper needed for all Emscripten based FS exports
export const writeFile = (FS, path, buffer) => {
    const { parentPath, name } = FS.analyzePath(path, true);
    FS.mkdirTree(parentPath);
    return FS.writeFile([parentPath, name].join("/"), new Uint8Array(buffer), {
        canOwn: true,
    });
};

// This is instead a fallback for Lua or others

Review comment (refactor):
What the heck does this mean? You get `withResolvers` from the existing `Promise` which has been polyfilled by `@ungap/with-resolvers` and then... write over `Promise` with a new subclass that has `withResolvers` as a non-static method, but `withResolvers` was supposed to be a static method??

Also, `Promise.withResolvers` looks really great, so many codebases have to make this utility one way or another.

Code after:
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 */

/**
 * Trim code only if it's a single line that prettier or other tools might have modified.
 * @param {string} code code that might be a single line
 * @returns {string}
 */
export const clean = (code) =>
    code.replace(/^[^\r\n]+$/, (line) => line.trim());

// REQUIRES INTEGRATION TEST
/* c8 ignore start */
export const io = new WeakMap();
export const stdio = (init) => {
    const context = init || console;
    const localIO = {
        stderr: (context.stderr || console.error).bind(context),
        stdout: (context.stdout || console.log).bind(context),
    };
    return {
        stderr: (...args) => localIO.stderr(...args),
        stdout: (...args) => localIO.stdout(...args),
        async get(engine) {
            const runtime = await engine;
            io.set(runtime, localIO);