-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathautomate_bindings.py
executable file
·1378 lines (1258 loc) · 43.3 KB
/
automate_bindings.py
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
#!/usr/bin/python
""" Generate bindings of Wt module(s).
Depends on pygccxml. Install it from PyPI (with pip), not from APT.
"""
import argparse
import glob
import logging
import os
import re
import shutil
import pygccxml
import yaml
GLOBAL_ENUMS_REGISTRY = {}
BUILTIN_TYPES_CONVERTERS = {
'int': ('lua_tointeger', 'lua_pushinteger'),
'bool': ('lua_toboolean', 'lua_pushboolean'),
'double': ('lua_tonumber', 'lua_pushnumber'),
'char const *': ('lua_tostring', 'lua_pushstring'),
}
PROBLEMATIC_FROM_BUILTIN_CONVERSIONS = {
'std::string' : ('std::string', 'char const *'),
'Wt::WString' : ('Wt::WString', 'char const *'),
'Wt::WLink' : ('Wt::WLink', 'char const *'),
'Wt::WLength' : ('Wt::WLength', 'double'),
}
PROBLEMATIC_TO_BUILTIN_CONVERSIONS = {
'std::string' : ('c_str', 'char const *'),
'Wt::WString' : ('toUTF8', 'std::string'),
'Wt::WLink' : ('url', 'std::string'),
'Wt::WLength' : ('value', 'double'),
}
XML_CACHE = 'src/luawt/xml'
INCLUDE_WT = '/usr/include/Wt'
# =============================================================================
# Loading/parsing functions: parse, loadAdditionalChunk.
# Get namespace object by the filename / module name.
def parse(filename, include_paths=None):
# Make sure Wt is installed to correct directory.
wconfigh = '%s/WConfig.h' % INCLUDE_WT
if not os.path.exists(wconfigh):
logging.fatal('Can not find file %s', wconfigh)
raise Exception('Can not find file %s' % wconfigh)
# Find out the c++ parser.
generator_path, generator_name = pygccxml.utils.find_xml_generator()
# Configure the xml generator.
xml_generator_config = pygccxml.parser.xml_generator_configuration_t(
xml_generator_path = generator_path,
xml_generator = generator_name,
include_paths = include_paths,
cflags = '-DWT_USE_BOOST_SIGNALS',
)
file_config = pygccxml.parser.create_cached_source_fc(
filename,
XML_CACHE + '/' + getModuleName(filename),
)
project_reader = pygccxml.parser.project_reader_t(xml_generator_config)
# Parse the c++ file.
decls = project_reader.read_files(
files = [file_config],
)
# Get access to the global namespace.
global_namespace = pygccxml.declarations.get_global_namespace(decls)
return global_namespace
def loadAdditionalChunk(module_str):
file_str = '%s/%s' % (INCLUDE_WT, module_str)
if os.path.isfile(file_str):
return parse(file_str).namespace('Wt')
else:
raise Exception('Unable to load module called %s' % module_str)
# =============================================================================
# Utility functions to transform or extract types/declarations or to get some info about them.
def isTemplate(method_name, decl_obj, namespace):
# Luawt doesn't support C++ templates.
if pygccxml.declarations.templates.is_instantiation(str(decl_obj)):
if not getEnumStr(decl_obj):
# WFlags<enum> is an exception. Treated as enum.
logging.warning(
"Its impossible to bind method %s because luawt " +
"doesn't support C++ templates",
method_name,
)
return True
return False
def isConstReference(checked_type):
if pygccxml.declarations.is_reference(checked_type):
unref = pygccxml.declarations.remove_reference(checked_type)
if pygccxml.declarations.is_const(unref):
return True
return False
def getClassStr(obj):
# Class or other type
class_str = hasattr(obj, 'name') and obj.name or str(obj)
return class_str.replace('Wt::', '')
def getClass(obj, Wt):
class_str = getClassStr(obj)
try:
return Wt.class_(name=class_str)
except:
namespace = loadAdditionalChunk(class_str)
return namespace.class_(name=class_str)
def isDescendantLogic(child_class, base_class_name):
for base in child_class.bases:
if base.related_class.name == base_class_name:
return True
elif isDescendantLogic(base.related_class, base_class_name):
return True
return False
def isDescendantOf(child, base_name, Wt):
try:
child_class = getClass(child, Wt)
except:
warning_str = '''
%(child)s wasn\'t found so there is no guarantee that %(child)s isn't descendant of %(base)s.
'''
warning_options = {
'base': base_name,
'child': getClassStr(child),
}
logging.warning(warning_str.strip() % warning_options)
return False
return isDescendantLogic(child_class, base_name)
def isBaseOrItsDescendant(child, base_name, Wt):
if isDescendantOf(child, base_name, Wt):
return True
elif getClassStr(child) == base_name:
return True
return False
def getInternalNamespace(decl_str):
chunks = decl_str.split('::')
if len(chunks) == 2:
# Decl has internal namespace, e.g. Namespace::Enum.
try:
return loadAdditionalChunk(chunks[0])
except:
return None
return None
def enumObjFromNamespace(enum_str, namespace):
try:
return namespace.enumeration(name=enum_str)
except:
return None
def getEnumObj(enum_str, default_namespace):
chunks = enum_str.split('::')
if len(chunks) == 2:
# Namespace::Enum --> Enum.
enum_str = chunks[1]
enum_obj = enumObjFromNamespace(enum_str, default_namespace)
if not enum_obj:
# Not found: enum isn't declared in the given module.
# Check other options: WGlobal and internal namespace.
glob = loadAdditionalChunk('WGlobal')
internal = getInternalNamespace(enum_str)
# Try WGlobal.
enum_obj = enumObjFromNamespace(enum_str, glob)
if not enum_obj and internal:
# Try internal namespace.
enum_obj = enumObjFromNamespace(enum_str, internal)
if not enum_obj:
raise Exception('Unable to find enum %s' % enum_str)
return enum_obj
def getEnumStr(type_obj):
decl_str = str(type_obj)
if pygccxml.declarations.templates.is_instantiation(decl_str):
name = pygccxml.declarations.templates.name(decl_str)
temp_args = pygccxml.declarations.templates.args(decl_str)
if (len(temp_args) == 1) and (name == 'Wt::WFlags'):
# WFlags<enum> is an exception. Treated as enum.
return temp_args[0]
if pygccxml.declarations.is_enum(type_obj):
return str(clearType(type_obj))
return ''
def getEnumName(full_type):
return getClassStr(getEnumStr(full_type))
def getEnumArrName(enum_name, end):
return 'luawt_enum_' + enum_name.replace('::', '_') + end
def addEnum(type_obj, namespace):
enum_str = getEnumStr(type_obj)
type_str = str(type_obj)
if enum_str:
enum_converters = (
'static_cast<%s>' % enum_str,
'lua_pushstring',
)
BUILTIN_TYPES_CONVERTERS[type_str] = enum_converters
enum_obj = getEnumObj(getClassStr(enum_str), namespace)
GLOBAL_ENUMS_REGISTRY[type_str] = (getEnumName(type_obj), [])
for val in enum_obj.values:
GLOBAL_ENUMS_REGISTRY[type_str][1].append((val[1], val[0]))
GLOBAL_ENUMS_REGISTRY[type_str][1].sort()
def getArgType(arg):
# For compatibility with pygccxml v1.7.1.
arg_field = hasattr(arg, 'decl_type') and arg.decl_type or arg.type
return arg_field
# =============================================================================
# Checker functions: checkArgumentType, checkReturnType and checkWtFunction.
# They do check that current version of luawt supports them.
def checkArgumentType(method_name, arg_type, Wt):
if isTemplate(method_name, arg_type, Wt):
return False
# Is built-in or problematic.
if getBuiltinType(str(arg_type)):
# Is problematic.
if findCorrespondingKeyInDict(
PROBLEMATIC_TO_BUILTIN_CONVERSIONS,
str(arg_type),
):
if pygccxml.declarations.is_reference(arg_type):
if isConstReference(arg_type):
return True
elif not pygccxml.declarations.is_pointer(arg_type):
return True
# Is built-in.
else:
if not pygccxml.declarations.is_pointer(arg_type):
if not pygccxml.declarations.is_reference(arg_type):
return True
elif isBaseOrItsDescendant(clearType(arg_type), 'WWidget', Wt):
if not pygccxml.declarations.is_pointer(arg_type):
logging.info(
'Argument of method %s has strange type %s',
method_name,
arg_type,
)
return True
logging.warning(
'Its impossible to bind method %s: its arg has type %s',
method_name,
arg_type,
)
return False
def checkReturnType(method_name, raw_return_type, Wt):
# Special cases.
if isTemplate(method_name, raw_return_type, Wt):
return False
if str(raw_return_type) == 'void':
return True
# Built-in or problematic return type.
if getBuiltinType(str(raw_return_type)):
if isConstReference(raw_return_type):
return True
elif not pygccxml.declarations.is_pointer(raw_return_type):
return True
elif isBaseOrItsDescendant(clearType(raw_return_type), 'WWidget', Wt):
if pygccxml.declarations.is_pointer(raw_return_type):
return True
elif isConstReference(raw_return_type):
return True
logging.warning(
'Its impossible to bind method %s: of its return type %s',
method_name,
raw_return_type,
)
return False
def checkWtFunction(is_constructor, func, Wt):
if func.access_type != 'public':
return False
for arg in func.arguments:
arg_field = getArgType(arg)
addEnum(arg_field, Wt)
if not checkArgumentType(func.name, arg_field, Wt):
return False
if not is_constructor:
addEnum(func.return_type, Wt)
if not checkReturnType(func.name, func.return_type, Wt):
return False
# OK, all checks've passed.
return True
# =============================================================================
# Getter functions: getSignals, getMembers, getConstructors, getIncludes etc.
def isSignal(func):
if len(func.arguments) != 0:
return False
if func.access_type != 'public':
return False
# Workaround!
if not 'EventSignal<' in str(func.return_type):
return False
if func.virtuality == "pure virtual":
return False
if not pygccxml.declarations.is_reference(func.return_type):
return False
return True
def getSignals(main_class):
signals = main_class.member_functions(
function=isSignal,
allow_empty=True,
).to_list()
for base in main_class.bases:
signals += getSignals(base.related_class)
return signals
def makeSignature(member):
# member.decl_string is a string in form
# ' ( ::Wt::WText::* )( ::Wt::WString const &,::Wt::WContainerWidget * )'
# Target format is in form 'WText(WString,WContainerWidget*)'
args = member.decl_string.split(')(')[1].split(')')[0]
args = args.replace(' const ', ' ')
args = args.replace('::Wt::', ' ')
args = args.replace('&', '')
args = args.replace(' ', '')
return '%s(%s)' % (member.name, args)
def inBlacklist(member, blacklisted):
if blacklisted is None:
# No record for this class in blacklist file.
return False
if 'whole class' in blacklisted:
return True
if member.name in blacklisted.get('methods', []):
return True
if makeSignature(member) in blacklisted.get('signatures', []):
return True
return False
# This function returns methods, signals and base of the given class.
def getMembers(global_namespace, module_name, blacklisted):
Wt = global_namespace.namespace('Wt')
main_class = Wt.class_(name=module_name)
base_r = None
for base in main_class.bases:
if isBaseOrItsDescendant(base.related_class, 'WWidget', Wt):
base_r = base.related_class
if module_name == 'WWidget' or module_name == 'WApplication':
base_r = '0'
if not base_r:
raise Exception('Unable to bind %s, because it isn\'t descendant of WWidget' % module_name)
custom_matcher = pygccxml.declarations.custom_matcher_t(
lambda decl: checkWtFunction(False, decl, Wt),
)
methods = main_class.member_functions(
function=custom_matcher,
recursive=False,
allow_empty=True,
).to_list()
methods = [
method
for method in methods
if not inBlacklist(method, blacklisted)
]
signals = [
signal
for signal in getSignals(main_class)
if not inBlacklist(signal, blacklisted)
]
return methods, signals, base_r
def noParent(args):
for arg in args:
if arg.name == 'parent':
type_s = str(clearType(getArgType(arg)))
if type_s == 'Wt::WContainerWidget':
return False
return True
# For widget tests generation.
# Function returns flag - constructors type:
# - 0 means error - no constructors supported by luawt.test are available;
# - 1 - only no-args constructors are available;
# - 2 means that there're constructors with single WContainerWidget arg.
def getConstructorsType(constructors):
has_void_args = False
has_sing_arg = False
for constructor in constructors:
args = constructor.arguments
req_args = constructor.required_args
if (len(args) == 1) and not noParent(args):
has_sing_arg = True
elif len(req_args) == 0:
has_void_args = True
if has_sing_arg:
return 2
if has_void_args:
return 1
return 0
def getConstructors(global_namespace, module_name, blacklisted):
Wt = global_namespace.namespace('Wt')
main_class = Wt.class_(name=module_name)
if main_class.is_abstract or module_name == 'WApplication':
return [], 0
custom_matcher = pygccxml.declarations.custom_matcher_t(
lambda decl: checkWtFunction(True, decl, Wt),
)
constructors = main_class.constructors(
function=custom_matcher,
recursive=False,
allow_empty=True,
)
result = []
for constructor in constructors:
if not constructor.is_artificial:
if not inBlacklist(constructor, blacklisted):
result.append(constructor)
if not result:
logging.warning('Unable to bind any constructors of %s' % module_name)
return result, getConstructorsType(result)
def isModule(module_str):
path = '%s/%s' % (INCLUDE_WT, module_str)
return os.path.isfile(path)
def addIncludeDir(type_o, list_o):
type_o = clearType(type_o)
type_o = getClassStr(type_o)
if isModule(type_o):
list_o.append(type_o)
def getModulesFromFuncSig(func):
class_strs = []
if hasattr(func, 'return_type'):
addIncludeDir(func.return_type, class_strs)
for arg in func.arguments:
arg_type = getArgType(arg)
addIncludeDir(arg_type, class_strs)
return class_strs
def getIncludes(module_name, methods, constructors):
if module_name == 'MyApplication':
module_name = 'WApplication'
includes = []
includes.append(module_name)
for method in methods:
includes += getModulesFromFuncSig(method)
for constructor in constructors:
includes += getModulesFromFuncSig(constructor)
# Erase repeats.
return set(includes)
def getModuleName(filename):
return os.path.basename(filename)
# =============================================================================
# Generator functions (actual logic of generating the code of bindings).
INCLUDES_TEMPLATE = r'''
#include "boost-xtime.hpp"
%s
#include "enums.hpp"
#include "globals.hpp"
'''
def generateIncludes(includes):
wt_includes = []
for include in includes:
include_str = '#include <Wt/%s>' % include
wt_includes.append(include_str)
return INCLUDES_TEMPLATE.lstrip() % '\n'.join(wt_includes)
def storeArgsIndex(module_name, func_name):
frame = r'''
int index = luawt_getSuitableArgsGroup(L, %s);
'''
arr_str = 'luawt_%s_%s_args' % (module_name, func_name)
return frame.lstrip() % arr_str
def ifIndex(index):
frame = r'''if (index == %d) {
'''
return frame % index
def elseIndex():
return r'''
} else '''
def elseFail(module_name, method_name):
return r'''{
return luaL_error(L, "Wrong arguments for %s.%s");
}''' % (module_name, method_name)
def getSelf(module_name):
frame = r'''%s* self = luawt_checkFromLua<%s>(L, 1);
'''
return frame % (module_name, module_name)
def findCorrespondingKeyInDict(dictionary, full_key):
if full_key in dictionary:
return full_key
for key in dictionary:
if key in full_key:
return key
return ''
def getNumberOfTransitionalTypes(problematic_type):
count = 0
while problematic_type in PROBLEMATIC_FROM_BUILTIN_CONVERSIONS:
problematic_type = PROBLEMATIC_FROM_BUILTIN_CONVERSIONS[problematic_type]
count += 1
return count
def getProblematicFromBuiltin(problematic_type, arg_n, arg_name):
builtin_to_problematic = r'''
%(curr_type)s %(curr_var)s = %(func)s(%(prev_var)s);
'''
convert_str = []
curr_type = problematic_type
n_transitional = getNumberOfTransitionalTypes(problematic_type)
curr_n = 1
while not curr_type in BUILTIN_TYPES_CONVERTERS:
if curr_n == 1:
prev_var = 'raw' + str(arg_n)
else:
prev_var = 'next' + str(n_transitional - curr_n - 1)
if curr_n == n_transitional:
curr_var = arg_name
else:
curr_var = 'next' + str(n_transitional - curr_n)
func, _ = PROBLEMATIC_FROM_BUILTIN_CONVERSIONS[curr_type]
options = {
'curr_type' : curr_type,
'curr_var' : curr_var,
'func' : func,
'prev_var' : prev_var,
}
convert_str.append(builtin_to_problematic % options)
_, curr_type = PROBLEMATIC_FROM_BUILTIN_CONVERSIONS[curr_type]
curr_n += 1
convert_str.reverse()
return ''.join(convert_str)
def getBuiltinTypeArgument(options):
get_problematic_arg_template = r'''
%(raw_type)s raw%(index)s = %(func)s(L, %(index)s);
'''
get_builtin_arg_template = r'''
%(argument_type)s %(argument_name)s = %(func)s(L, %(index)s);
'''
get_enum_arg_template = r'''
%(argument_type)s %(argument_name)s = %(func)s(luawt_getEnum(
L,
%(enum_str_arr)s,
%(enum_val_arr)s,
%(index)s,
"Wrong enum type in args of %(module)s.%(method)s"
));
'''
problematic_type = findCorrespondingKeyInDict(
PROBLEMATIC_TO_BUILTIN_CONVERSIONS,
str(options['argument_type']),
)
if problematic_type:
options['raw_type'] = getBuiltinType(problematic_type)
code = get_problematic_arg_template.lstrip() % options
code += getProblematicFromBuiltin(
problematic_type,
options['index'],
options['argument_name'],
).lstrip()
return code
else:
if 'static_cast' in options['func']:
# Enum.
return get_enum_arg_template.lstrip() % options
else:
return get_builtin_arg_template.lstrip() % options
def clearType(type_o):
type_o = pygccxml.declarations.remove_reference(type_o)
type_o = pygccxml.declarations.remove_pointer(type_o)
type_o = pygccxml.declarations.remove_cv(type_o)
return type_o
def getComplexArgument(options):
options['argument_type'] = clearType(options['argument_type'])
options['argument_type'] = str(options['argument_type'])
frame = r'''
%(argument_type)s* %(argument_name)s =
luawt_checkFromLua<%(argument_type)s>(L, %(index)s);
'''
return frame.lstrip() % options
def getArgsStr(args):
args_list = []
for arg in args:
t = getArgType(arg)
if getBuiltinType(str(t)):
args_list.append(arg.name)
else:
if pygccxml.declarations.is_pointer(t):
args_list.append(arg.name)
else:
args_list.append('*' + arg.name)
return ', '.join(arg_e for arg_e in args_list)
def addWidgetToContainer(module_name):
frame = r'''
MyApplication* app = MyApplication::instance();
if (!app) {
delete l_result;
throw std::logic_error("No WApplication when creating %s");
}
app->root()->addWidget(l_result);
'''
return frame % module_name
def callWtConstructor(args, module_name):
return_type = module_name + ' *'
call_s = 'new %s(' % module_name
args_s = getArgsStr(args)
constructor_s = call_s + args_s + ');'
return '%s l_result = %s' % (return_type, constructor_s)
def callWtFunction(return_type, args, method_name):
call_s = 'self->%s(' % method_name
args_s = getArgsStr(args)
func_s = call_s + args_s + ');'
if return_type == 'void':
return func_s
else:
return '%s l_result = %s' % (return_type, func_s)
RETURN_CALLS_TEMPLATE = r'''
%s(L, %sl_result%s);
return 1;
'''
RETURN_ENUM_TEMPLATE = r'''
luawt_returnEnum(L, %s, %s, l_result, "%s");
return 1;
'''
def getBuiltinTypeFromProblematic(problematic_type):
next_type = problematic_type
convert_f = ''
while not next_type in BUILTIN_TYPES_CONVERTERS:
method_str, next_type = PROBLEMATIC_TO_BUILTIN_CONVERSIONS[next_type]
convert_f += '.' + method_str + '()'
return convert_f
def returnValue(return_type):
void_frame = r'''
return 0;
'''
if return_type == 'void':
return void_frame
else:
ref_str = ''
builtin_type = getBuiltinType(return_type)
# func - function for returning values to Lua.
if builtin_type:
_, func_name = BUILTIN_TYPES_CONVERTERS[builtin_type]
else:
func_name = 'luawt_toLua'
# Is reference.
if '&' in return_type:
ref_str = '&'
# Method to convert problematic type to built-in.
# Empty by default.
convert_f = ''
problematic_type = findCorrespondingKeyInDict(
PROBLEMATIC_FROM_BUILTIN_CONVERSIONS,
return_type,
)
if problematic_type:
convert_f = getBuiltinTypeFromProblematic(problematic_type)
if return_type in GLOBAL_ENUMS_REGISTRY:
# Enum.
enum_name = GLOBAL_ENUMS_REGISTRY[return_type][0]
return RETURN_ENUM_TEMPLATE % (
getEnumArrName(enum_name, '_str'),
getEnumArrName(enum_name, '_val'),
enum_name,
)
else:
return RETURN_CALLS_TEMPLATE % (func_name, ref_str, convert_f)
def getBuiltinType(full_type):
builtin_type = findCorrespondingKeyInDict(
BUILTIN_TYPES_CONVERTERS,
full_type,
)
problematic_type = findCorrespondingKeyInDict(
PROBLEMATIC_TO_BUILTIN_CONVERSIONS,
full_type,
)
if problematic_type:
while not problematic_type in BUILTIN_TYPES_CONVERTERS:
_, problematic_type = PROBLEMATIC_TO_BUILTIN_CONVERSIONS[problematic_type]
return problematic_type
else:
return builtin_type
ARGS_ARRAY_TEMPLATE = r'''
static const char* const* const luawt_%(module)s_%(func)s_args[] = {%(body)s};
'''
def handleEnum(type_o):
detector, _ = BUILTIN_TYPES_CONVERTERS[type_o]
if 'static_cast' in detector:
return 'enum'
return type_o
def generateArgsArray(args_groups, module_name, func_name, is_constructor):
complex_type_frame = 'luawt_typeToStr<%s>()'
args_arrs = ''
body = ''
for i, args in enumerate(args_groups):
name = '%s_%s_args%d' % (module_name, func_name, i)
body += name + ', '
args_arrs += 'static const char* %s[] = {' % name
if not is_constructor:
args_arrs += complex_type_frame % module_name
args_arrs += ', '
for arg in args:
arg_type = getArgType(arg)
builtin_type = getBuiltinType(str(arg_type))
if builtin_type:
args_arrs += '"' + handleEnum(builtin_type) + '"'
else:
complex_type = str(clearType(arg_type))
args_arrs += complex_type_frame % complex_type
args_arrs += ', '
args_arrs += 'NULL};\n'
body += 'NULL'
options = {
'body' : body,
'func' : func_name,
'module' : module_name,
}
return args_arrs + ARGS_ARRAY_TEMPLATE.strip() % options
LUACFUNCTION_TEMPLATE = r'''
%(args_arr)s
int luawt_%(module)s_%(method)s(lua_State* L) {
%(body)s
}
'''
# Add optional arguments.
def makeArgsOverloads(functions):
args_overloads = []
for f in functions:
args_overloads.append(f.required_args)
for opt_arg in f.optional_args:
prev = args_overloads[len(args_overloads) - 1]
args_overloads.append(prev + [opt_arg])
return args_overloads
def implementLuaCFunction(
is_constructor,
module_name,
method_name,
functions_group,
):
args_overloads = makeArgsOverloads(functions_group)
body = []
body.append(storeArgsIndex(module_name, method_name))
# In Lua indices start with 1.
arg_index_offset = 1
if not is_constructor:
# The first one is object itself, so we have to increse offset.
arg_index_offset += 1
body.append(getSelf(module_name))
for index, args in enumerate(args_overloads):
return_type = functions_group[index].return_type
body.append(ifIndex(index))
for i, arg in enumerate(args):
arg_field = getArgType(arg)
options = {
'argument_name' : arg.name,
'argument_type' : arg_field,
'enum_str_arr' : getEnumArrName(getEnumName(arg_field), '_str'),
'enum_val_arr' : getEnumArrName(getEnumName(arg_field), '_val'),
'index' : i + arg_index_offset,
'method' : method_name,
'module' : module_name,
}
arg_type = getBuiltinType(str(arg_field))
if arg_type:
options['func'], _ = BUILTIN_TYPES_CONVERTERS[arg_type]
body.append(getBuiltinTypeArgument(options))
else:
body.append(getComplexArgument(options))
if is_constructor:
body.append(callWtConstructor(args, module_name))
if noParent(args):
body.append(addWidgetToContainer(module_name))
else:
body.append(callWtFunction(str(return_type), args, method_name))
body.append(returnValue(str(return_type)))
body.append(elseIndex())
body.append(elseFail(module_name, method_name))
args_arr = generateArgsArray(
args_overloads,
module_name,
method_name,
is_constructor,
)
return LUACFUNCTION_TEMPLATE.lstrip() % {
'args_arr': args_arr,
'module': module_name,
'method': method_name,
'body': ''.join(body).strip(),
}
METHODS_ARRAY_TEMPLATE = r'''
static const luaL_Reg luawt_%(module_name)s_methods[] = {
%(body)s
};
'''
def generateMethodsArray(module_name, methods):
base_element = r'''
METHOD(%s, %s),
'''
close_element = r'''
{NULL, NULL},
'''
body = []
for method in methods:
body.append(base_element.rstrip() % (module_name, method.name))
body.append(close_element.rstrip())
return METHODS_ARRAY_TEMPLATE % {
'module_name' : module_name,
'body' : ''.join(body).strip(),
}
MODULE_FUNC_TEMPLATE = r'''
void luawt_%(module_name)s(lua_State* L) {
%(get_base_str)s
DECLARE_CLASS(
%(module_name)s,
L,
%(make)s,
0,
luawt_%(module_name)s_methods,
%(base)s
);
}
'''
def generateModuleFunc(module_name, base, is_not_abstract):
base_frame = '''
const char* base = luawt_typeToStr<%s>();
assert(base);
'''
if base == '0':
# WApplication or WWidget.
get_base = ''
else:
get_base = base_frame.strip() % base.name
base = 'base'
if is_not_abstract:
make = 'wrap<luawt_%s_make>::func' % module_name
else:
make = '0'
options = {
'base' : base,
'get_base_str' : get_base,
'make' : make,
'module_name' : module_name,
}
return MODULE_FUNC_TEMPLATE.lstrip() % options
def generateConstructor(module_name, constructors):
if not constructors:
return ''
constructor_name = 'make'
return implementLuaCFunction(
True,
module_name,
constructor_name,
constructors,
)
def generateSignals(signals, module_name):
SIG_TEMPLATE = 'ADD_SIGNAL(%(name)s, %(module)s, %(event)s)\n'
sig_code = []
for signal in signals:
events = pygccxml.declarations.templates.args(str(signal))
if len(events) != 1:
continue
options = {
'event' : events[0],
'module' : module_name,
'name' : signal.name,
}
sig_code.append(SIG_TEMPLATE % options)
return ''.join(sig_code)
# Check if all values of the given enum are bitwise different.
def isSpecialEnum(enum_key):
enum_pairs = GLOBAL_ENUMS_REGISTRY[enum_key][1]
values_sum = 0
pairs_n = 0
for enum_pair in enum_pairs:
enum_val = enum_pair[0]
pairs_n += (enum_val != 0)
if values_sum & enum_val:
return False
values_sum = values_sum | enum_val
if pairs_n <= 1:
return False
return True
ENUM_STRING_ARRAY_TEMPLATE = r'''
static const char* const %s[] = {
%s
NULL
};
'''
ENUM_VALUE_ARRAY_TEMPLATE = r'''
static const lint %s[] = {
%s
};
'''
def generateSpecialEnumsSet(special_enums):
code = ''
body = ''
for special_enum in special_enums:
body += ' ' + '"' + special_enum + '"' + ',\n'
name = 'luawt_SpecialEnums_arr'
end = '%s + %s' % (name, len(special_enums))
code += ENUM_STRING_ARRAY_TEMPLATE % (name, body)
return code
def generateEnumArrays():
code = ''
names = []
# 'Special' enums (with bitwise different values).
special_enums = []
for enum_key in GLOBAL_ENUMS_REGISTRY:
enum_name = GLOBAL_ENUMS_REGISTRY[enum_key][0]
name_str = getEnumArrName(enum_name, '_str')
name_val = getEnumArrName(enum_name, '_val')
if enum_name in names:
continue
else:
names.append(enum_name)
body_str = ''
body_val = ''
for i, val in enumerate(GLOBAL_ENUMS_REGISTRY[enum_key][1]):
body_str += ' ' + '"' + val[1] + '"' + ','
body_val += ' ' + str(val[0])
if i != len(GLOBAL_ENUMS_REGISTRY[enum_key][1]) - 1:
body_str += '\n'
body_val += ',\n'
code += ENUM_STRING_ARRAY_TEMPLATE % (name_str, body_str)
code += ENUM_VALUE_ARRAY_TEMPLATE % (name_val, body_val)
if isSpecialEnum(enum_key):
special_enums.append(enum_name)
code += generateSpecialEnumsSet(special_enums)
return code, names
SET_ENUMS_FUNC_TEMPLATE = r'''
/* Sets all the enums tables, code is generated by script. */
inline void luawt_setEnumsTables(lua_State* L) {
luawt_setEnumsTable(L);
%s
}
'''
def generateSetEnumsCalls(enum_names):
call_template = r'''
CALL_SET_ENUM_TABLE(%s)