-
Notifications
You must be signed in to change notification settings - Fork 3
/
runtime.go
52 lines (41 loc) · 1.31 KB
/
runtime.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
package runtime
import (
"context"
"time"
"github.com/pkg/errors"
"github.com/artela-network/aspect-runtime/types"
"github.com/artela-network/aspect-runtime/wasmtime"
wasm "github.com/bytecodealliance/wasmtime-go/v20"
)
type (
engine func(ctx context.Context, logger types.Logger, code []byte, apis *types.HostAPIRegistry) (out types.AspectRuntime, err error)
RuntimeType int
)
const (
WASM RuntimeType = iota
)
var enginePool = map[RuntimeType]engine{
WASM: wasmtime.NewWASMTimeRuntime,
}
// NewAspectRuntime is the factory method for creating aspect runtime
func NewAspectRuntime(ctx context.Context, logger types.Logger, runtimeType RuntimeType, code []byte, apis *types.HostAPIRegistry) (types.AspectRuntime, error) {
engine := enginePool[runtimeType]
if engine == nil {
return nil, errors.New("runtime engine not support")
}
startTime := time.Now()
injectedCode, err := wasm.Instrument(code)
if err != nil {
return nil, err
}
logger.Debug("instrumentation done", "duration", time.Since(startTime).String(),
"beforeSize", len(code),
"afterSize", len(injectedCode))
startTime = time.Now()
aspectRuntime, err := engine(ctx, logger, injectedCode, apis)
if err != nil {
return nil, err
}
logger.Debug("runtime created", "duration", time.Since(startTime).String())
return aspectRuntime, nil
}