-
Notifications
You must be signed in to change notification settings - Fork 15
/
template.go
76 lines (58 loc) · 2.11 KB
/
template.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
Copyright 2022 Loophole Labs
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package scale
import (
"context"
"fmt"
"github.com/tetratelabs/wazero"
interfaces "github.com/loopholelabs/scale-signature-interfaces"
"github.com/loopholelabs/scale/scalefunc"
)
// template is a template for creating runnable scale functions
type template[T interfaces.Signature] struct {
// runtime is the scale runtime that the template belongs to
runtime *Scale[T]
// identifier is the identifier for the template
identifier string
// compiled is the compiled module source
compiled wazero.CompiledModule
// next is the (optional) next function in the chain
next *template[T]
// modulePool is the pool of modules for the template
modulePool *modulePool[T]
// env contains the (optional) environment variables for the template
env map[string]string
}
// newTemplate creates a new template from a scale function schema
func newTemplate[T interfaces.Signature](ctx context.Context, runtime *Scale[T], scaleFunc *scalefunc.V1BetaSchema, env map[string]string, opts ...uint32) (*template[T], error) {
compiled, err := runtime.runtime.CompileModule(ctx, scaleFunc.Function)
if err != nil {
return nil, fmt.Errorf("failed to compile wasm module '%s': %w", scaleFunc.Name, err)
}
var maxSize uint32
if len(opts) > 0 {
maxSize = opts[0]
} else {
maxSize = 0 // default value
}
templ := &template[T]{
runtime: runtime,
identifier: fmt.Sprintf("%s:%s", scaleFunc.Name, scaleFunc.Tag),
compiled: compiled,
env: env,
}
if scaleFunc.Stateless {
templ.modulePool = newModulePool[T](ctx, templ, maxSize)
}
return templ, nil
}