#运行时 #C API

wasmer-c-api-near

Wasmer C API库

1个稳定版本

1.0.1 2021年2月5日

287 / WebAssembly

MIT 协议

2.5MB
26K SLoC

wasmer-c-api 构建状态 加入Wasmer Slack MIT License

这个crate公开了Wasmer运行时的C和C++ API。它也完全支持wasm-c-api通用API

用法

一旦您在系统中安装了Wasmer,相关的共享对象文件头文件就会在Wasmer安装路径中可用。

$WASMER_DIR/
  lib/
    libwasmer.{so,dylib,dll}
  include/
    wasm.h
    wasmer.h
    wasmer.hh
    wasmer_wasm.h

Wasmer二进制文件还包含wasmer-config,这是一个工具,可以输出编译使用Wasmer的程序所需配置信息。

完整的C API文档可以在这里找到:https://wasmerio.github.io/wasmer/c-api/

以下是一个使用C API的简单示例

#include <stdio.h>
#include "wasmer_wasm.h"

int main(int argc, const char* argv[]) {
    const char *wat_string =
        "(module\n"
        "  (type $sum_t (func (param i32 i32) (result i32)))\n"
        "  (func $sum_f (type $sum_t) (param $x i32) (param $y i32) (result i32)\n"
        "    local.get $x\n"
        "    local.get $y\n"
        "    i32.add)\n"
        "  (export \"sum\" (func $sum_f)))";

    wasm_byte_vec_t wat;
    wasm_byte_vec_new(&wat, strlen(wat_string), wat_string);
    wasm_byte_vec_t wasm_bytes;
    wat2wasm(&wat, &wasm_bytes);

    printf("Creating the store...\n");
    wasm_engine_t* engine = wasm_engine_new();
    wasm_store_t* store = wasm_store_new(engine);

    printf("Compiling module...\n");
    wasm_module_t* module = wasm_module_new(store, &wasm_bytes);

    if (!module) {
        printf("> Error compiling module!\n");
        return 1;
    }

    wasm_byte_vec_delete(&wasm_bytes);

    printf("Creating imports...\n");
    wasm_extern_vec_t import_object = WASM_EMPTY_VEC;

    printf("Instantiating module...\n");
    wasm_instance_t* instance = wasm_instance_new(store, module, &import_object, NULL);

    if (!instance) {
      printf("> Error instantiating module!\n");
      return 1;
    }

    printf("Retrieving exports...\n");
    wasm_extern_vec_t exports;
    wasm_instance_exports(instance, &exports);

    if (exports.size == 0) {
        printf("> Error accessing exports!\n");
        return 1;
    }

    printf("Retrieving the `sum` function...\n");
    wasm_func_t* sum_func = wasm_extern_as_func(exports.data[0]);

    if (sum_func == NULL) {
        printf("> Failed to get the `sum` function!\n");
        return 1;
    }

    printf("Calling `sum` function...\n");
    wasm_val_t args_val[2] = { WASM_I32_VAL(3), WASM_I32_VAL(4) };
    wasm_val_t results_val[1] = { WASM_INIT_VAL };
    wasm_val_vec_t args = WASM_ARRAY_VEC(args_val);
    wasm_val_vec_t results = WASM_ARRAY_VEC(results_val);

    if (wasm_func_call(sum_func, &args, &results)) {
        printf("> Error calling the `sum` function!\n");

        return 1;
    }

    printf("Results of `sum`: %d\n", results_val[0].of.i32);

    wasm_func_delete(sum_func);
    wasm_module_delete(module);
    wasm_instance_delete(instance);
    wasm_store_delete(store);
    wasm_engine_delete(engine);
}

构建

您可以从源代码编译Wasmer共享库

make build-capi

这将生成共享库(取决于您的系统)

  • Windows: target/release/libwasmer_c_api.dll
  • macOS: target/release/libwasmer_runtime_c_api.dylib
  • Linux: target/release/libwasmer_runtime_c_api.so

如果您想以用法中所示的方式生成库和头文件,可以在Wasmer根目录中执行以下命令

make package-capi

此命令将生成一个package目录,您可以在Wasmer C API 示例中轻松使用。

测试

测试使用库的发布版构建运行。如果您进行更改或使用非默认功能编译,请确保以发布模式重新构建,以便测试能够看到更改。

要运行全部测试套件,请进入 Wasmer 根目录并运行以下命令

$ make test-capi

wasmer config

wasmer config输出用于编译使用 Wasmer 的程序的所需的各种配置信息。

wasmer config--pkg-config

它以pkg-config格式输出编译和链接程序到 Wasmer 所需的必要详细信息

$ wasmer config --pkg-config > $PKG_CONFIG_PATH/wasmer.pc

wasmer config--includedir

包含 Wasmer 头文件的目录

$ wasmer config --includedir
/users/myuser/.wasmer/include

wasmer config--libdir

包含 Wasmer 库的目录

$ wasmer config --libdir
/users/myuser/.wasmer/lib

wasmer config--libs

链接到 Wasmer 组件所需的库

$ wasmer config --libs
-L/Users/myuser/.wasmer/lib -lwasmer

wasmer config--libs

链接到 Wasmer 组件所需的库

$ wasmer config --cflags
-I/Users/myuser/.wasmer/include/wasmer

许可证

Wasmer 主要按照MIT 许可协议LICENSE)分发。

依赖项

~8–22MB
~332K SLoC