Debian Codegraph @

js/src/tests/non262/template-strings/tagTempl.js

raw
1 // This Source Code Form is subject to the terms of the Mozilla Public
2 // License, v. 2.0. If a copy of the MPL was not distributed with this
3 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
4
5 // This test case is weird in the sense the actual work happens at the eval
6 // at the end. If template strings are not enabled, the test cases would throw
7 // a syntax error and we'd have failure reported. To avoid that, the entire
8 // test case is commented out and is part of a function. We use toString to
9 // get the string version, obtain the actual lines to run, and then use eval to
10 // do the actual evaluation.
11
12 function syntaxError (script) {
13 try {
14 Function(script);
15 } catch (e) {
16 if (e.name === "SyntaxError") {
17 return;
18 }
19 }
20 throw new Error('Expected syntax error: ' + script);
21 }
22
23 // function definitions
24 function check(actual, expected) {
25 assertEq(actual.length, expected.length);
26 for (var i = 0; i < expected.length; i++)
27 assertEq(actual[i], expected[i]);
28 }
29
30 function cooked(cs) { return cs; }
31 function raw(cs) { return cs.raw; }
32 function args(cs, ...rest) { return rest; }
33
34
35 // TEST BEGIN
36
37 // Literals
38 check(raw``, [""]);
39 check(raw`${4}a`, ["","a"]);
40 check(raw`${4}`, ["",""]);
41 check(raw`a${4}`, ["a",""]);
42 check(raw`a${4}b`, ["a","b"]);
43 check(raw`a${4}b${3}`, ["a","b",""]);
44 check(raw`a${4}b${3}c`, ["a","b","c"]);
45 check(raw`a${4}${3}c`, ["a","","c"]);
46 check(raw`${4}${3}`, ["","",""]);
47 check(raw`${4}\r\r${3}`, ["","\\r\\r",""]);
48 check(raw`${4}${3}c`, ["","","c"]);
49 check(raw`zyx${4}wvut${3}c`, ["zyx","wvut","c"]);
50 check(raw`zyx${4}wvut${3}\r\n`, ["zyx","wvut","\\r\\n"]);
51
52 check(raw`hey`, ["hey"]);
53 check(raw`he\r\ny`, ["he\\r\\ny"]);
54 check(raw`he\ry`, ["he\\ry"]);
55 check(raw`he\r\ry`, ["he\\r\\ry"]);
56 check(raw`he\ny`, ["he\\ny"]);
57 check(raw`he\n\ny`, ["he\\n\\ny"]);
58
59 check(cooked`hey`, ["hey"]);
60 check(cooked`he\r\ny`, ["he\r\ny"]);
61 check(cooked`he\ry`, ["he\ry"]);
62 check(cooked`he\r\ry`, ["he\r\ry"]);
63 check(cooked`he\ny`, ["he\ny"]);
64 check(cooked`he\n\ny`, ["he\n\ny"]);
65
66 check(eval("raw`\r`"), ["\n"]);
67 check(eval("raw`\r\n`"), ["\n"]);
68 check(eval("raw`\r\r\n`"), ["\n\n"]);
69 check(eval("raw`he\r\ny`"), ["he\ny"]);
70 check(eval("raw`he\ry`"), ["he\ny"]);
71 check(eval("raw`he\r\ry`"), ["he\n\ny"]);
72 check(eval("raw`he\r\r\ny`"), ["he\n\ny"]);
73
74
75 check(eval("cooked`\r`"), ["\n"]);
76 check(eval("cooked`\r\n`"), ["\n"]);
77 check(eval("cooked`\r\r\n`"), ["\n\n"]);
78 check(eval("cooked`he\r\ny`"), ["he\ny"]);
79 check(eval("cooked`he\ry`"), ["he\ny"]);
80 check(eval("cooked`he\r\ry`"), ["he\n\ny"]);
81 check(eval("cooked`he\r\r\ny`"), ["he\n\ny"]);
82
83 // Expressions
84 check(args`hey${"there"}now`, ["there"]);
85 check(args`hey${4}now`, [4]);
86 check(args`hey${4}`, [4]);
87 check(args`${4}`, [4]);
88 check(args`${4}${5}`, [4,5]);
89 check(args`a${4}${5}`, [4,5]);
90 check(args`a${4}b${5}`, [4,5]);
91 check(args`a${4}b${5}c`, [4,5]);
92 check(args`${4}b${5}c`, [4,5]);
93 check(args`${4}${5}c`, [4,5]);
94
95 var a = 10;
96 var b = 15;
97 check(args`${4 + a}${5 + b}c`, [14,20]);
98 check(args`${4 + a}${a + b}c`, [14,25]);
99 check(args`${b + a}${5 + b}c`, [25,20]);
100 check(args`${4 + a}${a + b}c${"a"}`, [14,25,"a"]);
101 check(args`a${"b"}${"c"}${"d"}`, ["b","c","d"]);
102 check(args`a${"b"}${"c"}${a + b}`, ["b","c",25]);
103 check(args`a${"b"}`, ["b"]);
104
105 // Expressions - complex substitutions
106 check(args`${`hey ${b + a} there`}${5 + b}c`, ["hey 25 there",20]);
107 check(args`${`hey ${`my ${b + a} good`} there`}${5 + b}c`, ["hey my 25 good there",20]);
108
109 syntaxError("args`${}`");
110 syntaxError("args`${`");
111 syntaxError("args`${\\n}`");
112 syntaxError("args`${yield 0}`");
113 syntaxError("args`");
114 syntaxError("args`$");
115 syntaxError("args`${");
116 syntaxError("args.``");
117
118 // Template substitution tests in the context of tagged templates
119 // Extra whitespace inside a template substitution is ignored.
120 check(args`a${
121 0
122 }`, [0]);
123
124 // Extra whitespace between tag and template is ignored
125 check(args
126 `a
127 ${
128 0
129 }`, [0]);
130
131
132 check(args`${5}${ // Comments work in template substitutions.
133 // Even comments that look like code:
134 // 0}`, "FAIL"); /* NOTE: This whole line is a comment.
135 0}`, [5,0]);
136
137 check(args // Comments work in template substitutions.
138 // Even comments that look like code:
139 // 0}`, "FAIL"); /* NOTE: This whole line is a comment.
140 `${5}${0}`, [5,0]);
141
142
143 // Template substitutions are expressions, not statements.
144 syntaxError("args`${0;}`");
145 check(args`${
146 function f() {
147 return "ok";
148 }()
149 }`, ["ok"]);
150
151 // Template substitutions can have side effects.
152 var x = 0;
153 check(args`${x += 1}`, [1]);
154 assertEq(x, 1);
155
156 // The production for a template substitution is Expression, not
157 // AssignmentExpression.
158 x = 0;
159 check(args`${++x, "o"}k`, ["o"]);
160 assertEq(x, 1);
161
162
163 // --> is not a comment inside a template.
164 check(cooked`
165 --> this is text
166 `, ["\n--> this is text\n"]);
167
168 // reentrancy
169 function f(n) {
170 if (n === 0)
171 return "";
172 var res = args`${n}${f(n - 1)}`;
173 return res[0] + res[1] + "";
174 }
175 assertEq(f(9), "987654321");
176
177 // Template string substitutions in generator functions can yield.
178 function* g() {
179 var res = args`${yield 1} ${yield 2}`;
180 return res[0] + res[1] + "";
181 }
182
183 var it = g();
184 var next = it.next();
185 assertEq(next.done, false);
186 assertEq(next.value, 1);
187 next = it.next("hello");
188 assertEq(next.done, false);
189 assertEq(next.value, 2);
190 next = it.next("world");
191 assertEq(next.done, true);
192 assertEq(next.value, "helloworld");
193
194 // undefined
195 assertEq(args`${void 0}`[0] + "", "undefined");
196 assertEq(args`${Object.doesNotHaveThisProperty}`[0] + "", "undefined");
197
198 var callSiteObj = [];
199 callSiteObj[0] = cooked`aa${4}bb`;
200 for (var i = 1; i < 3; i++)
201 callSiteObj[i] = cooked`aa${4}bb`;
202
203 // Same call site object behavior
204 assertEq(callSiteObj[1], callSiteObj[2]);
205 // Template objects are canonicalized
206 assertEq(callSiteObj[0] !== callSiteObj[1], true);
207 assertEq("raw" in callSiteObj[0], true);
208
209 // Array length
210 assertEq(callSiteObj[0].raw.length, 2);
211 assertEq(callSiteObj[0].length, 2);
212
213 // Frozen objects
214 assertEq(Object.isFrozen(callSiteObj[0]), true);
215 assertEq(Object.isFrozen(callSiteObj[0].raw), true);
216
217 // Raw not enumerable
218 assertEq(callSiteObj[0].propertyIsEnumerable(callSiteObj[0].raw), false);
219
220 // Allow call syntax
221 check(new ((cs, sub) => function(){ return sub }) `${[1, 2, 3]}`, [1,2,3]);
222
223 var a = [];
224 function test() {
225 var x = callSite => callSite;
226 for (var i = 0; i < 2; i++)
227 a[i] = eval("x``");
228 }
229 test();
230 assertEq(a[0] !== a[1], true);
231
232 // Test that |obj.method`template`| works
233 var newObj = {
234 methodRetThis : function () {
235 return this;
236 },
237 methodRetCooked : function (a) {
238 return a;
239 },
240 methodRetRaw : function (a) {
241 return a.raw;
242 },
243 methodRetArgs : function (a, ...args) {
244 return args;
245 }
246 }
247
248 assertEq(newObj.methodRetThis`abc${4}`, newObj);
249 check(newObj.methodRetCooked`abc${4}\r`, ["abc","\r"]);
250 check(eval("newObj.methodRetCooked`abc${4}\r`"), ["abc","\n"]);
251 check(newObj.methodRetRaw`abc${4}\r`, ["abc","\\r"]);
252 check(eval("newObj.methodRetRaw`abc${4}\r`"), ["abc","\n"]);
253 check(eval("newObj.methodRetArgs`abc${4}${5}\r${6}`"), [4,5,6]);
254
255 // Chained calls
256 function func(a) {
257 if (a[0] === "hey") {
258 return function(a) {
259 if (a[0] === "there") {
260 return function(a) {
261 if (a[0] === "mine")
262 return "was mine";
263 else
264 return "was not mine";
265 }
266 } else {
267 return function(a) {
268 return "was not there";
269 }
270 }
271 }
272 } else {
273 return function(a) {
274 return function(a) {
275 return "was not hey";
276 }
277 }
278 }
279 }
280
281 assertEq(func`hey``there``mine`, "was mine");
282 assertEq(func`hey``there``amine`, "was not mine");
283 assertEq(func`hey``tshere``amine`, "was not there");
284 assertEq(func`heys``there``mine`, "was not hey");
285
286 // String.raw
287 assertEq(String.raw`h\r\ney${4}there\n`, "h\\r\\ney4there\\n");
288 assertEq(String.raw`hey`, "hey");
289 assertEq(String.raw``, "");
290
291 // Invalid escape sequences
292 check(raw`\01`, ["\\01"]);
293 check(raw`\01${0}right`, ["\\01","right"]);
294 check(raw`left${0}\01`, ["left","\\01"]);
295 check(raw`left${0}\01${1}right`, ["left","\\01","right"]);
296 check(raw`\1`, ["\\1"]);
297 check(raw`\1${0}right`, ["\\1","right"]);
298 check(raw`left${0}\1`, ["left","\\1"]);
299 check(raw`left${0}\1${1}right`, ["left","\\1","right"]);
300 check(raw`\xg`, ["\\xg"]);
301 check(raw`\xg${0}right`, ["\\xg","right"]);
302 check(raw`left${0}\xg`, ["left","\\xg"]);
303 check(raw`left${0}\xg${1}right`, ["left","\\xg","right"]);
304 check(raw`\xAg`, ["\\xAg"]);
305 check(raw`\xAg${0}right`, ["\\xAg","right"]);
306 check(raw`left${0}\xAg`, ["left","\\xAg"]);
307 check(raw`left${0}\xAg${1}right`, ["left","\\xAg","right"]);
308 check(raw`\u0`, ["\\u0"]);
309 check(raw`\u0${0}right`, ["\\u0","right"]);
310 check(raw`left${0}\u0`, ["left","\\u0"]);
311 check(raw`left${0}\u0${1}right`, ["left","\\u0","right"]);
312 check(raw`\u0g`, ["\\u0g"]);
313 check(raw`\u0g${0}right`, ["\\u0g","right"]);
314 check(raw`left${0}\u0g`, ["left","\\u0g"]);
315 check(raw`left${0}\u0g${1}right`, ["left","\\u0g","right"]);
316 check(raw`\u00g`, ["\\u00g"]);
317 check(raw`\u00g${0}right`, ["\\u00g","right"]);
318 check(raw`left${0}\u00g`, ["left","\\u00g"]);
319 check(raw`left${0}\u00g${1}right`, ["left","\\u00g","right"]);
320 check(raw`\u000g`, ["\\u000g"]);
321 check(raw`\u000g${0}right`, ["\\u000g","right"]);
322 check(raw`left${0}\u000g`, ["left","\\u000g"]);
323 check(raw`left${0}\u000g${1}right`, ["left","\\u000g","right"]);
324 check(raw`\u{}`, ["\\u{}"]);
325 check(raw`\u{}${0}right`, ["\\u{}","right"]);
326 check(raw`left${0}\u{}`, ["left","\\u{}"]);
327 check(raw`left${0}\u{}${1}right`, ["left","\\u{}","right"]);
328 check(raw`\u{-0}`, ["\\u{-0}"]);
329 check(raw`\u{-0}${0}right`, ["\\u{-0}","right"]);
330 check(raw`left${0}\u{-0}`, ["left","\\u{-0}"]);
331 check(raw`left${0}\u{-0}${1}right`, ["left","\\u{-0}","right"]);
332 check(raw`\u{g}`, ["\\u{g}"]);
333 check(raw`\u{g}${0}right`, ["\\u{g}","right"]);
334 check(raw`left${0}\u{g}`, ["left","\\u{g}"]);
335 check(raw`left${0}\u{g}${1}right`, ["left","\\u{g}","right"]);
336 check(raw`\u{0`, ["\\u{0"]);
337 check(raw`\u{0${0}right`, ["\\u{0","right"]);
338 check(raw`left${0}\u{0`, ["left","\\u{0"]);
339 check(raw`left${0}\u{0${1}right`, ["left","\\u{0","right"]);
340 check(raw`\u{\u{0}`, ["\\u{\\u{0}"]);
341 check(raw`\u{\u{0}${0}right`, ["\\u{\\u{0}","right"]);
342 check(raw`left${0}\u{\u{0}`, ["left","\\u{\\u{0}"]);
343 check(raw`left${0}\u{\u{0}${1}right`, ["left","\\u{\\u{0}","right"]);
344 check(raw`\u{110000}`, ["\\u{110000}"]);
345 check(raw`\u{110000}${0}right`, ["\\u{110000}","right"]);
346 check(raw`left${0}\u{110000}`, ["left","\\u{110000}"]);
347 check(raw`left${0}\u{110000}${1}right`, ["left","\\u{110000}","right"]);
348
349 check(cooked`\01`, [void 0]);
350 check(cooked`\01${0}right`, [void 0,"right"]);
351 check(cooked`left${0}\01`, ["left",void 0]);
352 check(cooked`left${0}\01${1}right`, ["left",void 0,"right"]);
353 check(cooked`\1`, [void 0]);
354 check(cooked`\1${0}right`, [void 0,"right"]);
355 check(cooked`left${0}\1`, ["left",void 0]);
356 check(cooked`left${0}\1${1}right`, ["left",void 0,"right"]);
357 check(cooked`\xg`, [void 0]);
358 check(cooked`\xg${0}right`, [void 0,"right"]);
359 check(cooked`left${0}\xg`, ["left",void 0]);
360 check(cooked`left${0}\xg${1}right`, ["left",void 0,"right"]);
361 check(cooked`\xAg`, [void 0]);
362 check(cooked`\xAg${0}right`, [void 0,"right"]);
363 check(cooked`left${0}\xAg`, ["left",void 0]);
364 check(cooked`left${0}\xAg${1}right`, ["left",void 0,"right"]);
365 check(cooked`\u0`, [void 0]);
366 check(cooked`\u0${0}right`, [void 0,"right"]);
367 check(cooked`left${0}\u0`, ["left",void 0]);
368 check(cooked`left${0}\u0${1}right`, ["left",void 0,"right"]);
369 check(cooked`\u0g`, [void 0]);
370 check(cooked`\u0g${0}right`, [void 0,"right"]);
371 check(cooked`left${0}\u0g`, ["left",void 0]);
372 check(cooked`left${0}\u0g${1}right`, ["left",void 0,"right"]);
373 check(cooked`\u00g`, [void 0]);
374 check(cooked`\u00g${0}right`, [void 0,"right"]);
375 check(cooked`left${0}\u00g`, ["left",void 0]);
376 check(cooked`left${0}\u00g${1}right`, ["left",void 0,"right"]);
377 check(cooked`\u000g`, [void 0]);
378 check(cooked`\u000g${0}right`, [void 0,"right"]);
379 check(cooked`left${0}\u000g`, ["left",void 0]);
380 check(cooked`left${0}\u000g${1}right`, ["left",void 0,"right"]);
381 check(cooked`\u{}`, [void 0]);
382 check(cooked`\u{}${0}right`, [void 0,"right"]);
383 check(cooked`left${0}\u{}`, ["left",void 0]);
384 check(cooked`left${0}\u{}${1}right`, ["left",void 0,"right"]);
385 check(cooked`\u{-0}`, [void 0]);
386 check(cooked`\u{-0}${0}right`, [void 0,"right"]);
387 check(cooked`left${0}\u{-0}`, ["left",void 0]);
388 check(cooked`left${0}\u{-0}${1}right`, ["left",void 0,"right"]);
389 check(cooked`\u{g}`, [void 0]);
390 check(cooked`\u{g}${0}right`, [void 0,"right"]);
391 check(cooked`left${0}\u{g}`, ["left",void 0]);
392 check(cooked`left${0}\u{g}${1}right`, ["left",void 0,"right"]);
393 check(cooked`\u{0`, [void 0]);
394 check(cooked`\u{0${0}right`, [void 0,"right"]);
395 check(cooked`left${0}\u{0`, ["left",void 0]);
396 check(cooked`left${0}\u{0${1}right`, ["left",void 0,"right"]);
397 check(cooked`\u{\u{0}`, [void 0]);
398 check(cooked`\u{\u{0}${0}right`, [void 0,"right"]);
399 check(cooked`left${0}\u{\u{0}`, ["left",void 0]);
400 check(cooked`left${0}\u{\u{0}${1}right`, ["left",void 0,"right"]);
401 check(cooked`\u{110000}`, [void 0]);
402 check(cooked`\u{110000}${0}right`, [void 0,"right"]);
403 check(cooked`left${0}\u{110000}`, ["left",void 0]);
404 check(cooked`left${0}\u{110000}${1}right`, ["left",void 0,"right"]);
405
406 syntaxError("`\\01`");
407 syntaxError("`\\01${0}right`");
408 syntaxError("`left${0}\\01`");
409 syntaxError("`left${0}\\01${1}right`");
410 syntaxError("`\\1`");
411 syntaxError("`\\1${0}right`");
412 syntaxError("`left${0}\\1`");
413 syntaxError("`left${0}\\1${1}right`");
414 syntaxError("`\\xg`");
415 syntaxError("`\\xg${0}right`");
416 syntaxError("`left${0}\\xg`");
417 syntaxError("`left${0}\\xg${1}right`");
418 syntaxError("`\\xAg`");
419 syntaxError("`\\xAg${0}right`");
420 syntaxError("`left${0}\\xAg`");
421 syntaxError("`left${0}\\xAg${1}right`");
422 syntaxError("`\\u0`");
423 syntaxError("`\\u0${0}right`");
424 syntaxError("`left${0}\\u0`");
425 syntaxError("`left${0}\\u0${1}right`");
426 syntaxError("`\\u0g`");
427 syntaxError("`\\u0g${0}right`");
428 syntaxError("`left${0}\\u0g`");
429 syntaxError("`left${0}\\u0g${1}right`");
430 syntaxError("`\\u00g`");
431 syntaxError("`\\u00g${0}right`");
432 syntaxError("`left${0}\\u00g`");
433 syntaxError("`left${0}\\u00g${1}right`");
434 syntaxError("`\\u000g`");
435 syntaxError("`\\u000g${0}right`");
436 syntaxError("`left${0}\\u000g`");
437 syntaxError("`left${0}\\u000g${1}right`");
438 syntaxError("`\\u{}`");
439 syntaxError("`\\u{}${0}right`");
440 syntaxError("`left${0}\\u{}`");
441 syntaxError("`left${0}\\u{}${1}right`");
442 syntaxError("`\\u{-0}`");
443 syntaxError("`\\u{-0}${0}right`");
444 syntaxError("`left${0}\\u{-0}`");
445 syntaxError("`left${0}\\u{-0}${1}right`");
446 syntaxError("`\\u{g}`");
447 syntaxError("`\\u{g}${0}right`");
448 syntaxError("`left${0}\\u{g}`");
449 syntaxError("`left${0}\\u{g}${1}right`");
450 syntaxError("`\\u{0`");
451 syntaxError("`\\u{0${0}right`");
452 syntaxError("`left${0}\\u{0`");
453 syntaxError("`left${0}\\u{0${1}right`");
454 syntaxError("`\\u{\\u{0}`");
455 syntaxError("`\\u{\\u{0}${0}right`");
456 syntaxError("`left${0}\\u{\\u{0}`");
457 syntaxError("`left${0}\\u{\\u{0}${1}right`");
458 syntaxError("`\\u{110000}`");
459 syntaxError("`\\u{110000}${0}right`");
460 syntaxError("`left${0}\\u{110000}`");
461 syntaxError("`left${0}\\u{110000}${1}right`");
462
463
464 reportCompare(0, 0, "ok");