Function.js 1.4 KB

1234567891011121314151617181920212223242526272829
  1. function assert(x) { if (!x) throw 1; }
  2. try {
  3. assert(Function.length === 1);
  4. assert(Function.prototype.length === 0);
  5. assert(typeof Function() === "function");
  6. assert(typeof new Function() === "function");
  7. assert(Function()() === undefined);
  8. assert(new Function()() === undefined);
  9. assert(Function("return 42")() === 42);
  10. assert(new Function("return 42")() === 42);
  11. assert(new Function("foo", "return foo")(42) === 42);
  12. assert(new Function("foo,bar", "return foo + bar")(1, 2) === 3);
  13. assert(new Function("foo", "bar", "return foo + bar")(1, 2) === 3);
  14. assert(new Function("foo", "bar,baz", "return foo + bar + baz")(1, 2, 3) === 6);
  15. assert(new Function("foo", "bar", "baz", "return foo + bar + baz")(1, 2, 3) === 6);
  16. assert(new Function("foo", "if (foo) { return 42; } else { return 'bar'; }")(true) === 42);
  17. assert(new Function("foo", "if (foo) { return 42; } else { return 'bar'; }")(false) === "bar");
  18. assert(new Function("return typeof Function()")() === "function");
  19. // FIXME: This is equivalent to
  20. // (function (x) { return function (y) { return x + y;} })(1)(2)
  21. // and should totally work, but both currently fail with
  22. // Uncaught exception: [ReferenceError]: 'x' not known
  23. // assert(new Function("x", "return function (y) { return x + y };")(1)(2) === 3);
  24. console.log("PASS");
  25. } catch (e) {
  26. console.log("FAIL: " + e.message);
  27. }