mirror of
https://github.com/python/cpython.git
synced 2025-12-08 06:10:17 +00:00
[3.14] gh-124621: Emscripten: Add support for async input devices (GH-136822) (GH-136935)
This is useful for implementing proper `input()`. It requires the
JavaScript engine to support the wasm JSPI spec which is now stage 4.
It is supported on Chrome since version 137 and on Firefox and node
behind a flag.
We override the `__wasi_fd_read()` syscall with our own variant that
checks for a readAsync operation. If it has it, we use our own async
variant of `fd_read()`, otherwise we use the original `fd_read()`.
We also add a variant of `FS.createDevice()` called
`FS.createAsyncInputDevice()`.
Finally, if JSPI is available, we wrap the `main()` symbol with
`WebAssembly.promising()` so that we can stack switch from `fd_read()`.
If JSPI is not available, attempting to read from an AsyncInputDevice
will raise an `OSError`.
(cherry picked from commit 7ae4749d06)
Co-authored-by: Hood Chatham <roberthoodchatham@gmail.com>
This commit is contained in:
parent
8e43b130f7
commit
d118bc061b
4 changed files with 252 additions and 1 deletions
25
Lib/test/test_capi/test_emscripten.py
Normal file
25
Lib/test/test_capi/test_emscripten.py
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
import unittest
|
||||
from test.support import is_emscripten
|
||||
|
||||
if not is_emscripten:
|
||||
raise unittest.SkipTest("Emscripten-only test")
|
||||
|
||||
from _testinternalcapi import emscripten_set_up_async_input_device
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
class EmscriptenAsyncInputDeviceTest(unittest.TestCase):
|
||||
def test_emscripten_async_input_device(self):
|
||||
jspi_supported = emscripten_set_up_async_input_device()
|
||||
p = Path("/dev/blah")
|
||||
self.addCleanup(p.unlink)
|
||||
if not jspi_supported:
|
||||
with open(p, "r") as f:
|
||||
self.assertRaises(OSError, f.readline)
|
||||
return
|
||||
|
||||
with open(p, "r") as f:
|
||||
for _ in range(10):
|
||||
self.assertEqual(f.readline().strip(), "ab")
|
||||
self.assertEqual(f.readline().strip(), "fi")
|
||||
self.assertEqual(f.readline().strip(), "xy")
|
||||
Loading…
Add table
Add a link
Reference in a new issue