proto_basic.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709
  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. /// <reference path="protolib.js" />
  6. if (this.WScript && this.WScript.LoadScriptFile) {
  7. WScript.LoadScriptFile("protolib.js");
  8. }
  9. function printProtoChain(obj) {
  10. var known = [
  11. Number.prototype, Boolean.prototype, String.prototype, Object.prototype, Array.prototype, Function.prototype,
  12. ];
  13. var names = [
  14. "Number.prototype", "Boolean.prototype", "String.prototype", "Object.prototype", "Array.prototype", "Function.prototype",
  15. ];
  16. var s = "";
  17. while (obj) {
  18. var i = known.indexOf(obj);
  19. var name = (i >= 0 ? names[i] : JSON.stringify(obj));
  20. if (s == "") {
  21. s = name;
  22. } else {
  23. s += " -> " + name;
  24. }
  25. obj = Object.getPrototypeOf(obj);
  26. }
  27. helpers.writeln(s);
  28. }
  29. function make_point(_x, _y) {
  30. return { x: _x, y: _y };
  31. }
  32. var tests;
  33. if (helpers.isVersion10OrLater) { // TODO: Change to _11_OrLater
  34. tests = [
  35. {
  36. name: "Test Object.prototype.__proto__ attributes",
  37. body: function () {
  38. var desc = Object.getOwnPropertyDescriptor(Object.prototype, "__proto__");
  39. helpers.printObject(desc);
  40. }
  41. },
  42. {
  43. name: "Read built-in __proto__",
  44. body: function () {
  45. assert.isTrue(12["__proto__"] === Number.prototype, "int __proto__");
  46. assert.isTrue(12.3["__proto__"] === Number.prototype, "float __proto__");
  47. assert.isTrue((new Number(12)).__proto__ === Number.prototype, "Number Object __proto__");
  48. assert.isTrue(true.__proto__ === Boolean.prototype, "Boolean Value __proto__");
  49. assert.isTrue(false.__proto__ === Boolean.prototype, "Boolean Value __proto__");
  50. assert.isTrue((new Boolean(true)).__proto__ === Boolean.prototype, "Boolean Object __proto__");
  51. assert.isTrue((new Boolean(false)).__proto__ === Boolean.prototype, "Boolean Object __proto__");
  52. assert.isTrue("hello".__proto__ === String.prototype, "String Value __proto__");
  53. assert.isTrue(new String("hello").__proto__ === String.prototype, "String Object __proto__");
  54. assert.isTrue({}.__proto__ === Object.prototype, "{} __proto__");
  55. assert.isTrue([].__proto__ === Array.prototype, "[] __proto__");
  56. assert.isTrue(Array.prototype.__proto__ === Object.prototype, "Array.prototype.__proto__");
  57. assert.isTrue(Object.prototype.__proto__ === null, "Object.prototype.__proto__");
  58. assert.isTrue(Array.__proto__ === Function.prototype, "Array.__proto__");
  59. assert.isTrue(Function.__proto__ === Function.prototype, "Function.__proto__");
  60. assert.isTrue(Function.prototype.__proto__ === Object.prototype, "Function.prototype.__proto__");
  61. var o = { a: 0 };
  62. assert.isTrue(o.__proto__ === Object.prototype, "o.__proto__");
  63. assert.isTrue(o.__proto__.__proto__ === null, "o.__proto__.__proto__");
  64. }
  65. },
  66. {
  67. name: "Change built-in's __proto__ chain",
  68. body: function () {
  69. function changeProtoAndTest(obj, newProto, test) {
  70. var oldProto = obj.__proto__;
  71. obj.__proto__ = newProto;
  72. printProtoChain(obj);
  73. try {
  74. test();
  75. } finally {
  76. obj.__proto__ = oldProto; // Restore old __proto__
  77. }
  78. }
  79. // Number doesn't have sort method, should throw
  80. assert.throws(function () { 12["sort"]() }, TypeError, "Object doesn't support property or method 'sort'");
  81. // Number gets Array methods from new prototype in the chain
  82. changeProtoAndTest(Number.prototype, Array.prototype, function () {
  83. 12["sort"]();
  84. var o = new Number(34);
  85. o[0] = 8;
  86. o[1] = 3;
  87. o[2] = 9;
  88. o[3] = 5;
  89. o.length = 4;
  90. assert.areEqual("8 3 9 5", o.join(" "));
  91. o.sort();
  92. assert.areEqual("3 5 8 9", o.join(" "));
  93. });
  94. // Boolean doesn't have String methods
  95. assert.throws(function () { true.toUpperCase() }, TypeError, "Object doesn't support property or method 'toUpperCase'");
  96. // Boolean gets String methods from new prototype in the chain
  97. changeProtoAndTest(Boolean.prototype, new String("abc"), function () {
  98. assert.areEqual("TRUE", true.toUpperCase());
  99. assert.areEqual("FALSE", false.toUpperCase());
  100. assert.areEqual("TRUE", (new Boolean(true)).toUpperCase());
  101. assert.areEqual("FALSE", (new Boolean(false)).toUpperCase());
  102. });
  103. }
  104. },
  105. {
  106. name: "Change __proto__ to null/undefined",
  107. body: function () {
  108. var a = {};
  109. // Set an object's prototype to undefined
  110. {
  111. var a = {};
  112. assert.isTrue(a.__proto__ === Object.prototype);
  113. assert.isTrue(!a.isPrototypeOf(a));
  114. a.__proto__ = undefined;
  115. assert.isTrue(a.__proto__ === Object.prototype);
  116. assert.isTrue(!a.isPrototypeOf(a));
  117. assert.throws__proto__ArgNotObjectOrNull(function () { Object.setPrototypeOf(a, undefined); }, "Object.setPrototypeOf");
  118. }
  119. // Set an object's prototype to null
  120. function f1(O, P) { O.__proto__ = P; }
  121. function f2(O, P) { Object.setPrototypeOf(O, P); }
  122. [f1, f2].forEach(function (f) {
  123. var a = {};
  124. assert.isTrue(a.__proto__ === Object.prototype);
  125. assert.isTrue(!a.isPrototypeOf(a));
  126. f(a, null);
  127. assert.isTrue(Object.getPrototypeOf(a) === null);
  128. assert.isTrue(a.__proto__ === undefined, "lost Object.prototype __proto__ getter");
  129. assert.isTrue(a.isPrototypeOf === undefined, "lost Object.prototype methods");
  130. });
  131. // side test -- try to change null/undefined's __proto__ should throw
  132. var __proto__setter = Object.getOwnPropertyDescriptor(Object.prototype, "__proto__").set;
  133. [null, undefined].forEach(function (o) {
  134. assert.throws__proto__ThisNotObject(function () {
  135. __proto__setter.apply(o, [{}]);
  136. });
  137. assert.throws__proto__ArgNotObject(function () {
  138. Object.setPrototypeOf(o, {});
  139. });
  140. });
  141. }
  142. },
  143. {
  144. name: "Change __proto__ to neither Object nor null, should throw",
  145. body: function () {
  146. function assertThrowAndNoChange(proto) {
  147. var a = {};
  148. a.__proto__ = proto;
  149. assert.isTrue(Object.getPrototypeOf(a) === Object.prototype, "Change __proto__ to: " + proto);
  150. assert.throws__proto__ArgNotObjectOrNull(function () {
  151. Object.setPrototypeOf(a, proto);
  152. }, "Object.setPrototypeOf");
  153. assert.isTrue(Object.getPrototypeOf(a) === Object.prototype, "Change __proto__ to: " + proto);
  154. }
  155. var __proto__setter = Object.getOwnPropertyDescriptor(Object.prototype, "__proto__").set;
  156. [
  157. undefined, 0, 123, -12.3, NaN, Infinity, true, false, "str"
  158. ].forEach(function (proto) {
  159. assertThrowAndNoChange(proto);
  160. // side test -- try to change these primitive's __proto__ should pass (but has no effect)
  161. if (proto !== undefined) {
  162. proto.__proto__ = {}; // This is ok
  163. __proto__setter.apply(proto, [{}]); // This is ok
  164. Object.setPrototypeOf(proto, {});
  165. }
  166. });
  167. }
  168. },
  169. {
  170. name: "Change object.__proto__ to an object",
  171. body: function () {
  172. var a = { a: 100, b: 100 };
  173. var b = { b: 200, c: 200 };
  174. b.__proto__ = a;
  175. helpers.printObject(b);
  176. helpers.writeln("\n-- delete b.b --");
  177. delete b.b;
  178. helpers.printObject(b);
  179. }
  180. },
  181. {
  182. name: "Change object.__proto__ to an array",
  183. body: function () {
  184. var a = [8, 3, 9, 5];
  185. var b = { b: 200 };
  186. // b.sort not available
  187. assert.throws(function () { b.sort(); });
  188. b.__proto__ = a;
  189. assert.isTrue(!Array.isArray(b), "Still not Array");
  190. helpers.printObject(b);
  191. helpers.writeln("\n-- a.sort --");
  192. a.sort();
  193. helpers.printObject(b);
  194. helpers.writeln("\n-- b.sort --");
  195. b.sort();
  196. helpers.printObject(b);
  197. }
  198. },
  199. {
  200. name: "Change array.__proto__ to an object",
  201. body: function () {
  202. var a = { a: 100 };
  203. var b = [8, 3, 9, 5];
  204. b.__proto__ = a;
  205. assert.isTrue(Array.isArray(b), "Still is Array");
  206. helpers.printObject(b);
  207. // But now b.sort is gone
  208. assert.throws(function () { b.sort(); });
  209. // We can sort by apply
  210. helpers.writeln("\n-- sort.apply --");
  211. Array.prototype.sort.apply(b);
  212. helpers.printObject(b);
  213. }
  214. },
  215. {
  216. name: "Attempt to change DOM object __proto__",
  217. body: function () {
  218. function test(domObj, expected) {
  219. var p0 = Object.getPrototypeOf(domObj);
  220. assert.isTrue(p0 === domObj.__proto__);
  221. var p1 = { __proto__: p0 };
  222. domObj.__proto__ = p1;
  223. assert.areEqual(expected, Object.getPrototypeOf(domObj) === p1);
  224. assert.isTrue(expected || domObj.document !== null); // expected succeed or otherwise window
  225. }
  226. if (helpers.isInBrowser()) {
  227. test(this, false);
  228. test(window, false);
  229. test(window.frames[0], false);
  230. test(document, true);
  231. test(document.body, true);
  232. test(document.createElement("div"), true);
  233. // test form scope
  234. var f = document.getElementById("form1");
  235. var p0 = f.__proto__;
  236. f.__proto__ = { form1_injected: "form1_injected_value", __proto__: p0 };
  237. var button1 = document.getElementById("button1");
  238. button1.click();
  239. assert.areEqual("form1_injected_value", button1.form1_injected_copy);
  240. }
  241. }
  242. },
  243. {
  244. name: "__proto__ and instanceof/isPrototypeOf",
  245. body: function () {
  246. function A() { }
  247. function B() { }
  248. var a = new A();
  249. var b = new B();
  250. assert.isTrue(!(b instanceof A), "Initially b is not instance of A");
  251. assert.isTrue(!(A.prototype.isPrototypeOf(b)));
  252. assert.isTrue(b instanceof B, "Initially b is instance of B");
  253. assert.isTrue(B.prototype.isPrototypeOf(b));
  254. b.__proto__ = A.prototype;
  255. assert.isTrue(b instanceof A, "Now b is instance of A");
  256. assert.isTrue(A.prototype.isPrototypeOf(b));
  257. assert.isTrue(!(b instanceof B), "Now b is not instance of B");
  258. assert.isTrue(!(B.prototype.isPrototypeOf(b)));
  259. b.__proto__ = a;
  260. assert.isTrue(b instanceof A, "b is still instance of A");
  261. assert.isTrue(A.prototype.isPrototypeOf(b));
  262. assert.isTrue(!(b instanceof B), "b is still not instance of B");
  263. assert.isTrue(!(B.prototype.isPrototypeOf(b)));
  264. b.__proto__ = B.prototype;
  265. assert.isTrue(!(b instanceof A), "b is back not instance of A");
  266. assert.isTrue(!(A.prototype.isPrototypeOf(b)));
  267. assert.isTrue(b instanceof B, "b is back instance of B");
  268. assert.isTrue(B.prototype.isPrototypeOf(b));
  269. }
  270. },
  271. {
  272. name: "Verify we reject simple __proto__ cycle",
  273. body: function () {
  274. var a = {};
  275. var b = {};
  276. var c = {};
  277. a.__proto__ = b;
  278. b.__proto__ = c;
  279. assert.throws__proto__Cyclic(function () { c.__proto__ = a });
  280. assert.throws__proto__Cyclic(function () { a.__proto__ = a });
  281. assert.throws__proto__Cyclic(function () { Array.prototype.__proto__ = [] });
  282. }
  283. },
  284. {
  285. name: "Verify proto cache is discarded",
  286. body: function () {
  287. var a = make_point(1, 2);
  288. var b = make_point(3, 4);
  289. //
  290. // Create prototype chain:
  291. // x -> y -> a
  292. // where x inherits properties from prototype a.
  293. //
  294. var y = Object.create(a);
  295. var x = Object.create(y);
  296. function print(o) {
  297. helpers.writeln(o.x + ", " + o.y);
  298. }
  299. helpers.writeln("Before change");
  300. print(x);
  301. //
  302. // Now change prototype chain:
  303. // x -> y -> b
  304. // While changing y.__proto__, we should discard inline proto cache in original chain.
  305. // x should now get changed properties from prototype b.
  306. //
  307. helpers.writeln("After change");
  308. y.__proto__ = b;
  309. print(x);
  310. }
  311. },
  312. {
  313. // Copy of above proto cache test and change a getter
  314. name: "Verify proto getter cache is discarded",
  315. body: function () {
  316. var a = make_point(1, 2);
  317. var b = make_point(3, 4);
  318. Object.defineProperty(a, "y", { get: function () { return 7; }, enumerable: true, configurable: true });
  319. //
  320. // Create prototype chain:
  321. // x -> y -> a
  322. // where x inherits properties from prototype a.
  323. //
  324. var y = Object.create(a);
  325. var x = Object.create(y);
  326. function print(o) {
  327. helpers.writeln(o.x + ", " + o.y);
  328. }
  329. helpers.writeln("Before change");
  330. print(x);
  331. //
  332. // Now change prototype chain:
  333. // x -> y -> b
  334. // While changing y.__proto__, we should discard inline proto cache in original chain.
  335. // x should now get changed properties from prototype b.
  336. //
  337. helpers.writeln("After change");
  338. y.__proto__ = b;
  339. print(x);
  340. },
  341. },
  342. {
  343. name: "Verify the new proto object is marked as proto, so that changing the proto object invalidates related proto cache",
  344. body: function () {
  345. var b = make_point(3, 4);
  346. function print(o) {
  347. helpers.writeln(o.x + ", " + o.y);
  348. }
  349. var x = {};
  350. helpers.writeln("Before change");
  351. print(x);
  352. helpers.writeln("After change");
  353. x.__proto__ = b; // This should mark "b" as a prototype object
  354. print(x);
  355. //
  356. // Now we make certain change to "b". If b is marked on prototype correctly, the following should invalidate related proto cache.
  357. //
  358. helpers.writeln("After change proto property");
  359. Object.defineProperty(b, "x", { get: function () { return 9; }, enumerable: true, configurable: true });
  360. print(x);
  361. }
  362. },
  363. {
  364. name: "Verify changing __proto__ works safely with ObjTypeSpec",
  365. body: function () {
  366. function f(o, a) {
  367. var m = o.x;
  368. if (a > 0)
  369. {
  370. o.__proto__ = {}; // ObjTypeSpec won't generate 2nd type check for the next "o.x" load. But implicit call bailout should bailout right here.
  371. }
  372. var n = o.x;
  373. assert.areEqual(1, m);
  374. assert.areEqual(a > 0 ? undefined : 1, n);
  375. }
  376. var p = { x: 1 };
  377. var o = Object.create(p);
  378. f(o, 0); // no __proto__ change in interpreter.
  379. f(o, 0); // -maxInterpretCount:1, still no __proto__ change in jit.
  380. f(o, 1); // -maxInterpretCount:1, now with __proto__ change in jit.
  381. }
  382. },
  383. {
  384. name: "Verify PathTypeHandler successor Types continue to work, case 1",
  385. body: function () {
  386. function f() {
  387. return { x_100: 1 }; // Use x_100 to start a unique path
  388. }
  389. var o1 = f();
  390. o1.__proto__ = { a: 2 };
  391. o1.y = 1; // This has path "x" -> "x, y"
  392. var o2 = f();
  393. o2.y = 1; // This also has path "x" -> "x, y"
  394. // But o1 and o2 can't go to the same Type. If they do, they have the same [[prototype]], which is wrong.
  395. assert.areEqual(2, o1.a, "from prototype");
  396. assert.areEqual(undefined, o2.a, "should be undefined");
  397. }
  398. },
  399. {
  400. name: "Verify PathTypeHandler successor Types continue to work, case 2",
  401. body: function () {
  402. function f() {
  403. return { x_101: 1 }; // Use x_101 to start a unique path
  404. }
  405. var o1 = f();
  406. o1.y = 1; // This has path "x" -> "x, y"
  407. var o2 = f();
  408. o2.__proto__ = { a: 2 };
  409. o2.y = 1; // This also has path "x" -> "x, y"
  410. // But o1 and o2 can't go to the same Type. If they do, they have the same [[prototype]], which is wrong.
  411. assert.areEqual(undefined, o1.a, "should be undefined");
  412. assert.areEqual(2, o2.a, "from prototype");
  413. }
  414. },
  415. {
  416. name: "Verify that we can shadow __proto__ property",
  417. body: function () {
  418. var p = {};
  419. var o = {};
  420. o.__proto__ = p;
  421. Object.defineProperty(p, "__proto__", { value: 10, writable: true, enumerable: true, configurable: true });
  422. assert.areEqual(10, o.__proto__);
  423. o.__proto__ = -100;
  424. assert.areEqual(-100, o.__proto__);
  425. delete o.__proto__;
  426. assert.areEqual(10, o.__proto__);
  427. delete p.__proto__;
  428. assert.areEqual(p, o.__proto__);
  429. assert.areEqual(Object.prototype, p.__proto__);
  430. o.__proto__ = { a: 123 };
  431. assert.areEqual(123, o.a);
  432. }
  433. },
  434. {
  435. name: "Test fast path o[i] with changed prototype",
  436. body: function () {
  437. var o = []; o.length = 10;
  438. var p = [123];
  439. o.__proto__ = p; // Now o's prototype != Array.prototype
  440. assert.areEqual(123, o[0]);
  441. var p0 = { "1": 4 };
  442. p.__proto__ = p0; // Now p's prototype != Object.prototype
  443. assert.areEqual(4, o[1]);
  444. p0.__proto__ = null;
  445. assert.areEqual(undefined, o[2]);
  446. }
  447. },
  448. {
  449. name: "Test fast path o[i] when o.__proto__ == null",
  450. body: function () {
  451. var o = [];
  452. o.__proto__ = null;
  453. assert.areEqual(undefined, o[1]);
  454. }
  455. },
  456. {
  457. name: "Test fast path o[i] when o.__proto__.__proto__ == null",
  458. body: function () {
  459. var o = [];
  460. o.__proto__ = [12, 34];
  461. o.__proto__.__proto__ = null;
  462. assert.areEqual(undefined, o[5]);
  463. }
  464. },
  465. {
  466. name: "Test Array methods with changed prototype",
  467. body: function () {
  468. function make_array() {
  469. var arr = [];
  470. for (var p in arguments) {
  471. var i = arguments[p];
  472. arr[i] = i;
  473. }
  474. return arr;
  475. }
  476. var o = []; o.length = 10;
  477. o.__proto__ = make_array(0, 1, 2);
  478. o.__proto__.__proto__ = make_array(3, 4);
  479. o.__proto__.__proto__.__proto__ = { "5": 5, "6": 6 };
  480. o.__proto__.__proto__.__proto__.__proto__ = Array.prototype;
  481. var a = o.slice();
  482. assert.areEqual("0,1,2,3,4,5,6,,,", a.toString());
  483. a.__proto__ = make_array(8, 9);
  484. assert.areEqual("0,1,2,3,4,5,6,,8,9", a.toString());
  485. }
  486. },
  487. {
  488. name: "Test cross-site change prototype",
  489. body: function () {
  490. if (this.WScript.LoadScriptFile) {
  491. var eng = make_engine();
  492. var other = eng.eval('var o = { get name() {return this.v}, set name(value) {this.v = value}, v: "a name" }; o');
  493. var o = { __proto__: other };
  494. assert.areEqual("a name", o.name);
  495. }
  496. }
  497. },
  498. {
  499. name: "Test change prototype of global object",
  500. body: function () {
  501. if (this.WScript.LoadScriptFile) {
  502. make_engine().run(function () {
  503. function test(x) {
  504. var p = { a: 1 };
  505. // Should go through general change prototype code path and mark "p" as prototype. Then changes to "p" should invalidate proto cache.
  506. x.__proto__ = p;
  507. assert.areEqual(1, x.a);
  508. Object.defineProperty(p, "a", { get: function () { return 2; }, configurable: true });
  509. assert.areEqual(2, x.a);
  510. }
  511. assert.isTrue(__proto__ === Object.prototype); // default binds to global this.__proto__
  512. test(this);
  513. __proto__ = { b: 3, __proto__: __proto__ };
  514. assert.areEqual(2, a);
  515. assert.areEqual(3, b);
  516. });
  517. }
  518. }
  519. },
  520. {
  521. name: "Blue 62526: __proto__: ArrayElementEnumerator does not expect non-TypeIds_Object on prototype",
  522. body: function () {
  523. var a = new String();
  524. a[1] = "a1";
  525. a[2] = "a2";
  526. a.__proto__ = [];
  527. var b = [0];
  528. b.__proto__ = a;
  529. b.length = 5;
  530. b = b.concat([]);
  531. assert.areEqual('[0,"a1","a2",null,null]', JSON.stringify(b));
  532. }
  533. },
  534. {
  535. name: "Blue 114364: __proto__: Object.preventExtensions should make [[prototype]] immutable",
  536. body: function () {
  537. var a = {};
  538. Object.preventExtensions(a);
  539. assert.throws(
  540. function () { a.__proto__ = { x: 1 } },
  541. TypeError);
  542. assert.isTrue(a.__proto__ === Object.prototype, "__proto__ should remain unchanged");
  543. }
  544. },
  545. {
  546. name: "Blue 245453: __proto__: Invalid has-only-writable-data-property cache",
  547. body: function () {
  548. var a = { __proto__: {} };
  549. a.x = 0; // populate cache -- whole prototype chain has only writable data properties
  550. a.__proto__.__proto__ = /a_regex/;
  551. a.source = 1;
  552. assert.areEqual("a_regex", a.source, "prototype chain should NOT has-only-writable-data-properties");
  553. }
  554. },
  555. {
  556. name: "Blue 245453: __proto__: Invalid has-only-writable-data-property cache -- verify cross-context",
  557. body: function () {
  558. if (this.WScript.LoadScriptFile) {
  559. var eng = make_engine();
  560. var other = eng.eval('({b: 1})');
  561. var a = { __proto__: other };
  562. assert.areEqual(1, a.b, "a.b from prototype");
  563. a.x = 0; // try to populate cache -- prototype chain has only writable data properties -- our code actually would not cache cross-context
  564. Object.defineProperty(other, "y", { value: 2, enumerable: true, writable: false });
  565. a.y = 1234;
  566. assert.areEqual(2, a.y, "prototype chain should NOT be has-only-writable-data-properties");
  567. }
  568. }
  569. },
  570. ];
  571. } else {
  572. tests = [
  573. {
  574. name: "Test Object.prototype.__proto__",
  575. body: function () {
  576. assert.isTrue(Object.prototype.__proto__ === undefined, "Object.prototype.__proto__ only supported on IE11 or later");
  577. assert.isTrue(Object.hasOwnProperty("getPrototypeOf") && !Object.hasOwnProperty("setPrototypeOf"), "Object.setPrototypeOf only supported on IE11 or later");
  578. var o = { __proto__: { pp: 123 } }; // compat mode: not working in object literal
  579. assert.areEqual(undefined, o.pp);
  580. }
  581. }
  582. ];
  583. }
  584. testRunner.run(tests);