kopia lustrzana https://github.com/micropython/micropython
webassembly: Implement replInit() and replProcessChar().
This is the JavaScript API for starting and running a REPL. Signed-off-by: Damien George <damien@micropython.org>pull/13583/head
rodzic
625b17a410
commit
6ff3e356e2
|
@ -34,6 +34,7 @@ include $(TOP)/extmod/extmod.mk
|
||||||
|
|
||||||
CC = emcc
|
CC = emcc
|
||||||
LD = emcc
|
LD = emcc
|
||||||
|
NODE ?= node
|
||||||
TERSER ?= npx terser
|
TERSER ?= npx terser
|
||||||
|
|
||||||
INC += -I.
|
INC += -I.
|
||||||
|
@ -76,9 +77,8 @@ JSFLAGS += -s EXPORTED_FUNCTIONS="\
|
||||||
_free,\
|
_free,\
|
||||||
_malloc,\
|
_malloc,\
|
||||||
_mp_js_init,\
|
_mp_js_init,\
|
||||||
_mp_js_init_repl,\
|
_mp_js_repl_init,\
|
||||||
_mp_js_do_str,\
|
_mp_js_repl_process_char,\
|
||||||
_mp_js_process_char,\
|
|
||||||
_mp_hal_get_interrupt_char,\
|
_mp_hal_get_interrupt_char,\
|
||||||
_mp_sched_keyboard_interrupt$(EXPORTED_FUNCTIONS_EXTRA)"
|
_mp_sched_keyboard_interrupt$(EXPORTED_FUNCTIONS_EXTRA)"
|
||||||
JSFLAGS += -s EXPORTED_RUNTIME_METHODS="\
|
JSFLAGS += -s EXPORTED_RUNTIME_METHODS="\
|
||||||
|
@ -124,7 +124,7 @@ OBJ += $(addprefix $(BUILD)/, $(SRC_C:.c=.o))
|
||||||
################################################################################
|
################################################################################
|
||||||
# Main targets.
|
# Main targets.
|
||||||
|
|
||||||
.PHONY: all min test test_min
|
.PHONY: all repl min test test_min
|
||||||
|
|
||||||
all: $(BUILD)/micropython.mjs
|
all: $(BUILD)/micropython.mjs
|
||||||
|
|
||||||
|
@ -136,6 +136,9 @@ $(BUILD)/micropython.mjs: $(OBJ) library.js $(SRC_JS)
|
||||||
$(BUILD)/micropython.min.mjs: $(BUILD)/micropython.mjs
|
$(BUILD)/micropython.min.mjs: $(BUILD)/micropython.mjs
|
||||||
$(TERSER) $< --compress --module -o $@
|
$(TERSER) $< --compress --module -o $@
|
||||||
|
|
||||||
|
repl: $(BUILD)/micropython.mjs
|
||||||
|
$(NODE) $<
|
||||||
|
|
||||||
min: $(BUILD)/micropython.min.mjs
|
min: $(BUILD)/micropython.min.mjs
|
||||||
|
|
||||||
test: $(BUILD)/micropython.mjs $(TOP)/tests/run-tests.py
|
test: $(BUILD)/micropython.mjs $(TOP)/tests/run-tests.py
|
||||||
|
|
|
@ -150,6 +150,27 @@ export async function loadMicroPython(options) {
|
||||||
);
|
);
|
||||||
return proxy_convert_mp_to_js_obj_jsside_with_free(value);
|
return proxy_convert_mp_to_js_obj_jsside_with_free(value);
|
||||||
},
|
},
|
||||||
|
replInit() {
|
||||||
|
Module.ccall("mp_js_repl_init", "null", ["null"]);
|
||||||
|
},
|
||||||
|
replProcessChar(chr) {
|
||||||
|
return Module.ccall(
|
||||||
|
"mp_js_repl_process_char",
|
||||||
|
"number",
|
||||||
|
["number"],
|
||||||
|
[chr],
|
||||||
|
);
|
||||||
|
},
|
||||||
|
// Needed if the GC/asyncify is enabled.
|
||||||
|
async replProcessCharWithAsyncify(chr) {
|
||||||
|
return Module.ccall(
|
||||||
|
"mp_js_repl_process_char",
|
||||||
|
"number",
|
||||||
|
["number"],
|
||||||
|
[chr],
|
||||||
|
{ async: true },
|
||||||
|
);
|
||||||
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -191,11 +212,11 @@ async function runCLI() {
|
||||||
});
|
});
|
||||||
|
|
||||||
if (repl) {
|
if (repl) {
|
||||||
mp_js_init_repl();
|
mp.replInit();
|
||||||
process.stdin.setRawMode(true);
|
process.stdin.setRawMode(true);
|
||||||
process.stdin.on("data", (data) => {
|
process.stdin.on("data", (data) => {
|
||||||
for (let i = 0; i < data.length; i++) {
|
for (let i = 0; i < data.length; i++) {
|
||||||
mp_js_process_char(data[i]).then((result) => {
|
mp.replProcessCharWithAsyncify(data[i]).then((result) => {
|
||||||
if (result) {
|
if (result) {
|
||||||
process.exit();
|
process.exit();
|
||||||
}
|
}
|
||||||
|
|
|
@ -44,46 +44,6 @@
|
||||||
#include "library.h"
|
#include "library.h"
|
||||||
#include "proxy_c.h"
|
#include "proxy_c.h"
|
||||||
|
|
||||||
#if MICROPY_ENABLE_COMPILER
|
|
||||||
int do_str(const char *src, mp_parse_input_kind_t input_kind) {
|
|
||||||
int ret = 0;
|
|
||||||
nlr_buf_t nlr;
|
|
||||||
if (nlr_push(&nlr) == 0) {
|
|
||||||
mp_lexer_t *lex = mp_lexer_new_from_str_len(MP_QSTR__lt_stdin_gt_, src, strlen(src), 0);
|
|
||||||
qstr source_name = lex->source_name;
|
|
||||||
mp_parse_tree_t parse_tree = mp_parse(lex, input_kind);
|
|
||||||
mp_obj_t module_fun = mp_compile(&parse_tree, source_name, false);
|
|
||||||
mp_call_function_0(module_fun);
|
|
||||||
nlr_pop();
|
|
||||||
} else {
|
|
||||||
// uncaught exception
|
|
||||||
if (mp_obj_is_subclass_fast(mp_obj_get_type((mp_obj_t)nlr.ret_val), &mp_type_SystemExit)) {
|
|
||||||
mp_obj_t exit_val = mp_obj_exception_get_value(MP_OBJ_FROM_PTR(nlr.ret_val));
|
|
||||||
if (exit_val != mp_const_none) {
|
|
||||||
mp_int_t int_val;
|
|
||||||
if (mp_obj_get_int_maybe(exit_val, &int_val)) {
|
|
||||||
ret = int_val & 255;
|
|
||||||
} else {
|
|
||||||
ret = 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
mp_obj_print_exception(&mp_plat_print, (mp_obj_t)nlr.ret_val);
|
|
||||||
ret = 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
int mp_js_do_str(const char *code) {
|
|
||||||
return do_str(code, MP_PARSE_FILE_INPUT);
|
|
||||||
}
|
|
||||||
|
|
||||||
int mp_js_process_char(int c) {
|
|
||||||
return pyexec_event_repl_process_char(c);
|
|
||||||
}
|
|
||||||
|
|
||||||
void mp_js_init(int heap_size) {
|
void mp_js_init(int heap_size) {
|
||||||
#if MICROPY_ENABLE_GC
|
#if MICROPY_ENABLE_GC
|
||||||
char *heap = (char *)malloc(heap_size * sizeof(char));
|
char *heap = (char *)malloc(heap_size * sizeof(char));
|
||||||
|
@ -111,10 +71,6 @@ void mp_js_init(int heap_size) {
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void mp_js_init_repl() {
|
|
||||||
pyexec_event_repl_init();
|
|
||||||
}
|
|
||||||
|
|
||||||
void mp_js_register_js_module(const char *name, uint32_t *value) {
|
void mp_js_register_js_module(const char *name, uint32_t *value) {
|
||||||
mp_obj_t module_name = MP_OBJ_NEW_QSTR(qstr_from_str(name));
|
mp_obj_t module_name = MP_OBJ_NEW_QSTR(qstr_from_str(name));
|
||||||
mp_obj_t module = proxy_convert_js_to_mp_obj_cside(value);
|
mp_obj_t module = proxy_convert_js_to_mp_obj_cside(value);
|
||||||
|
@ -175,6 +131,14 @@ void mp_js_do_exec_async(const char *src, uint32_t *out) {
|
||||||
mp_compile_allow_top_level_await = false;
|
mp_compile_allow_top_level_await = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void mp_js_repl_init(void) {
|
||||||
|
pyexec_event_repl_init();
|
||||||
|
}
|
||||||
|
|
||||||
|
int mp_js_repl_process_char(int c) {
|
||||||
|
return pyexec_event_repl_process_char(c);
|
||||||
|
}
|
||||||
|
|
||||||
#if MICROPY_GC_SPLIT_HEAP_AUTO
|
#if MICROPY_GC_SPLIT_HEAP_AUTO
|
||||||
|
|
||||||
// The largest new region that is available to become Python heap.
|
// The largest new region that is available to become Python heap.
|
||||||
|
|
|
@ -1,85 +0,0 @@
|
||||||
/*
|
|
||||||
* This file is part of the MicroPython project, http://micropython.org/
|
|
||||||
*
|
|
||||||
* The MIT License (MIT)
|
|
||||||
*
|
|
||||||
* Copyright (c) 2017, 2018 Rami Ali
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
|
||||||
* in the Software without restriction, including without limitation the rights
|
|
||||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
||||||
* copies of the Software, and to permit persons to whom the Software is
|
|
||||||
* furnished to do so, subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be included in
|
|
||||||
* all copies or substantial portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
||||||
* THE SOFTWARE.
|
|
||||||
*/
|
|
||||||
|
|
||||||
var Module = {};
|
|
||||||
|
|
||||||
var mainProgram = function()
|
|
||||||
{
|
|
||||||
mp_js_init = Module.cwrap('mp_js_init', 'null', ['number']);
|
|
||||||
mp_js_do_str = Module.cwrap('mp_js_do_str', 'number', ['string'], {async: true});
|
|
||||||
mp_js_init_repl = Module.cwrap('mp_js_init_repl', 'null', ['null']);
|
|
||||||
mp_js_process_char = Module.cwrap('mp_js_process_char', 'number', ['number'], {async: true});
|
|
||||||
|
|
||||||
if (typeof window === 'undefined' && require.main === module) {
|
|
||||||
var fs = require('fs');
|
|
||||||
var heap_size = 128 * 1024;
|
|
||||||
var contents = '';
|
|
||||||
var repl = true;
|
|
||||||
|
|
||||||
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('heapsize=')) {
|
|
||||||
heap_size = parseInt(process.argv[i + 1].split('heapsize=')[1]);
|
|
||||||
if (process.argv[i + 1].substr(-1).toLowerCase() === 'k') {
|
|
||||||
heap_size *= 1024;
|
|
||||||
} else if (process.argv[i + 1].substr(-1).toLowerCase() === 'm') {
|
|
||||||
heap_size *= 1024 * 1024;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else if (process.argv[i].includes('.py')) {
|
|
||||||
contents += fs.readFileSync(process.argv[i], 'utf8');
|
|
||||||
repl = false;;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (process.stdin.isTTY === false) {
|
|
||||||
contents = fs.readFileSync(0, 'utf8');
|
|
||||||
repl = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
mp_js_init(heap_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++) {
|
|
||||||
mp_js_process_char(data[i]).then(result => {
|
|
||||||
if (result) {
|
|
||||||
process.exit()
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
mp_js_do_str(contents).then(exitCode => {
|
|
||||||
process.exitCode = exitCode
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Module["onRuntimeInitialized"] = mainProgram;
|
|
Ładowanie…
Reference in New Issue