cross_context_test.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. WScript.LoadScriptFile("..\\UnitTestFramework\\UnitTestFramework.js");
  6. var tests = [
  7. {
  8. name: "Assigning a bound function to the proxy's prototype should not fire the assert",
  9. body: function () {
  10. var pr = new Proxy({}, {
  11. getPrototypeOf: function() {
  12. return;
  13. }
  14. }).__proto__ = Float64Array.bind()
  15. }
  16. },
  17. {
  18. name: "a function and it's bind function are one context and invoked from a different context",
  19. body: function () {
  20. var sc1 = WScript.LoadScript(`
  21. function assertAreEqual(a, b) { if (a != b) { throw new Error('expected : ' + a + ', actual : ' + b) } };
  22. function foo(a, b, c) {
  23. assertAreEqual(undefined, a);
  24. assertAreEqual(1, b.x);
  25. assertAreEqual('three', c);
  26. return {d:10};
  27. };
  28. function test() {
  29. var bf = foo.bind(undefined, undefined, {x:1}, 'three');
  30. assertAreEqual(10, bf().d);
  31. var bf1 = foo.bind(undefined, undefined);
  32. assertAreEqual(10, bf1({x:1}, 'three').d);
  33. }
  34. `,
  35. "samethread");
  36. sc1.test();
  37. }
  38. },
  39. {
  40. name: "a bound function is passed to first context and called from there",
  41. body: function () {
  42. var sc1 = WScript.LoadScript(`
  43. function assertAreEqual(a, b) { if (a != b) { throw new Error('expected : ' + b + ', actual : ' + a) } };
  44. function foo(a, b, c) {
  45. assertAreEqual(undefined, a);
  46. assertAreEqual(1, b.x);
  47. assertAreEqual('three', c);
  48. return {d:10};
  49. };
  50. var bf1 = foo.bind(undefined, undefined, {x:1}, 'three');
  51. var bf2 = foo.bind(undefined, undefined);
  52. function test() {
  53. return foo.bind(undefined, undefined, {x:1}, 'three');
  54. }
  55. function test1() {
  56. return foo.bind(undefined, undefined);
  57. }
  58. `,
  59. "samethread");
  60. assert.areEqual(10, sc1.bf1().d);
  61. assert.areEqual(10, sc1.bf2({x:1}, 'three').d);
  62. assert.areEqual(10, sc1.test()().d);
  63. assert.areEqual(10, sc1.test1()({x:1}, 'three').d);
  64. }
  65. },
  66. {
  67. name: "bound function is created on second context on the function passed from the first context",
  68. body: function () {
  69. function foo(a, b, c) {
  70. assert.areEqual(undefined, a);
  71. assert.areEqual(1, b.x);
  72. assert.areEqual('three', c);
  73. return {d:10};
  74. };
  75. var sc1 = WScript.LoadScript(`
  76. var bf1;
  77. var bf2;
  78. function setup(func) {
  79. bf1 = func.bind(undefined, undefined, {x:1}, 'three');
  80. }
  81. function setup1(func, a) {
  82. bf2 = func.bind(func, a);
  83. }
  84. function test() {
  85. return bf1();
  86. }
  87. function test1(a, b) {
  88. return bf2(a, b);
  89. }
  90. `,
  91. "samethread");
  92. sc1.setup(foo);
  93. sc1.setup1(foo, undefined);
  94. assert.areEqual(10, sc1.test().d);
  95. assert.areEqual(10, sc1.test({x:1}, 'three').d);
  96. }
  97. },
  98. {
  99. name: "bound function is created on second context on the function passed from the first context and invoked directly from first context",
  100. body: function () {
  101. function foo(a, b, c) {
  102. assert.areEqual(undefined, a);
  103. assert.areEqual(1, b.x);
  104. assert.areEqual('three', c);
  105. return {d:10};
  106. };
  107. var sc1 = WScript.LoadScript(`
  108. function test(func) {
  109. return func.bind(undefined, undefined, {x:1}, 'three');
  110. }
  111. function test1(func, a) {
  112. return func.bind(func, a);
  113. }
  114. `,
  115. "samethread");
  116. assert.areEqual(10, sc1.test(foo)().d);
  117. assert.areEqual(10, sc1.test1(foo, undefined)({x:1}, 'three').d);
  118. }
  119. },
  120. {
  121. name: "bound function is created on second context on the function passed from the third context",
  122. body: function () {
  123. var sc1 = WScript.LoadScript(`
  124. function assertAreEqual(a, b) { if (a != b) { throw new Error('expected : ' + b + ', actual : ' + a) } };
  125. function foo(a, b, c) {
  126. assertAreEqual(undefined, a);
  127. assertAreEqual(1, b.x);
  128. assertAreEqual('three', c);
  129. return {d:10};
  130. };
  131. `,
  132. "samethread");
  133. function foo(a, b, c) {
  134. assert.areEqual(undefined, a);
  135. assert.areEqual(1, b.x);
  136. assert.areEqual('three', c);
  137. return {d:10};
  138. };
  139. var sc2 = WScript.LoadScript(`
  140. var bf1, bf2;
  141. function setup(obj, a) {
  142. bf1 = obj.foo.bind(undefined, undefined, {x:1}, 'three');
  143. bf2 = obj.foo.bind(obj.foo, a);
  144. }
  145. function test() {
  146. return bf1();
  147. }
  148. function test1(a, b) {
  149. return bf2(a, b);
  150. }
  151. `,
  152. "samethread");
  153. sc2.setup(sc1, undefined);
  154. assert.areEqual(10, sc2.test().d);
  155. assert.areEqual(10, sc2.test1({x:1}, 'three').d);
  156. }
  157. },
  158. ];
  159. testRunner.runTests(tests, { verbose: WScript.Arguments[0] != "summary" });