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.

83 lines
3.2 KiB

const Extern = @import("./extern.zig").Extern;
const Store = @import("./store.zig").Store;
const Diagnostics = @import("./diagnostics.zig").Diagnostics;
const Error = @import("./error.zig").Error;
/// Representation of a memory in Wasmtime.
pub const Memory = extern struct {
store_id: u64,
index: usize,
pub fn init(context: *Store.Context, mem_type: *const MemoryType, diag: ?*Diagnostics) !Memory {
var result: Memory = undefined;
const err = wasmtime_memory_new(context, mem_type, &result);
return Diagnostics.handleError(err, error.MemoryInit, result, diag);
}
pub fn getType(self: *const Memory, context: *const Store.Context) *MemoryType {
return wasmtime_memory_type(context, self);
}
pub fn getData(self: *const Memory, context: *const Store.Context) []u8 {
var data = wasmtime_memory_data(context, self);
var size = wasmtime_memory_data_size(context, self);
return data[0..size];
}
pub fn getSize(self: *const Memory, context: *const Store.Context) u64 {
return wasmtime_memory_size(context, self);
}
pub fn grow(self: *const Memory, context: *Store.Context, delta: u64, diag: ?*Diagnostics) !u64 {
var prev_size: u64 = undefined;
const err = wasmtime_memory_grow(context, self, delta, &prev_size);
return Diagnostics.handleError(err, error.MemoryGrow, prev_size, diag);
}
pub fn toExtern(self: Memory) Extern {
return Extern.from(self, Memory);
}
extern "c" fn wasmtime_memory_new(*Store.Context, *const MemoryType, *Memory) ?*Error;
extern "c" fn wasmtime_memory_type(*const Store.Context, *const Memory) *MemoryType;
extern "c" fn wasmtime_memory_data(*const Store.Context, *const Memory) [*]u8;
extern "c" fn wasmtime_memory_data_size(*const Store.Context, *const Memory) usize;
extern "c" fn wasmtime_memory_size(*const Store.Context, *const Memory) u64;
extern "c" fn wasmtime_memory_grow(*Store.Context, *const Memory, u64, *u64) ?*Error;
};
/// An opaque object representing the type of a memory.
pub const MemoryType = opaque {
pub fn init(min: u64, max: ?u64, is_64: bool) *MemoryType {
return wasmtime_memorytype_new(min, max != null, max orelse 0, is_64);
}
pub fn copy(self: *const MemoryType) *MemoryType {
return wasm_functype_copy(self);
}
pub fn deinit(self: *MemoryType) void {
wasm_functype_delete(self);
}
pub fn getMinimum(self: *const MemoryType) u64 {
return wasmtime_memorytype_minimum(self);
}
pub fn getMaximum(self: *const MemoryType) ?u64 {
var result: u64 = undefined;
return if (wasmtime_memorytype_maximum(self, &result)) result else null;
}
pub fn is64(self: *const MemoryType) bool {
return wasmtime_memorytype_is64(self);
}
extern "c" fn wasm_functype_copy(*const MemoryType) *MemoryType;
extern "c" fn wasm_functype_delete(*MemoryType) void;
extern "c" fn wasmtime_memorytype_new(min: u64, max_present: bool, max: u64, is_64: bool) *MemoryType;
extern "c" fn wasmtime_memorytype_minimum(*const MemoryType) u64;
extern "c" fn wasmtime_memorytype_maximum(*const MemoryType, *u64) bool;
extern "c" fn wasmtime_memorytype_is64(*const MemoryType) bool;
};