'stdin to an emscripten, about how to continue input?

related to Providing stdin to an emscripten HTML program?

function stdin() {
      if (i < input.length) {
        var code = input.charCodeAt(i);
        ++i;
        return code;
      } else {
        return null;
      }
    }

input.length should be length+2. then stdin will continue, why?

example:

function stringToBuffer(value: string): Uint8Array {
  let view = new Uint8Array(value.length);
  for (let i = 0, length = value.length; i < length; i++) {
      view[i] = value.charCodeAt(i);
  }
  return view;
}

input = stringToBuffer("123");
i=0;
out();
input = stringToBuffer("xyz");
i=0;
out();

c file

int out()
{
   
    int chr;
    while ((chr = fgetc(stdin)) != EOF)
    {
      fputc(chr, stdout);
    }
    return 1;
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source