Skip to content

Commit 00dcc14

Browse files
committed
Add Emscripten port for version 6.0
1 parent a763f4f commit 00dcc14

File tree

257 files changed

+47254
-3
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

257 files changed

+47254
-3
lines changed

lib/lv_bindings

ports/javascript/JSBackend.patch

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
--- JSBackend.cpp 2018-01-10 16:35:07.331418145 +1100
2+
+++ JSBackend_mp_js.cpp 2018-01-10 16:40:04.804633134 +1100
3+
@@ -4280,6 +4280,7 @@
4+
5+
void JSWriter::calculateNativizedVars(const Function *F) {
6+
NativizedVars.clear();
7+
+ return;
8+
9+
for (Function::const_iterator I = F->begin(), BE = F->end(); I != BE; ++I) {
10+
auto BI = &*I;

ports/javascript/Makefile

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
include ../../py/mkenv.mk
2+
3+
CROSS = 0
4+
5+
QSTR_DEFS = qstrdefsport.h
6+
7+
include $(TOP)/py/py.mk
8+
9+
CC = emcc -g4 $(JSFLAGS)
10+
LD = emcc -g4
11+
12+
INC += -I.
13+
INC += -I$(TOP)
14+
INC += -I$(BUILD)
15+
16+
EM_CACHE ?= ~/.emscripten_cache
17+
18+
19+
BOTH_CFLAGS := -s WASM=0
20+
21+
CPP = clang -E
22+
23+
ifdef EMSCRIPTEN
24+
CPP += -isystem $(EMSCRIPTEN)/system/include/libc -isystem $(EMSCRIPTEN)/system/include/libcxx -isystem $(EM_CACHE)/asmjs/ports-builds/sdl2/include -isystem $(EMSCRIPTEN)/system/include
25+
# Act like 'emcc'
26+
CPP += -U__i386 -U__i386 -Ui386 -U__SSE -U__SSE_MATH -U__SSE2 -U__SSE2_MATH -U__MMX__ -U__SSE__ -U__SSE_MATH__ -U__SSE2__ -U__SSE2_MATH__
27+
endif
28+
29+
CFLAGS = -m32 -Wall $(INC) $(COPT)
30+
LDFLAGS = -m32 -Wl,-Map=$@.map,--cref -Wl,--gc-sections
31+
32+
OPTIM := -O2
33+
34+
CFLAGS += $(OPTIM) -DNDEBUG -D__EMSCRIPTEN__ -DLV_CONF_INCLUDE_SIMPLE
35+
CFLAGS += -fdata-sections -ffunction-sections
36+
CFLAGS += $(CFLAGS_MOD)
37+
38+
SRC_LIB = $(addprefix lib/,\
39+
utils/interrupt_char.c \
40+
utils/stdout_helpers.c \
41+
utils/pyexec.c \
42+
mp-readline/readline.c \
43+
)
44+
45+
SRC_C = \
46+
main.c \
47+
mphalport.c \
48+
modutime.c \
49+
$(SRC_MOD)
50+
51+
LIB_SRC_C = $(addprefix lib/,\
52+
lv_bindings/driver/SDL/SDL_monitor.c \
53+
lv_bindings/driver/SDL/SDL_mouse.c \
54+
lv_bindings/driver/SDL/modSDL.c \
55+
$(LIB_SRC_C_EXTRA) \
56+
timeutils/timeutils.c \
57+
)
58+
59+
OBJ =
60+
OBJ = $(PY_O)
61+
OBJ += $(addprefix $(BUILD)/, $(SRC_LIB:.c=.o))
62+
OBJ += $(addprefix $(BUILD)/, $(SRC_C:.c=.o))
63+
OBJ += $(addprefix $(BUILD)/, $(LIB_SRC_C:.c=.o))
64+
65+
# List of sources for qstr extraction
66+
SRC_QSTR += $(SRC_C) $(LIB_SRC_C) dummy_header.h
67+
# Append any auto-generated sources that are needed by sources listed in
68+
# SRC_QSTR
69+
SRC_QSTR_AUTO_DEPS +=
70+
71+
JSFLAGS = $(BOTH_CFLAGS) -s USE_SDL=2 $(OPTIM) -s EXPORTED_FUNCTIONS="['_mp_js_init', '_mp_handle_pending', '_mp_js_init_repl', '_mp_js_do_str', '_mp_js_process_char', '_mp_hal_get_interrupt_char', '_mp_keyboard_interrupt']" -s EXTRA_EXPORTED_RUNTIME_METHODS="['ccall', 'cwrap']" --memory-init-file 0 --js-library library.js
72+
73+
all: $(BUILD)/micropython.js
74+
75+
$(BUILD)/micropython.js: $(OBJ) library.js wrapper.js
76+
$(ECHO) "LINK $(BUILD)/firmware.js"
77+
$(Q)emcc $(LDFLAGS) -o $(BUILD)/firmware.js $(OBJ) $(JSFLAGS)
78+
cat wrapper.js $(BUILD)/firmware.js > $@
79+
80+
min: $(BUILD)/micropython.js
81+
uglifyjs $< -c -o $(BUILD)/micropython.min.js
82+
83+
test: $(BUILD)/micropython.js $(TOP)/tests/run-tests
84+
$(eval DIRNAME=ports/$(notdir $(CURDIR)))
85+
cd $(TOP)/tests && MICROPY_MICROPYTHON=../ports/javascript/node_run.sh ./run-tests
86+
87+
include $(TOP)/py/mkrules.mk

ports/javascript/README.md

+128
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
MicroPython.js
2+
==============
3+
4+
MicroPython transmuted into Javascript by Emscripten.
5+
6+
Dependencies
7+
------------
8+
9+
Building micropython.js bears the same requirements as the standard MicroPython
10+
ports with the addition of Emscripten (and uglify-js for the minified file).
11+
12+
A standard installation of Emscripten should provide functional code, however
13+
if memory errors are encountered it may be worthwhile to modify the tool.
14+
`emscripten-fastcomp/lib/Target/JSBackend.cpp` may require the minor fix
15+
found in JSBackend.patch. This patch attempts to address situations where
16+
C code running through Emscripten is denied access to Javascript variables
17+
leading to false-positives in the MicroPython garbage collector as variables
18+
with pointers exclusively in Javascript will be erased prematurely.
19+
Refer to Emscripten documentation for instructions on building Emscripten
20+
from source.
21+
22+
Build instructions
23+
------------------
24+
25+
In order to build micropython.js, run:
26+
27+
$ make
28+
29+
To generate the minified file micropython.min.js, run:
30+
31+
$ make min
32+
33+
Running with Node.js
34+
--------------------
35+
36+
Access the repl with:
37+
38+
$ node build/micropython.js
39+
40+
Stack size may be modified using:
41+
42+
$ node build/micropython.js -X stack=64K
43+
44+
Where stack size may be represented in Bytes, KiB or MiB.
45+
46+
MicroPython scripts may be executed using:
47+
48+
$ node build/micropython.js hello.py
49+
50+
Alternatively micropython.js may by accessed by other javascript programs in node
51+
using the require command and the general API outlined below. For example:
52+
53+
```javascript
54+
var mp_js = require('./build/micropython.js');
55+
56+
mp_js_init(64 * 1024);
57+
mp_js_do_str("print('hello world')\n");
58+
```
59+
60+
Running with HTML
61+
-----------------
62+
63+
The prerequisite for browser operation of micropython.js is an element with
64+
the id `mp_js_stdout` which receives `print` events. The following code
65+
demonstrates basic functionality:
66+
67+
```html
68+
<!doctype html>
69+
<html>
70+
<head>
71+
<script src="build/micropython.js"></script>
72+
</head>
73+
<body>
74+
<div id='mp_js_stdout'></div>
75+
<script>
76+
mp_js_stdout.addEventListener('print', function(e) {
77+
document.write(e.data);
78+
}, false);
79+
80+
mp_js_init(64 * 1024);
81+
mp_js_do_str('print(\'hello world\')');
82+
</script>
83+
</body>
84+
</html>
85+
```
86+
87+
MicroPython code execution will suspend the browser so be sure to atomize usage
88+
within this environment. Unfortunately interrupts have not been implemented for the
89+
browser.
90+
91+
Testing
92+
-------
93+
94+
Run the test suite using:
95+
96+
$ make test
97+
98+
API
99+
---
100+
101+
The following functions have been exposed to javascript.
102+
103+
```
104+
mp_js_init(stack_size)
105+
```
106+
107+
Initialize MicroPython with the given stack size in bytes. This must be
108+
called before attempting to interact with MicroPython.
109+
110+
```
111+
mp_js_do_str(code)
112+
```
113+
114+
Execute the input code. `code` must be a `string`.
115+
116+
```
117+
mp_js_init_repl()
118+
```
119+
120+
Initialize MicroPython repl. Must be called before entering characters into
121+
the repl.
122+
123+
```
124+
mp_js_process_char(char)
125+
```
126+
127+
Input character into MicroPython repl. `char` must be of type `number`. This
128+
will execute MicroPython code when necessary.

ports/javascript/dummy_header.h

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/* This exists to keep 'emcc' from thinking we are trying to precompile headers. */

ports/javascript/lextab.py

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# lextab.py. This file automatically created by PLY (version 3.10). Don't edit!
2+
_tabversion = '3.10'
3+
_lextokens = set(('VOID', 'LBRACKET', 'WCHAR_CONST', 'FLOAT_CONST', 'MINUS', 'RPAREN', 'LONG', 'PLUS', 'ELLIPSIS', 'GT', 'GOTO', 'ENUM', 'PERIOD', 'GE', 'INT_CONST_DEC', 'ARROW', '__INT128', 'HEX_FLOAT_CONST', 'DOUBLE', 'MINUSEQUAL', 'INT_CONST_OCT', 'TIMESEQUAL', 'OR', 'SHORT', 'RETURN', 'RSHIFTEQUAL', 'RESTRICT', 'STATIC', 'SIZEOF', 'UNSIGNED', 'UNION', 'COLON', 'WSTRING_LITERAL', 'DIVIDE', 'FOR', 'PLUSPLUS', 'EQUALS', 'ELSE', 'INLINE', 'EQ', 'AND', 'TYPEID', 'LBRACE', 'PPHASH', 'INT', 'SIGNED', 'CONTINUE', 'NOT', 'OREQUAL', 'MOD', 'RSHIFT', 'DEFAULT', 'CHAR', 'WHILE', 'DIVEQUAL', 'EXTERN', 'CASE', 'LAND', 'REGISTER', 'MODEQUAL', 'NE', 'SWITCH', 'INT_CONST_HEX', '_COMPLEX', 'PPPRAGMASTR', 'PLUSEQUAL', 'STRUCT', 'CONDOP', 'BREAK', 'VOLATILE', 'PPPRAGMA', 'ANDEQUAL', 'INT_CONST_BIN', 'DO', 'LNOT', 'CONST', 'LOR', 'CHAR_CONST', 'LSHIFT', 'RBRACE', '_BOOL', 'LE', 'SEMI', 'LT', 'COMMA', 'OFFSETOF', 'TYPEDEF', 'XOR', 'AUTO', 'TIMES', 'LPAREN', 'MINUSMINUS', 'ID', 'IF', 'STRING_LITERAL', 'FLOAT', 'XOREQUAL', 'LSHIFTEQUAL', 'RBRACKET'))
4+
_lexreflags = 64
5+
_lexliterals = ''
6+
_lexstateinfo = {'ppline': 'exclusive', 'pppragma': 'exclusive', 'INITIAL': 'inclusive'}
7+
_lexstatere = {'ppline': [('(?P<t_ppline_FILENAME>"([^"\\\\\\n]|(\\\\(([a-zA-Z._~!=&\\^\\-\\\\?\'"])|(\\d+)|(x[0-9a-fA-F]+))))*")|(?P<t_ppline_LINE_NUMBER>(0(([uU]ll)|([uU]LL)|(ll[uU]?)|(LL[uU]?)|([uU][lL])|([lL][uU]?)|[uU])?)|([1-9][0-9]*(([uU]ll)|([uU]LL)|(ll[uU]?)|(LL[uU]?)|([uU][lL])|([lL][uU]?)|[uU])?))|(?P<t_ppline_NEWLINE>\\n)|(?P<t_ppline_PPLINE>line)', [None, ('t_ppline_FILENAME', 'FILENAME'), None, None, None, None, None, None, ('t_ppline_LINE_NUMBER', 'LINE_NUMBER'), None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, ('t_ppline_NEWLINE', 'NEWLINE'), ('t_ppline_PPLINE', 'PPLINE')])], 'pppragma': [('(?P<t_pppragma_NEWLINE>\\n)|(?P<t_pppragma_PPPRAGMA>pragma)|(?P<t_pppragma_STR>.+)', [None, ('t_pppragma_NEWLINE', 'NEWLINE'), ('t_pppragma_PPPRAGMA', 'PPPRAGMA'), ('t_pppragma_STR', 'STR')])], 'INITIAL': [('(?P<t_PPHASH>[ \\t]*\\#)|(?P<t_NEWLINE>\\n+)|(?P<t_LBRACE>\\{)|(?P<t_RBRACE>\\})|(?P<t_FLOAT_CONST>((((([0-9]*\\.[0-9]+)|([0-9]+\\.))([eE][-+]?[0-9]+)?)|([0-9]+([eE][-+]?[0-9]+)))[FfLl]?))|(?P<t_HEX_FLOAT_CONST>(0[xX]([0-9a-fA-F]+|((([0-9a-fA-F]+)?\\.[0-9a-fA-F]+)|([0-9a-fA-F]+\\.)))([pP][+-]?[0-9]+)[FfLl]?))|(?P<t_INT_CONST_HEX>0[xX][0-9a-fA-F]+(([uU]ll)|([uU]LL)|(ll[uU]?)|(LL[uU]?)|([uU][lL])|([lL][uU]?)|[uU])?)', [None, ('t_PPHASH', 'PPHASH'), ('t_NEWLINE', 'NEWLINE'), ('t_LBRACE', 'LBRACE'), ('t_RBRACE', 'RBRACE'), ('t_FLOAT_CONST', 'FLOAT_CONST'), None, None, None, None, None, None, None, None, None, ('t_HEX_FLOAT_CONST', 'HEX_FLOAT_CONST'), None, None, None, None, None, None, None, ('t_INT_CONST_HEX', 'INT_CONST_HEX')]), ('(?P<t_INT_CONST_BIN>0[bB][01]+(([uU]ll)|([uU]LL)|(ll[uU]?)|(LL[uU]?)|([uU][lL])|([lL][uU]?)|[uU])?)|(?P<t_BAD_CONST_OCT>0[0-7]*[89])|(?P<t_INT_CONST_OCT>0[0-7]*(([uU]ll)|([uU]LL)|(ll[uU]?)|(LL[uU]?)|([uU][lL])|([lL][uU]?)|[uU])?)|(?P<t_INT_CONST_DEC>(0(([uU]ll)|([uU]LL)|(ll[uU]?)|(LL[uU]?)|([uU][lL])|([lL][uU]?)|[uU])?)|([1-9][0-9]*(([uU]ll)|([uU]LL)|(ll[uU]?)|(LL[uU]?)|([uU][lL])|([lL][uU]?)|[uU])?))|(?P<t_CHAR_CONST>\'([^\'\\\\\\n]|(\\\\(([a-zA-Z._~!=&\\^\\-\\\\?\'"])|(\\d+)|(x[0-9a-fA-F]+))))\')|(?P<t_WCHAR_CONST>L\'([^\'\\\\\\n]|(\\\\(([a-zA-Z._~!=&\\^\\-\\\\?\'"])|(\\d+)|(x[0-9a-fA-F]+))))\')|(?P<t_UNMATCHED_QUOTE>(\'([^\'\\\\\\n]|(\\\\(([a-zA-Z._~!=&\\^\\-\\\\?\'"])|(\\d+)|(x[0-9a-fA-F]+))))*\\n)|(\'([^\'\\\\\\n]|(\\\\(([a-zA-Z._~!=&\\^\\-\\\\?\'"])|(\\d+)|(x[0-9a-fA-F]+))))*$))|(?P<t_BAD_CHAR_CONST>(\'([^\'\\\\\\n]|(\\\\(([a-zA-Z._~!=&\\^\\-\\\\?\'"])|(\\d+)|(x[0-9a-fA-F]+))))[^\'\n]+\')|(\'\')|(\'([\\\\][^a-zA-Z._~^!=&\\^\\-\\\\?\'"x0-7])[^\'\\n]*\'))', [None, ('t_INT_CONST_BIN', 'INT_CONST_BIN'), None, None, None, None, None, None, None, ('t_BAD_CONST_OCT', 'BAD_CONST_OCT'), ('t_INT_CONST_OCT', 'INT_CONST_OCT'), None, None, None, None, None, None, None, ('t_INT_CONST_DEC', 'INT_CONST_DEC'), None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, ('t_CHAR_CONST', 'CHAR_CONST'), None, None, None, None, None, None, ('t_WCHAR_CONST', 'WCHAR_CONST'), None, None, None, None, None, None, ('t_UNMATCHED_QUOTE', 'UNMATCHED_QUOTE'), None, None, None, None, None, None, None, None, None, None, None, None, None, None, ('t_BAD_CHAR_CONST', 'BAD_CHAR_CONST')]), ('(?P<t_WSTRING_LITERAL>L"([^"\\\\\\n]|(\\\\(([a-zA-Z._~!=&\\^\\-\\\\?\'"])|(\\d+)|(x[0-9a-fA-F]+))))*")|(?P<t_BAD_STRING_LITERAL>"([^"\\\\\\n]|(\\\\(([a-zA-Z._~!=&\\^\\-\\\\?\'"])|(\\d+)|(x[0-9a-fA-F]+))))*?([\\\\][^a-zA-Z._~^!=&\\^\\-\\\\?\'"x0-7])([^"\\\\\\n]|(\\\\(([a-zA-Z._~!=&\\^\\-\\\\?\'"])|(\\d+)|(x[0-9a-fA-F]+))))*")|(?P<t_ID>[a-zA-Z_$][0-9a-zA-Z_$]*)|(?P<t_STRING_LITERAL>"([^"\\\\\\n]|(\\\\(([a-zA-Z._~!=&\\^\\-\\\\?\'"])|(\\d+)|(x[0-9a-fA-F]+))))*")|(?P<t_ELLIPSIS>\\.\\.\\.)|(?P<t_PLUSPLUS>\\+\\+)|(?P<t_LOR>\\|\\|)|(?P<t_XOREQUAL>\\^=)|(?P<t_OREQUAL>\\|=)|(?P<t_LSHIFTEQUAL><<=)|(?P<t_RSHIFTEQUAL>>>=)|(?P<t_PLUSEQUAL>\\+=)|(?P<t_TIMESEQUAL>\\*=)|(?P<t_PLUS>\\+)|(?P<t_MODEQUAL>%=)|(?P<t_DIVEQUAL>/=)', [None, ('t_WSTRING_LITERAL', 'WSTRING_LITERAL'), None, None, None, None, None, None, ('t_BAD_STRING_LITERAL', 'BAD_STRING_LITERAL'), None, None, None, None, None, None, None, None, None, None, None, None, None, ('t_ID', 'ID'), (None, 'STRING_LITERAL'), None, None, None, None, None, None, (None, 'ELLIPSIS'), (None, 'PLUSPLUS'), (None, 'LOR'), (None, 'XOREQUAL'), (None, 'OREQUAL'), (None, 'LSHIFTEQUAL'), (None, 'RSHIFTEQUAL'), (None, 'PLUSEQUAL'), (None, 'TIMESEQUAL'), (None, 'PLUS'), (None, 'MODEQUAL'), (None, 'DIVEQUAL')]), ('(?P<t_RBRACKET>\\])|(?P<t_CONDOP>\\?)|(?P<t_XOR>\\^)|(?P<t_LSHIFT><<)|(?P<t_LE><=)|(?P<t_LPAREN>\\()|(?P<t_ARROW>->)|(?P<t_EQ>==)|(?P<t_NE>!=)|(?P<t_MINUSMINUS>--)|(?P<t_OR>\\|)|(?P<t_TIMES>\\*)|(?P<t_LBRACKET>\\[)|(?P<t_GE>>=)|(?P<t_RPAREN>\\))|(?P<t_LAND>&&)|(?P<t_RSHIFT>>>)|(?P<t_MINUSEQUAL>-=)|(?P<t_PERIOD>\\.)|(?P<t_ANDEQUAL>&=)|(?P<t_EQUALS>=)|(?P<t_LT><)|(?P<t_COMMA>,)|(?P<t_DIVIDE>/)|(?P<t_AND>&)|(?P<t_MOD>%)|(?P<t_SEMI>;)|(?P<t_MINUS>-)|(?P<t_GT>>)|(?P<t_COLON>:)|(?P<t_NOT>~)|(?P<t_LNOT>!)', [None, (None, 'RBRACKET'), (None, 'CONDOP'), (None, 'XOR'), (None, 'LSHIFT'), (None, 'LE'), (None, 'LPAREN'), (None, 'ARROW'), (None, 'EQ'), (None, 'NE'), (None, 'MINUSMINUS'), (None, 'OR'), (None, 'TIMES'), (None, 'LBRACKET'), (None, 'GE'), (None, 'RPAREN'), (None, 'LAND'), (None, 'RSHIFT'), (None, 'MINUSEQUAL'), (None, 'PERIOD'), (None, 'ANDEQUAL'), (None, 'EQUALS'), (None, 'LT'), (None, 'COMMA'), (None, 'DIVIDE'), (None, 'AND'), (None, 'MOD'), (None, 'SEMI'), (None, 'MINUS'), (None, 'GT'), (None, 'COLON'), (None, 'NOT'), (None, 'LNOT')])]}
8+
_lexstateignore = {'ppline': ' \t', 'pppragma': ' \t', 'INITIAL': ' \t'}
9+
_lexstateerrorf = {'ppline': 't_ppline_error', 'pppragma': 't_pppragma_error', 'INITIAL': 't_error'}
10+
_lexstateeoff = {}

ports/javascript/library.h

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2017, 2018 Rami Ali
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include "py/obj.h"
28+
29+
extern void mp_js_write(const char *str, mp_uint_t len);
30+
extern int mp_js_ticks_ms(void);
31+
extern void mp_js_hook(void);

ports/javascript/library.js

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2017, 2018 Rami Ali
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
mergeInto(LibraryManager.library, {
28+
mp_js_write: function(ptr, len) {
29+
for (var i = 0; i < len; ++i) {
30+
c = String.fromCharCode(getValue(ptr + i, 'i8'));
31+
if (typeof window === 'undefined') {
32+
process.stdout.write(c);
33+
} else {
34+
if(mp_js_stdout === undefined || mp_js_stdout === null)
35+
mp_js_stdout = document.getElementById('mp_js_stdout');
36+
var print = new Event('print');
37+
print.data = c;
38+
mp_js_stdout.dispatchEvent(print);
39+
}
40+
}
41+
},
42+
43+
mp_js_ticks_ms: function() {
44+
return (new Date()).getTime() - MP_JS_EPOCH;
45+
},
46+
47+
mp_js_hook: function() {
48+
if (typeof window === 'undefined') {
49+
var mp_interrupt_char = Module.ccall('mp_hal_get_interrupt_char', 'number', ['number'], ['null']);
50+
var fs = require('fs');
51+
52+
var buf = new Buffer(1);
53+
try {
54+
var n = fs.readSync(process.stdin.fd, buf, 0, 1);
55+
if (n > 0) {
56+
if (buf[0] == mp_interrupt_char) {
57+
Module.ccall('mp_keyboard_interrupt', 'null', ['null'], ['null']);
58+
} else {
59+
process.stdout.write(String.fromCharCode(buf[0]));
60+
}
61+
}
62+
} catch (e) {
63+
if (e.code === 'EAGAIN') {
64+
} else {
65+
throw e;
66+
}
67+
}
68+
}
69+
},
70+
});

0 commit comments

Comments
 (0)