aboutsummaryrefslogtreecommitdiff
path: root/libgpython/runtime
diff options
context:
space:
mode:
Diffstat (limited to 'libgpython/runtime')
-rw-r--r--libgpython/runtime/py-obj_class.c88
-rw-r--r--libgpython/runtime/py-obj_classmethod.c17
-rw-r--r--libgpython/runtime/py-obj_staticmethod.c6
-rw-r--r--libgpython/runtime/py-runtime.c63
-rw-r--r--libgpython/runtime/py-type-utils.c15
5 files changed, 111 insertions, 78 deletions
diff --git a/libgpython/runtime/py-obj_class.c b/libgpython/runtime/py-obj_class.c
index d002f8b2739..ab501809867 100644
--- a/libgpython/runtime/py-obj_class.c
+++ b/libgpython/runtime/py-obj_class.c
@@ -42,8 +42,32 @@ void gpy_object_classobj_init_decl_attribs (const void * self,
for (idx = 0; attribs[idx] != NULL; ++idx)
{
gpy_object_attrib_t * i = attribs[idx];
- unsigned char * x = selfptr + (i->offset);
- x = (unsigned char *) &(i->addr);
+ unsigned char * typeoffs = selfptr + (i->offset);
+
+ gpy_object_t ** offs = (gpy_object_t **) typeoffs;
+ if (i->addr)
+ {
+ gpy_object_t * attrib = (gpy_object_t *)i->addr;
+ *offs = i->addr;
+ }
+ else
+ *offs = NULL;
+ }
+}
+
+static
+void gpy_object_classobj_methodattribs_addself (gpy_object_attrib_t ** attribs,
+ gpy_object_t * self)
+{
+ int idx;
+ for (idx = 0; attribs[idx] != NULL; ++idx)
+ {
+ gpy_object_attrib_t * i = attribs[idx];
+ if (i->addr)
+ {
+ gpy_object_t * att = (gpy_object_t *) i->addr;
+ gpy_object_classmethod_inherit_self (att, self);
+ }
}
}
@@ -78,10 +102,9 @@ gpy_object_t * gpy_object_classobj_new (gpy_typedef_t * type,
/* we need to walk though the field_init here */
unsigned char * __field_init__ = gpy_rr_eval_attrib_reference (retval, "__field_init__");
gpy_object_t * field_init = *((gpy_object_t **) __field_init__);
- unsigned char * codeaddr = gpy_object_staticmethod_getaddr (field_init);
+ unsigned char * codeaddr = gpy_object_classmethod_getaddr (field_init);
gpy_assert (codeaddr);
- debug ("calling the __init__!\n");
__field_init_ptr c = (__field_init_ptr)codeaddr;
c (self);
@@ -117,25 +140,58 @@ void gpy_object_classobj_print (gpy_object_t * self, FILE *fd, bool newline)
fprintf (fd, "\n");
}
+static
+gpy_object_t ** gpy_object_classobj_setupargs (gpy_object_t ** args,
+ gpy_object_t * self)
+{
+ int idx = 0;
+ gpy_object_t ** ptr;
+ for (ptr = args; *ptr != NULL; ++ptr)
+ idx ++;
+
+ gpy_object_t ** newargs = calloc (idx+2, sizeof (gpy_object_t *));
+ *newargs = self;
+ gpy_object_t ** newargsptr = newargs;
+ newargsptr++;
+
+ for (ptr = args; *ptr != NULL; ++ptr)
+ {
+ *newargsptr = *ptr;
+ newargsptr++;
+ }
+ *newargsptr = NULL;
+
+ return newargs;
+}
+
gpy_object_t * gpy_object_classobj_call (gpy_object_t * self,
gpy_object_t ** args)
{
- gpy_object_t * retval = NULL_OBJECT;
- gpy_assert (self->T == TYPE_OBJECT_DECL);
+ gpy_object_t * retval = NULL_OBJECT;
+ gpy_assert (self->T == TYPE_OBJECT_DECL);
+
+ gpy_typedef_t * type = self->o.object_state->definition;
+ void * oldstate = self->o.object_state->state;
- gpy_typedef_t * type = self->o.object_state->definition;
- void * state = self->o.object_state->state;
- retval = gpy_create_object_state (type, state);
- gpy_assert (retval);
+ void * newstate = malloc (type->state_size);
+ memcpy (newstate, oldstate, type->state_size);
+ retval = gpy_create_object_state (type, newstate);
+ gpy_assert (retval);
- unsigned char * __init__ = gpy_rr_eval_attrib_reference (retval, "__init__");
- gpy_object_t * init = *((gpy_object_t **)__init__);
- unsigned char * codeaddr = gpy_object_staticmethod_getaddr (init);
- gpy_assert (codeaddr);
+ unsigned char * __init__ = gpy_rr_eval_attrib_reference (retval, "__init__");
+ gpy_object_t * init = *((gpy_object_t **) __init__);
-
+ gpy_assert (init->T == TYPE_OBJECT_DECL);
+ gpy_typedef_t * calltype = init->o.object_state->definition;
+ if (type->tp_call)
+ {
+ gpy_object_t ** arguments = gpy_object_classobj_setupargs (args, retval);
+ calltype->tp_call (init, arguments);
+ }
+ else
+ fatal ("name is not callable!\n");
- return retval;
+ return retval;
}
static struct gpy_typedef_t class_obj = {
diff --git a/libgpython/runtime/py-obj_classmethod.c b/libgpython/runtime/py-obj_classmethod.c
index 815c017b886..b2a80cad1bd 100644
--- a/libgpython/runtime/py-obj_classmethod.c
+++ b/libgpython/runtime/py-obj_classmethod.c
@@ -31,13 +31,13 @@ along with GCC; see the file COPYING3. If not see
#include <gpython/objects.h>
struct gpy_object_classmethod_t {
- const unsigned char * code;
- const char * identifier;
+ unsigned char * code;
+ char * identifier;
unsigned int nargs;
};
gpy_object_t * gpy_object_classmethod_new (gpy_typedef_t * type,
- gpy_object_t ** args)
+ gpy_object_t ** args)
{
gpy_object_t * retval = NULL_OBJECT;
@@ -76,16 +76,19 @@ void gpy_object_classmethod_print (gpy_object_t * self, FILE *fd, bool newline)
}
gpy_object_t * gpy_object_classmethod_call (gpy_object_t * self,
- gpy_object_t ** args)
+ gpy_object_t ** args)
{
gpy_object_t * retval = NULL_OBJECT;
gpy_assert (self->T == TYPE_OBJECT_DECL);
struct gpy_object_classmethod_t * state = self->o.object_state->state;
- if (!state->code)
+ if (state->code)
{
- fndecl fnptr = (fndecl)state->code;
- fnptr (args);
+ classmethod_fndecl fnptr = (classmethod_fndecl)state->code;
+ gpy_object_t ** ptr = args;
+ gpy_object_t * class = *ptr;
+ ptr++;
+ fnptr (class, ptr);
}
return retval;
}
diff --git a/libgpython/runtime/py-obj_staticmethod.c b/libgpython/runtime/py-obj_staticmethod.c
index 0f287e18f9e..c7f791466ad 100644
--- a/libgpython/runtime/py-obj_staticmethod.c
+++ b/libgpython/runtime/py-obj_staticmethod.c
@@ -31,8 +31,8 @@ along with GCC; see the file COPYING3. If not see
#include <gpython/objects.h>
struct gpy_object_staticmethod_t {
- const unsigned char * code;
- const char * identifier;
+ unsigned char * code;
+ char * identifier;
unsigned int nargs;
};
@@ -85,7 +85,7 @@ gpy_object_t * gpy_object_staticmethod_call (gpy_object_t * self,
struct gpy_object_staticmethod_t * state = self->o.object_state->state;
if (!state->code)
{
- fndecl fnptr = (fndecl)state->code;
+ staticmethod_fndecl fnptr = (staticmethod_fndecl)state->code;
fnptr (args);
}
return retval;
diff --git a/libgpython/runtime/py-runtime.c b/libgpython/runtime/py-runtime.c
index a518ba9db69..6dfcd55f001 100644
--- a/libgpython/runtime/py-runtime.c
+++ b/libgpython/runtime/py-runtime.c
@@ -81,7 +81,7 @@ void gpy_rr_extend_runtime_stack (int nslots)
__GPY_GLOBL_RR_STACK = gpy_realloc (__GPY_GLOBL_RR_STACK, size);
__GPY_GLOBL_RR_STACK_POINTER = __GPY_GLOBL_RR_STACK;
- __GPY_GLOBL_RR_STACK2_POINTER += 3+nslots;
+ __GPY_GLOBL_RR_STACK_POINTER += 3 + nslots;
}
void gpy_rr_init_runtime (void)
@@ -97,7 +97,7 @@ void gpy_rr_cleanup_final (void)
mpfr_free_cache ();
}
-gpy_object_attrib_t * gpy_rr_fold_attribute (const char * identifier,
+gpy_object_attrib_t * gpy_rr_fold_attribute (const unsigned char * identifier,
unsigned char * code_addr,
unsigned int offset)
{
@@ -168,15 +168,10 @@ gpy_object_t * gpy_rr_fold_class_decl (gpy_object_attrib_t ** attribs,
args[2] = &a3;
args[3] = &a4;
- gpy_typedef_t * def = (gpy_typedef_t *)
- __GPY_GLOBL_PRIMITIVES->vector[2];
- gpy_assert (def);
-
+ gpy_typedef_t * def = __gpy_class_type_node;
retval = def->tp_new (def, args);
gpy_free (args);
- debug ("initilized class object <%p> to <%s>!\n",
- (void*)retval, identifier);
gpy_assert (retval->T == TYPE_OBJECT_DECL);
return retval;
@@ -212,15 +207,10 @@ gpy_object_t * gpy_rr_fold_staticmethod_decl (const char * identifier,
args[2] = &a3;
args[3] = &a4;
- gpy_typedef_t * def = (gpy_typedef_t *)
- __GPY_GLOBL_PRIMITIVES->vector[1];
- gpy_assert (def);
-
+ gpy_typedef_t * def = __gpy_staticmethod_type_node;
retval = def->tp_new (def, args);
gpy_free (args);
- debug ("initilized staticmethod object <%p> to <%s>!\n",
- (void*)retval, identifier);
gpy_assert (retval->T == TYPE_OBJECT_DECL);
return retval;
@@ -234,9 +224,9 @@ gpy_object_t * gpy_rr_fold_classmethod_decl (const char * identifier,
gpy_object_t ** args = (gpy_object_t **)
gpy_calloc (4, sizeof(gpy_object_t*));
- gpy_literal_t i;
- i.type = TYPE_STRING;
- i.literal.string = (char *)identifier;
+ gpy_literal_t s;
+ s.type = TYPE_STRING;
+ s.literal.string = (char *)identifier;
gpy_literal_t p;
p.type = TYPE_ADDR;
@@ -246,7 +236,7 @@ gpy_object_t * gpy_rr_fold_classmethod_decl (const char * identifier,
n.type = TYPE_INTEGER;
n.literal.integer = 0;
- gpy_object_t a1 = { .T = TYPE_OBJECT_LIT, .o.literal = &i };
+ gpy_object_t a1 = { .T = TYPE_OBJECT_LIT, .o.literal = &s };
gpy_object_t a2 = { .T = TYPE_OBJECT_LIT, .o.literal = &p };
gpy_object_t a3 = { .T = TYPE_OBJECT_LIT, .o.literal = &n };
gpy_object_t a4 = { .T = TYPE_NULL, .o.literal = NULL };
@@ -256,15 +246,10 @@ gpy_object_t * gpy_rr_fold_classmethod_decl (const char * identifier,
args[2] = &a3;
args[3] = &a4;
- gpy_typedef_t * def = (gpy_typedef_t *)
- __GPY_GLOBL_PRIMITIVES->vector[1];
- gpy_assert (def);
-
+ gpy_typedef_t * def = __gpy_classmethod_type_node;
retval = def->tp_new (def, args);
gpy_free (args);
- debug ("initilized classmethod object <%p> to <%s>!\n",
- (void*)retval, identifier);
gpy_assert (retval->T == TYPE_OBJECT_DECL);
return retval;
@@ -277,11 +262,21 @@ gpy_object_t * gpy_rr_fold_call (gpy_object_t * decl, int nargs, ...)
gpy_assert (decl->T == TYPE_OBJECT_DECL);
gpy_typedef_t * type = decl->o.object_state->definition;
- gpy_assert (type->members_defintion);
+
+ /* + 1 for sentinal */
+ gpy_object_t ** args = calloc (nargs + 1, sizeof (gpy_object_t *));
+ va_list ap;
+ int idx;
+ va_start (ap, nargs);
+ for (idx = 0; idx < nargs; ++idx)
+ {
+ args[idx] = va_arg (ap, gpy_object_t *);
+ }
+ args[idx] = NULL;
if (type->tp_call)
{
- retval = type->tp_call (decl, NULL);
+ retval = type->tp_call (decl, args);
}
else
fatal ("name is not callable!\n");
@@ -289,17 +284,12 @@ gpy_object_t * gpy_rr_fold_call (gpy_object_t * decl, int nargs, ...)
return retval;
}
-
-
unsigned char * gpy_rr_eval_attrib_reference (gpy_object_t * base,
const char * attrib)
{
unsigned char * retval = NULL;
- gpy_assert (base->T == TYPE_OBJECT_STATE);
-
gpy_typedef_t * type = base->o.object_state->definition;
- gpy_assert (type->members_defintion);
-
+
struct gpy_object_attrib_t ** members = type->members_defintion;
gpy_object_state_t * objs = base->o.object_state;
@@ -336,15 +326,9 @@ gpy_object_t * gpy_rr_fold_integer (const int x)
args[0] = &a1;
args[1] = &a2;
- gpy_typedef_t * Int_def = (gpy_typedef_t *)
- __GPY_GLOBL_PRIMITIVES->vector[0];
- gpy_assert (Int_def);
-
+ gpy_typedef_t * Int_def = __gpy_integer_type_node;
retval = Int_def->tp_new (Int_def, args);
gpy_free(args);
-
- debug ("initilized integer object <%p> to <%i>!\n",
- (void*)retval, x );
gpy_assert (retval->T == TYPE_OBJECT_STATE);
return retval;
@@ -370,7 +354,6 @@ void gpy_rr_eval_print (int fd, int count, ...)
for (idx = 0; idx<count; ++idx)
{
it = va_arg (vl, gpy_object_t *);
- gpy_assert (it->T == TYPE_OBJECT_STATE);
struct gpy_typedef_t * definition = it->o.object_state->definition;
switch (fd)
diff --git a/libgpython/runtime/py-type-utils.c b/libgpython/runtime/py-type-utils.c
index e11923e9a05..dfa4ca54f7a 100644
--- a/libgpython/runtime/py-type-utils.c
+++ b/libgpython/runtime/py-type-utils.c
@@ -32,7 +32,8 @@ along with GCC; see the file COPYING3. If not see
#define GPY_ARG_LIT_CHECK(A,I,X) \
gpy_assert (A[I]->T == TYPE_OBJECT_LIT); \
- gpy_assert (A[I]->o.literal->type == X);
+ gpy_assert (A[I]->o.literal->type == X); \
+ ++I;
bool gpy_args_check_fmt (gpy_object_t ** args, const char * fmt)
{
@@ -53,27 +54,24 @@ bool gpy_args_check_fmt (gpy_object_t ** args, const char * fmt)
case 'i':
{
GPY_ARG_LIT_CHECK (args, idx, TYPE_INTEGER);
- debug ("integer check pass!\n");
}
break;
case 's':
{
GPY_ARG_LIT_CHECK (args, idx, TYPE_STRING);
- debug ("string check pass!\n");
}
+ break;
case 'p':
{
GPY_ARG_LIT_CHECK (args, idx, TYPE_ADDR);
- debug ("pointer check pass!\n");
}
break;
case 'A':
{
GPY_ARG_LIT_CHECK (args, idx, TYPE_ATTRIB_L);
- debug ("pointer check pass!\n");
}
break;
@@ -84,9 +82,6 @@ bool gpy_args_check_fmt (gpy_object_t ** args, const char * fmt)
}
break;
}
- if (!retval)
- break;
- idx++;
}
return retval;
}
@@ -99,7 +94,6 @@ int gpy_args_lit_parse_int (gpy_object_t * arg)
gpy_assert (arg->o.literal->type == TYPE_INTEGER);
retval = arg->o.literal->literal.integer;
- debug ("parsed int <%i>!\n", retval);
return retval;
}
@@ -111,7 +105,6 @@ char * gpy_args_lit_parse_string (gpy_object_t * arg)
gpy_assert (arg->o.literal->type == TYPE_STRING);
retval = arg->o.literal->literal.string;
- debug ("parsed string <%s>!\n", retval);
return retval;
}
@@ -123,7 +116,6 @@ unsigned char * gpy_args_lit_parse_pointer (gpy_object_t * arg)
gpy_assert (arg->o.literal->type == TYPE_ADDR);
retval = arg->o.literal->literal.addr;
- debug ("parsed pointer <%p>!\n", (void*) retval);
return retval;
}
@@ -135,7 +127,6 @@ gpy_object_attrib_t ** gpy_args_lit_parse_attrib_table (gpy_object_t * arg)
gpy_assert (arg->o.literal->type == TYPE_ATTRIB_L);
retval = arg->o.literal->literal.attribs;
- debug ("parsed attribute table <%p>!\n", (void*) retval);
return retval;
}