diff options
-rw-r--r-- | src/preamble.js | 22 | ||||
-rw-r--r-- | tests/test_other.py | 16 |
2 files changed, 38 insertions, 0 deletions
diff --git a/src/preamble.js b/src/preamble.js index 88aaff77..03627760 100644 --- a/src/preamble.js +++ b/src/preamble.js @@ -640,6 +640,28 @@ function stringToUTF32(str, outPtr) { } Module['stringToUTF32'] = stringToUTF32; +function demangle(func) { + if (typeof func === 'number') func = Pointer_stringify(func); + assert(func[0] === '_'); + if (func[1] !== '_') return func.substr(1); // C function + assert(func[2] === 'Z'); + if (func[3] !== 'N') { + // not namespaced + var m = /(\d+)([^\d].*)/.exec(func.substr(3)); + return m[2].substr(0, m[1]); + } + // namespaced N-E + var i = 4, ret = []; + while (func[i] !== 'E') { + var size = parseInt(func.substr(i)); + var pre = size.toString().length; + ret.push(func.substr(i + pre, size)); + i += pre + size; + assert(pre > 0 && size > 0 && i < func.length); + } + return ret.join('::'); +} + // Memory management var PAGE_SIZE = 4096; diff --git a/tests/test_other.py b/tests/test_other.py index 264e75e1..2f31aae0 100644 --- a/tests/test_other.py +++ b/tests/test_other.py @@ -1906,3 +1906,19 @@ done. assert '''tests/hello_world.c"''' in out assert '''printf("hello, world!''' in out + def test_demangle(self): + open('src.cpp', 'w').write(''' + #include <stdio.h> + #include <emscripten.h> + int main() { + EM_ASM(Module.print(demangle('_main'))); + EM_ASM(Module.print(demangle('__Z2f2v'))); + EM_ASM(Module.print(demangle('__Z12abcdabcdabcdi'))); + EM_ASM(Module.print(demangle('__ZN4Waka1f12a234123412345pointEv'))); + return 0; + } + ''') + + Popen([PYTHON, EMCC, 'src.cpp']).communicate() + self.assertContained('main\nf2\nabcdabcdabcd\nWaka::f::a23412341234::point\n', run_js('a.out.js')) + |