03.assign_sm.js 15 KB

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