https://github.com/vshymanskyy/wasm2mpy Skip to content Navigation Menu Toggle navigation Sign in * Product + Actions Automate any workflow + Packages Host and manage packages + Security Find and fix vulnerabilities + Codespaces Instant dev environments + GitHub Copilot Write better code with AI + Code review Manage code changes + Issues Plan and track work + Discussions Collaborate outside of code Explore + All features + Documentation + GitHub Skills + Blog * Solutions By size + Enterprise + Teams + Startups By industry + Healthcare + Financial services + Manufacturing By use case + CI/CD & Automation + DevOps + DevSecOps * Resources Topics + AI + DevOps + Security + Software Development + View all Explore + Learning Pathways + White papers, Ebooks, Webinars + Customer Stories + Partners * Open Source + GitHub Sponsors Fund open source developers + The ReadME Project GitHub community articles Repositories + Topics + Trending + Collections * Enterprise + Enterprise platform AI-powered developer platform Available add-ons + Advanced Security Enterprise-grade security features + GitHub Copilot Enterprise-grade AI features + Premium Support Enterprise-grade 24/7 support * Pricing Search or jump to... Search code, repositories, users, issues, pull requests... Search [ ] Clear Search syntax tips Provide feedback We read every piece of feedback, and take your input very seriously. [ ] [ ] Include my email address so I can be contacted Cancel Submit feedback Saved searches Use saved searches to filter your results more quickly Name [ ] Query [ ] To see all available qualifiers, see our documentation. Cancel Create saved search Sign in Sign up Reseting focus You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session. Dismiss alert {{ message }} vshymanskyy / wasm2mpy Public * Notifications You must be signed in to change notification settings * Fork 1 * Star 37 Compile WebAssembly to native MicroPython modules License MIT license 37 stars 1 fork Branches Tags Activity Star Notifications You must be signed in to change notification settings * Code * Issues 0 * Pull requests 0 * Actions * Security * Insights Additional navigation options * Code * Issues * Pull requests * Actions * Security * Insights vshymanskyy/wasm2mpy This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository. main BranchesTags Go to file Code Folders and files Name Name Last commit Last commit message date Latest commit History 114 Commits .github/workflows .github/ workflows runtime runtime test test .gitignore .gitignore LICENSE LICENSE Makefile Makefile README.md README.md buildall.py buildall.py logo.png logo.png run-tests.sh run-tests.sh View all files Repository files navigation * README * MIT license [logo] wasm2mpy StandWithUkraine Build status GitHub license Support vshymanskyy Compile WebAssembly binary into a MicroPython module and load it dynamically on ESP32, Raspberry Pi Pico, STM32, nRF52 etc. Status App \ Target x86/x64 armv6m armv7m/+s/+d esp8266^1 esp32 rv32imc TypeScript^2 [?][?]^3 C++ Rust [?][?]^3 TinyGo [?][?]^3 [?] Zig [?][?]^3 Virgil [?] WAT Coremark builds and runs OK builds OK, doesn't run work in progress CoreMark results * STM32F405 168MHz: 233.918 * ESP32 240MHz: 228.363 * ESP32-S3 240MHz: 271.573 * iMXRT1062 600MHz: 1911.437 * i5-8250U 1.6GHz: 18696.248 Compile Important This is a Proof-of-Concept, not optimized or ready for actual use. You'll need: * Python 3 + pip install --upgrade pyelftools ar * wasm2c from WABT * Latest MicroPython source code * Target architecture toolchain Set up the environment and build the .mpy module from .wasm: export MPY_DIR=/path/to/micropython export PATH=/opt/wabt/bin:$PATH export PATH=/opt/xtensa-lx106-elf/bin:$PATH export PATH=/opt/xtensa-esp32-elf/bin:$PATH pip install -U pyelftools make ARCH=xtensawin APP=zig # x86, x64, armv6m, armv7m, armv7emsp, armv7emdp, xtensa, xtensawin Output: W2C test/zig.wasm GEN build/zig.config.h CC runtime/runtime.c CC runtime/wasm-rt-mem-impl.c CC runtime/wasm-rt-impl.c CC .wasm/wasm.c LINK build/runtime/runtime.o arch: EM_XTENSA text size: 3524 rodata size: 850 bss size: 144 GOT entries: 57 GEN zig.mpy Upload and Run mpremote cp zig.mpy :lib/ mpremote exec "import zig; zig.setup()" [?] Zig is running! Run any exported function Note This requires adding some glue code to the runtime. Glue code can be auto-generated, but for now it's a manual process. For example, test/simple.wasm just adds 2 numbers: (module (func (export "add") (param i32 i32) (result i32) (i32.add (local.get 0) (local.get 1)) ) ) MicroPython v1.24.0-preview.224.g6c3dc0c0b on 2024-08-22; Raspberry Pi Pico W with RP2040 Type "help()" for more information. >>> import simple >>> simple.add(3, 4) 7 >>> simple.add(10, 6) 16 Access WASM module memory >>> import cpp >>> cpp.setup() C++ is running! >>> cpp._memory[4096:4096+32] bytearray(b' Blink\x00\xf0\x9f\xa4\xa9 C++ is running!\x00\n\x00\x00\x00') >>> new_data = b"Hello C++ world" >>> cpp._memory[4096+12:4096+12+len(new_data)] = new_data >>> cpp.setup() Hello C++ world How It Works? The idea is very similar to embedded-wasm-apps: image TODO * [*] Support exports + [ ] Auto-generate exports bindings * [*] Support imports * [*] Support memory * [*] Support .a inputs for mpy_ld * [ ] XIP for native modules * [ ] TBD: Support globals * [ ] Add RISC-V support: micropython/micropython#15603 * [ ] Optimize codegen + [ ] Use u32 instead of u64 for mem addresses + [ ] Use a directly addressable .bss section as memory (skip indirection) * [ ] Implement WASM Custom Page Sizes * [ ] Implement WASM Exceptions * [ ] Implement WASM Stack Switching Further reading * Discussion on wasm2mpy * wasm2c, w2c2 * MicroPython Native Modules * Feasibility of WASM for MicroPython Footnotes 1. esp8266 requires the use of esp.set_native_code_location, and setting WASM_PAGE_SIZE to 8192 (or need to wait for WASM Custom Page Sizes) - 2. AssemblyScript - 3. not enough memory to run, need to wait for WASM Custom Page Sizes - -^2 -^3 -^4 About Compile WebAssembly to native MicroPython modules Topics raspberry-pi micropython esp32 webassembly wasm armv7 nrf52 webassembly-runtime rp2040 rp2350 Resources Readme License MIT license Activity Stars 37 stars Watchers 4 watching Forks 1 fork Report repository Languages * C 61.5% * Assembly 21.2% * C++ 11.8% * Python 2.7% * Makefile 1.6% * Shell 1.2% Footer (c) 2024 GitHub, Inc. Footer navigation * Terms * Privacy * Security * Status * Docs * Contact * Manage cookies * Do not share my personal information You can't perform that action at this time.