py: Use mp_locals/mp_globals accessor funcs instead of MP_STATE_CTX.
To improve maintainability of the code.
This commit is contained in:
parent
f4ee1ba9b4
commit
707f16b05c
17
py/runtime.c
17
py/runtime.c
@ -97,7 +97,8 @@ void mp_init(void) {
|
|||||||
mp_obj_dict_store(MP_OBJ_FROM_PTR(&MP_STATE_VM(dict_main)), MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR___main__));
|
mp_obj_dict_store(MP_OBJ_FROM_PTR(&MP_STATE_VM(dict_main)), MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR___main__));
|
||||||
|
|
||||||
// locals = globals for outer module (see Objects/frameobject.c/PyFrame_New())
|
// locals = globals for outer module (see Objects/frameobject.c/PyFrame_New())
|
||||||
MP_STATE_CTX(dict_locals) = MP_STATE_CTX(dict_globals) = &MP_STATE_VM(dict_main);
|
mp_locals_set(&MP_STATE_VM(dict_main));
|
||||||
|
mp_globals_set(&MP_STATE_VM(dict_main));
|
||||||
|
|
||||||
#if MICROPY_CAN_OVERRIDE_BUILTINS
|
#if MICROPY_CAN_OVERRIDE_BUILTINS
|
||||||
// start with no extensions to builtins
|
// start with no extensions to builtins
|
||||||
@ -136,8 +137,8 @@ mp_obj_t mp_load_name(qstr qst) {
|
|||||||
// logic: search locals, globals, builtins
|
// logic: search locals, globals, builtins
|
||||||
DEBUG_OP_printf("load name %s\n", qstr_str(qst));
|
DEBUG_OP_printf("load name %s\n", qstr_str(qst));
|
||||||
// If we're at the outer scope (locals == globals), dispatch to load_global right away
|
// If we're at the outer scope (locals == globals), dispatch to load_global right away
|
||||||
if (MP_STATE_CTX(dict_locals) != MP_STATE_CTX(dict_globals)) {
|
if (mp_locals_get() != mp_globals_get()) {
|
||||||
mp_map_elem_t *elem = mp_map_lookup(&MP_STATE_CTX(dict_locals)->map, MP_OBJ_NEW_QSTR(qst), MP_MAP_LOOKUP);
|
mp_map_elem_t *elem = mp_map_lookup(&mp_locals_get()->map, MP_OBJ_NEW_QSTR(qst), MP_MAP_LOOKUP);
|
||||||
if (elem != NULL) {
|
if (elem != NULL) {
|
||||||
return elem->value;
|
return elem->value;
|
||||||
}
|
}
|
||||||
@ -148,7 +149,7 @@ mp_obj_t mp_load_name(qstr qst) {
|
|||||||
mp_obj_t mp_load_global(qstr qst) {
|
mp_obj_t mp_load_global(qstr qst) {
|
||||||
// logic: search globals, builtins
|
// logic: search globals, builtins
|
||||||
DEBUG_OP_printf("load global %s\n", qstr_str(qst));
|
DEBUG_OP_printf("load global %s\n", qstr_str(qst));
|
||||||
mp_map_elem_t *elem = mp_map_lookup(&MP_STATE_CTX(dict_globals)->map, MP_OBJ_NEW_QSTR(qst), MP_MAP_LOOKUP);
|
mp_map_elem_t *elem = mp_map_lookup(&mp_globals_get()->map, MP_OBJ_NEW_QSTR(qst), MP_MAP_LOOKUP);
|
||||||
if (elem == NULL) {
|
if (elem == NULL) {
|
||||||
#if MICROPY_CAN_OVERRIDE_BUILTINS
|
#if MICROPY_CAN_OVERRIDE_BUILTINS
|
||||||
if (MP_STATE_VM(mp_module_builtins_override_dict) != NULL) {
|
if (MP_STATE_VM(mp_module_builtins_override_dict) != NULL) {
|
||||||
@ -188,24 +189,24 @@ mp_obj_t mp_load_build_class(void) {
|
|||||||
|
|
||||||
void mp_store_name(qstr qst, mp_obj_t obj) {
|
void mp_store_name(qstr qst, mp_obj_t obj) {
|
||||||
DEBUG_OP_printf("store name %s <- %p\n", qstr_str(qst), obj);
|
DEBUG_OP_printf("store name %s <- %p\n", qstr_str(qst), obj);
|
||||||
mp_obj_dict_store(MP_OBJ_FROM_PTR(MP_STATE_CTX(dict_locals)), MP_OBJ_NEW_QSTR(qst), obj);
|
mp_obj_dict_store(MP_OBJ_FROM_PTR(mp_locals_get()), MP_OBJ_NEW_QSTR(qst), obj);
|
||||||
}
|
}
|
||||||
|
|
||||||
void mp_delete_name(qstr qst) {
|
void mp_delete_name(qstr qst) {
|
||||||
DEBUG_OP_printf("delete name %s\n", qstr_str(qst));
|
DEBUG_OP_printf("delete name %s\n", qstr_str(qst));
|
||||||
// TODO convert KeyError to NameError if qst not found
|
// TODO convert KeyError to NameError if qst not found
|
||||||
mp_obj_dict_delete(MP_OBJ_FROM_PTR(MP_STATE_CTX(dict_locals)), MP_OBJ_NEW_QSTR(qst));
|
mp_obj_dict_delete(MP_OBJ_FROM_PTR(mp_locals_get()), MP_OBJ_NEW_QSTR(qst));
|
||||||
}
|
}
|
||||||
|
|
||||||
void mp_store_global(qstr qst, mp_obj_t obj) {
|
void mp_store_global(qstr qst, mp_obj_t obj) {
|
||||||
DEBUG_OP_printf("store global %s <- %p\n", qstr_str(qst), obj);
|
DEBUG_OP_printf("store global %s <- %p\n", qstr_str(qst), obj);
|
||||||
mp_obj_dict_store(MP_OBJ_FROM_PTR(MP_STATE_CTX(dict_globals)), MP_OBJ_NEW_QSTR(qst), obj);
|
mp_obj_dict_store(MP_OBJ_FROM_PTR(mp_globals_get()), MP_OBJ_NEW_QSTR(qst), obj);
|
||||||
}
|
}
|
||||||
|
|
||||||
void mp_delete_global(qstr qst) {
|
void mp_delete_global(qstr qst) {
|
||||||
DEBUG_OP_printf("delete global %s\n", qstr_str(qst));
|
DEBUG_OP_printf("delete global %s\n", qstr_str(qst));
|
||||||
// TODO convert KeyError to NameError if qst not found
|
// TODO convert KeyError to NameError if qst not found
|
||||||
mp_obj_dict_delete(MP_OBJ_FROM_PTR(MP_STATE_CTX(dict_globals)), MP_OBJ_NEW_QSTR(qst));
|
mp_obj_dict_delete(MP_OBJ_FROM_PTR(mp_globals_get()), MP_OBJ_NEW_QSTR(qst));
|
||||||
}
|
}
|
||||||
|
|
||||||
mp_obj_t mp_unary_op(mp_uint_t op, mp_obj_t arg) {
|
mp_obj_t mp_unary_op(mp_uint_t op, mp_obj_t arg) {
|
||||||
|
16
py/vm.c
16
py/vm.c
@ -285,12 +285,12 @@ dispatch_loop:
|
|||||||
DECODE_QSTR;
|
DECODE_QSTR;
|
||||||
mp_obj_t key = MP_OBJ_NEW_QSTR(qst);
|
mp_obj_t key = MP_OBJ_NEW_QSTR(qst);
|
||||||
mp_uint_t x = *ip;
|
mp_uint_t x = *ip;
|
||||||
if (x < MP_STATE_CTX(dict_locals)->map.alloc && MP_STATE_CTX(dict_locals)->map.table[x].key == key) {
|
if (x < mp_locals_get()->map.alloc && mp_locals_get()->map.table[x].key == key) {
|
||||||
PUSH(MP_STATE_CTX(dict_locals)->map.table[x].value);
|
PUSH(mp_locals_get()->map.table[x].value);
|
||||||
} else {
|
} else {
|
||||||
mp_map_elem_t *elem = mp_map_lookup(&MP_STATE_CTX(dict_locals)->map, MP_OBJ_NEW_QSTR(qst), MP_MAP_LOOKUP);
|
mp_map_elem_t *elem = mp_map_lookup(&mp_locals_get()->map, MP_OBJ_NEW_QSTR(qst), MP_MAP_LOOKUP);
|
||||||
if (elem != NULL) {
|
if (elem != NULL) {
|
||||||
*(byte*)ip = (elem - &MP_STATE_CTX(dict_locals)->map.table[0]) & 0xff;
|
*(byte*)ip = (elem - &mp_locals_get()->map.table[0]) & 0xff;
|
||||||
PUSH(elem->value);
|
PUSH(elem->value);
|
||||||
} else {
|
} else {
|
||||||
PUSH(mp_load_name(MP_OBJ_QSTR_VALUE(key)));
|
PUSH(mp_load_name(MP_OBJ_QSTR_VALUE(key)));
|
||||||
@ -314,12 +314,12 @@ dispatch_loop:
|
|||||||
DECODE_QSTR;
|
DECODE_QSTR;
|
||||||
mp_obj_t key = MP_OBJ_NEW_QSTR(qst);
|
mp_obj_t key = MP_OBJ_NEW_QSTR(qst);
|
||||||
mp_uint_t x = *ip;
|
mp_uint_t x = *ip;
|
||||||
if (x < MP_STATE_CTX(dict_globals)->map.alloc && MP_STATE_CTX(dict_globals)->map.table[x].key == key) {
|
if (x < mp_globals_get()->map.alloc && mp_globals_get()->map.table[x].key == key) {
|
||||||
PUSH(MP_STATE_CTX(dict_globals)->map.table[x].value);
|
PUSH(mp_globals_get()->map.table[x].value);
|
||||||
} else {
|
} else {
|
||||||
mp_map_elem_t *elem = mp_map_lookup(&MP_STATE_CTX(dict_globals)->map, MP_OBJ_NEW_QSTR(qst), MP_MAP_LOOKUP);
|
mp_map_elem_t *elem = mp_map_lookup(&mp_globals_get()->map, MP_OBJ_NEW_QSTR(qst), MP_MAP_LOOKUP);
|
||||||
if (elem != NULL) {
|
if (elem != NULL) {
|
||||||
*(byte*)ip = (elem - &MP_STATE_CTX(dict_globals)->map.table[0]) & 0xff;
|
*(byte*)ip = (elem - &mp_globals_get()->map.table[0]) & 0xff;
|
||||||
PUSH(elem->value);
|
PUSH(elem->value);
|
||||||
} else {
|
} else {
|
||||||
PUSH(mp_load_global(MP_OBJ_QSTR_VALUE(key)));
|
PUSH(mp_load_global(MP_OBJ_QSTR_VALUE(key)));
|
||||||
|
Loading…
Reference in New Issue
Block a user