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