Skip to content

port-javascript: can't push twice #4860

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

Closed
wants to merge 11 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 54 additions & 6 deletions py/builtinimport.c
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,38 @@ STATIC mp_import_stat_t find_file(const char *file_str, uint file_len, vstr_t *d
#endif
}

#if __EMSCRIPTEN__

mp_obj_t mp_parse_compile_execute_wasm(mp_lexer_t *lex, mp_parse_input_kind_t parse_input_kind, mp_obj_dict_t *globals, mp_obj_dict_t *locals) {
// save context
mp_obj_dict_t *volatile old_globals = mp_globals_get();
mp_obj_dict_t *volatile old_locals = mp_locals_get();

// set new context
mp_globals_set(globals);
mp_locals_set(locals);

qstr source_name = lex->source_name;
mp_parse_tree_t parse_tree = mp_parse(lex, parse_input_kind);
mp_obj_t module_fun = mp_compile(&parse_tree, source_name, MP_EMIT_OPT_NONE, false);

mp_obj_t ret;
if (MICROPY_PY_BUILTINS_COMPILE && globals == NULL) {
// for compile only, return value is the module function
ret = module_fun;
} else {
// execute module function and get return value
ret = mp_call_function_0(module_fun);
}

// finish nlr block, restore context and return value
mp_globals_set(old_globals);
mp_locals_set(old_locals);
return ret;
}

#endif

#if MICROPY_MODULE_FROZEN_STR || MICROPY_ENABLE_COMPILER
STATIC void do_load_from_lexer(mp_obj_t module_obj, mp_lexer_t *lex) {
#if MICROPY_PY___FILE__
Expand All @@ -140,16 +172,25 @@ STATIC void do_load_from_lexer(mp_obj_t module_obj, mp_lexer_t *lex) {

// parse, compile and execute the module in its context
mp_obj_dict_t *mod_globals = mp_obj_module_get_globals(module_obj);
#if __EMSCRIPTEN__
mp_parse_compile_execute_wasm(lex, MP_PARSE_FILE_INPUT, mod_globals, mod_globals);
#else
mp_parse_compile_execute(lex, MP_PARSE_FILE_INPUT, mod_globals, mod_globals);
#endif
}
#endif

#if MICROPY_PERSISTENT_CODE_LOAD || MICROPY_MODULE_FROZEN_MPY
#if MICROPY_PY___FILE__
STATIC void do_execute_raw_code(mp_obj_t module_obj, mp_raw_code_t *raw_code, const char* source_name) {
#else
STATIC void do_execute_raw_code(mp_obj_t module_obj, mp_raw_code_t *raw_code) {
#endif
#if MICROPY_PY___FILE__
// TODO
//qstr source_name = lex->source_name;
//mp_store_attr(module_obj, MP_QSTR___file__, MP_OBJ_NEW_QSTR(source_name));
if (source_name!=NULL){
mp_store_attr(module_obj, MP_QSTR___file__, MP_OBJ_NEW_QSTR(qstr_from_str(source_name)));
} else
mp_store_attr(module_obj, MP_QSTR___file__, MP_OBJ_NEW_QSTR(qstr_from_str("<stdin>") ));
#endif

// execute the module in its context
Expand All @@ -162,7 +203,6 @@ STATIC void do_execute_raw_code(mp_obj_t module_obj, mp_raw_code_t *raw_code) {
// set new context
mp_globals_set(mod_globals);
mp_locals_set(mod_globals);

nlr_buf_t nlr;
if (nlr_push(&nlr) == 0) {
mp_obj_t module_fun = mp_make_function_from_raw_code(raw_code, MP_OBJ_NULL, MP_OBJ_NULL);
Expand Down Expand Up @@ -206,7 +246,11 @@ STATIC void do_load(mp_obj_t module_obj, vstr_t *file) {
// its data) in the list of frozen files, execute it.
#if MICROPY_MODULE_FROZEN_MPY
if (frozen_type == MP_FROZEN_MPY) {
do_execute_raw_code(module_obj, modref);
#if MICROPY_PY___FILE__
do_execute_raw_code(module_obj, modref, file_str);
#else
do_execute_raw_code(module_obj, modref);
#endif
return;
}
#endif
Expand All @@ -215,8 +259,12 @@ STATIC void do_load(mp_obj_t module_obj, vstr_t *file) {
// the correct format and, if so, load and execute the file.
#if MICROPY_HAS_FILE_READER && MICROPY_PERSISTENT_CODE_LOAD
if (file_str[file->len - 3] == 'm') {
mp_raw_code_t *raw_code = mp_raw_code_load_file(file_str);
mp_raw_code_t *raw_code = mp_raw_code_load_file(file_str);
#if MICROPY_PY___FILE__
do_execute_raw_code(module_obj, raw_code, file_str);
#else
do_execute_raw_code(module_obj, raw_code);
#endif
return;
}
#endif
Expand Down
4 changes: 2 additions & 2 deletions py/emitglue.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,13 @@ typedef struct _mp_raw_code_t {
size_t fun_data_len;
uint16_t n_obj;
uint16_t n_raw_code;
#if MICROPY_EMIT_NATIVE || MICROPY_EMIT_INLINE_ASM
#if MICROPY_EMIT_NATIVE || MICROPY_EMIT_INLINE_ASM || MICROPY_EMIT_WASM
uint16_t prelude_offset;
uint16_t n_qstr;
mp_qstr_link_entry_t *qstr_link;
#endif
#endif
#if MICROPY_EMIT_NATIVE || MICROPY_EMIT_INLINE_ASM
#if MICROPY_EMIT_NATIVE || MICROPY_EMIT_INLINE_ASM || MICROPY_EMIT_WASM
mp_uint_t type_sig; // for viper, compressed as 2-bit types; ret is MSB, then arg0, arg1, etc
#endif
} mp_raw_code_t;
Expand Down
10 changes: 5 additions & 5 deletions py/persistentcode.c
Original file line number Diff line number Diff line change
Expand Up @@ -404,14 +404,14 @@ STATIC mp_raw_code_t *load_raw_code(mp_reader_t *reader, qstr_window_t *qw) {
ip2[2] = source_file; ip2[3] = source_file >> 8;
}


// Number of entries in constant table
size_t n_obj = read_uint(reader, NULL);
size_t n_raw_code = read_uint(reader, NULL);

mp_uint_t *const_table = NULL;
if (kind != MP_CODE_NATIVE_ASM) {
// Load constant table for bytecode, native and viper

// Number of entries in constant table
size_t n_obj = read_uint(reader, NULL);
size_t n_raw_code = read_uint(reader, NULL);

// Allocate constant table
size_t n_alloc = prelude.n_pos_args + prelude.n_kwonly_args + n_obj + n_raw_code;
if (kind != MP_CODE_BYTECODE) {
Expand Down