@@ -36,8 +36,11 @@ extension Store: Equatable {
3636}
3737
3838/// A caller context passed to host functions
39- public struct Caller {
39+ /// Not copyable to avoid storing stale stack pointer.
40+ public struct Caller : ~ Copyable {
4041 private let instanceHandle : InternalInstance ?
42+ /// The stack pointer at the point of the host function call.
43+ private let sp : Sp ?
4144 /// The instance that called the host function.
4245 /// - Note: This property is `nil` if a `Function` backed by a host function is called directly.
4346 public var instance : Instance ? {
@@ -55,15 +58,25 @@ public struct Caller {
5558 @available ( * , unavailable, message: " Use `engine` instead " )
5659 public var runtime : Runtime { fatalError ( ) }
5760
58- init ( instanceHandle: InternalInstance ? , store: Store ) {
61+ init ( instanceHandle: InternalInstance ? , store: Store , sp : Sp ? = nil ) {
5962 self . instanceHandle = instanceHandle
6063 self . store = store
64+ self . sp = sp
65+ }
66+
67+ /// Captures the current WebAssembly call stack backtrace.
68+ ///
69+ /// Returns `nil` if the caller context does not have stack pointer information
70+ /// (e.g., when a host function is called directly rather than from WebAssembly).
71+ public func captureBacktrace( ) -> Backtrace ? {
72+ guard let sp else { return nil }
73+ return Execution . captureBacktrace ( sp: sp, store: store)
6174 }
6275}
6376
6477struct HostFunctionEntity {
6578 let type : InternedFuncType
66- let implementation : ( Caller , [ Value ] ) throws -> [ Value ]
79+ let implementation : Function . Implementation
6780}
6881
6982extension Store {
0 commit comments