js/src/jit-test/tests/for-of/strings.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 // for-of works on strings and String objects.
     3 load(libdir + "string.js");
     5 function test(s, expectedCodePoints) {
     6     var copy = '';
     7     var codepoints = 0;
     8     var singleHighSurrogate = false;
     9     for (var v of s) {
    10         assertEq(typeof v, 'string');
    11         assertEq(v.length, isSurrogatePair(v) ? 2 : 1);
    12         assertEq(false, singleHighSurrogate && isLowSurrogate(v));
    13         copy += v;
    14         codepoints += 1;
    15         singleHighSurrogate = !isSurrogatePair(v) && isHighSurrogate(v);
    16     }
    17     assertEq(copy, String(s));
    18     assertEq(codepoints, expectedCodePoints);
    19 }
    21 test('', 0);
    22 test('abc', 3);
    23 test('a \0 \ufffe \ufeff', 7);
    25 // Non-BMP characters are generally passed to JS in UTF-16, as surrogate pairs.
    26 // ES6 requires that such pairs be treated as a single code point in for-of.
    27 test('\ud808\udf45', 1);
    29 // Also test invalid surrogate pairs:
    30 // (1) High surrogate not followed by low surrogate
    31 test('\ud808', 1);
    32 test('\ud808\u0000', 2);
    33 // (2) Low surrogate not preceded by high surrogate
    34 test('\udf45', 1);
    35 test('\u0000\udf45', 2);
    36 // (3) Low surrogate followed by high surrogate
    37 test('\udf45\ud808', 2);
    39 test(new String(''), 0);
    40 test(new String('abc'), 3);
    41 test(new String('a \0 \ufffe \ufeff'), 7);
    42 test(new String('\ud808\udf45'), 1);
    43 test(new String('\ud808'), 1);
    44 test(new String('\ud808\u0000'), 2);
    45 test(new String('\udf45'), 1);
    46 test(new String('\u0000\udf45'), 2);
    47 test(new String('\udf45\ud808'), 2);

mercurial