From b91dca87eeccb3d4f4f55daba73c7195f23f691a Mon Sep 17 00:00:00 2001 From: Artem S Vybornov Date: Mon, 16 Dec 2013 21:28:30 +0400 Subject: [PATCH] Asm.js: compliant name mangling --- lib/scope.js | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/lib/scope.js b/lib/scope.js index 6c19c19a..eb9b9723 100644 --- a/lib/scope.js +++ b/lib/scope.js @@ -260,6 +260,14 @@ AST_Scope.DEFMETHOD("def_variable", function(symbol){ AST_Scope.DEFMETHOD("next_mangled", function(options){ var ext = this.enclosed; + + // asm.js: don't mask module-scope names + var asm = this.has_directive("use asm"), + pasm = this.parent_scope ? this.parent_scope.has_directive("use asm") : false; + if (pasm) { + this.cname = this.parent_scope.cname; + } + out: while (true) { var m = base54(++this.cname); if (!is_identifier(m)) continue; // skip over "do" @@ -268,6 +276,21 @@ AST_Scope.DEFMETHOD("next_mangled", function(options){ // shadow a name excepted from mangling. if (options.except.indexOf(m) >= 0) continue; + // asm.js: don't reuse module name although it is out of the current scope + if (asm) { + var sym = this.name; + var penc = this.parent_scope.enclosed; + if (sym && penc.length) { + for (var i = 0; i < penc.length; i++) { + var psym = penc[i]; + if (sym.name == psym.name) { + var name = psym.mangled_name || (psym.unmangleable(options) && psym.name); + if (m == name) continue out; + } + } + } + } + // we must ensure that the mangled name does not shadow a name // from some parent scope that is referenced in this or in // inner scopes.