Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PGlite bindings for other runtimes? #89

Open
samkhawase opened this issue May 15, 2024 · 1 comment
Open

PGlite bindings for other runtimes? #89

samkhawase opened this issue May 15, 2024 · 1 comment
Labels
wasi outside browser usage

Comments

@samkhawase
Copy link

I'm trying to run and access PGLite through a golang program via wasmer-go. However it fails with the error regardgin env not instantiated. I assume that there is no support for other runtimes (#40)

Is there a way I could contribute to adding support for more runtimes?

$ wazero run postgres.wasm                                                                                                                                                                                         
error instantiating wasm binary: module[env] not instantiated

$ wasmtime postgres.wasm                                                                                                                                                                                           
Error: failed to run main module `postgres.wasm`

Caused by:
    0: failed to instantiate "postgres.wasm"
    1: unknown import: `env::invoke_ii` has not been defined
@e253
Copy link

e253 commented May 31, 2024

A .wasm file will declare a bunch of functions it needs to execute properly. WASI and WASIX are standard sets of such functions. A pure WASI/WASIX web assembly program can be run in a WASI/WASIX runtime like wasmer without changes. At the moment, PGlite ships a wasm binary with a JS file that provides many custom Emscripten non-standard functions that wasmer doesn't have.

// postgres.js (generated)
var wasmImports = {
//...
      emscripten_date_now: _emscripten_date_now,
      emscripten_dispatch_result: emscripten_dispatch_result,
      emscripten_get_heap_max: _emscripten_get_heap_max,
      emscripten_get_now: _emscripten_get_now,
      emscripten_get_now_res: _emscripten_get_now_res,
      emscripten_memcpy_js: _emscripten_memcpy_js,
      emscripten_resize_heap: _emscripten_resize_heap,
      environ_get: _environ_get,
      environ_sizes_get: _environ_sizes_get,
      exit: _exit,
      fd_close: _fd_close,
      fd_fdstat_get: _fd_fdstat_get,
      fd_pread: _fd_pread,
      fd_pwrite: _fd_pwrite,
      fd_read: _fd_read,
      fd_seek: _fd_seek,
      fd_sync: _fd_sync,
      fd_write: _fd_write,
// ...
}

A WASIX (note the x, not WASI) build of pglite shouldn't be too tough because it has a very broad API. You link the wasix libc and that libc will compile imports in the .wasm binary that wasmer understands. You could even compile totally vanilla postgres to wasm32-wasix-none. I'd love to use a wasix pg instead of bloated, ram hungry docker pg! https://wasix.org/docs/language-guide/c/usage.

WASI should be possible through Emscripten by using the -sWASM_STANDALONE=1 linker flag. It can work, but will take some finagling to pass tests.

@pmp-p pmp-p added the wasi outside browser usage label Jun 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
wasi outside browser usage
Projects
None yet
Development

No branches or pull requests

3 participants