News for hackers. Stuff that matters.
Re:Wasm2Mpy: Compiling WASM to MicroPython so it can run on ... by bragr on Tuesday September 24, 2024 @03:56PM (#41640257)
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.
Re:Wasm2Mpy: Compiling WASM to MicroPython so it can run on ... by ronsor on Tuesday September 24, 2024 @04:18PM (#41640470)
Probably at least one of "it's cool" or "I'm already using MicroPython"
Re:Wasm2Mpy: Compiling WASM to MicroPython so it can run on ... by kragen on Tuesday September 24, 2024 @05:22PM (#41641077)
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, though, to explain up front that the module being built is not made of python code, but rather callable from python code

Re:Wasm2Mpy: Compiling WASM to MicroPython so it can run on ... by kristianp on Tuesday September 24, 2024 @05:51PM (#41641330)
> i do think it would be clearer, though, to explain up front that the module being built is not made of python code, but rather callable from python code

I know nothing about micropython. Do the modules contain bytecode?

Re:Wasm2Mpy: Compiling WASM to MicroPython so it can run on ... by jononor on Tuesday September 24, 2024 @06:05PM (#41641462)
.mpy modules can contain MicroPython bytecode and/or native machine code. In this case, WASM is compiled (via C) to native code. So the performance is very good, much better than interpreting either MicroPython bytecode or WASM bytecode.

The conventional way of creating native modules for MicroPython is to write them in C. This work allows to use any language that supports WASM output target.

Re:Wasm2Mpy: Compiling WASM to MicroPython so it can run on ... by 01HNNWZ0MV43FF on Tuesday September 24, 2024 @06:47PM (#41641794)
Oh, I didn't know MicroPython was fully AOT. I'm surprised, since vanilla Python is typically interpreted with maybe a bytecode cache, right?
Re:Wasm2Mpy: Compiling WASM to MicroPython so it can run on ... by dbcurtis on Tuesday September 24, 2024 @08:32PM (#41642442)
It isn’t fully AOT, but support for AOT modules is excellent.
Re:Wasm2Mpy: Compiling WASM to MicroPython so it can run on ... by zorgmonkey on Wednesday September 25, 2024 @01:35AM (#41643994)
Thanks for the explanation this project makes so much more sense now. If people want a little more detail, they have a brief how it works section in readme https://github.com/vshymanskyy/wasm2mpy?tab=readme-ov-file#h...
Re:Wasm2Mpy: Compiling WASM to MicroPython so it can run on ... by HALtheWise on Tuesday September 24, 2024 @06:01PM (#41641434)
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

Re:Wasm2Mpy: Compiling WASM to MicroPython so it can run on ... by westurner on Tuesday September 24, 2024 @09:39PM (#41642816)
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?

Re:Wasm2Mpy: Compiling WASM to MicroPython so it can run on ... by vshymanskyy on Wednesday September 25, 2024 @02:04AM (#41644152)
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 ... by westurner on Thursday September 26, 2024 @01:51PM (#41661198)
If they're both Turing-complete opcodes instruction sets, Church-Turing says it is possible with just .pyc, too.

But that doesn't solve for WASM on a MicroPython device.

And that is why smart contracts without costed opcodes could not be built on Python.

Re:Wasm2Mpy: Compiling WASM to MicroPython so it can run on ... by 15155 on Wednesday September 25, 2024 @12:02AM (#41643561)
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.

Re:Wasm2Mpy: Compiling WASM to MicroPython so it can run on ... by kragen on Wednesday September 25, 2024 @07:15AM (#41646043)
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 ... by vshymanskyy on Wednesday September 25, 2024 @02:00AM (#41644129)
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).
Re:Wasm2Mpy: Compiling WASM to MicroPython so it can run on ... by 0points on Wednesday September 25, 2024 @04:24AM (#41645075)
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 ... by vshymanskyy on Wednesday September 25, 2024 @06:26AM (#41645764)
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 ... by ssnover on Wednesday September 25, 2024 @09:21AM (#41647219)
Curious if you've seen the WAMR runtime which is explicitly designed to be lightweight enough for freestanding embedded systems on some higher end microcontrollers (Cortex-M4F).
Re:Wasm2Mpy: Compiling WASM to MicroPython so it can run on ... by vshymanskyy on Wednesday September 25, 2024 @10:08AM (#41647725)
Yes, Wasm3 also targets Cortex-M4 and even Cortex-M0 :)
Re:Wasm2Mpy: Compiling WASM to MicroPython so it can run on ... by 0points on Thursday September 26, 2024 @02:09AM (#41655048)
I see, thanks for the clarification!
Re:Wasm2Mpy: Compiling WASM to MicroPython so it can run on ... by szundi on Tuesday September 24, 2024 @05:02PM (#41640863)
Can we have python compiled to wasm compiled to python compiled to wasm compiled to python - just for obfuscation reasons?
Re:Wasm2Mpy: Compiling WASM to MicroPython so it can run on ... by cyanydeez on Tuesday September 24, 2024 @05:07PM (#41640905)
Best add a LLM into the loop for real speed.
Re:Wasm2Mpy: Compiling WASM to MicroPython so it can run on ... by vshymanskyy on Wednesday September 25, 2024 @01:55AM (#41644106)
With this approach, wasm is compiled to native code. Translating that to Python is in theory possible, but very impractical.
Re:Wasm2Mpy: Compiling WASM to MicroPython so it can run on ... by boomskats on Wednesday September 25, 2024 @07:18AM (#41646065)
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.
Re:Wasm2Mpy: Compiling WASM to MicroPython so it can run on ... by oulipo on Tuesday September 24, 2024 @06:47PM (#41641787)
Re:Wasm2Mpy: Compiling WASM to MicroPython so it can run on ... by vshymanskyy on Wednesday September 25, 2024 @02:06AM (#41644161)
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 ... by oulipo on Wednesday September 25, 2024 @04:34AM (#41645154)
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 ... by vshymanskyy on Wednesday September 25, 2024 @06:30AM (#41645800)
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 ... by oulipo on Wednesday September 25, 2024 @04:49AM (#41645252)
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 ... by vshymanskyy on Wednesday September 25, 2024 @06:27AM (#41645776)
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 ... by oulipo on Wednesday September 25, 2024 @07:36PM (#41652991)
Okay great! But I'm not using MicroPython, just compiling directly for the ESP32, would that mean that to use wasm2mpy I would need to add a MicroPython VM in my code?
Re:Wasm2Mpy: Compiling WASM to MicroPython so it can run on ... by oulipo on Wednesday September 25, 2024 @07:38PM (#41653006)
would you say that using wasm2mpy is better suited for my needs than having a wasm runtime like the one of bytecodealliance? What would be the advantage in this case?
Re:Wasm2Mpy: Compiling WASM to MicroPython so it can run on ... by vshymanskyy on Thursday September 26, 2024 @07:16AM (#41657026)
Yes, for these microcontroller-type chips, ahead-of-time compilation would be definitely more suitable. WAMR probably supports it, but you can also to it via WASM2C, easily. I prefer not depending on WAMR, it is very bloated
Re:Wasm2Mpy: Compiling WASM to MicroPython so it can run on ... by oulipo on Friday September 27, 2024 @09:16AM (#41670062)
thanks for all those answers! Slava Ukraini
Re:Wasm2Mpy: Compiling WASM to MicroPython so it can run on ... by vshymanskyy on Sunday September 29, 2024 @06:05AM (#41686239)
Geroyam Slava!
Re:Wasm2Mpy: Compiling WASM to MicroPython so it can run on ... by tony-allan on Tuesday September 24, 2024 @07:26PM (#41642073)
A great way to allow code created in another language to be compiled to a MicroPython module and then run in a MicroPython environment.

These .mpy files are dynamically loaded into MicroPython using a regular python import.

Re:Wasm2Mpy: Compiling WASM to MicroPython so it can run on ... by vshymanskyy on Wednesday September 25, 2024 @12:51AM (#41643802)
Author here, ready to answer any questions.
Re:Wasm2Mpy: Compiling WASM to MicroPython so it can run on ... by jonathrg on Wednesday September 25, 2024 @04:33PM (#41651572)
Like several others I'm confused about the tagline for this. If I understand correctly, it's not compiling "WASM to micropython", but to native binary code with generated micropython bindings?
Re:Wasm2Mpy: Compiling WASM to MicroPython so it can run on ... by vshymanskyy on Wednesday September 25, 2024 @04:48PM (#41651710)
Correct. I didn't create the tagline, sorry