r/WebAssemblyDev • • Dec 06 '24

CheerpX 1.0 released: high performance x86 virtualization in the browser via WebAssembly

Thumbnail
cheerpx.io
2 Upvotes

r/WebAssemblyDev • • Dec 01 '24

How to compile WABT wasm2c output from Bytecode Alliance Javy (including wasi_snapshot_preview1) to a standalone executable?

3 Upvotes

I compiled JavaScript to Web Assembly using Bytecode Alliance's Javy. I then compiled the .wasm to C using WABT's wasm2c. Now I am trying to compile the resulting C to a standalone executable.

./javy emit-plugin -o plugin.wasm
./javy build -C dynamic -C plugin=plugin.wasm -o javy-permutations.wasm permutations.js

The code can be run using

wasmtime run --preload javy_quickjs_provider_v3=plugin.wasm javy-permutations.wasm

Compiling WASM to C

./wabt/bin/wasm2c javy-permutations.wasm -n array_nth_permutation -o javy-permutations.c
./wabt/bin/wasm2c plugin.wasm -n w2c_javy__quickjs__provider__v3 -o plugin.c

which writes plugin.c and plugin.h to the filesystem. The Javy implementation of WebAssembly toolchain.

I also, in my attempts to replicate a working JavaScript implementation of this using Node.js https://github.com/bytecodealliance/javy/blob/main/docs/docs-using-nodejs.md, compiled wasi_snapshot_preview1.reactor.wasm to C using wasm2c. I have not been able to reproduce what I do using JavaScript in C.

I was able to achieve the result for the factorial example in wasm2c https://github.com/WebAssembly/wabt/blob/main/wasm2c/README.md#tutorial-wat---wasm---c.

There's no examples in wasm2c for compiling WASI to a standalone executable.

Here's javy-permutations.h

/* Automatically generated by wasm2c */
#ifndef JAVY_PERMUTATIONS_H_GENERATED_
#define JAVY_PERMUTATIONS_H_GENERATED_

#include "wasm-rt.h"

#include <stdint.h>

#ifndef WASM_RT_CORE_TYPES_DEFINED
#define WASM_RT_CORE_TYPES_DEFINED
typedef uint8_t u8;
typedef int8_t s8;
typedef uint16_t u16;
typedef int16_t s16;
typedef uint32_t u32;
typedef int32_t s32;
typedef uint64_t u64;
typedef int64_t s64;
typedef float f32;
typedef double f64;
#endif

#ifdef __cplusplus
extern "C" {// 
#endif

struct w2c_javy__quickjs__provider__v3;
extern wasm_rt_memory_t* w2c_javy__quickjs__provider__v3_memory(struct w2c_javy__quickjs__provider__v3*);

typedef struct w2c_array__nth__permutation {
  struct w2c_javy__quickjs__provider__v3* w2c_javy__quickjs__provider__v3_instance;
  /* import: 'javy_quickjs_provider_v3' 'memory' */
  wasm_rt_memory_t *w2c_javy__quickjs__provider__v3_memory;
  bool data_segment_dropped_w2c_array__nth__permutation_d0 : 1;
} w2c_array__nth__permutation;

void wasm2c_array__nth__permutation_instantiate(w2c_array__nth__permutation*, struct w2c_javy__quickjs__provider__v3*);
void wasm2c_array__nth__permutation_free(w2c_array__nth__permutation*);
wasm_rt_func_type_t wasm2c_array__nth__permutation_get_func_type(uint32_t param_count, uint32_t result_count, ...);

/* import: 'javy_quickjs_provider_v3' 'canonical_abi_realloc' */
u32 w2c_javy__quickjs__provider__v3_canonical_abi_realloc(struct w2c_javy__quickjs__provider__v3*, u32, u32, u32, u32);

/* import: 'javy_quickjs_provider_v3' 'invoke' */
void w2c_javy__quickjs__provider__v3_invoke(struct w2c_javy__quickjs__provider__v3*, u32, u32, u32, u32);

extern const u64 wasm2c_array__nth__permutation_min_javy__quickjs__provider__v3_memory;
extern const u64 wasm2c_array__nth__permutation_max_javy__quickjs__provider__v3_memory;
extern const u8 wasm2c_array__nth__permutation_is64_javy__quickjs__provider__v3_memory;

/* export: '_start' */
void w2c_array__nth__permutation_0x5Fstart(w2c_array__nth__permutation*);

#ifdef __cplusplus
}
#endif

#endif  /* JAVY_PERMUTATIONS_H_GENERATED_ */

Here's what I tried so far in main.javy.c

#include <stdio.h>
#include <stdlib.h>

#include "javy-permutations.h"
#include "plugin.h"

int main(int argc, char** argv) {
  /* Make sure there is at least one command-line argument. */
  if (argc < 2) {
    printf("Invalid argument. Expected '%s NUMBER'\n", argv[0]);
    return 1;
  }

  /* Convert the argument from a string to an int. We'll implicitly cast the int
  to a `u32`, which is what `fac` expects. */
  // u32 x = atoi(argv[1]);

  /* Initialize the Wasm runtime. */
  wasm_rt_init();

  /* Declare an instance of the `fac` module. */
  w2c_array__nth__permutation array__nth__permutation;

  w2c_javy__quickjs__provider__v3 provider;

  /* Construct the module instance. */
  wasm2c_array__nth__permutation_instantiate(&array__nth__permutation, &provider);

  /* Call `fac`, using the mangled name. */
  // u32 result = w2c_fac_fac(&fac, x);

  /* Print the result. */
  // printf("fac(%u) -> %u\n", x, result);
  w2c_array__nth__permutation_0x5Fstart(array__nth__permutation*)
  /* Free the fac module. */
  w2c_w2c__javy__0x5Fquickjs__0x5Fprovider__0x5Fv3_canonical_abi_free(w2c_w2c__javy__0x5Fquickjs__0x5Fprovider__0x5Fv3*, u32, u32, u32);

  /* Free the Wasm runtime state. */
  wasm_rt_free();

  return 0;
}

which throws errors when trying to compile

cc -o javy-permutations main.javy.c javy-permutations.c wasm2c/wasm-rt-impl.c wasm2c/wasm-rt-mem-impl.c -Iwasm2c -lm
main.javy.c: In function ‘main’:
main.javy.c:24:3: error: unknown type name ‘w2c_javy__quickjs__provider__v3’; use ‘struct’ keyword to refer to the type
   24 |   w2c_javy__quickjs__provider__v3 provider;
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      |   struct 
main.javy.c:27:72: warning: passing argument 2 of ‘wasm2c_array__nth__permutation_instantiate’ from incompatible pointer type [-Wincompatible-pointer-types]
   27 | ray__nth__permutation_instantiate(&array__nth__permutation, &provider);
      |                                                             ^~~~~~~~~
      |                                                             |
      |                                                             int *

In file included from main.javy.c:4:
javy-permutations.h:37:79: note: expected ‘struct w2c_javy__quickjs__provider__v3 *’ but argument is of type ‘int *’
   37 | _nth__permutation_instantiate(w2c_array__nth__permutation*, struct w2c_javy__quickjs__provider__v3*);
      |                                                             ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

main.javy.c:34:65: error: expected expression before ‘)’ token
   34 |   w2c_array__nth__permutation_0x5Fstart(array__nth__permutation*)
      |                                          

I suspect I need to initialize import wasi_snapshot_preview into the plugin in C, though I am not sure how to do that.


r/WebAssemblyDev • • Nov 22 '24

wasmer-zig-api: Zig bindings for the Wasmer WebAssembly runtime

Thumbnail reddit.com
1 Upvotes

r/WebAssemblyDev • • Nov 20 '24

WebAssembly Won’t Replace Docker Anytime Soon: Docker CTO

Thumbnail
thenewstack.io
3 Upvotes

r/WebAssemblyDev • • Nov 19 '24

Easily Implement Google Authentication in the Blazor WebAssembly App

Thumbnail
syncfusion.com
1 Upvotes

r/WebAssemblyDev • • Nov 15 '24

Visual Basic 6 IDE and language re-created using Avalonia, running on WebAssembly

Thumbnail bandysc.github.io
8 Upvotes

r/WebAssemblyDev • • Nov 14 '24

I wrote a WebAssembly Interpreter in C# (It works in Unity)

7 Upvotes

I've wanted to run wasm modules in Unity for the longest time, but there hasn't been any AOT-only options that work in iOS Unity builds.

So I wrote my own!

https://github.com/kelnishi/WACS

I thought it would be fast and easy, so I buckled down with the spec and got coding.

It was neither fast nor easy.

But... the spec is very good and I eventually got all wast tests passing! Personally, I am going to use this to build plug-in systems for my games. I know many devs would love to have the ability to run downloadable code (in a safe, sandboxed way) in their games as well, so I've open-sourced the project. C# tooling is also unparalleled amongst programming languages, so this project is a great reference if you want to take it apart, trace it, or just see how WebAssembly works (everything in WACS is strongly typed and easily navigable).

I'd love to get some feedback on the code itself, if C# is your language of choice I'd appreciate your code reviews.


r/WebAssemblyDev • • Nov 13 '24

WebVM is a virtual Linux environment running in the browser via WebAssembly

Thumbnail
webvm.io
5 Upvotes

r/WebAssemblyDev • • Nov 12 '24

Modus: an open-source, serverless framework for building APIs powered by WebAssembly

Thumbnail
github.com
2 Upvotes

r/WebAssemblyDev • • Nov 10 '24

Jaws: another JavaScript to WASM compiler

Thumbnail
github.com
2 Upvotes

r/WebAssemblyDev • • Nov 10 '24

Js string builtins make MoonBit genereate wasm much smaller than Rust

Thumbnail
moonbitlang.com
4 Upvotes

r/WebAssemblyDev • • Nov 09 '24

Onyx 0.1.13 released

Thumbnail onyxlang.io
1 Upvotes

r/WebAssemblyDev • • Nov 08 '24

Mewz: Lightweight Execution Environment for WebAssembly with High Isolation and Portability using Unikernels

Thumbnail arxiv.org
5 Upvotes

r/WebAssemblyDev • • Nov 07 '24

ORB: The raw ingredients of WebAssembly made beautifully composable with Elixir

Thumbnail useorb.dev
5 Upvotes

r/WebAssemblyDev • • Nov 06 '24

CVE-2024-51745 - Wasmtime Vulnerability: Bypass Sandboxing via Superscript Device Filenames

Thumbnail vulmon.com
6 Upvotes

r/WebAssemblyDev • • Nov 06 '24

Model Predictive Control in the browser with WebAssembly

Thumbnail garethx.com
1 Upvotes

r/WebAssemblyDev • • Nov 06 '24

GoToSocial WASM-based SQLite driver and BSD

Thumbnail
tumfatig.net
1 Upvotes

r/WebAssemblyDev • • Oct 29 '24

Wasmer 5.0.0 released

Thumbnail
wasmer.io
6 Upvotes

r/WebAssemblyDev • • Oct 28 '24

Securing Stack Smashing Protection in WebAssembly Applications

Thumbnail arxiv.org
2 Upvotes

r/WebAssemblyDev • • Oct 28 '24

Proposal to add stack switching to webassembly, for better support of coroutines

Thumbnail
github.com
2 Upvotes

r/WebAssemblyDev • • Oct 25 '24

Faasm 0.29.0 released

Thumbnail faasm.readthedocs.io
1 Upvotes

r/WebAssemblyDev • • Oct 23 '24

WasmGC and the future of front-end Java development

Thumbnail
infoworld.com
4 Upvotes

r/WebAssemblyDev • • Oct 21 '24

WebAssembly Fantasy Consoles

Thumbnail
c7.se
5 Upvotes

r/WebAssemblyDev • • Oct 21 '24

Performant Bounds Checking for 64-Bit WebAssembly

Thumbnail dl.acm.org
2 Upvotes