constructor.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. function write(v) { WScript.Echo(v + ""); }
  6. Object.prototype.oString = Object.prototype.toString;
  7. Array.prototype.oString = Object.prototype.toString;
  8. Boolean.prototype.oString = Object.prototype.toString;
  9. Date.prototype.oString = Object.prototype.toString;
  10. Function.prototype.oString = Object.prototype.toString;
  11. Number.prototype.oString = Object.prototype.toString;
  12. RegExp.prototype.oString = Object.prototype.toString;
  13. String.prototype.oString = Object.prototype.toString;
  14. var x = 0;
  15. function testEval(str) {
  16. eval(str);
  17. write(str + " x:" + x + " typeof(x):" + typeof(x) + " x.str():" + x.oString());
  18. }
  19. function foo() {}
  20. var objs = [ "undefined", "null",
  21. "true", "false",
  22. "Boolean(true)", "Boolean(false)",
  23. "new Boolean(true)", "new Boolean(false)",
  24. "NaN", "+0", "-0", "0", "0.0", "-0.0", "+0.0",
  25. "1", "10", "10.0", "10.1", "-1",
  26. "-10", "-10.0", "-10.1",
  27. "Number.MAX_VALUE", "Number.MIN_VALUE", "Number.NaN", "Number.POSITIVE_INFINITY", "Number.NEGATIVE_INFINITY",
  28. "new Number(NaN)", "new Number(+0)", "new Number(-0)", "new Number(0)",
  29. "new Number(0.0)", "new Number(-0.0)", "new Number(+0.0)",
  30. "new Number(1)", "new Number(10)", "new Number(10.0)", "new Number(10.1)", "new Number(-1)",
  31. "new Number(-10)", "new Number(-10.0)", "new Number(-10.1)",
  32. "new Number(Number.MAX_VALUE)", "new Number(Number.MIN_VALUE)", "new Number(Number.NaN)",
  33. "new Number(Number.POSITIVE_INFINITY)", "new Number(Number.NEGATIVE_INFINITY)",
  34. "''", "0xa", "04", "'hello'", "'hel' + 'lo'",
  35. "String('')", "String('hello')", "String('h' + 'ello')",
  36. "new String('')", "new String('hello')", "new String('he' + 'llo')",
  37. "new Object()", "new Object()",
  38. "[1, 2, 3]", "[1 ,2 , 3]",
  39. "new Array(3)", "Array(3)", "new Array(1 ,2 ,3)", "Array(1)",
  40. "foo"
  41. ];
  42. testEval("x = Object();");
  43. testEval("x = new Object();");
  44. for (var i=0; i< objs.length; i++) {
  45. testEval("x = Object(" + objs[i] + ");");
  46. testEval("x = new Object(" + objs[i] + ");");
  47. }
  48. Object.prototype.protoFunc = function () { WScript.Echo("ObjectprotoFunc");}
  49. var customPrototype = { protoFunc: function () { WScript.Echo("protoFunc");}}
  50. // Constructor with only this statements
  51. function constr(arg1, arg2)
  52. {
  53. this.a = arg1;
  54. this.b = arg1;
  55. }
  56. // Constructor with more than only this statements
  57. function constr1(arg1, arg2)
  58. {
  59. if(!arg1) arg1 = 0;
  60. if(!arg2) arg2 = 0;
  61. this.a = arg1;
  62. this.b = arg1;
  63. }
  64. function body()
  65. {
  66. var arr = [];
  67. for(var i= 0; i < 2; i++)
  68. {
  69. arr.push(new constr(1, 2, 3)); // with arg constructor cache
  70. arr.push(new constr()); // no arg constructor cache test
  71. arr.push(new constr1(1, 2, 3)); // with arg constructor cache
  72. arr.push(new constr1()); // no arg constructor cache test
  73. }
  74. return arr;
  75. }
  76. WScript.Echo("Testing no prototype property construction");
  77. var arrayOfObjects = body();
  78. Dump(arrayOfObjects);
  79. WScript.Echo("Testing custom object prototype construction");
  80. constr.prototype = customPrototype;
  81. constr1.prototype = customPrototype;
  82. arrayOfObjects = body();
  83. Dump(arrayOfObjects);
  84. WScript.Echo("Testing integer prototype construction");
  85. constr.prototype = 1;
  86. constr1.prototype = 1;
  87. arrayOfObjects = body();
  88. Dump(arrayOfObjects);
  89. WScript.Echo("Testing no prototype property construction");
  90. delete constr.prototype;
  91. delete constr1.prototype;
  92. arrayOfObjects = body();
  93. Dump(arrayOfObjects);
  94. function Dump(arrayOfObjects)
  95. {
  96. for(var j= 0; j < arrayOfObjects.length; j++)
  97. {
  98. arrayOfObjects[j].protoFunc();
  99. }
  100. }
  101. WScript.Echo("Testing cross script context object creation");
  102. var otherScriptContext = WScript.LoadScriptFile("constructor-crossScript.js", "samethread");
  103. var obj = new otherScriptContext.crossContextObject();
  104. WScript.Echo(obj.prop);
  105. obj = new otherScriptContext.crossContextObject();
  106. WScript.Echo(obj.prop);
  107. obj = otherScriptContext.createObject();
  108. WScript.Echo(obj.prop);
  109. obj = otherScriptContext.createObject();
  110. WScript.Echo(obj.prop);
  111. obj = new otherScriptContext.crossContextObject();
  112. WScript.Echo(obj.prop);