03.assign.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  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. (function Test1() {
  7. var str = "Unresolvable reference";
  8. try {
  9. test1_value = 'test1 value...';
  10. } catch (e) {
  11. write("Exception: " + str);
  12. return;
  13. }
  14. write("Return: " + str);
  15. })();
  16. (function Test1_eval() {
  17. var str = "Test1_eval: Unresolvable reference";
  18. try {
  19. eval("test1_eval_value = 10");
  20. } catch (e) {
  21. write("Exception: " + str);
  22. return;
  23. }
  24. write("Return: " + str);
  25. })();
  26. (function Test1_1() {
  27. var str = "Test1_1: Globally resolvable reference";
  28. try {
  29. test1_1_value = 'value...'; // declared below
  30. } catch (e) {
  31. write("Exception: " + str);
  32. return;
  33. }
  34. write("Return: " + str);
  35. })();
  36. var test1_1_value; // declared globally
  37. (function Test1_2() {
  38. (function g() {
  39. var str = "Test1_2: Parent resolvable reference";
  40. try {
  41. test1_2_value = 'value...'; // declared below
  42. } catch (e) {
  43. write("Exception: " + str);
  44. return;
  45. }
  46. write("Return: " + str);
  47. })();
  48. var test1_2_value = 0; // declared in parent
  49. })();
  50. var glo = this;
  51. (function Test1_3() {
  52. (function g() {
  53. var str = "Test1_3: Explicitly bound reference";
  54. try {
  55. glo.test1_3_value = 'value...';
  56. } catch (e) {
  57. write("Exception: " + str);
  58. return;
  59. }
  60. write("Return: " + str);
  61. })();
  62. })();
  63. (function Test1_3_eval() {
  64. (function g() {
  65. var str = "Test1_3_eval: Explicitly bound reference";
  66. try {
  67. eval("this.test1_3_eval_value = 10");
  68. } catch (e) {
  69. write("Exception: " + str);
  70. return;
  71. }
  72. write("Return: " + str);
  73. })();
  74. })();
  75. (function Test2(){
  76. var str = "Readonly property";
  77. var obj = new Object();
  78. Object.defineProperty(obj, "foo", {
  79. writable:false,
  80. value:20
  81. });
  82. try {
  83. obj.foo = 30;
  84. } catch(e) {
  85. write("Exception: " + str);
  86. return;
  87. }
  88. write("Return: " + str);
  89. })();
  90. (function Test2_1(){
  91. var str = "Test2_1: Readonly property on global";
  92. Object.defineProperty(glo, "foo", {
  93. writable:false,
  94. value:20
  95. });
  96. try {
  97. foo = 30; // Implicitly assign to global
  98. } catch(e) {
  99. write("Exception: " + str);
  100. return;
  101. }
  102. write("Return: " + str);
  103. })();
  104. (function Test2_2(){
  105. var str = "Test2_2: Readonly property on global";
  106. try {
  107. glo.foo = 30; // Explicitly assign to global
  108. } catch(e) {
  109. write("Exception: " + str);
  110. return;
  111. }
  112. write("Return: " + str);
  113. })();
  114. (function Test2_3(){
  115. var str = "Test2_3: Readonly property on prototype";
  116. var proto = Object.create(Object.prototype, {
  117. "foo": {
  118. writable:false,
  119. value:20
  120. }
  121. });
  122. var obj = Object.create(proto);
  123. try {
  124. obj.foo = 30;
  125. } catch(e) {
  126. write("Exception: " + str);
  127. return;
  128. }
  129. write("Return: " + str);
  130. })();
  131. (function Test2_3_int(){
  132. var str = "Test2_3_int: Readonly property on Number prototype";
  133. Object.defineProperty(Number.prototype, "foo", {
  134. writable:false,
  135. configurable:true,
  136. value:20
  137. });
  138. try {
  139. 123["foo"] = 23;
  140. } catch(e) {
  141. write("Exception: " + str);
  142. return;
  143. } finally {
  144. delete Number.prototype.foo;
  145. }
  146. write("Return: " + str);
  147. })();
  148. (function Test2_4(){
  149. var str = "Test2_4: Readonly property with index property name";
  150. var prop = "7"; // Use a string
  151. var obj = Object.create(Object.prototype, {
  152. "7": {
  153. writable:false,
  154. value:20
  155. }
  156. });
  157. try {
  158. obj[prop] = 24;
  159. } catch(e) {
  160. write("Exception: " + str);
  161. return;
  162. }
  163. write("Return: " + str);
  164. })();
  165. (function Test2_4_arr(){
  166. var str = "Test2_4_arr: Readonly property on array with index property name";
  167. var prop = "7"; // Use a string
  168. var obj = [0,1,2,3,4,5,6,7,8,9];
  169. Object.defineProperty(obj, prop, {
  170. writable:false,
  171. value:20
  172. });
  173. try {
  174. obj[prop] = 24;
  175. } catch(e) {
  176. write("Exception: " + str);
  177. return;
  178. }
  179. write("Return: " + str);
  180. })();
  181. (function Test2_4_eval(){
  182. var str = "Test2_4_eval: Readonly property with index property name";
  183. var prop = "7"; // Use a string
  184. var obj = Object.create(Object.prototype, {
  185. "7": {
  186. writable:false,
  187. value:20
  188. }
  189. });
  190. try {
  191. eval("obj[prop] = 24");
  192. } catch(e) {
  193. write("Exception: " + str);
  194. return;
  195. }
  196. write("Return: " + str);
  197. })();
  198. (function Test2_5(){
  199. var str = "Test2_5: Readonly property with index property name";
  200. var prop = 3; // Use an integer
  201. var obj = Object.create(Object.prototype, {
  202. "3": {
  203. writable:false,
  204. value:20
  205. }
  206. });
  207. try {
  208. obj[prop] = 25;
  209. } catch(e) {
  210. write("Exception: " + str);
  211. return;
  212. }
  213. write("Return: " + str);
  214. })();
  215. (function Test2_5_arr(){
  216. var str = "Test2_5_arr: Readonly property on array with index property name";
  217. var prop = 3; // Use an integer
  218. var obj = [];
  219. Object.defineProperty(obj, prop, {
  220. writable:false,
  221. value:20
  222. });
  223. try {
  224. obj[prop] = 25;
  225. } catch(e) {
  226. write("Exception: " + str);
  227. return;
  228. }
  229. write("Return: " + str);
  230. })();
  231. (function Test2_5_eval(){
  232. var str = "Test2_5_eval: Readonly property with index property name";
  233. var prop = 3; // Use an integer
  234. var obj = Object.create(Object.prototype, {
  235. "3": {
  236. writable:false,
  237. value:20
  238. }
  239. });
  240. try {
  241. eval("obj[prop] = 25");
  242. } catch(e) {
  243. write("Exception: " + str);
  244. return;
  245. }
  246. write("Return: " + str);
  247. })();
  248. (function Test2_6(){
  249. var str = "Test2_6: Readonly property on arguments (empty)";
  250. Object.defineProperty(arguments, "1", {
  251. writable:false,
  252. value:20
  253. });
  254. try {
  255. arguments[1] = 26;
  256. } catch(e) {
  257. write("Exception: " + str);
  258. return;
  259. }
  260. write("Return: " + str);
  261. })();
  262. (function Test2_7(a,b,c){
  263. var str = "Test2_7: Readonly property on arguments (with formals)";
  264. Object.defineProperty(arguments, "1", {
  265. writable:false,
  266. value:20
  267. });
  268. try {
  269. arguments[1] = 27;
  270. } catch(e) {
  271. write("Exception: " + str);
  272. return;
  273. }
  274. write("Return: " + str);
  275. })(270,271,272);
  276. (function Test2_8(a,b,c){
  277. var str = "Test2_8: Undefined setter on arguments (with formals)";
  278. Object.defineProperty(arguments, "1", {
  279. get: function() { return "arguments[1] value"; } // Only getter specified
  280. });
  281. try {
  282. arguments[1] = 28;
  283. } catch(e) {
  284. write("Exception: " + str);
  285. return;
  286. }
  287. write("Return: " + str);
  288. })(280, 281, 282);
  289. (function Test2_9() {
  290. var str = "Test2_9: Readonly property indexed by variable";
  291. var prop = "prop"; // Use a string
  292. var obj = {};
  293. Object.defineProperty(obj, prop, {
  294. writable: false,
  295. value: 20
  296. });
  297. try {
  298. obj[prop] = 25;
  299. } catch (e) {
  300. write("Exception: " + str);
  301. return;
  302. }
  303. write("Return: " + str);
  304. })();
  305. (function Test3(){
  306. var str = "Setter undefined";
  307. var obj = new Object();
  308. Object.defineProperty(obj, "foo", {
  309. set: undefined
  310. });
  311. try {
  312. obj.foo = 30;
  313. } catch(e) {
  314. write("Exception: " + str);
  315. return;
  316. }
  317. write("Return: " + str);
  318. })();
  319. (function Test3_eval(){
  320. var str = "Test3_eval: Setter undefined";
  321. var obj = new Object();
  322. Object.defineProperty(obj, "foo", {
  323. set: undefined
  324. });
  325. try {
  326. eval("obj.foo = 30");
  327. } catch(e) {
  328. write("Exception: " + str);
  329. return;
  330. }
  331. write("Return: " + str);
  332. })();
  333. (function Test3_1(){
  334. var str = "Test3_1: Setter undefined";
  335. var obj = new Object();
  336. Object.defineProperty(obj, "foo", {
  337. get: function() { return "foo value"; } // Only getter specified
  338. });
  339. try {
  340. obj.foo = 30;
  341. } catch(e) {
  342. write("Exception: " + str);
  343. return;
  344. }
  345. write("Return: " + str);
  346. })();
  347. (function Test3_2(){
  348. var str = "Test3_2: Setter undefined on prototype";
  349. var proto = Object.create(Object.prototype, {
  350. "foo": {
  351. get: function() { return "foo value"; } // Only getter specified
  352. }
  353. });
  354. var obj = Object.create(proto);
  355. try {
  356. obj.foo = 30;
  357. } catch(e) {
  358. write("Exception: " + str);
  359. return;
  360. }
  361. write("Return: " + str);
  362. })();
  363. (function Test3_2_int(){
  364. var str = "Test3_2_int: Setter undefined on Number prototype";
  365. Object.defineProperty(Number.prototype, "foo", {
  366. get: function() { return "foo value"; }, // Only getter defined
  367. configurable:true
  368. });
  369. try {
  370. 123["foo"] = 32;
  371. } catch(e) {
  372. write("Exception: " + str);
  373. return;
  374. } finally {
  375. delete Number.prototype.foo;
  376. }
  377. write("Return: " + str);
  378. })();
  379. (function Test3_3(){
  380. var str = "Test3_3: Setter undefined on index property name";
  381. var prop = "7"; // Use a string
  382. var obj = Object.create(Object.prototype, {
  383. "7": {
  384. get: function() { return "foo value"; } // Only getter specified
  385. }
  386. });
  387. try {
  388. obj[prop] = 33;
  389. } catch(e) {
  390. write("Exception: " + str);
  391. return;
  392. }
  393. write("Return: " + str);
  394. })();
  395. (function Test3_3_arr(){
  396. var str = "Test3_3_arr: Setter undefined on array with index property name";
  397. var prop = "7"; // Use a string
  398. var obj = [0,1,2,3,4,5,6,7,8];
  399. Object.defineProperty(obj, prop, {
  400. get: function() { return "foo value"; } // Only getter specified
  401. });
  402. try {
  403. obj[prop] = 33;
  404. } catch(e) {
  405. write("Exception: " + str);
  406. return;
  407. }
  408. write("Return: " + str);
  409. })();
  410. (function Test3_4(){
  411. var str = "Test3_4: Setter undefined on index property name";
  412. var prop = 7; // Use a integer
  413. var obj = Object.create(Object.prototype, {
  414. "7": {
  415. get: function() { return "foo value"; } // Only getter specified
  416. }
  417. });
  418. try {
  419. obj[prop] = 34;
  420. } catch(e) {
  421. write("Exception: " + str);
  422. return;
  423. }
  424. write("Return: " + str);
  425. })();
  426. (function Test3_4_arr(){
  427. var str = "Test3_4_arr: Setter undefined on array with index property name";
  428. var prop = 3; // Use a integer
  429. var obj = [];
  430. Object.defineProperty(obj, prop, {
  431. get: function() { return "foo value"; } // Only getter specified
  432. });
  433. try {
  434. obj[prop] = 34;
  435. } catch(e) {
  436. write("Exception: " + str);
  437. return;
  438. }
  439. write("Return: " + str);
  440. })();
  441. (function Test3_5() {
  442. var str = "Test3_5: Setter undefined and indexed by variable";
  443. var prop = "prop"; // Use a string
  444. var obj = {};
  445. Object.defineProperty(obj, prop, {
  446. get: function () { return "foo value"; } // Only getter specified
  447. });
  448. try {
  449. obj[prop] = 25;
  450. } catch (e) {
  451. write("Exception: " + str);
  452. return;
  453. }
  454. write("Return: " + str);
  455. })();
  456. (function Test4(){
  457. var str = "Adding non-existent property to non-extensible object";
  458. var obj = new Object();
  459. Object.preventExtensions(obj);
  460. try {
  461. obj.foo = 20;
  462. } catch(e) {
  463. write("Exception: " + str);
  464. return;
  465. }
  466. write("Return: " + str);
  467. })();
  468. (function Test4_1(){
  469. var str = "Test4_1: Adding non-existent index property to non-extensible object";
  470. var obj = new Object();
  471. Object.preventExtensions(obj);
  472. try {
  473. obj[3] = 20;
  474. } catch(e) {
  475. write("Exception: " + str);
  476. return;
  477. }
  478. write("Return: " + str);
  479. })();
  480. (function Test4_arr_1(){
  481. var str = "Test4_arr_1: Adding non-existent property to non-extensible array with index property name";
  482. var obj = [];
  483. var prop = "7"; // Use a string
  484. Object.preventExtensions(obj);
  485. try {
  486. obj[prop] = 4;
  487. } catch(e) {
  488. write("Exception: " + str);
  489. return;
  490. }
  491. write("Return: " + str);
  492. })();
  493. (function Test4_arr_2(){
  494. var str = "Test4_arr_2: Adding non-existent property to non-extensible array with index property name";
  495. var obj = [];
  496. var prop = 3; // Use an integer
  497. Object.preventExtensions(obj);
  498. try {
  499. obj[prop] = 4;
  500. } catch(e) {
  501. write("Exception: " + str);
  502. return;
  503. }
  504. write("Return: " + str);
  505. })();
  506. (function Test5(){
  507. var str = "Postfix increment to non-writable property";
  508. var obj = new Object();
  509. Object.defineProperty(obj, "foo", {
  510. writable:false,
  511. value:20
  512. });
  513. try {
  514. obj.foo++;
  515. } catch(e) {
  516. write("Exception: " + str);
  517. return;
  518. }
  519. write("Return: " + str);
  520. } )();
  521. (function Test6(){
  522. var str = "Postfix increment on non-extensible object's non-existent property";
  523. var obj = new Object();
  524. Object.preventExtensions(obj);
  525. try {
  526. obj.foo++;
  527. } catch(e) {
  528. write("Exception: " + str);
  529. return;
  530. }
  531. write("Return: " + str);
  532. } )();
  533. (function Test7(){
  534. var str = "Assign NaN of globalObject via property";
  535. var globalObject = Function("return this;")();
  536. try {
  537. globalObject.NaN = "blah";
  538. } catch(e) {
  539. write("Exception: " + str);
  540. return;
  541. }
  542. write("Return: " + str);
  543. } )();
  544. (function Test8(){
  545. var str = "Assign Infinity of globalObject via indexer/literal";
  546. var globalObject = Function("return this;")();
  547. try {
  548. globalObject[Infinity] = "blah";
  549. } catch(e) {
  550. write("Exception: " + str);
  551. return;
  552. }
  553. write("Return: " + str);
  554. } )();
  555. (function Test9(){
  556. var str = "Assign Infinity of globalObject via indexer/string";
  557. var globalObject = Function("return this;")();
  558. try {
  559. globalObject["Infinity"] = "blah";
  560. } catch(e) {
  561. write("Exception: " + str);
  562. return;
  563. }
  564. write("Return: " + str);
  565. } )();