1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 10:38:11 +00:00

LibJS: Add Function() and Function.prototype

This commit is contained in:
Linus Groh 2020-04-04 14:34:31 +01:00 committed by Andreas Kling
parent 4d931b524d
commit 2944039d6b
15 changed files with 463 additions and 4 deletions

View file

@ -0,0 +1,29 @@
function assert(x) { if (!x) throw 1; }
try {
assert(Function.length === 1);
assert(Function.prototype.length === 0);
assert(typeof Function() === "function");
assert(typeof new Function() === "function");
assert(Function()() === undefined);
assert(new Function()() === undefined);
assert(Function("return 42")() === 42);
assert(new Function("return 42")() === 42);
assert(new Function("foo", "return foo")(42) === 42);
assert(new Function("foo,bar", "return foo + bar")(1, 2) === 3);
assert(new Function("foo", "bar", "return foo + bar")(1, 2) === 3);
assert(new Function("foo", "bar,baz", "return foo + bar + baz")(1, 2, 3) === 6);
assert(new Function("foo", "bar", "baz", "return foo + bar + baz")(1, 2, 3) === 6);
assert(new Function("foo", "if (foo) { return 42; } else { return 'bar'; }")(true) === 42);
assert(new Function("foo", "if (foo) { return 42; } else { return 'bar'; }")(false) === "bar");
assert(new Function("return typeof Function()")() === "function");
// FIXME: This is equivalent to
// (function (x) { return function (y) { return x + y;} })(1)(2)
// and should totally work, but both currently fail with
// Uncaught exception: [ReferenceError]: 'x' not known
// assert(new Function("x", "return function (y) { return x + y };")(1)(2) === 3);
console.log("PASS");
} catch (e) {
console.log("FAIL: " + e.message);
}

View file

@ -0,0 +1,53 @@
function assert(x) { if (!x) throw 1; }
try {
function Foo(arg) {
this.foo = arg;
}
function Bar(arg) {
this.bar = arg;
}
function FooBar(arg) {
Foo.apply(this, [arg]);
Bar.apply(this, [arg]);
}
function FooBarBaz(arg) {
Foo.apply(this, [arg]);
Bar.apply(this, [arg]);
this.baz = arg;
}
assert(Function.prototype.apply.length === 2);
var foo = new Foo("test");
assert(foo.foo === "test");
assert(foo.bar === undefined);
assert(foo.baz === undefined);
var bar = new Bar("test");
assert(bar.foo === undefined);
assert(bar.bar === "test");
assert(bar.baz === undefined);
var foobar = new FooBar("test");
assert(foobar.foo === "test");
assert(foobar.bar === "test");
assert(foobar.baz === undefined);
var foobarbaz = new FooBarBaz("test");
assert(foobarbaz.foo === "test");
assert(foobarbaz.bar === "test");
assert(foobarbaz.baz === "test");
assert(Math.abs.apply(null, [-1]) === 1);
var add = (x, y) => x + y;
assert(add.apply(null, [1, 2]) === 3);
var multiply = function (x, y) { return x * y; };
assert(multiply.apply(null, [3, 4]) === 12);
console.log("PASS");
} catch (e) {
console.log("FAIL: " + e);
}

View file

@ -0,0 +1,53 @@
function assert(x) { if (!x) throw 1; }
try {
function Foo(arg) {
this.foo = arg;
}
function Bar(arg) {
this.bar = arg;
}
function FooBar(arg) {
Foo.call(this, arg);
Bar.call(this, arg);
}
function FooBarBaz(arg) {
Foo.call(this, arg);
Bar.call(this, arg);
this.baz = arg;
}
assert(Function.prototype.call.length === 1);
var foo = new Foo("test");
assert(foo.foo === "test");
assert(foo.bar === undefined);
assert(foo.baz === undefined);
var bar = new Bar("test");
assert(bar.foo === undefined);
assert(bar.bar === "test");
assert(bar.baz === undefined);
var foobar = new FooBar("test");
assert(foobar.foo === "test");
assert(foobar.bar === "test");
assert(foobar.baz === undefined);
var foobarbaz = new FooBarBaz("test");
assert(foobarbaz.foo === "test");
assert(foobarbaz.bar === "test");
assert(foobarbaz.baz === "test");
assert(Math.abs.call(null, -1) === 1);
var add = (x, y) => x + y;
assert(add.call(null, 1, 2) === 3);
var multiply = function (x, y) { return x * y; };
assert(multiply.call(null, 3, 4) === 12);
console.log("PASS");
} catch (e) {
console.log("FAIL: " + e);
}

View file

@ -0,0 +1,21 @@
function assert(x) { if (!x) throw 1; }
try {
assert((function() {}).toString() === "function () {\n ???\n}");
assert((function(foo) {}).toString() === "function (foo) {\n ???\n}");
assert((function(foo, bar, baz) {}).toString() === "function (foo, bar, baz) {\n ???\n}");
assert((function(foo, bar, baz) {
if (foo) {
return baz;
} else if (bar) {
return foo;
}
return bar + 42;
}).toString() === "function (foo, bar, baz) {\n ???\n}");
assert(console.log.toString() === "function () {\n [NativeFunction]\n}");
assert(Function.toString() === "function () {\n [FunctionConstructor]\n}");
console.log("PASS");
} catch (e) {
console.log("FAIL: " + e);
}