From 04529897dc383357d093473dfc70cfd81310eaf0 Mon Sep 17 00:00:00 2001 From: Tom Robinson Date: Tue, 9 Feb 2010 14:55:50 -0800 Subject: [PATCH] Add setCode to Executable; use toMarkedString() in compiler; more robust function wrapper stripping in compressor --- .../CommonJS/lib/objective-j/compiler.js | 22 +++----- Objective-J/Executable.js | 51 ++++++++++--------- 2 files changed, 34 insertions(+), 39 deletions(-) diff --git a/Objective-J/CommonJS/lib/objective-j/compiler.js b/Objective-J/CommonJS/lib/objective-j/compiler.js index d3fb70657..372adc446 100644 --- a/Objective-J/CommonJS/lib/objective-j/compiler.js +++ b/Objective-J/CommonJS/lib/objective-j/compiler.js @@ -87,26 +87,16 @@ function compileWithResolvedFlags(aFilePath, objjcFlags, gccFlags) throw errorInfo; } - var preprocessed = "@STATIC;1.0;", - fileDependencies = executable.fileDependencies(), - index = 0, - count = fileDependencies.length; - - for (; index < count; ++index) - preprocessed += fileDependencies[index].toMarkedString(); - - var code = executable.code(); - if (shouldCompress) { - var code = compress("function(){" + code + "}", FILE.basename(aFilePath)); - - code = code.substr("function(){".length, code.length - "function(){};\n\n".length); + var code = executable.code(); + code = compress("function(){" + code + "}", FILE.basename(aFilePath)); + // more robust function wrapper stripping + code = code.replace(/^\s*function\s*\(\s*\)\s*{|}\s*;?\s*$/g, ""); + executable.setCode(code); } - preprocessed += "t;" + code.length + ";" + code; - - return preprocessed; + return executable.toMarkedString(); } function resolveFlags(args) diff --git a/Objective-J/Executable.js b/Objective-J/Executable.js index 999cd9613..7902dede8 100644 --- a/Objective-J/Executable.js +++ b/Objective-J/Executable.js @@ -41,29 +41,7 @@ function Executable(/*String*/ aCode, /*Array*/ fileDependencies, /*String*/ aSc if (this._function) return; - var code = this._code; - -#if COMMONJS - code = "function(" + this.functionParameters().join(" , ") + "){" + code + "/**/\n}"; - - if (typeof system !== "undefined" && system.engine === "rhino") - this._function = Packages.org.mozilla.javascript.Context.getCurrentContext().compileFunction(window, code, this._scope, 0, NULL); - else - this._function = eval("(" + code + ")"); -#else - // "//@ sourceURL=" at the end lets us name our eval'd files for debuggers, etc. - // * WebKit: http://pmuellr.blogspot.com/2009/06/debugger-friendly.html - // * Firebug: http://blog.getfirebug.com/2009/08/11/give-your-eval-a-name-with-sourceurl/ - //if (YES) { - code += "/**/\n//@ sourceURL=" + this._scope; - this._function = new Function(this.functionParameters(), code); - //} else { - // // Firebug only does it for "eval()", not "new Function()". Ugh. Slower. - // var functionText = "(function(){"+GET_CODE(aFragment)+"/**/\n})\n//@ sourceURL="+GET_FILE(aFragment).path; - // compiled = eval(functionText); - //} - this._function.displayName = this._scope; -#endif + this.setCode(aCode); } Executable.prototype.path = function() @@ -156,6 +134,33 @@ Executable.prototype.code = function() return this._code; } +Executable.prototype.setCode = function(code) +{ + this._code = code; + +#if COMMONJS + code = "function(" + this.functionParameters().join(" , ") + "){" + code + "/**/\n}"; + + if (typeof system !== "undefined" && system.engine === "rhino") + this._function = Packages.org.mozilla.javascript.Context.getCurrentContext().compileFunction(window, code, this._scope, 0, NULL); + else + this._function = eval("(" + code + ")"); +#else + // "//@ sourceURL=" at the end lets us name our eval'd files for debuggers, etc. + // * WebKit: http://pmuellr.blogspot.com/2009/06/debugger-friendly.html + // * Firebug: http://blog.getfirebug.com/2009/08/11/give-your-eval-a-name-with-sourceurl/ + //if (YES) { + code += "/**/\n//@ sourceURL=" + this._scope; + this._function = new Function(this.functionParameters(), code); + //} else { + // // Firebug only does it for "eval()", not "new Function()". Ugh. Slower. + // var functionText = "(function(){"+GET_CODE(aFragment)+"/**/\n})\n//@ sourceURL="+GET_FILE(aFragment).path; + // compiled = eval(functionText); + //} + this._function.displayName = this._scope; +#endif +} + Executable.prototype.fileDependencies = function() { return this._fileDependencies;