| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- //-------------------------------------------------------------------------------------------------------
- // Copyright (C) Microsoft. All rights reserved.
- // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
- //-------------------------------------------------------------------------------------------------------
- WScript.LoadScriptFile("..\\UnitTestFramework\\UnitTestFramework.js");
- var tests = [
- {
- name: "Assigning a bound function to the proxy's prototype should not fire the assert",
- body: function () {
- var pr = new Proxy({}, {
- getPrototypeOf: function() {
- return;
- }
- }).__proto__ = Float64Array.bind()
- }
- },
- {
- name: "a function and it's bind function are one context and invoked from a different context",
- body: function () {
- var sc1 = WScript.LoadScript(`
- function assertAreEqual(a, b) { if (a != b) { throw new Error('expected : ' + a + ', actual : ' + b) } };
- function foo(a, b, c) {
- assertAreEqual(undefined, a);
- assertAreEqual(1, b.x);
- assertAreEqual('three', c);
- return {d:10};
- };
-
- function test() {
- var bf = foo.bind(undefined, undefined, {x:1}, 'three');
- assertAreEqual(10, bf().d);
-
- var bf1 = foo.bind(undefined, undefined);
- assertAreEqual(10, bf1({x:1}, 'three').d);
- }
- `,
- "samethread");
-
- sc1.test();
- }
- },
- {
- name: "a bound function is passed to first context and called from there",
- body: function () {
- var sc1 = WScript.LoadScript(`
- function assertAreEqual(a, b) { if (a != b) { throw new Error('expected : ' + b + ', actual : ' + a) } };
- function foo(a, b, c) {
- assertAreEqual(undefined, a);
- assertAreEqual(1, b.x);
- assertAreEqual('three', c);
- return {d:10};
- };
-
- var bf1 = foo.bind(undefined, undefined, {x:1}, 'three');
- var bf2 = foo.bind(undefined, undefined);
-
- function test() {
- return foo.bind(undefined, undefined, {x:1}, 'three');
- }
- function test1() {
- return foo.bind(undefined, undefined);
- }
- `,
- "samethread");
-
- assert.areEqual(10, sc1.bf1().d);
- assert.areEqual(10, sc1.bf2({x:1}, 'three').d);
- assert.areEqual(10, sc1.test()().d);
- assert.areEqual(10, sc1.test1()({x:1}, 'three').d);
- }
- },
- {
- name: "bound function is created on second context on the function passed from the first context",
- body: function () {
- function foo(a, b, c) {
- assert.areEqual(undefined, a);
- assert.areEqual(1, b.x);
- assert.areEqual('three', c);
- return {d:10};
- };
- var sc1 = WScript.LoadScript(`
- var bf1;
- var bf2;
- function setup(func) {
- bf1 = func.bind(undefined, undefined, {x:1}, 'three');
- }
- function setup1(func, a) {
- bf2 = func.bind(func, a);
- }
-
- function test() {
- return bf1();
- }
- function test1(a, b) {
- return bf2(a, b);
- }
- `,
- "samethread");
- sc1.setup(foo);
- sc1.setup1(foo, undefined);
-
- assert.areEqual(10, sc1.test().d);
- assert.areEqual(10, sc1.test({x:1}, 'three').d);
-
- }
- },
- {
- name: "bound function is created on second context on the function passed from the first context and invoked directly from first context",
- body: function () {
- function foo(a, b, c) {
- assert.areEqual(undefined, a);
- assert.areEqual(1, b.x);
- assert.areEqual('three', c);
- return {d:10};
- };
- var sc1 = WScript.LoadScript(`
- function test(func) {
- return func.bind(undefined, undefined, {x:1}, 'three');
- }
- function test1(func, a) {
- return func.bind(func, a);
- }
- `,
- "samethread");
- assert.areEqual(10, sc1.test(foo)().d);
- assert.areEqual(10, sc1.test1(foo, undefined)({x:1}, 'three').d);
-
- }
- },
- {
- name: "bound function is created on second context on the function passed from the third context",
- body: function () {
- var sc1 = WScript.LoadScript(`
- function assertAreEqual(a, b) { if (a != b) { throw new Error('expected : ' + b + ', actual : ' + a) } };
- function foo(a, b, c) {
- assertAreEqual(undefined, a);
- assertAreEqual(1, b.x);
- assertAreEqual('three', c);
- return {d:10};
- };
- `,
- "samethread");
-
- function foo(a, b, c) {
- assert.areEqual(undefined, a);
- assert.areEqual(1, b.x);
- assert.areEqual('three', c);
- return {d:10};
- };
- var sc2 = WScript.LoadScript(`
- var bf1, bf2;
-
- function setup(obj, a) {
- bf1 = obj.foo.bind(undefined, undefined, {x:1}, 'three');
- bf2 = obj.foo.bind(obj.foo, a);
- }
-
- function test() {
- return bf1();
- }
- function test1(a, b) {
- return bf2(a, b);
- }
- `,
- "samethread");
-
- sc2.setup(sc1, undefined);
- assert.areEqual(10, sc2.test().d);
- assert.areEqual(10, sc2.test1({x:1}, 'three').d);
- }
- },
-
- ];
- testRunner.runTests(tests, { verbose: WScript.Arguments[0] != "summary" });
|