I have a successfully working spellchecker using web-assembly that I use from my web page. This spellchecker was built using C++ and Emscripten. It was based on the similar project that uses Hunspell but I am using the entire vocabulary as data inside the wasm. It works in a very simple way.
the create method call (made only once at the start) will setup the vocabulary in the form of a hash-map and returns a 'handle' back to the calling JavaScript code
later when the user enters a word, JavaScript calls 'suggest' method with the same handle and the word as arguments that will search the hash-map (and do some string manipulations) and finds suggestions and returns them.
Now the question about Rust. (I have created and run some example libs that interact between JavaScript and wasm using wasm-bindgen. ... but cannot proceed more)
I want to achieve the above using Rust. However I am not able to find a way to
create the object and return the handle at the start
should I create a lib or binary...
Please point me into right direction so that I can make a start.
Please let me know what more details I need to provide.
Are you aware of the Rust and WebAssembly book? It provides a good introduction to compiling Rust to WASM and might be helpful to you if you haven't already known about it:
Thanks for your prompt response.
Yes. Like I said above I have used that information and am able to bring up a wasm lib that can interact with JavaScript (to and from) in my webpage .... but that cannot create an 'object' that will remain in the memory forever and will 'serve' any requests made against it later.
Hope I have made myself clear
Okay. If I understand this, you are populating the hash-map in the main function. How can I do this in a library ..........Library does not have a main function. Correct me if wrong.
Can web-assembly be used for Rust binaries like it can be used for Rust libraries?, If yes, how?
That's the beauty of LazyLock, it will populate the first time it is accessed.
So you can put your static LazyLock in your lib.
The first time you access it, it will populate.