Skip to content

Javascript Port #3575

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed

Conversation

flowergrass
Copy link
Contributor

MicroPython transmuted into Javascript by Emscripten.

@talljosh
Copy link
Contributor

When I tried to build from this branch, clang kept getting confused and trying to use different versions of header files installed on my system. To get around this I had to make this change to the branch: talljosh@b5d894d

@talljosh
Copy link
Contributor

After an apparently successful build, I get this error when attempting to run it:

$ node build/micropython.js
./build/micropython.js:110
      throw ex;
      ^

TypeError: Cannot read property 'arguments' of undefined
    at Object.cwrap (micropython-flowergrass/ports/javascript/build/micropython.js:550:40)
    at Object.<anonymous> (micropython-flowergrass/ports/javascript/build/micropython.js:102118:26)
    at Module._compile (module.js:434:26)
    at Object.Module._extensions..js (module.js:452:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Function.Module.runMain (module.js:475:10)
    at startup (node.js:117:18)
    at node.js:951:3

@flowergrass
Copy link
Contributor Author

flowergrass commented Feb 22, 2018

Now that you mention it I've never tried building the port on another computer. I'll give it a crack when I'm free.

@flowergrass
Copy link
Contributor Author

@talljosh I just successfully built and ran the program on macOS High Sierra without problems. The original work was completed on an Arch Linux system.
Have you tried building using the emsdk found at http://kripken.github.io/emscripten-site/docs/getting_started/downloads.html?
Other installation methods can get dicey. If it still fails could we get some information on your build environment?

@talljosh
Copy link
Contributor

I was building on Ubuntu 16.04 LTS using emsdk-portable from the page you linked. The issue seemed to be that (emsdk's) clang was looking at my system header files ahead of the emsdk header files. The patch I linked fixed this issue.

I'm travelling at the moment and not on a machine where I can test this, but I can run it again next week when I'm home, and copy and paste the exact output for you.

@mgenti
Copy link

mgenti commented Mar 1, 2018

I also had to use the change @talljosh made to get it to compile using the latest trzeci/emscripten docker image.

@talljosh
Copy link
Contributor

talljosh commented Mar 9, 2018

This is what I see when I try to build from this branch on Ubuntu 16.04 using the latest emsdk-portable without my change.

josh@strider:~/ext/repo/micropython-flowergrass/ports/javascript$ make clean V=1
rm -rf build 

josh@strider:~/ext/repo/micropython-flowergrass/ports/javascript$ make V=1
mkdir -p build/genhdr
python ../../py/makeversionhdr.py build/genhdr/mpversion.h
Generating build/genhdr/mpversion.h
GEN build/genhdr/qstr.i.last
clang -E -DNO_QSTR -DN_X64 -DN_X86 -DN_THUMB -DN_ARM -DN_XTENSA -Ibuild/tmp -m32 -I. -I../.. -Ibuild -Wall -Werror -std=c99  -O0 -DNDEBUG -fdata-sections -ffunction-sections ../../py/mpstate.c ../../py/malloc.c ../../py/gc.c ../../py/pystack.c ../../py/qstr.c ../../py/vstr.c ../../py/mpprint.c ../../py/unicode.c ../../py/mpz.c ../../py/reader.c ../../py/lexer.c ../../py/parse.c ../../py/scope.c ../../py/compile.c ../../py/emitcommon.c ../../py/emitbc.c ../../py/asmbase.c ../../py/asmx64.c ../../py/asmx86.c ../../py/asmthumb.c ../../py/emitinlinethumb.c ../../py/asmarm.c ../../py/asmxtensa.c ../../py/emitinlinextensa.c ../../py/formatfloat.c ../../py/parsenumbase.c ../../py/parsenum.c ../../py/emitglue.c ../../py/persistentcode.c ../../py/runtime.c ../../py/runtime_utils.c ../../py/scheduler.c ../../py/nativeglue.c ../../py/stackctrl.c ../../py/argcheck.c ../../py/warning.c ../../py/map.c ../../py/obj.c ../../py/objarray.c ../../py/objattrtuple.c ../../py/objbool.c ../../py/objboundmeth.c ../../py/objcell.c ../../py/objclosure.c ../../py/objcomplex.c ../../py/objdict.c ../../py/objenumerate.c ../../py/objexcept.c ../../py/objfilter.c ../../py/objfloat.c ../../py/objfun.c ../../py/objgenerator.c ../../py/objgetitemiter.c ../../py/objint.c ../../py/objint_longlong.c ../../py/objint_mpz.c ../../py/objlist.c ../../py/objmap.c ../../py/objmodule.c ../../py/objobject.c ../../py/objpolyiter.c ../../py/objproperty.c ../../py/objnone.c ../../py/objnamedtuple.c ../../py/objrange.c ../../py/objreversed.c ../../py/objset.c ../../py/objsingleton.c ../../py/objslice.c ../../py/objstr.c ../../py/objstrunicode.c ../../py/objstringio.c ../../py/objtuple.c ../../py/objtype.c ../../py/objzip.c ../../py/opmethods.c ../../py/sequence.c ../../py/stream.c ../../py/binary.c ../../py/builtinimport.c ../../py/builtinevex.c ../../py/builtinhelp.c ../../py/modarray.c ../../py/modbuiltins.c ../../py/modcollections.c ../../py/modgc.c ../../py/modio.c ../../py/modmath.c ../../py/modcmath.c ../../py/modmicropython.c ../../py/modstruct.c ../../py/modsys.c ../../py/moduerrno.c ../../py/modthread.c ../../py/vm.c ../../py/bc.c ../../py/showbc.c ../../py/repl.c ../../py/smallint.c ../../py/frozenmod.c ../../py/../extmod/moductypes.c ../../py/../extmod/modujson.c ../../py/../extmod/modure.c ../../py/../extmod/moduzlib.c ../../py/../extmod/moduheapq.c ../../py/../extmod/modutimeq.c ../../py/../extmod/moduhashlib.c ../../py/../extmod/modubinascii.c ../../py/../extmod/virtpin.c ../../py/../extmod/machine_mem.c ../../py/../extmod/machine_pinbase.c ../../py/../extmod/machine_signal.c ../../py/../extmod/machine_pulse.c ../../py/../extmod/machine_i2c.c ../../py/../extmod/machine_spi.c ../../py/../extmod/modussl_axtls.c ../../py/../extmod/modussl_mbedtls.c ../../py/../extmod/modurandom.c ../../py/../extmod/moduselect.c ../../py/../extmod/modwebsocket.c ../../py/../extmod/modwebrepl.c ../../py/../extmod/modframebuf.c ../../py/../extmod/vfs.c ../../py/../extmod/vfs_reader.c ../../py/../extmod/vfs_fat.c ../../py/../extmod/vfs_fat_diskio.c ../../py/../extmod/vfs_fat_file.c ../../py/../extmod/vfs_fat_misc.c ../../py/../extmod/utime_mphal.c ../../py/../extmod/uos_dupterm.c ../../py/../lib/embed/abort_.c ../../py/../lib/utils/printf.c ../../py/emitnative.c main.c mphalport.c modutime.c >build/genhdr/qstr.i.last;
In file included from ../../py/mpstate.c:27:
In file included from ../../py/mpstate.h:31:
In file included from ../../py/mpconfig.h:45:
In file included from ./mpconfigport.h:204:
/usr/include/alloca.h:24:10: fatal error: 'stddef.h' file not found
#include <stddef.h>
         ^~~~~~~~~~
1 error generated.
../../py/mkrules.mk:73: recipe for target 'build/genhdr/qstr.i.last' failed
make: *** [build/genhdr/qstr.i.last] Error 1
make: *** Deleting file 'build/genhdr/qstr.i.last'
josh@strider:~/ext/repo/micropython-flowergrass/ports/javascript$ echo $?
2

INC += -I$(TOP)
INC += -I$(BUILD)

CPP = clang -E
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question: why not emcc -E ?

@flowergrass
Copy link
Contributor Author

@talljosh I'm now getting similar error [stdbool.h not found] which is fixed by your patch. Maybe it's caused by an update?
However the generated micropython.js seems to be functional. Is that the case for you too @mgenti?
It seems like I'll have to look into this properly (when I'm on the clock XD)

@talljosh
Copy link
Contributor

@flowergrass If the generated build's working for you I'd put it down to something wrong with my Node setup. I don't regularly use Node, and I didn't check to make sure I wasn't running some outdated Node installation. If it works for someone else, feel free to ignore my report on that.

But the build issues will need fixing.

@mgenti
Copy link

mgenti commented Mar 14, 2018

@flowergrass Yes, the micropython.js file appeared to be functional. I can't say that I tested it much more than making sure the REPL came up and I could print hello world.

@jedie
Copy link
Contributor

jedie commented Apr 13, 2018

I have some questions:

Is there somewhere a demo page?
How big are the files to download on a micropython.js page?!? (Because the biggest Problem with pypyjs is the large download size. e.g.: pypyjs/pypyjs#196 )

void mp_hal_delay_ms(mp_uint_t ms) {
uint32_t start = mp_hal_ticks_ms();
while (mp_hal_ticks_ms() - start < ms) {
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a busy wait, correct?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is correct. Javascript in most (all?) web browsers does not implement sleep. There are a couple solutions. The simplest is to busy-wait, at the expense of CPU time. If the code is in a web worker, you can launch a synchronous get request against something that will never return a result, with a timeout. Strictly speaking, you can do the same in the main thread too, but that behaviour is deprecated and may not work everywhere forever. Then again, sleeping in the main thread is frowned upon anyway.

The most proper solution is to set a timer to resume, then suspend the VM. Javascript closures make it relatively simple to do, but the rest of the code needs to support it via CPS or similar.

@jedie
Copy link
Contributor

jedie commented Sep 28, 2018

How big are the files to download on a micropython.js page?!?

I just found this old entry: https://stackoverflow.com/a/47739532 and there is:

113 KB micropython.js
240 KB micropython.wasm

That's small, isn't it?

Any news here?

@dpgeorge
Copy link
Member

This was merged in 7d675f3. Sorry it took so long.

@dpgeorge dpgeorge closed this Mar 13, 2019
@dpgeorge
Copy link
Member

Is there somewhere a demo page?

It would be good to make a demo page. If someone wants to attempt it it can be hosted at micropython.org.

@rafi16jan
Copy link

rafi16jan commented May 14, 2019

Is there somewhere a demo page?

It would be good to make a demo page. If someone wants to attempt it it can be hosted at micropython.org.

I make an attempt to integrate this port to CommonJS (Node.JS module system) and Webpack. Feel free to give a feedback/PR https://www.npmjs.com/package/micropython

@dpgeorge
Copy link
Member

@rafi16jan you may reach more people if you post about that at https://forum.micropython.org

@amirgon
Copy link
Contributor

amirgon commented May 14, 2019

Is there somewhere a demo page?

@jedie not anything formal, but @embeddedt created a simple proof-of-concept for Micropython (+LittlevGL binding) on https://embeddedt.github.io/lv_micropython/ports/javascript/lvgl.html

@embeddedt
Copy link

It's still a work in progress. The REPL could use some work (i.e. autocompletion) and it does seem a bit slow compared to pure LittlevGL in the browser.

Eventually we're hoping this can be used for prototyping Python GUI applications using LittlevGL.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.