2024-04-10 13:15:36 +01:00
|
|
|
package util
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
|
|
|
|
|
"github.com/tetratelabs/wazero/experimental"
|
2024-10-18 12:20:32 +01:00
|
|
|
|
|
|
|
|
"github.com/ncruces/go-sqlite3/internal/alloc"
|
2024-04-10 13:15:36 +01:00
|
|
|
)
|
|
|
|
|
|
2025-01-13 13:45:41 +00:00
|
|
|
type ConnKey struct{}
|
|
|
|
|
|
2024-04-10 13:15:36 +01:00
|
|
|
type moduleKey struct{}
|
|
|
|
|
type moduleState struct {
|
2025-10-15 16:22:36 +01:00
|
|
|
sysError error
|
2024-04-10 13:15:36 +01:00
|
|
|
mmapState
|
2024-04-28 10:33:39 +01:00
|
|
|
handleState
|
2024-04-10 13:15:36 +01:00
|
|
|
}
|
|
|
|
|
|
2024-04-26 16:25:45 +01:00
|
|
|
func NewContext(ctx context.Context) context.Context {
|
2024-04-10 13:15:36 +01:00
|
|
|
state := new(moduleState)
|
2024-10-17 13:04:23 +01:00
|
|
|
ctx = experimental.WithMemoryAllocator(ctx, experimental.MemoryAllocatorFunc(alloc.NewMemory))
|
2024-04-10 13:15:36 +01:00
|
|
|
ctx = experimental.WithCloseNotifier(ctx, state)
|
2024-04-26 16:25:45 +01:00
|
|
|
ctx = context.WithValue(ctx, moduleKey{}, state)
|
2024-04-10 13:15:36 +01:00
|
|
|
return ctx
|
|
|
|
|
}
|
2025-10-15 16:22:36 +01:00
|
|
|
|
|
|
|
|
func GetSystemError(ctx context.Context) error {
|
2025-10-18 11:06:50 +01:00
|
|
|
// Test needed to simplify testing.
|
|
|
|
|
s, ok := ctx.Value(moduleKey{}).(*moduleState)
|
|
|
|
|
if ok {
|
|
|
|
|
return s.sysError
|
|
|
|
|
}
|
|
|
|
|
return nil
|
2025-10-15 16:22:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func SetSystemError(ctx context.Context, err error) {
|
2025-10-18 11:06:50 +01:00
|
|
|
// Test needed to simplify testing.
|
2025-10-15 16:22:36 +01:00
|
|
|
s, ok := ctx.Value(moduleKey{}).(*moduleState)
|
|
|
|
|
if ok {
|
|
|
|
|
s.sysError = err
|
|
|
|
|
}
|
|
|
|
|
}
|