Skip to content

Commit 2e75a17

Browse files
committed
esp8266: Fix issue when current repl line was garbage-collected.
Reference it from root pointers section.
1 parent b1dfdaf commit 2e75a17

File tree

2 files changed

+22
-18
lines changed

2 files changed

+22
-18
lines changed

esp8266/mpconfigport.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ extern const struct _mp_obj_module_t onewire_module;
126126

127127
#define MICROPY_PORT_ROOT_POINTERS \
128128
const char *readline_hist[8]; \
129+
vstr_t *repl_line; \
129130
mp_obj_t mp_kbd_exception; \
130131

131132
// We need to provide a declaration/definition of alloca()

lib/utils/pyexec.c

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,11 @@ STATIC int parse_compile_execute(mp_lexer_t *lex, mp_parse_input_kind_t input_ki
121121
#if MICROPY_REPL_EVENT_DRIVEN
122122

123123
typedef struct _repl_t {
124-
// XXX line holds a root pointer!
125-
vstr_t line;
124+
// This structure originally also held current REPL line,
125+
// but it was moved to MP_STATE_VM(repl_line) as containing
126+
// root pointer. Still keep structure in case more state
127+
// will be added later.
128+
//vstr_t line;
126129
bool cont_line;
127130
} repl_t;
128131

@@ -132,9 +135,9 @@ STATIC int pyexec_raw_repl_process_char(int c);
132135
STATIC int pyexec_friendly_repl_process_char(int c);
133136

134137
void pyexec_event_repl_init(void) {
135-
vstr_init(&repl.line, 32);
138+
MP_STATE_VM(repl_line) = vstr_new_size(32);
136139
repl.cont_line = false;
137-
readline_init(&repl.line, ">>> ");
140+
readline_init(MP_STATE_VM(repl_line), ">>> ");
138141
if (pyexec_mode_kind == PYEXEC_MODE_RAW_REPL) {
139142
pyexec_raw_repl_process_char(CHAR_CTRL_A);
140143
} else {
@@ -155,27 +158,27 @@ STATIC int pyexec_raw_repl_process_char(int c) {
155158
return 0;
156159
} else if (c == CHAR_CTRL_C) {
157160
// clear line
158-
vstr_reset(&repl.line);
161+
vstr_reset(MP_STATE_VM(repl_line));
159162
return 0;
160163
} else if (c == CHAR_CTRL_D) {
161164
// input finished
162165
} else {
163166
// let through any other raw 8-bit value
164-
vstr_add_byte(&repl.line, c);
167+
vstr_add_byte(MP_STATE_VM(repl_line), c);
165168
return 0;
166169
}
167170

168171
// indicate reception of command
169172
mp_hal_stdout_tx_str("OK");
170173

171-
if (repl.line.len == 0) {
174+
if (MP_STATE_VM(repl_line)->len == 0) {
172175
// exit for a soft reset
173176
mp_hal_stdout_tx_str("\r\n");
174-
vstr_clear(&repl.line);
177+
vstr_clear(MP_STATE_VM(repl_line));
175178
return PYEXEC_FORCED_EXIT;
176179
}
177180

178-
mp_lexer_t *lex = mp_lexer_new_from_str_len(MP_QSTR__lt_stdin_gt_, repl.line.buf, repl.line.len, 0);
181+
mp_lexer_t *lex = mp_lexer_new_from_str_len(MP_QSTR__lt_stdin_gt_, MP_STATE_VM(repl_line)->buf, MP_STATE_VM(repl_line)->len, 0);
179182
if (lex == NULL) {
180183
mp_hal_stdout_tx_str("\x04MemoryError\r\n\x04");
181184
} else {
@@ -186,7 +189,7 @@ STATIC int pyexec_raw_repl_process_char(int c) {
186189
}
187190

188191
reset:
189-
vstr_reset(&repl.line);
192+
vstr_reset(MP_STATE_VM(repl_line));
190193
mp_hal_stdout_tx_str(">");
191194

192195
return 0;
@@ -216,19 +219,19 @@ STATIC int pyexec_friendly_repl_process_char(int c) {
216219
} else if (ret == CHAR_CTRL_D) {
217220
// exit for a soft reset
218221
mp_hal_stdout_tx_str("\r\n");
219-
vstr_clear(&repl.line);
222+
vstr_clear(MP_STATE_VM(repl_line));
220223
return PYEXEC_FORCED_EXIT;
221224
}
222225

223226
if (ret < 0) {
224227
return 0;
225228
}
226229

227-
if (!mp_repl_continue_with_input(vstr_null_terminated_str(&repl.line))) {
230+
if (!mp_repl_continue_with_input(vstr_null_terminated_str(MP_STATE_VM(repl_line)))) {
228231
goto exec;
229232
}
230233

231-
vstr_add_byte(&repl.line, '\n');
234+
vstr_add_byte(MP_STATE_VM(repl_line), '\n');
232235
repl.cont_line = true;
233236
readline_note_newline("... ");
234237
return 0;
@@ -249,14 +252,14 @@ STATIC int pyexec_friendly_repl_process_char(int c) {
249252
return 0;
250253
}
251254

252-
if (mp_repl_continue_with_input(vstr_null_terminated_str(&repl.line))) {
253-
vstr_add_byte(&repl.line, '\n');
255+
if (mp_repl_continue_with_input(vstr_null_terminated_str(MP_STATE_VM(repl_line)))) {
256+
vstr_add_byte(MP_STATE_VM(repl_line), '\n');
254257
readline_note_newline("... ");
255258
return 0;
256259
}
257260

258261
exec: ;
259-
mp_lexer_t *lex = mp_lexer_new_from_str_len(MP_QSTR__lt_stdin_gt_, vstr_str(&repl.line), vstr_len(&repl.line), 0);
262+
mp_lexer_t *lex = mp_lexer_new_from_str_len(MP_QSTR__lt_stdin_gt_, vstr_str(MP_STATE_VM(repl_line)), vstr_len(MP_STATE_VM(repl_line)), 0);
260263
if (lex == NULL) {
261264
printf("MemoryError\n");
262265
} else {
@@ -267,9 +270,9 @@ exec: ;
267270
}
268271

269272
input_restart:
270-
vstr_reset(&repl.line);
273+
vstr_reset(MP_STATE_VM(repl_line));
271274
repl.cont_line = false;
272-
readline_init(&repl.line, ">>> ");
275+
readline_init(MP_STATE_VM(repl_line), ">>> ");
273276
return 0;
274277
}
275278
}

0 commit comments

Comments
 (0)