CopyOnAccessArray_bugs.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. //Note: see function ArraySpliceHelper of JavascriptArray.cpp
  6. if (this.WScript && this.WScript.LoadScriptFile) { // Check for running in ch
  7. this.WScript.LoadScriptFile("..\\UnitTestFramework\\UnitTestFramework.js");
  8. }
  9. var tests = [
  10. {
  11. name: "Calling Array.prototype.slice()",
  12. body: function ()
  13. {
  14. var a=[1,2,3,4,5];
  15. var b=Array.prototype.slice.call(a,1,3);
  16. assert.areEqual([2,3], b, "Incorrect result from Array.prototype.slice()");
  17. }
  18. },
  19. {
  20. name: "Calling Array.prototype.push()",
  21. body: function ()
  22. {
  23. var a=[1,2];
  24. Array.prototype.push.call(a,1);
  25. assert.areEqual([1,2,1], a, "Incorrect result from Array.prototype.push()");
  26. }
  27. },
  28. {
  29. name: "Calling Array.isArray()",
  30. body: function ()
  31. {
  32. var a=[1,2,3,4,5,6,7];
  33. assert.areEqual(true, Array.isArray(a), "Incorrect result from Array.isArray()");
  34. }
  35. },
  36. {
  37. name: "Calling Array.prototype.unshift()",
  38. body: function ()
  39. {
  40. var a=[2,1,3,4];
  41. Array.prototype.unshift.call(a,0);
  42. assert.areEqual([0,2,1,3,4], a, "Incorrect result from Array.prototype.unshift()");
  43. }
  44. },
  45. {
  46. name: "Calling Array.prototype.shift()",
  47. body: function ()
  48. {
  49. var a=[1,2,3,4];
  50. var c=Array.prototype.shift.call(a);
  51. assert.areEqual([2,3,4], a, "Incorrect result from Array.prototype.shift()");
  52. assert.areEqual(1, c, "Incorrect result from Array.prototype.shift()");
  53. }
  54. },
  55. {
  56. name: "Calling Array.prototype.entries()",
  57. body: function ()
  58. {
  59. var a=[1,2,3,4];
  60. var c=Array.prototype.entries.call(a);
  61. for (var e of c)
  62. {
  63. print(e);
  64. }
  65. }
  66. },
  67. {
  68. name: "Calling Array.prototype.keys()",
  69. body: function ()
  70. {
  71. var a=[1,2,3,4];
  72. var c=Array.prototype.keys.call(a);
  73. for (var e of c)
  74. {
  75. print(e);
  76. }
  77. }
  78. },
  79. {
  80. name: "Calling Array.prototype.reverse()",
  81. body: function ()
  82. {
  83. var a=[1,2,3,4];
  84. Array.prototype.reverse.call(a);
  85. assert.areEqual([4,3,2,1], a, "Incorrect result from Array.prototype.reverse()");
  86. }
  87. },
  88. {
  89. name: "Calling Object.prototype.toString()",
  90. body: function ()
  91. {
  92. var a=[1,2,3,4,5,6];
  93. var c=Object.prototype.toString.call(a);
  94. assert.areEqual("[object Array]", c, "Incorrect result from Object.prototype.toString()");
  95. }
  96. },
  97. {
  98. name: "Calling Object.prototype.hasOwnProperty()",
  99. body: function ()
  100. {
  101. var a=[1,2,3,4,5,6];
  102. var c=Object.prototype.hasOwnProperty.call(a, 1);
  103. assert.areEqual(c, true);
  104. }
  105. },
  106. {
  107. name: "OS3713376: Accessing COA through proxy",
  108. body: function ()
  109. {
  110. var p = new Proxy([0,0,0,0,0], {});
  111. p.length = 1;
  112. assert.areEqual('0', p.toString(), 'Setting length of an array through Proxy');
  113. var q = new Proxy([0,0,0,0,0], {});
  114. q[0] = 1;
  115. assert.areEqual('1,0,0,0,0', q.toString(), 'Setting array element through Proxy');
  116. }
  117. },
  118. {
  119. name: "Reflect.set",
  120. body: function ()
  121. {
  122. assert.isTrue(Reflect.set([1950, 1960, 1970, 1980, 1990], "0", 1), "Should be able to set property on int array");
  123. assert.isTrue(Reflect.set([1950, 1960.1, 1970, 1980, 1990], "0", 1), "Should be able to set property on float array");
  124. }
  125. },
  126. {
  127. name: "Reflect.defineProperty",
  128. body: function ()
  129. {
  130. var b = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]
  131. Reflect.defineProperty(b, "length", {value: 0});
  132. assert.areEqual(b.length, 0, "Setting length property to 0");
  133. }
  134. },
  135. {
  136. name: "Reflect.set",
  137. body: function ()
  138. {
  139. assert.isTrue(Reflect.set([1950, 1960, 1970, 1980, 1990], "0", 1), "Should be able to set property on int array");
  140. assert.isTrue(Reflect.set([1950, 1960.1, 1970, 1980, 1990], "0", 1), "Should be able to set property on float array");
  141. }
  142. },
  143. {
  144. name: "Array.of",
  145. body: function ()
  146. {
  147. var target = [1,2,3,4,5];
  148. function constructor()
  149. {
  150. return target;
  151. }
  152. var a = Array.of.call(constructor);
  153. assert.areEqual(a, [], "Array.of.call with custom constructor");
  154. }
  155. },
  156. {
  157. name: "CopyOnAccess in ForInEnumerator - native ints",
  158. body: function ()
  159. {
  160. eval("[1,1,1,1,1,1];".repeat(0x4));
  161. x=[1,3,3,4,5,6,7];
  162. var getPropCalled = false;
  163. var handler = {
  164. getPrototypeOf: function(target, name){
  165. getPropCalled = true;
  166. return x;
  167. }
  168. };
  169. var s= [1,5,3,4,5,6,8,9,10,11,12];
  170. p = new Proxy(s, handler);
  171. for(var x1 in p) { };
  172. assert.isTrue(getPropCalled, "for-in enumerator should call getProp from prototype");
  173. assert.areEqual(x1,'10', "enumerator should complete");
  174. }
  175. },
  176. {
  177. name: "CopyOnAccess in ForInEnumerator - native floats",
  178. body: function ()
  179. {
  180. eval("[1,1,1,1,1,1];".repeat(0x4));
  181. x=[1.1,3.1,3.1,4.1,5.1,6.1,7.1];
  182. var getPropCalled = false;
  183. var handler = {
  184. getPrototypeOf: function(target, name){
  185. getPropCalled = true;
  186. return x;
  187. }
  188. };
  189. var s= [1.1,5.1,3.1,4.1,5.1,6.1,8.1,9.1,10.1,11.1,12.1];
  190. p = new Proxy(s, handler);
  191. for(var x1 in p) { };
  192. assert.isTrue(getPropCalled, "for-in enumerator should call getProp from prototype");
  193. assert.areEqual(x1,'10', "enumerator should complete");
  194. }
  195. },
  196. {
  197. name: "CopyOnAccess in for..of - native ints",
  198. body: function ()
  199. {
  200. eval("[1,1,1,1,1,1];".repeat(0x4));
  201. x=[1,3,3,4,5,6,7];
  202. var handler = {
  203. getPrototypeOf: function(target, name){
  204. return x;
  205. }
  206. };
  207. var s= [1,5,3,4,5,6,8,9,10,11,12];
  208. p = new Proxy(s, handler);
  209. for(var x1 of p) { };
  210. assert.areEqual(x1, 12, "enumerator should complete");
  211. }
  212. },
  213. ];
  214. testRunner.runTests(tests, { verbose: WScript.Arguments[0] != "summary" });