What I'm doing in d8

function encodeMessage(str) { return new Uint8Array([...str].map((s) => 
s.codePointAt())); } // ... const stdin = os.system("./readFile.js", [ 
`/proc/${pid.replace(/\D+/g, "")}/fd/0`, ]); if (stdin != undefined && 
stdin != null && stdin.length) { const data = encodeMessage(stdin); const 
size = data.subarray(0, 4); const view = new DataView(size.buffer); const 
length = view.getUint32(0, true); 
sendMessage(encodeMessage(JSON.stringify({ length, size: [...size] }))); 
const message = data.subarray(4); // ...

On Sunday, July 7, 2024 at 9:20:17 AM UTC-7 guest271314 wrote:

> Right now I'm using QuickJS to read standard input stream to d8 with 
> os.system(), to get around readline() not being capable of processing 
> Uint32Array from Chrome.
>
> How would I go about translating working QuickJS code to extending d8 to 
> read standard input into TypedArray's?
>
> #!/usr/bin/env -S /home/user/bin/qjs -m --std
> // Read stdin to V8's d8, send to d8
> // const stdin = os.system("./read_d8_stdin.js", 
> [`/proc/${pid.replace(/\D+/g, "")}/fd/0`]);
> function read_d8_stdin([, path] = scriptArgs) {
>   try {
>     // Uint32Array to read message length into
>     const size = new Uint32Array(1);
>     const err = {
>       errno: 0
>     };
>     // Open /proc/PID/fd/0 for reading
>     const pipe = std.open(
>       path,
>       "rb",
>       err,
>     );
>     if (err.errno !== 0) {
>       throw `${std.strerror(err.errno)}: ${path}`;
>     }
>     // Read /proc/PID/fd/0 into Uint32Array
>     pipe.read(size.buffer, 0, 4);
>     // Uint8Array to read message into
>     const output = new Uint8Array(size[0]);
>     // Read /proc/PID/fd/0 into Uint8Array
>     pipe.read(output.buffer, 0, output.length);
>     // Uint8Array containing Uint32Array buffer in Uint8Array and 
> Uint8Array containing message
>     const data = new Uint8Array([...new Uint8Array(size.buffer), 
> ...output]);
>     // Write Uint8Array to d8's stdin
>     std.out.write(data.buffer, 0, data.length);
>     std.out.flush();
>     // Exit and start again initiated from d8
>     std.exit(0);
>   } catch (e) {
>     // Handle error
>     const err = {
>       errno: 0
>     };
>     const file = std.open("qjsErr.txt", "w", err);
>     if (err.errno !== 0) {
>       file.puts(JSON.stringify(err));
>       file.close();
>       std.exit(1);
>     }
>     file.puts(JSON.stringify(e));
>     file.close();
>     std.out.puts(JSON.stringify(err));
>     std.exit(1);
>   }
> }
>
> read_d8_stdin();
>

-- 
-- 
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/6bd05808-fee3-46fd-9959-4f1e64741c03n%40googlegroups.com.

Reply via email to