wasi使用筆記

.c編譯成.wasm

  1. 第一個工具: wasmcc
    安裝
    curl https://raw.githubusercontent.com/wasienv/wasienv/master/install.sh | sh
    https://github.com/wasienv/wasienv
    其中安裝完成之后會包含很多個工具,wasirun可以運行wasm代碼
$ wasirun hello.wasm
Hello wasi
  1. emscripten
    https://segmentfault.com/a/1190000014208777
git clone https://github.com/juj/emsdk.git
cd emsdk
emsdk update
emsdk install latest
emsdk activate latest
//如果沒有出現(xiàn)emcc命令:
source ./emsdk_env.sh

編譯命令:emcc hello.c -s WASM=1 -o hello.wasm

c代碼調(diào)用wasm代碼

https://docs.wasmer.io/integrations/c/setup
環(huán)境設置:

# Extract the contents to a dir
mkdir wasmer-c-api
tar -C wasmer-c-api -zxvf wasmer-c-api*.tar.gz

export WASMER_C_API=`pwd`/wasmer-c-api

# Update LD_LIBRARY_PATH to link against the libwasmer.so in the examples
export LD_LIBRARY_PATH=`pwd`/wasmer-c-api/lib/:$LD_LIBRARY_PATH

編譯命令:

靜態(tài)鏈接:gcc test.c -I${WASMER_C_API}/include -L${WASMER_C_API}/lib -lwasmer -o test
動態(tài)鏈接:gcc -static main.c -I${WASMER_C_API}/include -L${WASMER_C_API}/lib -lwasmer -lpthread -lm -ldl -o main
  1. 例子
    在這里有很多wasi使用的例子
    https://docs.wasmer.io/integrations/c/setup
    但是沒看見直接運行整個wasm代碼的(例子是調(diào)用wasm函數(shù)的)
// wasm部分
#include<stdio.h>

int main()
{
    char s[20] = {0};
    scanf("%s", s);
    puts(s);
    return 0;
}
// c部分
#include <stdio.h>
#include "wasmer.h"
#include <assert.h>
#include <stdint.h>

// Function to print the most recent error string from Wasmer if we have them
void print_wasmer_error()
{
  int error_len = wasmer_last_error_length();
  char *error_str = malloc(error_len);
  wasmer_last_error_message(error_str, error_len);
  printf("Error: `%s`\n", error_str);
}

int main()
{
    FILE *file = fopen("hello.wasm", "r");
    assert(file != NULL);
    fseek(file, 0, SEEK_END);
    long len = ftell(file);
    uint8_t *bytes = (uint8_t*)malloc(len);
    fseek(file, 0, SEEK_SET);
    fread(bytes, 1, len, file);
    fclose(file);

    wasmer_module_t *module = NULL;
    wasmer_result_t compile_result = wasmer_compile(&module, bytes, len);
    if (compile_result != WASMER_OK)
    {
        print_wasmer_error();
        return -1;
    }
    wasmer_import_object_t *wasi_import_obj = wasmer_wasi_generate_default_import_object();

    // find out what version of WASI the module is
    Version wasi_version = wasmer_wasi_get_version(module);
    // char* progname = "ProgramName";
    // wasmer_byte_array args[] = { { .bytes = progname; .bytes_len = sizeof(progname); } };
    wasmer_import_object_t * import_object = wasmer_wasi_generate_import_object_for_version(wasi_version, 0, 1, NULL, 0, NULL, 0, NULL, 0);

    // Instantiate a WebAssembly Instance from Wasm bytes and imports
    wasmer_instance_t *instance = NULL;
    // clock_gettime(CLOCK_REALTIME, &start);
    wasmer_result_t instantiate_result = wasmer_module_import_instantiate(&instance, module, import_object);

    if(instantiate_result != WASMER_OK)
    {
        print_wasmer_error();
        return -1;
    }
    wasmer_value_t arguments[] = {0};
    wasmer_value_t results[] = {0};
    // Call the `sum` function with the prepared arguments and the return value.
    wasmer_result_t call_result = wasmer_instance_call(instance, "_start", arguments, 0, results, 1);
    int response_value = results[0].value.I32; 
    printf("%d\n", response_value);
    wasmer_instance_destroy(instance);
    return 0;
}

調(diào)用_start函數(shù)相當于運行整個wasm代碼

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。