Skip to content

Commit

Permalink
javascript: Fix Emscripten async load, and to compile with modern clang.
Browse files Browse the repository at this point in the history
  • Loading branch information
wolfv authored and dpgeorge committed Mar 13, 2019
1 parent 7d675f3 commit ea2fcdd
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 44 deletions.
10 changes: 7 additions & 3 deletions ports/javascript/Makefile
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion ports/javascript/mphalport.c
Expand Up @@ -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);
}

Expand Down
2 changes: 1 addition & 1 deletion ports/javascript/mphalport.h
Expand Up @@ -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);
Expand Down
85 changes: 46 additions & 39 deletions ports/javascript/wrapper.js
Expand Up @@ -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;

0 comments on commit ea2fcdd

Please sign in to comment.