Wasmtime bindings for Zig
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

115 lines
5.2 KiB

1 year ago
const Engine = @import("./engine.zig").Engine;
const Diagnostics = @import("./diagnostics.zig").Diagnostics;
const Error = @import("./error.zig").Error;
const ByteVec = @import("./vec.zig").ByteVec;
const _instance = @import("./instance.zig");
const ImportTypeVec = _instance.ImportTypeVec;
const ExportTypeVec = _instance.ExportTypeVec;
const wat2wasm = @import("./wat2wasm.zig").wat2wasm;
/// A compiled Wasmtime module.
///
/// This type represents a compiled WebAssembly module. The compiled module is
/// ready to be instantiated and can be inspected for imports/exports. It is
/// safe to use a module across multiple threads simultaneously.
pub const Module = opaque {
/// Compiles a WebAssembly binary into a `Module`.
pub fn init(engine: *Engine, wasm_bytes: []const u8, diag: ?*Diagnostics) !*Module {
var result: *Module = undefined;
const err = wasmtime_module_new(engine, wasm_bytes.ptr, wasm_bytes.len, &result);
return Diagnostics.handleError(err, error.ModuleInit, result, diag);
}
pub fn initFromWat(engine: *Engine, wat_bytes: []const u8, diag: ?*Diagnostics) !*Module {
const wasm_bytes = try wat2wasm(wat_bytes, diag);
1 year ago
defer wasm_bytes.deinit();
return init(engine, wasm_bytes.toSlice(), diag);
}
pub fn deinit(self: *Module) void {
wasmtime_module_delete(self);
}
/// Creates a shallow clone of the specified module, increasing the
/// internal reference count.
pub fn clone(self: *Module) *Module {
return wasmtime_module_clone(self);
}
/// Returns the list of imports that this module expects.
///
/// The list of imports returned are the types of items expected to be
/// passed to `Instance.init`. You can use `Import.getType` to learn about
/// the expected type of each import.
pub fn getImports(self: *Module) ImportTypeVec {
var result: ImportTypeVec = undefined;
wasmtime_module_imports(self, &result);
return result;
}
/// Returns the list of exports that this module provides.
///
/// The list of exports returned are in the same order as the items
/// returned by `Instance.getExports`.
pub fn getExports(self: *Module) ExportTypeVec {
var result: ExportTypeVec = undefined;
wasmtime_module_exports(self, &result);
return result;
}
/// Validate a WebAssembly binary.
///
/// This function will validate the provided byte sequence to determine if
/// it is a valid WebAssembly binary within the context of the engine
/// provided.
pub fn validate(engine: *Engine, wasm_bytes: []const u8, diag: ?*Diagnostics) !void {
const err = wasmtime_module_validate(engine, wasm_bytes.ptr, wasm_bytes.len);
try Diagnostics.handleError(err, error.ModuleValidate, {}, diag);
}
/// This function serializes compiled module artifacts as blob data.
pub fn serialize(self: *Module, diag: ?*Diagnostics) !ByteVec {
var result: ByteVec = undefined;
const err = wasmtime_module_serialize(self, &result);
return Diagnostics.handleError(err, error.ModuleSerialize, result, diag);
}
/// Build a module from serialized data.
///
/// This function is not safe to receive arbitrary user input. See the
/// Rust documentation for more information on what inputs are safe to
/// pass in here (e.g. only that of `Module.serialize`).
pub fn deserialize(engine: *Engine, bytes: []const u8, diag: ?*Diagnostics) !*Module {
var result: *Module = undefined;
const err = wasmtime_module_deserialize(engine, bytes.ptr, bytes.len, &result);
return Diagnostics.handleError(err, error.ModuleDeserialize, result, diag);
}
/// Deserialize a module from an on-disk file.
///
/// This function is the same as `Module.deserialize` except that it reads
/// the data for the serialized module from the path on disk. This can be
/// faster than the alternative which may require copying the data around.
///
/// This function is not safe to receive arbitrary user input. See the
/// Rust documentation for more information on what inputs are safe to
/// pass in here (e.g. only that of `Module.serialize`).
pub fn deserializeFromFile(engine: *Engine, path: [*]const u8, diag: ?*Diagnostics) !*Module {
var result: *Module = undefined;
const err = wasmtime_module_deserialize_file(engine, path.ptr, &result);
return Diagnostics.handleError(err, error.ModuleDeserializeFromFile, result, diag);
}
extern "c" fn wasmtime_module_new(*Engine, [*]const u8, usize, **Module) ?*Error;
extern "c" fn wasmtime_module_delete(*Module) void;
extern "c" fn wasmtime_module_clone(*Module) *Module;
extern "c" fn wasmtime_module_imports(*const Module, *ImportTypeVec) void;
extern "c" fn wasmtime_module_exports(*const Module, *ExportTypeVec) void;
extern "c" fn wasmtime_module_validate(*Engine, [*]const u8, usize) ?*Error;
extern "c" fn wasmtime_module_serialize(*Module, *ByteVec) ?*Error;
extern "c" fn wasmtime_module_deserialize(*Engine, [*]const u8, usize, **Module) ?*Error;
extern "c" fn wasmtime_module_deserialize_file(*Engine, [*]const u8, **Module) ?*Error;
};