summaryrefslogtreecommitdiff
path: root/libguile/foreign-object.c
blob: 21dc2c007deb20375043f759894042b9c5b18c48 (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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/* Copyright 2014,2017-2018
     Free Software Foundation, Inc.

   This file is part of Guile.

   Guile is free software: you can redistribute it and/or modify it
   under the terms of the GNU Lesser General Public License as published
   by the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   Guile 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 Lesser General Public
   License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with Guile.  If not, see
   <https://www.gnu.org/licenses/>.  */




#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

#include "eval.h"
#include "extensions.h"
#include "finalizers.h"
#include "goops.h"
#include "gsubr.h"
#include "list.h"
#include "modules.h"
#include "numbers.h"
#include "procs.h"
#include "threads.h"
#include "variable.h"
#include "version.h"

#include "foreign-object.h"




static SCM make_fobj_type_var;

static void
init_make_fobj_type_var (void)
{
  make_fobj_type_var = scm_c_private_lookup ("system foreign-object",
                                             "make-foreign-object-type");
}

SCM
scm_make_foreign_object_type (SCM name, SCM slot_names,
                              scm_t_struct_finalize finalizer)
{
  SCM type;

  static scm_i_pthread_once_t once = SCM_I_PTHREAD_ONCE_INIT;
  scm_i_pthread_once (&once, init_make_fobj_type_var);

  type = scm_call_2 (scm_variable_ref (make_fobj_type_var), name, slot_names);

  if (finalizer)
    SCM_SET_VTABLE_INSTANCE_FINALIZER (type, finalizer);

  return type;
}

void
scm_assert_foreign_object_type (SCM type, SCM val)
{
  /* FIXME: Add fast path for when type == struct vtable */
  if (!SCM_IS_A_P (val, type))
    scm_error (scm_arg_type_key, NULL, "Wrong type (expecting ~A): ~S",
               scm_list_2 (scm_class_name (type), val), scm_list_1 (val));
}

SCM
scm_make_foreign_object_0 (SCM type)
{
  return scm_make_foreign_object_n (type, 0, NULL);
}

SCM
scm_make_foreign_object_1 (SCM type, void *val0)
{
  return scm_make_foreign_object_n (type, 1, &val0);
}

SCM
scm_make_foreign_object_2 (SCM type, void *val0, void *val1)
{
  void *vals[2];

  vals[0] = val0;
  vals[1] = val1;

  return scm_make_foreign_object_n (type, 2, vals);
}

SCM
scm_make_foreign_object_3 (SCM type, void *val0, void *val1, void *val2)
{
  void *vals[3];

  vals[0] = val0;
  vals[1] = val1;
  vals[2] = val2;

  return scm_make_foreign_object_n (type, 3, vals);
}

SCM
scm_make_foreign_object_n (SCM type, size_t n, void *vals[])
#define FUNC_NAME "make-foreign-object"
{
  SCM obj;
  size_t i;

  SCM_VALIDATE_VTABLE (SCM_ARG1, type);

  if (SCM_VTABLE_SIZE (type) < n)
    scm_out_of_range (FUNC_NAME, scm_from_size_t (n));

  for (i = 0; i < n; i++)
    if (!SCM_VTABLE_FIELD_IS_UNBOXED (type, i))
      scm_wrong_type_arg_msg (FUNC_NAME, 0, type, "foreign object type");

  obj = scm_c_make_structv (type, 0, 0, NULL);

  for (i = 0; i < n; i++)
    SCM_STRUCT_DATA_SET (obj, i, (scm_t_bits) vals[i]);

  return obj;
}
#undef FUNC_NAME

scm_t_bits
scm_foreign_object_unsigned_ref (SCM obj, size_t n)
#define FUNC_NAME "foreign-object-ref"
{
  SCM_VALIDATE_STRUCT (SCM_ARG1, obj);
  
  if (SCM_STRUCT_SIZE (obj) <= n)
    scm_out_of_range (FUNC_NAME, scm_from_size_t (n));

  if (!SCM_STRUCT_FIELD_IS_UNBOXED (obj, n))
    scm_wrong_type_arg_msg (FUNC_NAME, 0, scm_from_size_t (n), "unboxed field");

  return SCM_STRUCT_DATA_REF (obj, n);
}
#undef FUNC_NAME

void
scm_foreign_object_unsigned_set_x (SCM obj, size_t n, scm_t_bits val)
#define FUNC_NAME "foreign-object-set!"
{
  SCM_VALIDATE_STRUCT (SCM_ARG1, obj);
  
  if (SCM_STRUCT_SIZE (obj) <= n)
    scm_out_of_range (FUNC_NAME, scm_from_size_t (n));

  if (!SCM_STRUCT_FIELD_IS_UNBOXED (obj, n))
    scm_wrong_type_arg_msg (FUNC_NAME, 0, scm_from_size_t (n), "unboxed field");

  SCM_STRUCT_DATA_SET (obj, n, val);
}
#undef FUNC_NAME

scm_t_signed_bits
scm_foreign_object_signed_ref (SCM obj, size_t n)
{
  scm_t_bits bits = scm_foreign_object_unsigned_ref (obj, n);
  return (scm_t_signed_bits) bits;
}

void
scm_foreign_object_signed_set_x (SCM obj, size_t n, scm_t_signed_bits val)
{
  scm_t_bits bits = (scm_t_bits) val;
  scm_foreign_object_unsigned_set_x (obj, n, bits);
}

void*
scm_foreign_object_ref (SCM obj, size_t n)
{
  scm_t_bits bits = scm_foreign_object_unsigned_ref (obj, n);
  return (void *) bits;
}

void
scm_foreign_object_set_x (SCM obj, size_t n, void *val)
{
  scm_t_bits bits = (scm_t_bits) val;
  scm_foreign_object_unsigned_set_x (obj, n, bits);
}
  
static void
invoke_finalizer (void *obj, void *data)
{
  scm_call_1 (PTR2SCM (data), PTR2SCM (obj));
}

static SCM
sys_add_finalizer_x (SCM obj, SCM finalizer)
#define FUNC_NAME "%add-finalizer!"
{
  SCM_VALIDATE_PROC (SCM_ARG2, finalizer);

  scm_i_add_finalizer (SCM2PTR (obj), invoke_finalizer, SCM2PTR (finalizer));

  return SCM_UNSPECIFIED;
}
#undef FUNC_NAME

static void
scm_init_foreign_object (void)
{
  scm_c_define_gsubr ("%add-finalizer!", 2, 0, 0,
                      (scm_t_subr) sys_add_finalizer_x);
}

void
scm_register_foreign_object (void)
{
  scm_c_register_extension ("libguile-" SCM_EFFECTIVE_VERSION,
                            "scm_init_foreign_object",
                            (scm_t_extension_init_func)scm_init_foreign_object,
                            NULL);
}