Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

> 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?



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


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.


.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.


Oh, I didn't know MicroPython was fully AOT. I'm surprised, since vanilla Python is typically interpreted with maybe a bytecode cache, right?


It isn’t fully AOT, but support for AOT modules is excellent.


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...




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: