I'm trying to use d8 as a Native Messaging host, using the same or similar 
algorithm I do for QuickJS, Deno, and Node.js hosts.

d8 is getting stuck somewhere in the process. I suspect trying to read and 
write Uint32Array from stdin with readline() and write Uint32Array with 
write().

Is there any way to make this work with d8 alone?

#!d8
function getMessage() {
  const input = readline();
  const uint8 = new Uint8Array(
      [...input].map((s) =>
        s.charCodeAt()
      )
    );
  const view = new DataView(uint8.buffer);
  const length = view.getUint32(0, true);
  const content = new Uint8Array(length);
  content.set(input.subarray(4));
  return uint8.subarray();
}

function sendMessage(json) {

  let header = Uint32Array.from(
    {
      length: 4,
    },
    (_, index) => (json.length >> (index * 8)) & 0xff
  );
  let output = new Uint8Array(header.length + json.length);
  output.set(header, 0);
  output.set(json, 4);
  write(output);
}

function main() {
  while (true) {
    try {
      const message = getMessage();
      sendMessage(message);
      break;
    } catch (e) {
      quit();
    }
  }
}

main();

-- 
-- 
v8-dev mailing list
v8-dev@googlegroups.com
http://groups.google.com/group/v8-dev
--- 
You received this message because you are subscribed to the Google Groups 
"v8-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to v8-dev+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/v8-dev/cf191a42-3215-4297-a224-e1613c238dd9n%40googlegroups.com.

Reply via email to