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

Javascript Port #3575

Open
wants to merge 2 commits into
base: master
from

Conversation

Projects
None yet
7 participants
@flowergrass
Contributor

flowergrass commented Jan 19, 2018

MicroPython transmuted into Javascript by Emscripten.

@talljosh

This comment has been minimized.

Contributor

talljosh commented Feb 22, 2018

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

This comment has been minimized.

Contributor

talljosh commented Feb 22, 2018

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

This comment has been minimized.

Contributor

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

This comment has been minimized.

Contributor

flowergrass commented Feb 26, 2018

@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

This comment has been minimized.

Contributor

talljosh commented Feb 27, 2018

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

This comment has been minimized.

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

This comment has been minimized.

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

This comment has been minimized.

@kkimdev

kkimdev Mar 9, 2018

Question: why not emcc -E ?

@flowergrass

This comment has been minimized.

Contributor

flowergrass commented Mar 11, 2018

@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

This comment has been minimized.

Contributor

talljosh commented Mar 12, 2018

@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

This comment has been minimized.

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

This comment has been minimized.

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) {
}

This comment has been minimized.

@alanjds

alanjds May 3, 2018

This is a busy wait, correct?

This comment has been minimized.

@perkinslr

perkinslr Oct 28, 2018

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

This comment has been minimized.

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment