diff options
Diffstat (limited to 'libgpython/runtime')
-rw-r--r-- | libgpython/runtime/py-obj_class.c | 2 | ||||
-rw-r--r-- | libgpython/runtime/py-obj_classmethod.c | 114 | ||||
-rw-r--r-- | libgpython/runtime/py-obj_staticmethod.c | 2 | ||||
-rw-r--r-- | libgpython/runtime/py-runtime.c | 60 |
4 files changed, 171 insertions, 7 deletions
diff --git a/libgpython/runtime/py-obj_class.c b/libgpython/runtime/py-obj_class.c index 408e40da349..d002f8b2739 100644 --- a/libgpython/runtime/py-obj_class.c +++ b/libgpython/runtime/py-obj_class.c @@ -29,6 +29,7 @@ along with GCC; see the file COPYING3. If not see #include <gpython/gpython.h> #include <gpython/vectors.h> #include <gpython/objects.h> +#include <gpython/runtime.h> typedef void (*__field_init_ptr)(void *); @@ -80,6 +81,7 @@ gpy_object_t * gpy_object_classobj_new (gpy_typedef_t * type, unsigned char * codeaddr = gpy_object_staticmethod_getaddr (field_init); gpy_assert (codeaddr); + debug ("calling the __init__!\n"); __field_init_ptr c = (__field_init_ptr)codeaddr; c (self); diff --git a/libgpython/runtime/py-obj_classmethod.c b/libgpython/runtime/py-obj_classmethod.c index e69de29bb2d..815c017b886 100644 --- a/libgpython/runtime/py-obj_classmethod.c +++ b/libgpython/runtime/py-obj_classmethod.c @@ -0,0 +1,114 @@ +/* This file is part of GCC. + +GCC is free software; you can redistribute it and/or modify it under +the terms of the GNU General Public License as published by the Free +Software Foundation; either version 3, or (at your option) any later +version. + +GCC is distributed in the hope that it will be useful, but WITHOUT ANY +WARRANTY; without even the implied warranty of MERCHANTABILITY or +FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +for more details. + +You should have received a copy of the GNU General Public License +along with GCC; see the file COPYING3. If not see +<http://www.gnu.org/licenses/>. */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <stdbool.h> + +#include <gmp.h> +#include <mpfr.h> + +#include <gpython/gpython.h> +#include <gpython/vectors.h> +#include <gpython/objects.h> + +struct gpy_object_classmethod_t { + const unsigned char * code; + const char * identifier; + unsigned int nargs; +}; + +gpy_object_t * gpy_object_classmethod_new (gpy_typedef_t * type, + gpy_object_t ** args) +{ + gpy_object_t * retval = NULL_OBJECT; + + bool check = gpy_args_check_fmt (args, "s,p,i."); + gpy_assert (check); + + char * id = gpy_args_lit_parse_string (args[0]); + unsigned char * code_addr = gpy_args_lit_parse_pointer (args[1]); + int nargs = gpy_args_lit_parse_int (args[2]); + + struct gpy_object_classmethod_t * self = gpy_malloc (type->state_size); + self->identifier = id; + self->code = code_addr; + self->nargs = nargs; + + retval = gpy_create_object_decl (type, self); + + return retval; +} + +/* free's the object state not the */ +void gpy_object_classmethod_dealloc (gpy_object_t * self) +{ + gpy_assert (self->T == TYPE_OBJECT_DECL); + gpy_object_state_t * object_state = self->o.object_state; + + gpy_free (object_state->state); + object_state->state = NULL; +} + +void gpy_object_classmethod_print (gpy_object_t * self, FILE *fd, bool newline) +{ + fprintf (fd, "class method instance <%p> ", (void *)self); + if (newline) + fprintf (fd, "\n"); +} + +gpy_object_t * gpy_object_classmethod_call (gpy_object_t * self, + 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) + { + fndecl fnptr = (fndecl)state->code; + fnptr (args); + } + return retval; +} + +unsigned char * gpy_object_classmethod_getaddr (gpy_object_t * self) +{ + gpy_object_state_t * state = self->o.object_state; + struct gpy_object_classmethod_t * s = state->state; + return s->code; +} + +static struct gpy_typedef_t class_functor_obj = { + "classmethod", + sizeof (struct gpy_object_classmethod_t), + &gpy_object_classmethod_new, + &gpy_object_classmethod_dealloc, + &gpy_object_classmethod_print, + &gpy_object_classmethod_call, + NULL, + NULL +}; + +void gpy_obj_classmethod_mod_init (gpy_vector_t * const vec) +{ + gpy_vec_push (vec, &class_functor_obj); +} diff --git a/libgpython/runtime/py-obj_staticmethod.c b/libgpython/runtime/py-obj_staticmethod.c index b81075d8f09..0f287e18f9e 100644 --- a/libgpython/runtime/py-obj_staticmethod.c +++ b/libgpython/runtime/py-obj_staticmethod.c @@ -109,7 +109,7 @@ static struct gpy_typedef_t functor_obj = { NULL }; -void gpy_obj_functor_mod_init (gpy_vector_t * const vec) +void gpy_obj_staticmethod_mod_init (gpy_vector_t * const vec) { gpy_vec_push (vec, &functor_obj); } diff --git a/libgpython/runtime/py-runtime.c b/libgpython/runtime/py-runtime.c index 2f234db8eb3..a518ba9db69 100644 --- a/libgpython/runtime/py-runtime.c +++ b/libgpython/runtime/py-runtime.c @@ -46,8 +46,9 @@ static void gpy_rr_init_primitives (void) { gpy_obj_integer_mod_init (__GPY_GLOBL_PRIMITIVES); - gpy_obj_functor_mod_init (__GPY_GLOBL_PRIMITIVES); + gpy_obj_staticmethod_mod_init (__GPY_GLOBL_PRIMITIVES); gpy_obj_class_mod_init (__GPY_GLOBL_PRIMITIVES); + gpy_obj_classmethod_mod_init (__GPY_GLOBL_PRIMITIVES); } static @@ -80,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_STACK_POINTER += 3+nslots; + __GPY_GLOBL_RR_STACK2_POINTER += 3+nslots; } void gpy_rr_init_runtime (void) @@ -104,7 +105,7 @@ gpy_object_attrib_t * gpy_rr_fold_attribute (const char * identifier, attrib->identifier = identifier; if (code_addr) { - gpy_object_t * f = gpy_rr_fold_functor_decl (identifier, code_addr); + gpy_object_t * f = gpy_rr_fold_classmethod_decl (identifier, code_addr); attrib->addr = f; } else @@ -181,8 +182,8 @@ gpy_object_t * gpy_rr_fold_class_decl (gpy_object_attrib_t ** attribs, return retval; } -gpy_object_t * gpy_rr_fold_functor_decl (const char * identifier, - unsigned char * code_addr) +gpy_object_t * gpy_rr_fold_staticmethod_decl (const char * identifier, + unsigned char * code_addr) { gpy_object_t * retval = NULL_OBJECT; @@ -218,13 +219,58 @@ gpy_object_t * gpy_rr_fold_functor_decl (const char * identifier, retval = def->tp_new (def, args); gpy_free (args); - debug ("initilized function object <%p> to <%s>!\n", + debug ("initilized staticmethod object <%p> to <%s>!\n", (void*)retval, identifier); gpy_assert (retval->T == TYPE_OBJECT_DECL); return retval; } +gpy_object_t * gpy_rr_fold_classmethod_decl (const char * identifier, + unsigned char * code_addr) +{ + gpy_object_t * retval = NULL_OBJECT; + + 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 p; + p.type = TYPE_ADDR; + p.literal.addr = code_addr; + + gpy_literal_t n; + n.type = TYPE_INTEGER; + n.literal.integer = 0; + + gpy_object_t a1 = { .T = TYPE_OBJECT_LIT, .o.literal = &i }; + 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 }; + + args[0] = &a1; + args[1] = &a2; + args[2] = &a3; + args[3] = &a4; + + gpy_typedef_t * def = (gpy_typedef_t *) + __GPY_GLOBL_PRIMITIVES->vector[1]; + gpy_assert (def); + + 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; +} + + gpy_object_t * gpy_rr_fold_call (gpy_object_t * decl, int nargs, ...) { gpy_object_t * retval = NULL_OBJECT; @@ -243,6 +289,8 @@ 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) { |