Skip to content

Commit

Permalink
Allows to set the version of CLI (#1176)
Browse files Browse the repository at this point in the history
Signed-off-by: Takeshi Yoneda <[email protected]>
  • Loading branch information
mathetake authored Feb 28, 2023
1 parent 599e01b commit 9c07b27
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 42 deletions.
12 changes: 4 additions & 8 deletions internal/engine/compiler/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -811,21 +811,17 @@ func (f *function) getSourceOffsetInWasmBinary(pc uint64) uint64 {
}
}

func NewEngine(ctx context.Context, enabledFeatures api.CoreFeatures, fileCache filecache.Cache) wasm.Engine {
return newEngine(ctx, enabledFeatures, fileCache)
func NewEngine(_ context.Context, enabledFeatures api.CoreFeatures, fileCache filecache.Cache) wasm.Engine {
return newEngine(enabledFeatures, fileCache)
}

func newEngine(ctx context.Context, enabledFeatures api.CoreFeatures, fileCache filecache.Cache) *engine {
var wazeroVersion string
if v := ctx.Value(version.WazeroVersionKey{}); v != nil {
wazeroVersion = v.(string)
}
func newEngine(enabledFeatures api.CoreFeatures, fileCache filecache.Cache) *engine {
return &engine{
enabledFeatures: enabledFeatures,
codes: map[wasm.ModuleID][]*code{},
setFinalizer: runtime.SetFinalizer,
fileCache: fileCache,
wazeroVersion: wazeroVersion,
wazeroVersion: version.GetWazeroVersion(),
}
}

Expand Down
4 changes: 2 additions & 2 deletions internal/engine/compiler/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (e *engineTester) ListenerFactory() experimental.FunctionListenerFactory {

// NewEngine implements the same method as documented on enginetest.EngineTester.
func (e *engineTester) NewEngine(enabledFeatures api.CoreFeatures) wasm.Engine {
return newEngine(context.Background(), enabledFeatures, nil)
return newEngine(enabledFeatures, nil)
}

// CompiledFunctionPointerValue implements the same method as documented on enginetest.EngineTester.
Expand Down Expand Up @@ -237,7 +237,7 @@ func TestCompiler_Releasecode_Panic(t *testing.T) {
// See comments on initialStackSize and initialCallFrameStackSize.
func TestCompiler_SliceAllocatedOnHeap(t *testing.T) {
enabledFeatures := api.CoreFeaturesV1
e := newEngine(context.Background(), enabledFeatures, nil)
e := newEngine(enabledFeatures, nil)
s := wasm.NewStore(enabledFeatures, e)

const hostModuleName = "env"
Expand Down
16 changes: 12 additions & 4 deletions internal/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,22 @@ import (
"strings"
)

// WazeroVersionKey is the key for holding wazero's version in context.Context.
type WazeroVersionKey struct{}
// version holds the current version from the go.mod of downstream users or set by ldflag for wazero CLI.
var version string

// GetWazeroVersion returns the current version of wazero in the go.mod.
// This assumes that users of wazero imports wazero as "github.com/tetratelabs/wazero".
// GetWazeroVersion returns the current version of wazero either in the go.mod or set by ldflag for wazero CLI.
//
// If this is not CLI, this assumes that downstream users of wazero imports wazero as "github.com/tetratelabs/wazero".
// To be precise, the returned string matches the require statement there.
// For example, if the go.mod has "require github.com/tetratelabs/wazero 0.1.2-12314124-abcd",
// then this returns "0.1.2-12314124-abcd".
//
// Note: this is tested in ./testdata/main_test.go with a separate go.mod to pretend as the wazero user.
func GetWazeroVersion() (ret string) {
if len(version) != 0 {
return version
}

info, ok := debug.ReadBuildInfo()
if ok {
for _, dep := range info.Deps {
Expand All @@ -33,6 +38,9 @@ func GetWazeroVersion() (ret string) {
if versionMissing(ret) {
return "dev" // don't return parens
}

// Cache for the subsequent calls.
version = ret
return ret
}

Expand Down
4 changes: 0 additions & 4 deletions runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/tetratelabs/wazero/api"
experimentalapi "github.com/tetratelabs/wazero/experimental"
internalsys "github.com/tetratelabs/wazero/internal/sys"
"github.com/tetratelabs/wazero/internal/version"
"github.com/tetratelabs/wazero/internal/wasm"
binaryformat "github.com/tetratelabs/wazero/internal/wasm/binary"
"github.com/tetratelabs/wazero/sys"
Expand Down Expand Up @@ -124,9 +123,6 @@ func NewRuntime(ctx context.Context) Runtime {

// NewRuntimeWithConfig returns a runtime with the given configuration.
func NewRuntimeWithConfig(ctx context.Context, rConfig RuntimeConfig) Runtime {
if v := ctx.Value(version.WazeroVersionKey{}); v == nil {
ctx = context.WithValue(ctx, version.WazeroVersionKey{}, wazeroVersion)
}
config := rConfig.(*runtimeConfig)
var engine wasm.Engine
var cacheImpl *cache
Expand Down
14 changes: 0 additions & 14 deletions runtime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"github.com/tetratelabs/wazero/internal/leb128"
"github.com/tetratelabs/wazero/internal/platform"
"github.com/tetratelabs/wazero/internal/testing/require"
"github.com/tetratelabs/wazero/internal/version"
"github.com/tetratelabs/wazero/internal/wasm"
binaryformat "github.com/tetratelabs/wazero/internal/wasm/binary"
"github.com/tetratelabs/wazero/sys"
Expand All @@ -41,19 +40,6 @@ func (h *HostContext) Err() error { return nil }

func (h *HostContext) Value(key interface{}) interface{} { return nil }

func TestNewRuntimeWithConfig_version(t *testing.T) {
cfg := NewRuntimeConfig().(*runtimeConfig)
oldNewEngine := cfg.newEngine
cfg.newEngine = func(ctx context.Context, features api.CoreFeatures, _ filecache.Cache) wasm.Engine {
// Ensures that wazeroVersion is propagated to the engine.
v := ctx.Value(version.WazeroVersionKey{})
require.NotNil(t, v)
require.Equal(t, wazeroVersion, v.(string))
return oldNewEngine(ctx, features, nil)
}
_ = NewRuntimeWithConfig(testCtx, cfg)
}

func TestRuntime_CompileModule(t *testing.T) {
tests := []struct {
name string
Expand Down
10 changes: 0 additions & 10 deletions version.go

This file was deleted.

0 comments on commit 9c07b27

Please sign in to comment.