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.

122 lines
4.6 KiB

const Message = @import("./vec.zig").Message;
pub const TrapError = error{
/// The current stack space was exhausted.
StackOverflow,
/// An out-of-bounds memory access.
MemoryOutOfBounds,
/// A wasm atomic operation was presented with a not-naturally-aligned linear-memory address.
HeapMisaligned,
/// An out-of-bounds access to a table.
TableOutOfBounds,
/// Indirect call to a null table entry.
IndirectCallToNull,
/// Signature mismatch on indirect call.
BadSignature,
/// An integer arithmetic operation caused an overflow.
IntegerOverflow,
/// An integer division by zero.
IntegerDevisionByZero,
/// Failed float-to-int conversion.
BadConversionToInteger,
/// Code that was supposed to have been unreachable was reached.
UnreachableCodeReached,
/// Execution has potentially run too long and may be interrupted.
Interrupt,
/// Execution has run out of the configured fuel amount.
OutOfFuel,
/// A trap without a known type has occurred.
UnknownTrap,
};
/// Code of an instruction trap.
pub const TrapCode = enum(u8) {
/// The current stack space was exhausted.
stack_overflow,
/// An out-of-bounds memory access.
memory_out_of_bounds,
/// A wasm atomic operation was presented with a not-naturally-aligned linear-memory address.
heap_misaligned,
/// An out-of-bounds access to a table.
table_out_of_bounds,
/// Indirect call to a null table entry.
indirect_call_to_null,
/// Signature mismatch on indirect call.
bad_signature,
/// An integer arithmetic operation caused an overflow.
integer_overflow,
/// An integer division by zero.
integer_division_by_zero,
/// Failed float-to-int conversion.
bad_conversion_to_integer,
/// Code that was supposed to have been unreachable was reached.
unreachable_code_reached,
/// Execution has potentially run too long and may be interrupted.
interrupt,
/// Execution has run out of the configured fuel amount.
out_of_fuel,
pub fn toError(self: TrapCode) TrapError {
return switch (self) {
// zig fmt: off
.stack_overflow => TrapError.StackOverflow,
.memory_out_of_bounds => TrapError.MemoryOutOfBounds,
.heap_misaligned => TrapError.HeapMisaligned,
.table_out_of_bounds => TrapError.TableOutOfBounds,
.indirect_call_to_null => TrapError.IndirectCallToNull,
.bad_signature => TrapError.BadSignature,
.integer_overflow => TrapError.IntegerOverflow,
.integer_division_by_zero => TrapError.IntegerDevisionByZero,
.bad_conversion_to_integer => TrapError.BadConversionToInteger,
.unreachable_code_reached => TrapError.UnreachableCodeReached,
.interrupt => TrapError.Interrupt,
.out_of_fuel => TrapError.OutOfFuel,
// zig fmt: on
};
}
};
pub const Trap = struct {
/// Creates a new trap.
/// The `Trap` returned is owned by the caller.
pub fn init(message: []const u8) Trap {
return wasmtime_trap_new(message.ptr, message.len);
}
pub fn copy(self: *const Trap) Trap {
return wasm_trap_copy(self);
}
pub fn deinit(self: *Trap) void {
wasm_trap_delete(self);
}
pub fn getMessage(self: *const Trap) Message {
var result: Message = undefined;
wasm_trap_message(self, &result);
return result;
}
/// Attempts to extract the trap code from this trap.
///
/// Returns a `TrapCode` if the trap is an instruction trap triggered
/// while executing Wasm. Returns `null` otherwise, for example when the
/// trap is created using `init`, or occur with WASI modules exiting with
/// a certain exit code.
pub fn getTrapCode(self: *const Trap) ?TrapCode {
var result: TrapCode = undefined;
const has_code = wasmtime_trap_code(self, &result);
return if (has_code) result else null;
}
extern "c" fn wasm_trap_copy(*const Trap) *Trap;
extern "c" fn wasm_trap_delete(*Trap) void;
extern "c" fn wasm_trap_message(*const Trap, *Message) void;
// extern "c" fn wasm_trap_origin(*const Trap) *Frame;
// extern "c" fn wasm_trap_trace(*const Trap, *FrameVec) void;
extern "c" fn wasmtime_trap_new([*]const u8, usize) *Trap;
extern "c" fn wasmtime_trap_code(*const Trap, *TrapCode) bool;
// extern "c" fn wasmtime_frame_func_name(*const Frame) *const Name;
// extern "c" fn wasmtime_frame_module_name(*const Frame) *const Name;
};