-
Notifications
You must be signed in to change notification settings - Fork 8
/
meta.c
1279 lines (1025 loc) · 33.1 KB
/
meta.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
#include "private.h"
#include <ctype.h> /* tolower() */
typedef struct ecs_lua_col_t
{
ecs_entity_t type;
size_t stride;
bool readback, update;
void *ptr;
const EcsMetaTypeSerialized *ser;
ecs_meta_cursor_t *cursor;
}ecs_lua_col_t;
typedef struct ecs_lua_each_t
{
ecs_iter_t *it;
int32_t i;
bool from_query, read_prev;
ecs_lua_col_t cols[];
}ecs_lua_each_t;
static
void serialize_type_op(
const ecs_world_t *world,
ecs_meta_type_op_t *op,
const void *base,
lua_State *L);
static
void serialize_type(
const ecs_world_t *world,
const ecs_vec_t *v_ops,
const void *base,
lua_State *L);
static
void serialize_elements(
const ecs_world_t *world,
ecs_meta_type_op_t *ops,
int32_t op_count,
const void *base,
int32_t elem_count,
int32_t elem_size,
lua_State *L);
static
void serialize_type_ops(
const ecs_world_t *world,
ecs_meta_type_op_t *ops,
int32_t op_count,
const void *base,
int32_t in_array,
lua_State *L)
{
int i, depth = 0;
for(i=0; i < op_count; i++)
{
ecs_meta_type_op_t *op = &ops[i];
if(in_array <= 0)
{
int32_t elem_count = op->count;
if(elem_count > 1)
{/* serialize inline array */
serialize_elements(world, op, op->op_count, base, op->count, op->size, L);
if(op->name) lua_setfield(L, -2, op->name);
i += op->op_count - 1;
continue;
}
}
switch(op->kind)
{
case EcsOpPush:
{
depth++;
in_array--;
if(depth > 1)
{
ecs_assert(op->name != NULL, ECS_INVALID_PARAMETER, NULL);
lua_pushstring(L, op->name);
}
int32_t member_count = ecs_map_count(&op->members->impl);
lua_createtable(L, 0, member_count);
break;
}
case EcsOpPop:
{
if(depth > 1) lua_settable(L, -3);
depth--;
in_array++;
break;
}
default:
{
serialize_type_op(world, op, base, L);
if(op->name && (in_array <= 0))
{
lua_setfield(L, -2, op->name);
}
break;
}
}
}
}
static
void serialize_primitive(
const ecs_world_t *world,
ecs_meta_type_op_t *op,
const void *base,
lua_State *L)
{
switch(op->kind)
{
case EcsBool:
lua_pushboolean(L, (int)*(bool*)base);
break;
case EcsChar:
lua_pushinteger(L, *(char*)base);
break;
case EcsString:
lua_pushstring(L, *(char**)base);
break;
case EcsByte:
lua_pushinteger(L, *(uint8_t*)base);
break;
case EcsU8:
lua_pushinteger(L, *(uint8_t*)base);
break;
case EcsU16:
lua_pushinteger(L, *(uint16_t*)base);
break;
case EcsU32:
lua_pushinteger(L, *(uint32_t*)base);
break;
case EcsU64:
lua_pushinteger(L, *(uint64_t*)base);
break;
case EcsI8:
lua_pushinteger(L, *(int8_t*)base);
break;
case EcsI16:
lua_pushinteger(L, *(int16_t*)base);
break;
case EcsI32:
lua_pushinteger(L, *(int32_t*)base);
break;
case EcsI64:
lua_pushinteger(L, *(int64_t*)base);
break;
case EcsF32:
lua_pushnumber(L, *(float*)base);
break;
case EcsF64:
lua_pushnumber(L, *(double*)base);
break;
case EcsEntity:
lua_pushinteger(L, *(ecs_entity_t*)base);
break;
case EcsIPtr:
lua_pushinteger(L, *(intptr_t*)base);
break;
case EcsUPtr:
lua_pushinteger(L, *(uintptr_t*)base);
break;
default:
luaL_error(L, "unknown primitive (%d)", op->kind);
}
}
static
void serialize_elements(
const ecs_world_t *world,
ecs_meta_type_op_t *ops,
int32_t op_count,
const void *base,
int32_t elem_count,
int32_t elem_size,
lua_State *L)
{
const void *ptr = base;
lua_createtable(L, elem_count, 0);
int i;
for(i=0; i < elem_count; i++)
{
serialize_type_ops(world, ops, op_count, ptr, 1, L);
lua_rawseti(L, -2, i + 1);
ptr = ECS_OFFSET(ptr, elem_size);
}
}
static
void serialize_type_elements(
const ecs_world_t *world,
ecs_entity_t type,
const void *base,
int32_t elem_count,
lua_State *L)
{
const EcsMetaTypeSerialized *ser = ecs_get(world, type, EcsMetaTypeSerialized);
ecs_assert(ser != NULL, ECS_INTERNAL_ERROR, NULL);
const EcsComponent *comp = ecs_get(world, type, EcsComponent);
ecs_assert(comp != NULL, ECS_INTERNAL_ERROR, NULL);
ecs_meta_type_op_t *ops = ecs_vec_first(&ser->ops);
int32_t op_count = ecs_vec_count(&ser->ops);
serialize_elements(world, ops, op_count, base, elem_count, comp->size, L);
}
static
void serialize_array(
const ecs_world_t *world,
ecs_meta_type_op_t *op,
const void *base,
lua_State *L)
{
const EcsArray *a = ecs_get(world, op->type, EcsArray);
ecs_assert(a != NULL, ECS_INTERNAL_ERROR, NULL);
serialize_type_elements(world, a->type, base, op->count, L);
}
static
void serialize_vector(
const ecs_world_t *world,
ecs_meta_type_op_t *op,
const void *base,
lua_State *L)
{
ecs_vec_t *value = *(ecs_vec_t**)base;
if(!value)
{
lua_pushnil(L);
return;
}
const EcsVector *v = ecs_get(world, op->type, EcsVector);
ecs_assert(v != NULL, ECS_INTERNAL_ERROR, NULL);
const EcsComponent *comp = ecs_get(world, v->type, EcsComponent);
ecs_assert(comp != NULL, ECS_INTERNAL_ERROR, NULL);
int32_t count = ecs_vec_count(value);
void *array = ecs_vec_first(value);
serialize_type_elements(world, v->type, array, count, L);
}
static
void serialize_type_op(
const ecs_world_t *world,
ecs_meta_type_op_t *op,
const void *base,
lua_State *L)
{
switch(op->kind)
{
case EcsOpPush:
case EcsOpPop:
ecs_abort(ECS_INVALID_PARAMETER, NULL);
break;
case EcsOpEnum:
case EcsOpBitmask:
lua_pushinteger(L, *(int32_t*)ECS_OFFSET(base, op->offset));
break;
case EcsOpArray:
serialize_array(world, op, ECS_OFFSET(base, op->offset), L);
break;
case EcsOpVector:
serialize_vector(world, op, ECS_OFFSET(base, op->offset), L);
break;
//case EcsOpMap:
// serialize_map(world, op, ECS_OFFSET(base, op->offset), L);
//break;
case EcsOpPrimitive:
serialize_primitive(world, op, ECS_OFFSET(base, op->offset), L);
break;
//////////////////////////
case EcsOpBool:
lua_pushboolean(L, (int)*(bool*)ECS_OFFSET(base, op->offset));
break;
case EcsOpChar:
lua_pushinteger(L, *(char*)ECS_OFFSET(base, op->offset));
break;
case EcsOpByte:
lua_pushinteger(L, *(uint8_t*)ECS_OFFSET(base, op->offset));
break;
case EcsOpU8:
lua_pushinteger(L, *(uint8_t*)ECS_OFFSET(base, op->offset));
break;
case EcsOpU16:
lua_pushinteger(L, *(uint16_t*)ECS_OFFSET(base, op->offset));
break;
case EcsOpU32:
lua_pushinteger(L, *(uint32_t*)ECS_OFFSET(base, op->offset));
break;
case EcsOpU64:
lua_pushinteger(L, *(uint64_t*)ECS_OFFSET(base, op->offset));
break;
case EcsOpI8:
lua_pushinteger(L, *(int8_t*)ECS_OFFSET(base, op->offset));
break;
case EcsOpI16:
lua_pushinteger(L, *(int16_t*)ECS_OFFSET(base, op->offset));
break;
case EcsOpI32:
lua_pushinteger(L, *(int32_t*)ECS_OFFSET(base, op->offset));
break;
case EcsOpI64:
lua_pushinteger(L, *(int64_t*)ECS_OFFSET(base, op->offset));
break;
case EcsOpF32:
lua_pushnumber(L, *(float*)ECS_OFFSET(base, op->offset));
break;
case EcsOpF64:
lua_pushnumber(L, *(double*)ECS_OFFSET(base, op->offset));
break;
case EcsOpEntity:
lua_pushinteger(L, *(ecs_entity_t*)ECS_OFFSET(base, op->offset));
break;
case EcsOpIPtr:
lua_pushinteger(L, *(intptr_t*)ECS_OFFSET(base, op->offset));
break;
case EcsOpUPtr:
lua_pushinteger(L, *(uintptr_t*)ECS_OFFSET(base, op->offset));
break;
case EcsOpString:
lua_pushstring(L, *(char**)ECS_OFFSET(base, op->offset));
break;
}
}
static
void serialize_constants(
ecs_world_t *world,
ecs_meta_type_op_t *op,
lua_State *L,
const char *prefix,
bool lowercase)
{
const ecs_map_t *map = NULL;
const EcsEnum *enum_type = NULL;
const EcsBitmask *bitmask_type = NULL;
enum_type = ecs_get(world, op->type, EcsEnum);
if(enum_type != NULL)
{
map = &enum_type->constants;
}
else
{
bitmask_type = ecs_get(world, op->type, EcsBitmask);
ecs_assert(bitmask_type != NULL, ECS_INVALID_PARAMETER, NULL);
map = &bitmask_type->constants;
}
void *ptr;
ecs_map_key_t key;
ecs_map_iter_t it = ecs_map_iter(map);
const char *name;
lua_Integer value;
while(ecs_map_next(&it))
{
ptr = ecs_map_ptr(&it);
if(enum_type != NULL)
{
ecs_enum_constant_t *constant = ptr;
name = constant->name;
value = constant->value;
}
else
{
ecs_bitmask_constant_t *constant = ptr;
name = constant->name;
value = constant->value;
}
if(prefix)
{
char *ptr = strstr(name, prefix);
size_t len = strlen(prefix);
if(ptr == name && ptr[len] != '\0')
{
name = ptr + len;
}
}
if(lowercase)
{
char *p = ecs_os_strdup(name);
int i;
for(i=0; p[i] != '\0'; i++)
{
p[i] = tolower(p[i]);
}
lua_pushstring(L, p);
ecs_os_free(p);
}
else lua_pushstring(L, name);
lua_pushinteger(L, value);
lua_settable(L, -3);
}
}
static
void serialize_type(
const ecs_world_t *world,
const ecs_vec_t *v_ops,
const void *base,
lua_State *L)
{
ecs_assert(base != NULL, ECS_INVALID_PARAMETER, NULL);
ecs_meta_type_op_t *ops = ecs_vec_first(v_ops);
int32_t count = ecs_vec_count(v_ops);
serialize_type_ops(world, ops, count, base, 0, L);
}
static
void update_type(
const ecs_world_t *world,
const ecs_vec_t *ser,
const void *base,
lua_State *L,
int idx)
{
ecs_assert(base != NULL, ECS_INVALID_PARAMETER, NULL);
ecs_meta_type_op_t *ops = (ecs_meta_type_op_t*)ecs_vec_first(ser);
int32_t count = ecs_vec_count(ser);
lua_pushvalue(L, idx);
int i, depth = 0;
for(i=0; i < count; i++)
{
ecs_meta_type_op_t *op = &ops[i];
switch(op->kind)
{
case EcsOpPush:
{
depth++;
if(depth > 1)
{
ecs_assert(op->name != NULL, ECS_INVALID_PARAMETER, NULL);
int t = lua_getfield(L, -1, op->name);
if(t != LUA_TTABLE)
{
lua_pop(L, 1);
lua_newtable(L);
lua_pushvalue(L, -1);
lua_setfield(L, -3, op->name);
}
}
break;
}
case EcsOpPop:
{
if(depth > 1) lua_pop(L, 1);
depth--;
break;
}
default:
{
serialize_type_op(world, op, base, L);
if(op->name && op != ops) lua_setfield(L, -2, op->name);
break;
}
}
}
lua_pop(L, 1);
}
static void deserialize_type(lua_State *L, int idx, ecs_meta_cursor_t *c, int depth)
{
int ktype, vtype, ret, mtype, prev_ikey;
bool in_array = false, designated_initializer = false;
idx = lua_absindex(L, idx);
vtype = lua_type(L, idx);
mtype = 0;
prev_ikey = 0;
if(vtype == LUA_TTABLE)
{
ret = ecs_meta_push(c);
ecs_assert(!ret, ECS_INTERNAL_ERROR, NULL);
in_array = ecs_meta_is_collection(c);
lua_pushnil(L);
}
else luaL_checktype(L, idx, LUA_TTABLE); // Error out (only struct/array components supported for now)
while(lua_next(L, idx))
{
ktype = lua_type(L, -2);
vtype = lua_type(L, -1);
if(!mtype) mtype = ktype;
if(ktype == LUA_TSTRING)
{
const char *key = lua_tostring(L, -2);
ecs_lua_dbg_pad(depth);
ecs_lua_dbg("meta_member field: %s", key);
ret = ecs_meta_member(c, key);
if(ret) luaL_error(L, "field \"%s\" does not exist", key);
if(mtype != ktype) luaL_error(L, "table has mixed key types (string key '%s')", key);
}
else if(ktype == LUA_TNUMBER)
{
lua_Integer key = lua_tointeger(L, -2) - 1;
ecs_lua_dbg_pad(depth);
ecs_lua_dbg("move idx: %lld", key);
//prefer not to trigger error messages in flecs
//XXX: double check we're never entering designated init mode after a string key was found
if(in_array)
{
ret = ecs_meta_elem(c, key);
}
else if(designated_initializer)
{
if(key == (prev_ikey + 1))
{
ret = ecs_meta_next(c);
prev_ikey = key;
}
else ret = 1;
}
else if(!key)
{
designated_initializer = true;
}
else //designated initializer does not start with 0
{
ret = 1;
}
if(ret) luaL_error(L, "invalid index %I (Lua [%I])", key, key + 1);
if(mtype != ktype) luaL_error(L, "table has mixed key types (int key [%I]", key+1); //XXX: move this up?
}
else luaL_error(L, "invalid key type '%s'", lua_typename(L, ktype));
switch(vtype)
{
case LUA_TTABLE:
{
ecs_lua_dbg_pad(depth);
ecs_lua_dbg("meta_push (nested)");
//ecs_meta_push(c);
//lua_pushnil(L);
depth++;
int top = lua_gettop(L);
deserialize_type(L, top, c, depth);
depth--;
break;
}
case LUA_TNUMBER:
{
if(lua_isinteger(L, -1))
{
lua_Integer integer = lua_tointeger(L, -1);
ecs_lua_dbg_pad(depth);
ecs_lua_dbg("set_int: %lld", integer);
/* XXX: Workaround for U64 bounds check */
ecs_meta_scope_t *scope = &c->scope[c->depth];
ecs_meta_type_op_t *op = &scope->ops[scope->op_cur];
if(op->kind == EcsOpU64) ret = ecs_meta_set_uint(c, integer);
else ret = ecs_meta_set_int(c, integer);
if(ret) luaL_error(L, "failed to set integer (%I)", integer);
}
else
{
lua_Number number = lua_tonumber(L, -1);
ecs_lua_dbg_pad(depth);
ecs_lua_dbg("set_float %f", number);
ret = ecs_meta_set_float(c, number);
if(ret) luaL_error(L, "failed to set float (%f)", number);
}
break;
}
case LUA_TBOOLEAN:
{
ecs_lua_dbg_pad(depth);
ecs_lua_dbg("set_bool: %d", lua_toboolean(L, -1));
ret = ecs_meta_set_bool(c, lua_toboolean(L, -1));
ecs_assert(!ret, ECS_INTERNAL_ERROR, NULL);
break;
}
case LUA_TSTRING:
{
ecs_lua_dbg_pad(depth);
ecs_lua_dbg("set_string: %s", lua_tostring(L, -1));
ret = ecs_meta_set_string(c, lua_tostring(L, -1));
ecs_assert(!ret, ECS_INTERNAL_ERROR, NULL);
break;
}
default:
{
if(ktype == LUA_TSTRING)
luaL_error(L, "invalid type for field '%s' (got %s)", lua_tostring(L, -2), lua_typename(L, vtype));
else
luaL_error(L, "invalid type at index [%d] (got %s)", lua_tointeger(L, -2), lua_typename(L, vtype));
}
}
lua_pop(L, 1);
}
ecs_lua_dbg_pad(depth);
ecs_lua_dbg("meta_pop");
ret = ecs_meta_pop(c);
ecs_assert(!ret, ECS_INTERNAL_ERROR, NULL);
}
static
void serialize_column(
ecs_world_t *world,
lua_State *L,
const EcsMetaTypeSerialized *ser,
const void *base,
int32_t count)
{
int32_t op_count = ecs_vec_count(&ser->ops);
ecs_meta_type_op_t *ops = ecs_vec_first(&ser->ops);
serialize_elements(world, ops, op_count, base, count, ops->size, L); //XXX: not sure about ops->size
}
static const EcsMetaTypeSerialized *get_serializer(lua_State *L, const ecs_world_t *world, ecs_entity_t type)
{
return ecs_get(world, type, EcsMetaTypeSerialized);
//TODO: check if this optimization is still needed
world = ecs_get_world(world);
int ret = lua_rawgetp(L, LUA_REGISTRYINDEX, world);
ecs_assert(ret == LUA_TTABLE, ECS_INTERNAL_ERROR, NULL);
ret = lua_rawgeti(L, -1, ECS_LUA_TYPES);
ecs_assert(ret == LUA_TTABLE, ECS_INTERNAL_ERROR, NULL);
ret = lua_rawgeti(L, -1, type);
ecs_ref_t *ref;
if(ret != LUA_TNIL)
{
ecs_assert(ret == LUA_TUSERDATA, ECS_INTERNAL_ERROR, NULL);
ref = lua_touserdata(L, -1);
lua_pop(L, 3);
}
else
{
lua_pop(L, 1); /* -nil */
ref = lua_newuserdata(L, sizeof(ecs_ref_t));
lua_rawseti(L, -2, type);
*ref = ecs_ref_init_id(world, type, ecs_id(EcsMetaTypeSerialized));
lua_pop(L, 2); /* -types, -world */
}
const EcsMetaTypeSerialized *ser = ecs_ref_get_id(world, ref, 0);
ecs_assert(ser != NULL, ECS_INTERNAL_ERROR, NULL);
return ser;
}
static int columns__len(lua_State *L)
{
ecs_iter_t *it = lua_touserdata(L, lua_upvalueindex(1));
lua_pushinteger(L, it->field_count);
return 1;
}
static int columns__index(lua_State *L)
{
ecs_iter_t *it = lua_touserdata(L, lua_upvalueindex(1));
ecs_world_t *world = it->world;
lua_Integer i = luaL_checkinteger(L, 2);
if(i < 1 || i > it->field_count) luaL_argerror(L, 1, "invalid term index");
if(!it->count)
{
lua_pushnil(L);
return 1;
}
ecs_entity_t type = ecs_get_typeid(world, ecs_field_id(it, i));
const EcsMetaTypeSerialized *ser = get_serializer(L, world, type);
if(!ser) luaL_error(L, "term %d cannot be serialized", i);
lua_settop(L, 1); /* (it.)columns */
const void *base = ecs_field_w_size(it, 0, i);
if(!ecs_field_is_self(it, i)) serialize_type(world, &ser->ops, base, L);
else serialize_column(world, L, ser, base, it->count);
lua_pushvalue(L, -1);
lua_rawseti(L, -3, i);
return 1;
}
static int entities__index(lua_State *L)
{
ecs_iter_t *it = ecs_lua__checkiter(L, 1);
lua_Integer i = luaL_checkinteger(L, 2);
if(i < 1 || i > it->count)
{
if(!it->count) return luaL_error(L, "no matched entities");
return luaL_error(L, "invalid index (%I)", i, it->count);
}
lua_pushinteger(L, it->entities[i-1]);
return 1;
}
/* expects "it" table at stack top */
static void push_columns(lua_State *L, ecs_iter_t *it)
{
if(!it->count)
{
lua_newtable(L);
lua_setfield(L, -2, "columns");
return;
}
/* it.columns[] */
lua_createtable(L, it->field_count, 1);
/* metatable */
lua_createtable(L, 0, 2);
lua_pushlightuserdata(L, it);
lua_pushcclosure(L, columns__index, 1);
lua_setfield(L, -2, "__index");
lua_pushlightuserdata(L, it);
lua_pushcclosure(L, columns__len, 1);
lua_setfield(L, -2, "__len");
lua_setmetatable(L, -2);
lua_setfield(L, -2, "columns");
}
/* expects "it" table at stack top */
static void push_iter_metadata(lua_State *L, ecs_iter_t *it)
{
lua_pushinteger(L, it->count);
lua_setfield(L, -2, "count");
lua_pushinteger(L, it->system);
lua_setfield(L, -2, "system");
lua_pushinteger(L, it->event);
lua_setfield(L, -2, "event");
lua_pushinteger(L, it->event_id);
lua_setfield(L, -2, "event_id");
// lua_pushinteger(L, it->self);
// lua_setfield(L, -2, "self");
lua_pushnumber(L, it->delta_time);
lua_setfield(L, -2, "delta_time");
lua_pushnumber(L, it->delta_system_time);
lua_setfield(L, -2, "delta_system_time");
lua_pushinteger(L, it->table_count);
lua_setfield(L, -2, "table_count");
lua_pushinteger(L, it->term_index);
lua_setfield(L, -2, "term_index");
if(it->system)
{
ecs_lua_callback *sys = it->binding_ctx;
if(sys->param_ref >= 0)
{
int type = ecs_lua_rawgeti(L, it->world, sys->param_ref);
ecs_assert(type != LUA_TNIL, ECS_INTERNAL_ERROR, NULL);
}
else lua_pushnil(L);
//lua_pushvalue(L, -1);
//lua_setfield(L, -3, "ctx");
lua_setfield(L, -2, "param");
}
/* it.entities */
lua_createtable(L, 0, 1);
/* metatable */
lua_createtable(L, 0, 2);
lua_pushlightuserdata(L, it);
lua_setfield(L, -2, "__ecs_iter");
lua_pushcfunction(L, entities__index);
lua_setfield(L, -2, "__index");
lua_setmetatable(L, -2);
lua_setfield(L, -2, "entities");
}
/* expects table at stack top */
static ecs_iter_t *push_iter_metafield(lua_State *L, ecs_iter_t *it, bool copy)
{
/* metatable */
lua_createtable(L, 0, 1);
/* metatable.__ecs_iter = it */
if(copy)
{
ecs_iter_t *ptr = lua_newuserdata(L, sizeof(ecs_iter_t));
memcpy(ptr, it, sizeof(ecs_iter_t));
it = ptr;
}
else lua_pushlightuserdata(L, it);
lua_setfield(L, -2, "__ecs_iter");
lua_setmetatable(L, -2);
return it;
}
/* Reset with a new base pointer */
static void meta_reset(ecs_meta_cursor_t *cursor, void *base)
{
ecs_assert(cursor != NULL, ECS_INVALID_PARAMETER, NULL);
ecs_assert(cursor->valid, ECS_INVALID_PARAMETER, NULL);
ecs_assert(base != NULL, ECS_INVALID_PARAMETER, NULL);
cursor->depth = 0;
cursor->scope[0].op_cur = 0;
cursor->scope[0].elem_cur = 0;
cursor->scope[0].ptr = base;
}
static ecs_meta_cursor_t *ecs_lua_cursor(lua_State *L, const ecs_world_t *world, ecs_entity_t type, void *base)
{
const ecs_world_t *real_world = ecs_get_world(world);
int ret = lua_rawgetp(L, LUA_REGISTRYINDEX, real_world);
ecs_assert(ret == LUA_TTABLE, ECS_INTERNAL_ERROR, NULL);
ret = lua_rawgeti(L, -1, ECS_LUA_CURSORS);
ecs_assert(ret == LUA_TTABLE, ECS_INTERNAL_ERROR, NULL);
ret = lua_rawgeti(L, -1, type);
ecs_meta_cursor_t *cursor;
if(ret == LUA_TUSERDATA)
{
cursor = lua_touserdata(L, -1);
lua_pop(L, 3);
meta_reset(cursor, base);
}
else
{
lua_pop(L, 1);
cursor = lua_newuserdata(L, sizeof(ecs_meta_cursor_t));
lua_rawseti(L, -2, type);
lua_pop(L, 2);
*cursor = ecs_meta_cursor(world, type, base);
}
return cursor;
}
static
void deserialize_column(
const ecs_world_t *world,
lua_State *L,
int idx,
ecs_entity_t type,
void *base,
size_t stride,
int32_t count)
{
ecs_meta_cursor_t *c = ecs_lua_cursor(L, world, type, base);
int j;
for(j=0; j < count; j++)
{
meta_reset(c, (char*)base + j * stride);
lua_rawgeti(L, idx, j + 1); /* columns[i+1][j+1] */
deserialize_type(L, -1, c, 0);
lua_pop(L, 1);
}
}
void ecs_ptr_to_lua(
const ecs_world_t *world,
lua_State *L,
ecs_entity_t type,
const void *ptr)
{
const EcsMetaTypeSerialized *ser = get_serializer(L, world, type);
serialize_type(world, &ser->ops, ptr, L);
}
void ecs_lua_to_ptr(
const ecs_world_t *world,
lua_State *L,
int idx,
ecs_entity_t type,
void *ptr)
{
ecs_meta_cursor_t *c = ecs_lua_cursor(L, world, type, ptr);
deserialize_type(L, idx, c, 0);
}
void ecs_lua_type_update(
const ecs_world_t *world,
lua_State *L,
int idx,
ecs_entity_t type,
void *ptr)
{
const EcsMetaTypeSerialized *ser = get_serializer(L, world, type);
update_type(world, &ser->ops, ptr, L, idx);
}
ecs_iter_t *ecs_iter_to_lua(ecs_iter_t *it, lua_State *L, bool copy)
{
/* it */