1
+ // new
1
2
package extism
2
3
3
4
import (
4
5
"context"
5
6
"errors"
6
7
"fmt"
7
- observe "github.com/dylibso/observe-sdk/go"
8
- "github.com/tetratelabs/wazero"
9
- "github.com/tetratelabs/wazero/api"
10
- "github.com/tetratelabs/wazero/imports/wasi_snapshot_preview1"
11
8
"os"
12
9
"strconv"
13
10
"strings"
14
11
"sync/atomic"
15
12
"time"
13
+
14
+ observe "github.com/dylibso/observe-sdk/go"
15
+ "github.com/tetratelabs/wazero"
16
+ "github.com/tetratelabs/wazero/api"
17
+ "github.com/tetratelabs/wazero/imports/wasi_snapshot_preview1"
16
18
)
17
19
18
20
type CompiledPlugin struct {
19
21
runtime wazero.Runtime
20
22
main wazero.CompiledModule
21
23
extism wazero.CompiledModule
22
24
env api.Module
25
+ modules map [string ]wazero.CompiledModule
23
26
24
27
// when a module (main) is instantiated, it may have a module name that's added
25
28
// to the data section of the wasm. If this is the case, we won't be able to
@@ -120,6 +123,7 @@ func NewCompiledPlugin(
120
123
observeAdapter : config .ObserveAdapter ,
121
124
observeOptions : config .ObserveOptions ,
122
125
enableHttpResponseHeaders : config .EnableHttpResponseHeaders ,
126
+ modules : make (map [string ]wazero.CompiledModule ),
123
127
}
124
128
125
129
if config .EnableWasi {
@@ -158,19 +162,18 @@ func NewCompiledPlugin(
158
162
// - If there is only one module in the manifest then that is the main module by default
159
163
// - Otherwise the last module listed is the main module
160
164
161
- modules := map [string ]wazero.CompiledModule {}
162
165
for i , wasm := range manifest .Wasm {
163
166
data , err := wasm .ToWasmData (ctx )
164
167
if err != nil {
165
168
return nil , err
166
169
}
167
170
168
- _ , mainExists := modules ["main" ]
171
+ _ , mainExists := p . modules ["main" ]
169
172
if data .Name == "" || i == len (manifest .Wasm )- 1 && ! mainExists {
170
173
data .Name = "main"
171
174
}
172
175
173
- _ , okm := modules [data .Name ]
176
+ _ , okm := p . modules [data .Name ]
174
177
175
178
if data .Name == "extism:host/env" || okm {
176
179
return nil , fmt .Errorf ("module name collision: '%s'" , data .Name )
@@ -194,7 +197,7 @@ func NewCompiledPlugin(
194
197
if data .Name == "main" {
195
198
p .main = m
196
199
} else {
197
- modules [data .Name ] = m
200
+ p . modules [data .Name ] = m
198
201
}
199
202
}
200
203
@@ -251,8 +254,6 @@ func (p *CompiledPlugin) Instance(ctx context.Context, config PluginInstanceConf
251
254
if err != nil {
252
255
return nil , fmt .Errorf ("failed to initialize Observe Adapter: %v" , err )
253
256
}
254
-
255
- trace .Finish ()
256
257
}
257
258
258
259
// Compile and instantiate the extism runtime. This runtime is stateful and needs to be
@@ -266,6 +267,20 @@ func (p *CompiledPlugin) Instance(ctx context.Context, config PluginInstanceConf
266
267
}
267
268
closers = append (closers , extism .Close )
268
269
270
+ // Instantiate all non-main modules first
271
+ instancedModules := make (map [string ]api.Module )
272
+ for name , module := range p .modules {
273
+ instance , err := p .runtime .InstantiateModule (ctx , module , moduleConfig .WithName (name ))
274
+ if err != nil {
275
+ for _ , m := range instancedModules {
276
+ m .Close (ctx )
277
+ }
278
+ return nil , fmt .Errorf ("instantiating module %s: %w" , name , err )
279
+ }
280
+ instancedModules [name ] = instance
281
+ closers = append (closers , instance .Close )
282
+ }
283
+
269
284
main , err := p .runtime .InstantiateModule (ctx , p .main , moduleConfig )
270
285
if err != nil {
271
286
return nil , fmt .Errorf ("instantiating module: %w" , err )
0 commit comments