From ea2fcdd338b9a9a545c119a7d86de1b8caf77314 Mon Sep 17 00:00:00 2001 From: Wolf Vollprecht Date: Sun, 10 Feb 2019 16:29:25 +0100 Subject: [PATCH] javascript: Fix Emscripten async load, and to compile with modern clang. --- ports/javascript/Makefile | 10 +++-- ports/javascript/mphalport.c | 2 +- ports/javascript/mphalport.h | 2 +- ports/javascript/wrapper.js | 85 +++++++++++++++++++----------------- 4 files changed, 55 insertions(+), 44 deletions(-) diff --git a/ports/javascript/Makefile b/ports/javascript/Makefile index 3ce698f10..9b0f4d89c 100644 --- a/ports/javascript/Makefile +++ b/ports/javascript/Makefile @@ -14,7 +14,12 @@ INC += -I$(TOP) INC += -I$(BUILD) CPP = clang -E -CFLAGS = -m32 $(INC) -Wall -Werror -std=c99 $(COPT) + +ifdef EMSCRIPTEN + CPP += -isystem $(EMSCRIPTEN)/system/include/libc -cxx-isystem $(EMSCRIPTEN)/system/include/libcxx +endif + +CFLAGS = -m32 -Wall -Werror $(INC) -std=c99 $(COPT) LDFLAGS = -m32 -Wl,-Map=$@.map,--cref -Wl,--gc-sections CFLAGS += -O0 -DNDEBUG @@ -46,8 +51,7 @@ all: $(BUILD)/micropython.js $(BUILD)/micropython.js: $(OBJ) library.js wrapper.js $(ECHO) "LINK $(BUILD)/firmware.js" $(Q)emcc $(LDFLAGS) -o $(BUILD)/firmware.js $(OBJ) $(JSFLAGS) - cat $(BUILD)/firmware.js > $@ - cat wrapper.js >> $@ + cat wrapper.js $(BUILD)/firmware.js > $@ min: $(BUILD)/micropython.js uglifyjs $< -c -o $(BUILD)/micropython.min.js diff --git a/ports/javascript/mphalport.c b/ports/javascript/mphalport.c index 18e3e2ade..c938a6392 100644 --- a/ports/javascript/mphalport.c +++ b/ports/javascript/mphalport.c @@ -27,7 +27,7 @@ #include "library.h" #include "mphalport.h" -void mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) { +void mp_hal_stdout_tx_strn(const char *str, size_t len) { mp_js_write(str, len); } diff --git a/ports/javascript/mphalport.h b/ports/javascript/mphalport.h index 8ba66fcc9..614ce8526 100644 --- a/ports/javascript/mphalport.h +++ b/ports/javascript/mphalport.h @@ -28,7 +28,7 @@ #include "lib/utils/interrupt_char.h" #define mp_hal_stdin_rx_chr() (0) -void mp_hal_stdout_tx_strn(const char *str, mp_uint_t len); +void mp_hal_stdout_tx_strn(const char *str, size_t len); void mp_hal_delay_ms(mp_uint_t ms); void mp_hal_delay_us(mp_uint_t us); diff --git a/ports/javascript/wrapper.js b/ports/javascript/wrapper.js index 6109de902..84ad2ae3a 100644 --- a/ports/javascript/wrapper.js +++ b/ports/javascript/wrapper.js @@ -24,47 +24,54 @@ * THE SOFTWARE. */ -mp_js_init = Module.cwrap('mp_js_init', 'null', ['number']); -mp_js_do_str = Module.cwrap('mp_js_do_str', 'null', ['string']); -mp_js_init_repl = Module.cwrap('mp_js_init_repl', 'null', ['null']); -mp_js_process_char = Module.cwrap('mp_js_process_char', 'number', ['number']); +var Module = {}; -var MP_JS_EPOCH = (new Date()).getTime(); +var mainProgram = function() +{ + mp_js_init = Module.cwrap('mp_js_init', 'null', ['number']); + mp_js_do_str = Module.cwrap('mp_js_do_str', 'null', ['string']); + mp_js_init_repl = Module.cwrap('mp_js_init_repl', 'null', ['null']); + mp_js_process_char = Module.cwrap('mp_js_process_char', 'number', ['number']); -if (typeof window === 'undefined' && require.main === module) { - var fs = require('fs'); - var stack_size = 64 * 1024; - var contents = ''; - var repl = true; + MP_JS_EPOCH = (new Date()).getTime(); - for (var i = 0; i < process.argv.length; i++) { - if (process.argv[i] === '-X' && i < process.argv.length - 1) { - if (process.argv[i + 1].includes('stack=')) { - stack_size = parseInt(process.argv[i + 1].split('stack=')[1]); - if (process.argv[i + 1].substr(-1).toLowerCase() === 'k') { - stack_size *= 1024; - } else if (process.argv[i + 1].substr(-1).toLowerCase() === 'm') { - stack_size *= 1024 * 1024; - } - } - } else if (process.argv[i].includes('.py')) { - contents += fs.readFileSync(process.argv[i], 'utf8'); - repl = false;; - } - } - mp_js_init(stack_size); + if (typeof window === 'undefined' && require.main === module) { + var fs = require('fs'); + var stack_size = 64 * 1024; + var contents = ''; + var repl = true; - if (repl) { - mp_js_init_repl(); - process.stdin.setRawMode(true); - process.stdin.on('data', function (data) { - for (var i = 0; i < data.length; i++) { - if (mp_js_process_char(data[i])) { - process.exit() - } - } - }); - } else { - mp_js_do_str(contents); - } + for (var i = 0; i < process.argv.length; i++) { + if (process.argv[i] === '-X' && i < process.argv.length - 1) { + if (process.argv[i + 1].includes('stack=')) { + stack_size = parseInt(process.argv[i + 1].split('stack=')[1]); + if (process.argv[i + 1].substr(-1).toLowerCase() === 'k') { + stack_size *= 1024; + } else if (process.argv[i + 1].substr(-1).toLowerCase() === 'm') { + stack_size *= 1024 * 1024; + } + } + } else if (process.argv[i].includes('.py')) { + contents += fs.readFileSync(process.argv[i], 'utf8'); + repl = false;; + } + } + mp_js_init(stack_size); + + if (repl) { + mp_js_init_repl(); + process.stdin.setRawMode(true); + process.stdin.on('data', function (data) { + for (var i = 0; i < data.length; i++) { + if (mp_js_process_char(data[i])) { + process.exit() + } + } + }); + } else { + mp_js_do_str(contents); + } + } } + +Module["onRuntimeInitialized"] = mainProgram; \ No newline at end of file