~ubuntu-branches/ubuntu/wily/lua-lgi/wily-proposed

« back to all changes in this revision

Viewing changes to lgi/marshal.c

  • Committer: Package Import Robot
  • Author(s): Enrico Tassi
  • Date: 2015-08-14 21:07:46 UTC
  • mfrom: (1.1.11)
  • Revision ID: package-import@ubuntu.com-20150814210746-w1i1de8zufeiimxp
Tags: 0.9.0-1
* remove pre-depend on multiarch-support
* new upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
194
194
/* Retrieves pointer to GIArgument in given array, given that array
195
195
   contains elements of type ti. */
196
196
static gssize
197
 
array_get_elt_size (GITypeInfo *ti)
 
197
array_get_elt_size (GITypeInfo *ti, gboolean force_ptr)
198
198
{
199
199
  gssize size = sizeof (gpointer);
200
 
  if (!g_type_info_is_pointer (ti))
 
200
  if (!g_type_info_is_pointer (ti) && !force_ptr)
201
201
    {
202
202
      switch (g_type_info_get_tag (ti))
203
203
        {
246
246
  g_array_free (array, FALSE);
247
247
}
248
248
 
 
249
static void
 
250
ptr_array_detach (GPtrArray *array)
 
251
{
 
252
  g_ptr_array_free (array, FALSE);
 
253
}
 
254
 
 
255
static void
 
256
byte_array_detach (GByteArray *array)
 
257
{
 
258
  g_byte_array_free (array, FALSE);
 
259
}
 
260
 
249
261
/* Marshalls array from Lua to C. Returns number of temporary elements
250
262
   pushed to the stack. */
251
263
static int
260
272
                      ? GI_TRANSFER_EVERYTHING : GI_TRANSFER_NOTHING);
261
273
  gboolean zero_terminated;
262
274
  GArray *array = NULL;
 
275
  int parent = 0;
263
276
 
264
277
  /* Represent nil as NULL array. */
265
278
  if (optional && lua_isnoneornil (L, narg))
273
286
      eti = g_type_info_get_param_type (ti, 0);
274
287
      lgi_gi_info_new (L, eti);
275
288
      eti_guard = lua_gettop (L);
276
 
      esize = array_get_elt_size (eti);
 
289
      esize = array_get_elt_size (eti, atype == GI_ARRAY_TYPE_PTR_ARRAY);
277
290
 
278
291
      /* Check the type. If this is C-array of byte-sized elements, we
279
292
         can try special-case and accept strings or buffers. */
312
325
             if needed. */
313
326
          if (*out_size > 0 || zero_terminated)
314
327
            {
315
 
              array = g_array_sized_new (zero_terminated, TRUE, esize,
316
 
                                         *out_size);
317
 
              g_array_set_size (array, *out_size);
318
 
              *lgi_guard_create (L, (GDestroyNotify)
319
 
                                 (transfer == GI_TRANSFER_EVERYTHING
320
 
                                  ? array_detach : g_array_unref)) = array;
 
328
              guint total_size = *out_size + (zero_terminated ? 1 : 0);
 
329
              switch (atype)
 
330
                {
 
331
                case GI_ARRAY_TYPE_C:
 
332
                case GI_ARRAY_TYPE_ARRAY:
 
333
                  array = g_array_sized_new (zero_terminated, TRUE, esize,
 
334
                                             *out_size);
 
335
                  g_array_set_size (array, *out_size);
 
336
                  *lgi_guard_create (L, (GDestroyNotify)
 
337
                                     (transfer == GI_TRANSFER_EVERYTHING
 
338
                                      ? array_detach : g_array_unref)) = array;
 
339
                  break;
 
340
 
 
341
                case GI_ARRAY_TYPE_PTR_ARRAY:
 
342
                  parent = LGI_PARENT_FORCE_POINTER;
 
343
                  array = (GArray *) g_ptr_array_sized_new (total_size);
 
344
                  g_ptr_array_set_size ((GPtrArray *) array, total_size);
 
345
                  *lgi_guard_create (L, (GDestroyNotify)
 
346
                                     (transfer == GI_TRANSFER_EVERYTHING
 
347
                                      ? ptr_array_detach :
 
348
                                      g_ptr_array_unref)) = array;
 
349
                  break;
 
350
 
 
351
                case GI_ARRAY_TYPE_BYTE_ARRAY:
 
352
                  array = (GArray *) g_byte_array_sized_new (total_size);
 
353
                  g_byte_array_set_size ((GByteArray *) array, *out_size);
 
354
                  *lgi_guard_create (L, (GDestroyNotify)
 
355
                                     (transfer == GI_TRANSFER_EVERYTHING
 
356
                                      ? byte_array_detach :
 
357
                                      g_byte_array_unref)) = array;
 
358
                  break;
 
359
                }
321
360
              vals = 1;
322
361
            }
323
362
 
331
370
                 array. */
332
371
              to_pop = lgi_marshal_2c (L, eti, NULL, exfer,
333
372
                                       array->data + index * esize, -1,
334
 
                                       0, NULL, NULL);
 
373
                                       parent, NULL, NULL);
335
374
 
336
375
              /* Remove temporary element from the stack. */
337
376
              lua_remove (L, - to_pop - 1);
343
382
 
344
383
          /* Return either GArray or direct pointer to the data,
345
384
             according to the array type. */
346
 
          *out_array = (atype == GI_ARRAY_TYPE_ARRAY || array == NULL)
347
 
            ? (void *) array : (void *) array->data;
 
385
          if (array == NULL)
 
386
            *out_array = NULL;
 
387
          else 
 
388
            switch (atype)
 
389
              {
 
390
              case GI_ARRAY_TYPE_C:
 
391
                *out_array = (void *) array->data;
 
392
                break;
 
393
 
 
394
              case GI_ARRAY_TYPE_ARRAY:
 
395
              case GI_ARRAY_TYPE_PTR_ARRAY:
 
396
              case GI_ARRAY_TYPE_BYTE_ARRAY:
 
397
                *out_array = (void *) array;
 
398
                break;
 
399
              }
348
400
        }
349
401
 
350
402
      lua_remove (L, eti_guard);
376
428
          data = ((GArray *) array)->data;
377
429
        }
378
430
    }
 
431
  else if (atype == GI_ARRAY_TYPE_BYTE_ARRAY)
 
432
    {
 
433
      if (array)
 
434
        {
 
435
          len = ((GByteArray *) array)->len;
 
436
          data = (char *) ((GByteArray *) array)->data;
 
437
        }
 
438
    }
 
439
  else if (atype == GI_ARRAY_TYPE_PTR_ARRAY)
 
440
    {
 
441
      if (array)
 
442
        {
 
443
          len = ((GPtrArray *) array)->len;
 
444
          data = (char *) ((GPtrArray *) array)->pdata;
 
445
          parent = LGI_PARENT_FORCE_POINTER;
 
446
        }
 
447
    }
379
448
  else
380
449
    {
381
450
      data = array;
396
465
  eti = g_type_info_get_param_type (ti, 0);
397
466
  lgi_gi_info_new (L, eti);
398
467
  eti_guard = lua_gettop (L);
399
 
  esize = array_get_elt_size (eti);
 
468
  esize = array_get_elt_size (eti, atype == GI_ARRAY_TYPE_PTR_ARRAY);
400
469
 
401
470
  /* Note that we ignore is_pointer check for uint8 type.  Although it
402
471
     is not exactly correct, we probably would not handle uint8*
454
523
    {
455
524
      if (atype == GI_ARRAY_TYPE_ARRAY)
456
525
        g_array_free (array, TRUE);
 
526
      else if (atype == GI_ARRAY_TYPE_BYTE_ARRAY)
 
527
        g_byte_array_free (array, TRUE);
 
528
      else if (atype == GI_ARRAY_TYPE_PTR_ARRAY)
 
529
        g_ptr_array_free (array, TRUE);
457
530
      else
458
531
        g_free (array);
459
532
    }
1073
1146
 
1074
1147
                /* Currently only fixed-size arrays are supported. */
1075
1148
                elt_size =
1076
 
                  array_get_elt_size (g_type_info_get_param_type (ti, 0));
 
1149
                  array_get_elt_size (g_type_info_get_param_type (ti, 0), FALSE);
1077
1150
                size = g_type_info_get_array_fixed_size (ti);
1078
1151
                g_assert (size > 0);
1079
1152