I've been trying to figure out how to run some javascript code in a .Net app written in C#, using WebAssembly, but I'm coming up empty. If anyone has any links to guidance on how to get this working, I would greatly appreciate it. The why is that we have some javascript code that is used by many frontend apps, and we would love to be able to use that same code from C#. The alternative is a rather lengthy rewrite of the entire functionality in C#.
While I certainly don't want to discourage you from using wasm, you will probably find it easier to use a JS engine like https://github.com/microsoft/ClearScript in the short term
Indeed you should even be able to run Wasm inside ClearScript with the V8 engine :smile:
Perhaps look into https://www.nuget.org/packages/Wasmtime to run the wasm from within dotnet. I'm also working in C#, may I ask which type of js code it is that you're looking to run? Personally have been thinking about a shared form data validation wasm lib.
We have a rules engine we've created that works great on the front-end. It uses sift.js, an npm package that allows mongo-shell style queries against application state. The only thing stopping us from implementing the same rules engine in C# is that query language. We would like to be able to use the exact same mongo query language to query app state in .Net. I've tried to find a way to leverage the mongoDb drivers against custom objects instead of an actual database to no avail. This question here is to see if it would be possible to just wholesale port the code we have in Javascript (inluding the sift.js npm package) to use in .Net. The third option, which is looking like what we will have to do, is to port sift.js into either a C# or F# library that we write ourselves.
There are plenty of examples of how to run wasm from within dotnet. Where I'm coming up empty is how to get the Javascript into a wasm.
Tim Hardy said:
Where I'm coming up empty is how to get the Javascript into a wasm.
Does this help? https://www.fermyon.com/wasm-languages/javascript
Does this help? https://www.fermyon.com/wasm-languages/javascript
I will take a look. Thanks!
You can call javascript from c# with the experimental NativeAot-LLVM compiler and something like https://github.com/dotnet/runtimelab/blob/feature/NativeAOT-LLVM/src/tests/nativeaot/SmokeTests/HelloWasm/HelloWasm.cs#L3941
Last updated: Nov 22 2024 at 16:03 UTC