Skip to content

Commit

Permalink
Bump to Wasmtime 15 (#202)
Browse files Browse the repository at this point in the history
* Bump to Wasmtime 15

* go fmt
  • Loading branch information
alexcrichton authored Nov 20, 2023
1 parent 650f03f commit edcd045
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 83 deletions.
2 changes: 1 addition & 1 deletion BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ go_library(
],
"//conditions:default": [],
}),
importpath = "github.com/bytecodealliance/wasmtime-go/v14",
importpath = "github.com/bytecodealliance/wasmtime-go/v15",
visibility = ["//visibility:public"],
)

Expand Down
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
<a href="https://github.com/bytecodealliance/wasmtime-go/actions?query=workflow%3ACI">
<img src="https://github.com/bytecodealliance/wasmtime-go/workflows/CI/badge.svg" alt="CI status"/>
</a>
<a href="https://pkg.go.dev/github.com/bytecodealliance/wasmtime-go/v14">
<img src="https://godoc.org/github.com/bytecodealliance/wasmtime-go/v14?status.svg" alt="Documentation"/>
<a href="https://pkg.go.dev/github.com/bytecodealliance/wasmtime-go/v15">
<img src="https://godoc.org/github.com/bytecodealliance/wasmtime-go/v15?status.svg" alt="Documentation"/>
</a>
<a href="https://bytecodealliance.github.io/wasmtime-go/coverage.html">
<img src="https://img.shields.io/badge/coverage-main-green" alt="Code Coverage"/>
Expand All @@ -25,7 +25,7 @@
## Installation

```sh
go get -u github.com/bytecodealliance/wasmtime-go/v14@v14.0.0
go get -u github.com/bytecodealliance/wasmtime-go/v15@v15.0.0
```

Be sure to check out the [API documentation][api]!
Expand All @@ -39,16 +39,16 @@ need to arrange to build Wasmtime and use `CGO_*` env vars to compile correctly.

This project has been tested with Go 1.13 or later.

[api]: https://pkg.go.dev/github.com/bytecodealliance/wasmtime-go/v14
[api]: https://pkg.go.dev/github.com/bytecodealliance/wasmtime-go/v15
[wasmtime]: https://github.com/bytecodealliance/wasmtime

If you are a bazel user, add following to your WORKSPACE file:

```
go_repository(
name = "com_github_bytecodealliance_wasmtime_go",
importpath = "github.com/bytecodealliance/wasmtime-go/v14",
version = "v14.0.0",
importpath = "github.com/bytecodealliance/wasmtime-go/v15",
version = "v15.0.0",
)
```

Expand All @@ -61,7 +61,7 @@ package main

import (
"fmt"
"github.com/bytecodealliance/wasmtime-go/v14"
"github.com/bytecodealliance/wasmtime-go/v15"
)

func main() {
Expand Down
4 changes: 3 additions & 1 deletion ci/download-wasmtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import glob


version = 'v14.0.0'
version = 'v15.0.0'
urls = [
['wasmtime-{}-x86_64-mingw-c-api.zip', 'windows-x86_64'],
['wasmtime-{}-x86_64-linux-c-api.tar.xz', 'linux-x86_64'],
Expand Down Expand Up @@ -61,6 +61,8 @@
os.remove(dylib)
for dylib in glob.glob("build/**/*.so"):
os.remove(dylib)
for dylib in glob.glob("build/**/*-min.a"):
os.remove(dylib)

for subdir, dirs, files in os.walk("build"):
dir_name = os.path.basename(os.path.normpath(subdir))
Expand Down
15 changes: 9 additions & 6 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"strings"
"time"

"github.com/bytecodealliance/wasmtime-go/v14"
"github.com/bytecodealliance/wasmtime-go/v15"
)

// Example of limiting a WebAssembly function's runtime using "fuel consumption".
Expand All @@ -19,7 +19,7 @@ func ExampleConfig_fuel() {
config.SetConsumeFuel(true)
engine := wasmtime.NewEngineWithConfig(config)
store := wasmtime.NewStore(engine)
err := store.AddFuel(10000)
err := store.SetFuel(10000)
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -57,18 +57,21 @@ func ExampleConfig_fuel() {
if fibonacci == nil {
log.Fatal("Failed to find function export `fibonacci`")
}
fuel := uint64(10000)
for n := 0; ; n++ {
fuelBefore, _ := store.FuelConsumed()
err := store.SetFuel(fuel)
if err != nil {
log.Fatal(err)
}
output, err := fibonacci.Call(store, n)
if err != nil {
break
}
fuelAfter, _ := store.FuelConsumed()
fmt.Printf("fib(%d) = %d [consumed %d fuel]\n", n, output, fuelAfter-fuelBefore)
err = store.AddFuel(fuelAfter - fuelBefore)
fuelAfter, err := store.GetFuel()
if err != nil {
log.Fatal(err)
}
fmt.Printf("fib(%d) = %d [consumed %d fuel]\n", n, output, fuel-fuelAfter)
}
// Output:
// fib(0) = 0 [consumed 6 fuel]
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/bytecodealliance/wasmtime-go/v14
module github.com/bytecodealliance/wasmtime-go/v15

go 1.18

Expand Down
14 changes: 7 additions & 7 deletions includebuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import (
// included in vendored dependencies.
// Cf. https://github.com/golang/go/issues/26366

_ "github.com/bytecodealliance/wasmtime-go/v14/build/include"
_ "github.com/bytecodealliance/wasmtime-go/v14/build/include/wasmtime"
_ "github.com/bytecodealliance/wasmtime-go/v14/build/linux-aarch64"
_ "github.com/bytecodealliance/wasmtime-go/v14/build/linux-x86_64"
_ "github.com/bytecodealliance/wasmtime-go/v14/build/macos-aarch64"
_ "github.com/bytecodealliance/wasmtime-go/v14/build/macos-x86_64"
_ "github.com/bytecodealliance/wasmtime-go/v14/build/windows-x86_64"
_ "github.com/bytecodealliance/wasmtime-go/v15/build/include"
_ "github.com/bytecodealliance/wasmtime-go/v15/build/include/wasmtime"
_ "github.com/bytecodealliance/wasmtime-go/v15/build/linux-aarch64"
_ "github.com/bytecodealliance/wasmtime-go/v15/build/linux-x86_64"
_ "github.com/bytecodealliance/wasmtime-go/v15/build/macos-aarch64"
_ "github.com/bytecodealliance/wasmtime-go/v15/build/macos-x86_64"
_ "github.com/bytecodealliance/wasmtime-go/v15/build/windows-x86_64"
)
52 changes: 17 additions & 35 deletions store.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,23 +227,27 @@ func goFinalizeFuncWrap(env unsafe.Pointer) {
gEngineFuncWrapSlab.deallocate(idx)
}

// FuelConsumed returns the amount of fuel consumed by this context's store
// execution so far.
// GetFuel returns the amount of fuel remaining in this store.
//
// If fuel consumption is not enabled via `Config.SetConsumeFuel` then this function
// will return false. Otherwise true is returned and the fuel parameter is
// filled in with fuel consumed so far.
// If fuel consumption is not enabled via `Config.SetConsumeFuel` then
// this function will return an error. Otherwise this will retrieve the fuel
// remaining and return it.
//
// Also note that fuel, if enabled, must be originally configured via `Store.AddFuel`.
func (store *Store) FuelConsumed() (uint64, bool) {
fuel := C.uint64_t(0)
enable := C.wasmtime_context_fuel_consumed(store.Context(), &fuel)
// Also note that fuel, if enabled, must be originally configured via
// `Store.SetFuel`.
func (store *Store) GetFuel() (uint64, error) {
var remaining uint64
c_remaining := C.uint64_t(remaining)
err := C.wasmtime_context_get_fuel(store.Context(), &c_remaining)
runtime.KeepAlive(store)
if err != nil {
return 0, mkError(err)
}

return uint64(fuel), bool(enable)
return uint64(c_remaining), nil
}

// AddFuel adds fuel to this context's store for wasm to consume while executing.
// SetFuel sets this store's fuel to the specified value.
//
// For this method to work fuel consumption must be enabled via
// `Config.SetConsumeFuel`. By default a store starts with 0 fuel
Expand All @@ -255,8 +259,8 @@ func (store *Store) FuelConsumed() (uint64, bool) {
// wasm to trap. More usages of fuel are planned for the future.
//
// If fuel is not enabled within this store then an error is returned.
func (store *Store) AddFuel(fuel uint64) error {
err := C.wasmtime_context_add_fuel(store.Context(), C.uint64_t(fuel))
func (store *Store) SetFuel(fuel uint64) error {
err := C.wasmtime_context_set_fuel(store.Context(), C.uint64_t(fuel))
runtime.KeepAlive(store)
if err != nil {
return mkError(err)
Expand All @@ -265,28 +269,6 @@ func (store *Store) AddFuel(fuel uint64) error {
return nil
}

// ConsumeFuel attempts to manually consume fuel from the store.
//
// If fuel consumption is not enabled via `Config.SetConsumeFuel` then
// this function will return an error. Otherwise this will attempt to consume
// the specified amount of `fuel` from the store. If successful the remaining
// amount of fuel is returned. If `fuel` couldn't be consumed
// then an error is returned.
//
// Also note that fuel, if enabled, must be originally configured via
// `Store.AddFuel`.
func (store *Store) ConsumeFuel(fuel uint64) (uint64, error) {
var remaining uint64
c_remaining := C.uint64_t(remaining)
err := C.wasmtime_context_consume_fuel(store.Context(), C.uint64_t(fuel), &c_remaining)
runtime.KeepAlive(store)
if err != nil {
return 0, mkError(err)
}

return uint64(c_remaining), nil
}

// Limiter provides limits for a store. Used by hosts to limit resource
// consumption of instances. Use negative value to keep the default value
// for the limit.
Expand Down
30 changes: 5 additions & 25 deletions store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ func TestFuelConsumed(t *testing.T) {
engine := NewEngine()
store := NewStore(engine)

fuel, enable := store.FuelConsumed()
require.False(t, enable)
fuel, enable := store.GetFuel()
require.Error(t, enable)
require.Equal(t, fuel, uint64(0))
}

Expand All @@ -53,35 +53,15 @@ func TestAddFuel(t *testing.T) {
engine := NewEngineWithConfig(config)
store := NewStore(engine)

fuel, enable := store.FuelConsumed()
require.True(t, enable)
fuel, enable := store.GetFuel()
require.NoError(t, enable)
require.Equal(t, fuel, uint64(0))

const add_fuel = 3
err := store.AddFuel(add_fuel)
err := store.SetFuel(add_fuel)
require.NoError(t, err)
}

func TestConsumeFuel(t *testing.T) {
config := NewConfig()
config.SetConsumeFuel(true)
engine := NewEngineWithConfig(config)
store := NewStore(engine)

fuel, enable := store.FuelConsumed()
require.True(t, enable)
require.Equal(t, fuel, uint64(0))

const add_fuel = 3
err := store.AddFuel(add_fuel)
require.NoError(t, err)

consume_fuel := uint64(1)
remaining, err := store.ConsumeFuel(consume_fuel)
require.NoError(t, err)
require.Equal(t, (add_fuel - consume_fuel), remaining)
}

func TestLimiterMemorySizeFail(t *testing.T) {
engine := NewEngine()
store := NewStore(engine)
Expand Down

0 comments on commit edcd045

Please sign in to comment.