forked from rebolsource/r3
-
Notifications
You must be signed in to change notification settings - Fork 27
/
t-struct.c
1645 lines (1391 loc) · 48.3 KB
/
t-struct.c
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
//
// File: %t-strut.c
// Summary: "C struct object datatype"
// Section: datatypes
// Project: "Rebol 3 Interpreter and Run-time (Ren-C branch)"
// Homepage: https://github.com/metaeducation/ren-c/
//
//=////////////////////////////////////////////////////////////////////////=//
//
// Copyright 2014 Atronix Engineering, Inc.
// Copyright 2014-2016 Rebol Open Source Contributors
// REBOL is a trademark of REBOL Technologies
//
// See README.md and CREDITS.md for more information.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
//=////////////////////////////////////////////////////////////////////////=//
//
#include "sys-core.h"
#define STATIC_assert(e) do {(void)sizeof(char[1 - 2*!(e)]);} while(0)
//
// Get_FFType_Enum_Info_Core: C
//
// Returns whether the FFI type is integer-like (REB_INTEGER) or decimal-like
// (REB_DECIMAL). If it is neither, it gives back REB_0. Returns a symbol
// if one is applicable.
//
// !!! Previously this was a table, which was based on a duplicate of the
// FFI_TYPE constants as an enum (STRUCT_TYPE_XXX). Getting rid of the
// STRUCT_TYPE_XXX helps reduce confusion and redundancy, but there is no
// FFI_TYPE_MAX or fixed ordering guaranteed necessarily of the constants.
// Having a `switch` is worth not creating a mirror enum, however.
//
void *Get_FFType_Enum_Info_Core(
REBSTR **name_out,
enum Reb_Kind *kind_out,
unsigned short type
) {
switch (type) {
case FFI_TYPE_UINT8:
*name_out = Canon(SYM_UINT8);
*kind_out = REB_INTEGER;
return &ffi_type_uint8;
case FFI_TYPE_SINT8:
*name_out = Canon(SYM_INT8);
*kind_out = REB_INTEGER;
return &ffi_type_sint8;
case FFI_TYPE_UINT16:
*name_out = Canon(SYM_UINT16);
*kind_out = REB_INTEGER;
return &ffi_type_uint16;
case FFI_TYPE_SINT16:
*name_out = Canon(SYM_INT16);
*kind_out = REB_INTEGER;
return &ffi_type_sint16;
case FFI_TYPE_UINT32:
*name_out = Canon(SYM_UINT32);
*kind_out = REB_INTEGER;
return &ffi_type_uint32;
case FFI_TYPE_SINT32:
*name_out = Canon(SYM_INT32);
*kind_out = REB_INTEGER;
return &ffi_type_sint32;
case FFI_TYPE_UINT64:
*name_out = Canon(SYM_INT64);
*kind_out = REB_INTEGER;
return &ffi_type_uint64;
case FFI_TYPE_SINT64:
*name_out = Canon(SYM_INT64);
*kind_out = REB_INTEGER;
return &ffi_type_sint64;
case FFI_TYPE_FLOAT:
*name_out = Canon(SYM_FLOAT);
*kind_out = REB_DECIMAL;
return &ffi_type_float;
case FFI_TYPE_DOUBLE:
*name_out = Canon(SYM_DOUBLE);
*kind_out = REB_DECIMAL;
return &ffi_type_double;
case FFI_TYPE_POINTER:
*name_out = Canon(SYM_POINTER);
*kind_out = REB_0;
return &ffi_type_pointer;
// !!! SYM_INTEGER, SYM_DECIMAL, SYM_STRUCT was "-1" in original table
default:
assert(FALSE);
*name_out = NULL;
*kind_out = REB_0;
return NULL;
}
}
static void fail_if_non_accessible(const REBVAL *val)
{
if (VAL_STRUCT_INACCESSIBLE(val)) {
REBSER *data = VAL_STRUCT_DATA_BIN(val);
REBVAL i;
SET_INTEGER(&i, cast(REBUPT, SER_DATA_RAW(data)));
fail (Error(RE_BAD_MEMORY, &i, val));
}
}
static void get_scalar(
REBSTU *stu,
const struct Struct_Field *field,
REBCNT n, // element index, starting from 0
REBVAL *val
){
if (STU_INACCESSIBLE(stu)) {
//
// !!! This just sets the field to void with no error...that seems
// like a bad idea.
//
if (field->type != FFI_TYPE_STRUCT) {
SET_VOID(val);
return;
}
}
assert(n == 0 || field->is_array); // only index in field[N], not field
REBYTE *data = SER_AT(
REBYTE,
STU_DATA_BIN(stu),
STU_OFFSET(stu) + field->offset + n * field->size
);
switch (field->type) {
case FFI_TYPE_UINT8:
SET_INTEGER(val, *cast(u8*, data));
break;
case FFI_TYPE_SINT8:
SET_INTEGER(val, *cast(i8*, data));
break;
case FFI_TYPE_UINT16:
SET_INTEGER(val, *cast(u16*, data));
break;
case FFI_TYPE_SINT16:
SET_INTEGER(val, *cast(i8*, data));
break;
case FFI_TYPE_UINT32:
SET_INTEGER(val, *cast(u32*, data));
break;
case FFI_TYPE_SINT32:
SET_INTEGER(val, *cast(i32*, data));
break;
case FFI_TYPE_UINT64:
SET_INTEGER(val, *cast(u64*, data));
break;
case FFI_TYPE_SINT64:
SET_INTEGER(val, *cast(i64*, data));
break;
case FFI_TYPE_FLOAT:
SET_DECIMAL(val, *cast(float*, data));
break;
case FFI_TYPE_DOUBLE:
SET_DECIMAL(val, *cast(double*, data));
break;
case FFI_TYPE_POINTER:
if (field->is_rebval) {
assert(field->size == sizeof(REBVAL));
assert(field->dimension == 4);
memcpy(val, data, sizeof(REBVAL));
}
else
SET_INTEGER(val, cast(REBUPT, *cast(void**, data)));
break;
case FFI_TYPE_STRUCT:
{
// In order for the schema to participate in GC it must be a series.
// Currently this series is created with a single value of the root
// schema in the case of a struct expansion. This wouldn't be
// necessary if each field that was a structure offered a REBSER
// already... !!! ?? !!! ... it will be necessary if the schemas
// are to uniquely carry an ffi_type freed when they are GC'd
//
REBSER *field_1 = Make_Series(
sizeof(struct Struct_Field), 1, MKS_NONE
);
*SER_HEAD(struct Struct_Field, field_1) = *field;
SET_SERIES_LEN(field_1, 1);
MANAGE_SERIES(field_1);
REBSTU *sub_stu = Alloc_Singular_Array();
ARR_SERIES(sub_stu)->link.schema = field_1;
// In this case the structure lives at an offset inside another.
//
VAL_RESET_HEADER(val, REB_STRUCT);
val->payload.structure.stu = sub_stu;
val->payload.structure.data = STU_DATA_BIN(stu); // inside parent data
val->extra.struct_offset = data - BIN_HEAD(VAL_STRUCT_DATA_BIN(val));
assert(VAL_STRUCT_SIZE(val) == field->size); // implicit from schema
// With all fields initialized, assign canon value as singular value
//
*ARR_HEAD(sub_stu) = *val;
TERM_ARRAY_LEN(sub_stu, 1);
MANAGE_ARRAY(sub_stu);
}
break;
default:
assert(FALSE);
fail (Error(RE_MISC));
}
}
//
// Get_Struct_Var: C
//
static REBOOL Get_Struct_Var(REBSTU *stu, const REBVAL *word, REBVAL *val)
{
REBSER *fieldlist = STU_FIELDLIST(stu);
struct Struct_Field *field = SER_HEAD(struct Struct_Field, fieldlist);
REBCNT i;
for (i = 0; i < SER_LEN(fieldlist); ++i, ++field) {
if (STR_CANON(field->name) == VAL_WORD_CANON(word)) {
if (field->is_array) {
//
// Structs contain packed data for the field type in an array.
// This data cannot expand or contract, and is not in a
// Rebol-compatible format. A Rebol Array is made by
// extracting the information.
//
// !!! Perhaps a fixed-size VECTOR! could have its data
// pointer into these arrays?
//
REBARR *array = Make_Array(field->dimension);
REBCNT n;
for (n = 0; n < field->dimension; n ++) {
REBVAL elem;
get_scalar(stu, field, n, &elem);
Append_Value(array, &elem);
}
Val_Init_Block(val, array);
}
else
get_scalar(stu, field, 0, val);
return TRUE;
}
}
return FALSE; // word not found in struct's field symbols
}
#ifdef NEED_SET_STRUCT_VARS
//
// Set_Struct_Vars: C
//
static void Set_Struct_Vars(REBSTU *strut, REBVAL *blk)
{
}
#endif
//
// Struct_To_Array: C
//
// Used by MOLD to create a block.
//
// Cannot fail(), because fail() could call MOLD on a struct!, which will end
// up infinitive recursive calls
//
REBARR *Struct_To_Array(REBSTU *stu)
{
REBARR *array = Make_Array(10);
REBSER *fieldlist = STU_FIELDLIST(stu);
struct Struct_Field *field = SER_HEAD(struct Struct_Field, fieldlist);
// We are building a recursive structure. So if we did not hand each
// sub-series over to the GC then a single Free_Series() would not know
// how to free them all. There would have to be a specialized walk to
// free the resulting structure. Hence, don't invoke the GC until the
// root series being returned is done being used or is safe from GC!
//
MANAGE_ARRAY(array);
// fail_if_non_accessible(STU_TO_VAL(stu));
REBCNT i;
for(i = 0; i < SER_LEN(fieldlist); i++, field ++) {
REBVAL *val = NULL;
REBVAL *type_blk = NULL;
/* required field name */
val = Alloc_Tail_Array(array);
Val_Init_Word(val, REB_SET_WORD, field->name);
/* required type */
type_blk = Alloc_Tail_Array(array);
Val_Init_Block(type_blk, Make_Array(1));
val = Alloc_Tail_Array(VAL_ARRAY(type_blk));
if (field->type == FFI_TYPE_STRUCT) {
REBVAL *nested;
DS_PUSH_TRASH;
SET_TRASH_SAFE(DS_TOP);
nested = DS_TOP;
Val_Init_Word(val, REB_WORD, Canon(SYM_STRUCT_X));
get_scalar(stu, field, 0, nested);
val = Alloc_Tail_Array(VAL_ARRAY(type_blk));
Val_Init_Block(val, Struct_To_Array(VAL_STRUCT(nested)));
DS_DROP;
}
else {
REBSTR *name;
enum Reb_Kind kind; // dummy
Get_FFType_Enum_Info(&name, &kind, field->type);
assert(name != NULL); // !!! was not previously asserted (?)
Val_Init_Word(val, REB_WORD, name);
}
/* optional dimension */
if (field->dimension > 1) {
REBARR *dim = Make_Array(1);
REBVAL *dv = NULL;
val = Alloc_Tail_Array(VAL_ARRAY(type_blk));
Val_Init_Block(val, dim);
dv = Alloc_Tail_Array(dim);
SET_INTEGER(dv, field->dimension);
}
/* optional initialization */
if (field->dimension > 1) {
REBARR *dim = Make_Array(1);
REBCNT n = 0;
val = Alloc_Tail_Array(array);
Val_Init_Block(val, dim);
for (n = 0; n < field->dimension; n ++) {
REBVAL *dv = Alloc_Tail_Array(dim);
get_scalar(stu, field, n, dv);
}
} else {
val = Alloc_Tail_Array(array);
get_scalar(stu, field, 0, val);
}
}
return array;
}
static REBOOL same_fields(REBSER *tgt, REBSER *src)
{
struct Struct_Field *tgt_fields = SER_HEAD(struct Struct_Field, tgt);
struct Struct_Field *src_fields = SER_HEAD(struct Struct_Field, src);
if (SER_LEN(tgt) != SER_LEN(src))
return FALSE;
REBCNT n;
for(n = 0; n < SER_LEN(src); n ++) {
if (tgt_fields[n].type != src_fields[n].type) {
return FALSE;
}
if (!SAME_STR(tgt_fields[n].name, src_fields[n].name)
|| tgt_fields[n].offset != src_fields[n].offset
|| tgt_fields[n].dimension != src_fields[n].dimension
|| tgt_fields[n].size != src_fields[n].size) {
return FALSE;
}
if (tgt_fields[n].type == FFI_TYPE_STRUCT
&& ! same_fields(tgt_fields[n].fields, src_fields[n].fields)) {
return FALSE;
}
}
return TRUE;
}
static REBOOL assign_scalar_core(
REBSER *data_bin,
REBCNT offset,
struct Struct_Field *field,
REBCNT n,
const REBVAL *val
){
assert(n == 0 || field->is_array);
void *data = SER_AT(
REBYTE,
data_bin,
offset + field->offset + n * field->size
);
u64 i = 0;
double d = 0;
if (field->is_rebval) {
assert(FALSE); // need to actually adjust for correct n
assert(field->type == FFI_TYPE_POINTER);
assert(field->dimension % 4 == 0);
assert(field->size == sizeof(REBVAL));
memcpy(data, val, sizeof(REBVAL));
return TRUE;
}
REBSTR *sym; // dummy
enum Reb_Kind kind;
Get_FFType_Enum_Info(&sym, &kind, field->type);
switch (VAL_TYPE(val)) {
case REB_DECIMAL:
if (kind != REB_INTEGER && kind != REB_DECIMAL)
fail (Error_Invalid_Type(VAL_TYPE(val)));
d = VAL_DECIMAL(val);
i = (u64) d;
break;
case REB_INTEGER:
if (kind != REB_INTEGER && kind != REB_DECIMAL)
if (field->type != FFI_TYPE_POINTER)
fail (Error_Invalid_Type(VAL_TYPE(val)));
i = (u64) VAL_INT64(val);
d = (double)i;
break;
case REB_STRUCT:
if (FFI_TYPE_STRUCT != field->type)
fail (Error_Invalid_Type(VAL_TYPE(val)));
break;
default:
fail (Error_Invalid_Type(VAL_TYPE(val)));
}
switch (field->type) {
case FFI_TYPE_SINT8:
*(i8*)data = (i8)i;
break;
case FFI_TYPE_UINT8:
*(u8*)data = (u8)i;
break;
case FFI_TYPE_SINT16:
*(i16*)data = (i16)i;
break;
case FFI_TYPE_UINT16:
*(u16*)data = (u16)i;
break;
case FFI_TYPE_SINT32:
*(i32*)data = (i32)i;
break;
case FFI_TYPE_UINT32:
*(u32*)data = (u32)i;
break;
case FFI_TYPE_SINT64:
*(i64*)data = (i64)i;
break;
case FFI_TYPE_UINT64:
*(u64*)data = (u64)i;
break;
case FFI_TYPE_POINTER:
*cast(void**, data) = cast(void*, cast(REBUPT, i));
break;
case FFI_TYPE_FLOAT:
*(float*)data = (float)d;
break;
case FFI_TYPE_DOUBLE:
*(double*)data = (double)d;
break;
case FFI_TYPE_STRUCT:
if (field->size != VAL_STRUCT_SIZE(val))
fail (Error_Invalid_Arg(val));
if (same_fields(field->fields, VAL_STRUCT_FIELDLIST(val))) {
memcpy(
data,
SER_AT(
REBYTE,
VAL_STRUCT_DATA_BIN(val),
VAL_STRUCT_OFFSET(val)
),
field->size
);
} else
fail (Error_Invalid_Arg(val));
break;
default:
/* should never be here */
return FALSE;
}
return TRUE;
}
inline static REBOOL assign_scalar(
REBSTU *stu,
struct Struct_Field *field,
REBCNT n,
const REBVAL *val
) {
return assign_scalar_core(
STU_DATA_BIN(stu), STU_OFFSET(stu), field, n, val
);
}
//
// Set_Struct_Var: C
//
static REBOOL Set_Struct_Var(
REBSTU *stu,
const REBVAL *word,
const REBVAL *elem,
const REBVAL *val
) {
REBSER *fieldlist = STU_FIELDLIST(stu);
struct Struct_Field *field = SER_HEAD(struct Struct_Field, fieldlist);
REBCNT i;
for (i = 0; i < SER_LEN(fieldlist); i ++, field ++) {
if (VAL_WORD_CANON(word) == STR_CANON(field->name)) {
if (field->is_array) {
if (elem == NULL) { //set the whole array
REBCNT n = 0;
if (!IS_BLOCK(val))
return FALSE;
if (field->dimension != VAL_LEN_AT(val))
return FALSE;
for(n = 0; n < field->dimension; n ++) {
if (!assign_scalar(
stu, field, n, KNOWN(VAL_ARRAY_AT_HEAD(val, n))
)) {
return FALSE;
}
}
}
else { // set only one element
if (!IS_INTEGER(elem)
|| VAL_INT32(elem) <= 0
|| VAL_INT32(elem) > cast(REBINT, field->dimension)
) {
return FALSE;
}
return assign_scalar(stu, field, VAL_INT32(elem) - 1, val);
}
return TRUE;
} else {
return assign_scalar(stu, field, 0, val);
}
return TRUE;
}
}
return FALSE;
}
/* parse struct attribute */
static void parse_attr (REBVAL *blk, REBINT *raw_size, REBUPT *raw_addr)
{
REBVAL *attr = KNOWN(VAL_ARRAY_AT(blk));
*raw_size = -1;
*raw_addr = 0;
while (NOT_END(attr)) {
if (IS_SET_WORD(attr)) {
switch (VAL_WORD_SYM(attr)) {
case SYM_RAW_SIZE:
++ attr;
if (NOT_END(attr) && IS_INTEGER(attr)) {
if (*raw_size > 0) /* duplicate raw-size */
fail (Error_Invalid_Arg(attr));
*raw_size = VAL_INT64(attr);
if (*raw_size <= 0)
fail (Error_Invalid_Arg(attr));
}
else
fail (Error_Invalid_Arg(attr));
break;
case SYM_RAW_MEMORY:
++ attr;
if (NOT_END(attr) && IS_INTEGER(attr)) {
if (*raw_addr != 0) /* duplicate raw-memory */
fail (Error_Invalid_Arg(attr));
*raw_addr = cast(REBU64, VAL_INT64(attr));
if (*raw_addr == 0)
fail (Error_Invalid_Arg(attr));
}
else
fail (Error_Invalid_Arg(attr));
break;
case SYM_EXTERN:
++ attr;
if (*raw_addr != 0) // raw-memory is exclusive with extern
fail (Error_Invalid_Arg(attr));
if (IS_END(attr) || !IS_BLOCK(attr)
|| VAL_LEN_AT(attr) != 2) {
fail (Error_Invalid_Arg(attr));
}
else {
REBVAL *lib = KNOWN(VAL_ARRAY_AT_HEAD(attr, 0));
if (!IS_LIBRARY(lib))
fail (Error_Invalid_Arg(attr));
if (IS_LIB_CLOSED(VAL_LIBRARY(lib)))
fail (Error(RE_BAD_LIBRARY));
REBVAL *sym = KNOWN(VAL_ARRAY_AT_HEAD(attr, 1));
if (!ANY_BINSTR(sym))
fail (Error_Invalid_Arg(sym));
CFUNC *addr = OS_FIND_FUNCTION(
VAL_LIBRARY_FD(lib),
s_cast(VAL_RAW_DATA_AT(sym))
);
if (!addr)
fail (Error(RE_SYMBOL_NOT_FOUND, sym));
*raw_addr = cast(REBUPT, addr);
}
break;
/*
case SYM_ALIGNMENT:
++ attr;
if (IS_INTEGER(attr)) {
alignment = VAL_INT64(attr);
} else {
fail (Error_Invalid_Arg(attr));
}
break;
*/
default:
fail (Error_Invalid_Arg(attr));
}
}
else
fail (Error_Invalid_Arg(attr));
++ attr;
}
}
//
// set storage memory to external addr: raw_addr
//
// !!! The STRUCT! type is being converted to be "more like a user defined
// type", in that it will be driven less by specialized C structures and
// done more in usermode mechanics. One glitch in that is this routine
// which apparently was used specifically on structs to allow their data
// pointer to come from an external memory address. With STRUCT! relying
// on BINARY! for its storage, this introduces the concept of an
// "external binary". While a generalized external binary might be
// an interesting feature, it would come at the cost that every series
// access on BINARY! for the data would have to check if it was loaded
// or not.
//
// This suggests perhaps that BINARY! can't be used and some kind of handle
// would instead.
//
// This is something that should be discussed with Atronix to figure out
// exactly why this was added
//
static REBSER *make_ext_storage(
REBCNT len,
REBINT raw_size,
REBUPT raw_addr
) {
if (raw_size >= 0 && raw_size != cast(REBINT, len)) {
REBVAL i;
SET_INTEGER(&i, raw_size);
fail (Error(RE_INVALID_DATA, &i));
}
REBSER *ser = Make_Series(
len + 1, // include term. !!! not included otherwise (?)
1, // width
MKS_EXTERNAL
);
SER_SET_EXTERNAL_DATA(ser, cast(REBYTE*, raw_addr));
SET_SER_FLAG(ser, SERIES_FLAG_ACCESSIBLE); // accessible by default
SET_SERIES_LEN(ser, len);
MANAGE_SERIES(ser);
return ser;
}
//
// This takes a spec like `[int32 [2]]` and sets the output field's properties
// such as `type`, `size`, `is_array`, `dimension`, etc. It does this by
// recognizing a finite set of FFI type keywords defined in %words.r.
//
// This also allows for embedded structure types. If the type is not being
// included by reference, but rather with a sub-definition inline, then it
// will actually be creating a new `inner` STRUCT! value. Since this value
// is managed and not referred to elsewhere, there can't be evaluations.
//
static void Parse_Field_Type_May_Fail(
struct Struct_Field *field,
REBVAL *spec,
REBVAL *inner,
REBVAL **init
){
RELVAL *val = VAL_ARRAY_AT(spec);
if (IS_END(val))
fail (Error(RE_MISC)); // !!! better error
field->is_rebval = FALSE; // by default, not a REBVAL
if (IS_WORD(val)) {
switch (VAL_WORD_SYM(val)) {
case SYM_UINT8:
field->type = FFI_TYPE_UINT8;
field->size = 1;
break;
case SYM_INT8:
field->type = FFI_TYPE_SINT8;
field->size = 1;
break;
case SYM_UINT16:
field->type = FFI_TYPE_UINT16;
field->size = 2;
break;
case SYM_INT16:
field->type = FFI_TYPE_SINT16;
field->size = 2;
break;
case SYM_UINT32:
field->type = FFI_TYPE_UINT32;
field->size = 4;
break;
case SYM_INT32:
field->type = FFI_TYPE_SINT32;
field->size = 4;
break;
case SYM_UINT64:
field->type = FFI_TYPE_UINT64;
field->size = 8;
break;
case SYM_INT64:
field->type = FFI_TYPE_SINT64;
field->size = 8;
break;
case SYM_FLOAT:
field->type = FFI_TYPE_FLOAT;
field->size = 4;
break;
case SYM_DOUBLE:
field->type = FFI_TYPE_DOUBLE;
field->size = 8;
break;
case SYM_POINTER:
field->type = FFI_TYPE_POINTER;
field->size = sizeof(void*);
break;
case SYM_STRUCT_X:
++ val;
if (IS_BLOCK(val)) {
REBVAL specified;
COPY_VALUE(&specified, val, VAL_SPECIFIER(spec));
MAKE_Struct(inner, REB_STRUCT, &specified); // may fail()
field->size = SER_LEN(VAL_STRUCT_DATA_BIN(inner));
field->type = FFI_TYPE_STRUCT;
field->fields = VAL_STRUCT_FIELDLIST(inner);
field->spec = VAL_STRUCT_SPEC(inner);
*init = inner; // a shortcut for struct intialization
}
else
fail (Error_Unexpected_Type(REB_BLOCK, VAL_TYPE(val)));
break;
case SYM_REBVAL:
field->is_rebval = TRUE;
field->type = FFI_TYPE_POINTER;
field->size = sizeof(void*); // multiplied by 4 at the end
break;
default:
fail (Error_Invalid_Type(VAL_TYPE(val)));
}
}
else if (IS_STRUCT(val)) {
//
// [b: [struct-a] val-a]
//
field->size = SER_LEN(VAL_STRUCT_DATA_BIN(val));
field->type = FFI_TYPE_STRUCT;
field->fields = VAL_STRUCT_FIELDLIST(val);
field->spec = VAL_STRUCT_SPEC(val);
COPY_VALUE(*init, val, VAL_SPECIFIER(spec));
}
else
fail (Error_Invalid_Type(VAL_TYPE(val)));
++ val;
if (IS_END(val)) {
field->dimension = 1; // scalar
field->is_array = 0; // FALSE, but bitfield must be integer
}
else if (IS_BLOCK(val)) {
//
// make struct! [a: [int32 [2]] [0 0]]
//
REBVAL ret;
if (Do_At_Throws(
&ret,
VAL_ARRAY(val),
VAL_INDEX(val),
VAL_SPECIFIER(spec)
)) {
// !!! Does not check for thrown cases...what should this
// do in case of THROW, BREAK, QUIT?
fail (Error_No_Catch_For_Throw(&ret));
}
if (!IS_INTEGER(&ret))
fail (Error_Unexpected_Type(REB_INTEGER, VAL_TYPE(val)));
field->dimension = cast(REBCNT, VAL_INT64(&ret));
field->is_array = 1; // TRUE, but bitfield must be integer
++ val;
}
else
fail (Error_Invalid_Type(VAL_TYPE(val)));
if (field->is_rebval)
field->dimension = field->dimension * 4;
}
//
// Total_Struct_Dimensionality: C
//
// This recursively counts the total number of data elements inside of a
// struct. This includes for instance every array element inside a
// nested struct's field, along with its fields.
//
// !!! Is this really how char[1000] would be handled in the FFI? By
// creating 1000 ffi_types? :-/
//
static REBCNT Total_Struct_Dimensionality(REBSER *fields)
{
REBCNT n_fields = 0;
REBCNT i;
for (i = 0; i < SER_LEN(fields); ++i) {
struct Struct_Field *field = SER_AT(struct Struct_Field, fields, i);
if (field->type != FFI_TYPE_STRUCT)
n_fields += field->dimension;
else
n_fields += Total_Struct_Dimensionality(field->fields);
}
return n_fields;
}
//
// Init_Struct_Fields: C
//
// a: make struct! [uint 8 i: 1]
// b: make a [i: 10]
//
void Init_Struct_Fields(REBVAL *ret, REBVAL *spec)
{
REBVAL *blk = NULL;
for (blk = KNOWN(VAL_ARRAY_AT(spec)); NOT_END(blk); blk += 2) {
unsigned int i = 0;
REBVAL *word = blk;
REBVAL *fld_val = blk + 1;
if (IS_BLOCK(word)) { // options: raw-memory, etc
REBINT raw_size = -1;
REBUPT raw_addr = 0;
// make sure no other field initialization
if (VAL_LEN_HEAD(spec) != 1)
fail (Error_Invalid_Arg(spec));
parse_attr(word, &raw_size, &raw_addr);
ret->payload.structure.data
= make_ext_storage(VAL_STRUCT_SIZE(ret), raw_size, raw_addr);
break;
}
else if (! IS_SET_WORD(word))
fail (Error_Invalid_Arg(word));
if (IS_END(fld_val))
fail (Error(RE_NEED_VALUE, fld_val));
REBSER *fieldlist = VAL_STRUCT_FIELDLIST(ret);
for (i = 0; i < SER_LEN(fieldlist); i ++) {
struct Struct_Field *fld
= SER_AT(struct Struct_Field, fieldlist, i);
if (STR_CANON(fld->name) == VAL_WORD_CANON(word)) {
if (fld->dimension > 1) {
REBCNT n = 0;
if (IS_BLOCK(fld_val)) {
if (VAL_LEN_AT(fld_val) != fld->dimension)
fail (Error_Invalid_Arg(fld_val));
for(n = 0; n < fld->dimension; n ++) {
if (!assign_scalar(
VAL_STRUCT(ret),
fld,
n,
KNOWN(VAL_ARRAY_AT_HEAD(fld_val, n))
)) {
fail (Error_Invalid_Arg(fld_val));
}
}
}
else if (IS_INTEGER(fld_val)) {
void *ptr = cast(void *,
cast(REBUPT, VAL_INT64(fld_val))
);
// assuming valid pointer to enough space
memcpy(
SER_AT(
REBYTE,
VAL_STRUCT_DATA_BIN(ret),
fld->offset
),
ptr,
fld->size * fld->dimension
);
}
else
fail (Error_Invalid_Arg(fld_val));
}
else {