WasmEdge C SDK

The WasmEdge C API denotes an interface to embed the WasmEdge runtime into a C program. The followings are the quick start guides to working with the C APIs of WasmEdge. For the details of the WasmEdge C API, please refer to the full documentation.

The WasmEdge C API also the fundamental API for other languages’ SDK.

Quick Start Guide for the WasmEdge runner

The following is an example for running a WASM file. Assume that the WASM file fibonacci.wasm is copied into the current directory, and the C file test_wasmedge.c is as following:

1. #include <wasmedge/wasmedge.h>
2. #include <stdio.h>
3. int main(int Argc, const char* Argv[]) {
4. /* Create the configure context and add the WASI support. */
5. /* This step is not necessary unless you need WASI support. */
6. WasmEdge_ConfigureContext *ConfCxt = WasmEdge_ConfigureCreate();
7. WasmEdge_ConfigureAddHostRegistration(ConfCxt, WasmEdge_HostRegistration_Wasi);
8. /* The configure and store context to the VM creation can be NULL. */
9. WasmEdge_VMContext *VMCxt = WasmEdge_VMCreate(ConfCxt, NULL);

11. /* The parameters and returns arrays. */
12. WasmEdge_Value Params[1] = { WasmEdge_ValueGenI32(32) };
13. WasmEdge_Value Returns[1];
14. /* Function name. */
15. WasmEdge_String FuncName = WasmEdge_StringCreateByCString("fib");
16. /* Run the WASM function from file. */
17. WasmEdge_Result Res = WasmEdge_VMRunWasmFromFile(VMCxt, Argv[1], FuncName, Params, 1, Returns, 1);

19. if (WasmEdge_ResultOK(Res)) {
20. printf("Get result: %d\n", WasmEdge_ValueGetI32(Returns[0]));
21. } else {
22. printf("Error message: %s\n", WasmEdge_ResultGetMessage(Res));
23. }

25. /* Resources deallocations. */
26. WasmEdge_VMDelete(VMCxt);
27. WasmEdge_ConfigureDelete(ConfCxt);
28. WasmEdge_StringDelete(FuncName);
29. return 0;
30. }

Then you can compile and run: (the 32th fibonacci number is 3524578 in 0-based index)

1. $ gcc test_wasmedge.c -lwasmedge_c -o test_wasmedge
2. $ ./test_wasmedge fibonacci.wasm
3. Get result: 3524578

Quick Start Guide for the WasmEdge AOT compiler

Assume that the WASM file fibonacci.wasm is copied into the current directory, and the C file test_wasmedge_compiler.c is as following:

1. #include <wasmedge/wasmedge.h>
2. #include <stdio.h>
3. int main(int Argc, const char* Argv[]) {
4. /* Create the configure context. */
5. WasmEdge_ConfigureContext *ConfCxt = WasmEdge_ConfigureCreate();
6. /* ... Adjust settings in the configure context. */
7. /* Result. */
8. WasmEdge_Result Res;

10. /* Create the compiler context. The configure context can be NULL. */
11. WasmEdge_CompilerContext *CompilerCxt = WasmEdge_CompilerCreate(ConfCxt);
12. /* Compile the WASM file with input and output paths. */
13. Res = WasmEdge_CompilerCompile(CompilerCxt, Argv[1], Argv[2]);
14. if (!WasmEdge_ResultOK(Res)) {
15. printf("Compilation failed: %s\n", WasmEdge_ResultGetMessage(Res));
16. return 1;
17. }

19. WasmEdge_CompilerDelete(CompilerCxt);
20. WasmEdge_ConfigureDelete(ConfCxt);
21. return 0;
22. }

Then you can compile and run (the output file is fibonacci.wasm.so):

1. $ gcc test_wasmedge_compiler.c -lwasmedge_c -o test_wasmedge_compiler
2. $ ./test_wasmedge_compiler fibonacci.wasm fibonacci.wasm.so
3. [2021-07-02 11:08:08.651] [info] compile start
4. [2021-07-02 11:08:08.653] [info] verify start
5. [2021-07-02 11:08:08.653] [info] optimize start
6. [2021-07-02 11:08:08.670] [info] codegen start
7. [2021-07-02 11:08:08.706] [info] compile done

The compiled-WASM file can be used as a WASM input for the WasmEdge runner. The following is the comparison of the interpreter mode and the AOT mode:

1. $ time ./test_wasmedge fibonacci.wasm
2. Get result: 5702887

4. real 0m2.715s
5. user 0m2.700s
6. sys 0m0.008s

8. $ time ./test_wasmedge fibonacci.wasm.so
9. Get result: 5702887

11. real 0m0.036s
12. user 0m0.022s
13. sys 0m0.011s