fix encoding and stack overflow

This commit is contained in:
Pavol Bielik 2015-01-25 15:20:50 +01:00
parent d9424d8794
commit b2198226dd
3 changed files with 37 additions and 21 deletions

View File

@ -80,12 +80,12 @@ function processFile(file, print_ast, features, json_formatting, skip_minified)
try { try {
var output = UglifyJS.extractFeatures(code, file, print_ast, features, skip_minified); var output = UglifyJS.extractFeatures(code, file, print_ast, features, skip_minified);
} catch (ex){ } catch (ex){
if (ex instanceof UglifyJS.JS_Parse_Error){ if (ex instanceof UglifyJS.Parse_Error){
sys.error("ERROR: ".red + "cannot parse file '" + file + "'"); sys.error("ERROR: ".red + "cannot parse file '" + file + "': " + ex.message);
} else if (ex instanceof UglifyJS.JS_Minified_Error){ } else if (ex instanceof UglifyJS.Minified_Error){
//sys.error("WARN: ".yellow + "skipping minified file '" + file + "'"); //sys.error("WARN: ".yellow + "skipping minified file '" + file + "'");
} else { } else {
sys.error("ERROR: ".red + "'" + file + "'" + ex); sys.error("ERROR: ".red + "'" + file + "': " + ex);
} }
return; return;

View File

@ -58,14 +58,22 @@ function replaceMangled(code, file) {
return stream.toString(); return stream.toString();
} }
function JS_Minified_Error(message) { function Minified_Error(message) {
this.message = message; this.message = message;
} }
function Parse_Error(ex) {
this.message = ex.toString();
}
function extractFeatures(code, file, print_ast, features, skip_minified) { function extractFeatures(code, file, print_ast, features, skip_minified) {
var toplevel; var toplevel;
toplevel = parseFile(code, file); try {
toplevel = parseFile(code, file);
} catch (ex){
throw new Parse_Error(ex);
}
extendAst(toplevel); extendAst(toplevel);
@ -74,7 +82,7 @@ function extractFeatures(code, file, print_ast, features, skip_minified) {
} }
if (skip_minified && isMinified(toplevel, code, file)){ if (skip_minified && isMinified(toplevel, code, file)){
throw new JS_Minified_Error("Skipping minified file"); throw new Minified_Error("Skipping minified file");
//console.warn("Skipping minified file: '%s'", file); //console.warn("Skipping minified file: '%s'", file);
//return null; //return null;
} }
@ -104,7 +112,7 @@ function extractFeatures(code, file, print_ast, features, skip_minified) {
/* -----[ functions ]----- */ /* -----[ functions ]----- */
function nodeToString(node) { function nodeToString(node, parent) {
if (node == null) return null; if (node == null) return null;
if (node instanceof AST_Symbol){ if (node instanceof AST_Symbol){
@ -120,31 +128,37 @@ function nodeToString(node) {
return GIVEN + "!" + nodeType(node) + "!" + String(node.value).slice(0,64); return GIVEN + "!" + nodeType(node) + "!" + String(node.value).slice(0,64);
} else if (node instanceof AST_Sub){ } else if (node instanceof AST_Sub){
//x[1], x -> expression, 1 -> property //x[1], x -> expression, 1 -> property
return nodeToString(node.expression) + "[]"; return (nodeToString(node.expression, node) != null) ? nodeToString(node.expression, node) + "[]" : null;
} else if (node instanceof AST_PropAccess){ } else if (node instanceof AST_PropAccess){
return GIVEN + node.property; return GIVEN + node.property;
} else if (node instanceof AST_Defun) { } else if (node instanceof AST_Defun) {
//function foo(...) { ... } //function foo(...) { ... }
return nodeToString(node.name); return nodeToString(node.name, node);
} else if (node instanceof AST_VarDef){ } else if (node instanceof AST_VarDef){
// var x = function () { ... } // var x = function () { ... }
return nodeToString(node.name); return nodeToString(node.name, node);
} else if (node instanceof AST_Assign){ } else if (node instanceof AST_Assign){
//x = function () { ... } //x = function () { ... }
return nodeToString(node.left); return nodeToString(node.left, node);
} else if (node instanceof AST_ObjectProperty){ } else if (node instanceof AST_ObjectProperty){
// { "x" : function () { ... } } // { "x" : function () { ... } }
return GIVEN + node.key; return GIVEN + node.key;
} else if (node instanceof AST_Call){ } else if (node instanceof AST_Call){
//x.foo( function () { ... } ) //x.foo( function () { ... } )
//foo( function () { ... } ) //foo( function () { ... } )
return nodeToString(node.expression); return nodeToString(node.expression, node);
} else if (node instanceof AST_Lambda) { } else if (node instanceof AST_Lambda) {
if (node.parent instanceof AST_Call){ if (node.parent instanceof AST_Call){
//'node.parent.expression != node' as lambda can call itself //'node.parent.expression != node' as lambda can call itself
return (node.parent.expression == node) ? null : nodeToString(node.parent.expression) + "(" + node.child_id + ")"; if (node.parent.expression == node) {
return null;
}
return (nodeToString(node.parent.expression, node) != null) ? nodeToString(node.parent.expression, node) + "(" + node.child_id + ")" : null;
}
if (node.parent != parent) {
return nodeToString(node.parent, node);
} }
return nodeToString(node.parent);
} }
return null; return null;
@ -544,7 +558,9 @@ StringMap.prototype.getId = function(input){
/* ------------------------ */ /* ------------------------ */
function escapeString(input){ function escapeString(input){
return encodeURIComponent(input); //escape method escapes certain characters that encodeURIComponent cannot process
//unescape is used to make the resulting string smaller
return unescape(encodeURIComponent(escape(input)));
} }
function parseFile(code, file) { function parseFile(code, file) {

View File

@ -196,7 +196,7 @@ function JS_Parse_Error(message, line, col, pos) {
}; };
JS_Parse_Error.prototype.toString = function() { JS_Parse_Error.prototype.toString = function() {
return this.message + " (line: " + this.line + ", col: " + this.col + ", pos: " + this.pos + ")" + "\n\n" + this.stack; return this.message + " (line: " + this.line + ", col: " + this.col + ", pos: " + this.pos + ")";// + "\n\n" + this.stack;
}; };
function js_error(message, filename, line, col, pos) { function js_error(message, filename, line, col, pos) {