DenoDDeno
Powered by
jeff.hykinJ
Denoβ€’3y agoβ€’
4 replies
jeff.hykin

Sync Child Stdin Read

I'd like to make a JS wrapper around the repl of another language.

For example:
let a = otherLang`10`
let b = otherLang`{ c = 11; d = 12; }`;
console.log(b.c) // 11
let a = otherLang`10`
let b = otherLang`{ c = 11; d = 12; }`;
console.log(b.c) // 11


I can spawn a repl in a subprocess, keep it open and hook into the stdin/out/err of it. Then the idea would be to do something like the following:

otherLang`{ c = 11; d = 12; }`

// roughly becomes
child.stdin.write(`
  print(json(listAttrNames(
    { c = 11; d = 12; }
  )))
  print(${endUuid})
`)
while (1) {
   child.stdout.read() // until endUuid
}
otherLang`{ c = 11; d = 12; }`

// roughly becomes
child.stdin.write(`
  print(json(listAttrNames(
    { c = 11; d = 12; }
  )))
  print(${endUuid})
`)
while (1) {
   child.stdout.read() // until endUuid
}


The problem is something like that^ needs to run inside of a getter for
console.log(b.a)
console.log(b.a)
to work. E.g. no async.

I can do some incredibly hacky work, like funneling stdout to a file and then sync-reading (.seek()) the file over and over. But surely there's a better way.

Relevant documentation on streams:
https://deno.land/api@v1.38.5?s=ReadableStreamBYOBReader
Deno
ReadableStreamBYOBReader | Runtime APIs | Deno
ReadableStreamBYOBReader | Runtime APIs | Deno
Deno banner
DenoJoin
Chat about Deno, a modern runtime for JavaScript and TypeScript.
20,944Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

How to read all text from stdin.
pomdtrPpomdtr / help
3y ago
Synchronous read single line from Deno.stdin
Octal!OOctal! / help
4y ago
Read child process I/O
mattvrMmattvr / help
2y ago