#!/usr/bin/python ''' Tool that generates namespace boilerplate. Given _mangled_name1_ = MyClass::MyClass() _mangled_name2_ = MyClass::~MyClass() _mangled_name3_ = MyClass::DoSomething(int) the tool will generate MyClass = { 'MyClass': _mangled_name1_, '~MyClass': _mangled_name2_, 'DoSomething': _mangled_name3_ }; You can then do the following in JavaScript: MyClass.MyClass(ptr); MyClass['~MyClass'](ptr); // Need string here, due to |~| MyClass.DoSomething(ptr, 17); Note that you need to send the |this| pointer yourself. TODO: a more OO boilerplate on top of that. ''' import os, sys, json data = open(sys.argv[1], 'r').readlines() space = {} for line in data: line = line.rstrip() realname, signature = line.split(' = ') signature = signature.replace(') const', ')') signature = signature[:-1].split('(') func, params = signature[0], '('.join(signature[1:]) if ' ' in func: i = func.index(' ') ret = func[:i] if '<' not in ret and '[' not in ret and '(' not in ret: func = func[i+1:] funcparts = ['Namespace'] + func.split('::') currspace = space for part in funcparts[:-1]: currspace = currspace.setdefault(part, {}) currspace.setdefault(funcparts[-1], []).append((realname,params)); def clean(currspace): if type(currspace) is list: if len(currspace) == 1: return currspace[0][0] else: ret = {} for item in currspace: i = str(len(ret)/2) ret[i] = item[0] ret[i + '_params'] = item[1] return ret else: for key in currspace.keys(): currspace[key] = clean(currspace[key]) return currspace space = clean(space) def finalize(line): try: key, val = line.split(': ') assert val != '}' and '_params"' not in key return key + ': ' + val.replace('"', '') except: return line print '\n'.join(map(finalize, json.dumps(space, sort_keys=True, indent=2).split('\n')))