Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to transfer arguments to WebAssembly binary generated from C?

See original GitHub issue

Hello, I converted below C code to WebAssembly binary using wasi-sdk-14 (I downloaded wasi-sdk-14.0-macos.tar.gz)

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

int main (int argc, char ** argv) {
    char * buf = (char *) malloc(100);
    memcpy(buf, argv[1], 100);
    printf("buf: %s\n", buf); 
    return 0;

And I am using below script to run the WebAssembly binary file which name is prog.wasm.

open System
open Wasmtime

let main _ =
  let path = "prog.wasm"
  let engine = new Engine()
  let wm = Module.FromFile(engine, path)
  let linker = new Linker(engine)
  let store = new Store(engine)

  let cfg = new WasiConfiguration()


  let instance = linker.Instantiate(store, wm)
  let func = instance.GetFunction(store, "_start")
  let result = func.Invoke(store)

What I want is to transfer arguments to the binary. But _start function does not have any arguments. I guess _start function is entry point of code which executes main function…

So my question is how to transfer arguments to the binary generated from C code? Additionally, I also wonder that how to pass standard input.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5

github_iconTop GitHub Comments

iwatecommented, Apr 6, 2022


When you add WithInheritedArgs() config, the args of your app will be passed to wasmtime. So, WithInheritedArgs() equals to .WithArgs(Environment.GetCommandLineArgs())

The screenshot’s program and command line arguments print this output.

buf: Hello

So, -- is placeholder for arg0 which is insted of path/to/the/app.dll.

iwatecommented, Apr 6, 2022

I think WithArgs can do it.

let cfg = (new WasiConfiguration()).WithArgs("--", "Hello,World!")

This is my code for learning. I hope this will help.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to pass command line arguments to C code with ...
From there, it was as simple as calling _start from the wasm 's exported functions.
Read more >
Compiling C to WebAssembly and Running It - Depth-First
This article shows how to compile C functions to WebAssembly without Emscripten, and then execute the result in a browser. Only a recent...
Read more >
wasm2c: Convert wasm files to C source and header
It is a variadic function where the first two arguments give the number of parameters and results, and the following arguments are the...
Read more >
Pragmatic compiling of C++ to WebAssembly. A Guide.
Most C++-Programmers I know of have already heard of WebAssembly, but most have had troubles getting started. This guide brings you beyond of...
Read more >
Compiling a New C/C++ Module to WebAssembly
When you've written a new code module in a language like C/C++, you can compile it into WebAssembly using a tool like Emscripten....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Post

No results found

github_iconTop Related Hashnode Post

No results found