Add a Module::deserialize_file method (#3266)

* Add a `Module::deserialize_file` method

This commit adds a new method to the `wasmtime::Module` type,
`deserialize_file`. This is intended to be the same as the `deserialize`
method except for the serialized module is present as an on-disk file.
This enables Wasmtime to internally use `mmap` to avoid copying bytes
around and generally makes loading a module much faster.

A C API is added in this commit as well for various bindings to use this
accelerated path now as well. Another option perhaps for a Rust-based
API is to have an API taking a `File` itself to allow for a custom file
descriptor in one way or another, but for now that's left for a possible
future refactoring if we find a use case.

* Fix compat with main - handle readdonly mmap

* wip

* Try to fix Windows support
This commit is contained in:
Alex Crichton
2021-08-31 13:05:51 -05:00
committed by GitHub
parent 4378ea8e01
commit 9e0c910023
9 changed files with 339 additions and 20 deletions

View File

@@ -3,6 +3,9 @@ use crate::{
wasm_extern_t, wasm_importtype_t, wasm_importtype_vec_t, wasm_store_t, wasmtime_error_t,
wasmtime_moduletype_t, StoreRef,
};
use anyhow::Context;
use std::ffi::CStr;
use std::os::raw::c_char;
use wasmtime::{Engine, Extern, Module};
#[derive(Clone)]
@@ -202,3 +205,19 @@ pub unsafe extern "C" fn wasmtime_module_deserialize(
*out = Box::into_raw(Box::new(wasmtime_module_t { module }));
})
}
#[no_mangle]
pub unsafe extern "C" fn wasmtime_module_deserialize_file(
engine: &wasm_engine_t,
path: *const c_char,
out: &mut *mut wasmtime_module_t,
) -> Option<Box<wasmtime_error_t>> {
let path = CStr::from_ptr(path);
let result = path
.to_str()
.context("input path is not valid utf-8")
.and_then(|path| Module::deserialize_file(&engine.engine, path));
handle_result(result, |module| {
*out = Box::into_raw(Box::new(wasmtime_module_t { module }));
})
}