js/src/jit-test/tests/basic/spread-call-funapply.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 load(libdir + "asserts.js");
michael@0 2 load(libdir + "eqArrayHelper.js");
michael@0 3 load(libdir + "iteration.js");
michael@0 4
michael@0 5 function checkCommon(f) {
michael@0 6 assertEqArray(f.apply(null, ...[[1, 2, 3]]), [1, 2, 3]);
michael@0 7 assertEqArray(f.apply(...[null], [1, 2, 3]), [1, 2, 3]);
michael@0 8 assertEqArray(f.apply(...[null], ...[[1, 2, 3]]), [1, 2, 3]);
michael@0 9 assertEqArray(f.apply(...[null, [1, 2, 3]]), [1, 2, 3]);
michael@0 10
michael@0 11 // other iterable objects
michael@0 12 assertEqArray(f.apply(...Set([null, [1, 2, 3]])), [1, 2, 3]);
michael@0 13 assertEqArray(f.apply(...[null, [1, 2, 3]][std_iterator]()), [1, 2, 3]);
michael@0 14 let itr = {};
michael@0 15 itr[std_iterator] = function() {
michael@0 16 return {
michael@0 17 i: 0,
michael@0 18 next: function() {
michael@0 19 this.i++;
michael@0 20 if (this.i == 1)
michael@0 21 return { value: null, done: false };
michael@0 22 else if (this.i == 2)
michael@0 23 return { value: [1, 2, 3], done: false };
michael@0 24 else
michael@0 25 return { value: undefined, done: true };
michael@0 26 }
michael@0 27 };
michael@0 28 };
michael@0 29 assertEqArray(f.apply(...itr), [1, 2, 3]);
michael@0 30 function* gen() {
michael@0 31 yield null;
michael@0 32 yield [1, 2, 3];
michael@0 33 }
michael@0 34 assertEqArray(f.apply(...gen()), [1, 2, 3]);
michael@0 35
michael@0 36 let a;
michael@0 37 assertEqArray(f.apply(null, ...a=[[1, 2, 3]]), [1, 2, 3]);
michael@0 38
michael@0 39 // According to the draft spec, null and undefined are to be treated as empty
michael@0 40 // arrays. However, they are not iterable. If the spec is not changed to be in
michael@0 41 // terms of iterables, these tests should be fixed.
michael@0 42 //assertEqArray(f.apply(null, ...null, [1, 2, 3]), [1, 2, 3]);
michael@0 43 //assertEqArray(f.apply(null, ...undefined, [1, 2, 3]), [1, 2, 3]);
michael@0 44 assertThrowsInstanceOf(() => f.apply(null, ...null, [1, 2, 3]), TypeError);
michael@0 45 assertThrowsInstanceOf(() => f.apply(null, ...undefined, [1, 2, 3]), TypeError);
michael@0 46 }
michael@0 47
michael@0 48 function checkNormal(f) {
michael@0 49 checkCommon(f);
michael@0 50
michael@0 51 assertEqArray(f.apply(null, ...[[]]), [undefined, undefined, undefined]);
michael@0 52 assertEqArray(f.apply(null, ...[[1]]), [1, undefined, undefined]);
michael@0 53 assertEqArray(f.apply(null, ...[[1, 2]]), [1, 2, undefined]);
michael@0 54 assertEqArray(f.apply(null, ...[[1, 2, 3, 4]]), [1, 2, 3]);
michael@0 55
michael@0 56 assertEqArray(f.apply(null, ...[[undefined]]), [undefined, undefined, undefined]);
michael@0 57 }
michael@0 58
michael@0 59 checkNormal(function(a, b, c) [a, b, c]);
michael@0 60 checkNormal((a, b, c) => [a, b, c]);
michael@0 61
michael@0 62 function checkDefault(f) {
michael@0 63 checkCommon(f);
michael@0 64
michael@0 65 assertEqArray(f.apply(null, ...[[]]), [-1, -2, -3]);
michael@0 66 assertEqArray(f.apply(null, ...[[1]]), [1, -2, -3]);
michael@0 67 assertEqArray(f.apply(null, ...[[1, 2]]), [1, 2, -3]);
michael@0 68 assertEqArray(f.apply(null, ...[[1, 2, 3, 4]]), [1, 2, 3]);
michael@0 69
michael@0 70 assertEqArray(f.apply(null, ...[[undefined]]), [-1, -2, -3]);
michael@0 71 }
michael@0 72
michael@0 73 checkDefault(function(a = -1, b = -2, c = -3) [a, b, c]);
michael@0 74 checkDefault((a = -1, b = -2, c = -3) => [a, b, c]);
michael@0 75
michael@0 76 function checkRest(f) {
michael@0 77 checkCommon(f);
michael@0 78
michael@0 79 assertEqArray(f.apply(null, ...[[]]), []);
michael@0 80 assertEqArray(f.apply(null, ...[[1]]), [1]);
michael@0 81 assertEqArray(f.apply(null, ...[[1, 2]]), [1, 2]);
michael@0 82 assertEqArray(f.apply(null, ...[[1, 2, 3, 4]]), [1, 2, 3, 4]);
michael@0 83
michael@0 84 assertEqArray(f.apply(null, ...[[undefined]]), [undefined]);
michael@0 85
michael@0 86 // other iterable objects
michael@0 87 assertEqArray(f.apply(null, ...Map([[["a", "A"], ["b", "B"]]])).map(([k, v]) => k + v), ["aA", "bB"]);
michael@0 88 }
michael@0 89
michael@0 90 checkRest(function(...x) x);
michael@0 91 checkRest((...x) => x);

mercurial