Live data from Hacker News

Wasm2Mpy: Compiling WASM to MicroPython so it can run on Raspberrys

github.com

21–30 of 41 posts

Re: Wasm2Mpy: Compiling WASM to MicroPython so it can run on Raspberrys

#21

Earlier quoted context omitted.

In this case, the modules contain native code compiled for the target architecture. Micropython has something approximating a dynamic linker to load them. https://docs.micropython.org/en/latest/develop/natmod.html

tools/mpy_ld.py: https://github.com/micropython/micropython/blob/master/tools... tools/mpy-tool.py lists opcodes: https://github.com/micropython/micropython/blob/master/tools... Can the same be done with .pyc files; what are the advantages of MicroPython native modules? Why does it need wasm2c?

I don't think .pyc files can contain native code, but .mpy can! And that's exactly what I'm using here:

- compile any language to wasm

- translate wasm to C99

- use the native target toolchain to compile C99 to .mpy

- run that on the device

Re: Wasm2Mpy: Compiling WASM to MicroPython so it can run on Raspberrys

#22
post #11

What would be the recommendation to run on ESP32? https://github.com/wasm3/wasm3 ? https://github.com/espressif/esp-wasmachine ? https://github.com/bytecodealliance/wasm-micro-runtime/tree/... ? https://github.com/TOPLLab/WARDuino ?

For esp32 I'd recommend https://github.com/wasm3/embedded-wasm-apps

It obviously depends on your needs/goals.

P.S. I'm also the author of wasm3

Re: Wasm2Mpy: Compiling WASM to MicroPython so it can run on Raspberrys

#23
post #2

I feel like this project is a classic example of the README missing a "Why?" section. Not to justify the project, it just makes it hard to evaluate without understanding why they choose implementing a transpiler rather than an embedded WASM VM. Or why not transpile to native assembly? I'm sure they have good reasons but they aren't listed here.

Thanks for the feedback. I'll improve the README based on your inputs. I was focused on the actual research. Overall, this allows writing code in statically compiled languages and running it (fast) on embedded systems with MicroPython. MicroPython itself is comparatively slow, and this provides tools to deliver more demanding software (AI, signal processing, etc).

There exists tooling already to compile for wasm from static languages (rust, C++, and so on) and there exists tooling to run this WASM on a raspberry pi (wasmer, etc).

That makes me feel the end goal here is not what's described ("make wasm so it can run on a raspberry"), but rather "make wasm run in micropython".

Am I missing something?

Re: Wasm2Mpy: Compiling WASM to MicroPython so it can run on Raspberrys

#24
post #11

What would be the recommendation to run on ESP32? https://github.com/wasm3/wasm3 ? https://github.com/espressif/esp-wasmachine ? https://github.com/bytecodealliance/wasm-micro-runtime/tree/... ? https://github.com/TOPLLab/WARDuino ?

For esp32 I'd recommend https://github.com/wasm3/embedded-wasm-apps It obviously depends on your needs/goals. P.S. I'm also the author of wasm3

thanks for your answer!! Can you tell me a bit more about why this suggestion might be more appropriate than the others?

Is it in terms of maturity? Speed? Memory footprint? Safety?

Re: Wasm2Mpy: Compiling WASM to MicroPython so it can run on Raspberrys

#25
post #11

What would be the recommendation to run on ESP32? https://github.com/wasm3/wasm3 ? https://github.com/espressif/esp-wasmachine ? https://github.com/bytecodealliance/wasm-micro-runtime/tree/... ? https://github.com/TOPLLab/WARDuino ?

For esp32 I'd recommend https://github.com/wasm3/embedded-wasm-apps It obviously depends on your needs/goals. P.S. I'm also the author of wasm3

Actually in my case I need a VM, because I want users to be able to upload "extension plugins" in real-time, without reuploading firmware

In that case which approach / library would you recommend to embedd a WASM VM?

Re: Wasm2Mpy: Compiling WASM to MicroPython so it can run on Raspberrys

#26
post #23

Earlier quoted context omitted.

Thanks for the feedback. I'll improve the README based on your inputs. I was focused on the actual research. Overall, this allows writing code in statically compiled languages and running it (fast) on embedded systems with MicroPython. MicroPython itself is comparatively slow, and this provides tools to deliver more demanding software (AI, signal processing, etc).

There exists tooling already to compile for wasm from static languages (rust, C++, and so on) and there exists tooling to run this WASM on a raspberry pi (wasmer, etc). That makes me feel the end goal here is not what's described ("make wasm so it can run on a raspberry"), but rather "make wasm run in micropython". Am I missing something?

Yes, we're talking about Raspberry Pi Pico (RP2040) here. Not the Linux-based SBCs (Single Board Computers)

Re: Wasm2Mpy: Compiling WASM to MicroPython so it can run on Raspberrys

#27
post #25

Earlier quoted context omitted.

For esp32 I'd recommend https://github.com/wasm3/embedded-wasm-apps It obviously depends on your needs/goals. P.S. I'm also the author of wasm3

Actually in my case I need a VM, because I want users to be able to upload "extension plugins" in real-time, without reuploading firmware In that case which approach / library would you recommend to embedd a WASM VM?

This is exactly what wasm2mpy does for MicroPython: Dynamically loaded, AOT-compiled WASM modules

Re: Wasm2Mpy: Compiling WASM to MicroPython so it can run on Raspberrys

#28
post #24

Earlier quoted context omitted.

For esp32 I'd recommend https://github.com/wasm3/embedded-wasm-apps It obviously depends on your needs/goals. P.S. I'm also the author of wasm3

thanks for your answer!! Can you tell me a bit more about why this suggestion might be more appropriate than the others? Is it in terms of maturity? Speed? Memory footprint? Safety?

Wasm3 happily runs on ESP32, it provides a fully dynamic wasm runtime. But it's much slower than AOT compiled native code, obviously.

Re: Wasm2Mpy: Compiling WASM to MicroPython so it can run on Raspberrys

#29
post #16
post #7

Earlier quoted context omitted.

i think your questions are implicitly answered in the top page of the readme, but by showing rather than telling esp32 and stm32 run three different native assembly instruction sets (though this only supports two of them, the others listed in the first line of the readme are all arm) the coremark results seem like an adequate answer to why you'd use a compiler rather than an interpreter i do think it would be clearer…

There's no scenario where a bespoke WASM interpreter is slower than MicroPython - though this isn't really Python. WASM is an almost "ideal" bytecode target for embedded AOT native compilation as well, though yeah, you'd have to implement all of the backend targets.

maybe if you don't have enough space in flash for both the μpython interpreter and the wasm interpreter? but, yeah, this isn't being compiled to python but to c (for invocation from python)

Re: Wasm2Mpy: Compiling WASM to MicroPython so it can run on Raspberrys

#30
post #5

Can we have python compiled to wasm compiled to python compiled to wasm compiled to python - just for obfuscation reasons?

Jokes aside, when I first saw this I also assumed that WASM was being transpiled into a subset of micropython as some sort of homage to WASM's asm.js roots. The explanations in this thread make much more sense.
Post reply on HN