diff options
author | Daniel Wirtz <dcode@dcode.io> | 2018-02-21 23:22:11 +0100 |
---|---|---|
committer | Alon Zakai <alonzakai@gmail.com> | 2018-02-21 14:22:11 -0800 |
commit | 3f5ee87d262080265b65a3789392b399c91f30ad (patch) | |
tree | 131b31b939cb6f0608e470fcb6d933cb9bb5016c /src/wasm2asm.h | |
parent | 5578bb58402fde2bb2c932bfa08ab71045854a41 (diff) | |
download | binaryen-3f5ee87d262080265b65a3789392b399c91f30ad.tar.gz binaryen-3f5ee87d262080265b65a3789392b399c91f30ad.tar.bz2 binaryen-3f5ee87d262080265b65a3789392b399c91f30ad.zip |
Improve name mangling of asm.js identifiers (#1433)
Also refactors mangling to its own file so it can be reused by generators and consumers, i.e., where it is important to know that an import must be named 'switch_' where it otherwise would be 'switch'.
* Update tests and JS dist files
Diffstat (limited to 'src/wasm2asm.h')
-rw-r--r-- | src/wasm2asm.h | 34 |
1 files changed, 14 insertions, 20 deletions
diff --git a/src/wasm2asm.h b/src/wasm2asm.h index eded082ad..59f2d6ab8 100644 --- a/src/wasm2asm.h +++ b/src/wasm2asm.h @@ -26,6 +26,7 @@ #include <numeric> #include "asmjs/shared-constants.h" +#include "asmjs/asmangle.h" #include "wasm.h" #include "wasm-builder.h" #include "emscripten-optimizer/optimizer.h" @@ -156,27 +157,16 @@ public: frees[type].push_back(temp); } - static IString fromName(Name name) { - // TODO: more clever name fixing, including checking we do not collide - const char* str = name.str; - // check the various issues, and recurse so we check the others - if (strchr(str, '-')) { - char* mod = strdup(str); - str = mod; - while (*mod) { - if (*mod == '-') *mod = '_'; - mod++; - } - IString result = fromName(IString(str, false)); - free((void*)str); - return result; - } - if (isdigit(str[0]) || strcmp(str, "if") == 0) { - std::string prefixed = "$$"; - prefixed += name.str; - return fromName(IString(prefixed.c_str(), false)); + IString fromName(Name name) { + // TODO: checking names do not collide after mangling + auto it = mangledNames.find(name.c_str()); + if (it != mangledNames.end()) { + return it->second; } - return name; + auto mangled = asmangle(std::string(name.c_str())); + IString ret(mangled.c_str(), false); + mangledNames[name.c_str()] = ret; + return ret; } void setStatement(Expression* curr) { @@ -202,6 +192,10 @@ private: // Expressions that will be a statement. std::set<Expression*> willBeStatement; + // Mangled names cache by interned names. + // Utilizes the usually reused underlying cstring's pointer as the key. + std::unordered_map<const char*, IString> mangledNames; + // All our function tables have the same size TODO: optimize? size_t tableSize; |