1个稳定版本

1.0.1 2021年2月5日

#36#wasm-engine

23 每月下载次数
用于 wasmer-c-api-near

MIT 许可证

605KB
12K SLoC

Wasmer引擎对象文件

这是一个为wasmer WebAssembly虚拟机编写的引擎

此引擎用于生成一个本地对象文件,可以与之链接,为编译的模块提供一个沙箱化的WebAssembly运行时环境,无需运行时编译。

使用示例

首先,我们使用Wasmer编译我们的WebAssembly文件

wasmer compile path/to/wasm/file.wasm --llvm --object-file -o my_wasm.o --header my_wasm.h

然后,您将看到类似以下输出

Engine: objectfile
Compiler: llvm
Target: x86_64-apple-darwin
✔ File compiled successfully to `my_wasm.o`.
✔ Header file generated successfully at `my_wasm.h`.

现在让我们创建一个程序来链接这个对象文件。

#ifdef __cplusplus
extern "C" {
#endif

#include "wasmer_wasm.h"
#include "wasm.h"
#include "my_wasm.h"

#include <stdio.h>
#include <stdlib.h>

#define own

#ifdef __cplusplus
}
#endif

void print_wasmer_error()
{
    int error_len = wasmer_last_error_length();
    printf("Error len: `%d`\n", error_len);
    char* error_str = (char*) malloc(error_len);
    wasmer_last_error_message(error_str, error_len);
    printf("Error str: `%s`\n", error_str);
}

int main() {
        printf("Initializing...\n");
        wasm_config_t* config = wasm_config_new();
        wasm_config_set_engine(config, OBJECT_FILE);
        wasm_engine_t* engine = wasm_engine_new_with_config(config);
        wasm_store_t* store = wasm_store_new(engine);

        wasm_module_t* module = wasmer_object_file_engine_new(store, "qjs.wasm");
        if (! module) {
                printf("Failed to create module\n");
                print_wasmer_error();
                return -1;
        }
        
        // We have now finished the memory buffer book keeping and we have a valid Module.

        // In this example we're passing some JavaScript source code as a command line argument
        // to a WASI module that can evaluate JavaScript.
        wasi_config_t* wasi_config = wasi_config_new("constant_value_here");
        const char* js_string = "function greet(name) { return JSON.stringify('Hello, ' + name); }; print(greet('World'));";
        wasi_config_arg(wasi_config, "--eval");
        wasi_config_arg(wasi_config, js_string);
        wasi_env_t* wasi_env = wasi_env_new(wasi_config);
        if (!wasi_env) {
                printf("> Error building WASI env!\n");
                print_wasmer_error();
                return 1;
        }

        wasm_importtype_vec_t import_types;
        wasm_module_imports(module, &import_types);
        int num_imports = import_types.size;
        wasm_extern_t** imports = (wasm_extern_t**) malloc(num_imports * sizeof(wasm_extern_t*));
        wasm_importtype_vec_delete(&import_types);
        
        bool get_imports_result = wasi_get_imports(store, module, wasi_env, imports);
        if (!get_imports_result) {
                printf("> Error getting WASI imports!\n");
                print_wasmer_error();
                return 1;
        }

        wasm_instance_t* instance = wasm_instance_new(store, module, (const wasm_extern_t* const*) imports, NULL);
        if (! instance) {
                printf("Failed to create instance\n");
                print_wasmer_error();
                return -1;
        }
        wasi_env_set_instance(wasi_env, instance);
        
        // WASI is now set up.
        own wasm_func_t* start_function = wasi_get_start_function(instance);
        if (!start_function) {
                fprintf(stderr, "`_start` function not found\n");
                print_wasmer_error();
                return -1;
        }

        fflush(stdout);
        own wasm_trap_t* trap = wasm_func_call(start_function, NULL, NULL);
        if (trap) {
                fprintf(stderr, "Trap is not NULL: TODO:\n");
                return -1;
        }

        wasm_instance_delete(instance);
        wasm_module_delete(module);
        wasm_store_delete(store);
        wasm_engine_delete(engine);
        return 0;
}

我们将该源代码保存到 test.c 并运行

clang -O2 -c test.c -o test.o

现在我们只需要将所有这些链接在一起

clang -O2 test.o my_wasm.o libwasmer_c_api.a

我们将创建的对象文件与我们的C代码、由Wasmer生成的对象文件以及 libwasmer_c_api 链接在一起,并生成一个可调用我们编译的WebAssembly的可执行文件!

依赖项

~9–20MB
~289K SLoC