diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 5f975463d..af6d9e2b7 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -225,6 +225,36 @@ jobs: paths: "test-results/*.xml" if: always() + test-bun: + runs-on: ${{ matrix.os }} + + needs: [build-core] + strategy: + fail-fast: false + matrix: + os: [ubuntu-latest] + + steps: + - uses: actions/checkout@v4 + + - name: Download build artifact + uses: actions/download-artifact@v4 + with: + name: core-build-${{ runner.os }} + path: ./dist/ + + - uses: oven-sh/setup-bun@v1 + + - name: install test requirements + working-directory: src/test-bun + run: | + bun --version + bun install + + - name: run bun tests + working-directory: src/test-bun + run: bun smoke-test.js + test-deno: runs-on: ${{ matrix.os }} diff --git a/src/js/compat.ts b/src/js/compat.ts index 4f83befd3..5a08873c9 100644 --- a/src/js/compat.ts +++ b/src/js/compat.ts @@ -262,6 +262,10 @@ export async function calculateDirname(): Promise { } let fileName = ErrorStackParser.parse(err)[0].fileName!; + if (IN_NODE && !fileName.startsWith("file://")) { + fileName = `file://${fileName}`; // Error stack filenames are not starting with `file://` in `Bun` + } + if (IN_NODE_ESM) { const nodePath = await import("node:path"); const nodeUrl = await import("node:url"); diff --git a/src/js/environments.ts b/src/js/environments.ts index c41d4245d..bc42780b9 100644 --- a/src/js/environments.ts +++ b/src/js/environments.ts @@ -5,8 +5,7 @@ export const IN_NODE = typeof process === "object" && typeof process.versions === "object" && typeof process.versions.node === "string" && - typeof process.browser === - "undefined"; /* This last condition checks if we run the browser shim of process */ + !process.browser; /* This last condition checks if we run the browser shim of process */ /** @private */ export const IN_NODE_COMMONJS = @@ -19,6 +18,9 @@ export const IN_NODE_COMMONJS = /** @private */ export const IN_NODE_ESM = IN_NODE && !IN_NODE_COMMONJS; +/** @private */ +export const IN_BUN = typeof globalThis.Bun !== "undefined"; + /** @private */ export const IN_DENO = typeof Deno !== "undefined"; // just in case... @@ -55,6 +57,7 @@ export function detectEnvironment(): Record { IN_NODE: IN_NODE, IN_NODE_COMMONJS: IN_NODE_COMMONJS, IN_NODE_ESM: IN_NODE_ESM, + IN_BUN: IN_BUN, IN_DENO: IN_DENO, IN_BROWSER: IN_BROWSER, IN_BROWSER_MAIN_THREAD: IN_BROWSER_MAIN_THREAD, diff --git a/src/test-bun/package.json b/src/test-bun/package.json new file mode 100644 index 000000000..0daa2a552 --- /dev/null +++ b/src/test-bun/package.json @@ -0,0 +1,17 @@ +{ + "name": "test-bun", + "module": "smoke-test.js", + "type": "module", + "scripts": { + "dev": "bun --watch smoke-test.js" + }, + "devDependencies": { + "@types/bun": "latest" + }, + "peerDependencies": { + "typescript": "^5.0.0" + }, + "dependencies": { + "pyodide": "../../dist/" + } +} diff --git a/src/test-bun/smoke-test.js b/src/test-bun/smoke-test.js new file mode 100644 index 000000000..4ca528d41 --- /dev/null +++ b/src/test-bun/smoke-test.js @@ -0,0 +1,13 @@ +import { loadPyodide } from "pyodide"; + +console.time("[load pyodide]"); +const pyodide = await loadPyodide(); +console.timeEnd("[load pyodide]"); + +console.time("[run pyodide]"); +const result = await pyodide.runPythonAsync(` +3+4 +`); +console.timeEnd("[run pyodide]"); + +console.log("result:", result.toString());