function-tostring.js 7.62 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
// Copyright 2016 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

var prefix = "/*before*/";
var suffix = "/*after*/";

function checkStringRepresentation(f, source) {
  assertEquals(typeof f, "function");
  assertEquals(source, f.toString());
}

function testDeclaration(source) {
  // this eval should define a local variable f that is a function
  eval(prefix + source + suffix);
  checkStringRepresentation(f, source);
}
testDeclaration(          "function f(){}");
testDeclaration(          "function*f(){}");
testDeclaration("async     function f(){}");
testDeclaration(          "function/*A*/ f/*B*/(/*C*/a/*D*/,/*E*/b/*G*/)/*H*/{/*I*/}");
testDeclaration(          "function/*A*/*f/*B*/(/*C*/a/*D*/,/*E*/b/*G*/)/*H*/{/*I*/}");
testDeclaration("async/*Z*/function/*A*/ f/*B*/(/*C*/a/*D*/,/*E*/b/*G*/)/*H*/{/*I*/}");
testDeclaration(          "function  \t  f  \n ( \r  a \r\n,\n\r b     )  {'\u2654'}");
testDeclaration(          "function  \t *f  \n ( \r  a \r\n,\n\r b     )     {     }");
testDeclaration(          "function *\t  f  \n ( \r  a \r\n,\n\r b     )     {     }");
testDeclaration("async \t  function      f  \n ( \r  a \r\n,\n\r b     )     {     }");

function testExpression(source) {
  // this eval should return a function
  var f = eval("(" + prefix + source + suffix + ")");
  checkStringRepresentation(f, source);
}
testExpression(          "function  (){}");
testExpression(          "function f(){}");
testExpression(          "function* (){}");
testExpression(          "function*f(){}");
testExpression("async     function  (){}");
testExpression("async     function f(){}");
testExpression(          "function/*A*/  /*B*/(/*C*/a/*D*/,/*E*/b/*G*/)/*H*/{/*I*/}");
testExpression(          "function/*A*/ f/*B*/(/*C*/a/*D*/,/*E*/b/*G*/)/*H*/{/*I*/}");
testExpression(          "function/*A*/* /*B*/(/*C*/a/*D*/,/*E*/b/*G*/)/*H*/{/*I*/}");
testExpression(          "function/*A*/*f/*B*/(/*C*/a/*D*/,/*E*/b/*G*/)/*H*/{/*I*/}");
testExpression("async/*Z*/function/*A*/ f/*B*/(/*C*/a/*D*/,/*E*/b/*G*/)/*H*/{/*I*/}");
testExpression(          "function  \t     \n ( \r  a \r\n,\n\r b     )     {     }");
testExpression(          "function  \t  f  \n ( \r  a \r\n,\n\r b     )     {     }");
testExpression(          "function  \t *   \n ( \r  a \r\n,\n\r b     )     {     }");
testExpression(          "function  \t *f  \n ( \r  a \r\n,\n\r b     )     {     }");
testExpression(          "function *\t     \n ( \r  a \r\n,\n\r b     )     {     }");
testExpression(          "function *\t  f  \n ( \r  a \r\n,\n\r b     )     {     }");
testExpression("async \t  function         \n ( \r  a \r\n,\n\r b     )     {     }");

testExpression(      "(/*A*/ /*B*/ /*C*/ /*D*/ /*E*/ /*F*/)/*G*/=>/*H*/0");
testExpression(            "a/*B*/ /*C*/ /*D*/ /*E*/ /*F*/ /*G*/=>/*H*/{}");
testExpression(      "(/*A*/a/*B*/ /*C*/ /*D*/ /*E*/ /*F*/)/*G*/=>/*H*/0");
testExpression(      "(/*A*/a/*B*/,/*C*/b/*D*/,/*E*/c/*F*/)/*G*/=>/*H*/{}");
testExpression("async (/*A*/ /*B*/ /*C*/ /*D*/ /*E*/ /*F*/)/*G*/=>/*H*/0");
testExpression("async       a/*B*/ /*C*/ /*D*/ /*E*/ /*F*/ /*G*/=>/*H*/{}");
testExpression("async (/*A*/a/*B*/ /*C*/ /*D*/ /*E*/ /*F*/)/*G*/=>/*H*/0");
testExpression("async (/*A*/a/*B*/,/*C*/b/*D*/,/*E*/c/*F*/)/*G*/=>/*H*/{}");

function testSimpleMethod(source) {
  // the source should define a method f

  // object method
  var f = eval("({" + prefix + source + suffix + "}.f)");
  checkStringRepresentation(f, source);

  // nonstatic class method
  var f = eval("new class{" + prefix + source + suffix + "}().f");
  checkStringRepresentation(f, source);

  // static class method
  var f = eval("(class{static" + prefix + source + suffix + "}).f");
  checkStringRepresentation(f, source);
}
testSimpleMethod("f(){}");
testSimpleMethod("*f(){}");
testSimpleMethod("async f(){}");
testSimpleMethod("f \t (){}");
testSimpleMethod("* \tf(){}");
testSimpleMethod("async \t f (){}");

function testAccessorMethod(source, getOrSet) {
  // the source should define a getter or setter method

  // object method
  var f = Object.getOwnPropertyDescriptor(eval("({" + prefix + source + suffix + "})"), "f")[getOrSet];
  checkStringRepresentation(f, source);

  // nonstatic class method
  var f = Object.getOwnPropertyDescriptor(eval("(class{" + prefix + source + suffix + "})").prototype, "f")[getOrSet];

  // static class method
  var f = Object.getOwnPropertyDescriptor(eval("(class{static" + prefix + source + suffix + "})"), "f")[getOrSet];
  checkStringRepresentation(f, source);
}

testAccessorMethod("get f( ){}", "get");
testAccessorMethod("set f(a){}", "set");
testAccessorMethod("get/*A*/f/*B*/(/*C*/ /*D*/)/*E*/{/*F*/}", "get");
testAccessorMethod("set/*A*/f/*B*/(/*C*/a/*D*/)/*E*/{/*F*/}", "set");

const GeneratorFunction = function*(){}.constructor;
const AsyncFunction = async function(){}.constructor;
function testDynamicFunction(...args) {
  var P = args.slice(0, args.length - 1).join(",");
  var bodyText = args.length > 0 ? args[args.length - 1] : "";
  var source = " anonymous(" + P + "\n) {\n" + bodyText + "\n}";
  checkStringRepresentation(         Function(...args),       "function"  + source);
  checkStringRepresentation(GeneratorFunction(...args),       "function*" + source);
  checkStringRepresentation(    AsyncFunction(...args), "async function"  + source);
}
testDynamicFunction();
testDynamicFunction(";");
testDynamicFunction("return");
testDynamicFunction("a", "return a");
testDynamicFunction("a", "b", "return a");
testDynamicFunction("a,   b", "return a");
testDynamicFunction("a,/*A*/b", "return a");
testDynamicFunction("/*A*/a,b", "return a");
testDynamicFunction("a,b", "return a/*A*/");
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141

// Proxies of functions should not throw, but return a NativeFunction.
assertEquals("function () { [native code] }",
             new Proxy(function () { hidden }, {}).toString());
assertEquals("function () { [native code] }",
             new Proxy(() => { hidden }, {}).toString());
assertEquals("function () { [native code] }",
             new Proxy(class {}, {}).toString());
assertEquals("function () { [native code] }",
             new Proxy(function() { hidden }.bind({}), {}).toString());
assertEquals("function () { [native code] }",
             new Proxy(function*() { hidden }, {}).toString());
assertEquals("function () { [native code] }",
             new Proxy(async function() { hidden }, {}).toString());
assertEquals("function () { [native code] }",
             new Proxy(async function*() { hidden }, {}).toString());
assertEquals("function () { [native code] }",
             new Proxy({ method() { hidden } }.method, {}).toString());

142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
// Assert that we return a NativeFunction for script that has too large an
// offset between function token position and start position for us to return
// an exact representation of the source code.
function testLongFunctionTokenOffset(functionType) {
  var expected = "function f() { [native code] }";
  // Spec requires that we return something that will cause eval to throws if we
  // can't reproduce the function's source code.
  assertThrows(() => eval(expected), SyntaxError);

  var functionSource = functionType + " ".repeat(65535) + " f(){}";

  // Function declaration
  eval(functionSource);
  assertEquals(expected, f.toString());

  // Function expression
  var f = eval("(" + functionSource + ")");
  assertEquals(expected, f.toString());
}
testLongFunctionTokenOffset("function");
testLongFunctionTokenOffset("function*");
testLongFunctionTokenOffset("async function");
testLongFunctionTokenOffset("async function*");

166 167 168
// Non-callable proxies still throw.
assertThrows(() => Function.prototype.toString.call(new Proxy({}, {})),
             TypeError);