CopyOnAccessArray_bugs.js 7.4 KB

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