a portable and fast pairing-based cryptography library
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
mcl/ffi/js/export-functions.py

53 lines
1.8 KiB

import sys, re, argparse
7 years ago
#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(modName, fileNames, reToAddUnderscore):
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 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 modName:
retType = 'null' if ret == 'void' else 'number'
if arg == '' or arg == 'void':
paramType = '[]'
else:
paramType = '[' + ("'number', " * len(arg.split(','))) + ']'
if reSpecialFunctionName.search(name):
exportName = '_' + name # to wrap function
else:
exportName = name
print "{0} = mod.cwrap('{1}', '{2}', {3})".format(exportName, name, retType, paramType)
7 years ago
else:
print comma + "'_" + name + "'",
if comma == '':
comma = ','
if modName:
print '}'
7 years ago
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')
args = p.parse_args()
reToAddUnderscore = ''
if args.re:
reToAddUnderscore = open(args.re).read().strip()
export_functions(args.js, args.header, reToAddUnderscore)
7 years ago
if __name__ == '__main__':
main()