Wykres commitów

7 Commity (master)

Autor SHA1 Wiadomość Data
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 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 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