d8's readline() is useless for reading STDIN where standard input is not 
UTF-8, e.g., readline() will stop reading when the initial input is a 
Uint32Array denoting the size of the following message.

While ECMA-262 does not specify reading stadard input or writing to 
standard output there is precedent in d8 for writing to 
STDOUT: writeFile("/proc/self/fd/1")*. *SpiderMonkey's js does read past 
the initial Uint32Array with readline(), though like d8, expects a newline 
character.

Circa 2024 JavaScript is a general programming language - that lacks the 
basic capability to read STDIN and write to STDOUT in an engine and runtime 
agnostic way, using an (resizable) ArrayBuffer to store variable standard 
input. For the non-browser environments we can use Web IDL to indicate when 
tyo expose or not expose the capability, just like certain interfaces are 
only available or not available in certain contexts; whether that be no 
fetch() in an AudioWorklet, or TCPSocketServer only in Isolated Web Apps.

Something like 
this 
https://github.com/guest271314/NativeMessagingHosts/blob/main/nm_host.js#L45-L69
 
should be standardized for JavaScript as a whole so we can use the same 
code in different engines and runtimes, instead of having to use different 
standard input and standard output code for each engine and runtime. 

Thanks for your consideration.

async function* getMessage() {
  let messageLength = 0;
  let readOffset = 0;
  for await (let message of readable) {
    if (buffer.byteLength === 0) {
      buffer.resize(4);
      for (let i = 0; i < 4; i++) {
        view.setUint8(i, message[i]);
      }
      messageLength = view.getUint32(0, true);
      message = message.subarray(4);
      buffer.resize(0);
    }
    buffer.resize(buffer.byteLength + message.length);
    for (let i = 0; i < message.length; i++, readOffset++) {
      view.setUint8(readOffset, message[i]);
    }
    if (buffer.byteLength === messageLength) {
      yield new Uint8Array(buffer);
      messageLength = 0;
      readOffset = 0;
      buffer.resize(0);
    }
  }
}

try {
  await sendMessage(encodeMessage([{ dirname, filename, url }, ...args]));
  for await (const message of getMessage()) {
    await sendMessage(message);
  }
} catch (e) {
  exit();
}



-- 
-- 
v8-users mailing list
v8-users@googlegroups.com
http://groups.google.com/group/v8-users
--- 
You received this message because you are subscribed to the Google Groups 
"v8-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to v8-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/v8-users/72eabb86-cf95-4035-a1c1-35e180efc915n%40googlegroups.com.

Reply via email to