Coder Social home page Coder Social logo

pmp-p / micropython-ports-wasm Goto Github PK

View Code? Open in Web Editor NEW
37.0 13.0 1.0 6.73 MB

*experimental* wasm micropython port *not a fork* for Pythons Everywhere

Home Page: http://pmp-p.github.io/layout.html

Makefile 1.27% C 65.09% Python 22.76% JavaScript 3.74% HTML 4.17% C++ 0.40% Shell 0.66% COBOL 1.91%
micropython wasm emscripten wip-do-not-use

micropython-ports-wasm's Introduction

MicroPython and Web Assembly (wasm)

NEWS:

Gitter

10 August 2020 : now WaPy can load as an es6 module, it also feature step-debugging for synchronous code.

3 June 2020 : https://github.com/pmp-p/wapy/tree/wapy-wipdonotuse is quite stable and can run 2x to 5x slower than cpython native

5x slower means full preemption like micropython.schedule can do on MCU except there it returns to js coroutines.

so time.sleep, open(file & sockets), import are going to be implemented with "aio" functions (POSIX.1-2001, POSIX.1-2008)

the ffi/ctypes support seems enough to run some PySDL2 samples ( ctypes wrapper for CPython and SDL2 )

demo: https://pmp-p.github.io/wapy/minide/

to build : apply https://patch-diff.githubusercontent.com/raw/pmp-p/wapy/pull/3.diff on a micropython-master tree named build-no_nlr

Neither micropython/micropython or pfalcon/pycopy seem actually attracted to have a bare metal preemptible VM :

micropython/micropython#4131 (comment)

pfalcon/pycopy#18

I don't see much point to bother integrate in upstream "ports" folder : so future of wapy could well be wasi-header-only or an arduino lib tied to Wasm3 which should allow to write wapy drivers directly with bare-metal C or arduino.

1 May 2020 : Project lives again, but is not compatible with either micropython or pycopy because of adoption of a no nlr, registers and a heavily customized vm.c i'll se how to integrate again when time comes. Meanwhile i'll try to maintain prebuilt and testsuite here https://github.com/pmp-p/wapy

20 august 2019 : blocked again https://bugs.llvm.org/show_bug.cgi?id=43060

16 august 2019 : retrying stackless with clang CI intregration this is using emsdk tot-upstream (tip of tree) not latest-upstream (yet)

10 august 2019 : currently paused because reached somehow limit of emscripten compiler by abusing jump tables meanwhile trying new upstream clang with micropython no nlr branch which is better for stackless https://github.com/pmp-p/micropython/tree/wasm-nonlr/ports/wasm-no-nlr

old working versions always stored in branches.

What ?

wasm is a virtual machine for internet browsers which run bytecode closer to native speeds see https://webassembly.org/

but we want to run python virtual machine and its own bytecode ;) so thanks to micropython we'll be able to by compiling micropython core to wasm bytecode first.

Requirements

  • Linux OS with gcc/clang, a decent build environnement and libltdl-dev

  • have python3 in your path 3.6 should do it but 3.7 / 3.8 are safer.

  • Follow the instructions for getting started with Emscripten here.

Getting started ( wapy, only basic js skill required )

Get the content of https://github.com/pmp-p/pmp-p.github.io/tree/master/wapy.es6 and hack it to your liking.

Getting started (this port)

( does not apply to wapy, involves compilation )

Beware this is not a micropython fork : it's a port folder to add support to official micropython for a new "machine"

You first need to get easy building your own official micropython and its javascript port

Follow the instructions for getting started with micropython unix build

https://github.com/micropython/micropython/

to check if your emscripten build works ( facultative, FYI last test on emscripten 1.38.31 was ok )

https://github.com/micropython/micropython/tree/master/ports/javascript

but i suggest using emmake make -C ports/javascript PYTHON=python2 instead of just make

What are the differences between this repo and the official javascript port?

see #4

Dive in !

now you're ready to build the port, you can run

# micropython-ports-wasm will go in micropython/ports/wasm
# as it's not a fork but a drop in target port we need to checkout a full microPython

# get the micropython core.

# official ( should always work ! )
git clone --recursive --recurse-submodules https://github.com/micropython/micropython
cd micropython


# or lvgl enabled ( wip could not work )
# git clone --recursive --recurse-submodules https://github.com/littlevgl/lv_micropython.git
# cd lv_micropython
# git checkout lvgl_javascript
# git submodule update --recursive --init
# if you want to use asyncio with lvgl just add the patch https://patch-diff.githubusercontent.com/raw/littlevgl/lv_binding_micropython/pull/30.diff
# that allows you to service with SDL.run_once()

#build host tools
make -C mpy-cross
make -C ports/unix

#add the target port
cd micropython/ports
git clone https://github.com/pmp-p/micropython-ports-wasm.git wasm


cd wasm

#transpile the mixed python/C module to pure C
#use a python3 version with annotations support !
# . modgen.sh

. /path/to/emsdk/emsdk_set_env.sh

# for LVGL support use "./rebuild.sh LVGL=1" instead
# test will be run automatically.
# emmake make USER_C_MODULES=cmod && . runtest.sh

# modgen and make have been merged in rebuild-upsteam.sh script
chmod +x *sh

# avoid using ./rebuild-fastcom.sh
# only there for testing clang 8-10 and binaryen discrepancy

./rebuild-upstream.sh

# fast rebuild
# ./rebuild-upstream.sh fast=1

# using asyncfy (bad perf even on upstream) avoid fast=1 when changing
# mode
# ./rebuild-upstream.sh ASYNCIFY=1

now you can navigate http://127.0.0.1:8000/index.html

to edit code samples look in micropython/*.html

Usage

copy the 4 files located in micropython folder found inside the build folder pythons.js micropython.js micropython.data micropython.wasm and drop them where your main let's say myupython.html file will be

now you have 3 options to run code :

  1. from the web via arguments ( sys.argv ) with a call myupython.html?full_url_to_the_script.py

  2. from a <script type="text/µpython> tag

  3. interactively from repl with xterm.js

for 1&2 you will need to provide a javascript term_impl(text) function that output stdout stream where you want it to go

Preview1

History:

codebase https://github.com/matthewelse/micropython

Micropython webassembly target micropython/micropython#3313

Support for Emscripten micropython/micropython#2618

RFC: emscripten: Got something to compile and link. micropython/micropython#1561

Javascript Port - MicroPython transmuted into Javascript by Emscripten. micropython/micropython#3575

Emscripten

micropython/micropython#888

MicroPython and emscripten

https://www.bountysource.com/issues/5037165-emscripten

micropython/micropython#3474

https://github.com/kkimdev/epsilon

Experimental Features (current or future):

Explicit exceptions : https://github.com/dpgeorge/micropython/tree/py-remove-nlr

Using requestIdleCallback : https://developers.google.com/web/updates/2015/08/using-requestidlecallback

multi core interpreters : https://github.com/ericsnowcurrently/multi-core-python/wiki

Multiplexed rpc : https://brionv.com/log/2019/05/10/wasm-rpc-thoughts/

A C api for pythons ? https://github.com/pyhandle/hpy

native code compiler ? https://github.com/windelbouwman/ppci-mirror

integrated gui https://github.com/littlevgl/lv_binding_micropython

various shared/distributed memory objects related subjects. https://github.com/apache/arrow https://capnproto.org/ http://doc.pypy.org/en/latest/stm.html#python-3-cpython-and-others

mesh network https://github.com/chr15m/bugout https://chr15m.github.io/bep44-for-decentralized-applications.html

goal ( far far away ):

This is a research project aimed toward a cooperative multitasking multicore/multinode python solution. Though still keeping some level of preemption over coroutines at byte code level for soft-rt purpose.

testing on browser+wasm makes it easier than baremetal.

meet me on #microPython or #micropython-fr on freenode

https://kiwi.freenode.net/?nick=upy-wasm-guest&channel=#micropython-fr

micropython-ports-wasm's People

Contributors

pmp-p avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Forkers

alanjds

micropython-ports-wasm's Issues

REPL

Hi,
Nice work!
Any chance for an online REPL, just to get the feel of it?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.