Stream: wasm

Topic: How to run Javascript from C# using WebAssembly?


view this post on Zulip Tim Hardy (Apr 12 2023 at 18:35):

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#.

view this post on Zulip Lann Martin (Apr 12 2023 at 18:42):

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

A library for adding scripting to .NET applications. Supports V8 (Windows, Linux, macOS) and JScript/VBScript (Windows). - GitHub - microsoft/ClearScript: A library for adding scripting to .NET app...

view this post on Zulip Lann Martin (Apr 12 2023 at 18:43):

Indeed you should even be able to run Wasm inside ClearScript with the V8 engine :smile:

view this post on Zulip Michael Ball (Apr 13 2023 at 13:52):

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.

A .NET embedding of Wasmtime. Wasmtime is a standalone runtime for WebAssembly, using the Cranelift JIT compiler. The .NET embedding of Wasmtime enables .NET code to instantiate WebAssembly modules and to interact with them in-process.

view this post on Zulip Tim Hardy (Apr 13 2023 at 16:38):

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.

view this post on Zulip Tim Hardy (Apr 13 2023 at 16:42):

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.

view this post on Zulip Michael Ball (Apr 13 2023 at 18:57):

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

JavaScript can be compiled to WebAssembly. There are even multiple implementations of JavaScript WebAssembly runtimes.

view this post on Zulip Tim Hardy (Apr 14 2023 at 21:38):

Does this help? https://www.fermyon.com/wasm-languages/javascript

I will take a look. Thanks!

JavaScript can be compiled to WebAssembly. There are even multiple implementations of JavaScript WebAssembly runtimes.

view this post on Zulip Scott Waye (Apr 19 2023 at 12:59):

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

This repo is for experimentation and exploring new ideas that may or may not make it into the main dotnet/runtime repo. - runtimelab/HelloWasm.cs at feature/NativeAOT-LLVM · dotnet/runtimelab

Last updated: Nov 22 2024 at 16:03 UTC