From 5ff3664fbfebff6fa2f364d3ef7aa51a931d4e31 Mon Sep 17 00:00:00 2001 From: Dan Gohman Date: Tue, 4 Mar 2014 14:57:57 -0800 Subject: Handle name prefixing in sanitizeGlobal/sanitizeLocal. In the case of sanitizeGlobal, "_" was being prefixed in two different places; this factors it out into one place. --- lib/Target/JSBackend/JSBackend.cpp | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/lib/Target/JSBackend/JSBackend.cpp b/lib/Target/JSBackend/JSBackend.cpp index f1a6aa59ee..b2ef5975cc 100644 --- a/lib/Target/JSBackend/JSBackend.cpp +++ b/lib/Target/JSBackend/JSBackend.cpp @@ -431,6 +431,10 @@ static inline char halfCharToHex(unsigned char half) { } static inline void sanitizeGlobal(std::string& str) { + // Global names are prefixed with "_" to prevent them from colliding with + // names of things in normal JS. + str = "_" + str; + // functions and globals should already be in C-style format, // in addition to . for llvm intrinsics and possibly $ and so forth. // There is a risk of collisions here, we just lower all these @@ -444,6 +448,10 @@ static inline void sanitizeGlobal(std::string& str) { } static inline void sanitizeLocal(std::string& str) { + // Local names are prefixed with "$" to prevent them from colliding with + // global names. + str = "$" + str; + // We need to convert every string that is not a valid JS identifier into // a valid one, without collisions - we cannot turn "x.a" into "x_a" while // also leaving "x_a" as is, for example. @@ -568,10 +576,10 @@ const std::string &JSWriter::getJSName(const Value* val) { std::string name; if (val->hasName()) { if (isa(val) || isa(val)) { - name = std::string("_") + val->getName().str(); + name = val->getName().str(); sanitizeGlobal(name); } else { - name = std::string("$") + val->getName().str(); + name = val->getName().str(); sanitizeLocal(name); } } else { @@ -1867,7 +1875,7 @@ void JSWriter::printFunction(const Function *F) { std::string Name = F->getName(); sanitizeGlobal(Name); - Out << "function _" << Name << "("; + Out << "function " << Name << "("; for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end(); AI != AE; ++AI) { if (AI != F->arg_begin()) Out << ","; -- cgit v1.2.3-18-g5258