Wykres commitów

13 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 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 c37eb93f2d webassembly/proxy_c: Support more than 4 args when JS calls Py func.
Signed-off-by: Damien George <damien@micropython.org>
2024-05-13 11:48:41 +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 50b43fec1a webassembly/proxy_c: Only proxy across resolve/reject funs when needed.
To improve efficiency.

Signed-off-by: Damien George <damien@micropython.org>
2024-05-06 14:04:22 +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 d998ca78c8 webassembly/proxy_c: Fix then-continue to convert reason to throw value.
When a Promise is rejected on the JavaScript side, the reject reason should
be thrown into the encapsulating generator on the Python side.

Signed-off-by: Damien George <damien@micropython.org>
2024-04-24 16:23:42 +10:00
Damien George 92b3b69648 webassembly/proxy_c: Fix proxy then reject handling.
An exception on the Python side should be passed to the Promise reject
callback on the JavaScript side.

Signed-off-by: Damien George <damien@micropython.org>
2024-04-24 16:14:17 +10: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 9b090603a0 webassembly: Implement runPythonAsync() for top-level async code.
With this commit, `interpreter.runPythonAsync(code)` can now be used to run
Python code that uses `await` at the top level.  That will yield up to
JavaScript and produce a thenable, which the JavaScript runtime can then
resume.  Also implemented is the ability for Python code to await on
JavaScript promises/thenables.  For example, outer JavaScript code can
await on `runPythonAsync(code)` which then runs Python code that does
`await js.fetch(url)`.  The entire chain of calls will be suspended until
the fetch completes.

Signed-off-by: Damien George <damien@micropython.org>
2024-03-22 13:37:47 +11:00
Damien George 39bd0b8a0a webassembly: Add JavaScript proxying, and js and jsffi modules.
This commit improves the webassembly port by adding:

- Proxying of Python objects to JavaScript with a PyProxy type that lives
  on the JavaScript side.  PyProxy implements JavaScript Proxy traps such
  as has, get, set and ownKeys, to make Python objects have functionality
  on the JavaScript side.

- Proxying of JavaScript objects to Python with a JsProxy type that lives
  on the Python side.  JsProxy passes through calls, attributes,
  subscription and iteration from Python to JavaScript.

- A top-level API on the JavaScript side to construct a MicroPython
  interpreter instance via `loadMicroPython()`.  That function returns an
  object that can be used to execute Python code, access the Python globals
  dict, access the Emscripten filesystem, and other things.  This API is
  based on the API provided by Pyodide (https://pyodide.org/).  As part of
  this, the top-level file is changed from `micropython.js` to
  `micropython.mjs`.

- A Python `js` module which can be used to access all JavaScript-side
  symbols, for example the DOM when run within a browser.

- A Python `jsffi` module with various helper functions like
  `create_proxy()` and `to_js()`.

- A dedenting lexer which automatically dedents Python source code if every
  non-empty line in that source starts with a common whitespace prefix.
  This is very helpful when Python source code is indented within a string
  within HTML or JavaScript for formatting reasons.

Signed-off-by: Damien George <damien@micropython.org>
2024-03-22 13:37:47 +11:00