-
Notifications
You must be signed in to change notification settings - Fork 123
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
Compiling and running scryer as a WebAssembly binary? #615
Comments
I have compiled scryer without signal (nix package) under windows OS |
I did that one or two months ago, based on c342d18. It seems possible, but it certainly isn't trivial. Compilation problems
Cross-compilingI don't remember the exact command I used to compile it, but I used rustup's nightly toolchain for the Syscalls in the browserI implemented necessary syscalls to an extent that allowed to load a prolog source file from a virtual in-browser file system, and to see output written to the ResultI got this tiny "Hello world" to work eventually: :- initialization(hello_world).
hello_world :-
write('Hello, World!'), nl. Partial screenshot of the web page (with some debugging info): I didn't try much more after that, this had already taken me many hours and I mostly did it to gain some experience with Rust → WebAssembly. |
@tniessen, thank you! That's awesome. Despite these issues, it seems like porting scryer would be easier.
@tniessen, do you happen to still have your changes somewhere? It would be a nice starting point. |
@rla It might be possible to avoid this in some browsers with only minimal modifications in scryer-prolog. I wrote synchronous-channel a while back to allow WebAssembly in worker threads to retrieve information (such as terminal input) from the main thread. I did not test the library in browsers, but it should work. So the idea would be for the main thread to write terminal input to a
I'll try to find and commit them, but it might take a few days. |
Very interested in a wasm module with TCP/UDP bindings. I'm trying to build the SWI-Prolog WASM just to see where I can find those calls... any idea if I'll be able to sniff those calls out with a nodeJS WASI wrapper? |
Sorry @jacobfriedman and @rla, I am super busy with university, OSS, and everything else right now, but it's still on my list. I'd love to collaborate on this, and I'll try to find time soon-ish, I hope. |
@jacobfriedman @rla I am trying to get it to work again but recent changes have only made it more difficult.
I am not sure why there are so many different crypto libraries in the list of dependencies. TCP (the |
Well, a red flag for me: What I've been seeing is that many libraries just wrap C instead of implementing in the native language. I wonder if these dependencies can be replaced with native (rust) drop-ins. Are you using emscripten to compile, or compiling straight to WASM? |
Thank you a lot for resuming work on this project! The cryptographic crates are used to support various cryptographic predicates in The big bouquet of crates is needed because none of the existing crates covers all the functionality that If you do not need this functionality, you can, in your WASM branch, simply remove all cryptographic predicates from scryer-prolog/src/machine/system_calls.rs Line 4749 in 10e92ee
scryer-prolog/src/machine/system_calls.rs Line 5197 in 10e92ee
and then simply remove the inclusion of these crates. This could be useful to start with this project. If at all possible, it would be great to preserve the cryptographic functionality as far as practical also in the WASM port. Please let me know if you have any questions about this. |
Thank you for the help! @tniessen will you take the wheel on this, as I'm relocating this week? If so please fork/branch & post the URL here :) |
One additional remark: If you plan to get one of the cryptographic crates working, I suggest to focus on ring. This is the most useful and well designed of the cryptographic crates, and porting it would enable a lot of the cryptographic functionality, most notably secure hashes, encryption and authentication. The other crates are used for more rarely used hashes, and low-level reasoning over elliptic curves. I plan to get rid of specifically the |
I wonder if this would be a good use-case for a |
Sure, I'll try to get a minimal version to work. |
Cool. I can work on it first thing next week. Also- are you working with
emscripten?
…On Thu., Apr. 8, 2021, 1:06 p.m. Tobias Nießen, ***@***.***> wrote:
@tniessen <https://github.com/tniessen> will you take the wheel on this,
as I'm relocating this week?
Sure, I'll try to get a minimal version to work.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#615 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAFKHXWQRJBPYMWT2E5N6NDTHXPBZANCNFSM4OJ3LNXQ>
.
|
@jacobfriedman No, I am trying to get it to work with cargo wasi. Emscripten might come in handy when we need to provide the WASI environment in the browser, but I'd personally prefer a "clean" build without Emscripten first. |
Oh, and best of luck on your move :) |
Thanks!
Ok, good to know. I'll start digging into the docs.
…On Thu., Apr. 8, 2021, 1:26 p.m. Tobias Nießen, ***@***.***> wrote:
Oh, and best of luck on your move :)
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#615 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAFKHXT5QLPOHRBGWQQUZB3THXRNNANCNFSM4OJ3LNXQ>
.
|
I made the crypto and terminal dependencies optional, but getting rid of |
There must be a better way than a huge diff. In my past experiences the
problems always trickled down over time.
Maintainers, what do you say?
…On Fri., Apr. 9, 2021, 9:57 a.m. Tobias Nießen, ***@***.***> wrote:
I made the crypto and terminal dependencies optional, but getting rid of
slice-deque seems like a bigger challenge here. That will probably become
a large diff.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#615 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAFKHXWMX2FZ7ULKRAPQHKLTH4BU7ANCNFSM4OJ3LNXQ>
.
|
If I figure out how to redefine the Edit: the APIs of |
Is it worth trying to ask the maintainers of the affected crates for the implementation of the needed functionality to simplify this change? You can link to this issue to explain the motivation for the needed features. |
Ah, the tangled web of dependency maintenance. Who's going to maintain the maintainers? @triska, I remember in one of your PoP videos you spoke of depending on systems for not months, not years, but decades. I left the node/npm ecosystem because of careless package dependency. How is it possible to lessen the dependencies so the installation is unaffected by external forces? |
According to the maintainers, We can replace It would be great if the APIs were compatible, but that seems impossible at this point, and would mean breaking changes for the maintainers of those crates. So the best option might still be our own |
@tniessen et al: Would you like some help finishing this? I'm interested in a good WASM Prolog too and have free time to contribute. |
@mpabst: I only want to add that I would greatly appreciate you looking into targeting WASM, and please let me know if I can help in any way by ensuring that the cryptographic libraries can be ported! It seems that simply outlining the steps and issues you encountered would already be a great contribution! Thank you a lot! |
@tniessen: Please have a look at the latest developments in the https://github.com/mthom/scryer-prolog/tree/rebis-dev Notably, db20cda changes many occurrences of |
Ok! I've made a PR. @triska It looks like there are still a lot of things left to do to make it truly usable, but I'll try :) |
I have changed all the commented codes to use guards like I'll hopefully push the changes tomorrow in the existing PR first. |
Thank you a lot, this sounds awesome! It is great to hear that ring and thus all the cryptographic functionality of Scryer Prolog will be available in the WASM build! While you are making so fast progress, you can also simply force-push to the branch so that always the latest changes are easily visible for everyone interested in this topic! And yes, for self-contained changes that are immediately applicable and which do not cause any regressions (on any platform), please do file separate PRs if possible, so that they can be easily merged as soon as possible. I look forward to all improvements in these areas! |
… the upcoming wasm32 support (see mthom#615).
I'm made another PR for the optional features, most of my changes are there. After that is merged, the more "hardcore" 32-bit related changes will be much easier to review. There is one thing though: exactly one crypto function is implemented using sodiumoxide, which has to be marked as an optional feature. It will be great if that can be implemented with ring instead, so we can remove that dependency. Is it possible? @triska |
… the upcoming wasm32 support (see mthom#615).
@rujialiu: Thank you so much for working on this! Yes, it seems possible that we can use the excellent |
I have filed #1970 to eliminate the sodiumoxide dependency by using |
Anyone interested in supporting wasix? It's relatively new, wasmer-only (currently), but the featureset is too attractive. Here is a list from https://wasmer.io/posts/announcing-wasix
So for example hyper should work out-of-box. But personally I don't need these in near future :) |
I decided to tackle i686 32-bit platform before wasi because it's easier to debug :) Here is current output of
Could @mthom or @triska make some guesses about potential causes? Since some tests already passed, but some are not, maybe you can guess from "what's different between those tests", which I'm totally clueless... Or maybe some more low-level tests that I can understand easier? Thanks a lot! |
Getting closer! Only 3 tests failing. It looks like UntypedArenaPtr points to an ArenaHeader (instead of a pointer to it) which is stored at the end of AllocSlab, so I changed this: fn header_offset_from_payload() -> usize {
mem::size_of::<*const ArenaHeader>()
} to: fn header_offset_from_payload() -> usize {
mem::size_of::<ArenaHeader>()
} However, for some reason, in target After fixing some other minor issue that I forgot, only three tests are failing: heap_stackless_iter_tests, heap_marking_tests and heap_stackless_post_order_iter. And after skipping these test, all remaining 23 test can also pass:
So I guess we're pretty close to getting i686-pc-windows-msvc to work! Any hunch/suggestions are welcome! For example, are there any other fixed-layout structs that needs BTW: It's a bit surprising that failing these 3 tests did not prevent us from succeeding these "prolog program tests" :) |
Ah, I forgot that |
Awesome, thank you a lot! Could you please push these changes somewhere (I recommend a PR) and explain how we can try them? I am very interested in 32-bit support! |
Ok! Great to see the "optional-features" merged, will try to make a new PR soon. |
It will likely take some time. I have zero ideas about Rust. Most of the original problems with SWI port got eventually solved and building the wasm version of SWI is now officially supported. It also has auto-yielding from VM which solved the blocked terminal problem. The hundreds of lines of handwritten JS binding code are now distributed with SWI. All of this is wrapped into the very high-quality swipl-wasm NPM package with Typescript bindings. Building everything is done automatically through GH Actions. Getting there took years of work from many people. Currently the biggest issue with scryer-wasm I see is the lack of clear building instructions. I have zero ideas how to compile any Rust code. It should be a list of commands that you can copy-paste into a terminal and execute them. Even better would be a docker file that installs all required tools, pull code, applies patches, builds everything and finally gives you the runnable files. Some sort of online demo would be really nice. Beyond there could be some work on the actual interfaces for the Prolog user: how to interact with the browser; use the DOM; run code from JS etc. |
@rla: I have added build instructions at #1966 (comment), I hope this helps! |
…ently 6/12 crypto functions supported. Tested in browser with all default features disabled.
…ently 6/12 crypto functions supported. Tested in browser with all default features disabled.
Basic WebAssembly support with minimal Javascript API #615
@rla: Scryer Prolog can now be compiled to WebAssembly, and the README explains the necessary steps, please take a look: https://github.com/mthom/scryer-prolog#building-webassembly @infogulch: Do you think it would be possible to build such a version automatically as one of the configured targets? |
@rujialiu: Regarding wasmer, there is currently an interesting discussion in Zig that seems worth considering, for example I found this recent post by a Zig developer: |
Yes, I read that one as soons as it was posted because I'm following zig's development 8-) @triska |
Hello @mthom! Would it be possible to compile and run scryer as a WebAssembly binary? I worked some time ago on SWI-Prolog WebAssembly port but it turned out to be rather difficult due to:
Also, it was quite inconvenient to provide API for JavaScript. It takes hundreds of lines of handwritten code to bind JS functions to low-level FLI, a SWI-Prolog's FFI.
More info: SWI-Prolog/roadmap#43
It seems like Rust has some support for WebAssembly (https://developer.mozilla.org/en-US/docs/WebAssembly/Rust_to_wasm).
What do you think:
The text was updated successfully, but these errors were encountered: