From 4cd03908bec0b60c4d1a44ab15c165eba105341e Mon Sep 17 00:00:00 2001 From: Benjamin Peterson Date: Mon, 23 Jul 2012 17:03:41 -0700 Subject: [PATCH] Bug 776317 - Separate the body of a constructed function from its braces with newlines. r=luke --- content/events/test/test_bug448602.html | 2 +- content/events/test/test_bug659350.html | 8 ++++---- content/events/test/test_bug689564.html | 16 ++++++++-------- .../tests/basic/function-tosource-constructor.js | 14 +++++++------- js/src/jit-test/tests/basic/testLet.js | 4 ++-- js/src/jsfun.cpp | 4 ++-- 6 files changed, 24 insertions(+), 24 deletions(-) diff --git a/content/events/test/test_bug448602.html b/content/events/test/test_bug448602.html index 793d84aa1045..e14e91bfe0ea 100644 --- a/content/events/test/test_bug448602.html +++ b/content/events/test/test_bug448602.html @@ -40,7 +40,7 @@ function runTests() { root.setAttribute("onclick", listenerSource); infos = els.getListenerInfoFor(root, {}); is(infos.length, 1, "Element should have listeners (1)"); - is(infos[0].toSource(), 'function onclick(event) { ' + listenerSource + ' }', + is(infos[0].toSource(), 'function onclick(event) {\n' + listenerSource + '\n}', "Unexpected serialization (1)"); is(infos[0].type, "click", "Wrong type (1)"); is(infos[0].capturing, false, "Wrong phase (1)"); diff --git a/content/events/test/test_bug659350.html b/content/events/test/test_bug659350.html index ff2fde6c4860..0d566c0b16e2 100644 --- a/content/events/test/test_bug659350.html +++ b/content/events/test/test_bug659350.html @@ -45,10 +45,10 @@ div.onload = f; is(div.onload, f, "Should have 'f' as div's onload"); div.setAttribute("onload", ""); isnot(div.onload, f, "Should not longer have 'f' as div's onload"); -is(div.onload.toString(), "function onload(event) { }", +is(div.onload.toString(), "function onload(event) {\n\n}", "Should have wrapped empty string in a function"); div.setAttribute("onload", "foopy();"); -is(div.onload.toString(), "function onload(event) { foopy(); }", +is(div.onload.toString(), "function onload(event) {\nfoopy();\n}", "Should have wrapped call in a function"); div.removeAttribute("onload"); is(div.onload, null, "Should have null onload now"); @@ -69,11 +69,11 @@ function testPropagationToWindow(eventName) { document.body.setAttribute("on"+eventName, eventName); is(window["on"+eventName].toString(), - "function on"+eventName+"(event) { "+eventName+" }", + "function on"+eventName+"(event) {\n"+eventName+"\n}", "Setting on"+eventName+"attribute on body should propagate to window"); document.createElement("body").setAttribute("on"+eventName, eventName+"2"); is(window["on"+eventName].toString(), - "function on"+eventName+"(event) { "+eventName+"2 }", + "function on"+eventName+"(event) {\n"+eventName+"2\n}", "Setting on"+eventName+"attribute on body outside the document should propagate to window"); } diff --git a/content/events/test/test_bug689564.html b/content/events/test/test_bug689564.html index aa74de420bbe..d5250ad7c7b9 100644 --- a/content/events/test/test_bug689564.html +++ b/content/events/test/test_bug689564.html @@ -21,12 +21,12 @@ https://bugzilla.mozilla.org/show_bug.cgi?id=689564 var div = document.createElement("div"); div.setAttribute("onclick", "div"); is(window.onclick, null, "div should not forward onclick"); -is(div.onclick.toString(), "function onclick(event) { div }", +is(div.onclick.toString(), "function onclick(event) {\ndiv\n}", "div should have an onclick handler"); div.setAttribute("onscroll", "div"); is(window.onscroll, null, "div should not forward onscroll"); -is(div.onscroll.toString(), "function onscroll(event) { div }", +is(div.onscroll.toString(), "function onscroll(event) {\ndiv\n}", "div should have an onscroll handler"); div.setAttribute("onpopstate", "div"); @@ -36,25 +36,25 @@ is(div.onpopstate, null, "div should not have onpopstate handler"); var body = document.createElement("body"); body.setAttribute("onclick", "body"); is(window.onclick, null, "body should not forward onclick"); -is(body.onclick.toString(), "function onclick(event) { body }", +is(body.onclick.toString(), "function onclick(event) {\nbody\n}", "body should have an onclick handler"); body.setAttribute("onscroll", "body"); -is(window.onscroll.toString(), "function onscroll(event) { body }", +is(window.onscroll.toString(), "function onscroll(event) {\nbody\n}", "body should forward onscroll"); body.setAttribute("onpopstate", "body"); -is(window.onpopstate.toString(), "function onpopstate(event) { body }", +is(window.onpopstate.toString(), "function onpopstate(event) {\nbody\n}", "body should forward onpopstate"); var frameset = document.createElement("frameset"); frameset.setAttribute("onclick", "frameset"); is(window.onclick, null, "frameset should not forward onclick"); -is(frameset.onclick.toString(), "function onclick(event) { frameset }", +is(frameset.onclick.toString(), "function onclick(event) {\nframeset\n}", "frameset should have an onclick handler"); frameset.setAttribute("onscroll", "frameset"); -is(window.onscroll.toString(), "function onscroll(event) { frameset }", +is(window.onscroll.toString(), "function onscroll(event) {\nframeset\n}", "frameset should forward onscroll"); frameset.setAttribute("onpopstate", "frameset"); -is(window.onpopstate.toString(), "function onpopstate(event) { frameset }", +is(window.onpopstate.toString(), "function onpopstate(event) {\nframeset\n}", "frameset should forward onpopstate"); diff --git a/js/src/jit-test/tests/basic/function-tosource-constructor.js b/js/src/jit-test/tests/basic/function-tosource-constructor.js index b2d2d5b6b0cb..59e04a8f5aff 100644 --- a/js/src/jit-test/tests/basic/function-tosource-constructor.js +++ b/js/src/jit-test/tests/basic/function-tosource-constructor.js @@ -1,18 +1,18 @@ var f = Function("a", "b", "return a + b;"); -assertEq(f.toString(), "function anonymous(a, b) { return a + b; }"); -assertEq(f.toSource(), "(function anonymous(a, b) { return a + b; })"); +assertEq(f.toString(), "function anonymous(a, b) {\nreturn a + b;\n}"); +assertEq(f.toSource(), "(function anonymous(a, b) {\nreturn a + b;\n})"); assertEq(decompileFunction(f), f.toString()); assertEq(decompileBody(f), "return a + b;"); f = Function("a", "...rest", "return rest[42] + b;"); -assertEq(f.toString(), "function anonymous(a, ...rest) { return rest[42] + b; }"); -assertEq(f.toSource(), "(function anonymous(a, ...rest) { return rest[42] + b; })") +assertEq(f.toString(), "function anonymous(a, ...rest) {\nreturn rest[42] + b;\n}"); +assertEq(f.toSource(), "(function anonymous(a, ...rest) {\nreturn rest[42] + b;\n})") assertEq(decompileFunction(f), f.toString()); assertEq(decompileBody(f), "return rest[42] + b;"); f = Function("x", "return let (y) x;"); -assertEq(f.toSource(), "(function anonymous(x) { return let (y) x; })"); +assertEq(f.toSource(), "(function anonymous(x) {\nreturn let (y) x;\n})"); f = Function(""); -assertEq(f.toString(), "function anonymous() { }"); +assertEq(f.toString(), "function anonymous() {\n\n}"); f = Function("", "(abc)"); -assertEq(f.toString(), "function anonymous() { (abc) }"); +assertEq(f.toString(), "function anonymous() {\n(abc)\n}"); f = Function("", "return function (a, b) a + b;")(); assertEq(f.toString(), "function (a, b) a + b"); diff --git a/js/src/jit-test/tests/basic/testLet.js b/js/src/jit-test/tests/basic/testLet.js index 9e0426cbb41f..17ebcb602b13 100644 --- a/js/src/jit-test/tests/basic/testLet.js +++ b/js/src/jit-test/tests/basic/testLet.js @@ -7,8 +7,8 @@ function test(str, arg, result) var fun = new Function('x', str); - var got = fun.toSource().replace(/\n/g,''); - var expect = '(function anonymous(x) { ' + str + ' })'; + var got = fun.toSource(); + var expect = '(function anonymous(x) {\n' + str + '\n})'; if (got !== expect) { print("GOT: " + got); print("EXPECT: " + expect); diff --git a/js/src/jsfun.cpp b/js/src/jsfun.cpp index 76f1e74e6b35..b1e0bdd33335 100644 --- a/js/src/jsfun.cpp +++ b/js/src/jsfun.cpp @@ -641,7 +641,7 @@ JSFunction::toString(JSContext *cx, bool bodyOnly, bool lambdaParen) return NULL; } } - if (!out.append(") { ")) + if (!out.append(") {\n")) return NULL; } if ((bodyOnly && !funCon) || addUseStrict) { @@ -678,7 +678,7 @@ JSFunction::toString(JSContext *cx, bool bodyOnly, bool lambdaParen) return NULL; } if (buildBody) { - if (!out.append(" }")) + if (!out.append("\n}")) return NULL; } if (bodyOnly) {