-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlua2cpp.rb
More file actions
executable file
·994 lines (910 loc) · 28.4 KB
/
lua2cpp.rb
File metadata and controls
executable file
·994 lines (910 loc) · 28.4 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
#!/usr/bin/env ruby
$decl = <<-INTRO
author: lichuan
qq: 308831759
email: 308831759@qq.com
homepage: www.lichuan.me
github: https://github.com/lichuan/lua2cpp
date: 2013-05-11
desc: this is the generator of binding code between lua and c++
INTRO
def parse_namespace_class(tbl_namespace, tbl_class, full_name)
full_name.prepend(".")
full_name.scan(/\._([^\.]+)/) do |arr|
namespace = arr[0]
tbl_namespace[tbl_namespace.size] = namespace
end
full_name.scan(/\.([^_][^\.]*)/) do |arr|
class_name = arr[0]
tbl_class[tbl_class.size] = class_name
end
end
def is_basic_type(type_str)
["string", "int32", "number", "uint32", "bool"].each { |basic_type| return true if type_str == basic_type }
false
end
def parse_argument(tbl_arg, args)
args.scan(/([^\s,]+)/) do |arr|
arg = arr[0]
idx = tbl_arg.size
tbl_arg[idx] = {}
parse_type(tbl_arg[idx], arg, false)
end
end
def error_msg(str)
puts "Error: #{str}"
exit(1)
end
def parse_function_1(tbl_func, func_str)
match_list = /static (.*)/.match(func_str)
if match_list.nil?
parse_function_2(tbl_func, func_str)
else
tbl_func["is_static"] = true
parse_function_2(tbl_func, match_list[1])
end
end
def parse_type(tbl_type, type_str, is_ret)
return if type_str.empty?
match_list = /([^\s\*\|\&]+)(\*|\&)?(\|gc\|)?/.match(type_str)
if match_list.nil?
error_msg("type invalid: #{type_str}")
else
if match_list[3] == "|gc|"
if not is_ret
error_msg("function parameter should not be |gc|: #{type_str}")
end
end
type = match_list[1]
tbl_type["name"] = type
if is_basic_type type
tbl_type["is_basic"] = true
["*", "|gc|", "&"].each do |v|
error_msg("basic type does not support #{v}: #{type_str}") if match_list[2] == v or match_list[3] == v
end
elsif not $reg_info.has_key?(match_list[1])
error_msg("type #{match_list[1]} is not registered: #{type_str}")
else
if match_list[2] == "*"
tbl_type["is_ptr"] = true
elsif match_list[2] == "&"
tbl_type["is_ref"] = true
end
if match_list[3] == "|gc|"
tbl_type["is_gc"] = true
end
end
end
end
def parse_function_2(tbl_func, func_str)
match_list = /([^\s]*?) *([^\s]+)\((.*)\)/.match(func_str)
tbl_func["ret_type"] = {}
if match_list.nil?
parse_function_3(tbl_func, func_str)
else
func_name = match_list[2]
if not func_name.empty?
tbl_func["name"] = func_name
if not tbl_func.has_key?("export_name")
tbl_func["export_name"] = func_name
end
if not match_list[1].empty?
parse_type(tbl_func["ret_type"], match_list[1], true)
end
parse_argument(tbl_func["arg"], match_list[3])
end
end
end
def parse_function_3(tbl_func, func_str)
match_list = /(^[^\s=]*?) *([^\s=]+)=([^\s]+)/.match(func_str)
if match_list.nil?
match_list = /(^[^\s=]+) +([^\s]+)/.match(func_str)
if match_list.nil?
error_msg("function is invalid: #{func_str}")
else
parse_type(tbl_func["ret_type"], match_list[1], true)
tbl_func["is_get"] = true
tbl_func["name"] = match_list[2]
if not tbl_func.has_key?("export_name")
tbl_func["export_name"] = match_list[2]
end
end
elsif not tbl_func.has_key?("export_name")
error_msg("no export name assign: #{func_str}")
else
tbl_func["is_set"] = true
tbl_func["name"] = match_list[2]
parse_argument(tbl_func["arg"], match_list[3])
parse_type(tbl_func["ret_type"], match_list[1], true)
end
end
def parse_function(tbl_func, func_str)
match_list = /([^\s]+) : (.+)/.match(func_str)
if match_list.nil?
parse_function_1(tbl_func, func_str)
else
tbl_func["export_name"] = match_list[1]
parse_function_1(tbl_func, match_list[2])
end
end
def parse_file
$reg_info = {}
lua_reg_content = File.open("./lua2cpp.txt").readlines().join
match_list = /([^\{]*?)[^\n]*\n\{/.match(lua_reg_content)
$head = match_list[1]
body = lua_reg_content[$head.size..-1]
body.scan(/([^\n]+)\n\{\n(.*?)\n\}/m) do |part1, part2|
full_name = part1.match(/[^\s]*/)[0]
tbl = {}
tbl["namespace"] = {}
tbl["class"] = {}
tbl["name"] = full_name
tbl["super"] = {}
tbl["function"] = {}
error_msg("#{part1} have already registered") if $reg_info.has_key?(full_name)
$reg_info[full_name] = tbl
end
body.scan(/(^[^\s][^\n]*)\n\{\n(.*?)\n\}/m) do |part1, part2|
full_name = part1.match(/[^\s]*/)[0]
tbl = $reg_info[full_name]
parse_namespace_class(tbl["namespace"], tbl["class"], full_name)
if not tbl["class"].empty?
match_list = / extends (.*)/.match(part1)
if not match_list.nil?
super_class_list = match_list[1]
super_class_list.scan(/([^\s,]+)/) do |arr|
super_class = arr[0]
error_msg("#{super_class} is not registered: #{part1}") if not $reg_info.has_key?(super_class)
tbl["super"][tbl["super"].size] = super_class
end
end
end
func_idx = 0
new_idx = 0
part2.scan(/([^\s].+)/) do |arr|
function_line = arr[0]
tbl["function"][func_idx] = {}
tbl["function"][func_idx]["arg"] = {}
match_list = /^\((.*)\)/.match(function_line)
if not match_list.nil?
tbl["function"][func_idx]["is_new"] = true
tbl["function"][func_idx]["export_name"] = "new" if new_idx == 0
tbl["function"][func_idx]["export_name"] = "new#{new_idx}" if new_idx > 0
args = match_list[1]
parse_argument(tbl["function"][func_idx]["arg"], args)
new_idx += 1
else
parse_function(tbl["function"][func_idx], function_line)
end
func_idx += 1
end
end
end
def generate_header()
header = "/*\n"
$decl.each_line do |line|
header += " #{line}"
end
header = header.sub(/this is the generator of binding code between lua and c\+\+/, 'this is the binding code between lua and c++ generated by lua2cpp.rb')
header += "*/
"
#header += $head
header += <<-HEADER
static void get_global_table(lua_State *lua_state, const char *nodes_name)
{
char buf[1024];
strcpy(buf, nodes_name);
char *p = buf;
const char *q = p;
int count = 0;
while(*p != 0)
{
if(*p == '.')
{
*p = 0;
if(count == 0)
{
lua_getglobal(lua_state, q);
if(lua_isnil(lua_state, -1))
{
return;
}
}
else
{
lua_pushstring(lua_state, q);
lua_rawget(lua_state, -2);
if(lua_isnil(lua_state, -1))
{
return;
}
}
q = p + 1;
++count;
}
++p;
}
if(count == 0)
{
lua_getglobal(lua_state, q);
if(lua_isnil(lua_state, -1))
{
return;
}
}
else
{
lua_pushstring(lua_state, q);
lua_rawget(lua_state, -2);
if(lua_isnil(lua_state, -1))
{
return;
}
}
}
static void build_global_table(lua_State *lua_state, const char *nodes_name)
{
char buf[1024];
strcpy(buf, nodes_name);
char *p = buf;
const char *q = p;
int count = 0;
while(*p != 0)
{
if(*p == '.')
{
*p = 0;
if(count == 0)
{
lua_getglobal(lua_state, q);
if(lua_isnil(lua_state, -1))
{
lua_newtable(lua_state);
lua_pushvalue(lua_state, -1);
lua_setglobal(lua_state, q);
}
}
else
{
lua_pushstring(lua_state, q);
lua_rawget(lua_state, -2);
if(lua_isnil(lua_state, -1))
{
lua_pop(lua_state, 1);
lua_pushstring(lua_state, q);
lua_newtable(lua_state);
lua_pushvalue(lua_state, -1);
lua_insert(lua_state, -4);
lua_rawset(lua_state, -3);
lua_pop(lua_state, 1);
}
}
q = p + 1;
++count;
}
++p;
}
if(count == 0)
{
lua_getglobal(lua_state, q);
if(lua_isnil(lua_state, -1))
{
lua_newtable(lua_state);
lua_setglobal(lua_state, q);
}
}
else
{
lua_pushstring(lua_state, q);
lua_rawget(lua_state, -2);
if(lua_isnil(lua_state, -1))
{
lua_pop(lua_state, 1);
lua_pushstring(lua_state, q);
lua_newtable(lua_state);
lua_rawset(lua_state, -3);
}
}
lua_settop(lua_state, 0);
}
HEADER
end
def generate_ns_class_name(ns_tbl, cls_tbl)
gen_str = ""
ns_tbl.each_value do |ns|
gen_str << ns << "::"
end
have_cls = false
cls_tbl.each_value do |cls|
have_cls = true
gen_str << cls << "::"
end
gen_str = gen_str[0..-3] if have_cls
gen_str
end
def generate_ns_class_prefix(ns_tbl, cls_tbl)
gen_str = ""
ns_tbl.each_value do |ns|
gen_str << ns << "::"
end
cls_tbl.each_value do |cls|
gen_str << cls << "::"
end
gen_str
end
def generate_new_function(tbl, func)
ns_cls_name = generate_ns_class_name(tbl["namespace"], tbl["class"])
gen_list = generate_arg_list func, 1
gen_str = gen_list[0]
func_str = gen_list[1]
gen_str += %Q{
uint32 *udata = (uint32*)lua_newuserdata(lua_state, sizeof(uint32) + sizeof(#{ns_cls_name}*));
uint32 &gc_flag = *udata;
gc_flag = 1; /* need gc default in constructor */
udata += 1;
*(#{ns_cls_name}**)udata = new #{ns_cls_name}(#{func_str.join(', ')});
luaL_setmetatable(lua_state, "#{tbl["name"]}");
return 1;
}
gen_str
end
def generate_function(tbl, func)
gen_str = ""
ns_cls_prefix = generate_ns_class_prefix(tbl["namespace"], tbl["class"])
ns_cls_name = generate_ns_class_name(tbl["namespace"], tbl["class"])
if func.has_key? "is_static" or tbl["class"].empty?
gen_list = generate_arg_list func, 1
gen_str += gen_list[0]
func_str = gen_list[1]
if func["ret_type"].empty?
gen_str += "
#{ns_cls_prefix}#{func["name"]}(#{func_str.join(', ')});
return 0;
"
elsif func["ret_type"].has_key? "is_basic"
case func["ret_type"]["name"]
when "bool"
gen_str += "
bool v = #{ns_cls_prefix}#{func["name"]}(#{func_str.join(', ')});
lua_pushboolean(lua_state, v ? 1 : 0);
return 1;
"
when "int32"
gen_str += "
int32 v = #{ns_cls_prefix}#{func["name"]}(#{func_str.join(', ')});
lua_pushinteger(lua_state, v);
return 1;
"
when "uint32"
gen_str += "
uint32 v = #{ns_cls_prefix}#{func["name"]}(#{func_str.join(', ')});
lua_pushunsigned(lua_state, v);
return 1;
"
when "number"
gen_str += "
double v = #{ns_cls_prefix}#{func["name"]}(#{func_str.join(', ')});
lua_pushnumber(lua_state, v);
return 1;
"
when "string"
gen_str += "
std::string v = #{ns_cls_prefix}#{func["name"]}(#{func_str.join(', ')});
lua_pushstring(lua_state, v.c_str());
return 1;
"
else
end
elsif func["ret_type"].has_key? "is_ptr"
ret_type_info = $reg_info[func["ret_type"]["name"]]
ns_cls_name = generate_ns_class_name(ret_type_info["namespace"], ret_type_info["class"])
gen_str += %Q{
const #{ns_cls_name} *v = #{ns_cls_prefix}#{func["name"]}(#{func_str.join(', ')});
uint32 *udata = (uint32*)lua_newuserdata(lua_state, sizeof(uint32) + sizeof(#{ns_cls_name}*));
uint32 &gc_flag = *udata;
gc_flag = #{func["ret_type"].has_key?("is_gc") ? 1 : 0};
udata += 1;
*(#{ns_cls_name}**)udata = (#{ns_cls_name}*)v;
luaL_setmetatable(lua_state, "#{ret_type_info["name"]}");
return 1;
}
elsif func["ret_type"].has_key? "is_ref"
ret_type_info = $reg_info[func["ret_type"]["name"]]
ns_cls_name = generate_ns_class_name(ret_type_info["namespace"], ret_type_info["class"])
gen_str += %Q{
const #{ns_cls_name} *v = &#{ns_cls_prefix}#{func["name"]}(#{func_str.join(', ')});
uint32 *udata = (uint32*)lua_newuserdata(lua_state, sizeof(uint32) + sizeof(#{ns_cls_name}*));
uint32 &gc_flag = *udata;
gc_flag = #{func["ret_type"].has_key?("is_gc") ? 1 : 0};
udata += 1;
*(#{ns_cls_name}**)udata = (#{ns_cls_name}*)v;
luaL_setmetatable(lua_state, "#{ret_type_info["name"]}");
return 1;
}
else
ret_type_info = $reg_info[func["ret_type"]["name"]]
ns_cls_name = generate_ns_class_name(ret_type_info["namespace"], ret_type_info["class"])
gen_str += %Q{
#{ns_cls_name} *v = new #{ns_cls_name};
*v = #{ns_cls_prefix}#{func["name"]}(#{func_str.join(', ')});
uint32 *udata = (uint32*)lua_newuserdata(lua_state, sizeof(uint32) + sizeof(#{ns_cls_name}*));
uint32 &gc_flag = *udata;
gc_flag = 1; /* no ptr, no ref, it's a new obj, so it need gc */
udata += 1;
*(#{ns_cls_name}**)udata = v;
luaL_setmetatable(lua_state, "#{ret_type_info["name"]}");
return 1;
}
end
else #non-static
gen_str += %Q{
uint32 *udata_self = (uint32*)luaL_checkudata(lua_state, 1, "#{tbl["name"]}");
udata_self += 1;
#{ns_cls_name} *obj = *(#{ns_cls_name}**)udata_self;}
gen_list = generate_arg_list func, 2
gen_str += gen_list[0]
func_str = gen_list[1]
if func["ret_type"].empty?
gen_str += "
obj->#{func["name"]}(#{func_str.join(', ')});
return 0;
"
elsif func["ret_type"].has_key? "is_basic"
case func["ret_type"]["name"]
when "bool"
gen_str += "
bool v = obj->#{func["name"]}(#{func_str.join(', ')});
lua_pushboolean(lua_state, v ? 1 : 0);
return 1;
"
when "int32"
gen_str += "
int32 v = obj->#{func["name"]}(#{func_str.join(', ')});
lua_pushinteger(lua_state, v);
return 1;
"
when "uint32"
gen_str += "
uint32 v = obj->#{func["name"]}(#{func_str.join(', ')});
lua_pushunsigned(lua_state, v);
return 1;
"
when "number"
gen_str += "
double v = obj->#{func["name"]}(#{func_str.join(', ')});
lua_pushnumber(lua_state, v);
return 1;
"
when "string"
gen_str += "
std::string v = obj->#{func["name"]}(#{func_str.join(', ')});
lua_pushstring(lua_state, v.c_str());
return 1;
"
else
end
elsif func["ret_type"].has_key? "is_ptr"
ret_type_info = $reg_info[func["ret_type"]["name"]]
ns_cls_name = generate_ns_class_name(ret_type_info["namespace"], ret_type_info["class"])
gen_str += %Q{
const #{ns_cls_name} *v = obj->#{func["name"]}(#{func_str.join(', ')});
uint32 *udata = (uint32*)lua_newuserdata(lua_state, sizeof(uint32) + sizeof(#{ns_cls_name}*));
uint32 &gc_flag = *udata;
gc_flag = #{func["ret_type"].has_key?("is_gc") ? 1 : 0};
udata += 1;
*(#{ns_cls_name}**)udata = (#{ns_cls_name}*)v;
luaL_setmetatable(lua_state, "#{ret_type_info["name"]}");
return 1;
}
elsif func["ret_type"].has_key? "is_ref"
ret_type_info = $reg_info[func["ret_type"]["name"]]
ns_cls_name = generate_ns_class_name(ret_type_info["namespace"], ret_type_info["class"])
gen_str += %Q{
const #{ns_cls_name} *v = &obj->#{func["name"]}(#{func_str.join(', ')});
uint32 *udata = (uint32*)lua_newuserdata(lua_state, sizeof(uint32) + sizeof(#{ns_cls_name}*));
uint32 &gc_flag = *udata;
gc_flag = #{func["ret_type"].has_key?("is_gc") ? 1 : 0};
udata += 1;
*(#{ns_cls_name}**)udata = (#{ns_cls_name}*)v;
luaL_setmetatable(lua_state, "#{ret_type_info["name"]}");
return 1;
}
else
ret_type_info = $reg_info[func["ret_type"]["name"]]
ns_cls_name = generate_ns_class_name(ret_type_info["namespace"], ret_type_info["class"])
gen_str += %Q{
#{ns_cls_name} *v = new #{ns_cls_name};
*v = obj->#{func["name"]}(#{func_str.join(', ')});
uint32 *udata = (uint32*)lua_newuserdata(lua_state, sizeof(uint32) + sizeof(#{ns_cls_name}*));
uint32 &gc_flag = *udata;
gc_flag = 1; /* no ptr, no ref, it's a new obj, so it need gc */
udata += 1;
*(#{ns_cls_name}**)udata = v;
luaL_setmetatable(lua_state, "#{ret_type_info["name"]}");
return 1;
}
end
end
gen_str
end
def generate_get_function(tbl, func)
ns_cls_prefix = generate_ns_class_prefix(tbl["namespace"], tbl["class"])
ns_cls_name = generate_ns_class_name(tbl["namespace"], tbl["class"])
gen_str = ""
if func.has_key? "is_static" or tbl["class"].empty?
if func["ret_type"].has_key? "is_basic"
case func["ret_type"]["name"]
when "bool"
gen_str += "
bool v = #{ns_cls_prefix}#{func["name"]};
lua_pushboolean(lua_state, v ? 1 : 0);
return 1;
"
when "int32"
gen_str += "
int32 v = #{ns_cls_prefix}#{func["name"]};
lua_pushinteger(lua_state, v);
return 1;
"
when "uint32"
gen_str += "
uint32 v = #{ns_cls_prefix}#{func["name"]};
lua_pushunsigned(lua_state, v);
return 1;
"
when "number"
gen_str += "
double v = #{ns_cls_prefix}#{func["name"]};
lua_pushnumber(lua_state, v);
return 1;
"
when "string"
gen_str += "
std::string v = #{ns_cls_prefix}#{func["name"]};
lua_pushstring(lua_state, v.c_str());
return 1;
"
else
end
elsif func["ret_type"].has_key? "is_ptr"
ret_type_info = $reg_info[func["ret_type"]["name"]]
ns_cls_name = generate_ns_class_name(ret_type_info["namespace"], ret_type_info["class"])
gen_str += %Q{
const #{ns_cls_name} *v = #{ns_cls_prefix}#{func["name"]};
uint32 *udata = (uint32*)lua_newuserdata(lua_state, sizeof(uint32) + sizeof(#{ns_cls_name}*));
uint32 &gc_flag = *udata;
gc_flag = #{func["ret_type"].has_key?("is_gc") ? 1 : 0};
udata += 1;
*(#{ns_cls_name}**)udata = (#{ns_cls_name}*)v;
luaL_setmetatable(lua_state, "#{ret_type_info["name"]}");
return 1;
}
elsif func["ret_type"].has_key? "is_ref"
ret_type_info = $reg_info[func["ret_type"]["name"]]
ns_cls_name = generate_ns_class_name(ret_type_info["namespace"], ret_type_info["class"])
gen_str += %Q{
const #{ns_cls_name} *v = &#{ns_cls_prefix}#{func["name"]};
uint32 *udata = (uint32*)lua_newuserdata(lua_state, sizeof(uint32) + sizeof(#{ns_cls_name}*));
uint32 &gc_flag = *udata;
gc_flag = #{func["ret_type"].has_key?("is_gc") ? 1 : 0};
udata += 1;
*(#{ns_cls_name}**)udata = (#{ns_cls_name}*)v;
luaL_setmetatable(lua_state, "#{ret_type_info["name"]}");
return 1;
}
else
ret_type_info = $reg_info[func["ret_type"]["name"]]
ns_cls_name = generate_ns_class_name(ret_type_info["namespace"], ret_type_info["class"])
gen_str += %Q{
#{ns_cls_name} *v = new #{ns_cls_name};
*v = #{ns_cls_prefix}#{func["name"]};
uint32 *udata = (uint32*)lua_newuserdata(lua_state, sizeof(uint32) + sizeof(#{ns_cls_name}*));
uint32 &gc_flag = *udata;
gc_flag = 1; /* no ptr, no ref, it's a new obj, so it need gc */
udata += 1;
*(#{ns_cls_name}**)udata = v;
luaL_setmetatable(lua_state, "#{ret_type_info["name"]}");
return 1;
}
end
else #non-static
gen_str += %Q{
uint32 *udata_self = (uint32*)luaL_checkudata(lua_state, 1, "#{tbl["name"]}");
udata_self += 1;
#{ns_cls_name} *obj = *(#{ns_cls_name}**)udata_self;}
if func["ret_type"].has_key? "is_basic"
case func["ret_type"]["name"]
when "bool"
gen_str += "
bool v = obj->#{func["name"]};
lua_pushboolean(lua_state, v ? 1 : 0);
return 1;
"
when "int32"
gen_str += "
int32 v = obj->#{func["name"]};
lua_pushinteger(lua_state, v);
return 1;
"
when "uint32"
gen_str += "
uint32 v = obj->#{func["name"]};
lua_pushunsigned(lua_state, v);
return 1;
"
when "number"
gen_str += "
double v = obj->#{func["name"]};
lua_pushnumber(lua_state, v);
return 1;
"
when "string"
gen_str += "
std::string v = obj->#{func["name"]};
lua_pushstring(lua_state, v.c_str());
return 1;
"
else
end
elsif func["ret_type"].has_key? "is_ptr"
ret_type_info = $reg_info[func["ret_type"]["name"]]
ns_cls_name = generate_ns_class_name(ret_type_info["namespace"], ret_type_info["class"])
gen_str += %Q{
const #{ns_cls_name} *v = obj->#{func["name"]};
uint32 *udata = (uint32*)lua_newuserdata(lua_state, sizeof(uint32) + sizeof(#{ns_cls_name}*));
uint32 &gc_flag = *udata;
gc_flag = #{func["ret_type"].has_key?("is_gc") ? 1 : 0};
udata += 1;
*(#{ns_cls_name}**)udata = (#{ns_cls_name}*)v;
luaL_setmetatable(lua_state, "#{ret_type_info["name"]}");
return 1;
}
elsif func["ret_type"].has_key? "is_ref"
ret_type_info = $reg_info[func["ret_type"]["name"]]
ns_cls_name = generate_ns_class_name(ret_type_info["namespace"], ret_type_info["class"])
gen_str += %Q{
const #{ns_cls_name} *v = &obj->#{func["name"]};
uint32 *udata = (uint32*)lua_newuserdata(lua_state, sizeof(uint32) + sizeof(#{ns_cls_name}*));
uint32 &gc_flag = *udata;
gc_flag = #{func["ret_type"].has_key?("is_gc") ? 1 : 0};
udata += 1;
*(#{ns_cls_name}**)udata = (#{ns_cls_name}*)v;
luaL_setmetatable(lua_state, "#{ret_type_info["name"]}");
return 1;
}
else
ret_type_info = $reg_info[func["ret_type"]["name"]]
ns_cls_name = generate_ns_class_name(ret_type_info["namespace"], ret_type_info["class"])
gen_str += %Q{
#{ns_cls_name} *v = new #{ns_cls_name};
*v = obj->#{func["name"]};
uint32 *udata = (uint32*)lua_newuserdata(lua_state, sizeof(uint32) + sizeof(#{ns_cls_name}*));
uint32 &gc_flag = *udata;
gc_flag = 1; /* no ptr, no ref, it's a new obj, so it need gc */
udata += 1;
*(#{ns_cls_name}**)udata = v;
luaL_setmetatable(lua_state, "#{ret_type_info["name"]}");
return 1;
}
end
end
gen_str
end
def generate_arg_list(func, start_idx)
gen_str = ""
idx, stack_idx = 1, start_idx
func_str = []
func["arg"].each_value do |arg|
if arg.has_key? "is_basic"
case arg["name"]
when "bool"
gen_str += "
bool arg_#{idx} = luaL_checkint(lua_state, #{stack_idx}) > 0 ? true : false;"
func_str << "arg_#{idx}"
when "int32"
gen_str += "
int32 arg_#{idx} = luaL_checkint(lua_state, #{stack_idx});"
func_str << "arg_#{idx}"
when "number"
gen_str += "
double arg_#{idx} = luaL_checknumber(lua_state, #{stack_idx});"
func_str << "arg_#{idx}"
when "uint32"
gen_str += "
uint32 arg_#{idx} = luaL_checkunsigned(lua_state, #{stack_idx});"
func_str << "arg_#{idx}"
when "string"
gen_str += "
const char *arg_#{idx} = luaL_checkstring(lua_state, #{stack_idx});"
func_str << "arg_#{idx}"
else
end
elsif arg.has_key? "is_ptr"
arg_cls_info = $reg_info[arg["name"]]
arg_cls = generate_ns_class_name(arg_cls_info["namespace"], arg_cls_info["class"])
gen_str += %Q{
uint32 *udata_#{idx} = (uint32*)luaL_checkudata(lua_state, #{stack_idx}, "#{arg_cls_info["name"]}");
udata_#{idx} += 1;
#{arg_cls} *arg_#{idx} = *(#{arg_cls}**)udata_#{idx};}
func_str << "arg_#{idx}"
else #is_ref
arg_cls_info = $reg_info[arg["name"]]
arg_cls = generate_ns_class_name(arg_cls_info["namespace"], arg_cls_info["class"])
gen_str += %Q{
uint32 *udata_#{idx} = (uint32*)luaL_checkudata(lua_state, #{stack_idx}, "#{arg_cls_info["name"]}");
udata_#{idx} += 1;
#{arg_cls} *arg_#{idx} = *(#{arg_cls}**)udata_#{idx};}
func_str << "*arg_#{idx}"
end
idx += 1
stack_idx += 1
end
gen_str += "
lua_settop(lua_state, 0);"
[gen_str, func_str]
end
def generate_set_function(tbl, func)
gen_str = ""
ns_cls_prefix = generate_ns_class_prefix(tbl["namespace"], tbl["class"])
ns_cls_name = generate_ns_class_name(tbl["namespace"], tbl["class"])
if func.has_key? "is_static" or tbl["class"].empty?
gen_list = generate_arg_list func, 1
gen_str += gen_list[0]
func_str = gen_list[1]
gen_str += "
#{ns_cls_prefix}#{func["name"]} = #{func_str.join(', ')};
return 0;
"
else
gen_str += %Q{
uint32 *udata = (uint32*)luaL_checkudata(lua_state, 1, "#{tbl["name"]}");
udata += 1;
#{ns_cls_name} *obj = *(#{ns_cls_name}**)udata;}
gen_list = generate_arg_list func, 2
gen_str += gen_list[0]
func_str = gen_list[1]
gen_str += "
obj->#{func["name"]} = #{func_str.join(', ')};
return 0;
"
end
gen_str
end
def merge_super_function_to_derived()
$reg_info.each_value do |v|
v["super"].each_value do |super_cls|
merge_super_function_to_derived_impl(v["function"], $reg_info[super_cls])
end
end
end
def merge_super_function_to_derived_impl(derived_func_tbl, super_info)
super_info["super"].each_value do |super_cls|
merge_super_function_to_derived_impl(super_info["function"], $reg_info[super_cls])
end
already_exist_functions = []
derived_func_tbl.each_value do |func|
next if func.has_key? "is_static" or func.has_key? "is_new"
already_exist_functions << func["export_name"]
end
super_info["function"].each_value do |func|
next if func.has_key? "is_static" or func.has_key? "is_new"
next if already_exist_functions.include? func["export_name"]
derived_func_tbl[derived_func_tbl.size] = func
end
end
def generate_file()
$reg_func_tbl = {}
gen_str = generate_header
merge_super_function_to_derived
$reg_info.each do |full_name, tbl|
$reg_func_tbl[full_name] = []
tbl["function"].each_value do |func|
gen_str += "
static int "
gen_func_name = "lua"
tbl["namespace"].each_value do |ns|
gen_func_name += "____" + ns
end
tbl["class"].each_value do |cls|
gen_func_name += "___" + cls
end
gen_func_name += "__" + func["export_name"]
$reg_func_tbl[full_name].push([func["export_name"], gen_func_name])
gen_str += gen_func_name + "(lua_State *lua_state)"
gen_str += "
{"
if func.has_key? "is_new"
gen_str += generate_new_function(tbl, func)
elsif func.has_key? "is_get"
gen_str += generate_get_function(tbl, func)
elsif func.has_key? "is_set"
gen_str += generate_set_function(tbl, func)
else
gen_str += generate_function(tbl, func)
end
gen_str += "}
"
end
if not tbl["class"].empty? #generate gc function
gen_str += "
static int "
gen_func_name = "lua"
tbl["namespace"].each_value do |ns|
gen_func_name += "____" + ns
end
tbl["class"].each_value do |cls|
gen_func_name += "___" + cls
end
gen_func_name += "__" + "garbage_colloect"
$reg_func_tbl[full_name].push(["__gc", gen_func_name])
gen_str += gen_func_name + "(lua_State *lua_state)"
gen_str += "
{"
ns_cls_name = generate_ns_class_name(tbl["namespace"], tbl["class"])
gen_str += %Q{
uint32 *udata = (uint32*)luaL_checkudata(lua_state, 1, "#{tbl["name"]}");
uint32 &gc_flag = *udata;
if(gc_flag == 1)
\{
udata += 1;
#{ns_cls_name} *obj = *(#{ns_cls_name}**)udata;
delete obj;
\}
return 0;
\}
}
end
end
gen_str += "
static void register_lua2cpp(lua_State *lua_state)
{"
if $reg_func_tbl.has_key? "_"
func_list = $reg_func_tbl["_"]
gen_str += "
/* register global namespace */"
func_list.each do |export_name, func_name|
gen_str += %Q{
lua_pushcfunction(lua_state, #{func_name});
lua_setglobal(lua_state, "#{export_name}");}
end
gen_str += "
"
end
gen_str += "
/* register non-global namespace */"
$reg_info.each_key do |full_name|
gen_str += %Q{
build_global_table(lua_state, "#{full_name}");} if full_name != "_" #except global namespace
end
gen_str += "
"
$reg_func_tbl.each do |full_name, func_list|
next if full_name == "_"
reg_func_arr_name = full_name.gsub(/\./, "_")
gen_str += %Q{
\{
luaL_Reg #{reg_func_arr_name}[] =
\{}
func_list.each do |export_name, func_name|
gen_str += %Q{
\{"#{export_name}", #{func_name}\},}
end
gen_str += %Q{
\{NULL, NULL\}
\};
lua_settop(lua_state, 0);
luaL_newmetatable(lua_state, "#{full_name}");
luaL_setfuncs(lua_state, #{reg_func_arr_name}, 0);
lua_setfield(lua_state, -1, "__index");
get_global_table(lua_state, "#{full_name}");
luaL_setfuncs(lua_state, #{reg_func_arr_name}, 0);
\}
}
end
gen_str += "}
"
File.open("./lua2cpp.cpp", "w").write(gen_str)
end
#parse and generate
parse_file
generate_file