Skip to content
This repository was archived by the owner on Apr 25, 2021. It is now read-only.

Commit f41fb93

Browse files
committed
xxx_hook_obj 가 gc 되는 버그 고침
1 parent c09ad1f commit f41fb93

5 files changed

Lines changed: 27 additions & 23 deletions

File tree

src/main.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ void debug_printer(void *self, const char *buf, size_t len) {
2121

2222
const struct _mp_print_t debug_print = {NULL, debug_printer};
2323

24+
void mp_init_port() {
25+
MP_STATE_PORT(object_hook_obj) = mp_const_none;
26+
MP_STATE_PORT(signal_hook_obj) = mp_const_none;
27+
MP_STATE_PORT(stdin_hook_obj) = mp_const_none;
28+
MP_STATE_PORT(stdout_hook_obj) = mp_const_none;
29+
}
30+
2431
void do_str(const char *src, mp_parse_input_kind_t input_kind) {
2532
nlr_buf_t nlr;
2633
if (nlr_push(&nlr) == 0) {
@@ -61,6 +68,7 @@ int main(int argc, char **argv) {
6168
gc_init(&_ram_start, ((void *)&_ram_start) + ram_size);
6269

6370
mp_init();
71+
mp_init_port();
6472
mp_obj_list_init(mp_sys_path, 0);
6573
mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR_));
6674
mp_obj_list_init(mp_sys_argv, 0);

src/modmachine.c

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@
44
#include "extmod/machine_mem.h"
55
#include "syscall.h"
66

7-
mp_obj_t signal_hook_obj = mp_const_none;
8-
mp_obj_t stdout_hook_obj = mp_const_none;
9-
mp_obj_t stdin_hook_obj = mp_const_none;
10-
117

128
mp_obj_t machine_debug(mp_obj_t obj) {
139
size_t len = 0;
@@ -21,23 +17,23 @@ MP_DEFINE_CONST_FUN_OBJ_1(machine_debug_obj, machine_debug);
2117

2218

2319
STATIC mp_obj_t machine_hook_signal(mp_obj_t hook_obj) {
24-
signal_hook_obj = hook_obj;
20+
MP_STATE_PORT(signal_hook_obj) = hook_obj;
2521
return hook_obj;
2622
}
2723

2824
MP_DEFINE_CONST_FUN_OBJ_1(machine_hook_signal_obj, machine_hook_signal);
2925

3026

3127
STATIC mp_obj_t machine_hook_stdout(mp_obj_t hook_obj) {
32-
stdout_hook_obj = hook_obj;
28+
MP_STATE_PORT(stdout_hook_obj) = hook_obj;
3329
return hook_obj;
3430
}
3531

3632
MP_DEFINE_CONST_FUN_OBJ_1(machine_hook_stdout_obj, machine_hook_stdout);
3733

3834

3935
STATIC mp_obj_t machine_hook_stdin(mp_obj_t hook_obj) {
40-
stdin_hook_obj = hook_obj;
36+
MP_STATE_PORT(stdin_hook_obj) = hook_obj;
4137
return hook_obj;
4238
}
4339

src/moduvalue.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@
88
mp_obj_t wrap_result(int code);
99

1010

11-
mp_obj_t object_hook_obj = mp_const_none;
12-
13-
1411
STATIC mp_obj_t uvalue_dispose(mp_obj_t raw_value_obj) {
1512
uvalue_obj_t *self = MP_OBJ_TO_PTR(raw_value_obj);
1613
if (self->value != mp_const_none) {
@@ -66,16 +63,16 @@ mp_obj_t uvalue_new(mp_obj_t value) {
6663

6764
mp_obj_t oc_create_value(mp_obj_t raw_value_obj) {
6865
mp_obj_t value_obj = uvalue_new(raw_value_obj);
69-
if (object_hook_obj == mp_const_none) {
66+
if (MP_STATE_PORT(object_hook_obj) == mp_const_none) {
7067
return value_obj;
7168
}
7269

73-
return mp_call_function_1(object_hook_obj, value_obj);
70+
return mp_call_function_1(MP_STATE_PORT(object_hook_obj), value_obj);
7471
}
7572

7673

7774
STATIC mp_obj_t uvalue_hook_value(mp_obj_t hook_obj) {
78-
object_hook_obj = hook_obj;
75+
MP_STATE_PORT(object_hook_obj) = hook_obj;
7976
return hook_obj;
8077
}
8178

src/mpconfigport.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,11 @@ extern const struct _mp_obj_type_t mp_type_SystemError;
232232
#define MICROPY_PORT_CONSTANTS
233233

234234
#define MICROPY_PORT_ROOT_POINTERS \
235-
const char *readline_hist[8];
235+
const char *readline_hist[8]; \
236+
mp_obj_t signal_hook_obj; \
237+
mp_obj_t stdin_hook_obj; \
238+
mp_obj_t stdout_hook_obj; \
239+
mp_obj_t object_hook_obj; \
236240

237241
#define mp_type_fileio mp_type_vfs_openpie_fileio
238242
#define mp_type_textio mp_type_vfs_openpie_textio

src/mphalport.c

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,15 @@
44

55
#include "py/runtime.h"
66
#include "py/objstr.h"
7+
#include "machine.h"
78
#include "mphalport.h"
89
#include "syscall.h"
910

10-
extern mp_obj_t signal_hook_obj;
11-
extern mp_obj_t stdin_hook_obj;
12-
extern mp_obj_t stdout_hook_obj;
13-
1411
int mp_hal_stdin_rx_chr(void) {
15-
if (stdin_hook_obj == mp_const_none) {
12+
if (MP_STATE_PORT(stdin_hook_obj) == mp_const_none) {
1613
return 0;
1714
} else {
18-
mp_obj_t obj = mp_call_function_0(stdin_hook_obj);
15+
mp_obj_t obj = mp_call_function_0(MP_STATE_PORT(stdin_hook_obj));
1916
return (int) mp_obj_get_int(obj);
2017
}
2118
}
@@ -25,11 +22,11 @@ void mp_hal_stdout_tx_str(const char *str) {
2522
}
2623

2724
void mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) {
28-
if (stdout_hook_obj == mp_const_none) {
25+
if (MP_STATE_PORT(stdout_hook_obj) == mp_const_none) {
2926
__syscall2(SYS_DEBUG, (int)str, (int)len);
3027
} else {
3128
mp_obj_t str_obj = mp_obj_new_str(str, len);
32-
mp_call_function_1(stdout_hook_obj, str_obj);
29+
mp_call_function_1(MP_STATE_PORT(stdout_hook_obj), str_obj);
3330
}
3431
}
3532

@@ -44,7 +41,9 @@ void mp_hal_delay_ms(mp_uint_t ms) {
4441
mp_handle_pending();
4542
int32_t ticks = (ms - elapsed) / (1000 / 20);
4643
mp_obj_t tick_obj = mp_obj_new_int(ticks < 0? 0 : ticks);
47-
mp_call_function_1(signal_hook_obj, tick_obj);
44+
if (MP_STATE_PORT(signal_hook_obj) == mp_const_none)
45+
__fatal_error("signal_hook is None");
46+
mp_call_function_1(MP_STATE_PORT(signal_hook_obj), tick_obj);
4847
elapsed = mp_hal_ticks_ms() - start;
4948
}
5049
}

0 commit comments

Comments
 (0)