MCPcopy Index your code
hub / github.com/sqlc-dev/sqlc / loadAndCompileWASM

Method loadAndCompileWASM

internal/ext/wasm/wasm.go:116–165  ·  view source on GitHub ↗
(ctx context.Context, cache string, expected string)

Source from the content-addressed store, hash-verified

114}
115
116func (r *Runner) loadAndCompileWASM(ctx context.Context, cache string, expected string) (*runtimeAndCode, error) {
117 pluginDir := filepath.Join(cache, expected)
118 pluginPath := filepath.Join(pluginDir, "plugin.wasm")
119 _, staterr := os.Stat(pluginPath)
120
121 uri := r.URL
122 if staterr == nil {
123 uri = "file://" + pluginPath
124 }
125
126 wmod, actual, err := r.fetch(ctx, uri)
127 if err != nil {
128 return nil, err
129 }
130
131 if expected != actual {
132 return nil, fmt.Errorf("invalid checksum: expected %s, got %s", expected, actual)
133 }
134
135 if staterr != nil {
136 err := os.Mkdir(pluginDir, 0755)
137 if err != nil && !os.IsExist(err) {
138 return nil, fmt.Errorf("mkdirall: %w", err)
139 }
140 if err := os.WriteFile(pluginPath, wmod, 0444); err != nil {
141 return nil, fmt.Errorf("cache wasm: %w", err)
142 }
143 }
144
145 wazeroCache, err := wazero.NewCompilationCacheWithDir(filepath.Join(cache, "wazero"))
146 if err != nil {
147 return nil, fmt.Errorf("wazero.NewCompilationCacheWithDir: %w", err)
148 }
149
150 config := wazero.NewRuntimeConfig().WithCompilationCache(wazeroCache)
151 rt := wazero.NewRuntimeWithConfig(ctx, config)
152
153 if _, err := wasi_snapshot_preview1.Instantiate(ctx, rt); err != nil {
154 return nil, fmt.Errorf("wasi_snapshot_preview1 instantiate: %w", err)
155 }
156
157 // Compile the Wasm binary once so that we can skip the entire compilation
158 // time during instantiation.
159 code, err := rt.CompileModule(ctx, wmod)
160 if err != nil {
161 return nil, fmt.Errorf("compile module: %w", err)
162 }
163
164 return &runtimeAndCode{rt: rt, code: code}, nil
165}
166
167// removePGCatalog removes the pg_catalog schema from the request. There is a
168// mysterious (reason unknown) bug with wasm plugins when a large amount of

Callers 1

loadAndCompileMethod · 0.95

Calls 2

fetchMethod · 0.95
JoinMethod · 0.45

Tested by

no test coverage detected