Skip to content

Commit 5033a49

Browse files
committed
Make JSPI work correctly
1 parent 3756d12 commit 5033a49

File tree

6 files changed

+49
-1
lines changed

6 files changed

+49
-1
lines changed

src/pyodide/internal/pool/builtin_wrappers.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ export function finishSetup() {
7474
}
7575

7676
export function newWasmModule(buffer: Uint8Array): WebAssembly.Module {
77+
if (!UnsafeEval) {
78+
return new WebAssembly.Module(buffer);
79+
}
7780
if (finishedSetup) {
7881
checkCallee();
7982
}

src/pyodide/python-entrypoint-helper.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,13 @@ async function preparePython(): Promise<PyModule> {
135135
return mainModule;
136136
}
137137

138+
function callHandler(handler: PyCallable, args: any[]): any {
139+
if (handler.callWithOptions) {
140+
return handler.callWithOptions({ relaxed: true, promising: true }, ...args);
141+
}
142+
return handler.callRelaxed(...args);
143+
}
144+
138145
function makeHandler(pyHandlerName: string): Handler {
139146
return async function (...args: any[]) {
140147
try {
@@ -149,7 +156,7 @@ function makeHandler(pyHandlerName: string): Handler {
149156
);
150157
}
151158
const result = await enterJaegerSpan('python_code', () => {
152-
return handler.callRelaxed(...args);
159+
return callHandler(handler, args);
153160
});
154161

155162
// Support returning a pyodide.ffi.FetchResponse.

src/pyodide/types/Pyodide.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ declare type Handler = (...args: any[]) => any;
55
declare type PyCallable = ((...args: any[]) => any) & {
66
call: (...args: any[]) => any;
77
callRelaxed: (...args: any[]) => any;
8+
callWithOptions?: (
9+
options: { relaxed?: boolean; promising?: boolean; kwargs?: boolean },
10+
...args: any[]
11+
) => any;
812
};
913

1014
interface PyModule {

src/workerd/server/tests/python/BUILD.bazel

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,8 @@ py_wd_test("filter-non-py-files")
7272
py_wd_test("durable-object")
7373

7474
py_wd_test("worker-entrypoint")
75+
76+
py_wd_test(
77+
"jspi",
78+
skip_python_flags = ["0.26.0a2"],
79+
)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using Workerd = import "/workerd/workerd.capnp";
2+
3+
const unitTests :Workerd.Config = (
4+
services = [
5+
( name = "jspi",
6+
worker = (
7+
modules = [
8+
(name = "worker.py", pythonModule = embed "worker.py")
9+
],
10+
compatibilityDate = "2024-01-15",
11+
compatibilityFlags = [%PYTHON_FEATURE_FLAGS],
12+
)
13+
),
14+
],
15+
);
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from asyncio import sleep
2+
3+
from pyodide import __version__
4+
5+
6+
def test():
7+
if __version__ == "0.26.0a2":
8+
print("JSPI not supported on 0.26.0a2, skipping")
9+
return
10+
11+
from pyodide.ffi import run_sync
12+
13+
run_sync(sleep(0.01))
14+
print("Okay")

0 commit comments

Comments
 (0)