Wykres commitów

14 Commity (master)

Autor SHA1 Wiadomość Data
Damien George cfd5a8ea3a webassembly/proxy_c: Return undefined if dict lookup failed on JS side.
Instead of raising KeyError.  These semantics match JavaScript behaviour
and make it much more seamless to pass Python dicts through to JavaScript
as though they were JavaScript {} objects.

Signed-off-by: Damien George <damien@micropython.org>
2024-05-16 12:49:42 +10:00
Damien George aa2e3880c1 webassembly/proxy_js: Create a special "undefined" type for Python.
This adds a new undefined singleton to Python, that corresponds directly to
JavaScript `undefined`.  It's accessible via `js.undefined`.

Signed-off-by: Damien George <damien@micropython.org>
2024-05-16 12:49:10 +10:00
Damien George 0148bbb495 webassembly/proxy_js: Revert back to converting Py None to JS null.
This reverts part of commit fa23e4b093, to
make it so that Python `None` converts to JavaScript `null` (and JavaScript
`null` already converts to Python `None`).  That's consistent with how the
`json` module converts these values back and forth.

Signed-off-by: Damien George <damien@micropython.org>
2024-05-16 12:44:43 +10:00
Damien George fa23e4b093 webassembly/proxy_js: Convert JS undefined and JS null to Py None.
And change Py None conversion so it converts to JS undefined.

The semantics for conversion of these objects are then:
- Python None           -> JavaScript undefined
- JavaScript undefined  -> Python None
- JavaScript null       -> Python None

This follows Pyodide:
https://pyodide.org/en/stable/usage/type-conversions.html

Signed-off-by: Damien George <damien@micropython.org>
2024-05-13 11:53:10 +10:00
Damien George a67e326cb9 webassembly/proxy_c: Ensure objs thrown into generators are exceptions.
This commit defines a new `JsException` exception type which is used on the
Python side to wrap JavaScript errors.  That's then used when a JavaScript
Promise is rejected, and the reason is then converted to a `JsException`
for the Python side to handle.

This new exception is exposed as `jsffi.JsException`.

Signed-off-by: Damien George <damien@micropython.org>
2024-05-13 11:52:17 +10:00
Damien George 3f34be69c7 webassembly/asyncio: Fix case where a Promise is resolved with no arg.
Signed-off-by: Damien George <damien@micropython.org>
2024-05-13 11:48:41 +10:00
Damien George be1ecb54e6 webassembly/api: Resolve thenables returned from runPythonAsync.
JavaScript semantics are such that the caller of an async function does not
need to await that function for it to run to completion.  This commit makes
that behaviour also apply to top-level async Python code run via
`runPythonAsync()`.

Signed-off-by: Damien George <damien@micropython.org>
2024-05-07 11:33:05 +10:00
Damien George c056840ee8 webassembly/objpyproxy: Implement JS iterator protocol for Py iterables.
This allows using JavaScript for..of on Python iterables.

Signed-off-by: Damien George <damien@micropython.org>
2024-05-07 00:20:56 +10:00
Damien George 9da63a343e webassembly/proxy_c: Reject promises with a PythonError instance.
The `reason` in a rejected promise should be an instance of `Error`.  That
leads to better error messages on the JavaScript side.

Signed-off-by: Damien George <damien@micropython.org>
2024-05-06 14:04:13 +10:00
Damien George 8a3546b3bd webassembly: Add JavaScript-based asyncio support.
This commit adds a significant portion of the existing MicroPython asyncio
module to the webassembly port, using parts of the existing asyncio code
and some custom JavaScript parts.

The key difference to the standard asyncio is that this version uses the
JavaScript runtime to do the actual scheduling and waiting on events, eg
Promise fulfillment, timeouts, fetching URLs.

This implementation does not include asyncio.run(). Instead one just uses
asyncio.create_task(..) to start tasks and then returns to the JavaScript.
Then JavaScript will run the tasks.

The implementation here tries to reuse as much existing asyncio code as
possible, and gets all the semantics correct for things like cancellation
and asyncio.wait_for.  An alternative approach would reimplement Task,
Event, etc using JavaScript Promise's.  That approach is very difficult to
get right when trying to implement cancellation (because it's not possible
to cancel a JavaScript Promise).

Signed-off-by: Damien George <damien@micropython.org>
2024-04-24 16:24:00 +10:00
Damien George 5114f2c1ea webassembly/proxy_js: Allow a Python proxy of a function to be undone.
This optimises the case where a Python function is, for example, stored to
a JavaScript attribute and then later retrieved from Python.  The Python
function no longer needs to be a proxy with double proxying needed for the
call from Python -> JavaScript -> Python.

Signed-off-by: Damien George <damien@micropython.org>
2024-03-30 13:13:51 +11:00
Damien George 7c62fbe3f2 webassembly/proxy_js: Promote Python thenable to a Promise.
Signed-off-by: Damien George <damien@micropython.org>
2024-03-30 13:13:51 +11:00
Damien George 3997532186 webassembly/proxy_c: Ensure return value of async fun is passed to JS.
Signed-off-by: Damien George <damien@micropython.org>
2024-03-30 13:13:51 +11:00
Damien George c1513a078d tests/ports/webassembly: Add webassembly JS tests.
Signed-off-by: Damien George <damien@micropython.org>
2024-03-22 14:31:25 +11:00