Files
sqlite3/compile.go

60 lines
1.1 KiB
Go
Raw Permalink Normal View History

2023-01-12 13:43:35 +00:00
package sqlite3
import (
"context"
"os"
2023-01-28 12:47:39 +00:00
"strconv"
2023-01-12 13:43:35 +00:00
"sync"
"sync/atomic"
"github.com/tetratelabs/wazero"
2023-01-28 12:47:39 +00:00
"github.com/tetratelabs/wazero/api"
2023-01-12 13:43:35 +00:00
)
// Configure SQLite.
var (
Binary []byte // Binary to load.
Path string // Path to load the binary from.
)
2023-01-28 12:47:39 +00:00
var sqlite3 sqlite3Runtime
2023-01-12 13:43:35 +00:00
2023-01-28 12:47:39 +00:00
type sqlite3Runtime struct {
once sync.Once
runtime wazero.Runtime
compiled wazero.CompiledModule
instances atomic.Uint64
ctx context.Context
err error
}
2023-01-12 13:43:35 +00:00
2023-01-28 12:47:39 +00:00
func (s *sqlite3Runtime) instantiateModule(ctx context.Context) (api.Module, error) {
s.ctx = ctx
s.once.Do(s.compileModule)
if s.err != nil {
return nil, s.err
}
2023-01-18 01:30:11 +00:00
2023-01-28 12:47:39 +00:00
cfg := wazero.NewModuleConfig().
WithName("sqlite3-" + strconv.FormatUint(s.instances.Add(1), 10))
return s.runtime.InstantiateModule(ctx, s.compiled, cfg)
}
func (s *sqlite3Runtime) compileModule() {
s.runtime = wazero.NewRuntime(s.ctx)
s.err = vfsInstantiate(s.ctx, s.runtime)
if s.err != nil {
return
2023-01-18 01:30:11 +00:00
}
2023-01-12 13:43:35 +00:00
2023-01-28 12:47:39 +00:00
bin := Binary
if bin == nil && Path != "" {
bin, s.err = os.ReadFile(Path)
if s.err != nil {
return
2023-01-12 13:43:35 +00:00
}
}
2023-01-28 12:47:39 +00:00
s.compiled, s.err = s.runtime.CompileModule(s.ctx, bin)
2023-01-12 13:43:35 +00:00
}