Skip to content

Commit ec3a0a7

Browse files
committed
Expose VMConfig constructor
1 parent 7250c10 commit ec3a0a7

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

internal/api/lib.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"encoding/json"
99
"fmt"
1010
"os"
11+
"path/filepath"
1112
"runtime"
1213
"strings"
1314
"syscall"
@@ -49,7 +50,7 @@ func InitCache(config types.VMConfig) (Cache, error) {
4950
return Cache{}, fmt.Errorf("Could not create base directory")
5051
}
5152

52-
lockfile, err := os.OpenFile(config.Cache.BaseDir+"/exclusive.lock", os.O_WRONLY|os.O_CREATE, 0o666)
53+
lockfile, err := os.OpenFile(filepath.Join(config.Cache.BaseDir, "exclusive.lock"), os.O_WRONLY|os.O_CREATE, 0o666)
5354
if err != nil {
5455
return Cache{}, fmt.Errorf("Could not open exclusive.lock")
5556
}

lib_libwasmvm.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ type VM struct {
3030
// `cacheSize` sets the size in MiB of an in-memory cache for e.g. module caching. Set to 0 to disable.
3131
// `deserCost` sets the gas cost of deserializing one byte of data.
3232
func NewVM(dataDir string, supportedCapabilities []string, memoryLimit uint32, printDebug bool, cacheSize uint32) (*VM, error) {
33-
// TODO: expose config variant of NewVM
3433
cache, err := api.InitCache(types.VMConfig{
3534
Cache: types.CacheOptions{
3635
BaseDir: dataDir,
@@ -45,6 +44,17 @@ func NewVM(dataDir string, supportedCapabilities []string, memoryLimit uint32, p
4544
return &VM{cache: cache, printDebug: printDebug}, nil
4645
}
4746

47+
// NewVMWithConfig creates a new VM with a custom configuration.
48+
// This allows for more fine-grained control over the VM's behavior compared to NewVM and
49+
// can be extended more easily in the future.
50+
func NewVMWithConfig(config types.VMConfig, printDebug bool) (*VM, error) {
51+
cache, err := api.InitCache(config)
52+
if err != nil {
53+
return nil, err
54+
}
55+
return &VM{cache: cache, printDebug: printDebug}, nil
56+
}
57+
4858
// Cleanup should be called when no longer using this instances.
4959
// It frees resources in libwasmvm (the Rust part) and releases a lock in the base directory.
5060
func (vm *VM) Cleanup() {

types/config.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import (
44
"encoding/json"
55
)
66

7+
// VMConfig defines the configuration for the VM.
8+
// For full documentation see the Rust side:
9+
// https://github.com/CosmWasm/cosmwasm/blob/main/packages/vm/src/config.rs#L27
710
type VMConfig struct {
811
WasmLimits WasmLimits `json:"wasm_limits"`
912
Cache CacheOptions `json:"cache"`

0 commit comments

Comments
 (0)