After a few dozen or so tests I've got Mozilla SpiderMonkey's jsshell to 
echo back a single message from the Native Messaging client on Chromium 
Version 128.0.6541.0 (Developer Build) (64-bit) by sending "\r\n\r\n" 
following the message that is expected to be echoed.

nm_jsshell.js

#!/usr/bin/env -S JS_STDERR=err.txt 
/home/user/.jsvu/engines/spidermonkey/spidermonkey
// SpiderMonkey Native Messaging host (W.I.P.)
// guest271314 7-7-2023

function encodeMessage(str) {
  return new Uint8Array([...str].map((s) => s.codePointAt()));
}

function getMessage() {
  const stdin = readline();
  const data = encodeMessage(stdin);// new Uint8Array([...stdin].map((s) => 
s.codePointAt()));
  const view = new DataView(data.buffer);
  const length = new Uint32Array([view.getUint32(0, true)]);
  const message = data.subarray(4);
  return message;
}

function sendMessage(message) {
  os.file.writeTypedArrayToFile("/proc/self/fd/1", new 
Uint32Array([message.length]));
  os.file.writeTypedArrayToFile("/proc/self/fd/1", message);
}

function main() {
  // while (true) {
  const message = getMessage();
  sendMessage(message);
 // }
}

try {
  main();
} catch (e) {
  os.file.writeTypedArrayToFile("caught.txt", encodeMessage(e.message));
  quit();
}

background.js (MV3 ServiceWorker)

globalThis.name = chrome.runtime.getManifest().short_name;

async function sendNativeMessage(message) {
  return new Promise((resolve, reject) => {
    globalThis.port = chrome.runtime.connectNative(globalThis.name);
    port.onMessage.addListener((message) => {
      resolve(message);
      port.disconnect();
    });
    port.onDisconnect.addListener(() => {
      reject(chrome.runtime.lastError);
    });
    port.postMessage(message);
    port.postMessage("\r\n\r\n");
  });
}

sendNativeMessage(new Array(209715)).then(console.log).catch(console.error);

Now I'm trying to figure out why the same/similar code doesn't work in d8.

I suspect the Uint32Array equivalent which is not UTF-8 is causing an issue 
for readline(). 

On Saturday, June 15, 2024 at 8:21:49 AM UTC-7 guest271314 wrote:

> I am able to read STDIN with readline(). When I do I can't write to STDOUT 
> with writeFile() in the same script.
>
> readline() is blocking in a Native Messaging host. 
>
> Since we can write to STDIN in d8 with
>
>     writeFile("/proc/self/fd/1", length);
>     writeFile("/proc/self/fd/1", message);
>
> and in SpiderMonkey with   
>
>     os.file.writeTypedArrayToFile("/proc/self/fd/1", length);
>     os.file.writeTypedArrayToFile("/proc/self/fd/1", message);
>
> it makes sense to me to provide a means to read from STDIN in a 
> non-blocking manner. Because writing to STDOUT is not in ECMA-262, either.
>
> On Thu, Jun 13, 2024 at 1:58 AM Jakob Kummerow <[email protected]> 
> wrote:
>
>> V8 itself doesn't do anything with stdin, because ECMAScript doesn't.
>>
>> It's up to embedders of V8 to provide integration with desired I/O 
>> channels. The d8 shell (which I think is what JSVU gives you) does 
>> implement stdin reading via the readline() function, and in an 
>> interactive session it works for me:
>>
>> $ /usr/bin/env -S out/x64.release/d8
>> V8 version 12.8.0 (candidate)
>> d8> var a = readline()
>> hello world                          <<< This is what I typed
>> undefined
>> d8> a
>> "hello world"
>>
>> Piping JS programs into d8 also works, though the printed output is a bit 
>> weird in that case:
>>
>> $ echo "2+3" | /usr/bin/env -S out/x64.release/d8
>> V8 version 12.8.0 (candidate)
>> d8> 5
>> d8>
>>
>> That said, d8 is intended for our team's testing and development needs, 
>> it doesn't aim or claim to be a general-purpose shell. You can extend it 
>> for your needs if you want, or you can take a look at more feature-rich V8 
>> embedders such as Node.
>>
>>
>> On Thu, Jun 13, 2024 at 7:51 AM guest271314 <[email protected]> wrote:
>>
>>> I'm launching `v8` from a non-TTY program, e.g., 
>>>
>>>     #!/usr/bin/env -S /home/user/.jsvu/engines/v8/v8
>>>
>>>
>>> readline() is not reading STDOUT from the launching process.
>>>
>>> read() isn't reading from /dev/stdin or /proc/self/fd/0 either. 
>>>
>>> How to read from /dev/stdin and /proc/self/fd/0 in v8?
>>>
>>> -- 
>>> -- 
>>> v8-dev mailing list
>>> [email protected]
>>> 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 [email protected].
>>> To view this discussion on the web visit 
>>> https://groups.google.com/d/msgid/v8-dev/1c15ed97-8fe6-49f6-a7a4-7420dffb7923n%40googlegroups.com
>>>  
>>> <https://groups.google.com/d/msgid/v8-dev/1c15ed97-8fe6-49f6-a7a4-7420dffb7923n%40googlegroups.com?utm_medium=email&utm_source=footer>
>>> .
>>>
>> -- 
>> -- 
>> v8-dev mailing list
>> [email protected]
>> 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 [email protected].
>>
> To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/v8-dev/CAKSzg3TTqmU5so4Pt%2BEnnGFj3YyYH_H6JtKWhWfzX20xeWs8oQ%40mail.gmail.com
>>  
>> <https://groups.google.com/d/msgid/v8-dev/CAKSzg3TTqmU5so4Pt%2BEnnGFj3YyYH_H6JtKWhWfzX20xeWs8oQ%40mail.gmail.com?utm_medium=email&utm_source=footer>
>> .
>>
>

-- 
-- 
v8-dev mailing list
[email protected]
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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/v8-dev/4b94ea7f-4b62-4690-b437-7fe7f8f2077fn%40googlegroups.com.

Reply via email to