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

Add full support for WebAssembly in both browser and Node.js environments #288

Closed
wants to merge 29 commits into from

Conversation

albrow
Copy link

@albrow albrow commented Aug 2, 2019

This PR expands on #277 (which supported Node.js only) and adds support for running goleveldb in the browser. This is accomplished by using BrowserFS and writing syscall/js bindings that mimic the functions and methods in the Go standard library. AFAIK there is no way for us to embed BrowserFS, so users will need to include and initialize it separately (typically inside of a script tag prior to loading the WebAssembly bytecode).

This PR also includes a mechanism for running a small set of tests in a headless browser by simply running go test a the repository root. These tests require Node.js and npm.

@syndtr if this is something you are interested in, we would love to get it merged upstream so that other people could make use of it. I imagine we will need to add some more documentation specifically around browser support. I also anticipate that you might want to tweak some things about the file structure, naming conventions, and how browser tests are run. Let me know if you have any questions or if there is anything I can do to help.

@albrow
Copy link
Author

albrow commented Aug 6, 2019

The browser tests pass locally but I'm having some difficulty getting them to work in CI. There are a couple of issues:

  • Go versions < 1.11 don't support Go Modules so they are unable to find and download our QUnit bindings, which the browser tests depend on.
  • Go versions < 1.11 don't support WebAssembly at all.
  • Go version 1.11 uses a different API for the syscall/js package, which is used to call out to JavaScript code. I wrote the bindings with the latest Go version (1.12 as of this comment) in mind.
  • TravisCI seems to have some issues with running puppeteer (a headless Chrome browser used for the tests).

Any ideas about how to address these? I'm not very familiar with TravisCI but it might just be a matter of configuring the tests to only run for certain versions of Go. It would be nice if the tests could run in CI to make sure that browser support doesn't break in the future.

@albrow albrow force-pushed the full-wasm-support branch from 5ac28e2 to 3ff7350 Compare November 1, 2019 00:55
@albrow
Copy link
Author

albrow commented Nov 15, 2019

I just pushed a few more commits which allow support for asynchronous BrowserFS backends (i.e. those that don't have support for functions with the Sync suffix).

For 0x Mesh we are now using BrowserFS backed by IndexedDB, which wasn't possible before. You can see this PR for more context.

@goldbock
Copy link

any update on this? go-ethereum fails to compile to wasm due the lack of support of wasm target by go-leveldb.

@simbadMarino
Copy link

@syndtr , any plans to merge this? Would be great to have Wasm support =)

@simbadMarino
Copy link

hello @albrow , I'm using your commit from 0xProject in the mean time by replacing in my go.mod, however I'm getting the following errors during build:
image

Any ideas on what I'm missing or what could be happening? Thanks

@albrow
Copy link
Author

albrow commented Sep 15, 2023

@simbadMarino I haven't worked with Go and WebAssembly in a few years. The syscall/js package was always marked as "experimental" and it looks like some things may have changed under the hood since I originally wrote that code. It would probably not be too difficult to update if you take a look at the latest documentation for syscall/js: https://pkg.go.dev/syscall/js.

@simbadMarino
Copy link

simbadMarino commented Sep 15, 2023

@simbadMarino I haven't worked with Go and WebAssembly in a few years. The syscall/js package was always marked as "experimental" and it looks like some things may have changed under the hood since I originally wrote that code. It would probably not be too difficult to update if you take a look at the latest documentation for syscall/js: https://pkg.go.dev/syscall/js.

Thanks for the quick reply! let me dig a bit into it :), will keep you posted

EDIT: Most likely something in either of these commits broke something:

golang/go@cd53093
golang/go@2bd767b

@simbadMarino
Copy link

@simbadMarino I haven't worked with Go and WebAssembly in a few years. The syscall/js package was always marked as "experimental" and it looks like some things may have changed under the hood since I originally wrote that code. It would probably not be too difficult to update if you take a look at the latest documentation for syscall/js: https://pkg.go.dev/syscall/js.

Thanks for the quick reply! let me dig a bit into it :), will keep you posted

EDIT: Most likely something in either of these commits broke something:

golang/go@cd53093 golang/go@2bd767b

I managed to fix it by using reflect to compare, if anyone is interested I created a fork which compiles wasm successfully on go v1.19.9: https://github.com/simbadMarino/goleveldb. Thanks!

@0xProject 0xProject closed this by deleting the head repository Oct 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants