1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/js/src/jit-test/tests/for-of/strings.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,47 @@ 1.4 +// for-of works on strings and String objects. 1.5 + 1.6 +load(libdir + "string.js"); 1.7 + 1.8 +function test(s, expectedCodePoints) { 1.9 + var copy = ''; 1.10 + var codepoints = 0; 1.11 + var singleHighSurrogate = false; 1.12 + for (var v of s) { 1.13 + assertEq(typeof v, 'string'); 1.14 + assertEq(v.length, isSurrogatePair(v) ? 2 : 1); 1.15 + assertEq(false, singleHighSurrogate && isLowSurrogate(v)); 1.16 + copy += v; 1.17 + codepoints += 1; 1.18 + singleHighSurrogate = !isSurrogatePair(v) && isHighSurrogate(v); 1.19 + } 1.20 + assertEq(copy, String(s)); 1.21 + assertEq(codepoints, expectedCodePoints); 1.22 +} 1.23 + 1.24 +test('', 0); 1.25 +test('abc', 3); 1.26 +test('a \0 \ufffe \ufeff', 7); 1.27 + 1.28 +// Non-BMP characters are generally passed to JS in UTF-16, as surrogate pairs. 1.29 +// ES6 requires that such pairs be treated as a single code point in for-of. 1.30 +test('\ud808\udf45', 1); 1.31 + 1.32 +// Also test invalid surrogate pairs: 1.33 +// (1) High surrogate not followed by low surrogate 1.34 +test('\ud808', 1); 1.35 +test('\ud808\u0000', 2); 1.36 +// (2) Low surrogate not preceded by high surrogate 1.37 +test('\udf45', 1); 1.38 +test('\u0000\udf45', 2); 1.39 +// (3) Low surrogate followed by high surrogate 1.40 +test('\udf45\ud808', 2); 1.41 + 1.42 +test(new String(''), 0); 1.43 +test(new String('abc'), 3); 1.44 +test(new String('a \0 \ufffe \ufeff'), 7); 1.45 +test(new String('\ud808\udf45'), 1); 1.46 +test(new String('\ud808'), 1); 1.47 +test(new String('\ud808\u0000'), 2); 1.48 +test(new String('\udf45'), 1); 1.49 +test(new String('\u0000\udf45'), 2); 1.50 +test(new String('\udf45\ud808'), 2);