summaryrefslogtreecommitdiffstatshomepage
path: root/py/objclass.c
diff options
context:
space:
mode:
authorDamien <damien.p.george@gmail.com>2013-12-21 18:17:45 +0000
committerDamien <damien.p.george@gmail.com>2013-12-21 18:17:45 +0000
commitd99b05282d14ceb0163cbcd059aa37bdb415af43 (patch)
tree978135f9fe83d3c4d5b3c95f84cb104c0092936a /py/objclass.c
parente2880aa2fdc75298df487df7519d483acb03959c (diff)
downloadmicropython-d99b05282d14ceb0163cbcd059aa37bdb415af43.tar.gz
micropython-d99b05282d14ceb0163cbcd059aa37bdb415af43.zip
Change object representation from 1 big union to individual structs.
A big change. Micro Python objects are allocated as individual structs with the first element being a pointer to the type information (which is itself an object). This scheme follows CPython. Much more flexible, not necessarily slower, uses same heap memory, and can allocate objects statically. Also change name prefix, from py_ to mp_ (mp for Micro Python).
Diffstat (limited to 'py/objclass.c')
-rw-r--r--py/objclass.c85
1 files changed, 85 insertions, 0 deletions
diff --git a/py/objclass.c b/py/objclass.c
new file mode 100644
index 0000000000..52e43724f7
--- /dev/null
+++ b/py/objclass.c
@@ -0,0 +1,85 @@
+#include <stdlib.h>
+#include <stdint.h>
+#include <string.h>
+#include <assert.h>
+
+#include "nlr.h"
+#include "misc.h"
+#include "mpconfig.h"
+#include "obj.h"
+#include "runtime.h"
+#include "map.h"
+
+typedef struct _mp_obj_class_t {
+ mp_obj_base_t base;
+ mp_map_t *locals;
+} mp_obj_class_t;
+
+// args are in reverse order in the array
+mp_obj_t class_call_n(mp_obj_t self_in, int n_args, const mp_obj_t *args) {
+ // instantiate an instance of a class
+
+ mp_obj_class_t *self = self_in;
+
+ // make instance
+ mp_obj_t o = mp_obj_new_instance(self_in);
+
+ // look for __init__ function
+ mp_map_elem_t *init_fn = mp_qstr_map_lookup(self->locals, qstr_from_str_static("__init__"), false);
+
+ if (init_fn != NULL) {
+ // call __init__ function
+ mp_obj_t init_ret;
+ if (n_args == 0) {
+ init_ret = rt_call_function_n(init_fn->value, 1, (mp_obj_t*)&o);
+ } else {
+ mp_obj_t *args2 = m_new(mp_obj_t, n_args + 1);
+ memcpy(args2, args, n_args * sizeof(mp_obj_t));
+ args2[n_args] = o;
+ init_ret = rt_call_function_n(init_fn->value, n_args + 1, args2);
+ m_free(args2);
+ }
+ if (init_ret != mp_const_none) {
+ nlr_jump(mp_obj_new_exception_msg_1_arg(rt_q_TypeError, "__init__() should return None, not '%s'", mp_obj_get_type_str(init_ret)));
+ }
+
+ } else {
+ // TODO
+ if (n_args != 0) {
+ nlr_jump(mp_obj_new_exception_msg_1_arg(rt_q_TypeError, "function takes 0 positional arguments but %d were given", (void*)(machine_int_t)n_args));
+ }
+ }
+
+ return o;
+}
+
+mp_map_t *mp_obj_class_get_locals(mp_obj_t self_in) {
+ assert(MP_OBJ_IS_TYPE(self_in, &class_type));
+ mp_obj_class_t *self = self_in;
+ return self->locals;
+}
+
+const mp_obj_type_t class_type = {
+ { &mp_const_type },
+ "class",
+ NULL, // print
+ class_call_n, // call_n
+ NULL, // unary_op
+ NULL, // binary_op
+ NULL, // getiter
+ NULL, // iternext
+ {{NULL, NULL},}, // method list
+};
+
+mp_obj_t mp_obj_new_class(mp_map_t *class_locals) {
+ mp_obj_class_t *o = m_new_obj(mp_obj_class_t);
+ o->base.type = &class_type;
+ o->locals = class_locals;
+ return o;
+}
+
+// temporary way of making C modules
+// hack: use class to mimic a module
+mp_obj_t mp_module_new(void) {
+ return mp_obj_new_class(mp_map_new(MP_MAP_QSTR, 0));
+}