aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/dexon-foundation/mcl/ffi/js/export-functions.py
blob: 2a929564bf9e8f57ab05da71079c724f39b733e9 (plain) (blame)
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
import sys, re, argparse

#RE_PROTOTYPE = re.compile(r'MCLBN_DLL_API\s\w\s\w\([^)]*\);')
RE_PROTOTYPE = re.compile(r'\w*\s(\w*)\s(\w*)\(([^)]*)\);')
def export_functions(args, fileNames, reToAddUnderscore):
    modName = args.js
    json = args.json
    if not reToAddUnderscore:
        reToAddUnderscore = r'(mclBn_init|setStr|getStr|[sS]erialize|setLittleEndian|setHashOf|hashAndMapTo|DecStr|HexStr|HashTo|blsSign|blsVerify|GetCurveOrder|GetFieldOrder|KeyShare|KeyRecover|blsSignatureRecover|blsInit)'
    reSpecialFunctionName = re.compile(reToAddUnderscore)
    if json:
        print '['
    elif modName:
        print 'function define_exported_' + modName + '(mod) {'
    comma = ''
    for fileName in fileNames:
        with open(fileName, 'rb') as f:
            for line in f.readlines():
                p = RE_PROTOTYPE.search(line)
                if p:
                    ret = p.group(1)
                    name = p.group(2)
                    arg = p.group(3)
                    if json or modName:
                        retType = 'null' if ret == 'void' else 'number'
                        if arg == '' or arg == 'void':
                            paramNum = 0
                        else:
                            paramNum = len(arg.split(','))
                        if reSpecialFunctionName.search(name):
                            exportName = '_' + name # to wrap function
                        else:
                            exportName = name
                        if json:
                            print comma + '{'
                            if comma == '':
                                comma = ','
                            print '  "name":"{0}",'.format(name)
                            print '  "exportName":"{0}",'.format(exportName)
                            print '  "ret":"{0}",'.format(retType)
                            print '  "args":[',
                            if paramNum > 0:
                                print '"number"' + (', "number"' * (paramNum - 1)),
                            print ']'
                            print '}'
                        else:
                            paramType = '[' + ("'number', " * paramNum) + ']'
                            print "{0} = mod.cwrap('{1}', '{2}', {3})".format(exportName, name, retType, paramType)
                    else:
                        print comma + "'_" + name + "'",
                        if comma == '':
                            comma = ','
    if json:
        print ']'
    elif modName:
        print '}'

def main():
    p = argparse.ArgumentParser('export_functions')
    p.add_argument('header', type=str, nargs='+', help='headers')
    p.add_argument('-js', type=str, nargs='?', help='module name')
    p.add_argument('-re', type=str, nargs='?', help='regular expression file to add underscore to function name')
    p.add_argument('-json', action='store_true', help='output json')
    args = p.parse_args()

    reToAddUnderscore = ''
    if args.re:
        reToAddUnderscore = open(args.re).read().strip()
    export_functions(args, args.header, reToAddUnderscore)

if __name__ == '__main__':
    main()