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 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
|
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
* GObject introspection: Invoke functionality
*
* Copyright (C) 2005 Matthias Clasen
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*
* This library 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 2 of the License, or (at your option) any later version.
*
* This library 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 this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#include "config.h"
#include <stdlib.h>
#include <glib.h>
#include <glib-object.h>
#include <girepository/girepository.h>
#include "girffi.h"
/**
* value_to_ffi_type:
* @gvalue: (transfer none): a [type@GObject.Value] to convert
* @value: (out caller-allocates): return location for the ffi data
*
* Convert @gvalue to a format suitable for passing to ffi.
*
* @value is only valid as long as @gvalue is alive.
*
* Returns: pointer to the `ffi_type` associated with @value
* Since: 2.80
*/
static ffi_type *
value_to_ffi_type (const GValue *gvalue, void **value)
{
ffi_type *rettype = NULL;
GType type = g_type_fundamental (G_VALUE_TYPE (gvalue));
g_assert (type != G_TYPE_INVALID);
switch (type)
{
case G_TYPE_BOOLEAN:
case G_TYPE_CHAR:
case G_TYPE_INT:
rettype = &ffi_type_sint;
*value = (void *) &(gvalue->data[0].v_int);
break;
case G_TYPE_UCHAR:
case G_TYPE_UINT:
rettype = &ffi_type_uint;
*value = (void *) &(gvalue->data[0].v_uint);
break;
case G_TYPE_STRING:
case G_TYPE_OBJECT:
case G_TYPE_BOXED:
case G_TYPE_POINTER:
case G_TYPE_PARAM:
rettype = &ffi_type_pointer;
*value = (void *) &(gvalue->data[0].v_pointer);
break;
case G_TYPE_FLOAT:
rettype = &ffi_type_float;
*value = (void *) &(gvalue->data[0].v_float);
break;
case G_TYPE_DOUBLE:
rettype = &ffi_type_double;
*value = (void *) &(gvalue->data[0].v_double);
break;
case G_TYPE_LONG:
rettype = &ffi_type_slong;
*value = (void *) &(gvalue->data[0].v_long);
break;
case G_TYPE_ULONG:
rettype = &ffi_type_ulong;
*value = (void *) &(gvalue->data[0].v_ulong);
break;
case G_TYPE_INT64:
rettype = &ffi_type_sint64;
*value = (void *) &(gvalue->data[0].v_int64);
break;
case G_TYPE_UINT64:
rettype = &ffi_type_uint64;
*value = (void *) &(gvalue->data[0].v_uint64);
break;
default:
rettype = &ffi_type_pointer;
*value = NULL;
g_warning ("Unsupported fundamental type: %s", g_type_name (type));
break;
}
return rettype;
}
/**
* g_value_to_ffi_return_type:
* @gvalue: (transfer none): a [type@GObject.Value] to convert
* @ffi_value: (transfer none): a [type@GIRepository.Argument] containing the
* data to use
* @value: (out caller-allocates): return location for the ffi data
*
* Convert @ffi_value to a format suitable for passing to ffi, using the type
* data from @gvalue.
*
* @value is only valid as long as @gvalue and @ffi_value are alive.
*
* Returns: pointer to the `ffi_type` associated with @value
* Since: 2.80
*/
static ffi_type *
g_value_to_ffi_return_type (const GValue *gvalue,
const GIArgument *ffi_value,
void **value)
{
ffi_type *rettype = NULL;
GType type = g_type_fundamental (G_VALUE_TYPE (gvalue));
g_assert (type != G_TYPE_INVALID);
*value = (void *) &(ffi_value->v_long);
switch (type) {
case G_TYPE_CHAR:
rettype = &ffi_type_sint8;
break;
case G_TYPE_UCHAR:
rettype = &ffi_type_uint8;
break;
case G_TYPE_BOOLEAN:
case G_TYPE_INT:
rettype = &ffi_type_sint;
break;
case G_TYPE_UINT:
rettype = &ffi_type_uint;
break;
case G_TYPE_STRING:
case G_TYPE_OBJECT:
case G_TYPE_BOXED:
case G_TYPE_POINTER:
case G_TYPE_PARAM:
rettype = &ffi_type_pointer;
break;
case G_TYPE_FLOAT:
rettype = &ffi_type_float;
*value = (void *) &(ffi_value->v_float);
break;
case G_TYPE_DOUBLE:
rettype = &ffi_type_double;
*value = (void *) &(ffi_value->v_double);
break;
case G_TYPE_LONG:
rettype = &ffi_type_slong;
break;
case G_TYPE_ULONG:
rettype = &ffi_type_ulong;
break;
case G_TYPE_INT64:
rettype = &ffi_type_sint64;
*value = (void *) &(ffi_value->v_int64);
break;
case G_TYPE_UINT64:
rettype = &ffi_type_uint64;
*value = (void *) &(ffi_value->v_uint64);
break;
default:
rettype = &ffi_type_pointer;
*value = NULL;
g_warning ("Unsupported fundamental type: %s", g_type_name (type));
break;
}
return rettype;
}
/**
* g_value_from_ffi_value:
* @gvalue: (inout): a [type@GObject.Value] to set
* @value: (transfer none): ffi data to convert
*
* Convert @value to a [type@GObject.Value] according to the type already set
* on @gvalue.
*
* @gvalue is valid even after @value is finalised.
*
* Since: 2.80
*/
static void
g_value_from_ffi_value (GValue *gvalue,
const GIArgument *value)
{
switch (g_type_fundamental (G_VALUE_TYPE (gvalue))) {
case G_TYPE_INT:
g_value_set_int (gvalue, (gint)value->v_long);
break;
case G_TYPE_FLOAT:
g_value_set_float (gvalue, (gfloat)value->v_float);
break;
case G_TYPE_DOUBLE:
g_value_set_double (gvalue, (gdouble)value->v_double);
break;
case G_TYPE_BOOLEAN:
g_value_set_boolean (gvalue, (gboolean)value->v_long);
break;
case G_TYPE_STRING:
g_value_set_string (gvalue, (char*)value->v_pointer);
break;
case G_TYPE_CHAR:
g_value_set_schar (gvalue, (char)value->v_long);
break;
case G_TYPE_UCHAR:
g_value_set_uchar (gvalue, (guchar)value->v_ulong);
break;
case G_TYPE_UINT:
g_value_set_uint (gvalue, (guint)value->v_ulong);
break;
case G_TYPE_POINTER:
g_value_set_pointer (gvalue, (gpointer)value->v_pointer);
break;
case G_TYPE_LONG:
g_value_set_long (gvalue, (glong)value->v_long);
break;
case G_TYPE_ULONG:
g_value_set_ulong (gvalue, (gulong)value->v_ulong);
break;
case G_TYPE_INT64:
g_value_set_int64 (gvalue, (gint64)value->v_int64);
break;
case G_TYPE_UINT64:
g_value_set_uint64 (gvalue, (guint64)value->v_uint64);
break;
case G_TYPE_BOXED:
g_value_set_boxed (gvalue, (gpointer)value->v_pointer);
break;
case G_TYPE_PARAM:
g_value_set_param (gvalue, (gpointer)value->v_pointer);
break;
default:
g_warning ("Unsupported fundamental type: %s",
g_type_name (g_type_fundamental (G_VALUE_TYPE (gvalue))));
}
}
/**
* gi_cclosure_marshal_generic: (skip)
* @closure: a [type@GObject.Closure]
* @return_gvalue: (optional) (out caller-allocates): return location for the
* return value from the closure, or `NULL` to ignore
* @n_param_values: number of param values
* @param_values: (array length=n_param_values): values to pass to the closure
* parameters
* @invocation_hint: invocation hint
* @marshal_data: marshal data
*
* A generic C closure marshal function using ffi and
* [type@GIRepository.Argument].
*
* Since: 2.80
*/
void
gi_cclosure_marshal_generic (GClosure *closure,
GValue *return_gvalue,
unsigned int n_param_values,
const GValue *param_values,
void *invocation_hint,
void *marshal_data)
{
GIArgument return_ffi_value = { 0, };
ffi_type *rtype;
void *rvalue;
unsigned int n_args;
ffi_type **atypes;
void **args;
unsigned int i;
ffi_cif cif;
GCClosure *cc = (GCClosure*) closure;
if (return_gvalue && G_VALUE_TYPE (return_gvalue))
{
rtype = g_value_to_ffi_return_type (return_gvalue, &return_ffi_value,
&rvalue);
}
else
{
rtype = &ffi_type_void;
rvalue = &return_ffi_value.v_long;
}
n_args = n_param_values + 1;
atypes = g_alloca (sizeof (ffi_type *) * n_args);
args = g_alloca (sizeof (void *) * n_args);
if (n_param_values > 0)
{
if (G_CCLOSURE_SWAP_DATA (closure))
{
atypes[n_args-1] = value_to_ffi_type (param_values + 0,
&args[n_args-1]);
atypes[0] = &ffi_type_pointer;
args[0] = &closure->data;
}
else
{
atypes[0] = value_to_ffi_type (param_values + 0, &args[0]);
atypes[n_args-1] = &ffi_type_pointer;
args[n_args-1] = &closure->data;
}
}
else
{
atypes[0] = &ffi_type_pointer;
args[0] = &closure->data;
}
for (i = 1; i < n_args - 1; i++)
atypes[i] = value_to_ffi_type (param_values + i, &args[i]);
if (ffi_prep_cif (&cif, FFI_DEFAULT_ABI, n_args, rtype, atypes) != FFI_OK)
return;
ffi_call (&cif, marshal_data ? marshal_data : cc->callback, rvalue, args);
if (return_gvalue && G_VALUE_TYPE (return_gvalue))
g_value_from_ffi_value (return_gvalue, &return_ffi_value);
}
|