aboutsummaryrefslogtreecommitdiff
path: root/libgpython/runtime/py-obj_staticmethod.c
blob: 0f287e18f9e918357a7d20903248189602401fa3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/* 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_staticmethod_t {
  const unsigned char * code;
  const char * identifier;
  unsigned int nargs;
};

/* args = code addr/nargs */
gpy_object_t * gpy_object_staticmethod_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_staticmethod_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_staticmethod_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_staticmethod_print (gpy_object_t * self, FILE *fd, bool newline)
{
  fprintf (fd, "static method instance <%p> ", (void *)self);
  if (newline)
    fprintf (fd, "\n");
}

gpy_object_t * gpy_object_staticmethod_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_staticmethod_t * state = self->o.object_state->state;
  if (!state->code)
    {
      fndecl fnptr = (fndecl)state->code;
      fnptr (args);
    }
  return retval;
}

unsigned char * gpy_object_staticmethod_getaddr (gpy_object_t * self)
{
  gpy_object_state_t * state = self->o.object_state;
  struct gpy_object_staticmethod_t * s = state->state;
  return s->code;
}

static struct gpy_typedef_t functor_obj = {
  "staticmethod",
  sizeof (struct gpy_object_staticmethod_t),
  &gpy_object_staticmethod_new,
  &gpy_object_staticmethod_dealloc,
  &gpy_object_staticmethod_print,
  &gpy_object_staticmethod_call,
  NULL,
  NULL
};

void gpy_obj_staticmethod_mod_init (gpy_vector_t * const vec)
{
  gpy_vec_push (vec, &functor_obj);
}