cleaning up and adding in object state code
[official-gcc.git] / libgpython / runtime / obj-functor.c
blobf8b75917dcfbcd6ae94eb1e15380a5a5f1aa665a
1 /* This file is part of GCC.
3 GCC is free software; you can redistribute it and/or modify it under
4 the terms of the GNU General Public License as published by the Free
5 Software Foundation; either version 3, or (at your option) any later
6 version.
8 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
9 WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
11 for more details.
13 You should have received a copy of the GNU General Public License
14 along with GCC; see the file COPYING3. If not see
15 <http://www.gnu.org/licenses/>. */
17 #ifdef HAVE_CONFIG_H
18 #include "config.h"
19 #endif
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <stdbool.h>
26 #include <gmp.h>
27 #include <mpfr.h>
29 #include <gpython/gpython.h>
30 #include <gpython/vectors.h>
31 #include <gpython/objects.h>
33 struct gpy_object_staticmethod_t {
34 const unsigned char * code;
35 const char * identifier;
36 unsigned int nargs;
39 /* args = code addr/nargs */
40 gpy_object_t * gpy_object_staticmethod_new (gpy_typedef_t * type,
41 gpy_object_t ** args)
43 gpy_object_t * retval = NULL_OBJECT;
45 bool check = gpy_args_check_fmt (args, "s,p,i.");
46 gpy_assert (check);
48 char * id = gpy_args_lit_parse_string (args[0]);
49 unsigned char * code_addr = gpy_args_lit_parse_pointer (args[1]);
50 int nargs = gpy_args_lit_parse_int (args[2]);
52 struct gpy_object_staticmethod_t * self = gpy_malloc (type->state_size);
53 self->identifier = id;
54 self->code = code_addr;
55 self->nargs = nargs;
57 retval = gpy_create_object_decl (type, self);
59 return retval;
62 /* free's the object state not the */
63 void gpy_object_staticmethod_dealloc (gpy_object_t * self)
65 gpy_assert (self->T == TYPE_OBJECT_DECL);
66 gpy_object_state_t * object_state = self->o.object_state;
68 gpy_free (object_state->state);
69 object_state->state = NULL;
72 void gpy_object_staticmethod_print (gpy_object_t * self, FILE *fd, bool newline)
74 fprintf (fd, "static method instance <%p> ", (void *)self);
75 if (newline)
76 fprintf (fd, "\n");
79 gpy_object_t * gpy_object_staticmethod_call (gpy_object_t * self,
80 gpy_object_t ** args)
82 gpy_object_t * retval = NULL_OBJECT;
83 gpy_assert (self->T == TYPE_OBJECT_DECL);
85 struct gpy_object_staticmethod_t * state = self->o.object_state->state;
86 if (!state->code)
88 fndecl fnptr = (fndecl)state->code;
89 fnptr (args);
91 return retval;
94 static struct gpy_typedef_t functor_obj = {
95 "staticmethod",
96 sizeof (struct gpy_object_staticmethod_t),
97 &gpy_object_staticmethod_new,
98 &gpy_object_staticmethod_dealloc,
99 &gpy_object_staticmethod_print,
100 &gpy_object_staticmethod_call,
101 NULL,
102 NULL
105 void gpy_obj_functor_mod_init (gpy_vector_t * const vec)
107 gpy_vec_push (vec, &functor_obj);