How do I use a C library in a Rust library compiled to WebAssembly?

TL;DR: Jump to “New week, new adventures” in order to get “Hello from C and Rust!” The nice way would be creating a WASM library and passing it to the linker. rustc has an option for that (and there seem to be source-code directives too): rustc <yourcode.rs> –target wasm32-unknown-unknown –crate-type=cdylib -C link-arg=<library.wasm> The trick is … Read more

How to compile Java to WASM (WebAssembly)?

Here are a few compilers that can do this for you: https://github.com/konsoletyper/teavm (most popular & my own reccomendation: https://teavm.org/) https://github.com/i-net-software/JWebAssembly (only to webassembly, the others can do webassembly and javascript) https://github.com/mirkosertic/Bytecoder https://github.com/leaningtech/cheerpj-meta Do note, that all of them have their limitations, most commonly that every Java class in Java’s standard library won’t work well with … Read more

How to use alert(),confirm() and prompt() function using Blazor?

Unfortunately, there is not implementation of such useful functions in Blazor yet. So you need to use JSRuntime instance. @inject IJSRuntime JsRuntime … @code { //… await JsRuntime.InvokeVoidAsync(“alert”, “Warning!”); // Alert bool confirmed = await JsRuntime.InvokeAsync<bool>(“confirm”, “Are you sure?”); // Confirm string prompted = await JsRuntime.InvokeAsync<string>(“prompt”, “Take some input:”); // Prompt //… } It makes … Read more

Access the DOM in WebAssembly

I have recently started using Web Assembly for some of my projects. I am doing this because I heard that wasm is faster than javascript. WebAssembly is faster than JavaScript, but only for certain use-cases. With WebAssembly your browser has to do less work to download and compile your code, giving it faster start-up times. … Read more

using WebAssembly in chrome extension

I’ve been fiddling with WebAssembly recently, and found a way to make it work. Here are the script files: main.js chrome.browserAction.onClicked.addListener(function(tab) { chrome.tabs.executeScript(null, {file: “content_script.js”}); }); content_script.js var importObject = { imports: { imported_func: arg => console.log(arg) } }; url=”data:application/wasm;base64,” + “AGFzbQEAAAABCAJgAX8AYAAAAhkBB2ltcG9ydHMNaW1wb3J0ZWRfZnVuYwAAAwIBAQcRAQ1leHBvcnRlZF9mdW5jAAEKCAEGAEEqEAAL”; WebAssembly.instantiateStreaming(fetch(url), importObject) .then(obj => obj.instance.exports.exported_func()); The data URL belongs to the common tutorial … Read more

How do I compile a C++ file to WebAssembly?

I found this gist to be very helpful. Basically, this are the steps: (build llvm and clang 5.0.0 or above with -DLLVM_EXPERIMENTAL_TARGETS_TO_BUILD=WebAssembly) Compile the .cpp soure to llvm bitcode with clang: clang -emit-llvm –target=wasm32 -Oz math.cpp -c -o math.bc Compile the bitcode to s-assembly: llc -asm-verbose=false -o math.s math.bc Use binaryen’s s2wasm tool to create … Read more

Why the JVM cannot be used in place of WebAssembly?

There are a great many reasons why the JVM was not deemed a suitable runtime in place of WebAssembly … WebAssembly was designed with delivery-over-HTTP and browser-based in mind. For that reason, it supports streaming compilation – i.e. you can start compiling the code while downloading. WebAssembly was designed to have rapid compilation times (resulting … Read more

How can I return a JavaScript string from a WebAssembly function

WebAssembly doesn’t natively support a string type, it rather supports i32 / i64 / f32 / f64 value types as well as i8 / i16 for storage. You can interact with a WebAssembly instance using: exports, where from JavaScript you call into WebAssembly, and WebAssembly returns a single value type. imports where WebAssembly calls into … Read more

How can one generate and save a file client side using Blazor?

Creator of Blazor Steve Sanderson used JavaScript interop for similar task during one of his last presentations. You can find example on BlazorExcelSpreadsheet Solution includes three parts: 1) JavaScript function saveAsFile(filename, bytesBase64) { var link = document.createElement(‘a’); link.download = filename; link.href = “https://stackoverflow.com/questions/52683706/data:application/octet-stream;base64,” + bytesBase64; document.body.appendChild(link); // Needed for Firefox link.click(); document.body.removeChild(link); } 2) C# … Read more

Blazor performance

Is it true that the browser will need to download the WebAssembly library every time the page loads? No, browsers can cache the files. Common CDNs for Blazor applications will do the trick. Is this system faster to work than, for example, React / Vue.js, compiled in JavaScript? Blazor uses WebAssembly, On paper WebAssembly should … Read more

tech