OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 // Flags: --harmony-function-tostring |
| 6 |
| 7 var prefix = "/*before*/"; |
| 8 var suffix = "/*after*/"; |
| 9 |
| 10 function checkStringRepresentation(f, source) { |
| 11 assertEquals(typeof f, "function"); |
| 12 var normalizedSource = source.replace(/\r\n/g, "\n").replace(/\r/g, "\n") |
| 13 assertEquals(f.toString(), normalizedSource); |
| 14 } |
| 15 |
| 16 function testDeclaration(source) { |
| 17 // this eval should define a local variable f that is a function |
| 18 eval(prefix + source + suffix); |
| 19 checkStringRepresentation(f, source); |
| 20 } |
| 21 testDeclaration( "function f(){}"); |
| 22 testDeclaration( "function*f(){}"); |
| 23 testDeclaration("async function f(){}"); |
| 24 testDeclaration( "function/*A*/ f/*B*/(/*C*/a/*D*/,/*E*/b/*G*/)/*H*/{/*
I*/}"); |
| 25 testDeclaration( "function/*A*/*f/*B*/(/*C*/a/*D*/,/*E*/b/*G*/)/*H*/{/*
I*/}"); |
| 26 testDeclaration("async/*Z*/function/*A*/ f/*B*/(/*C*/a/*D*/,/*E*/b/*G*/)/*H*/{/*
I*/}"); |
| 27 testDeclaration( "function \t f \n ( \r a \r\n,\n\r b ) {'\u26
54'}"); |
| 28 testDeclaration( "function \t *f \n ( \r a \r\n,\n\r b ) {
}"); |
| 29 testDeclaration( "function *\t f \n ( \r a \r\n,\n\r b ) {
}"); |
| 30 testDeclaration("async \t function f \n ( \r a \r\n,\n\r b ) {
}"); |
| 31 |
| 32 function testExpression(source) { |
| 33 // this eval should return a function |
| 34 var f = eval("(" + prefix + source + suffix + ")"); |
| 35 checkStringRepresentation(f, source); |
| 36 } |
| 37 testExpression( "function (){}"); |
| 38 testExpression( "function f(){}"); |
| 39 testExpression( "function* (){}"); |
| 40 testExpression( "function*f(){}"); |
| 41 testExpression("async function (){}"); |
| 42 testExpression("async function f(){}"); |
| 43 testExpression( "function/*A*/ /*B*/(/*C*/a/*D*/,/*E*/b/*G*/)/*H*/{/*I
*/}"); |
| 44 testExpression( "function/*A*/ f/*B*/(/*C*/a/*D*/,/*E*/b/*G*/)/*H*/{/*I
*/}"); |
| 45 testExpression( "function/*A*/* /*B*/(/*C*/a/*D*/,/*E*/b/*G*/)/*H*/{/*I
*/}"); |
| 46 testExpression( "function/*A*/*f/*B*/(/*C*/a/*D*/,/*E*/b/*G*/)/*H*/{/*I
*/}"); |
| 47 testExpression("async/*Z*/function/*A*/ f/*B*/(/*C*/a/*D*/,/*E*/b/*G*/)/*H*/{/*I
*/}"); |
| 48 testExpression( "function \t \n ( \r a \r\n,\n\r b ) {
}"); |
| 49 testExpression( "function \t f \n ( \r a \r\n,\n\r b ) {
}"); |
| 50 testExpression( "function \t * \n ( \r a \r\n,\n\r b ) {
}"); |
| 51 testExpression( "function \t *f \n ( \r a \r\n,\n\r b ) {
}"); |
| 52 testExpression( "function *\t \n ( \r a \r\n,\n\r b ) {
}"); |
| 53 testExpression( "function *\t f \n ( \r a \r\n,\n\r b ) {
}"); |
| 54 testExpression("async \t function \n ( \r a \r\n,\n\r b ) {
}"); |
| 55 |
| 56 testExpression( "(/*A*/ /*B*/ /*C*/ /*D*/ /*E*/ /*F*/)/*G*/=>/*H*/0"); |
| 57 testExpression( "a/*B*/ /*C*/ /*D*/ /*E*/ /*F*/ /*G*/=>/*H*/{}"); |
| 58 testExpression( "(/*A*/a/*B*/ /*C*/ /*D*/ /*E*/ /*F*/)/*G*/=>/*H*/0"); |
| 59 testExpression( "(/*A*/a/*B*/,/*C*/b/*D*/,/*E*/c/*F*/)/*G*/=>/*H*/{}"); |
| 60 testExpression("async (/*A*/ /*B*/ /*C*/ /*D*/ /*E*/ /*F*/)/*G*/=>/*H*/0"); |
| 61 testExpression("async a/*B*/ /*C*/ /*D*/ /*E*/ /*F*/ /*G*/=>/*H*/{}"); |
| 62 testExpression("async (/*A*/a/*B*/ /*C*/ /*D*/ /*E*/ /*F*/)/*G*/=>/*H*/0"); |
| 63 testExpression("async (/*A*/a/*B*/,/*C*/b/*D*/,/*E*/c/*F*/)/*G*/=>/*H*/{}"); |
| 64 |
| 65 function testSimpleMethod(source) { |
| 66 // the source should define a method f |
| 67 |
| 68 // object method |
| 69 var f = eval("({" + prefix + source + suffix + "}.f)"); |
| 70 checkStringRepresentation(f, source); |
| 71 |
| 72 // nonstatic class method |
| 73 var f = eval("new class{" + prefix + source + suffix + "}().f"); |
| 74 checkStringRepresentation(f, source); |
| 75 |
| 76 // static class method |
| 77 var f = eval("(class{static" + prefix + source + suffix + "}).f"); |
| 78 checkStringRepresentation(f, source); |
| 79 } |
| 80 testSimpleMethod("f(){}"); |
| 81 testSimpleMethod("*f(){}"); |
| 82 testSimpleMethod("async f(){}"); |
| 83 testSimpleMethod("f \t (){}"); |
| 84 testSimpleMethod("* \tf(){}"); |
| 85 testSimpleMethod("async \t f (){}"); |
| 86 |
| 87 function testAccessorMethod(source, getOrSet) { |
| 88 // the source should define a getter or setter method |
| 89 |
| 90 // object method |
| 91 var f = Object.getOwnPropertyDescriptor(eval("({" + prefix + source + suffix +
"})"), "f")[getOrSet]; |
| 92 checkStringRepresentation(f, source); |
| 93 |
| 94 // nonstatic class method |
| 95 var f = Object.getOwnPropertyDescriptor(eval("(class{" + prefix + source + suf
fix + "})").prototype, "f")[getOrSet]; |
| 96 |
| 97 // static class method |
| 98 var f = Object.getOwnPropertyDescriptor(eval("(class{static" + prefix + source
+ suffix + "})"), "f")[getOrSet]; |
| 99 checkStringRepresentation(f, source); |
| 100 } |
| 101 |
| 102 testAccessorMethod("get f( ){}", "get"); |
| 103 testAccessorMethod("set f(a){}", "set"); |
| 104 testAccessorMethod("get/*A*/f/*B*/(/*C*/ /*D*/)/*E*/{/*F*/}", "get"); |
| 105 testAccessorMethod("set/*A*/f/*B*/(/*C*/a/*D*/)/*E*/{/*F*/}", "set"); |
| 106 |
| 107 const GeneratorFunction = function*(){}.constructor; |
| 108 const AsyncFunction = async function(){}.constructor; |
| 109 function testDynamicFunction(...args) { |
| 110 var P = args.slice(0, args.length - 1).join(","); |
| 111 var bodyText = args.length > 0 ? args[args.length - 1] : ""; |
| 112 var source = " anonymous(" + P + "\n) {" + bodyText + "\n}"; |
| 113 checkStringRepresentation( Function(...args), "function" + sour
ce); |
| 114 checkStringRepresentation(GeneratorFunction(...args), "function*" + sour
ce); |
| 115 checkStringRepresentation( AsyncFunction(...args), "async function" + sour
ce); |
| 116 } |
| 117 testDynamicFunction(); |
| 118 testDynamicFunction(";"); |
| 119 testDynamicFunction("return"); |
| 120 testDynamicFunction("a", "return a"); |
| 121 testDynamicFunction("a", "b", "return a"); |
| 122 testDynamicFunction("a, b", "return a"); |
| 123 testDynamicFunction("a,/*A*/b", "return a"); |
| 124 testDynamicFunction("/*A*/a,b", "return a"); |
| 125 testDynamicFunction("a,b", "return a/*A*/"); |
OLD | NEW |