Skip to content

Commit

Permalink
lint and fmt code in Go style (bytecodealliance#22)
Browse files Browse the repository at this point in the history
* Update to latest wasmtime APIs

* Update CI to latest Go 1.13 and 1.14 releases (bytecodealliance#19)

1.14.3 at least had a change to cgo, maybe we'll get lucky.

Not sure what your thoughts are on how many point releases to cover in CI, so I just bumped 1.13 and 1.14 to the latest.

* fix readme of wrong NewInstance not enough arguments error (bytecodealliance#18)

* fix readme of wrong NewInstance not enough arguments error

* [readme] update go package version to 0.17.0

* lint and fmt code in Go style

- DONE: convert strcut and func docstring to Go style
- DONE: if block ends with a return statement, so drop this else and outdent its block
- DONE: don't use ALL_CAPS in Go names; use CamelCase
- DONE: dont use underscore in names
- DONE: receiver name config should be consistent with previous
- DONE: add comments according to webassembly spec

* fmt

Co-authored-by: Alex Crichton <alex@alexcrichton.com>
Co-authored-by: Alex Vidal <alexvidal@khanacademy.org>
Co-authored-by: lan <liangyuanpengem@163.com>
  • Loading branch information
4 people committed Jun 25, 2020
1 parent fd2772b commit 1b3b541
Show file tree
Hide file tree
Showing 34 changed files with 544 additions and 462 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ jobs:

- uses: actions/setup-go@v1
with:
go-version: 1.14
go-version: '1.14'
- run: go test -coverprofile cover.out ./...
- run: go tool cover -html=cover.out -o coverage.html
- uses: actions/upload-artifact@v1
Expand Down
73 changes: 36 additions & 37 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,48 +9,47 @@ import (
"unsafe"
)

// Compilation strategies for wasmtime
// Strategy is the compilation strategies for wasmtime
type Strategy C.wasmtime_strategy_t

const (
// Wasmtime will automatically pick an appropriate compilation strategy
STRATEGY_AUTO Strategy = C.WASMTIME_STRATEGY_AUTO
// Force wasmtime to use the Cranelift backend
STRATEGY_CRANELIFT Strategy = C.WASMTIME_STRATEGY_CRANELIFT
// Force wasmtime to use the lightbeam backend
STRATEGY_LIGHTBEAM Strategy = C.WASMTIME_STRATEGY_LIGHTBEAM
// StrategyAuto will let wasmtime automatically pick an appropriate compilation strategy
StrategyAuto Strategy = C.WASMTIME_STRATEGY_AUTO
// StrategyCranelift will force wasmtime to use the Cranelift backend
StrategyCranelift Strategy = C.WASMTIME_STRATEGY_CRANELIFT
// StrategyLightbeam will force wasmtime to use the lightbeam backend
StrategyLightbeam Strategy = C.WASMTIME_STRATEGY_LIGHTBEAM
)

// What degree of optimization wasmtime will perform on generated machine code
// OptLevel decides what degree of optimization wasmtime will perform on generated machine code
type OptLevel C.wasmtime_opt_level_t

const (
// No optimizations will be performed
OPT_LEVEL_NONE OptLevel = C.WASMTIME_OPT_LEVEL_NONE
// Machine code will be optimized to be as fast as possible
OPT_LEVEL_SPEED OptLevel = C.WASMTIME_OPT_LEVEL_SPEED
// Machine code will be optimized for speed, but also optimized
// OptLevelNone will perform no optimizations
OptLevelNone OptLevel = C.WASMTIME_OPT_LEVEL_NONE
// OptLevelSpeed will optimize machine code to be as fast as possible
OptLevelSpeed OptLevel = C.WASMTIME_OPT_LEVEL_SPEED
// OptLevelSpeedAndSize will optimize machine code for speed, but also optimize
// to be small, sometimes at the cost of speed.
OPT_LEVEL_SPEED_AND_SIZE OptLevel = C.WASMTIME_OPT_LEVEL_SPEED_AND_SIZE
OptLevelSpeedAndSize OptLevel = C.WASMTIME_OPT_LEVEL_SPEED_AND_SIZE
)

// What sort of profiling to enable, if any.
// ProfilingStrategy decides what sort of profiling to enable, if any.
type ProfilingStrategy C.wasmtime_profiling_strategy_t

const (
// No profiler will be used
PROFILING_STRATEGY_NONE ProfilingStrategy = C.WASMTIME_PROFILING_STRATEGY_NONE
// The "jitdump" linux support will be used
PROFILING_STRATEGY_JITDUMP ProfilingStrategy = C.WASMTIME_PROFILING_STRATEGY_JITDUMP
// ProfilingStrategyNone means no profiler will be used
ProfilingStrategyNone ProfilingStrategy = C.WASMTIME_PROFILING_STRATEGY_NONE
// ProfilingStrategyJitdump will use the "jitdump" linux support
ProfilingStrategyJitdump ProfilingStrategy = C.WASMTIME_PROFILING_STRATEGY_JITDUMP
)

// Configuration of an `Engine` which is used to globally configure things
// like wasm features and such.
// Config holds options used to create an Engine and customize its behavior.
type Config struct {
_ptr *C.wasm_config_t
}

// Creates a new `Config` with all default options configured.
// NewConfig creates a new `Config` with all default options configured.
func NewConfig() *Config {
config := &Config{_ptr: C.wasm_config_new()}
runtime.SetFinalizer(config, func(config *Config) {
Expand All @@ -59,43 +58,43 @@ func NewConfig() *Config {
return config
}

// Configures whether dwarf debug information for JIT code is enabled
// SetDebugInfo configures whether dwarf debug information for JIT code is enabled
func (cfg *Config) SetDebugInfo(enabled bool) {
C.wasmtime_config_debug_info_set(cfg.ptr(), C.bool(enabled))
runtime.KeepAlive(cfg)
}

// Configures whether the wasm threads proposal is enabled
// SetWasmThreads configures whether the wasm threads proposal is enabled
func (cfg *Config) SetWasmThreads(enabled bool) {
C.wasmtime_config_wasm_threads_set(cfg.ptr(), C.bool(enabled))
runtime.KeepAlive(cfg)
}

// Configures whether the wasm reference types proposal is enabled
// SetWasmReferenceTypes configures whether the wasm reference types proposal is enabled
func (cfg *Config) SetWasmReferenceTypes(enabled bool) {
C.wasmtime_config_wasm_reference_types_set(cfg.ptr(), C.bool(enabled))
runtime.KeepAlive(cfg)
}

// Configures whether the wasm SIMD proposal is enabled
// SetWasmSIMD configures whether the wasm SIMD proposal is enabled
func (cfg *Config) SetWasmSIMD(enabled bool) {
C.wasmtime_config_wasm_simd_set(cfg.ptr(), C.bool(enabled))
runtime.KeepAlive(cfg)
}

// Configures whether the wasm bulk memory proposal is enabled
// SetWasmBulkMemory configures whether the wasm bulk memory proposal is enabled
func (cfg *Config) SetWasmBulkMemory(enabled bool) {
C.wasmtime_config_wasm_bulk_memory_set(cfg.ptr(), C.bool(enabled))
runtime.KeepAlive(cfg)
}

// Configures whether the wasm multi value proposal is enabled
// SetWasmMultiValue configures whether the wasm multi value proposal is enabled
func (cfg *Config) SetWasmMultiValue(enabled bool) {
C.wasmtime_config_wasm_multi_value_set(cfg.ptr(), C.bool(enabled))
runtime.KeepAlive(cfg)
}

// Configures what compilation strategy is used to compile wasm code
// SetStrategy configures what compilation strategy is used to compile wasm code
func (cfg *Config) SetStrategy(strat Strategy) error {
err := C.wasmtime_config_strategy_set(cfg.ptr(), C.wasmtime_strategy_t(strat))
runtime.KeepAlive(cfg)
Expand All @@ -105,20 +104,20 @@ func (cfg *Config) SetStrategy(strat Strategy) error {
return nil
}

// Configures whether the cranelift debug verifier will be active when
// SetCraneliftDebugVerifier configures whether the cranelift debug verifier will be active when
// cranelift is used to compile wasm code.
func (cfg *Config) SetCraneliftDebugVerifier(enabled bool) {
C.wasmtime_config_cranelift_debug_verifier_set(cfg.ptr(), C.bool(enabled))
runtime.KeepAlive(cfg)
}

// Configures the cranelift optimization level for generated code
// SetCraneliftOptLevel configures the cranelift optimization level for generated code
func (cfg *Config) SetCraneliftOptLevel(level OptLevel) {
C.wasmtime_config_cranelift_opt_level_set(cfg.ptr(), C.wasmtime_opt_level_t(level))
runtime.KeepAlive(cfg)
}

// Configures what profiler strategy to use for generated code
// SetProfiler configures what profiler strategy to use for generated code
func (cfg *Config) SetProfiler(profiler ProfilingStrategy) error {
err := C.wasmtime_config_profiler_set(cfg.ptr(), C.wasmtime_profiling_strategy_t(profiler))
runtime.KeepAlive(cfg)
Expand All @@ -128,7 +127,7 @@ func (cfg *Config) SetProfiler(profiler ProfilingStrategy) error {
return nil
}

// Enables compiled code caching for this `Config` using the default settings
// CacheConfigLoadDefault enables compiled code caching for this `Config` using the default settings
// configuration can be found.
//
// For more information about caching see
Expand All @@ -142,7 +141,7 @@ func (cfg *Config) CacheConfigLoadDefault() error {
return nil
}

// Enables compiled code caching for this `Config` using the setting specified
// CacheConfigLoad enables compiled code caching for this `Config` using the settings specified
// in the configuration file `path`.
//
// For more information about caching and configuration options see
Expand All @@ -158,16 +157,16 @@ func (cfg *Config) CacheConfigLoad(path string) error {
return nil
}

// Configures whether generated wasm code will be interrupted via interrupt
// SetInterruptable configures whether generated wasm code can be interrupted via interrupt
// handles.
func (cfg *Config) SetInterruptable(interruptable bool) {
C.wasmtime_config_interruptable_set(cfg.ptr(), C.bool(interruptable))
runtime.KeepAlive(cfg)
}

// See comments in `ffi.go` for what's going on here
func (config *Config) ptr() *C.wasm_config_t {
ret := config._ptr
func (cfg *Config) ptr() *C.wasm_config_t {
ret := cfg._ptr
maybeGC()
if ret == nil {
panic("Config has already been used up")
Expand Down
12 changes: 6 additions & 6 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@ func TestConfig(t *testing.T) {
NewConfig().SetWasmSIMD(true)
NewConfig().SetWasmBulkMemory(true)
NewConfig().SetWasmMultiValue(true)
err := NewConfig().SetStrategy(STRATEGY_AUTO)
err := NewConfig().SetStrategy(StrategyAuto)
if err != nil {
panic(err)
}
err = NewConfig().SetStrategy(STRATEGY_CRANELIFT)
err = NewConfig().SetStrategy(StrategyCranelift)
if err != nil {
panic(err)
}
NewConfig().SetCraneliftDebugVerifier(true)
NewConfig().SetCraneliftOptLevel(OPT_LEVEL_NONE)
NewConfig().SetCraneliftOptLevel(OPT_LEVEL_SPEED)
NewConfig().SetCraneliftOptLevel(OPT_LEVEL_SPEED_AND_SIZE)
NewConfig().SetProfiler(PROFILING_STRATEGY_NONE)
NewConfig().SetCraneliftOptLevel(OptLevelNone)
NewConfig().SetCraneliftOptLevel(OptLevelSpeed)
NewConfig().SetCraneliftOptLevel(OptLevelSpeedAndSize)
NewConfig().SetProfiler(ProfilingStrategyNone)
err = NewConfig().CacheConfigLoadDefault()
if err != nil {
panic(err)
Expand Down
5 changes: 2 additions & 3 deletions doc.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
A WebAssembly runtime for Go powered by Wasmtime.
Package wasmtime is a WebAssembly runtime for Go powered by Wasmtime.
This package provides everything necessary to compile and execute WebAssembly
modules as part of a Go program. Wasmtime is a JIT compiler written in Rust,
Expand All @@ -14,6 +14,5 @@ https://github.com/bytecodealliance/wasmtime-go/issues/new.
It's also worth pointing out that the authors of this package up to this point
primarily work in Rust, so if you've got suggestions of how to make this package
more idiomatic for Go we'd love to hear your thoughts!
*/
more idiomatic for Go we'd love to hear your thoughts! */
package wasmtime
22 changes: 11 additions & 11 deletions doc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func Example() {

// Next up we instantiate a module which is where we link in all our
// imports. We've got one import so we pass that in here.
instance, err := NewInstance(module, []*Extern{item.AsExtern()})
instance, err := NewInstance(store, module, []*Extern{item.AsExtern()})
check(err)

// After we've instantiated we can lookup our `run` function and call
Expand All @@ -49,7 +49,7 @@ func Example() {
// Output: Hello from Go!
}

const GCD_WAT = `
const GcdWat = `
(module
(func $gcd (param i32 i32) (result i32)
(local i32)
Expand Down Expand Up @@ -82,11 +82,11 @@ const GCD_WAT = `
// An example of a wasm module which calculates the GCD of two numbers
func Example_gcd() {
store := NewStore(NewEngine())
wasm, err := Wat2Wasm(GCD_WAT)
wasm, err := Wat2Wasm(GcdWat)
check(err)
module, err := NewModule(store, wasm)
check(err)
instance, err := NewInstance(module, []*Extern{})
instance, err := NewInstance(store, module, []*Extern{})
check(err)
run := instance.GetExport("gcd").Func()
result, err := run.Call(6, 27)
Expand All @@ -99,7 +99,7 @@ func Example_gcd() {
func Example_memory() {
// Create our `Store` context and then compile a module and create an
// instance from the compiled module all in one go.
wasmtime_store := NewStore(NewEngine())
wasmtimeStore := NewStore(NewEngine())
wasm, err := Wat2Wasm(`
(module
(memory (export "memory") 2 3)
Expand All @@ -116,9 +116,9 @@ func Example_memory() {
)
`)
check(err)
module, err := NewModule(wasmtime_store, wasm)
module, err := NewModule(wasmtimeStore, wasm)
check(err)
instance, err := NewInstance(module, []*Extern{})
instance, err := NewInstance(wasmtimeStore, module, []*Extern{})
check(err)

// Load up our exports from the instance
Expand Down Expand Up @@ -192,7 +192,7 @@ func Example_memory() {
// Finally we can also create standalone memories to get imported by
// wasm modules too.
memorytype := NewMemoryType(Limits{Min: 5, Max: 5})
memory2 := NewMemory(wasmtime_store, memorytype)
memory2 := NewMemory(wasmtimeStore, memorytype)
assert(memory2.Size() == 5)
assert(!memory2.Grow(1))
assert(memory2.Grow(0))
Expand Down Expand Up @@ -242,7 +242,7 @@ func Example_multi() {
return b + 1, a + 1
})

instance, err := NewInstance(module, []*Extern{callback.AsExtern()})
instance, err := NewInstance(store, module, []*Extern{callback.AsExtern()})
check(err)

g := instance.GetExport("g").Func()
Expand All @@ -257,8 +257,8 @@ func Example_multi() {
assert(a == 4)
assert(b == 2)

round_trip_many := instance.GetExport("round_trip_many").Func()
results, err = round_trip_many.Call(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
roundTripMany := instance.GetExport("round_trip_many").Func()
results, err = roundTripMany.Call(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
check(err)
arr = results.([]Val)

Expand Down
6 changes: 3 additions & 3 deletions engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ package wasmtime
import "C"
import "runtime"

// An instance of a wasmtime engine which is used to create a `Store`.
// Engine is an instance of a wasmtime engine which is used to create a `Store`.
//
// Engines are a form of global configuration for wasm compilations and modules
// and such.
type Engine struct {
_ptr *C.wasm_engine_t
}

// Creates a new `Engine` with default configuration.
// NewEngine creates a new `Engine` with default configuration.
func NewEngine() *Engine {
engine := &Engine{_ptr: C.wasm_engine_new()}
runtime.SetFinalizer(engine, func(engine *Engine) {
Expand All @@ -21,7 +21,7 @@ func NewEngine() *Engine {
return engine
}

// Creates a new `Engine` with the `Config` provided
// NewEngineWithConfig creates a new `Engine` with the `Config` provided
//
// Note that once a `Config` is passed to this method it cannot be used again.
func NewEngineWithConfig(config *Config) *Engine {
Expand Down
14 changes: 8 additions & 6 deletions exporttype.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@ package wasmtime
import "C"
import "runtime"

// ExportType is one of the exports component.
// A module defines a set of exports that become accessible to the host environment once the module has been instantiated.
type ExportType struct {
_ptr *C.wasm_exporttype_t
_owner interface{}
}

// Creates a new `ExportType` with the `name` and the type provided.
// NewExportType creates a new `ExportType` with the `name` and the type provided.
func NewExportType(name string, ty AsExternType) *ExportType {
name_vec := stringToByteVec(name)
nameVec := stringToByteVec(name)

// Creating an export type requires taking ownership, so create a copy
// so we don't have to invalidate pointers here. Shouldn't be too
Expand All @@ -21,9 +23,9 @@ func NewExportType(name string, ty AsExternType) *ExportType {
runtime.KeepAlive(extern)

// And once we've got all that create the export type!
export_ptr := C.wasm_exporttype_new(&name_vec, ptr)
exportPtr := C.wasm_exporttype_new(&nameVec, ptr)

return mkExportType(export_ptr, nil)
return mkExportType(exportPtr, nil)
}

func mkExportType(ptr *C.wasm_exporttype_t, owner interface{}) *ExportType {
Expand All @@ -49,15 +51,15 @@ func (ty *ExportType) owner() interface{} {
return ty
}

// Returns the name in the module this export type is exporting
// Name returns the name in the module this export type is exporting
func (ty *ExportType) Name() string {
ptr := C.wasm_exporttype_name(ty.ptr())
ret := C.GoStringN(ptr.data, C.int(ptr.size))
runtime.KeepAlive(ty)
return ret
}

// Returns the type of item this export type expects
// Type returns the type of item this export type expects
func (ty *ExportType) Type() *ExternType {
ptr := C.wasm_exporttype_type(ty.ptr())
return mkExternType(ptr, ty.owner())
Expand Down
Loading

0 comments on commit 1b3b541

Please sign in to comment.