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);
  10. /* The parameters and returns arrays. */
  11. WasmEdge_Value Params[1] = { WasmEdge_ValueGenI32(32) };
  12. WasmEdge_Value Returns[1];
  13. /* Function name. */
  14. WasmEdge_String FuncName = WasmEdge_StringCreateByCString("fib");
  15. /* Run the WASM function from file. */
  16. WasmEdge_Result Res = WasmEdge_VMRunWasmFromFile(VMCxt, Argv[1], FuncName, Params, 1, Returns, 1);
  17. if (WasmEdge_ResultOK(Res)) {
  18. printf("Get result: %d\n", WasmEdge_ValueGetI32(Returns[0]));
  19. } else {
  20. printf("Error message: %s\n", WasmEdge_ResultGetMessage(Res));
  21. }
  22. /* Resources deallocations. */
  23. WasmEdge_VMDelete(VMCxt);
  24. WasmEdge_ConfigureDelete(ConfCxt);
  25. WasmEdge_StringDelete(FuncName);
  26. return 0;
  27. }

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

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
  3. real 0m2.715s
  4. user 0m2.700s
  5. sys 0m0.008s
  6. $ time ./test_wasmedge fibonacci.wasm.so
  7. Get result: 5702887
  8. real 0m0.036s
  9. user 0m0.022s
  10. sys 0m0.011s