-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathluacstruct.c
More file actions
2352 lines (2096 loc) · 56.6 KB
/
luacstruct.c
File metadata and controls
2352 lines (2096 loc) · 56.6 KB
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
/*
* Copyright (c) 2021 YASUOKA Masahiko <yasuoka@yasuoka.net>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/types.h>
#include <sys/cdefs.h>
#include <sys/queue.h>
#include <sys/tree.h>
#include <errno.h>
#include <endian.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <wchar.h>
#include <lua.h>
#include <lauxlib.h>
#include "luacstruct.h"
/*
* When this is used by multiple modules, meta names in Lua will be conflicted,
* which might cause problems. Define LUACS_VARIANT uniquely per a module to
* avoid the problems.
*/
#ifndef LUACS_VARIANT
#define LUACS_VARIANT "3"
#endif
#define METANAMELEN 80
#define METANAME_LUACSTRUCT "luacstruct" LUACS_VARIANT
#define METANAME_LUACSENUM "luacenum" LUACS_VARIANT
#define METANAME_LUACTYPE "luactype" LUACS_VARIANT "."
#define METANAME_LUACARRAY "luacarray" LUACS_VARIANT
#define METANAME_LUACARRAYTYPE "luacarraytype" LUACS_VARIANT
#define METANAME_LUACSTRUCTOBJ "luacstructobj" LUACS_VARIANT
#define METANAME_LUACSENUMVAL "luacenumval" LUACS_VARIANT
#define METANAME_LUACSUSERTABLE "luacusertable" LUACS_VARIANT
#define LUACS_REGISTRY_NAME "luacstruct_registry"
#if LUA_VERSION_NUM == 501
#define lua_rawlen(_x, _i) lua_objlen((_x), (_i))
#define lua_absindex(_L, _i) (((_i) > 0 || (_i) <= LUA_REGISTRYINDEX) \
? (_i) : lua_gettop(_L) + (_i) + 1)
#endif
#ifdef LUACS_DEBUG
#define LUACS_DBG(...) \
do { \
fprintf(stdout, __VA_ARGS__); \
fputs("\n", stdout); \
} while (0/*CONSTCOND*/)
#define LUACS_ASSERT(_L, _cond) \
do { \
if (!(_cond)) { \
lua_pushfstring((_L), "ASSERT(%s) failed " \
"in %s() at %s:%d", #_cond, __func__, \
__FILE__, __LINE__); \
lua_error((_L)); \
} \
} while (0/*CONSTCOND*/)
#else
#define LUACS_DBG(...) ((void)0)
#define LUACS_ASSERT(_L, _cond) ((void)0)
#endif
#ifndef MINIMUM
#define MINIMUM(_a, _b) ((_a) < (_b)? (_a) : (_b))
#endif
#ifndef MAXIMUM
#define MAXIMUM(_a, _b) ((_a) > (_b)? (_a) : (_b))
#endif
#ifndef nitems
#define nitems(_x) (sizeof(_x) / sizeof((_x)[0]))
#endif
#ifndef __unused
#define __unused __attribute__((__unused__))
#endif
#ifdef _WIN32
#if BYTE_ORDER == LITTLE_ENDIAN
#define htobe16(x) _byteswap_ushort(x)
#define htole16(x) (x)
#define htobe32(x) _byteswap_ulong(x)
#define htole32(x) (x)
#define htobe64(x) _byteswap_uint64(x)
#define htole64(x) (x)
#define be16toh(x) _byteswap_ushort(x)
#define le16toh(x) (x)
#define be32toh(x) _byteswap_ulong(x)
#define le32toh(x) (x)
#define be64toh(x) _byteswap_uint64(x)
#define le64toh(x) (x)
#else
#define htobe16(x) (x)
#define htole16(x) _byteswap_ushort(x)
#define htobe32(x) (x)
#define htole32(x) _byteswap_ulong(x)
#define htobe64(x) (x)
#define htole64(x) _byteswap_uint64(x)
#define be16toh(x) (x)
#define le16toh(x) _byteswap_ushort(x)
#define be32toh(x) (x)
#define le32toh(x) _byteswap_ulong(x)
#define be64toh(x) (x)
#define le64toh(x) _byteswap_uint64(x)
#endif
#endif
#ifdef __APPLE__
#include <libkern/OSByteOrder.h>
#define htobe16(x) OSSwapHostToBigInt16(x)
#define htole16(x) OSSwapHostToLittleInt16(x)
#define be16toh(x) OSSwapBigToHostInt16(x)
#define le16toh(x) OSSwapLittleToHostInt16(x)
#define htobe32(x) OSSwapHostToBigInt32(x)
#define htole32(x) OSSwapHostToLittleInt32(x)
#define be32toh(x) OSSwapBigToHostInt32(x)
#define le32toh(x) OSSwapLittleToHostInt32(x)
#define htobe64(x) OSSwapHostToBigInt64(x)
#define htole64(x) OSSwapHostToLittleInt64(x)
#define be64toh(x) OSSwapBigToHostInt64(x)
#define le64toh(x) OSSwapLittleToHostInt64(x)
#endif
struct luacstruct {
const char *typename;
char metaname[METANAMELEN];
SPLAY_HEAD(luacstruct_fields, luacstruct_field)
fields;
TAILQ_HEAD(,luacstruct_field) sorted;
};
struct luacarraytype {
const char *typename;
char metaname[METANAMELEN];
enum luacstruct_type type;
size_t size;
int nmemb;
int typref;
unsigned flags;
};
struct luacregion {
enum luacstruct_type type;
int off;
size_t size;
int typref;
unsigned flags;
};
struct luacstruct_field {
enum luacstruct_type type;
const char *fieldname;
struct luacregion region;
int constval;
int nmemb;
unsigned flags;
int ref;
SPLAY_ENTRY(luacstruct_field) tree;
TAILQ_ENTRY(luacstruct_field) queue;
};
struct luacobject {
enum luacstruct_type type;
struct luacstruct *cs;
caddr_t ptr;
size_t size;
int nmemb;
int typref;
unsigned flags;
};
struct luacenum {
const char *enumname;
char metaname[METANAMELEN];
size_t valwidth;
SPLAY_HEAD(luacenum_labels, luacenum_value)
labels;
SPLAY_HEAD(luacenum_values, luacenum_value)
values;
int func_get;
int func_memberof;
};
struct luacenum_value {
struct luacenum *ce;
intmax_t value;
int ref;
const char *label;
SPLAY_ENTRY(luacenum_value) treel;
SPLAY_ENTRY(luacenum_value) treev;
};
static struct luacstruct
*luacs_checkstruct(lua_State *, int);
static int luacs_usertable(lua_State *, int);
static int luacs_deleteusertable(lua_State *, int);
static int luacs_struct__gc(lua_State *);
static struct luacstruct_field
*luacs_declare(lua_State *, enum luacstruct_type, const char *,
const char *, size_t, int, int, unsigned);
static int luacstruct_field_cmp(struct luacstruct_field *,
struct luacstruct_field *);
static struct luacstruct_field
*luacsfield_copy(lua_State *, struct luacstruct_field *);
static void luacstruct_field_free(lua_State *, struct luacstruct *,
struct luacstruct_field *);
static int luacs_pushctype(lua_State *, enum luacstruct_type,
const char *);
static int luacs_newarray0(lua_State *, enum luacstruct_type, int, size_t,
int, unsigned, void *);
static int luacs_array__len(lua_State *);
static int luacs_array__index(lua_State *);
static int luacs_array__newindex(lua_State *);
static int luacs_array_copy(lua_State *);
static int luacs_array__next(lua_State *);
static int luacs_array__pairs(lua_State *);
static int luacs_array__gc(lua_State *);
static int luacs_newobject0(lua_State *, void *);
static int luacs_object__luacstructdump(struct lua_State *);
struct luacobj_compat;
static void luacs_object_compat(lua_State *, int, struct luacobj_compat *);
static int luacs_object__eq(lua_State *);
static int luacs_object__tostring(lua_State *);
static int luacs_object__index(lua_State *);
static int luacs_object__get(lua_State *, struct luacobject *,
struct luacstruct_field *);
static int luacs_object__newindex(lua_State *);
static int luacs_object_copy(lua_State *);
static int luacs_object__next(lua_State *);
static int luacs_object__pairs(lua_State *);
static int luacs_object__gc(lua_State *);
static int luacs_pushregion(lua_State *, struct luacobject *,
struct luacregion *);
static void luacs_pullregion(lua_State *, struct luacobject *,
struct luacregion *, int);
struct luacenum *luacs_checkenum(lua_State*, int);
struct luacenum_value
*luacs_enum_get0(struct luacenum *, intmax_t);
static int luacs_enum_get(lua_State *);
static int luacs_enum_memberof(lua_State *);
static int luacs_enum__index(lua_State *);
static int luacs_enum__pairs(lua_State *);
static int luacs_enum__next(lua_State *);
static int luacs_enum__gc(lua_State *);
static int luacs_enumvalue_tointeger(lua_State *);
static int luacs_enumvalue_label(lua_State *);
static int luacs_enumvalue__tostring(lua_State *);
static int luacs_enumvalue__gc(lua_State *);
static int luacs_enumvalue__eq(lua_State *);
static int luacs_enumvalue__lt(lua_State *);
static int luacenum_label_cmp(struct luacenum_value *,
struct luacenum_value *);
static int luacenum_value_cmp(struct luacenum_value *,
struct luacenum_value *);
static int luacs_ref(lua_State *);
static int luacs_getref(lua_State *, int);
static int luacs_unref(lua_State *, int);
static int luacs_pushwstring(lua_State *, const wchar_t *);
SPLAY_PROTOTYPE(luacstruct_fields, luacstruct_field, tree,
luacstruct_field_cmp);
SPLAY_PROTOTYPE(luacenum_labels, luacenum_value, treel, luacenum_label_cmp);
SPLAY_PROTOTYPE(luacenum_values, luacenum_value, treev, luacenum_value_cmp);
/* Declare a new struct */
int
luacs_newstruct0(lua_State *L, const char *tname, const char *supertname)
{
int ret;
struct luacstruct *cs, *supercs = NULL;
char metaname[METANAMELEN], metaname0[METANAMELEN],
buf[128];
struct luacstruct_field *fieldf, *fieldt;
/* Use if already exists */
snprintf(metaname, sizeof(metaname), "%s%s", METANAME_LUACTYPE, tname);
lua_getfield(L, LUA_REGISTRYINDEX, metaname);
if (!lua_isnil(L, -1)) {
cs = luacs_checkstruct(L, -1);
return (1);
}
lua_pop(L, 1);
/* Prepare the super struct */
if (supertname != NULL) {
snprintf(metaname0, sizeof(metaname0), "%s%s",
METANAME_LUACTYPE, supertname);
lua_getfield(L, LUA_REGISTRYINDEX, metaname0);
if (lua_isnil(L, -1)) {
snprintf(buf, sizeof(buf), "`%s' is not regisiterd",
supertname);
lua_pushstring(L, buf);
lua_error(L);
return (0); /* not reached */
}
supercs = luacs_checkstruct(L, -1);
}
/* Create and initialize a new cstruct */
cs = lua_newuserdata(L, sizeof(struct luacstruct));
lua_pushvalue(L, -1);
lua_setfield(L, LUA_REGISTRYINDEX, metaname);
memcpy(cs->metaname, metaname, MINIMUM(sizeof(metaname),
sizeof(cs->metaname)));
cs->typename = strchr(cs->metaname, '.') + 1;
SPLAY_INIT(&cs->fields);
TAILQ_INIT(&cs->sorted);
/* Inherit from the super struct if specified */
if (supercs != NULL) {
TAILQ_FOREACH(fieldf, &supercs->sorted, queue) {
if ((fieldt = luacsfield_copy(L, fieldf)) == NULL) {
strerror_r(errno, buf, sizeof(buf));
lua_pushstring(L, buf);
lua_error(L);
return (0); /* not reached */
}
TAILQ_INSERT_TAIL(&cs->sorted, fieldt, queue);
SPLAY_INSERT(luacstruct_fields, &cs->fields, fieldt);
}
lua_remove(L, -2);
}
if ((ret = luaL_newmetatable(L, METANAME_LUACSTRUCT)) != 0) {
lua_pushcfunction(L, luacs_struct__gc);
lua_setfield(L, -2, "__gc");
}
lua_pushvalue(L, -1);
lua_setfield(L, -2, "__index");
lua_setmetatable(L, -2);
return (1);
}
int
luacs_delstruct(lua_State *L, const char *tname)
{
char metaname[METANAMELEN];
snprintf(metaname, sizeof(metaname), "%s%s", METANAME_LUACTYPE, tname);
lua_pushnil(L);
lua_setfield(L, LUA_REGISTRYINDEX, metaname);
return (0);
}
struct luacstruct *
luacs_checkstruct(lua_State *L, int csidx)
{
return (luaL_checkudata(L, csidx, METANAME_LUACSTRUCT));
}
int
luacs_usertable(lua_State *L, int idx)
{
int absidx;
absidx = lua_absindex(L, idx);
lua_getfield(L, LUA_REGISTRYINDEX, METANAME_LUACSUSERTABLE);
if (lua_isnil(L, -1)) {
/* Create a weak table */
lua_pop(L, 1);
lua_newtable(L);
lua_newtable(L);
lua_pushstring(L, "k");
lua_setfield(L, -2, "__mode");
lua_setmetatable(L, -2);
lua_setfield(L, LUA_REGISTRYINDEX, METANAME_LUACSUSERTABLE);
lua_getfield(L, LUA_REGISTRYINDEX, METANAME_LUACSUSERTABLE);
}
lua_pushvalue(L, absidx);
lua_gettable(L, -2);
if (lua_isnil(L, -1)) {
/* Create a new table which is weakly refered by the userdata */
lua_pop(L, 1);
lua_newtable(L);
lua_pushvalue(L, absidx);
lua_pushvalue(L, -2);
lua_settable(L, -4);
}
lua_remove(L, -2);
return (1);
}
int
luacs_deleteusertable(lua_State *L, int idx)
{
int absidx;
absidx = lua_absindex(L, idx);
lua_getfield(L, LUA_REGISTRYINDEX, METANAME_LUACSUSERTABLE);
if (!lua_isnil(L, -1)) {
lua_pushvalue(L, absidx);
lua_pushnil(L);
lua_settable(L, -3);
}
lua_pop(L, 1);
return (1);
}
int
luacs_struct__gc(lua_State *L)
{
struct luacstruct *cs;
struct luacstruct_field *field;
lua_settop(L, 1);
cs = luacs_checkstruct(L, 1);
if (cs) {
while ((field = SPLAY_MIN(luacstruct_fields, &cs->fields)) !=
NULL)
luacstruct_field_free(L, cs, field);
}
return (0);
}
int
luacs_declare_field(lua_State *L, enum luacstruct_type _type,
const char *tname, const char *name, size_t siz, int off, int nmemb,
unsigned flags)
{
luacs_declare(L, _type, tname, name, siz, off, nmemb, flags);
return (0);
}
struct luacstruct_field *
luacs_declare(lua_State *L, enum luacstruct_type _type,
const char *tname, const char *name, size_t siz, int off, int nmemb,
unsigned flags)
{
struct luacstruct_field *field, *field0;
struct luacstruct *cs;
char buf[BUFSIZ];
cs = luacs_checkstruct(L, -1);
if ((field = calloc(1, sizeof(struct luacstruct_field))) == NULL) {
strerror_r(errno, buf, sizeof(buf));
lua_pushstring(L, buf);
lua_error(L);
}
if ((field->fieldname = strdup(name)) == NULL) {
free(field);
strerror_r(errno, buf, sizeof(buf));
lua_pushstring(L, buf);
lua_error(L);
}
while ((field0 = SPLAY_FIND(luacstruct_fields, &cs->fields, field))
!= NULL)
luacstruct_field_free(L, cs, field0);
field->region.type = _type;
field->region.off = off;
field->region.size = siz;
field->region.flags = flags;
field->nmemb = nmemb;
field->flags = flags;
switch (_type) {
case LUACS_TOBJREF:
case LUACS_TOBJENT:
case LUACS_TENUM:
case LUACS_TARRAY:
luacs_pushctype(L, _type, tname);
field->region.typref = luacs_ref(L);
break;
case LUACS_TINT64:
case LUACS_TUINT64:
if (sizeof(lua_Integer) < 8) {
lua_pushliteral(L,
"Lua runtime doesn't support 64bit integer");
lua_error(L);
}
default:
break;
}
field->type = (field->nmemb > 0)? LUACS_TARRAY : _type;
SPLAY_INSERT(luacstruct_fields, &cs->fields, field);
TAILQ_FOREACH(field0, &cs->sorted, queue) {
if (field->region.off < field0->region.off)
break;
}
if (field0 == NULL)
TAILQ_INSERT_TAIL(&cs->sorted, field, queue);
else
TAILQ_INSERT_BEFORE(field0, field, queue);
return (field);
}
int
luacs_declare_method(lua_State *L, const char *name, int (*func)(lua_State *))
{
struct luacstruct_field *field;
field = luacs_declare(L, LUACS_TMETHOD, NULL, name, 0, 0, 0,
LUACS_FREADONLY);
lua_pushcfunction(L, func);
field->ref = luacs_ref(L);
return (0);
}
int
luacs_declare_const(lua_State *L, const char *name, int constval)
{
struct luacstruct_field *field;
field = luacs_declare(L, LUACS_TCONST, NULL, name, 0, 0, 0,
LUACS_FREADONLY);
field->constval = constval;
return (0);
}
int
luacstruct_field_cmp(struct luacstruct_field *a, struct luacstruct_field *b)
{
return (strcmp(a->fieldname, b->fieldname));
}
struct luacstruct_field *
luacsfield_copy(lua_State *L, struct luacstruct_field *from)
{
struct luacstruct_field *to;
to = calloc(1, sizeof(struct luacstruct_field));
if (to == NULL)
return (NULL);
if ((to->fieldname = strdup(from->fieldname)) == NULL) {
free(to);
return (NULL);
}
to->type = from->type;
to->region = from->region;
to->constval = from->constval;
to->nmemb = from->nmemb;
to->flags = from->flags;
/* Update refs */
if (from->region.typref != 0) {
luacs_getref(L, from->region.typref);
to->region.typref = luacs_ref(L);
}
if (from->ref != 0) {
luacs_getref(L, from->ref);
to->ref = luacs_ref(L);
}
return (to);
}
void
luacstruct_field_free(lua_State *L, struct luacstruct *cs,
struct luacstruct_field *field)
{
if (field) {
if (field->region.typref > 0)
luacs_unref(L, field->region.typref);
if (field->ref != 0)
luacs_unref(L, field->ref);
TAILQ_REMOVE(&cs->sorted, field, queue);
SPLAY_REMOVE(luacstruct_fields, &cs->fields, field);
free((char *)field->fieldname);
}
free(field);
}
int
luacs_pushctype(lua_State *L, enum luacstruct_type _type, const char *tname)
{
char metaname[METANAMELEN];
snprintf(metaname, sizeof(metaname), "%s%s", METANAME_LUACTYPE, tname);
lua_getfield(L, LUA_REGISTRYINDEX, metaname);
if (lua_isnil(L, -1)) {
if (_type == LUACS_TARRAY)
lua_pushfstring(L, "array `%s' is not registered",
tname);
else
lua_pushfstring(L, "`%s %s' is not registered",
_type == LUACS_TENUM? "enum" : "struct", tname);
lua_error(L);
}
if (_type == LUACS_TARRAY)
luaL_checkudata(L, -1, METANAME_LUACARRAYTYPE);
else if (_type == LUACS_TENUM)
luacs_checkenum(L, -1);
else
luacs_checkstruct(L, -1);
return (1);
}
/* array */
int
luacs_arraytype__gc(lua_State *L)
{
luaL_checkudata(L, -1, METANAME_LUACARRAYTYPE);
return (0);
}
int
luacs_newarraytype(lua_State *L, const char *tname, enum luacstruct_type _type,
const char *membtname, size_t size, int nmemb, unsigned flags)
{
int ret;
struct luacarraytype *cat;
char metaname[METANAMELEN];
snprintf(metaname, sizeof(metaname), "%s%s", METANAME_LUACTYPE, tname);
lua_getfield(L, LUA_REGISTRYINDEX, metaname);
if (!lua_isnil(L, -1)) {
cat = luaL_checkudata(L, -1, METANAME_LUACARRAYTYPE);
return (1);
}
lua_pop(L, 1);
cat = lua_newuserdata(L, sizeof(struct luacarraytype));
cat->type = _type;
cat->size = size;
cat->nmemb = nmemb;
cat->flags = flags;
switch (_type) {
case LUACS_TOBJREF:
case LUACS_TOBJENT:
case LUACS_TARRAY:
if (membtname == NULL) {
lua_pushfstring(L, "`membtname' argument must be "
"specified when creating an array of %s",
_type == LUACS_TENUM? "LUACS_TENUM" :
_type == LUACS_TOBJREF ? "LUACS_TOBJREF" :
"LUACS_TOBJENT");
lua_error(L);
}
luacs_pushctype(L, _type, membtname);
cat->typref = luacs_ref(L);
break;
default:
break;
}
lua_pushvalue(L, -1);
lua_setfield(L, LUA_REGISTRYINDEX, metaname);
memcpy(cat->metaname, metaname, MINIMUM(sizeof(metaname),
sizeof(cat->metaname)));
cat->typename = strchr(cat->metaname, '.') + 1;
if ((ret = luaL_newmetatable(L, METANAME_LUACARRAYTYPE)) != 0) {
lua_pushcfunction(L, luacs_arraytype__gc);
lua_setfield(L, -2, "__gc");
}
lua_pushvalue(L, -1);
lua_setfield(L, -2, "__index");
lua_setmetatable(L, -2);
return (1);
}
int
luacs_newarray(lua_State *L, enum luacstruct_type _type, const char *membtname,
size_t size, int nmemb, unsigned flags, void *ptr)
{
int typref = 0;
switch (_type) {
case LUACS_TENUM:
case LUACS_TOBJREF:
case LUACS_TOBJENT:
case LUACS_TARRAY:
if (membtname == NULL) {
lua_pushfstring(L, "`membtname' argument must be "
"specified when creating an array of %s",
_type == LUACS_TENUM? "LUACS_TENUM" :
_type == LUACS_TOBJREF ? "LUACS_TOBJREF" :
"LUACS_TOBJENT");
lua_error(L);
}
luacs_pushctype(L, _type, membtname);
typref = luacs_ref(L);
break;
default:
break;
}
return (luacs_newarray0(L, _type, typref, size, nmemb, flags, ptr));
}
int
luacs_newarray0(lua_State *L, enum luacstruct_type _type, int typidx,
size_t size, int nmemb, unsigned flags, void *ptr)
{
struct luacobject *obj;
int ret, absidx;
absidx = lua_absindex(L, typidx);
if (ptr != NULL) {
obj = lua_newuserdata(L, sizeof(struct luacobject));
obj->ptr = ptr;
} else {
obj = lua_newuserdata(L, sizeof(struct luacobject) +
size * nmemb);
obj->ptr = (caddr_t)(obj + 1);
}
obj->type =_type;
obj->size = size;
obj->nmemb = nmemb;
obj->flags = flags;
if (typidx != 0) {
lua_pushvalue(L, absidx);
obj->typref = luacs_ref(L);
}
if ((ret = luaL_newmetatable(L, METANAME_LUACARRAY)) != 0) {
lua_pushcfunction(L, luacs_array__len);
lua_setfield(L, -2, "__len");
lua_pushcfunction(L, luacs_array__index);
lua_setfield(L, -2, "__index");
lua_pushcfunction(L, luacs_array__newindex);
lua_setfield(L, -2, "__newindex");
lua_pushcfunction(L, luacs_array__next);
lua_pushcclosure(L, luacs_array__pairs, 1);
lua_setfield(L, -2, "__pairs");
lua_pushcfunction(L, luacs_array__gc);
lua_setfield(L, -2, "__gc");
}
lua_setmetatable(L, -2);
return (1);
}
int
luacs_array__len(lua_State *L)
{
struct luacobject *obj;
lua_settop(L, 1);
obj = luaL_checkudata(L, 1, METANAME_LUACARRAY);
lua_pushinteger(L, obj->nmemb);
return (1);
}
int
luacs_array__index(lua_State *L)
{
struct luacobject *obj;
struct luacarraytype *cat;
int idx;
struct luacregion region;
void *ptr;
lua_settop(L, 2);
obj = luaL_checkudata(L, 1, METANAME_LUACARRAY);
idx = luaL_checkinteger(L, 2);
if (idx < 1 || obj->nmemb < idx) {
lua_pushnil(L);
return (1);
}
memset(®ion, 0, sizeof(region));
region.type = obj->type;
region.off = (idx - 1) * obj->size;
region.size = obj->size;
region.typref = obj->typref;
switch (obj->type) {
default:
return (luacs_pushregion(L, obj, ®ion));
break;
case LUACS_TOBJREF:
case LUACS_TOBJENT:
if (obj->type == LUACS_TOBJENT)
ptr = obj->ptr + region.off;
else
ptr = *(void **)(obj->ptr + region.off);
if (ptr == NULL)
lua_pushnil(L);
else {
luacs_usertable(L, 1);
lua_rawgeti(L, -1, idx);
if (lua_isnil(L, -1)) {
lua_pop(L, 1);
luacs_getref(L, obj->typref);
luacs_newobject0(L, ptr);
lua_pushvalue(L, -1);
lua_rawseti(L, -4, idx);
lua_remove(L, -2);
}
lua_remove(L, -2);
}
break;
case LUACS_TEXTREF:
luacs_usertable(L, 1);
lua_rawgeti(L, -1, idx);
lua_remove(L, -2);
break;
case LUACS_TARRAY:
ptr = obj->ptr + region.off;
if (ptr == NULL)
lua_pushnil(L);
else {
luacs_usertable(L, 1);
lua_rawgeti(L, -1, idx);
if (lua_isnil(L, -1)) {
lua_pop(L, 1);
luacs_getref(L, obj->typref);
cat = luaL_checkudata(L, -1,
METANAME_LUACARRAYTYPE);
lua_pop(L, 1);
if (cat->typref != 0)
luacs_getref(L, cat->typref);
luacs_newarray0(L, cat->type, cat->typref,
(cat->typref != 0)? -1 : 0, cat->nmemb,
cat->flags, obj->ptr + region.off);
if (cat->typref != 0)
lua_remove(L, -2);
lua_pushvalue(L, -1);
lua_rawseti(L, -3, idx);
}
lua_remove(L, -2);
}
break;
}
return (1);
}
int
luacs_array__newindex(lua_State *L)
{
struct luacobject *obj, *ano = NULL;
int idx;
struct luacregion region;
struct luacstruct *cs0;
lua_settop(L, 3);
obj = luaL_checkudata(L, 1, METANAME_LUACARRAY);
idx = luaL_checkinteger(L, 2);
if (idx < 1 || obj->nmemb < idx) { /* out of the range */
lua_pushfstring(L, "array index %d out of the range 1:%d",
idx, obj->nmemb);
lua_error(L);
}
region.type = obj->type;
region.off = (idx - 1) * obj->size;
region.size = obj->size;
region.typref = obj->typref;
region.flags = obj->flags;
if ((obj->flags & LUACS_FREADONLY) != 0) {
readonly:
lua_pushliteral(L, "array is readonly");
lua_error(L);
}
switch (region.type) {
default:
luacs_pullregion(L, obj, ®ion, 3);
break;
case LUACS_TSTRPTR:
case LUACS_TWSTRPTR:
goto readonly;
case LUACS_TOBJREF:
case LUACS_TOBJENT:
/* get c struct of the field */
luacs_getref(L, region.typref);
cs0 = luacs_checkstruct(L, -1);
lua_pop(L, 1);
/* given instance of struct */
if (region.type == LUACS_TOBJENT || !lua_isnil(L, 3))
ano = luaL_checkudata(L, 3, METANAME_LUACSTRUCTOBJ);
if (ano != NULL && cs0 != ano->cs) {
lua_pushfstring(L,
"must be an instance of `struct %s'",
cs0->typename);
lua_error(L);
}
if (region.type == LUACS_TOBJENT) {
lua_pushcfunction(L, luacs_object_copy);
/* ca't assume the object is cached */
lua_pushcfunction(L, luacs_array__index);
lua_pushvalue(L, 1);
lua_pushinteger(L, 2);
lua_call(L, 2, 1);
lua_pushvalue(L, 3);
lua_call(L, 2, 0);
} else {
*(void **)(obj->ptr + region.off) = ano? ano->ptr :
NULL;
/* use the same object */
luacs_usertable(L, 1);
lua_pushvalue(L, 3);
lua_rawseti(L, -2, idx);
lua_pop(L, 1);
}
break;
case LUACS_TEXTREF:
luacs_usertable(L, 1);
lua_pushvalue(L, 3);
lua_rawseti(L, -2, idx);
lua_pop(L, 1);
break;
case LUACS_TARRAY:
lua_pushcfunction(L, luacs_array_copy);
lua_pushcfunction(L, luacs_array__index);
lua_pushvalue(L, 1);
lua_pushinteger(L, 2);
lua_call(L, 2, 1);
lua_pushvalue(L, 3);
lua_call(L, 2, 0);
break;
}
return (0);
}
int
luacs_array_copy(lua_State *L)
{
struct luacobject *l, *r;
struct luacregion region;
int idx;
lua_settop(L, 2);
l = luaL_checkudata(L, 1, METANAME_LUACARRAY);
r = luaL_checkudata(L, 2, METANAME_LUACARRAY);
if (l->type != r->type) {
lua_pushliteral(L,
"can't copy between arrays of a different type");
lua_error(L);
}
if (l->nmemb != r->nmemb) {
lua_pushliteral(L,
"can't copy between arrays which size are different");
lua_error(L);
}
switch (l->type) {
default:
for (idx = 1; idx <= l->nmemb; idx++) {
region.type = l->type;
region.off = (idx - 1) * l->size;
region.size = l->size;
region.typref = l->typref;
lua_pushcfunction(L, luacs_array__index);
lua_pushvalue(L, 2);
lua_pushinteger(L, idx);
lua_call(L, 2, 1);
luacs_pullregion(L, l, ®ion, -1);
lua_pop(L, 1);
}
break;
case LUACS_TOBJREF:
luacs_usertable(L, 1);
for (idx = 1; idx <= l->nmemb; idx++) {
/* use the same pointer */
*(void **)(l->ptr + (idx - 1) * l->size) =
*(void **)(r->ptr + (idx - 1) * r->size);
lua_pushcfunction(L, luacs_array__index);
lua_pushvalue(L, 2);
lua_pushinteger(L, idx);
lua_call(L, 2, 1);
lua_rawseti(L, -2, idx);
}
lua_pop(L, 1);
break;