Function.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. load("test-common.js");
  2. try {
  3. assert(Function.length === 1);
  4. assert(Function.name === "Function");
  5. assert(Function.prototype.length === 0);
  6. assert(Function.prototype.name === "");
  7. assert(typeof Function() === "function");
  8. assert(typeof new Function() === "function");
  9. assert(Function()() === undefined);
  10. assert(new Function()() === undefined);
  11. assert(Function("return 42")() === 42);
  12. assert(new Function("return 42")() === 42);
  13. assert(new Function("foo", "return foo")(42) === 42);
  14. assert(new Function("foo,bar", "return foo + bar")(1, 2) === 3);
  15. assert(new Function("foo", "bar", "return foo + bar")(1, 2) === 3);
  16. assert(new Function("foo", "bar,baz", "return foo + bar + baz")(1, 2, 3) === 6);
  17. assert(new Function("foo", "bar", "baz", "return foo + bar + baz")(1, 2, 3) === 6);
  18. assert(new Function("foo", "if (foo) { return 42; } else { return 'bar'; }")(true) === 42);
  19. assert(new Function("foo", "if (foo) { return 42; } else { return 'bar'; }")(false) === "bar");
  20. assert(new Function("return typeof Function()")() === "function");
  21. assert(new Function("x", "return function (y) { return x + y };")(1)(2) === 3);
  22. assert(new Function().name === "anonymous");
  23. assert(new Function().toString() === "function anonymous() {\n ???\n}");
  24. assertThrowsError(() => {
  25. new Function("[");
  26. }, {
  27. error: SyntaxError,
  28. // This might be confusing at first but keep in mind it's actually parsing
  29. // function anonymous() { [ }
  30. // This is in line with what other engines are reporting.
  31. message: "Unexpected token CurlyClose. Expected BracketClose (line: 1, column: 26)"
  32. });
  33. console.log("PASS");
  34. } catch (e) {
  35. console.log("FAIL: " + e.message);
  36. }