-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDirect.xs
1589 lines (1269 loc) · 38.6 KB
/
Direct.xs
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
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#include "ppport.h"
#include <webgpu/webgpu.h>
// Call new if possible, otherwise return fields
SV *_coerce_obj( SV *CLASS, SV *fields );
SV *_new( SV *CLASS, SV *fields );
SV * _get_mg_obj(pTHX_ SV *obj, SV *base)
{
if ( !SvOK(obj) )
{
return NULL;
}
obj = _coerce_obj(base, obj);
if ( !sv_isobject(obj) )
{
croak("%s is not an object", SvPVbyte_nolen(obj));
}
if ( base )
{
if (!sv_derived_from(obj, SvPVbyte_nolen(base)))
{
croak("_get_struct_ptr: %s is not of type %s", SvPVbyte_nolen(obj), SvPVbyte_nolen(base));
}
}
return obj;
}
SV * _get_mg_hash(pTHX_ SV *obj, SV *base)
{
SV *result = _get_mg_obj(aTHX_ obj, base);
return result == NULL ? NULL : SvRV(result);
}
void * _get_mg(pTHX_ SV *obj, SV *base)
{
SV *h = _get_mg_hash(aTHX_ obj, base);
MAGIC *mg = mg_find(h, PERL_MAGIC_ext);
return mg;
}
void * _get_struct_ptr(pTHX_ SV *obj, SV *base)
{
MAGIC *mg = _get_mg(aTHX_ obj, base);
if ( mg == NULL )
{
return NULL;
}
return mg->mg_ptr;
}
PERL_STATIC_INLINE
SV **nn_hv_store(pTHX_ HV *h, const char *key, I32 klen, SV *obj, SV *base)
{
SvREFCNT_inc(obj);
SV ** f = hv_store(h, key, klen, obj, 0);
if ( !f )
{
SvREFCNT_dec(obj);
croak("Could not save value to hash for %s in type %s", key, SvPV_nolen(base));
}
return f;
}
PERL_STATIC_INLINE
SV **nn_av_store(pTHX_ AV *h, const Size_t idx, SV *obj, SV *base)
{
SvREFCNT_inc(obj);
SV ** f = av_store(h, idx, obj);
if ( !f )
{
SvREFCNT_dec(obj);
croak("Could not save value to array for item %zd in type %s", idx, SvPV_nolen(base));
}
return f;
}
SV *_void__wrap( const void *n )
{
SV *h = newSViv( *(IV *)n);
SV *RETVAL = sv_2mortal(newRV(h));
sv_magicext((SV *)h, NULL, PERL_MAGIC_ext, NULL, (const char *)n, 0);
sv_bless(RETVAL, gv_stashpv("WebGPU::Direct::Opaque", GV_ADD));
return SvREFCNT_inc(RETVAL);
}
SV *_coerce_obj( SV *CLASS, SV *fields )
{
// If its already an object, don't bother
if ( sv_isobject(fields) )
{
return fields;
}
// If it's null or not a hashref, don't bother either
if ( CLASS == NULL )
{
return fields;
}
if ( !SvROK(fields) )
{
return fields;
}
if ( SvTYPE(SvRV(fields)) != SVt_PVHV
&& SvTYPE(SvRV(fields)) != SVt_PVAV )
{
return fields;
}
return _new(CLASS, fields);
}
SV *_new( SV *CLASS, SV *fields )
{
dSP;
ENTER;
SAVETMPS;
PUSHMARK(SP);
EXTEND(SP, 2);
PUSHs(CLASS);
if ( fields != NULL )
{
EXTEND(SP, 1);
PUSHs(fields);
}
PUTBACK;
int count = call_method("new", G_SCALAR);
SPAGAIN;
if (count != 1)
{
croak("Could not call new on %s\n", SvPV_nolen(CLASS));
}
SV *THIS = SvREFCNT_inc(POPs);
PUTBACK;
FREETMPS;
LEAVE;
return THIS;
}
void _unpack( SV *THIS )
{
// Do not attempt to call unpack on undef
if ( !SvOK(THIS) )
{
return;
}
dSP;
PUSHMARK(SP);
EXTEND(SP, 1);
PUSHs(THIS);
PUTBACK;
int count = call_method("unpack", G_SCALAR);
if (count != 1)
{
croak("Could not call unpack on %s\n", SvPV_nolen(THIS));
}
return;
}
void _pack( SV *THIS )
{
// Do not attempt to call unpack on undef
if ( !SvOK(THIS) )
{
return;
}
dSP;
PUSHMARK(SP);
EXTEND(SP, 1);
PUSHs(THIS);
PUTBACK;
int count = call_method("pack", G_SCALAR);
if (count != 1)
{
croak("Could not call pack on %s\n", SvPV_nolen(THIS));
}
return;
}
// Generate a new, empty blessed hashref that can be unpacked into
SV *_empty_with( SV *CLASS, void *n)
{
if ( n == NULL )
{
return SvREFCNT_inc(newSV(0));
}
SV *h = (SV *)newHV();
SV *RETVAL = sv_2mortal(newRV(h));
sv_magicext((SV *)h, NULL, PERL_MAGIC_ext, NULL, (const char *)n, 0);
sv_bless(RETVAL, gv_stashpv(SvPV_nolen(CLASS), GV_ADD));
return SvREFCNT_inc(RETVAL);
}
// Generate a new blessed hashref that can be packed from
SV *_new_with( void *field, SV *base, Size_t size)
{
if ( field == NULL )
{
croak("_new_with requires a defined field pointer");
}
// Coerce an empty hash into an object, and copy to the field ptr
SV *href = sv_2mortal(newRV( (SV *)newHV() ));
SV *RETVAL = _get_mg_obj(aTHX_ href, base);
ASSUME( RETVAL != NULL );
MAGIC *mg = _get_mg(aTHX_ RETVAL, base);
ASSUME( mg != NULL );
void *old_ptr = mg->mg_ptr;
Copy(old_ptr, field, size, char);
mg->mg_ptr = field;
Safefree(old_ptr);
return RETVAL;
}
SV *_new_opaque( SV *CLASS, void *n)
{
if ( n == NULL )
{
return SvREFCNT_inc(newSV(0));
}
SV *h = newSViv( (Size_t)n);
SV *RETVAL = sv_2mortal(newRV(h));
sv_magicext((SV *)h, NULL, PERL_MAGIC_ext, NULL, (const char *)n, 0);
sv_bless(RETVAL, gv_stashpv(SvPV_nolen(CLASS), GV_ADD));
return SvREFCNT_inc(RETVAL);
}
/* ------------------------------------------------------------------
{
set => 'Sets the field of the struct',
unpack => 'Sets the perl hash from the struct',
pack => 'Sets the struct from the perl hash',
find => '',
store => 'Sets the perl hash and packs the object',
}
------------------------------------------------------------------ */
/* ------------------------------------------------------------------
obj
------------------------------------------------------------------ */
void _set_obj(pTHX_ SV *new_value, void *field, Size_t size, SV *base)
{
if ( !base )
{
croak("Could not find requirement base for %s", SvPV_nolen(new_value));
}
if ( !size )
{
croak("Could not find size for %s", SvPV_nolen(new_value));
}
void *v = SvOK(new_value) ? _get_struct_ptr(aTHX_ new_value, base) : NULL;
Copy(v, field, size, char);
}
int _mg_set_obj(pTHX_ SV* sv, MAGIC* mg)
{
SV *base = mg->mg_obj;
_set_obj(aTHX_ sv, (void *) mg->mg_ptr, 0, base);
return 0;
}
STATIC MGVTBL _mg_vtbl_obj = {
.svt_set = _mg_set_obj
};
SV *_unpack_obj(pTHX_ HV *h, const char *key, I32 klen, void *field, Size_t size, SV* base)
{
SV **f = NULL;
if ( field == NULL )
{
croak("The field value for objptr must not be null, for %s{%s}", SvPV_nolen(base), key);
}
f = hv_fetch(h, key, klen, 1);
if ( !( f && *f ) )
{
croak("Could not save new value for %s", key);
}
void *n = NULL;
if ( sv_isobject(*f) )
{
n = _get_struct_ptr(aTHX_ *f, base);
}
if ( n == NULL || n != field )
{
SV *obj = _empty_with(base, field);
f = nn_hv_store(aTHX_ h, key, klen, obj, base);
}
_unpack(*f);
return *f;
}
SV *_pack_obj(pTHX_ HV *h, const char *key, I32 klen, void *field, Size_t size, SV *base)
{
SV **f;
SV *fp;
if ( field == NULL )
{
croak("The field value to _pack_obj must not be null, for %s{%s}", SvPV_nolen(base), key);
}
// Find the field from the hash
f = hv_fetch(h, key, klen, 0);
// If the field is not found, create a default one
if ( !( f && *f ) )
{
SV *val = _new_with(field, base, size);
f = nn_hv_store(aTHX_ h, key, klen, val, base);
}
SV *obj = _get_mg_obj(aTHX_ *f, base);
ASSUME( obj != NULL );
if ( *f != obj )
{
// Since we know it was coerced, we can go ahead and reuse the obj
MAGIC *mg = _get_mg(aTHX_ obj, base);
ASSUME( mg != NULL );
void *old_ptr = mg->mg_ptr;
Copy(old_ptr, field, size, char);
mg->mg_ptr = field;
Safefree(old_ptr);
f = nn_hv_store(aTHX_ h, key, klen, obj, base);
}
else
{
// Save the new value to the field
// This will copy the C struct from *f to field
_set_obj(aTHX_ *f, field, size, base);
}
SvREFCNT_inc(*f);
// If the struct ptrs differ, build a new object to store
// Its a bit of action at a distance, but it reflects what the C
// level would be doing
if ( SvOK(*f) && _get_struct_ptr(aTHX_ *f, base) != field )
{
return _unpack_obj(aTHX_ h, key, klen, field, size, base);
}
return *f;
}
SV *_find_obj(pTHX_ HV *h, const char *key, I32 klen, void *field, Size_t size, SV *base)
{
SV **f;
if ( field == NULL )
{
croak("The field value to _find_obj must not be null, for %s{%s}", SvPV_nolen(base), key);
}
// Find the field from the hash
f = hv_fetch(h, key, klen, 0);
// If the field is not found, create a default one
if ( !( f && *f ) )
{
return _unpack_obj(aTHX_ h, key, klen, field, size, base);
}
return *f;
}
void _store_obj(pTHX_ HV *h, const char *key, I32 klen, void *field, Size_t size, SV* base, SV *value)
{
SV **f = nn_hv_store(aTHX_ h, key, klen, value, base);
_pack_obj(aTHX_ h, key, klen, field, size, base);
return;
}
/* ------------------------------------------------------------------
objptr
------------------------------------------------------------------ */
void _set_objptr(pTHX_ SV *new_value, void **field, SV *base)
{
if ( !base )
{
croak("Could not find requirement base for %s", SvPV_nolen(new_value));
}
void *v = SvOK(new_value) ? _get_struct_ptr(aTHX_ new_value, base) : NULL;
*field = v;
}
int _mg_set_objptr(pTHX_ SV* sv, MAGIC* mg)
{
SV *base = mg->mg_obj;
_set_objptr(aTHX_ sv, (void *) mg->mg_ptr, base);
return 0;
}
STATIC MGVTBL _mg_vtbl_objptr = {
.svt_set = _mg_set_objptr
};
SV *_unpack_objptr(pTHX_ HV *h, const char *key, I32 klen, void **field, SV* base)
{
SV **f = NULL;
if ( field == NULL )
{
croak("The field value for objptr must not be null, for %s{%s}", SvPV_nolen(base), key);
}
f = hv_fetch(h, key, klen, 1);
if ( !( f && *f ) )
{
croak("Could not save new value for %s", key);
}
void *n = NULL;
if ( sv_isobject(*f) )
{
n = _get_struct_ptr(aTHX_ *f, base);
}
if ( n == NULL || n != *field )
{
SV *obj = _empty_with(base, *field);
f = nn_hv_store(aTHX_ h, key, klen, obj, base);
}
_unpack(*f);
return *f;
}
SV *_pack_objptr(pTHX_ HV *h, const char *key, I32 klen, void **field, SV *base)
{
SV **f;
SV *fp;
if ( field == NULL )
{
croak("The field value to _pack_objptr must not be null, for %s{%s}", SvPV_nolen(base), key);
}
// Find the field from the hash
f = hv_fetch(h, key, klen, 0);
// If the field is not found, create a default one
if ( !( f && *f ) )
{
return _unpack_objptr(aTHX_ h, key, klen, field, base);
}
// If _new returns something different, it coerced it up to an object
SV *obj = _coerce_obj(base, *f);
if ( obj != *f )
{
f = nn_hv_store(aTHX_ h, key, klen, obj, base);
}
// Save the new value to the field
_set_objptr(aTHX_ *f, field, base);
SvREFCNT_inc(*f);
_pack(*f);
return *f;
}
SV *_find_objptr(pTHX_ HV *h, const char *key, I32 klen, void **field, SV *base)
{
SV **f;
if ( field == NULL )
{
croak("The field value to _find_objptr must not be null, for %s{%s}", SvPV_nolen(base), key);
}
// Find the field from the hash
f = hv_fetch(h, key, klen, 0);
// If the field is not found, create a default one
if ( !( f && *f ) )
{
return _unpack_objptr(aTHX_ h, key, klen, field, base);
}
return *f;
}
void _store_objptr(pTHX_ HV *h, const char *key, I32 klen, void **field, SV* base, SV *value)
{
SV **f = nn_hv_store(aTHX_ h, key, klen, value, base);
_pack_objptr(aTHX_ h, key, klen, field, base);
return;
}
/* ------------------------------------------------------------------
objarray
------------------------------------------------------------------ */
SV * _array_new(SV *base, void *n, Size_t size, Size_t count)
{
/* Make sure the count is less than an excessive limit */
if ( count > 268435456 )
{
croak("Array count of %zd cannot be that large", count);
}
bool is_enum = sv_derived_from(base, "WebGPU::Direct::Enum");
bool is_opaque = sv_derived_from(base, "WebGPU::Direct::Opaque");
if ( is_enum && size != sizeof(uint32_t) )
{
croak("Enum is expected to be of size %zu, not %zu", sizeof(uint32_t), size);
}
AV *ret = newAV();
av_extend(ret, count);
void *field = n;
for ( Size_t i = 0; i < count; i++ )
{
SV *obj = NULL;
if ( is_enum )
{
obj = _new(base, newSViv(*(uint32_t *)field));
}
else if ( is_opaque )
{
obj = _new_opaque(base, field);
}
else
{
obj = _new_with(field, base, size);
}
SV **f = nn_av_store(aTHX_ ret, i, obj, base);
field = ((char *)field) + size;
}
SV *aref = sv_2mortal(newRV((SV*)ret));
SV *array_base = newSVsv(base);
sv_catpvs(array_base, "::Array");
sv_bless(aref, gv_stashpv(SvPV_nolen(array_base), GV_ADD));
return aref;
}
void _set_objarray(pTHX_ SV *new_value, void **field, Size_t *cntField, Size_t size, SV *base)
{
if ( !base )
{
croak("Could not find requirement base for %s", SvPV_nolen(new_value));
}
if ( new_value == NULL || !SvOK(new_value) )
{
*field = NULL;
*cntField = 0;
return;
}
if ( !SvROK(new_value) )
{
croak("The field value to _set_objarray must be a reference, for %s", SvPV_nolen(base));
}
if ( SvTYPE(SvRV(new_value)) != SVt_PVAV )
{
croak("The field value to _set_objarray must be an array reference, for %s", SvPV_nolen(base));
}
void *v = _get_struct_ptr(aTHX_ new_value, base);
*field = v;
*cntField = av_count(SvRV(new_value));
}
SV *_unpack_objarray(pTHX_ HV *h, const char *key, I32 klen, void **field, Size_t *cntField, Size_t size, SV* base)
{
SV **f = NULL;
SV **c = NULL;
SV *keyCnt = NULL;
Size_t count = 0;
if ( field == NULL )
{
croak("The field value for objarray must not be null, for %s{%s}", SvPV_nolen(base), key);
}
f = hv_fetch(h, key, klen, 1);
if ( !( f && *f ) )
{
croak("Could not save new value for %s", key);
}
// -1 so it removes the s at the end of the key
keyCnt = newSVpv(key, klen-1);
sv_catpvs(keyCnt, "Count");
Size_t keyCntLen = sv_len(keyCnt);
c = hv_fetch(h, SvPV_nolen(keyCnt), keyCntLen, 1);
if ( !( c && *c ) )
{
croak("Could not save new value for %s", SvPV_nolen(keyCnt));
}
*c = newSViv(*cntField);
SV *array_base = newSVsv(base);
sv_catpvs(array_base, "::Array");
void *n = NULL;
if ( sv_isobject(*f) )
{
n = _get_struct_ptr(aTHX_ *f, array_base);
}
if ( n == NULL || n != *field )
{
SV *val = _array_new(base, *field, size, *cntField);
f = nn_hv_store(aTHX_ h, key, klen, val, base);
}
return *f;
}
SV *_pack_objarray(pTHX_ HV *h, const char *key, I32 klen, void **field, Size_t *cntField, Size_t size, SV *base)
{
SV **f;
SV **c = NULL;
SV *keyCnt = NULL;
I32 keyCntLen;
Size_t count = 0;
void *arr = NULL;
AV *objs = NULL;
if ( field == NULL )
{
croak("The field value to _pack_objarray must not be null, for %s{%s}", SvPV_nolen(base), key);
}
// Find the field from the hash
f = hv_fetch(h, key, klen, 0);
// If the field is not found, create a default one
if ( !( f && *f ) )
{
return _unpack_objarray(aTHX_ h, key, klen, field, cntField, size, base);
}
// -1 so it removes the s at the end of the key
{
I32 rmlen = 1;
ASSUME( key[klen-1] == 's' );
if ( key[klen-2] == 'e' && key[klen-3] == 'i' )
{
rmlen = 3;
}
keyCnt = newSVpv(key, klen-rmlen);
if ( rmlen != 1 )
{
sv_catpvs(keyCnt, "y");
}
sv_catpvs(keyCnt, "Count");
keyCntLen = sv_len(keyCnt);
}
SV *array_base = newSVsv(base);
sv_catpvs(array_base, "::Array");
if ( !SvOK(*f) )
{
SV *val = sv_2mortal(newRV( (SV *)newAV() ));
f = nn_hv_store(aTHX_ h, key, klen, val, base);
}
if ( !SvROK(*f) )
{
croak("The field value to _pack_objarray must be a reference, for %s{%s}", SvPV_nolen(base), key);
}
if ( SvTYPE(SvRV(*f)) == SVt_PVHV )
{
AV *new_fields = newAV();
SV **arrf = nn_av_store(aTHX_ new_fields, 0, *f, base);
SV **new_array = NULL;
*f = sv_2mortal(newRV((SV*)new_fields));
}
if ( SvTYPE(SvRV(*f)) != SVt_PVAV )
{
croak("The field value to _pack_objarray must be an array, for %s{%s}", SvPV_nolen(base), key);
}
objs = (AV *)SvRV(*f);
count = av_count(objs);
if ( !sv_isobject(*f) )
{
SV *a = (SV *)newAV();
*f = sv_2mortal(newRV(a));
sv_bless(*f, gv_stashpv(SvPV_nolen(array_base), GV_ADD));
f = nn_hv_store(aTHX_ h, key, klen, *f, base);
}
arr = _get_struct_ptr(aTHX_ *f, array_base);
AV *out_objs = (AV *)SvRV(*f);
if ( arr != NULL )
{
Renew(arr, (count+1) * size, char);
}
else
{
Newxz(arr, (count+1) * size, char);
}
bool is_enum = sv_derived_from(base, "WebGPU::Direct::Enum");
bool is_opaque = sv_derived_from(base, "WebGPU::Direct::Opaque");
void *new_ptr = arr;
for ( Size_t i = 0; i < count; i++ )
{
SV **f = av_fetch(objs, i, 1);
if ( !( f && *f ) )
{
croak("Could not fetch array entry %zd, for %s{%s}", i, SvPV_nolen(base), key);
}
SV *obj = _coerce_obj(base, *f);
if ( obj != *f )
{
f = nn_av_store(aTHX_ out_objs, i, obj, base);
}
assert(SvOK(*f));
if ( is_enum )
{
*(I32 *)new_ptr = (I32)SvIV(*f);
}
else if ( is_opaque )
{
void *old_ptr = _get_struct_ptr(aTHX_ *f, base);
if ( !old_ptr )
{
croak("Could not find a %s type element at index %zu for %s", SvPV_nolen(base), i, key);
}
*(void **)new_ptr = old_ptr;
}
else
{
void *old_ptr = _get_struct_ptr(aTHX_ *f, base);
if ( old_ptr )
{
if ( old_ptr != new_ptr )
{
// TODO: object should probably be duplicated (if refcnt is minimal?)
Copy(old_ptr, new_ptr, size, char);
MAGIC *mg = _get_mg(aTHX_ *f, base);
if ( mg == NULL )
{
sv_magicext(*f, NULL, PERL_MAGIC_ext, NULL, (const char *)new_ptr, 0);
}
else
{
mg->mg_ptr = new_ptr;
}
Safefree(old_ptr);
}
}
else
{
SV *h = _get_mg_hash(aTHX_ *f, base);
sv_magicext(h, NULL, PERL_MAGIC_ext, NULL, (const char *)new_ptr, 0);
}
}
new_ptr = ((char *)new_ptr) + size;
}
Zero(new_ptr, size, char);
{
SV *cnt = newSViv(count);
SV **cnt_f = nn_hv_store(aTHX_ h, SvPV_nolen(keyCnt), keyCntLen, cnt, base);
}
SvREFCNT_inc(*f);
sv_magicext((SV *)out_objs, NULL, PERL_MAGIC_ext, NULL, (const char *)arr, 0);
_set_objarray(aTHX_ (count == 0 ? NULL : *f), field, cntField, size, array_base);
return *f;
}
SV *_find_objarray(pTHX_ HV *h, const char *key, I32 klen, void **field, Size_t *cntField, Size_t size, SV *base)
{
SV **f;
if ( field == NULL )
{
croak("The field value to _find_objarray must not be null, for %s{%s}", SvPV_nolen(base), key);
}
// Find the field from the hash
f = hv_fetch(h, key, klen, 0);
// If the field is not found, create a default one
if ( !( f && *f ) )
{
return _unpack_objarray(aTHX_ h, key, klen, field, cntField, size, base);
}
return *f;
}
void _store_objarray(pTHX_ HV *h, const char *key, I32 klen, void **field, Size_t *cntField, Size_t size, SV* base, SV *value)
{
SV **f = nn_hv_store(aTHX_ h, key, klen, value, base);
_pack_objarray(aTHX_ h, key, klen, field, cntField, size, base);
return;
}
/* ------------------------------------------------------------------
opaque (Impl)
------------------------------------------------------------------ */
void _set_opaque(pTHX_ SV *new_value, void **field, SV *base)
{
if ( !base )
{
croak("Could not find requirement base for %s", SvPV_nolen(new_value));
}
void *v = SvOK(new_value) ? _get_struct_ptr(aTHX_ new_value, base) : NULL;
*field = v;
}
int _mg_set_opaque(pTHX_ SV* sv, MAGIC* mg)
{
SV *base = mg->mg_obj;
_set_opaque(aTHX_ sv, (void *) mg->mg_ptr, base);
return 0;
}
STATIC MGVTBL _mg_vtbl_opaque = {
.svt_set = _mg_set_opaque
};
SV *_unpack_opaque(pTHX_ HV *h, const char *key, I32 klen, void **field, SV* base)
{
SV **f = NULL;
if ( field == NULL )
{
croak("The field value for void must not be null, for %s{%s}", SvPV_nolen(base), key);
}
f = hv_fetch(h, key, klen, 1);
if ( !( f && *f ) )
{
croak("Could not save new value for %s", key);
}
void *n = NULL;
if ( sv_isobject(*f) )
{
n = _get_struct_ptr(aTHX_ *f, base);
}
if ( n == NULL || n != *field )
{
SV *val = _new_opaque(base, *field);
f = nn_hv_store(aTHX_ h, key, klen, val, base);
}
return *f;
}
SV *_pack_opaque(pTHX_ HV *h, const char *key, I32 klen, void **field, SV *base)
{
SV **f;
SV *fp;
if ( field == NULL )
{
croak("The field value to _pack_opaque must not be null, for %s{%s}", SvPV_nolen(base), key);
}
// Find the field from the hash
f = hv_fetch(h, key, klen, 0);
// If the field is not found, create a default one
if ( !( f && *f ) )
{
return _unpack_opaque(aTHX_ h, key, klen, field, base);
}
// Save the new value to the field
_set_opaque(aTHX_ *f, field, base);
SvREFCNT_inc(*f);
return *f;
}
SV *_find_opaque(pTHX_ HV *h, const char *key, I32 klen, void **field, SV *base)
{
SV **f;
if ( field == NULL )
{
croak("The field value to _find_opaque must not be null, for %s{%s}", SvPV_nolen(base), key);
}
// Find the field from the hash
f = hv_fetch(h, key, klen, 0);
// If the field is not found, create a default one
if ( !( f && *f ) )
{
return _unpack_opaque(aTHX_ h, key, klen, field, base);
}
return *f;
}
void _store_opaque(pTHX_ HV *h, const char *key, I32 klen, void **field, SV* base, SV *value)
{
SV **f = nn_hv_store(aTHX_ h, key, klen, value, base);
_pack_opaque(aTHX_ h, key, klen, field, base);
return;
}