Skip to content

Commit ec63cce

Browse files
committed
Add simple support for C modules.
1 parent 92c0656 commit ec63cce

2 files changed

Lines changed: 19 additions & 6 deletions

File tree

py/runtime.c

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1499,14 +1499,18 @@ void rt_load_method(py_obj_t base, qstr attr, py_obj_t *dest) {
14991499

15001500
void rt_store_attr(py_obj_t base, qstr attr, py_obj_t value) {
15011501
DEBUG_OP_printf("store attr %p.%s <- %p\n", base, qstr_str(attr), value);
1502-
if (IS_O(base, O_OBJ)) {
1502+
if (IS_O(base, O_CLASS)) {
1503+
// TODO CPython allows STORE_ATTR to a class, but is this the correct implementation?
1504+
py_obj_base_t *o = base;
1505+
py_qstr_map_lookup(o->u_class.locals, attr, true)->value = value;
1506+
} else if (IS_O(base, O_OBJ)) {
15031507
// logic: look in class locals (no add) then obj members (add) (TODO check this against CPython)
15041508
py_obj_base_t *o = base;
15051509
py_map_elem_t *elem = py_qstr_map_lookup(o->u_obj.class->u_class.locals, attr, false);
15061510
if (elem != NULL) {
15071511
elem->value = value;
15081512
} else {
1509-
elem = py_qstr_map_lookup(o->u_obj.class->u_class.locals, attr, true)->value = value;
1513+
py_qstr_map_lookup(o->u_obj.members, attr, true)->value = value;
15101514
}
15111515
} else {
15121516
printf("?AttributeError: '%s' object has no attribute '%s'\n", py_obj_get_type_str(base), qstr_str(attr));
@@ -1618,3 +1622,13 @@ void rt_f_vector(rt_fun_kind_t fun_kind) {
16181622
(rt_f_table[fun_kind])();
16191623
}
16201624
*/
1625+
1626+
// temporary way of making C modules
1627+
// hack: use class to mimic a module
1628+
1629+
py_obj_t py_module_new() {
1630+
py_obj_base_t *o = m_new(py_obj_base_t, 1);
1631+
o->kind = O_CLASS;
1632+
o->u_class.locals = py_map_new(MAP_QSTR, 0);
1633+
return o;
1634+
}

py/runtime.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,10 @@ int rt_get_unique_code_id(bool is_main_module);
9595
void rt_assign_byte_code(int unique_code_id, byte *code, uint len, int n_args, int n_locals, int n_stack, bool is_generator);
9696
void rt_assign_native_code(int unique_code_id, py_fun_t f, uint len, int n_args);
9797
void rt_assign_inline_asm_code(int unique_code_id, py_fun_t f, uint len, int n_args);
98-
py_fun_t rt_get_code(qstr id);
9998
void py_obj_print(py_obj_t o);
10099
int rt_is_true(py_obj_t arg);
101100
int rt_get_int(py_obj_t arg);
102101
py_obj_t rt_load_const_str(qstr qstr);
103-
//py_obj_t rt_load_const_code(qstr qstr);
104102
py_obj_t rt_load_name(qstr qstr);
105103
py_obj_t rt_load_global(qstr qstr);
106104
py_obj_t rt_load_build_class();
@@ -118,8 +116,6 @@ py_obj_t rt_call_function_0(py_obj_t fun);
118116
py_obj_t rt_call_function_1(py_obj_t fun, py_obj_t arg);
119117
py_obj_t rt_call_function_2(py_obj_t fun, py_obj_t arg1, py_obj_t arg2);
120118
py_obj_t rt_call_function_n(py_obj_t fun, int n_args, const py_obj_t *args);
121-
py_obj_t rt_call_method_1(py_obj_t fun, py_obj_t self);
122-
py_obj_t rt_call_method_2(py_obj_t fun, py_obj_t self, py_obj_t arg);
123119
py_obj_t rt_call_method_n(int n_args, const py_obj_t *args);
124120
py_obj_t rt_build_tuple(int n_args, py_obj_t *items);
125121
py_obj_t rt_build_list(int n_args, py_obj_t *items);
@@ -134,3 +130,6 @@ void rt_store_attr(py_obj_t base, qstr attr, py_obj_t val);
134130
void rt_store_subscr(py_obj_t base, py_obj_t index, py_obj_t val);
135131
py_obj_t rt_getiter(py_obj_t o);
136132
py_obj_t rt_iternext(py_obj_t o);
133+
134+
// temporary way of making C modules
135+
py_obj_t py_module_new();

0 commit comments

Comments
 (0)