Skip to content

Commit 0a7e01a

Browse files
committed
objtype: Rename mp_obj_class_t -> mp_obj_instance_t and move to local header.
TODO: Rename methods.
1 parent d8351ca commit 0a7e01a

2 files changed

Lines changed: 20 additions & 19 deletions

File tree

py/objtype.c

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "obj.h"
1111
#include "runtime0.h"
1212
#include "runtime.h"
13+
#include "objtype.h"
1314

1415
#if 0 // print debugging info
1516
#define DEBUG_PRINT (1)
@@ -19,21 +20,13 @@
1920
#endif
2021

2122
/******************************************************************************/
22-
// class object
23-
// creating an instance of a class makes one of these objects
24-
25-
typedef struct _mp_obj_class_t {
26-
mp_obj_base_t base;
27-
mp_map_t members;
28-
mp_obj_t subobj[];
29-
// TODO maybe cache __getattr__ and __setattr__ for efficient lookup of them
30-
} mp_obj_class_t;
23+
// instance object
3124

3225
#define is_native_type(type) ((type)->make_new != class_make_new)
3326
STATIC mp_obj_t class_make_new(mp_obj_t self_in, uint n_args, uint n_kw, const mp_obj_t *args);
3427

3528
STATIC mp_obj_t mp_obj_new_class(mp_obj_t class, uint subobjs) {
36-
mp_obj_class_t *o = m_new_obj_var(mp_obj_class_t, mp_obj_t, subobjs);
29+
mp_obj_instance_t *o = m_new_obj_var(mp_obj_instance_t, mp_obj_t, subobjs);
3730
o->base.type = class;
3831
mp_map_init(&o->members, 0);
3932
mp_seq_clear(o->subobj, 0, subobjs, sizeof(*o->subobj));
@@ -71,7 +64,7 @@ STATIC int class_count_native_bases(const mp_obj_type_t *type, const mp_obj_type
7164
// it was - because instance->subobj[0] is of that type. The only exception is when
7265
// object is not yet constructed, then we need to know base native type to construct
7366
// instance->subobj[0]. This case is handled via class_count_native_bases() though.
74-
STATIC void mp_obj_class_lookup(mp_obj_class_t *o, const mp_obj_type_t *type, qstr attr, machine_uint_t meth_offset, mp_obj_t *dest) {
67+
STATIC void mp_obj_class_lookup(mp_obj_instance_t *o, const mp_obj_type_t *type, qstr attr, machine_uint_t meth_offset, mp_obj_t *dest) {
7568
assert(dest[0] == NULL);
7669
assert(dest[1] == NULL);
7770
for (;;) {
@@ -138,7 +131,7 @@ STATIC void mp_obj_class_lookup(mp_obj_class_t *o, const mp_obj_type_t *type, qs
138131
}
139132

140133
STATIC void class_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
141-
mp_obj_class_t *self = self_in;
134+
mp_obj_instance_t *self = self_in;
142135
qstr meth = (kind == PRINT_STR) ? MP_QSTR___str__ : MP_QSTR___repr__;
143136
mp_obj_t member[2] = {MP_OBJ_NULL};
144137
mp_obj_class_lookup(self, self->base.type, meth, offsetof(mp_obj_type_t, print), member);
@@ -178,7 +171,7 @@ STATIC mp_obj_t class_make_new(mp_obj_t self_in, uint n_args, uint n_kw, const m
178171
uint num_native_bases = class_count_native_bases(self, &native_base);
179172
assert(num_native_bases < 2);
180173

181-
mp_obj_class_t *o = mp_obj_new_class(self_in, num_native_bases);
174+
mp_obj_instance_t *o = mp_obj_new_class(self_in, num_native_bases);
182175

183176
// look for __init__ function
184177
mp_obj_t init_fn[2] = {MP_OBJ_NULL};
@@ -227,7 +220,7 @@ STATIC const qstr unary_op_method_name[] = {
227220
};
228221

229222
STATIC mp_obj_t class_unary_op(int op, mp_obj_t self_in) {
230-
mp_obj_class_t *self = self_in;
223+
mp_obj_instance_t *self = self_in;
231224
qstr op_name = unary_op_method_name[op];
232225
/* Still try to lookup native slot
233226
if (op_name == 0) {
@@ -311,7 +304,7 @@ STATIC void class_convert_return_attr(mp_obj_t self, mp_obj_t member, mp_obj_t *
311304
STATIC mp_obj_t class_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
312305
// Note: For ducktyping, CPython does not look in the instance members or use
313306
// __getattr__ or __getattribute__. It only looks in the class dictionary.
314-
mp_obj_class_t *lhs = lhs_in;
307+
mp_obj_instance_t *lhs = lhs_in;
315308
qstr op_name = binary_op_method_name[op];
316309
/* Still try to lookup native slot
317310
if (op_name == 0) {
@@ -335,7 +328,7 @@ STATIC mp_obj_t class_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
335328

336329
STATIC void class_load_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
337330
// logic: look in obj members then class locals (TODO check this against CPython)
338-
mp_obj_class_t *self = self_in;
331+
mp_obj_instance_t *self = self_in;
339332

340333
mp_map_elem_t *elem = mp_map_lookup(&self->members, MP_OBJ_NEW_QSTR(attr), MP_MAP_LOOKUP);
341334
if (elem != NULL) {
@@ -388,7 +381,7 @@ STATIC void class_load_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
388381
}
389382

390383
STATIC bool class_store_attr(mp_obj_t self_in, qstr attr, mp_obj_t value) {
391-
mp_obj_class_t *self = self_in;
384+
mp_obj_instance_t *self = self_in;
392385

393386
#if MICROPY_ENABLE_PROPERTY
394387
// for property, we need to do a lookup first in the class dict
@@ -422,7 +415,7 @@ STATIC bool class_store_attr(mp_obj_t self_in, qstr attr, mp_obj_t value) {
422415
}
423416

424417
STATIC mp_obj_t class_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
425-
mp_obj_class_t *self = self_in;
418+
mp_obj_instance_t *self = self_in;
426419
mp_obj_t member[2] = {MP_OBJ_NULL};
427420
uint meth_args;
428421
if (value == MP_OBJ_NULL) {
@@ -455,7 +448,7 @@ STATIC mp_obj_t class_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
455448
}
456449

457450
STATIC mp_obj_t class_call(mp_obj_t self_in, uint n_args, uint n_kw, const mp_obj_t *args) {
458-
mp_obj_class_t *self = self_in;
451+
mp_obj_instance_t *self = self_in;
459452
mp_obj_t member[2] = {MP_OBJ_NULL};
460453
mp_obj_class_lookup(self, self->base.type, MP_QSTR___call__, offsetof(mp_obj_type_t, call), member);
461454
if (member[0] == MP_OBJ_NULL) {

py/objtype.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// instance object
2+
// creating an instance of a class makes one of these objects
3+
typedef struct _mp_obj_instance_t {
4+
mp_obj_base_t base;
5+
mp_map_t members;
6+
mp_obj_t subobj[];
7+
// TODO maybe cache __getattr__ and __setattr__ for efficient lookup of them
8+
} mp_obj_instance_t;

0 commit comments

Comments
 (0)