/external/webkit/Source/JavaScriptCore/tests/mozilla/js1_5/Object/regress-90596-001.js

https://gitlab.com/brian0218/rk3188_r-box_android4.2.2_sdk · JavaScript · 278 lines · 181 code · 46 blank · 51 comment · 13 complexity · 3b6b4567cc8c478622327c8fef343e5e MD5 · raw file

  1. /*
  2. * The contents of this file are subject to the Netscape Public
  3. * License Version 1.1 (the "License"); you may not use this file
  4. * except in compliance with the License. You may obtain a copy of
  5. * the License at http://www.mozilla.org/NPL/
  6. *
  7. * Software distributed under the License is distributed on an "AS
  8. * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  9. * implied. See the License for the specific language governing
  10. * rights and limitations under the License.
  11. *
  12. * The Original Code is mozilla.org code.
  13. *
  14. * The Initial Developer of the Original Code is Netscape
  15. * Communications Corporation. Portions created by Netscape are
  16. * Copyright (C) 1998 Netscape Communications Corporation.
  17. * All Rights Reserved.
  18. *
  19. * Contributor(s): pschwartau@netscape.com
  20. * Date: 28 August 2001
  21. *
  22. * SUMMARY: A [DontEnum] prop, if overridden, should appear in toSource().
  23. * See http://bugzilla.mozilla.org/show_bug.cgi?id=90596
  24. *
  25. * NOTE: some inefficiencies in the test are made for the sake of readability.
  26. * Sorting properties alphabetically is done for definiteness in comparisons.
  27. */
  28. //-----------------------------------------------------------------------------
  29. var UBound = 0;
  30. var bug = 90596;
  31. var summary = 'A [DontEnum] prop, if overridden, should appear in toSource()';
  32. var cnCOMMA = ',';
  33. var cnLBRACE = '{';
  34. var cnRBRACE = '}';
  35. var cnLPAREN = '(';
  36. var cnRPAREN = ')';
  37. var status = '';
  38. var statusitems = [];
  39. var actual = '';
  40. var actualvalues = [];
  41. var expect= '';
  42. var expectedvalues = [];
  43. var obj = {};
  44. status = inSection(1);
  45. obj = {toString:9};
  46. actual = obj.toSource();
  47. expect = '({toString:9})';
  48. addThis();
  49. status = inSection(2);
  50. obj = {hasOwnProperty:"Hi"};
  51. actual = obj.toSource();
  52. expect = '({hasOwnProperty:"Hi"})';
  53. addThis();
  54. status = inSection(3);
  55. obj = {toString:9, hasOwnProperty:"Hi"};
  56. actual = obj.toSource();
  57. expect = '({toString:9, hasOwnProperty:"Hi"})';
  58. addThis();
  59. status = inSection(4);
  60. obj = {prop1:1, toString:9, hasOwnProperty:"Hi"};
  61. actual = obj.toSource();
  62. expect = '({prop1:1, toString:9, hasOwnProperty:"Hi"})';
  63. addThis();
  64. // TRY THE SAME THING IN EVAL CODE
  65. var s = '';
  66. status = inSection(5);
  67. s = 'obj = {toString:9}';
  68. eval(s);
  69. actual = obj.toSource();
  70. expect = '({toString:9})';
  71. addThis();
  72. status = inSection(6);
  73. s = 'obj = {hasOwnProperty:"Hi"}';
  74. eval(s);
  75. actual = obj.toSource();
  76. expect = '({hasOwnProperty:"Hi"})';
  77. addThis();
  78. status = inSection(7);
  79. s = 'obj = {toString:9, hasOwnProperty:"Hi"}';
  80. eval(s);
  81. actual = obj.toSource();
  82. expect = '({toString:9, hasOwnProperty:"Hi"})';
  83. addThis();
  84. status = inSection(8);
  85. s = 'obj = {prop1:1, toString:9, hasOwnProperty:"Hi"}';
  86. eval(s);
  87. actual = obj.toSource();
  88. expect = '({prop1:1, toString:9, hasOwnProperty:"Hi"})';
  89. addThis();
  90. // TRY THE SAME THING IN FUNCTION CODE
  91. function A()
  92. {
  93. status = inSection(9);
  94. var s = 'obj = {toString:9}';
  95. eval(s);
  96. actual = obj.toSource();
  97. expect = '({toString:9})';
  98. addThis();
  99. }
  100. A();
  101. function B()
  102. {
  103. status = inSection(10);
  104. var s = 'obj = {hasOwnProperty:"Hi"}';
  105. eval(s);
  106. actual = obj.toSource();
  107. expect = '({hasOwnProperty:"Hi"})';
  108. addThis();
  109. }
  110. B();
  111. function C()
  112. {
  113. status = inSection(11);
  114. var s = 'obj = {toString:9, hasOwnProperty:"Hi"}';
  115. eval(s);
  116. actual = obj.toSource();
  117. expect = '({toString:9, hasOwnProperty:"Hi"})';
  118. addThis();
  119. }
  120. C();
  121. function D()
  122. {
  123. status = inSection(12);
  124. var s = 'obj = {prop1:1, toString:9, hasOwnProperty:"Hi"}';
  125. eval(s);
  126. actual = obj.toSource();
  127. expect = '({prop1:1, toString:9, hasOwnProperty:"Hi"})';
  128. addThis();
  129. }
  130. D();
  131. //-----------------------------------------------------------------------------
  132. test();
  133. //-----------------------------------------------------------------------------
  134. /*
  135. * Sort properties alphabetically -
  136. */
  137. function addThis()
  138. {
  139. statusitems[UBound] = status;
  140. actualvalues[UBound] = sortThis(actual);
  141. expectedvalues[UBound] = sortThis(expect);
  142. UBound++;
  143. }
  144. /*
  145. * Takes string of form '({"c", "b", "a", 2})' and returns '({"a","b","c",2})'
  146. */
  147. function sortThis(sList)
  148. {
  149. sList = compactThis(sList);
  150. sList = stripParens(sList);
  151. sList = stripBraces(sList);
  152. var arr = sList.split(cnCOMMA);
  153. arr = arr.sort();
  154. var ret = String(arr);
  155. ret = addBraces(ret);
  156. ret = addParens(ret);
  157. return ret;
  158. }
  159. /*
  160. * Strips out any whitespace from the text -
  161. */
  162. function compactThis(text)
  163. {
  164. var charCode = 0;
  165. var ret = '';
  166. for (var i=0; i<text.length; i++)
  167. {
  168. charCode = text.charCodeAt(i);
  169. if (!isWhiteSpace(charCode))
  170. ret += text.charAt(i);
  171. }
  172. return ret;
  173. }
  174. function isWhiteSpace(charCode)
  175. {
  176. switch (charCode)
  177. {
  178. case (0x0009):
  179. case (0x000B):
  180. case (0x000C):
  181. case (0x0020):
  182. case (0x000A): // '\n'
  183. case (0x000D): // '\r'
  184. return true;
  185. break;
  186. default:
  187. return false;
  188. }
  189. }
  190. /*
  191. * strips off parens at beginning and end of text -
  192. */
  193. function stripParens(text)
  194. {
  195. // remember to escape the parens...
  196. var arr = text.match(/^\((.*)\)$/);
  197. // defend against a null match...
  198. if (arr != null && arr[1] != null)
  199. return arr[1];
  200. return text;
  201. }
  202. /*
  203. * strips off braces at beginning and end of text -
  204. */
  205. function stripBraces(text)
  206. {
  207. // remember to escape the braces...
  208. var arr = text.match(/^\{(.*)\}$/);
  209. // defend against a null match...
  210. if (arr != null && arr[1] != null)
  211. return arr[1];
  212. return text;
  213. }
  214. function addBraces(text)
  215. {
  216. return cnLBRACE + text + cnRBRACE;
  217. }
  218. function addParens(text)
  219. {
  220. return cnLPAREN + text + cnRPAREN;
  221. }
  222. function test()
  223. {
  224. enterFunc ('test');
  225. printBugNumber (bug);
  226. printStatus (summary);
  227. for (var i=0; i<UBound; i++)
  228. {
  229. reportCompare(expectedvalues[i], actualvalues[i], statusitems[i]);
  230. }
  231. exitFunc ('test');
  232. }