@@ -29,6 +29,15 @@ import (
29
29
"github.com/sqlc-dev/sqlc/internal/plugin"
30
30
)
31
31
32
+ func NewRunner (url string , checksum string , env []string ) * Runner {
33
+ return & Runner {
34
+ URL : url ,
35
+ SHA256 : checksum ,
36
+ Env : env ,
37
+ rt : wazero .NewRuntime (context .Background ()),
38
+ }
39
+ }
40
+
32
41
func cacheDir () (string , error ) {
33
42
cache := os .Getenv ("SQLCCACHE" )
34
43
if cache != "" {
@@ -61,7 +70,7 @@ func (r *Runner) getChecksum(ctx context.Context) (string, error) {
61
70
return sum , nil
62
71
}
63
72
64
- func (r * Runner ) loadBytes (ctx context.Context ) ([] byte , error ) {
73
+ func (r * Runner ) loadAndCompile (ctx context.Context ) (wazero. CompiledModule , error ) {
65
74
expected , err := r .getChecksum (ctx )
66
75
if err != nil {
67
76
return nil , err
@@ -71,14 +80,14 @@ func (r *Runner) loadBytes(ctx context.Context) ([]byte, error) {
71
80
return nil , err
72
81
}
73
82
value , err , _ := flight .Do (expected , func () (interface {}, error ) {
74
- return r .loadWASM (ctx , cacheDir , expected )
83
+ return r .loadAndCompileWASM (ctx , cacheDir , expected )
75
84
})
76
85
if err != nil {
77
86
return nil , err
78
87
}
79
- data , ok := value .([] byte )
88
+ data , ok := value .(wazero. CompiledModule )
80
89
if ! ok {
81
- return nil , fmt .Errorf ("returned value was not a byte slice " )
90
+ return nil , fmt .Errorf ("returned value was not a compiled module " )
82
91
}
83
92
return data , nil
84
93
}
@@ -124,7 +133,7 @@ func (r *Runner) fetch(ctx context.Context, uri string) ([]byte, string, error)
124
133
return wmod , actual , nil
125
134
}
126
135
127
- func (r * Runner ) loadWASM (ctx context.Context , cache string , expected string ) ([] byte , error ) {
136
+ func (r * Runner ) loadAndCompileWASM (ctx context.Context , cache string , expected string ) (wazero. CompiledModule , error ) {
128
137
pluginDir := filepath .Join (cache , expected )
129
138
pluginPath := filepath .Join (pluginDir , "plugin.wasm" )
130
139
_ , staterr := os .Stat (pluginPath )
@@ -153,7 +162,22 @@ func (r *Runner) loadWASM(ctx context.Context, cache string, expected string) ([
153
162
}
154
163
}
155
164
156
- return wmod , nil
165
+ wazeroCache , err := wazero .NewCompilationCacheWithDir (filepath .Join (cache , "wazero" ))
166
+ if err != nil {
167
+ return nil , fmt .Errorf ("wazero.NewCompilationCacheWithDir: %w" , err )
168
+ }
169
+ config := wazero .NewRuntimeConfig ().WithCompilationCache (wazeroCache )
170
+ r .rt = wazero .NewRuntimeWithConfig (ctx , config )
171
+ // TODO: Handle error
172
+ wasi_snapshot_preview1 .MustInstantiate (ctx , r .rt )
173
+
174
+ // Compile the Wasm binary once so that we can skip the entire compilation time during instantiation.
175
+ code , err := r .rt .CompileModule (ctx , wmod )
176
+ if err != nil {
177
+ return nil , fmt .Errorf ("compile module: %w" , err )
178
+ }
179
+
180
+ return code , nil
157
181
}
158
182
159
183
// removePGCatalog removes the pg_catalog schema from the request. There is a
@@ -195,47 +219,25 @@ func (r *Runner) Invoke(ctx context.Context, method string, args any, reply any,
195
219
return fmt .Errorf ("failed to encode codegen request: %w" , err )
196
220
}
197
221
198
- cacheDir , err := cache .PluginsDir ()
199
- if err != nil {
200
- return err
201
- }
202
-
203
- cache , err := wazero .NewCompilationCacheWithDir (filepath .Join (cacheDir , "wazero" ))
204
- if err != nil {
205
- return err
206
- }
207
-
208
- wasmBytes , err := r .loadBytes (ctx )
209
- if err != nil {
210
- return fmt .Errorf ("loadModule: %w" , err )
211
- }
212
-
213
- config := wazero .NewRuntimeConfig ().WithCompilationCache (cache )
214
- rt := wazero .NewRuntimeWithConfig (ctx , config )
215
- defer rt .Close (ctx )
216
-
217
- // TODO: Handle error
218
- wasi_snapshot_preview1 .MustInstantiate (ctx , rt )
219
-
220
- // Compile the Wasm binary once so that we can skip the entire compilation time during instantiation.
221
- mod , err := rt .CompileModule (ctx , wasmBytes )
222
+ wasmCompiled , err := r .loadAndCompile (ctx )
222
223
if err != nil {
223
- return err
224
+ return fmt . Errorf ( "loadBytes: %w" , err )
224
225
}
225
226
226
227
var stderr , stdout bytes.Buffer
227
228
228
- conf := wazero .NewModuleConfig ()
229
- conf = conf .WithArgs ("plugin.wasm" , method )
230
- conf = conf .WithEnv ("SQLC_VERSION" , info .Version )
229
+ conf := wazero .NewModuleConfig ().
230
+ WithName ("" ).
231
+ WithArgs ("plugin.wasm" , method ).
232
+ WithStdin (bytes .NewReader (stdinBlob )).
233
+ WithStdout (& stdout ).
234
+ WithStderr (& stderr ).
235
+ WithEnv ("SQLC_VERSION" , info .Version )
231
236
for _ , key := range r .Env {
232
237
conf = conf .WithEnv (key , os .Getenv (key ))
233
238
}
234
- conf = conf .WithStdin (bytes .NewReader (stdinBlob ))
235
- conf = conf .WithStdout (& stdout )
236
- conf = conf .WithStderr (& stderr )
237
239
238
- result , err := rt .InstantiateModule (ctx , mod , conf )
240
+ result , err := r . rt .InstantiateModule (ctx , wasmCompiled , conf )
239
241
if result != nil {
240
242
defer result .Close (ctx )
241
243
}
0 commit comments