trealla-prolog / go

Trealla Prolog embedded in Go using WASM
MIT License
79 stars 3 forks source link

Persistent interpreter instances #4

Closed guregu closed 1 year ago

guregu commented 2 years ago

Currently all queries go through a fresh interpreter. It would be nice to keep them alive so they can keep their knowledge base contents between requests.

I'm not sure how easy it will be because the stdin interfaces for Go WASM libraries don't look so great. Would be really nice to be able to pass an io.Reader to them. Might be possible to work around by exporting Go/C functions and calling from Trealla FFI (need to add FFI to Trealla WASM first).

guregu commented 2 years ago

This would be easy if this issue were fixed: https://github.com/wasmerio/wasmer-go/issues/338 Seems like that issue is blocked by this... https://github.com/wasmerio/wasmer/issues/2334 which could maybe be resolved by this https://github.com/wasmerio/wasmer/pull/3032 (and of course needs to make its way to the Go lib).

Perhaps WasmEdge has better support? Not sure 🤔

One crazy trick I tried was using mkfifo and reading from inside WASM and it seems to work fine, so I'm thinking we can use FIFOs as a kind of temporary workaround. This won't work on Windows but neither does wasmer-go at the moment.

guregu commented 2 years ago

Played around with named pipes. I think it could probably work but it adds a lot of complexity, including filesystem stuff that I would rather avoid. At that point it's probably better to just execute tpl in a subprocess.

I think exposing a host function is the best option, probably. Not sure how to actually do this yet.