Wykres commitów

51 Commity (master)

Autor SHA1 Wiadomość Data
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 e860e32e24 webassembly/objjsproxy: Fix proxying in arguments to JS new function.
Signed-off-by: Damien George <damien@micropython.org>
2024-05-06 14:47:05 +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 9681a66c6b webassembly/api: Fix importing micropython.mjs module from node REPL.
Fixes issue #14363.

Signed-off-by: Damien George <damien@micropython.org>
2024-05-06 13:53:58 +10:00
Damien George 49af8cad49 webassembly/api: Inject asyncio.run if needed by the script.
This allows a simple way to run the existing asyncio tests under the
webassembly port, which doesn't support `asyncio.run()`.

Signed-off-by: Damien George <damien@micropython.org>
2024-04-24 16:24:00 +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 84d6f8e8cb webassembly/modjsffi: Add jsffi.async_timeout_ms.
This function exposes `setTimeout()` as an async function.

Signed-off-by: Damien George <damien@micropython.org>
2024-04-24 16:24:00 +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 4c3f5f552b webassembly/objjsproxy: Fix handling of thrown value into JS generator.
Signed-off-by: Damien George <damien@micropython.org>
2024-04-24 16:07:00 +10:00
Damien George 9c7f0659e2 webassembly/api: Allocate code data on C heap when running Python code.
Otherwise Emscripten allocates it on the Emscripten C stack, which will
overflow for large amounts of code.

Fixes issue #14307.

Signed-off-by: Damien George <damien@micropython.org>
2024-04-24 13:15:54 +10:00
Damien George 45848f77ca webassembly/api: Fix waiting for Emscripten module to be loaded.
In modularize mode, the `_createMicroPythonModule()` constructor must be
await'ed on, before `Module` is ready to use.

Signed-off-by: Damien George <damien@micropython.org>
2024-04-24 13:15:54 +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 c2cf58befc webassembly/library: Fix formatting and style for Biome.
Signed-off-by: Damien George <damien@micropython.org>
2024-03-22 14:31:25 +11:00
Damien George 26d6969fef webassembly: Update README.md to describe latest changes.
Signed-off-by: Damien George <damien@micropython.org>
2024-03-22 14:31:25 +11:00
Damien George b9eb74e73b webassembly/variants/pyscript: Add pyscript variant.
This commit adds a pyscript variant for use in https://pyscript.net/.

The configuration is:
- No ASYNCIFY, in order to keep the WASM size down and have good
  performance.
- MICROPY_CONFIG_ROM_LEVEL_FULL_FEATURES to enable most features.
- Custom manifest that includes many of the python-stdlib libraries.
- MICROPY_GC_SPLIT_HEAP_AUTO to increase GC heap size instead of doing a
  collection when memory is exhausted.  This is needed because ASYNCIFY is
  disabled.  Instead the GC collection is run at the top-level before
  executing any Python code.
- No MICROPY_VARIANT_ENABLE_JS_HOOK because there is no asynchronous
  keyboard input to interrupt a running script.

Signed-off-by: Damien George <damien@micropython.org>
2024-03-22 14:31:25 +11:00
Damien George 6ff3e356e2 webassembly: Implement replInit() and replProcessChar().
This is the JavaScript API for starting and running a REPL.

Signed-off-by: Damien George <damien@micropython.org>
2024-03-22 14:31:25 +11:00
Damien George 625b17a410 webassembly: Implement runCLI() for a Node-based CLI.
This allows running MicroPython webassembly from the command line using:

    node micropython.mjs

Signed-off-by: Damien George <damien@micropython.org>
2024-03-22 14:31:25 +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
Damien George 691cd3a56d webassembly: Clean up Makefile and add variant support.
This commit cleans up and generalises the Makefile, adds support for
variants (following the unix port) and adds the "standard" variant as the
default variant.

Signed-off-by: Damien George <damien@micropython.org>
2024-03-22 13:37:09 +11:00
Damien George 98a8ff7a1a webassembly: Add support for enabling MICROPY_GC_SPLIT_HEAP_AUTO.
When enabled the GC will not reclaim any memory on a call to
`gc_collect()`.  Instead it will grow the heap.

Signed-off-by: Damien George <damien@micropython.org>
2024-03-22 13:10:05 +11:00
Damien George ae6bcc9d23 webassembly: Use POSIX write for output and add stderr.
All output is now handled by Emscripten's stdio facility.

Signed-off-by: Damien George <damien@micropython.org>
2024-03-22 13:06:39 +11:00
Damien George 8e3b701dee webassembly: Enable time localtime, gmtime, time, time_ns.
Signed-off-by: Damien George <damien@micropython.org>
2024-03-22 13:05:54 +11:00
Damien George 76898cbfa1 webassembly: Implement MICROPY_PY_RANDOM_SEED_INIT_FUNC.
Signed-off-by: Damien George <damien@micropython.org>
2024-03-22 13:05:54 +11:00
Damien George 8282bd93a2 webassembly: Move MP_JS_EPOCH init to library postset.
This eliminates the need for wrapper.js to run to set up the time.

Signed-off-by: Damien George <damien@micropython.org>
2024-03-22 13:05:54 +11:00
Damien George ff15dfcaa8 webassembly: Include lib in sys.path.
Following other ports.

Signed-off-by: Damien George <damien@micropython.org>
2024-03-22 13:05:54 +11:00
Angus Gratton decf8e6a8b all: Remove the "STATIC" macro and just use "static" instead.
The STATIC macro was introduced a very long time ago in commit
d5df6cd44a.  The original reason for this was
to have the option to define it to nothing so that all static functions
become global functions and therefore visible to certain debug tools, so
one could do function size comparison and other things.

This STATIC feature is rarely (if ever) used.  And with the use of LTO and
heavy inline optimisation, analysing the size of individual functions when
they are not static is not a good representation of the size of code when
fully optimised.

So the macro does not have much use and it's simpler to just remove it.
Then you know exactly what it's doing.  For example, newcomers don't have
to learn what the STATIC macro is and why it exists.  Reading the code is
also less "loud" with a lowercase static.

One other minor point in favour of removing it, is that it stops bugs with
`STATIC inline`, which should always be `static inline`.

Methodology for this commit was:

1) git ls-files | egrep '\.[ch]$' | \
   xargs sed -Ei "s/(^| )STATIC($| )/\1static\2/"

2) Do some manual cleanup in the diff by searching for the word STATIC in
   comments and changing those back.

3) "git-grep STATIC docs/", manually fixed those cases.

4) "rg -t python STATIC", manually fixed codegen lines that used STATIC.

This work was funded through GitHub Sponsors.

Signed-off-by: Angus Gratton <angus@redyak.com.au>
2024-03-07 14:20:42 +11:00
Damien George 90e517862d webassembly/Makefile: Remove --memory-init-file from linker options.
It's no longer supported by Emscripten (at least at 3.1.55).  And it's not
needed when the output is WASM, which it is by default.

Signed-off-by: Damien George <damien@micropython.org>
2024-03-04 11:33:07 +11:00
Maarten van der Schrieck 3bca93b2d0 ports: Fix sys.stdout.buffer.write() return value.
MicroPython code may rely on the return value of sys.stdout.buffer.write()
to reflect the number of bytes actually written. While in most scenarios a
write() operation is successful, there are cases where it fails, leading to
data loss. This problem arises because, currently, write() merely returns
the number of bytes it was supposed to write, without indication of
failure.

One scenario where write() might fail, is where USB is used and the
receiving end doesn't read quickly enough to empty the receive buffer. In
that case, write() on the MicroPython side can timeout, resulting in the
loss of data without any indication, a behavior observed notably in
communication between a Pi Pico as a client and a Linux host using the ACM
driver.

A complex issue arises with mp_hal_stdout_tx_strn() when it involves
multiple outputs, such as USB, dupterm and hardware UART. The challenge is
in handling cases where writing to one output is successful, but another
fails, either fully or partially. This patch implements the following
solution:

mp_hal_stdout_tx_strn() attempts to write len bytes to all of the possible
destinations for that data, and returns the minimum successful write
length.

The implementation of this is complicated by several factors:
- multiple outputs may be enabled or disabled at compiled time
- multiple outputs may be enabled or disabled at runtime
- mp_os_dupterm_tx_strn() is one such output, optionally containing
  multiple additional outputs
- each of these outputs may or may not be able to report success
- each of these outputs may or may not be able to report partial writes

As a result, there's no single strategy that fits all ports, necessitating
unique logic for each instance of mp_hal_stdout_tx_strn().

Note that addressing sys.stdout.write() is more complex due to its data
modification process ("cooked" output), and it remains unchanged in this
patch. Developers who are concerned about accurate return values from
write operations should use sys.stdout.buffer.write().

This patch might disrupt some existing code, but it's also expected to
resolve issues, considering that the peculiar return value behavior of
sys.stdout.buffer.write() is not well-documented and likely not widely
known. Therefore, it's improbable that much existing code relies on the
previous behavior.

Signed-off-by: Maarten van der Schrieck <maarten@thingsconnected.nl>
2023-12-22 10:32:46 +11:00
Jim Mussared 5015779a6f py/builtinevex: Handle invalid filenames for execfile.
If a non-string buffer was passed to execfile, then it would be passed
as a non-null-terminated char* to mp_lexer_new_from_file.

This changes mp_lexer_new_from_file to take a qstr instead (as in almost
all cases a qstr will be created from this input anyway to set the
`__file__` attribute on the module).

This now makes execfile require a string (not generic buffer) argument,
which is probably a good fix to make anyway.

Fixes issue #12522.

This work was funded through GitHub Sponsors.

Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2023-10-12 15:17:59 +11:00
Nicholas H.Tollervey 14c2b64131 webassembly: Replace typeof window check with ENVIRONMENT_IS_NODE flag.
When the "typeof window" check is run within a web worker the window is
undefined, causing an error because "require" is only defined in a Node
environment.  Change the logic to reflect the true intentions of when this
code should run, ie in Node only.

Signed-off-by: Damien George <damien@micropython.org>
2023-07-13 13:24:35 +10:00
elibdev 813d559bc0 webassembly: Make mp_js_process_char asynchronous.
This may also call the garbage collector.

Signed-off-by: Eli Bierman <eli@elib.dev>
2023-06-27 15:27:29 +10:00
elibdev b2ad7e238b webassembly: Make mp_js_do_str asynchronous.
This fixes a bug where `gc.collect()` would crash due to
emscripten_scan_stack being called synchronously within mp_js_do_str.  The
fix is to make mp_js_do_str asynchronous.

Fixes #10692.

Signed-off-by: Eli Bierman <eli@elib.dev>
2023-06-27 15:26:42 +10:00
Damien George fa8a81ae23 webassembly/modutime: Use extmod version of time module.
No API or functional change.

Signed-off-by: Damien George <damien@micropython.org>
2023-04-27 15:11:52 +10:00
Damien George 9955553001 extmod/modutime: Provide a generic time module.
Based on extmod/utime_mphal.c, with:
- a globals dict added
- time.localtime wrapper added
- time.time wrapper added
- time.time_ns function added

New configuration options are added for this module:
- MICROPY_PY_UTIME (enabled at basic features level)
- MICROPY_PY_UTIME_GMTIME_LOCALTIME_MKTIME
- MICROPY_PY_UTIME_TIME_TIME_NS

Signed-off-by: Damien George <damien@micropython.org>
2023-04-27 15:09:56 +10:00
Antonin ENFRUN db19ee7e15 webassembly/library: Extract and send data to print as UInt8Array.
This allows utf-8 data to work.  It's the receiving layer's responsibility
to deal with decoding the data.
2022-12-13 17:16:37 +11:00
David Lechner ea07ab04f8 webassembly/library: Make use of CustomEvent detail property.
This changes the CustomEvent for stdout to use the existing `detail`
property of CustomEvent instead of adding a `data` property.

Signed-off-by: David Lechner <david@pybricks.com>
2022-11-11 13:21:28 +11:00
Damien George 5987130afd webassembly/Makefile: Change compiler optimisation from O3 to Os.
Emscripten strongly advises the use of optimisation when compiling with
ASYNCIFY enabled.  Testing the difference betwen O3 and Os for various
configurations gives:

    flags                      firmware.wasm   micropython.js  perf
    -O3 -s ASYNCIFY            1342003          212845         0 (baseline)
    -O3 -s ASYNCIFY -s WASM=0        -         7064750         -30%
    -O3                         367131          196569         +140%
    -O3 -s WASM=0                    -         2818260         +30%
    -Os -s ASYNCIFY            1135450          213064         +40%
    -Os -s ASYNCIFY -s WASM=0        -         6239768         -30%
    -Os                         295028          196569         +180%
    -Os -s WASM=0                    -         2271358         +30%

The first row is prior to this commit.  The second and third columns show
firmware size (add them to get the total size).  The fourth column shows
the approximate change in performance compared to the baseline.  The
performance was measured using run-perfbench.py and the error was large, up
to 20%, although general trends in the change in performance could still be
seen.

In summary, using using Os instead of O3 makes it a little bit faster in
all cases, and smaller output (wasm/js) in all cases.

Signed-off-by: Damien George <damien@micropython.org>
2022-11-07 17:18:42 +11:00
Damien George 1ed740b152 webassembly/README: Update README to describe new stdout behaviour.
Signed-off-by: Damien George <damien@micropython.org>
2022-11-07 17:18:31 +11:00
Nicholas H.Tollervey db978d7155 webassembly: Dispatch micropython-print via document not mp_js_stdout. 2022-11-07 15:09:56 +11:00
Damien George 2d406f2226 webassembly: Support piping in a program on stdin.
The performance benchmark suite can now be run on the webassembly port.

Signed-off-by: Damien George <damien@micropython.org>
2022-11-03 23:35:22 +11:00
Damien George 7a505d57dc webassembly: Change "stack" argument to "heapsize".
Because that's what mp_js_init() takes as its argument.

Signed-off-by: Damien George <damien@micropython.org>
2022-11-03 18:47:48 +11:00
Damien George d65edaa232 webassembly: Use Date's now() instead of getTime().
Using now() is a bit faster because it doesn't need to create a new Date.

Signed-off-by: Damien George <damien@micropython.org>
2022-11-03 18:47:48 +11:00
Damien George 46bb52adf6 webassembly: Add support for VFS and enable VFS_POSIX.
This gets filesystem support working.

Signed-off-by: Damien George <damien@micropython.org>
2022-11-01 13:53:06 +11:00
Damien George 54478eb9e7 webassembly/mpconfigport: Use MICROPY_CONFIG_ROM_LEVEL_EXTRA_FEATURES.
This simplifies the config file.  This is not a no-op, it does enable a few
new features to bring the port in line with this config level.

Signed-off-by: Damien George <damien@micropython.org>
2022-11-01 13:10:45 +11:00