Movatterモバイル変換


[0]ホーム

URL:


Wasmtime

    Hello, world!

    You can also browse this source code online and clone the wasmtimerepository to run the example locally:

    This example shows off how to instantiate a simple wasm module and interact withit.

    Wasm Source

    (module  (func $hello (import "" "hello"))  (func (export "run") (call $hello)))

    Host Source

    //! Small example of how to instantiate a wasm module that imports one function,//! showing how you can fill in host functionality for a wasm module.// You can execute this example with `cargo run --example hello`use wasmtime::*;struct MyState {    name: String,    count: usize,}fn main() -> Result<()> {    // First the wasm module needs to be compiled. This is done with a global    // "compilation environment" within an `Engine`. Note that engines can be    // further configured through `Config` if desired instead of using the    // default like this is here.    println!("Compiling module...");    let engine = Engine::default();    let module = Module::from_file(&engine, "examples/hello.wat")?;    // After a module is compiled we create a `Store` which will contain    // instantiated modules and other items like host functions. A Store    // contains an arbitrary piece of host information, and we use `MyState`    // here.    println!("Initializing...");    let mut store = Store::new(        &engine,        MyState {            name: "hello, world!".to_string(),            count: 0,        },    );    // Our wasm module we'll be instantiating requires one imported function.    // the function takes no parameters and returns no results. We create a host    // implementation of that function here, and the `caller` parameter here is    // used to get access to our original `MyState` value.    println!("Creating callback...");    let hello_func = Func::wrap(&mut store, |mut caller: Caller<'_, MyState>| {        println!("Calling back...");        println!("> {}", caller.data().name);        caller.data_mut().count += 1;    });    // Once we've got that all set up we can then move to the instantiation    // phase, pairing together a compiled module as well as a set of imports.    // Note that this is where the wasm `start` function, if any, would run.    println!("Instantiating module...");    let imports = [hello_func.into()];    let instance = Instance::new(&mut store, &module, &imports)?;    // Next we poke around a bit to extract the `run` function from the module.    println!("Extracting export...");    let run = instance.get_typed_func::<(), ()>(&mut store, "run")?;    // And last but not least we can call it!    println!("Calling export...");    run.call(&mut store, ())?;    println!("Done.");    Ok(())}
    /*Example of instantiating of the WebAssembly module and invoking its exportedfunction.You can build using cmake:mkdir build && cd build && cmake .. && cmake --build . --target wasmtime-hello*/#include <assert.h>#include <stdio.h>#include <stdlib.h>#include <wasm.h>#include <wasmtime.h>static void exit_with_error(const char *message, wasmtime_error_t *error,                            wasm_trap_t *trap);static wasm_trap_t *hello_callback(void *env, wasmtime_caller_t *caller,                                   const wasmtime_val_t *args, size_t nargs,                                   wasmtime_val_t *results, size_t nresults) {  (void)env;  (void)caller;  (void)args;  (void)nargs;  (void)results;  (void)nresults;  printf("Calling back...\n");  printf("> Hello World!\n");  return NULL;}int main() {  int ret = 0;  // Set up our compilation context. Note that we could also work with a  // `wasm_config_t` here to configure what feature are enabled and various  // compilation settings.  printf("Initializing...\n");  wasm_engine_t *engine = wasm_engine_new();  assert(engine != NULL);  // With an engine we can create a *store* which is a long-lived group of wasm  // modules. Note that we allocate some custom data here to live in the store,  // but here we skip that and specify NULL.  wasmtime_store_t *store = wasmtime_store_new(engine, NULL, NULL);  assert(store != NULL);  wasmtime_context_t *context = wasmtime_store_context(store);  // Read our input file, which in this case is a wasm text file.  FILE *file = fopen("examples/hello.wat", "r");  assert(file != NULL);  fseek(file, 0L, SEEK_END);  size_t file_size = ftell(file);  fseek(file, 0L, SEEK_SET);  wasm_byte_vec_t wat;  wasm_byte_vec_new_uninitialized(&wat, file_size);  if (fread(wat.data, file_size, 1, file) != 1) {    printf("> Error loading module!\n");    return 1;  }  fclose(file);  // Parse the wat into the binary wasm format  wasm_byte_vec_t wasm;  wasmtime_error_t *error = wasmtime_wat2wasm(wat.data, wat.size, &wasm);  if (error != NULL)    exit_with_error("failed to parse wat", error, NULL);  wasm_byte_vec_delete(&wat);  // Now that we've got our binary webassembly we can compile our module.  printf("Compiling module...\n");  wasmtime_module_t *module = NULL;  error = wasmtime_module_new(engine, (uint8_t *)wasm.data, wasm.size, &module);  wasm_byte_vec_delete(&wasm);  if (error != NULL)    exit_with_error("failed to compile module", error, NULL);  // Next up we need to create the function that the wasm module imports. Here  // we'll be hooking up a thunk function to the `hello_callback` native  // function above. Note that we can assign custom data, but we just use NULL  // for now).  printf("Creating callback...\n");  wasm_functype_t *hello_ty = wasm_functype_new_0_0();  wasmtime_func_t hello;  wasmtime_func_new(context, hello_ty, hello_callback, NULL, NULL, &hello);  wasm_functype_delete(hello_ty);  // With our callback function we can now instantiate the compiled module,  // giving us an instance we can then execute exports from. Note that  // instantiation can trap due to execution of the `start` function, so we need  // to handle that here too.  printf("Instantiating module...\n");  wasm_trap_t *trap = NULL;  wasmtime_instance_t instance;  wasmtime_extern_t import;  import.kind = WASMTIME_EXTERN_FUNC;  import.of.func = hello;  error = wasmtime_instance_new(context, module, &import, 1, &instance, &trap);  if (error != NULL || trap != NULL)    exit_with_error("failed to instantiate", error, trap);  // Lookup our `run` export function  printf("Extracting export...\n");  wasmtime_extern_t run;  bool ok = wasmtime_instance_export_get(context, &instance, "run", 3, &run);  assert(ok);  assert(run.kind == WASMTIME_EXTERN_FUNC);  // And call it!  printf("Calling export...\n");  error = wasmtime_func_call(context, &run.of.func, NULL, 0, NULL, 0, &trap);  if (error != NULL || trap != NULL)    exit_with_error("failed to call function", error, trap);  // Clean up after ourselves at this point  printf("All finished!\n");  ret = 0;  wasmtime_module_delete(module);  wasmtime_store_delete(store);  wasm_engine_delete(engine);  return ret;}static void exit_with_error(const char *message, wasmtime_error_t *error,                            wasm_trap_t *trap) {  fprintf(stderr, "error: %s\n", message);  wasm_byte_vec_t error_message;  if (error != NULL) {    wasmtime_error_message(error, &error_message);    wasmtime_error_delete(error);  } else {    wasm_trap_message(trap, &error_message);    wasm_trap_delete(trap);  }  fprintf(stderr, "%.*s\n", (int)error_message.size, error_message.data);  wasm_byte_vec_delete(&error_message);  exit(1);}
    #include <fstream>#include <iostream>#include <sstream>#include <wasmtime.hh>using namespace wasmtime;std::string readFile(const char *name) {  std::ifstream watFile;  watFile.open(name);  std::stringstream strStream;  strStream << watFile.rdbuf();  return strStream.str();}int main() {  // First the wasm module needs to be compiled. This is done with a global  // "compilation environment" within an `Engine`. Note that engines can be  // further configured through `Config` if desired instead of using the  // default like this is here.  std::cout << "Compiling module\n";  Engine engine;  auto module =      Module::compile(engine, readFile("examples/hello.wat")).unwrap();  // After a module is compiled we create a `Store` which will contain  // instantiated modules and other items like host functions. A Store  // contains an arbitrary piece of host information, and we use `MyState`  // here.  std::cout << "Initializing...\n";  Store store(engine);  // Our wasm module we'll be instantiating requires one imported function.  // the function takes no parameters and returns no results. We create a host  // implementation of that function here.  std::cout << "Creating callback...\n";  Func host_func =      Func::wrap(store, []() { std::cout << "Calling back...\n"; });  // Once we've got that all set up we can then move to the instantiation  // phase, pairing together a compiled module as well as a set of imports.  // Note that this is where the wasm `start` function, if any, would run.  std::cout << "Instantiating module...\n";  auto instance = Instance::create(store, module, {host_func}).unwrap();  // Next we poke around a bit to extract the `run` function from the module.  std::cout << "Extracting export...\n";  auto run = std::get<Func>(*instance.get(store, "run"));  // And last but not least we can call it!  std::cout << "Calling export...\n";  run.call(store, {}).unwrap();  std::cout << "Done\n";}

    [8]ページ先頭

    ©2009-2025 Movatter.jp