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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
|
#include "lua_module_manage_internal.h"
#include "stellar/utils.h"
#include <string.h>
#include <assert.h>
#include <unistd.h>
#include <toml.h>
#include <utlist.h>
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
/* ***** ***** ***** ***** ***** ***** */
/* context */
/* ***** ***** ***** ***** ***** ***** */
const char *context_magic_code = "ctx_code";
struct lua_context
{
const char *magic_code;
struct lua_state *state;
int lua_context_ref_id;
};
struct lua_context *lua_context_new(struct lua_state *state)
{
if (!state)
return NULL;
lua_State *L = (lua_State *)(state);
lua_newtable(L);
int ref_id = luaL_ref(L, LUA_REGISTRYINDEX);
struct lua_context *new_context = lua_context_new_with_ref_id(state, ref_id);
if (!new_context)
luaL_unref(L, LUA_REGISTRYINDEX, ref_id);
return new_context;
}
struct lua_context *lua_context_new_with_ref_id(struct lua_state *state, int ref_id)
{
if (!state)
return NULL;
struct lua_context *new_context = CALLOC(struct lua_context, 1);
new_context->magic_code = context_magic_code;
new_context->state = state;
new_context->lua_context_ref_id = ref_id;
return new_context;
}
void lua_context_free(struct lua_context *context)
{
if (!context)
return;
luaL_unref((lua_State *)(context->state), LUA_REGISTRYINDEX, context->lua_context_ref_id);
FREE(context);
return;
}
void lua_context_push_stack(struct lua_context *context)
{
if (context && context->lua_context_ref_id)
lua_rawgeti((lua_State *)(context->state), LUA_REGISTRYINDEX, context->lua_context_ref_id);
return;
}
int lua_context_check_if_context(void *context)
{
struct lua_context *check = (struct lua_context *)context;
if (check && check->magic_code == context_magic_code)
return TRUE;
return FALSE;
}
/* ***** ***** ***** ***** ***** ***** */
/* operation to lua_cdata */
/* ***** ***** ***** ***** ***** ***** */
struct lua_cnode
{
char *node_string_key;
int node_integer_key;
struct lua_cdata *node_data;
};
struct lua_ctable
{
size_t array_size;
size_t node_size;
struct lua_cdata **array_data;
struct lua_cnode **node_data;
};
struct lua_cdata
{
enum LUA_DATATYPE type;
size_t data_len; /* 只有在类型为buff或function时使用此标识 */
union
{
int bool;
int integer;
double number;
char *string;
char *buff;
int table;
void *pointer;
struct lua_context *context;
void *function;
struct lua_ctable *ctable;
};
};
static struct lua_ctable *lua_ctable_new(void);
static void lua_ctable_free(struct lua_ctable *ctable);
static int lua_ctable_push_stack(struct lua_state *state, struct lua_ctable *ctable);
static int lua_ctable_pop_stack(struct lua_state *state, struct lua_ctable *ctable);
static struct lua_cnode *lua_cnode_new(void);
static void lua_cnode_free(struct lua_cnode *cnode);
static int lua_cdata_function_reader(lua_State *state __unused, const void *p, size_t sz, void *ud);
static const char *lua_cdata_function_writer(lua_State *state __unused, void *ud, size_t *sz);
struct lua_cdata *lua_cdata_new(void)
{
struct lua_cdata *new_data = CALLOC(struct lua_cdata, 1);
memset(new_data, 0, sizeof(struct lua_cdata));
return new_data;
}
static void lua_cdata_inner_data_clean(struct lua_cdata *cdata)
{
cdata->type = DATATYPE_BEGIN;
cdata->data_len = 0;
switch (cdata->type)
{
case DATATYPE_STRING:
FREE(cdata->string);
break;
case DATATYPE_LUA_TABLE:
lua_ctable_free(cdata->ctable);
break;
case DATATYPE_BUFF:
FREE(cdata->buff);
break;
case DATATYPE_FUNCTION:
FREE(cdata->function);
break;
default:
break;
}
return;
}
void lua_cdata_free(struct lua_cdata *cdata)
{
if (!cdata)
return;
lua_cdata_inner_data_clean(cdata);
FREE(cdata);
return;
}
struct lua_cdata *lua_cdata_array_new(size_t cdata_num)
{
if (cdata_num == 0)
return NULL;
struct lua_cdata *new_cdata = CALLOC(struct lua_cdata, cdata_num);
memset(new_cdata, 0, sizeof(struct lua_cdata) * cdata_num);
return new_cdata;
}
void lua_cdata_array_free(struct lua_cdata *cdata_array, size_t data_num)
{
if (!cdata_array || data_num == 0)
return;
for (size_t data_index = 0; data_index < data_num; ++data_index)
{
lua_cdata_inner_data_clean(&cdata_array[data_index]);
}
FREE(cdata_array);
return;
}
struct lua_cdata *lua_cdata_array_get_index(struct lua_cdata *data_array, size_t index)
{
return (struct lua_cdata *)(data_array + index);
}
int lua_cdata_data_set(struct lua_cdata *cdata, enum LUA_DATATYPE type, void *value_p, int value_i)
{
if (!cdata)
return PARAM_ERR;
lua_cdata_inner_data_clean(cdata);
switch (type)
{
case DATATYPE_NIL:
break;
case DATATYPE_BOOL:
cdata->bool = value_i;
break;
case DATATYPE_INT:
cdata->integer = value_i;
break;
case DATATYPE_NUM:
cdata->number = *(double *)value_p;
break;
case DATATYPE_STRING:
if (!value_p || value_i <= 0)
return CDATA_SET_VALUE_NULL_POINTER;
cdata->string = CALLOC(char, value_i);
memcpy(cdata->string, value_p, (size_t)value_i);
cdata->data_len = (size_t)value_i;
break;
case DATATYPE_BUFF:
if (!value_p || value_i <= 0)
return CDATA_SET_VALUE_NULL_POINTER;
cdata->buff = CALLOC(char, value_i);
memcpy(cdata->buff, value_p, (size_t)value_i);
cdata->data_len = (size_t)value_i;
break;
case DATATYPE_TABLE:
cdata->table = value_i;
break;
case DATATYPE_POINTER:
cdata->pointer = value_p;
break;
case DATATYPE_CONTEXT:
cdata->context = (struct lua_context *)value_p;
break;
default:
return CDATA_SET_VALUE_TYPE_UNSUPPORT;
}
cdata->type = type;
return SUCCESS;
}
int lua_cdata_push_stack(struct lua_state *state, struct lua_cdata *cdata)
{
if (!state || !cdata)
return PARAM_ERR;
lua_State *L = (lua_State *)(state);
switch (cdata->type)
{
case DATATYPE_NIL:
lua_pushnil(L);
break;
case DATATYPE_BOOL:
lua_pushboolean(L, cdata->bool);
break;
case DATATYPE_INT:
lua_pushinteger(L, cdata->integer);
break;
case DATATYPE_NUM:
lua_pushnumber(L, cdata->number);
break;
case DATATYPE_STRING:
lua_pushstring(L, cdata->string);
break;
case DATATYPE_BUFF:
lua_pushlstring(L, cdata->buff, cdata->data_len);
break;
case DATATYPE_TABLE:
if (cdata->table)
lua_rawgeti(L, LUA_REGISTRYINDEX, cdata->table);
break;
case DATATYPE_LUA_TABLE:
lua_ctable_push_stack(state, cdata->ctable);
break;
case DATATYPE_POINTER:
lua_pushlightuserdata(L, (void *)cdata->pointer);
break;
case DATATYPE_CONTEXT:
lua_context_push_stack(cdata->context);
break;
case DATATYPE_FUNCTION:
lua_load(L, lua_cdata_function_writer, (void *)cdata, NULL, NULL);
break;
default:
return CDATA_PUSH_STACK_TYPE_UNKNOWN;
}
return SUCCESS;
}
int lua_cdata_pop_stack(struct lua_state *state, struct lua_cdata *cdata)
{
if (!state || !cdata)
return PARAM_ERR;
lua_State *L = (lua_State *)(state);
lua_cdata_inner_data_clean(cdata);
switch (lua_type(L, -1))
{
case LUA_TNIL:
cdata->type = DATATYPE_NIL;
break;
case LUA_TBOOLEAN:
cdata->type = DATATYPE_BOOL;
cdata->bool = lua_toboolean(L, -1);
break;
case LUA_TLIGHTUSERDATA:
cdata->type = DATATYPE_POINTER;
cdata->pointer = (void *)lua_topointer(L, -1);
break;
case LUA_TNUMBER:
cdata->type = DATATYPE_NUM;
cdata->number = lua_tonumber(L, -1);
int try_int = (int)cdata->number;
if ((double)try_int == cdata->number)
{
cdata->type = DATATYPE_INT;
cdata->integer = lua_tointeger(L, -1);
}
break;
case LUA_TSTRING:
cdata->type = DATATYPE_STRING;
cdata->string = (char *)strdup(lua_tostring(L, -1));
break;
case LUA_TTABLE:
cdata->type = DATATYPE_LUA_TABLE;
cdata->ctable = lua_ctable_new();
lua_ctable_pop_stack(state, cdata->ctable);
break;
case LUA_TFUNCTION:
cdata->type = DATATYPE_FUNCTION;
lua_dump(L, lua_cdata_function_reader, cdata, 0);
break;
case LUA_TUSERDATA:
cdata->type = DATATYPE_POINTER;
cdata->pointer = (void *)lua_topointer(L, -1);
break;
default:
return CDATA_POP_STACK_TYPE_UNSUPPORT;
}
lua_pop(L, 1);
return SUCCESS;
}
static struct lua_ctable *lua_ctable_new(void)
{
struct lua_ctable *new_table = CALLOC(struct lua_ctable, 1);
memset(new_table, 0, sizeof(struct lua_ctable));
return new_table;
}
static void lua_ctable_free(struct lua_ctable *ctable)
{
if (!ctable)
return;
for (unsigned array_index = 0; array_index < ctable->array_size; array_index++)
{
lua_cdata_free(ctable->array_data[array_index]);
}
for (unsigned node_index = 0; node_index < ctable->node_size; node_index++)
{
lua_cnode_free(ctable->node_data[node_index]);
}
if (ctable->array_data)
FREE(ctable->array_data);
if (ctable->node_data)
FREE(ctable->node_data);
FREE(ctable);
return;
}
static int lua_ctable_push_stack(struct lua_state *state, struct lua_ctable *ctable)
{
if (!state || !ctable)
return PARAM_ERR;
lua_State *L = (lua_State *)(state);
lua_newtable(L);
int push_ret = 0;
for (unsigned array_index = 0; array_index < ctable->array_size; array_index++)
{
lua_pushinteger(L, (array_index + 1));
push_ret = lua_cdata_push_stack(state, ctable->array_data[array_index]);
if (push_ret)
{
lua_settop(L, 0);
return push_ret;
}
lua_settable(L, -3);
}
for (unsigned node_index = 0; node_index < ctable->node_size; node_index++)
{
if (ctable->node_data[node_index]->node_string_key)
lua_pushstring(L, ctable->node_data[node_index]->node_string_key);
else
lua_pushinteger(L, ctable->node_data[node_index]->node_integer_key);
push_ret = lua_cdata_push_stack(state, ctable->node_data[node_index]->node_data);
if (push_ret)
{
lua_settop(L, 0);
return push_ret;
}
lua_settable(L, -3);
}
return SUCCESS;
}
static int lua_ctable_pop_stack(struct lua_state *state, struct lua_ctable *ctable)
{
if (!state || !ctable)
return PARAM_ERR;
lua_State *L = (lua_State *)(state);
/* 获取table长度 */
ctable->array_size = (size_t)lua_rawlen(L, -1);
lua_pushnil(L);
while (lua_next(L, -2))
{
ctable->node_size++;
if (lua_type(L, -2) == LUA_TNUMBER)
{
int numkey = lua_tointeger(L, -2);
if (numkey > 0 && numkey <= (int)ctable->array_size)
ctable->node_size--;
}
lua_pop(L, 1);
}
ctable->array_data = CALLOC(struct lua_cdata *, ctable->array_size);
for (unsigned array_index = 0; array_index < ctable->array_size; array_index++)
{
ctable->array_data[array_index] = lua_cdata_new();
}
ctable->node_data = CALLOC(struct lua_cnode *, ctable->node_size);
for (unsigned node_index = 0; node_index < ctable->node_size; node_index++)
{
ctable->node_data[node_index] = lua_cnode_new();
}
/* 依次获取数据 */
int key_index = 0;
lua_pushnil(L);
while (lua_next(L, -2))
{
if (lua_type(L, -2) == LUA_TNUMBER)
{
int numkey = lua_tointeger(L, -2);
if (numkey > 0 && numkey <= (int)ctable->array_size)
lua_cdata_pop_stack(state, ctable->array_data[numkey - 1]);
else
{
lua_cdata_pop_stack(state, ctable->node_data[key_index]->node_data);
ctable->node_data[key_index]->node_integer_key = lua_tointeger(L, -1);
++key_index;
}
}
else if (lua_type(L, -2) == LUA_TSTRING)
{
lua_cdata_pop_stack(state, ctable->node_data[key_index]->node_data);
ctable->node_data[key_index]->node_string_key = strdup((char *)lua_tostring(L, -1));
++key_index;
}
}
lua_settop(L, 0);
return SUCCESS;
}
static struct lua_cnode *lua_cnode_new(void)
{
struct lua_cnode *new_node = CALLOC(struct lua_cnode, 1);
memset(new_node, 0, sizeof(struct lua_cnode));
new_node->node_data = lua_cdata_new();
return new_node;
}
static void lua_cnode_free(struct lua_cnode *cnode)
{
if (cnode->node_string_key)
FREE(cnode->node_string_key);
if (cnode->node_data)
lua_cdata_free(cnode->node_data);
FREE(cnode);
return;
}
static int lua_cdata_function_reader(lua_State *state __unused, const void *p, size_t sz, void *ud)
{
/* TODO: 这个函数在lua中会被大量调用, 频繁的realloc可能会导致内存碎片化, 后续调整一下内存分配机制 */
struct lua_cdata *data = (struct lua_cdata *)ud;
data->type = DATATYPE_FUNCTION;
if (!data->function)
data->function = (void *)calloc(1, sizeof(sz));
else
data->function = (void *)realloc(data->function, (data->data_len + sz));
memcpy((data->function + data->data_len), p, sz);
data->data_len += sz;
return 0;
}
static const char *lua_cdata_function_writer(lua_State *state __unused, void *ud, size_t *sz)
{
struct lua_cdata *data = (struct lua_cdata *)ud;
*sz = data->data_len;
return (const char *)data->function;
}
/* ***** ***** ***** ***** ***** ***** */
/* lua fn arg pair */
/* ***** ***** ***** ***** ***** ***** */
struct lua_fn_arg_pair
{
struct lua_fn_arg_pair *next;
struct lua_module_manager *lua_mod_mgr;
int lua_fn_ref_id;
int lua_arg_ref_id;
};
struct lua_fn_arg_pair *lua_fn_arg_pair_new(struct lua_module_manager *lua_mod_mgr, int fn_ref_id, int arg_ref_id)
{
struct lua_fn_arg_pair *new_pair = CALLOC(struct lua_fn_arg_pair, 1);
new_pair->lua_mod_mgr = lua_mod_mgr;
new_pair->lua_fn_ref_id = fn_ref_id;
new_pair->lua_arg_ref_id = arg_ref_id;
return new_pair;
}
void lua_fn_arg_pair_free(struct lua_fn_arg_pair *pair)
{
if (pair)
FREE(pair);
return;
}
void lua_fn_arg_pair_insert(struct lua_fn_arg_pair *pair)
{
return lua_module_manger_pair_list_insert(pair->lua_mod_mgr, pair);
}
int lua_fn_arg_pair_get_fn_ref_id(struct lua_fn_arg_pair *pair)
{
if (pair)
return pair->lua_fn_ref_id;
return 0;
}
int lua_fn_arg_pair_get_arg_ref_id(struct lua_fn_arg_pair *pair)
{
if (pair)
return pair->lua_arg_ref_id;
return 0;
}
struct lua_module_manager *lua_fn_arg_pair_get_lua_mod_mgr(struct lua_fn_arg_pair *pair)
{
if (pair)
return pair->lua_mod_mgr;
return NULL;
}
/* ***** ***** ***** ***** ***** ***** */
/* lua state */
/* ***** ***** ***** ***** ***** ***** */
struct lua_state
{
lua_State *state;
};
#define LUA_GLOBAL_INFO_REF_ID (LUA_RIDX_LAST + 2)
#define LUA_GLOBAL_INFO_MANAGE_POINTER "__global_lua_module_manage"
// #define LUA_GLOBAL_FUNCTION_REF_ID (LUA_RIDX_LAST + 3)
// #define LUA_GLOBAL_CONTEXT_REF_ID (LUA_RIDX_LAST + 4)
struct lua_state *lua_state_new(struct lua_module_manager *lua_mod_mgr)
{
lua_State *new_L = luaL_newstate();
if (!new_L)
return NULL;
luaL_openlibs(new_L);
lua_newtable(new_L);
lua_pushlightuserdata(new_L, (void *)lua_mod_mgr);
lua_setfield(new_L, -2, LUA_GLOBAL_INFO_MANAGE_POINTER);
int global_ref = luaL_ref(new_L, LUA_REGISTRYINDEX);
lua_settop(new_L, 0);
assert(global_ref == LUA_GLOBAL_INFO_REF_ID);
return (struct lua_state *)new_L;
}
void lua_state_free(struct lua_state *state)
{
if (!state)
return;
lua_close((lua_State *)state);
return;
}
int lua_state_cbinding_function(struct lua_state *state, struct lua_state_cbind_func_spec *bind_func_array, size_t bind_func_num)
{
if (!state || !bind_func_array)
return PARAM_ERR;
lua_State *L = (lua_State *)(state);
for (size_t index = 0; index < bind_func_num; ++index)
{
if (bind_func_array[index].func && bind_func_array[index].func_name)
{
if (bind_func_array[index].space_name)
{
lua_getglobal(L, bind_func_array[index].space_name);
if (lua_type(L, -1) == LUA_TNIL)
{
lua_pop(L, 1);
lua_newtable(L);
}
else
{
if (lua_type(L, -1) != LUA_TTABLE)
{
lua_settop(L, 0);
return STATE_BIND_FUNC_NAMESPACE_CONFLICT;
}
lua_getfield(L, -1, bind_func_array[index].func_name);
if (lua_type(L, -1) != LUA_TNIL)
{
lua_settop(L, 0);
return STATE_BIND_FUNC_NAME_CONFLICT;
}
lua_pop(L, 1);
}
lua_pushcfunction(L, (lua_CFunction)bind_func_array[index].func);
lua_setfield(L, -2, bind_func_array[index].func_name);
lua_setglobal(L, bind_func_array[index].space_name);
}
else
{
lua_getglobal(L, bind_func_array[index].func_name);
if (lua_type(L, -1) != LUA_TNIL)
{
lua_settop(L, 0);
return STATE_BIND_FUNC_NAME_CONFLICT;
}
lua_pop(L, 1);
lua_pushcfunction(L, (lua_CFunction)bind_func_array[index].func);
lua_setglobal(L, bind_func_array[index].func_name);
}
lua_settop(L, 0);
}
else
break;
}
return SUCCESS;
}
int lua_state_cbinding_data(struct lua_state *state, struct lua_state_cbind_data_spec *bind_data_array, size_t bind_data_num)
{
if (!state || !bind_data_array)
return PARAM_ERR;
lua_State *L = (lua_State *)(state);
for (size_t index = 0; index < bind_data_num; ++index)
{
if (bind_data_array[index].data_value && bind_data_array[index].data_name)
{
if (bind_data_array[index].space_name)
{
lua_getglobal(L, bind_data_array[index].space_name);
if (lua_type(L, -1) == LUA_TNIL)
{
lua_pop(L, 1);
lua_newtable(L);
}
else
{
if (lua_type(L, -1) != LUA_TTABLE)
{
lua_settop(L, 0);
return STATE_BIND_DATA_NAMESPACE_CONFLICT;
}
lua_getfield(L, -1, bind_data_array[index].data_name);
if (lua_type(L, -1) != LUA_TNIL)
{
lua_settop(L, 0);
return STATE_BIND_DATA_NAME_CONFLICT;
}
lua_pop(L, 1);
}
switch (bind_data_array[index].data_type)
{
case DATATYPE_BOOL:
if (strstr(bind_data_array[index].data_value, "true") || strstr(bind_data_array[index].data_value, "TRUE"))
lua_pushboolean(L, 1);
else
lua_pushboolean(L, 0);
break;
case DATATYPE_INT:
lua_pushinteger(L, atoi(bind_data_array[index].data_value));
break;
case DATATYPE_NUM:
lua_pushnumber(L, (lua_Number)atof(bind_data_array[index].data_value));
break;
case DATATYPE_STRING:
lua_pushlstring(L, (const char *)bind_data_array[index].data_value, strlen(bind_data_array[index].data_value));
break;
default:
lua_settop(L, 0);
return STATE_BIND_DATA_TYPE_ERR;
}
lua_setfield(L, -2, bind_data_array[index].data_name);
lua_setglobal(L, bind_data_array[index].space_name);
}
else
{
lua_getglobal(L, bind_data_array[index].data_name);
if (lua_type(L, -1) != LUA_TNIL)
{
lua_settop(L, 0);
return STATE_BIND_DATA_NAME_CONFLICT;
}
lua_pop(L, 1);
switch (bind_data_array[index].data_type)
{
case DATATYPE_BOOL:
if (strstr(bind_data_array[index].data_value, "true") || strstr(bind_data_array[index].data_value, "TRUE"))
lua_pushboolean(L, 1);
else
lua_pushboolean(L, 0);
break;
case DATATYPE_INT:
lua_pushinteger(L, atoi(bind_data_array[index].data_value));
break;
case DATATYPE_NUM:
lua_pushnumber(L, (lua_Number)atof(bind_data_array[index].data_value));
break;
case DATATYPE_STRING:
lua_pushlstring(L, (const char *)bind_data_array[index].data_value, strlen(bind_data_array[index].data_value));
break;
default:
lua_settop(L, 0);
return STATE_BIND_DATA_TYPE_ERR;
}
lua_setglobal(L, bind_data_array[index].data_name);
}
lua_settop(L, 0);
}
else
break;
}
return SUCCESS;
}
int lua_state_copy_ref_value(struct lua_state *to, struct lua_state *from)
{
if (!to || !from)
return PARAM_ERR;
lua_State *from_L = (lua_State *)(from);
lua_State *to_L = (lua_State *)(to);
lua_newtable(from_L);
int lua_ref_max_id = luaL_ref(from_L, LUA_REGISTRYINDEX);
int data_ret = 0;
for (int ref_index = (LUA_GLOBAL_INFO_REF_ID + 1); ref_index < lua_ref_max_id; ++ref_index)
{
struct lua_cdata *trans_data = lua_cdata_new();
lua_settop(from_L, 0);
lua_rawgeti(from_L, LUA_REGISTRYINDEX, ref_index);
data_ret = lua_cdata_pop_stack(from, trans_data);
if (data_ret)
{
lua_cdata_free(trans_data);
return (STATE_COPY_REF_VALUE_ERR + data_ret);
}
data_ret = lua_cdata_push_stack(to, trans_data);
if (data_ret)
{
lua_cdata_free(trans_data);
return (STATE_COPY_REF_VALUE_ERR + data_ret);
}
int ref_id = luaL_ref(to_L, LUA_REGISTRYINDEX);
lua_cdata_free(trans_data);
if (ref_id != ref_index)
{
return STATE_COPY_REF_VALUE_CONFLICT;
}
}
luaL_unref(from_L, LUA_REGISTRYINDEX, lua_ref_max_id);
return SUCCESS;
}
int lua_state_execute_chunk(
struct lua_state *state,
int chunk_ref_id,
struct lua_cdata *param_array,
size_t param_num,
struct lua_cdata *return_array,
size_t return_num,
char *err_log,
size_t err_log_len)
{
if (!state || chunk_ref_id <= 0 || (param_num && !param_array) || (return_num && !return_array))
return PARAM_ERR;
lua_State *L = (lua_State *)state;
lua_rawgeti(L, LUA_REGISTRYINDEX, chunk_ref_id);
if (lua_type(L, -1) != LUA_TFUNCTION)
{
lua_settop(L, 0);
return STATE_CHUNK_REF_ID_NOT_FUNCTION;
}
int pushret = 0;
for (unsigned param_index = 0; param_index < param_num; ++param_index)
{
if ((pushret = lua_cdata_push_stack(state, ¶m_array[param_index])))
{
lua_settop(L, 0);
return (STATE_CHUNK_DATA_PUSH_ERR + pushret);
}
}
if (lua_pcall(L, param_num, LUA_MULTRET, 0))
{
if (err_log)
snprintf(err_log, (err_log_len - 1), "run script[%d] err %s\n", chunk_ref_id, lua_tostring(L, -1));
lua_settop(L, 0);
return STATE_CHUNK_RUNNING_ERR;
}
if (return_num > 0)
{
int count = lua_gettop(L);
/* 如果接收返回值数量不够, 只保留前r_num个返回值 */
if (count > (int)return_num)
{
lua_pop(L, (count - (int)return_num));
count = (int)return_num;
}
int pop_ret = 0;
for (int i = (count - 1); i >= 0; --i)
{
if ((pop_ret = lua_cdata_pop_stack(state, &(return_array[i]))))
{
lua_settop(L, 0);
return (STATE_CHUNK_DATA_POP_ERR + pop_ret);
}
}
}
lua_settop(L, 0);
return SUCCESS;
}
struct lua_module_manager *lua_state_get_lua_module_manager(struct lua_state *state)
{
lua_State *L = (lua_State *)(state);
lua_rawgeti(L, LUA_REGISTRYINDEX, LUA_GLOBAL_INFO_REF_ID);
lua_getfield(L, -1, LUA_GLOBAL_INFO_MANAGE_POINTER);
struct lua_module_manager *lua_mod_mgr = (struct lua_module_manager *)lua_topointer(L, -1);
lua_settop(L, 0);
return lua_mod_mgr;
}
/* ***** ***** ***** ***** ***** ***** */
/* lua module manager */
/* ***** ***** ***** ***** ***** ***** */
struct lua_load_script
{
char *file_path;
char *init_func_name;
char *exit_func_name;
int lua_init_fn_ref_id;
int lua_exit_fn_ref_id;
int lua_script_env_ref_id;
};
struct lua_module_manager
{
struct stellar_module_manager *mod_mgr;
size_t state_num;
size_t load_script_num;
struct lua_state **state_array;
struct lua_load_script *load_script_array;
struct lua_fn_arg_pair *pair_list;
};
/*
struct lua_fn_arg_pair *mq_dispatch_list;
struct lua_fn_arg_pair *mq_msg_free_list;
struct lua_fn_arg_pair *mq_on_msg_list;
struct lua_fn_arg_pair *on_packet_stage_list;
struct lua_fn_arg_pair *on_session_tcp_list;
struct lua_fn_arg_pair *on_session_udp_list;
struct lua_fn_arg_pair *on_session_control_packet_list;
struct lua_fn_arg_pair *on_tcp_stream_list;
*/
struct lua_module_manager *lua_module_manager_new(struct stellar_module_manager *mod_mgr)
{
if (!mod_mgr)
return NULL;
struct lua_module_manager *new_lua_mod_mgr = CALLOC(struct lua_module_manager, 1);
memset(new_lua_mod_mgr, 0, sizeof(struct lua_module_manager));
new_lua_mod_mgr->mod_mgr = mod_mgr;
return new_lua_mod_mgr;
}
void lua_module_manager_free(struct lua_module_manager *lua_mod_mgr)
{
if (!lua_mod_mgr)
return;
lua_module_manager_call_exit(lua_mod_mgr);
for (size_t state_index = 0; state_index < lua_mod_mgr->state_num; ++state_index)
{
lua_state_free(lua_mod_mgr->state_array[state_index]);
}
FREE(lua_mod_mgr->state_array);
for (size_t script_index = 0; script_index < lua_mod_mgr->load_script_num; ++script_index)
{
if (lua_mod_mgr->load_script_array[script_index].file_path)
FREE(lua_mod_mgr->load_script_array[script_index].file_path);
if (lua_mod_mgr->load_script_array[script_index].init_func_name)
FREE(lua_mod_mgr->load_script_array[script_index].init_func_name);
if (lua_mod_mgr->load_script_array[script_index].exit_func_name)
FREE(lua_mod_mgr->load_script_array[script_index].exit_func_name);
}
FREE(lua_mod_mgr->load_script_array);
struct lua_fn_arg_pair *free_pair = lua_mod_mgr->pair_list;
struct lua_fn_arg_pair *next_pair = NULL;
while (free_pair)
{
next_pair = free_pair->next;
lua_fn_arg_pair_free(free_pair);
free_pair = next_pair;
}
FREE(lua_mod_mgr);
return;
}
int lua_module_manager_state_create(
struct lua_module_manager *lua_mod_mgr,
struct lua_state_cbind_func_spec *bind_func_array,
size_t bind_func_num,
struct lua_state_cbind_data_spec *bind_data_array,
size_t bind_data_num)
{
if (!lua_mod_mgr)
return PARAM_ERR;
int thread_num = stellar_module_manager_get_max_thread_num(lua_mod_mgr->mod_mgr);
if (thread_num < 0)
return LMM_STATE_THREAD_NUM_ERR;
lua_mod_mgr->state_num = (size_t)thread_num;
if (lua_mod_mgr->state_num == 0)
return SUCCESS;
lua_mod_mgr->state_array = CALLOC(struct lua_state *, lua_mod_mgr->state_num);
memset(lua_mod_mgr->state_array, 0, sizeof(struct lua_state *) * lua_mod_mgr->state_num);
int bind_ret = SUCCESS;
for (size_t state_index = 0; state_index < lua_mod_mgr->state_num; ++state_index)
{
lua_mod_mgr->state_array[state_index] = lua_state_new(lua_mod_mgr);
if (!lua_mod_mgr->state_array[state_index])
return LMM_STATE_CREATE_ERR;
bind_ret = lua_state_cbinding_function(lua_mod_mgr->state_array[state_index], bind_func_array, bind_func_num);
if (bind_ret)
return bind_ret;
bind_ret = lua_state_cbinding_data(lua_mod_mgr->state_array[state_index], bind_data_array, bind_data_num);
if (bind_ret)
return bind_ret;
}
return SUCCESS;
}
int lua_module_manager_load_config(struct lua_module_manager *lua_mod_mgr, const char *conf_file_path)
{
if (!lua_mod_mgr || !conf_file_path)
return PARAM_ERR;
lua_mod_mgr->load_script_num = 0;
if (access(conf_file_path, F_OK))
return LMM_LOAD_CONFIG_FILE_NOT_EXIST;
FILE *fp = fopen(conf_file_path, "r");
if (!fp)
return LMM_LOAD_CONFIG_FILE_OPEN_ERR;
char errbuff[256] = {0};
toml_table_t *toml_table = toml_parse_file(fp, errbuff, sizeof(errbuff));
if (fp)
fclose(fp);
if (!toml_table)
return LMM_LOAD_CONFIG_FILE_TOML_PARSE_ERR;
toml_array_t *module_array = toml_array_in(toml_table, "module");
if (!module_array)
{
/* 没有module配置 */
toml_free(toml_table);
return SUCCESS;
}
lua_mod_mgr->load_script_num = (size_t)toml_array_nelem(module_array);
lua_mod_mgr->load_script_array = CALLOC(struct lua_load_script, lua_mod_mgr->load_script_num);
memset(lua_mod_mgr->load_script_array, 0, (sizeof(struct lua_load_script) * lua_mod_mgr->load_script_num));
for (size_t index = 0; index < lua_mod_mgr->load_script_num; ++index)
{
toml_table_t *plugin = toml_table_at(module_array, index);
const char *raw_filepath = toml_raw_in(plugin, "path");
const char *raw_load_func_name = toml_raw_in(plugin, "init");
const char *raw_unload_func_name = toml_raw_in(plugin, "exit");
/* TODO:是不是需要进行一下配置检查 */
lua_mod_mgr->load_script_array[index].file_path = strdup(raw_filepath);
lua_mod_mgr->load_script_array[index].init_func_name = strdup(raw_load_func_name);
lua_mod_mgr->load_script_array[index].exit_func_name = strdup(raw_unload_func_name);
}
toml_free(toml_table);
return SUCCESS;
}
int lua_module_manager_call_init(struct lua_module_manager *lua_mod_mgr)
{
if (!lua_mod_mgr)
return PARAM_ERR;
if (lua_mod_mgr->state_num == 0)
return SUCCESS;
lua_State *L = (lua_State *)(lua_mod_mgr->state_array[0]);
int execute_ret = SUCCESS;
for (size_t script_index = 0; script_index < lua_mod_mgr->load_script_num; ++script_index)
{
if (luaL_loadfile(L, lua_mod_mgr->load_script_array[script_index].file_path) || lua_pcall(L, 0, 0, 0))
{
lua_settop(L, 0);
return LMM_LOAD_SCRIPT_FILE_OPEN_ERR;
}
lua_getglobal(L, lua_mod_mgr->load_script_array[script_index].init_func_name);
lua_getglobal(L, lua_mod_mgr->load_script_array[script_index].exit_func_name);
if (lua_type(L, -1) != LUA_TFUNCTION || lua_type(L, -2) != LUA_TFUNCTION)
{
lua_settop(L, 0);
return LMM_LOAD_SCRIPT_FUNC_TYPE_ERR;
}
lua_mod_mgr->load_script_array[script_index].lua_exit_fn_ref_id = luaL_ref(L, LUA_REGISTRYINDEX);
lua_mod_mgr->load_script_array[script_index].lua_init_fn_ref_id = luaL_ref(L, LUA_REGISTRYINDEX);
lua_newtable(L);
lua_mod_mgr->load_script_array[script_index].lua_script_env_ref_id = luaL_ref(L, LUA_REGISTRYINDEX);
execute_ret = lua_state_execute_chunk(lua_mod_mgr->state_array[0], lua_mod_mgr->load_script_array[script_index].lua_init_fn_ref_id, NULL, 0, NULL, 0, NULL, 0);
if (execute_ret)
return execute_ret;
}
return SUCCESS;
}
int lua_module_manager_duplicate_state(struct lua_module_manager *lua_mod_mgr)
{
if (!lua_mod_mgr)
return PARAM_ERR;
if (lua_mod_mgr->state_num <= 1)
return SUCCESS;
int copy_ret = SUCCESS;
for (size_t thread_index = 1; thread_index < lua_mod_mgr->state_num; ++thread_index)
{
copy_ret = lua_state_copy_ref_value(lua_mod_mgr->state_array[thread_index], lua_mod_mgr->state_array[0]);
if (copy_ret)
return copy_ret;
}
return SUCCESS;
}
void lua_module_manger_pair_list_insert(struct lua_module_manager *lua_mod_mgr, struct lua_fn_arg_pair *pair)
{
if (!lua_mod_mgr || !pair)
return;
LL_APPEND(lua_mod_mgr->pair_list, pair);
return;
}
int lua_module_manager_call_exit(struct lua_module_manager *lua_mod_mgr)
{
if (!lua_mod_mgr)
return PARAM_ERR;
if (lua_mod_mgr->state_num == 0)
return SUCCESS;
int execute_ret = SUCCESS;
for (size_t script_index = 0; script_index < lua_mod_mgr->load_script_num; ++script_index)
{
execute_ret = lua_state_execute_chunk(lua_mod_mgr->state_array[0], lua_mod_mgr->load_script_array[script_index].lua_exit_fn_ref_id, NULL, 0, NULL, 0, NULL, 0);
if (execute_ret)
return execute_ret;
}
return SUCCESS;
}
struct stellar_module_manager *lua_module_manager_get_stellar_module_manager(struct lua_module_manager *lua_mod_mgr)
{
if (lua_mod_mgr)
return lua_mod_mgr->mod_mgr;
return NULL;
}
struct mq_schema *lua_module_manager_get_mq_schema(struct lua_module_manager *lua_mod_mgr)
{
if (lua_mod_mgr)
return stellar_module_manager_get_mq_schema(lua_mod_mgr->mod_mgr);
return NULL;
}
struct mq_runtime *lua_module_manager_get_mq_runtime(struct lua_module_manager *lua_mod_mgr)
{
if (lua_mod_mgr)
return stellar_module_manager_get_mq_runtime(lua_mod_mgr->mod_mgr);
return NULL;
}
struct logger *lua_module_manager_get_logger(struct lua_module_manager *lua_mod_mgr)
{
if (lua_mod_mgr)
return stellar_module_manager_get_logger(lua_mod_mgr->mod_mgr);
return NULL;
}
struct packet_manager *lua_module_manager_get_packet_manager(struct lua_module_manager *lua_mod_mgr)
{
if (lua_mod_mgr)
{
struct stellar_module *pkt_module = stellar_module_manager_get_module(lua_mod_mgr->mod_mgr, PACKET_MANAGER_MODULE_NAME);
return (struct packet_manager *)stellar_module_get_ctx(pkt_module);
}
return NULL;
}
struct session_manager *lua_module_manager_get_session_manager(struct lua_module_manager *lua_mod_mgr)
{
if (lua_mod_mgr)
{
struct stellar_module *sess_module = stellar_module_manager_get_module(lua_mod_mgr->mod_mgr, SESSION_MANAGER_MODULE_NAME);
return (struct session_manager *)stellar_module_get_ctx(sess_module);
}
return NULL;
}
int lua_module_manager_get_current_thread_id(struct lua_module_manager *lua_mod_mgr)
{
if (lua_mod_mgr)
return stellar_module_manager_get_thread_id(lua_mod_mgr->mod_mgr);
return -1;
}
struct lua_state *lua_module_manager_get_current_thread_state(struct lua_module_manager *lua_mod_mgr)
{
if (!lua_mod_mgr)
return NULL;
int thread_id = stellar_module_manager_get_thread_id(lua_mod_mgr->mod_mgr);
if (thread_id < 0)
return NULL;
return lua_mod_mgr->state_array[thread_id];
}
int lua_module_manager_get_max_thread_num(struct lua_module_manager *lua_mod_mgr)
{
if (lua_mod_mgr)
return stellar_module_manager_get_max_thread_num(lua_mod_mgr->mod_mgr);
return -1;
}
|