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.

107 lines
5.2 KiB

const Engine = @import("./engine.zig").Engine;
const Diagnostics = @import("./diagnostics.zig").Diagnostics;
const Error = @import("./error.zig").Error;
/// A store is the unit of isolation between WebAssembly instances in an
/// embedding of Wasmtime. Values in one `Store` cannot flow into another.
/// Stores are cheap to create and cheap to dispose. It's expected that
/// one-off stores are common in embeddings.
///
/// Objects stored within a `Store` are referenced with integer handles rather
/// than interior pointers. This means that most APIs require that the store
/// be explicitly passed in, which is done via `Store.Context`. It is safe to
/// move a `Store` to any thread at any time. A store generally cannot be
/// concurrently used, however.
pub const Store = opaque {
/// Creates a new store within the specified engine.
/// The returned store must be deleted with `deinit`.
pub fn init(engine: *Engine) *Store {
// TODO: Support passing in user-provided data.
return wasmtime_store_new(engine, null, null);
}
/// Deletes a store.
pub fn deinit(self: *Store) void {
wasmtime_store_delete(self);
}
/// Returns the interior `Context` pointer to this store
pub fn getContext(self: *Store) *Context {
return wasmtime_store_context(self);
}
extern "c" fn wasmtime_store_new(*Engine, ?*anyopaque, ?*const fn (*anyopaque) void) *Store;
extern "c" fn wasmtime_store_delete(*Store) void;
extern "c" fn wasmtime_store_context(*Store) *Context;
// extern "c" fn wasmtime_store_limiter(*Store, memory_size: i64, table_elements: i64, instances: i64, tables: i64, memories: i64) void;
// extern "c" fn wasmtime_store_epoch_deadline_callback(*Store, *const fn (*Context, *anyopaque, *u64) ?*Error, *anyopaque) void;
/// Interior pointer into a `Store`, used as "context" for many functions.
///
/// This context pointer is used pervasively throught Wasmtime's API. This
/// can be acquired from `Store.getContext` or `Caller.getContext`. The
/// context pointer for a store is the same for the entire lifetime of a
/// store, so it can safely be stored adjacent to a `Store` itself.
///
/// Usage of a `Context` must not outlive the original `Store`.
/// Additionally `Context` can only be used in situations where it has
/// explicitly been granted access to doing so. For example finalizers
/// cannot use `Context` because they are not given access to it.
pub const Context = opaque {
/// Returns the user-specified data associated with the specified store.
pub fn getData(self: *const Context) ?*anyopaque {
return wasmtime_context_get_data(self);
}
/// Overwrites the user-specified data associated with this store.
///
/// Note that this does not execute the original finalizer for the
/// provided data, and the original finalizer will be executed for the
/// provided data when the store is deleted.
pub fn setData(self: *Context, data: ?*anyopaque) void {
wasmtime_context_set_data(self, data);
}
/// Perform garbage collection within the given context.
///
/// Garbage collects `ExternRef`s that are used within this store. Any
/// `ExternRef`s that are discovered to be unreachable by other code
/// or objects will have their finalizers run.
pub fn runGarbageCollection(self: *Context) void {
wasmtime_context_gc(self);
}
// TODO: Do the documentation for fuel related functions.
pub fn getFuelRemaining(self: *const Context) ?u64 {
var result: u64 = undefined;
if (wasmtime_context_fuel_remaining(self, &result)) result else null;
}
pub fn getFuelConsumed(self: *const Context) ?u64 {
var result: u64 = undefined;
if (wasmtime_context_fuel_consumed(self, &result)) result else null;
}
pub fn addFuel(self: *Context, fuel: u64, diag: ?*Diagnostics) !void {
const err = wasmtime_context_add_fuel(self, fuel);
try Diagnostics.handleError(err, error.ContextAddFuel, {}, diag);
}
pub fn consumeFuel(self: *Context, fuel: u64, diag: ?*Diagnostics) !u64 {
var result: u64 = undefined;
const err = wasmtime_context_consume_fuel(self, fuel, &result);
try Diagnostics.handleError(err, error.ContextConsumeFuel, result, diag);
}
extern "c" fn wasmtime_context_get_data(*const Context) ?*anyopaque;
extern "c" fn wasmtime_context_set_data(*Context, ?*anyopaque) void;
extern "c" fn wasmtime_context_gc(*Context) void;
extern "c" fn wasmtime_context_add_fuel(*Context, fuel: u64) ?*Error;
extern "c" fn wasmtime_context_fuel_consumed(*const Context, fuel: *u64) bool;
extern "c" fn wasmtime_context_fuel_remaining(*const Context, fuel: *u64) bool;
extern "c" fn wasmtime_context_consume_fuel(*Context, fuel: u64, remaining: *u64) ?*Error;
// extern "c" fn wasmtime_context_set_wasi(*Context, *wasi_config_t) ?*Error;
// extern "c" fn wasmtime_context_set_epoch_deadline(*Context, ticks_beyond_current: u64) void;
};
};