string-basic.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. String.prototype[5] = "five";
  2. String.prototype.foo = "foo";
  3. var lastSetThisValue = null;
  4. class TerribleClass {
  5. get #privateStrict() {
  6. "use strict";
  7. lastSetThisValue = this;
  8. }
  9. get #privateNonStrict() {
  10. lastSetThisValue = this;
  11. }
  12. get 10() {
  13. "use strict";
  14. return this.#privateStrict;
  15. }
  16. get 11() {
  17. return this.#privateNonStrict;
  18. }
  19. set 12(v) {
  20. "use strict";
  21. return this.#privateStrict;
  22. }
  23. set 13(v) {
  24. return this.#privateNonStrict;
  25. }
  26. get strictGetPrivate() {
  27. "use strict";
  28. return this.#privateStrict;
  29. }
  30. get nonStrictGetPrivate() {
  31. return this.#privateNonStrict;
  32. }
  33. set strictSetPrivate(v) {
  34. "use strict";
  35. this.#privateStrict;
  36. }
  37. set nonStrictSetPrivate(v) {
  38. this.#privateNonStrict;
  39. }
  40. }
  41. String.prototype.__proto__ = {
  42. get nonStrictThis() {
  43. return this;
  44. },
  45. get strictThis() {
  46. "use strict";
  47. return this;
  48. },
  49. set setNonStrictThis(v) {
  50. lastSetThisValue = this;
  51. },
  52. set setStrictThis(v) {
  53. "use strict";
  54. lastSetThisValue = this;
  55. },
  56. get 6() {
  57. "use strict";
  58. return this;
  59. },
  60. get 7() {
  61. return this;
  62. },
  63. set 8(v) {
  64. "use strict";
  65. lastSetThisValue = this;
  66. },
  67. set 9(v) {
  68. lastSetThisValue = this;
  69. },
  70. };
  71. String.prototype.__proto__.__proto__ = new TerribleClass();
  72. test("primitive string: numeric indexing", () => {
  73. expect(""[0]).toBeUndefined();
  74. expect("foo"[0]).toBe("f");
  75. expect("foo"[2]).toBe("o");
  76. expect("foo"[3]).toBeUndefined();
  77. expect("foo"[-1]).toBeUndefined();
  78. expect("foo"[1.5]).toBeUndefined();
  79. expect("foo"[5]).toBe("five");
  80. expect(typeof "foo"[6]).toBe("string");
  81. expect("foo"[6]).toBe("foo");
  82. expect(typeof "foo"[7]).toBe("object");
  83. expect("foo"[7] instanceof String).toBeTrue();
  84. expect("foo"[7] !== "foo"[7]).toBeTrue();
  85. expect("foo"[7] !== String.prototype).toBeTrue();
  86. "foo"[8] = "test";
  87. expect(typeof lastSetThisValue).toBe("string");
  88. expect(lastSetThisValue).toBe("foo");
  89. lastSetThisValue = null;
  90. "foo"[9] = "test";
  91. expect(typeof lastSetThisValue).toBe("object");
  92. expect(lastSetThisValue instanceof String).toBeTrue();
  93. expect(lastSetThisValue !== String.prototype);
  94. let oldThisValue = lastSetThisValue;
  95. lastSetThisValue = null;
  96. "foo"[9] = "test";
  97. expect(lastSetThisValue !== oldThisValue).toBeTrue();
  98. lastSetThisValue = null;
  99. expect(() => "foo"[10]).toThrow();
  100. expect(lastSetThisValue).toBeNull();
  101. lastSetThisValue = null;
  102. expect(() => "foo"[11]).toThrow();
  103. expect(lastSetThisValue).toBeNull();
  104. lastSetThisValue = null;
  105. expect(() => ("foo"[12] = "wat")).toThrow();
  106. expect(lastSetThisValue).toBeNull();
  107. lastSetThisValue = null;
  108. expect(() => ("foo"[13] = "wat")).toThrow();
  109. expect(lastSetThisValue).toBeNull();
  110. lastSetThisValue = null;
  111. });
  112. test("primitive string: string property indexing", () => {
  113. expect(""["0"]).toBeUndefined();
  114. expect("foo"["0"]).toBe("f");
  115. expect("foo"["01"]).toBeUndefined();
  116. expect("foo"[" 1"]).toBeUndefined();
  117. expect("foo"["1 "]).toBeUndefined();
  118. expect("foo"["2"]).toBe("o");
  119. expect("foo"["3"]).toBeUndefined();
  120. expect("foo"["-1"]).toBeUndefined();
  121. expect("foo"["1.5"]).toBeUndefined();
  122. expect("foo"["5"]).toBe("five");
  123. expect(typeof "foo"["6"]).toBe("string");
  124. expect("foo"["6"]).toBe("foo");
  125. expect(typeof "foo"["7"]).toBe("object");
  126. expect("foo"["7"] instanceof String).toBeTrue();
  127. expect("foo"["7"] !== "foo"[7]).toBeTrue();
  128. expect("foo"["7"] !== String.prototope).toBeTrue();
  129. expect(""["length"]).toBe(0);
  130. expect("foo"["length"]).toBe(3);
  131. "foo"["8"] = "test";
  132. expect(typeof lastSetThisValue).toBe("string");
  133. expect(lastSetThisValue).toBe("foo");
  134. lastSetThisValue = null;
  135. "foo"["9"] = "test";
  136. expect(typeof lastSetThisValue).toBe("object");
  137. expect(lastSetThisValue instanceof String).toBeTrue();
  138. expect(lastSetThisValue !== String.prototype);
  139. let oldThisValue = lastSetThisValue;
  140. lastSetThisValue = null;
  141. "foo"["9"] = "test";
  142. expect(lastSetThisValue !== oldThisValue).toBeTrue();
  143. lastSetThisValue = null;
  144. expect(() => "foo"["10"]).toThrow();
  145. expect(lastSetThisValue).toBeNull();
  146. lastSetThisValue = null;
  147. expect(() => "foo"["11"]).toThrow();
  148. expect(lastSetThisValue).toBeNull();
  149. lastSetThisValue = null;
  150. expect(() => ("foo"["12"] = "wat")).toThrow();
  151. expect(lastSetThisValue).toBeNull();
  152. lastSetThisValue = null;
  153. expect(() => ("foo"["13"] = "wat")).toThrow();
  154. expect(lastSetThisValue).toBeNull();
  155. lastSetThisValue = null;
  156. });
  157. test("primitive string: string property name access", () => {
  158. expect("".length).toBe(0);
  159. expect("foo".length).toBe(3);
  160. expect("foo".bar).toBeUndefined();
  161. expect("foo".foo).toBe("foo");
  162. expect(typeof "foo".strictThis).toBe("string");
  163. expect("foo".strictThis).toBe("foo");
  164. expect(typeof "foo".nonStrictThis).toBe("object");
  165. expect("foo".nonStrictThis !== "foo".nonStrictThis).toBeTrue();
  166. expect("foo".nonStrictThis !== String.prototype).toBeTrue();
  167. expect("foo".nonStrictThis instanceof String).toBeTrue();
  168. let str = new String("foo");
  169. str.setStrictThis = "test";
  170. expect(typeof lastSetThisValue).toBe("object");
  171. expect(lastSetThisValue).toBe(str);
  172. lastSetThisValue = null;
  173. str.setNonStrictThis = "test";
  174. expect(typeof lastSetThisValue).toBe("object");
  175. expect(lastSetThisValue instanceof String).toBeTrue();
  176. expect(lastSetThisValue).toBe(str);
  177. let oldThisValue = lastSetThisValue;
  178. lastSetThisValue = null;
  179. str.setNonStrictThis = "test";
  180. expect(lastSetThisValue === oldThisValue).toBeTrue();
  181. lastSetThisValue = null;
  182. expect(() => "foo".strictGetPrivate).toThrow();
  183. expect(lastSetThisValue).toBeNull();
  184. lastSetThisValue = null;
  185. expect(() => "foo".nonStrictGetPrivate).toThrow();
  186. expect(lastSetThisValue).toBeNull();
  187. lastSetThisValue = null;
  188. expect(() => ("foo".strictSetPrivate = "wat")).toThrow();
  189. expect(lastSetThisValue).toBeNull();
  190. lastSetThisValue = null;
  191. expect(() => ("foo".nonStrictSetPrivate = "wat")).toThrow();
  192. expect(lastSetThisValue).toBeNull();
  193. lastSetThisValue = null;
  194. });
  195. test("string object: string numeric indexing", () => {
  196. expect(new String("")[0]).toBeUndefined();
  197. expect(new String("foo")[0]).toBe("f");
  198. expect(new String("foo")[2]).toBe("o");
  199. expect(new String("foo")[3]).toBeUndefined();
  200. expect(new String("foo")[-1]).toBeUndefined();
  201. expect(new String("foo")[1.5]).toBeUndefined();
  202. expect(new String("foo")[5]).toBe("five");
  203. expect(typeof new String("foo")[6]).toBe("object");
  204. let str = new String("foo");
  205. expect(str[7]).toBe(str);
  206. expect(typeof "foo"[7]).toBe("object");
  207. expect(str[7] instanceof String).toBeTrue();
  208. expect(str[7]).toBe(str);
  209. expect(str[7] !== String.prototope).toBeTrue();
  210. str["8"] = "test";
  211. expect(typeof lastSetThisValue).toBe("object");
  212. expect(lastSetThisValue).toBe(str);
  213. lastSetThisValue = null;
  214. str["9"] = "test";
  215. expect(typeof lastSetThisValue).toBe("object");
  216. expect(lastSetThisValue instanceof String).toBeTrue();
  217. expect(lastSetThisValue).toBe(str);
  218. let oldThisValue = lastSetThisValue;
  219. lastSetThisValue = null;
  220. str["9"] = "test";
  221. expect(lastSetThisValue === oldThisValue).toBeTrue();
  222. lastSetThisValue = null;
  223. });
  224. test("string object: string property indexing", () => {
  225. expect(new String("")["0"]).toBeUndefined();
  226. expect(new String("foo")["0"]).toBe("f");
  227. expect(new String("foo")["2"]).toBe("o");
  228. expect(new String("foo")["3"]).toBeUndefined();
  229. expect(new String("foo")["-1"]).toBeUndefined();
  230. expect(new String("foo")["1.5"]).toBeUndefined();
  231. expect(new String("foo")["5"]).toBe("five");
  232. expect(typeof new String("foo")["6"]).toBe("object");
  233. let str = new String("foo");
  234. expect(str["7"]).toBe(str);
  235. expect(typeof "foo"["7"]).toBe("object");
  236. expect(str["7"] instanceof String).toBeTrue();
  237. expect(str["7"]).toBe(str);
  238. expect(str["7"] !== String.prototope).toBeTrue();
  239. str["setStrictThis"] = "test";
  240. expect(typeof lastSetThisValue).toBe("object");
  241. expect(lastSetThisValue).toBe(str);
  242. lastSetThisValue = null;
  243. str["setNonStrictThis"] = "test";
  244. expect(typeof lastSetThisValue).toBe("object");
  245. expect(lastSetThisValue instanceof String).toBeTrue();
  246. expect(lastSetThisValue).toBe(str);
  247. let oldThisValue = lastSetThisValue;
  248. lastSetThisValue = null;
  249. str["setNonStrictThis"] = "test";
  250. expect(lastSetThisValue === oldThisValue).toBeTrue();
  251. lastSetThisValue = null;
  252. expect(() => str.strictGetPrivate).toThrow();
  253. expect(lastSetThisValue).toBeNull();
  254. lastSetThisValue = null;
  255. expect(() => str.nonStrictGetPrivate).toThrow();
  256. expect(lastSetThisValue).toBeNull();
  257. lastSetThisValue = null;
  258. expect(() => (str.strictSetPrivate = "wat")).toThrow();
  259. expect(lastSetThisValue).toBeNull();
  260. lastSetThisValue = null;
  261. expect(() => (str.nonStrictSetPrivate = "wat")).toThrow();
  262. expect(lastSetThisValue).toBeNull();
  263. lastSetThisValue = null;
  264. });
  265. test("string object: string property name access", () => {
  266. expect(new String("").length).toBe(0);
  267. expect(new String("foo").length).toBe(3);
  268. expect(new String("foo").bar).toBeUndefined();
  269. expect(new String("foo").foo).toBe("foo");
  270. expect(typeof new String("foo").strictThis).toBe("object");
  271. let str = new String("foo");
  272. expect(str.strictThis).toBe(str);
  273. expect(typeof str.nonStrictThis).toBe("object");
  274. expect(str.nonStrictThis === str.nonStrictThis).toBeTrue();
  275. expect(str.nonStrictThis !== String.prototype).toBeTrue();
  276. expect(str.nonStrictThis instanceof String).toBeTrue();
  277. expect(new String("foo").nonStrictThis !== new String("foo").nonStrictThis).toBeTrue();
  278. expect(new String("foo").strictThis !== new String("foo").strictThis).toBeTrue();
  279. lastSetThisValue = null;
  280. expect(() => str.strictGetPrivate).toThrow();
  281. expect(lastSetThisValue).toBeNull();
  282. lastSetThisValue = null;
  283. expect(() => str.nonStrictGetPrivate).toThrow();
  284. expect(lastSetThisValue).toBeNull();
  285. lastSetThisValue = null;
  286. expect(() => (str.strictSetPrivate = "wat")).toThrow();
  287. expect(lastSetThisValue).toBeNull();
  288. lastSetThisValue = null;
  289. expect(() => (str.nonStrictSetPrivate = "wat")).toThrow();
  290. expect(lastSetThisValue).toBeNull();
  291. lastSetThisValue = null;
  292. });