-
Notifications
You must be signed in to change notification settings - Fork 1
/
translator.go
47 lines (37 loc) · 1.2 KB
/
translator.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
package wasm
import (
"context"
"fmt"
"io"
embind "github.com/jerbob92/wazero-emscripten-embind"
"github.com/tetratelabs/wazero"
"github.com/tetratelabs/wazero/api"
"github.com/KSpaceer/gobergamot/internal/gen"
)
type CompileConfig struct {
// Stderr and Stdout enable redirection of any logs. If left nil they point at os.Stderr and os.Stdout. Turn off by setting them to io.Discard
Stderr, Stdout io.Writer
}
func CompileBergamot(
ctx context.Context,
wasmRuntime wazero.Runtime,
embindEng embind.Engine,
cfg CompileConfig,
) (api.Module, error) {
bergamotCompiledModule, err := wasmRuntime.CompileModule(ctx, bergamotTranslatorWorkerWASM)
if err != nil {
return nil, fmt.Errorf("CompileModule: %w", err)
}
if err := BuildImports(ctx, wasmRuntime, embindEng, bergamotCompiledModule); err != nil {
return nil, fmt.Errorf("BuildImports: %w", err)
}
moduleConfig := wazero.NewModuleConfig().
WithStderr(cfg.Stderr).
WithStdout(cfg.Stdout).
WithStartFunctions("_initialize")
bergamotModule, err := wasmRuntime.InstantiateModule(ctx, bergamotCompiledModule, moduleConfig)
if err != nil {
return nil, fmt.Errorf("InstantiateModule: %w", err)
}
return bergamotModule, gen.Attach(embindEng)
}