11cb0ef41Sopenharmony_ci// META: title=Encoding API: Fatal flag
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_civar bad = [
41cb0ef41Sopenharmony_ci    { encoding: 'utf-8', input: [0xFF], name: 'invalid code' },
51cb0ef41Sopenharmony_ci    { encoding: 'utf-8', input: [0xC0], name: 'ends early' },
61cb0ef41Sopenharmony_ci    { encoding: 'utf-8', input: [0xE0], name: 'ends early 2' },
71cb0ef41Sopenharmony_ci    { encoding: 'utf-8', input: [0xC0, 0x00], name: 'invalid trail' },
81cb0ef41Sopenharmony_ci    { encoding: 'utf-8', input: [0xC0, 0xC0], name: 'invalid trail 2' },
91cb0ef41Sopenharmony_ci    { encoding: 'utf-8', input: [0xE0, 0x00], name: 'invalid trail 3' },
101cb0ef41Sopenharmony_ci    { encoding: 'utf-8', input: [0xE0, 0xC0], name: 'invalid trail 4' },
111cb0ef41Sopenharmony_ci    { encoding: 'utf-8', input: [0xE0, 0x80, 0x00], name: 'invalid trail 5' },
121cb0ef41Sopenharmony_ci    { encoding: 'utf-8', input: [0xE0, 0x80, 0xC0], name: 'invalid trail 6' },
131cb0ef41Sopenharmony_ci    { encoding: 'utf-8', input: [0xFC, 0x80, 0x80, 0x80, 0x80, 0x80], name: '> 0x10FFFF' },
141cb0ef41Sopenharmony_ci    { encoding: 'utf-8', input: [0xFE, 0x80, 0x80, 0x80, 0x80, 0x80], name: 'obsolete lead byte' },
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_ci    // Overlong encodings
171cb0ef41Sopenharmony_ci    { encoding: 'utf-8', input: [0xC0, 0x80], name: 'overlong U+0000 - 2 bytes' },
181cb0ef41Sopenharmony_ci    { encoding: 'utf-8', input: [0xE0, 0x80, 0x80], name: 'overlong U+0000 - 3 bytes' },
191cb0ef41Sopenharmony_ci    { encoding: 'utf-8', input: [0xF0, 0x80, 0x80, 0x80], name: 'overlong U+0000 - 4 bytes' },
201cb0ef41Sopenharmony_ci    { encoding: 'utf-8', input: [0xF8, 0x80, 0x80, 0x80, 0x80], name: 'overlong U+0000 - 5 bytes' },
211cb0ef41Sopenharmony_ci    { encoding: 'utf-8', input: [0xFC, 0x80, 0x80, 0x80, 0x80, 0x80], name: 'overlong U+0000 - 6 bytes' },
221cb0ef41Sopenharmony_ci
231cb0ef41Sopenharmony_ci    { encoding: 'utf-8', input: [0xC1, 0xBF], name: 'overlong U+007F - 2 bytes' },
241cb0ef41Sopenharmony_ci    { encoding: 'utf-8', input: [0xE0, 0x81, 0xBF], name: 'overlong U+007F - 3 bytes' },
251cb0ef41Sopenharmony_ci    { encoding: 'utf-8', input: [0xF0, 0x80, 0x81, 0xBF], name: 'overlong U+007F - 4 bytes' },
261cb0ef41Sopenharmony_ci    { encoding: 'utf-8', input: [0xF8, 0x80, 0x80, 0x81, 0xBF], name: 'overlong U+007F - 5 bytes' },
271cb0ef41Sopenharmony_ci    { encoding: 'utf-8', input: [0xFC, 0x80, 0x80, 0x80, 0x81, 0xBF], name: 'overlong U+007F - 6 bytes' },
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_ci    { encoding: 'utf-8', input: [0xE0, 0x9F, 0xBF], name: 'overlong U+07FF - 3 bytes' },
301cb0ef41Sopenharmony_ci    { encoding: 'utf-8', input: [0xF0, 0x80, 0x9F, 0xBF], name: 'overlong U+07FF - 4 bytes' },
311cb0ef41Sopenharmony_ci    { encoding: 'utf-8', input: [0xF8, 0x80, 0x80, 0x9F, 0xBF], name: 'overlong U+07FF - 5 bytes' },
321cb0ef41Sopenharmony_ci    { encoding: 'utf-8', input: [0xFC, 0x80, 0x80, 0x80, 0x9F, 0xBF], name: 'overlong U+07FF - 6 bytes' },
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ci    { encoding: 'utf-8', input: [0xF0, 0x8F, 0xBF, 0xBF], name: 'overlong U+FFFF - 4 bytes' },
351cb0ef41Sopenharmony_ci    { encoding: 'utf-8', input: [0xF8, 0x80, 0x8F, 0xBF, 0xBF], name: 'overlong U+FFFF - 5 bytes' },
361cb0ef41Sopenharmony_ci    { encoding: 'utf-8', input: [0xFC, 0x80, 0x80, 0x8F, 0xBF, 0xBF], name: 'overlong U+FFFF - 6 bytes' },
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_ci    { encoding: 'utf-8', input: [0xF8, 0x84, 0x8F, 0xBF, 0xBF], name: 'overlong U+10FFFF - 5 bytes' },
391cb0ef41Sopenharmony_ci    { encoding: 'utf-8', input: [0xFC, 0x80, 0x84, 0x8F, 0xBF, 0xBF], name: 'overlong U+10FFFF - 6 bytes' },
401cb0ef41Sopenharmony_ci
411cb0ef41Sopenharmony_ci    // UTF-16 surrogates encoded as code points in UTF-8
421cb0ef41Sopenharmony_ci    { encoding: 'utf-8', input: [0xED, 0xA0, 0x80], name: 'lead surrogate' },
431cb0ef41Sopenharmony_ci    { encoding: 'utf-8', input: [0xED, 0xB0, 0x80], name: 'trail surrogate' },
441cb0ef41Sopenharmony_ci    { encoding: 'utf-8', input: [0xED, 0xA0, 0x80, 0xED, 0xB0, 0x80], name: 'surrogate pair' },
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_ci    { encoding: 'utf-16le', input: [0x00], name: 'truncated code unit' },
471cb0ef41Sopenharmony_ci    // Mismatched UTF-16 surrogates are exercised in utf16-surrogates.html
481cb0ef41Sopenharmony_ci
491cb0ef41Sopenharmony_ci    // FIXME: Add legacy encoding cases
501cb0ef41Sopenharmony_ci];
511cb0ef41Sopenharmony_ci
521cb0ef41Sopenharmony_cibad.forEach(function(t) {
531cb0ef41Sopenharmony_ci    test(function() {
541cb0ef41Sopenharmony_ci        assert_throws_js(TypeError, function() {
551cb0ef41Sopenharmony_ci            new TextDecoder(t.encoding, {fatal: true}).decode(new Uint8Array(t.input))
561cb0ef41Sopenharmony_ci        });
571cb0ef41Sopenharmony_ci    }, 'Fatal flag: ' + t.encoding + ' - ' + t.name);
581cb0ef41Sopenharmony_ci});
591cb0ef41Sopenharmony_ci
601cb0ef41Sopenharmony_citest(function() {
611cb0ef41Sopenharmony_ci    assert_true('fatal' in new TextDecoder(), 'The fatal attribute should exist on TextDecoder.');
621cb0ef41Sopenharmony_ci    assert_equals(typeof  new TextDecoder().fatal, 'boolean', 'The type of the fatal attribute should be boolean.');
631cb0ef41Sopenharmony_ci    assert_false(new TextDecoder().fatal, 'The fatal attribute should default to false.');
641cb0ef41Sopenharmony_ci    assert_true(new TextDecoder('utf-8', {fatal: true}).fatal, 'The fatal attribute can be set using an option.');
651cb0ef41Sopenharmony_ci
661cb0ef41Sopenharmony_ci}, 'The fatal attribute of TextDecoder');
671cb0ef41Sopenharmony_ci
681cb0ef41Sopenharmony_citest(() => {
691cb0ef41Sopenharmony_ci  const bytes = new Uint8Array([226, 153, 165]);
701cb0ef41Sopenharmony_ci  const decoder = new TextDecoder('utf-8', {fatal: true});
711cb0ef41Sopenharmony_ci  assert_equals(decoder.decode(new DataView(bytes.buffer, 0, 3)),
721cb0ef41Sopenharmony_ci                '♥',
731cb0ef41Sopenharmony_ci                'decode() should decode full sequence');
741cb0ef41Sopenharmony_ci  assert_throws_js(TypeError,
751cb0ef41Sopenharmony_ci                   () => decoder.decode(new DataView(bytes.buffer, 0, 2)),
761cb0ef41Sopenharmony_ci                   'decode() should throw on incomplete sequence');
771cb0ef41Sopenharmony_ci  assert_equals(decoder.decode(new DataView(bytes.buffer, 0, 3)),
781cb0ef41Sopenharmony_ci                '♥',
791cb0ef41Sopenharmony_ci                'decode() should not throw on subsequent call');
801cb0ef41Sopenharmony_ci}, 'Error seen with fatal does not prevent future decodes');
81