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


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
}


The problem is something like that^ needs to run inside of a getter for 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
Was this page helpful?