File: seed-structs.c

package info (click to toggle)
seed-webkit2 4.0.0%2B20161014%2B6c77960%2Bdfsg1-6
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 2,760 kB
  • sloc: ansic: 25,104; makefile: 1,023; xml: 206; python: 173; sh: 46
file content (776 lines) | stat: -rw-r--r-- 24,069 bytes parent folder | download | duplicates (2)
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
/* -*- mode: C; indent-tabs-mode: t; tab-width: 8; c-basic-offset: 2; -*- */

/*
 * This file is part of Seed, the GObject Introspection<->Javascript bindings.
 *
 * Seed 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.
 * Seed 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 Seed.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Copyright (C) Robert Carr 2009 <carrr@rpi.edu>
 */

#include "seed-private.h"
JSClassRef seed_struct_class = 0;
JSClassRef seed_union_class = 0;
JSClassRef seed_pointer_class = 0;
JSClassRef seed_boxed_class = 0;

typedef struct _seed_struct_privates
{
    gpointer pointer;
    GIBaseInfo* info;

    gboolean free_pointer;
    gboolean slice_alloc;
    gsize size;
} seed_struct_privates;

GHashTable* struct_prototype_hash = NULL;
GHashTable* union_prototype_hash = NULL;

static void
seed_pointer_finalize(JSObjectRef object)
{
    seed_struct_privates* priv
      = (seed_struct_privates*) JSObjectGetPrivate(object);

    SEED_NOTE(STRUCTS, "Finalizing seed_pointer object %p. with "
                       "priv->free_pointer = %d with type: %s, size: %zu",
              priv->pointer, priv->free_pointer,
              priv->info ? g_base_info_get_name(priv->info) : "[generic]",
              priv->size);

    if (priv->free_pointer) {
        if (priv->slice_alloc)
            g_slice_free1(priv->size, priv->pointer);
        else
            g_free(priv->pointer);
    }
    if (priv->info)
        g_base_info_unref(priv->info);

    g_slice_free1(sizeof(seed_struct_privates), priv);
}

static void
seed_boxed_finalize(JSObjectRef object)
{
    seed_struct_privates* priv
      = (seed_struct_privates*) JSObjectGetPrivate(object);
    GType type;
    GIRegisteredTypeInfo* info
      = (GIRegisteredTypeInfo*) g_base_info_get_type(priv->info);

    SEED_NOTE(STRUCTS, "Finalizing boxed object of type %s \n",
              g_base_info_get_name(priv->info));

    type = g_registered_type_info_get_g_type(info);
    g_base_info_unref((GIBaseInfo*) info);

    g_boxed_free(type, priv->pointer);
}

GIFieldInfo*
seed_union_find_field(GIUnionInfo* info, gchar* field_name)
{
    gint n, i;
    GIFieldInfo* field;

    n = g_union_info_get_n_fields(info);
    for (i = 0; i < n; i++) {
        const gchar* name;

        field = g_union_info_get_field(info, i);
        name = g_base_info_get_name((GIBaseInfo*) field);
        if (!g_strcmp0(name, field_name))
            return field;
        else
            g_base_info_unref((GIBaseInfo*) field);
    }

    return NULL;
}

GIFieldInfo*
seed_struct_find_field(GIStructInfo* info, gchar* field_name)
{
    gint n, i;
    const gchar* name;
    GIFieldInfo* field;

    n = g_struct_info_get_n_fields(info);
    for (i = 0; i < n; i++) {

        field = g_struct_info_get_field(info, i);
        name = g_base_info_get_name((GIBaseInfo*) field);
        if (!g_strcmp0(name, field_name))
            return field;
        else
            g_base_info_unref((GIBaseInfo*) field);
    }

    return NULL;
}

JSValueRef
seed_field_get_value(JSContextRef ctx,
                     gpointer object,
                     GIFieldInfo* field,
                     JSValueRef* exception)
{
    GITypeInfo* field_type;
    GIBaseInfo* interface;
    GArgument field_value;
    JSValueRef ret = JSValueMakeNull(ctx);
    gint offset;

    field_type = g_field_info_get_type(field);
    if (!g_field_info_get_field(field, object, &field_value)) {
        GITypeTag tag;

        tag = g_type_info_get_tag(field_type);
        if (tag == GI_TYPE_TAG_INTERFACE) {
            interface = g_type_info_get_interface(field_type);
            offset = g_field_info_get_offset(field);

            g_base_info_unref((GIBaseInfo*) field_type);
            switch (g_base_info_get_type(interface)) {
                case GI_INFO_TYPE_STRUCT:
                    ret = seed_make_struct(ctx, (object + offset), interface);
                    break;
                case GI_INFO_TYPE_UNION:
                    ret = seed_make_union(ctx, (object + offset), interface);
                    break;
                case GI_INFO_TYPE_BOXED:
                    ret = seed_make_boxed(ctx, (object + offset), interface);
                    break;
                default:
                    break;
            }
            g_base_info_unref(interface);

            return ret;
        }

        return JSValueMakeNull(ctx);
    }

    // Maybe need to release argument.
    ret = seed_value_from_gi_argument(ctx, &field_value, field_type, exception);
    if (field_type)
        g_base_info_unref((GIBaseInfo*) field_type);
    return ret;
}

static JSValueRef
seed_union_get_property(JSContextRef context,
                        JSObjectRef object,
                        JSStringRef property_name,
                        JSValueRef* exception)
{
    gchar* cproperty_name;
    gsize length;
    seed_struct_privates* priv = JSObjectGetPrivate(object);
    GIFieldInfo* field = 0;
    JSValueRef ret;

    length = JSStringGetMaximumUTF8CStringSize(property_name);
    cproperty_name = g_alloca(length * sizeof(gchar));
    JSStringGetUTF8CString(property_name, cproperty_name, length);

    SEED_NOTE(STRUCTS, "Getting property on union of type: %s "
                       "with name %s \n",
              g_base_info_get_name(priv->info), cproperty_name);

    field = seed_union_find_field((GIUnionInfo*) priv->info, cproperty_name);
    if (!field) {
        return 0;
    }

    ret = seed_field_get_value(context, priv->pointer, field, exception);

    g_base_info_unref((GIBaseInfo*) field);

    return ret;
}

static bool
seed_union_set_property(JSContextRef context,
                        JSObjectRef object,
                        JSStringRef property_name,
                        JSValueRef value,
                        JSValueRef* exception)
{
    gsize length;
    GArgument field_value;
    GIFieldInfo* field;
    gchar* cproperty_name;
    GITypeInfo* field_type;
    seed_struct_privates* priv
      = (seed_struct_privates*) JSObjectGetPrivate(object);
    gboolean ret;

    length = JSStringGetMaximumUTF8CStringSize(property_name);
    cproperty_name = g_alloca(length * sizeof(gchar));
    JSStringGetUTF8CString(property_name, cproperty_name, length);

    SEED_NOTE(STRUCTS, "Setting property on union of type: %s  "
                       "with name %s \n",
              g_base_info_get_name(priv->info), cproperty_name);

    field = seed_union_find_field((GIUnionInfo*) priv->info, cproperty_name);

    if (!field) {
        return FALSE;
    }

    field_type = g_field_info_get_type(field);

    seed_value_to_gi_argument(context, value, field_type, GI_TRANSFER_NOTHING,
                              &field_value, exception);

    ret = g_field_info_set_field(field, priv->pointer, &field_value);
    if (!ret)
        g_warning("Setting property failed on union of type: %s  "
                  "with name %s \n",
                  g_base_info_get_name(priv->info), cproperty_name);

    g_base_info_unref((GIBaseInfo*) field_type);
    g_base_info_unref((GIBaseInfo*) field);

    return TRUE;
}

static bool
seed_struct_set_property(JSContextRef context,
                         JSObjectRef object,
                         JSStringRef property_name,
                         JSValueRef value,
                         JSValueRef* exception)
{
    gsize length;
    GArgument field_value;
    GIFieldInfo* field;
    gchar* cproperty_name;
    GITypeInfo* field_type;
    seed_struct_privates* priv
      = (seed_struct_privates*) JSObjectGetPrivate(object);
    gboolean ret;

    length = JSStringGetMaximumUTF8CStringSize(property_name);
    cproperty_name = g_alloca(length * sizeof(gchar));
    JSStringGetUTF8CString(property_name, cproperty_name, length);

    SEED_NOTE(STRUCTS, "Setting property on struct of type: %s  "
                       "with name %s \n",
              g_base_info_get_name(priv->info), cproperty_name);

    field = seed_struct_find_field((GIStructInfo*) priv->info, cproperty_name);

    if (!field) {
        return FALSE;
    }

    field_type = g_field_info_get_type(field);

    seed_value_to_gi_argument(context, value, field_type, GI_TRANSFER_NOTHING,
                              &field_value, exception);
    ret = g_field_info_set_field(field, priv->pointer, &field_value);

    if (!ret)
        g_warning("Setting property failed on struct of type: %s  "
                  "with name %s \n",
                  g_base_info_get_name(priv->info), cproperty_name);

    g_base_info_unref((GIBaseInfo*) field_type);
    g_base_info_unref((GIBaseInfo*) field);

    return TRUE;
}

static JSValueRef
seed_struct_get_property(JSContextRef context,
                         JSObjectRef object,
                         JSStringRef property_name,
                         JSValueRef* exception)
{
    gchar* cproperty_name;
    gsize length;
    seed_struct_privates* priv = JSObjectGetPrivate(object);
    GIFieldInfo* field = NULL;
    JSValueRef ret;

    length = JSStringGetMaximumUTF8CStringSize(property_name);
    cproperty_name = g_alloca(length * sizeof(gchar));
    JSStringGetUTF8CString(property_name, cproperty_name, length);

    SEED_NOTE(STRUCTS, "Getting property on struct of type: %s  "
                       "with name %s \n",
              g_base_info_get_name(priv->info), cproperty_name);

    // for a gvalue, it has a special property 'value' (read-only)
    GType gtype
      = g_registered_type_info_get_g_type((GIRegisteredTypeInfo*) priv->info);

    if (g_type_is_a(gtype, G_TYPE_VALUE)
        && !g_strcmp0(cproperty_name, "value")) {
        return seed_value_from_gvalue(context, (GValue*) priv->pointer,
                                      exception);
    }

    field = seed_struct_find_field((GIStructInfo*) priv->info, cproperty_name);

    if (!field) {
        return NULL;
    }

    ret = seed_field_get_value(context, priv->pointer, field, exception);

    g_base_info_unref((GIBaseInfo*) field);

    return ret;
}

static void
seed_enumerate_structlike_properties(JSContextRef ctx,
                                     JSObjectRef object,
                                     JSPropertyNameAccumulatorRef propertyNames)
{
    GIFieldInfo* field;
    gint i, n;
    guchar type = 0;
    seed_struct_privates* priv
      = (seed_struct_privates*) JSObjectGetPrivate(object);
    GIBaseInfo* info = priv->info;

    if (!info)
        return;

    if (JSValueIsObjectOfClass(ctx, object, seed_struct_class))
        type = 1;
    else if (JSValueIsObjectOfClass(ctx, object, seed_union_class))
        type = 2;
    else
        g_assert_not_reached();

    (type == 1) ? (n = g_struct_info_get_n_fields((GIStructInfo*) info))
                : (n = g_union_info_get_n_fields((GIUnionInfo*) info));

    for (i = 0; i < n; i++) {
        JSStringRef jname;

        (type == 1) ? (field = g_struct_info_get_field((GIStructInfo*) info, i))
                    : (field = g_union_info_get_field((GIUnionInfo*) info, i));

        jname = JSStringCreateWithUTF8CString(
          g_base_info_get_name((GIBaseInfo*) field));

        g_base_info_unref((GIBaseInfo*) field);
        JSPropertyNameAccumulatorAddName(propertyNames, jname);

        JSStringRelease(jname);
    }
}

JSClassDefinition seed_pointer_def = {
    0,                    /* Version, always 0 */
    0,    "seed_pointer", /* Class Name */
    NULL,                 /* Parent Class */
    NULL,                 /* Static Values */
    NULL,                 /* Static Functions */
    NULL, seed_pointer_finalize,
    NULL,       /* Has Property */
    0,    NULL, /* Set Property */
    NULL,       /* Delete Property */
    NULL, NULL, /* Call As Function */
    NULL,       /* Call As Constructor */
    NULL,       /* Has Instance */
    NULL        /* Convert To Type */
};

JSClassDefinition seed_struct_def = {
    0, /* Version, always 0 */
    kJSClassAttributeNoAutomaticPrototype,
    "seed_struct", /* Class Name */
    NULL,          /* Parent Class */
    NULL,          /* Static Values */
    NULL,          /* Static Functions */
    NULL,
    NULL,
    NULL, /* Has Property */
    seed_struct_get_property,
    seed_struct_set_property,             /* Set Property */
    NULL,                                 /* Delete Property */
    seed_enumerate_structlike_properties, /* Get Property Names */
    NULL,                                 /* Call As Function */
    NULL,                                 /* Call As Constructor */
    NULL,                                 /* Has Instance */
    NULL                                  /* Convert To Type */
};

JSClassDefinition seed_union_def = {
    0, /* Version, always 0 */
    kJSClassAttributeNoAutomaticPrototype,
    "seed_union", /* Class Name */
    NULL,         /* Parent Class */
    NULL,         /* Static Values */
    NULL,         /* Static Functions */
    NULL,
    NULL,
    NULL, /* Has Property */
    seed_union_get_property,
    seed_union_set_property,              /* Set Property */
    NULL,                                 /* Delete Property */
    seed_enumerate_structlike_properties, /* Get Property Names */
    NULL,                                 /* Call As Function */
    NULL,                                 /* Call As Constructor */
    NULL,                                 /* Has Instance */
    NULL                                  /* Convert To Type */
};

JSClassDefinition seed_boxed_def = {
    0, /* Version, always 0 */
    kJSClassAttributeNoAutomaticPrototype,
    "seed_boxed", /* Class Name */
    NULL,         /* Parent Class */
    NULL,         /* Static Values */
    NULL,         /* Static Functions */
    NULL,
    seed_boxed_finalize,
    NULL, /* Has Property */
    NULL,
    NULL, /* Set Property */
    NULL, /* Delete Property */
    NULL, /* Get Property Names */
    NULL, /* Call As Function */
    NULL, /* Call As Constructor */
    NULL, /* Has Instance */
    NULL  /* Convert To Type */
};

gpointer
seed_pointer_get_pointer(JSContextRef ctx, JSValueRef pointer)
{
    if (JSValueIsObjectOfClass(ctx, pointer, seed_pointer_class)) {
        seed_struct_privates* priv = JSObjectGetPrivate((JSObjectRef) pointer);
        return priv->pointer;
    }
    return NULL;
}

void
seed_pointer_set_free(JSContextRef ctx,
                      JSValueRef pointer,
                      gboolean free_pointer)
{
    if (JSValueIsObjectOfClass(ctx, pointer, seed_pointer_class)) {
        seed_struct_privates* priv = JSObjectGetPrivate((JSObjectRef) pointer);
        priv->free_pointer = free_pointer;
    }
}

static void
seed_pointer_set_slice(JSContextRef ctx,
                       JSValueRef pointer,
                       gboolean free_pointer,
                       gsize size)
{
    seed_struct_privates* priv = JSObjectGetPrivate((JSObjectRef) pointer);
    priv->slice_alloc = free_pointer;
    priv->size = size;
}

JSObjectRef
seed_make_pointer(JSContextRef ctx, gpointer pointer)
{
    seed_struct_privates* priv = g_slice_alloc(sizeof(seed_struct_privates));
    priv->pointer = pointer;
    priv->info = 0;
    priv->free_pointer = FALSE;

    return JSObjectMake(ctx, seed_pointer_class, priv);
}

JSObjectRef
seed_union_prototype(JSContextRef ctx, GIBaseInfo* info)
{
    JSObjectRef proto;
    const gchar* namespace, *name;
    gchar* key;
    gint n_methods, i;
    GIFunctionInfo* finfo;

    name = g_base_info_get_name(info);
    namespace = g_base_info_get_namespace(info);
    key = g_strjoin(NULL, namespace, name, NULL);

    proto = (JSObjectRef) g_hash_table_lookup(union_prototype_hash, key);

    if (!proto) {
        proto = JSObjectMake(ctx, 0, 0);
        JSValueProtect(eng->context, proto);

        n_methods = g_union_info_get_n_methods((GIUnionInfo*) info);
        for (i = 0; i < n_methods; i++) {
            finfo = g_union_info_get_method((GIUnionInfo*) info, i);

            seed_gobject_define_property_from_function_info(
              ctx, (GIFunctionInfo*) finfo, proto, TRUE);

            g_base_info_unref((GIBaseInfo*) finfo);
        }

        g_hash_table_insert(union_prototype_hash, key, proto);
    } else {
        g_free(key);
    }

    return proto;
}

JSObjectRef
seed_make_union(JSContextRef ctx, gpointer younion, GIBaseInfo* info)
{
    JSObjectRef object;

    if (younion == NULL) {
        return (JSObjectRef) JSValueMakeNull(ctx);
    }

    seed_struct_privates* priv = g_slice_alloc(sizeof(seed_struct_privates));

    priv->pointer = younion;
    priv->info = info ? g_base_info_ref(info) : 0;
    priv->free_pointer = FALSE;

    object = JSObjectMake(ctx, seed_union_class, priv);

    if (info) {
        JSObjectRef proto = seed_union_prototype(ctx, info);
        if (proto)
            JSObjectSetPrototype(ctx, object, proto);
        else
            g_assert_not_reached();
    }

    return object;
}

JSObjectRef
seed_make_boxed(JSContextRef ctx, gpointer boxed, GIBaseInfo* info)
{
    JSObjectRef object;
    seed_struct_privates* priv = g_slice_alloc(sizeof(seed_struct_privates));

    priv->info = info ? g_base_info_ref(info) : 0;
    priv->pointer = boxed;
    // Boxed finalize handler handles freeing.
    priv->free_pointer = FALSE;

    object = JSObjectMake(ctx, seed_boxed_class, priv);

    // FIXME: Instance methods?

    return object;
}

JSObjectRef
seed_struct_prototype(JSContextRef ctx, GIBaseInfo* info)
{
    JSObjectRef proto;
    const gchar* namespace, *name;
    gchar* key;
    gint n_methods, i;

    name = g_base_info_get_name(info);
    namespace = g_base_info_get_namespace(info);
    key = g_strjoin(NULL, namespace, name, NULL);

    proto = (JSObjectRef) g_hash_table_lookup(struct_prototype_hash, key);

    if (!proto) {
        proto = JSObjectMake(ctx, 0, 0);
        JSValueProtect(eng->context, proto);

        n_methods = g_struct_info_get_n_methods((GIStructInfo*) info);
        for (i = 0; i < n_methods; i++) {
            GIFunctionInfo* finfo;

            finfo = g_struct_info_get_method((GIStructInfo*) info, i);

            seed_gobject_define_property_from_function_info(
              ctx, (GIFunctionInfo*) finfo, proto, TRUE);

            g_base_info_unref((GIBaseInfo*) finfo);
        }

        g_hash_table_insert(struct_prototype_hash, key, proto);
    } else {
        g_free(key);
    }

    return proto;
}

JSObjectRef
seed_make_struct(JSContextRef ctx, gpointer strukt, GIBaseInfo* info)
{
    JSObjectRef object, proto;

    if (strukt == NULL) {
        return (JSObjectRef) JSValueMakeNull(ctx);
    }

    seed_struct_privates* priv = g_slice_alloc(sizeof(seed_struct_privates));

    priv->info = info ? g_base_info_ref(info) : 0;
    priv->pointer = strukt;
    priv->free_pointer = FALSE;

    object = JSObjectMake(ctx, seed_struct_class, priv);
    // Examine cases where struct is being used without info.
    if (info) {
        proto = seed_struct_prototype(ctx, info);
        if (proto)
            JSObjectSetPrototype(ctx, object, proto);
        else
            g_assert_not_reached();
    }

    return object;
}

void
seed_structs_init(void)
{
    seed_pointer_class = JSClassCreate(&seed_pointer_def);
    seed_struct_def.parentClass = seed_pointer_class;
    seed_struct_class = JSClassCreate(&seed_struct_def);
    seed_union_def.parentClass = seed_pointer_class;
    seed_union_class = JSClassCreate(&seed_union_def);
    seed_boxed_def.parentClass = seed_struct_class;
    seed_boxed_class = JSClassCreate(&seed_boxed_def);

    struct_prototype_hash = g_hash_table_new(g_str_hash, g_str_equal);
    union_prototype_hash = g_hash_table_new(g_str_hash, g_str_equal);
}

JSObjectRef
seed_construct_struct_type_with_parameters(JSContextRef ctx,
                                           GIBaseInfo* info,
                                           JSObjectRef parameters,
                                           JSValueRef* exception)
{
    gsize size = 0;
    gpointer object;
    GIInfoType type = g_base_info_get_type(info);
    JSObjectRef ret;
    gboolean set_ret;
    gint nparams, i = 0;
    gsize length;
    GIFieldInfo* field = 0;
    JSPropertyNameArrayRef jsprops;
    JSStringRef jsprop_name;
    JSValueRef jsprop_value;
    GArgument field_value;
    gchar* prop_name;
    GITypeInfo* field_type;

    if (type == GI_INFO_TYPE_STRUCT) {
        GType gtype
          = g_registered_type_info_get_g_type((GIRegisteredTypeInfo*) info);
        if (g_type_is_a(gtype, G_TYPE_VALUE)) {
            GValue* gval = g_slice_alloc0(sizeof(GValue));
            if (!parameters) {
                seed_make_exception(ctx, exception, "ArgumentError",
                                    "Missing Type in GValue constructor");
                return (JSObjectRef) JSValueMakeNull(ctx);
            }
            SEED_NOTE(CONSTRUCTION, "Created a GValue  struct");
            seed_value_to_gvalue(ctx, parameters, 0, gval, exception);
            ret = seed_make_struct(ctx, (gpointer) gval, info);
            return ret;
        }

        size = g_struct_info_get_size((GIStructInfo*) info);
    } else {
        size = g_union_info_get_size((GIUnionInfo*) info);
    }
    if (!size) {
        g_critical("Struct/union of type: %s has size 0 in introspection data. "
                   "Please check GIR",
                   g_base_info_get_name(info));
        g_assert(size);
    }
    object = g_slice_alloc0(size);

    SEED_NOTE(CONSTRUCTION,
              "Constructing struct/union of type: %s. Size: %zu \n",
              g_base_info_get_name(info), size);

    if (type == GI_INFO_TYPE_STRUCT)
        ret = seed_make_struct(ctx, object, info);
    else
        ret = seed_make_union(ctx, object, info);

    seed_pointer_set_free(ctx, ret, TRUE);
    seed_pointer_set_slice(ctx, ret, TRUE, size);

    if (!parameters)
        return ret;

    jsprops = JSObjectCopyPropertyNames(ctx, (JSObjectRef) parameters);
    nparams = JSPropertyNameArrayGetCount(jsprops);

    while (i < nparams) {
        jsprop_name = JSPropertyNameArrayGetNameAtIndex(jsprops, i);

        length = JSStringGetMaximumUTF8CStringSize(jsprop_name);
        prop_name = g_alloca(length * sizeof(gchar));
        JSStringGetUTF8CString(jsprop_name, prop_name, length);

        if (type == GI_INFO_TYPE_STRUCT)
            field = seed_struct_find_field((GIStructInfo*) info, prop_name);
        else
            field = seed_union_find_field((GIUnionInfo*) info, prop_name);
        if (!field) {
            seed_make_exception(ctx, exception, "PropertyError",
                                "Invalid property for construction: %s",
                                prop_name);

            JSPropertyNameArrayRelease(jsprops);
            return (JSObjectRef) JSValueMakeNull(ctx);
        }
        field_type = g_field_info_get_type(field);

        jsprop_value = JSObjectGetProperty(ctx, (JSObjectRef) parameters,
                                           jsprop_name, NULL);

        seed_value_to_gi_argument(ctx, jsprop_value, field_type,
                                  GI_TRANSFER_NOTHING, &field_value, exception);

        set_ret = g_field_info_set_field(field, object, &field_value);

        if (!set_ret)
            g_warning(
              "Constructor setting property failed on struct of type: %s "
              "with name %s \n",
              g_base_info_get_name(info), prop_name);

        g_base_info_unref((GIBaseInfo*) field_type);
        g_base_info_unref((GIBaseInfo*) field);

        i++;
    }
    JSPropertyNameArrayRelease(jsprops);

    return ret;
}