using System; using System.Runtime.Serialization; using System.Text; namespace Wasmtime { /// /// The exception for WebAssembly traps. /// [Serializable] public class TrapException : WasmtimeException { /// public TrapException() { } /// public TrapException(string message) : base(message) { } /// public TrapException(string message, Exception inner) : base(message, inner) { } /// protected TrapException(SerializationInfo info, StreamingContext context) : base(info, context) { } internal static TrapException FromOwnedTrap(IntPtr trap) { unsafe { Interop.wasm_trap_message(trap, out var bytes); var byteSpan = new ReadOnlySpan(bytes.data, checked((int)bytes.size)); int indexOfNull = byteSpan.IndexOf((byte)0); if (indexOfNull != -1) byteSpan = byteSpan.Slice(0, indexOfNull); var message = Encoding.UTF8.GetString(byteSpan); Interop.wasm_byte_vec_delete(ref bytes); Interop.wasm_trap_delete(trap); return new TrapException(message); } } // TODO: expose trap frames } }