How to transfer arguments to WebAssembly binary generated from C?
See original GitHub issueHello, 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
[<EntryPoint>]
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()
linker.DefineWasi()
store.SetWasiConfiguration(cfg)
let instance = linker.Instantiate(store, wm)
let func = instance.GetFunction(store, "_start")
let result = func.Invoke(store)
0
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:
- Created a year ago
- Comments:5
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@kimdora
When you add
WithInheritedArgs()
config, the args of your app will be passed to wasmtime. So,WithInheritedArgs()
equals to.WithArgs(Environment.GetCommandLineArgs())
https://github.com/bytecodealliance/wasmtime-dotnet/blob/9fad6c4e5918798c9b221e42980c6e524984f54c/src/WasiConfiguration.cs#L96-L102The screenshot’s program and command line arguments print this output.
So,
--
is placeholder for arg0 which is insted ofpath/to/the/app.dll
.I think
WithArgs
can do it.This is my code for learning. I hope this will help. https://github.com/iwate/learn-wasm-with-dotnet/blob/ede974d438e908bb36eb6587bbd3e6835cf96836/challenge04/dotnet/Iwate.WasmtimeChallenge04/Program.cs#L56