Bot releases are visible (Hide)
Published by Hywan over 3 years ago
CompilerKind
, EngineKind
, IsCompilerAvailable
and IsEngineAvailable
symbols.Config.UseSinglepassCompiler
.custom_wasmer_runtime
build tag.(nothing)
Published by jubianchi over 3 years ago
aarch64
Published by jubianchi almost 4 years ago
Update to Wasmer 1.0.0
Published by jubianchi almost 4 years ago
The whole API changed to better match Wasmer and Wasm C API
// Create an Engine
engine := wasmer.NewEngine()
// Create a Store
store := wasmer.NewStore(engine)
fmt.Println("Compiling module...")
module, err := wasmer.NewModule(store, wasmBytes)
if err != nil {
fmt.Println("Failed to compile module:", err)
}
// Create an empty import object.
importObject := wasmer.NewImportObject()
fmt.Println("Instantiating module...")
// Let's instantiate the Wasm module.
instance, err := wasmer.NewInstance(module, importObject)
if err != nil {
panic(fmt.Sprintln("Failed to instantiate the module:", err))
}
Please refer to the examples and documentation to learn more about the changes.
Published by Hywan over 4 years ago
Published by Hywan over 4 years ago
Memory can be imported, thus making the distinction between owned and borrowed memory (#119 by @koponen-styra).
The main new methods are Imports.AppendMemory
, and NewMemory
to create a new memory with bounds (in pages).
The Imports.Append
method is now deprecated in favor of Imports.AppendFunction
.
// Compile a WebAssembly module
module, _ := wasm.Compile(wasmBytes)
// Create a new memory (that's new!)
memory, _ := wasm.NewMemory(/* min page */ 1, /* max page */ 2)
// Create an import object
imports := wasm.NewImports().Namespace("env").AppendMemory("memory", memory)
importObject := wasm.NewImportObject()
_ = importObject.Extend(*imports)
instance, _ := module.InstantiateWithImportObject(importObject)
defer instance.Close()
// Now we can read or write the memory with `memory.Data()` as usual
Add Bazel files to build the project with Bazel (#108 by @joesonw)
Support multiple WASI version with WasiGetVersion
, NewDefaultWasiImportObjectForVersion
, NewWasiImportObjectForVersion
and WasiVersion
(#92 by @Hywan).
Supported version are:
Latest
,Snapshot0
,Snapshot1
,Unknown
(in case of error).// Compile a WebAssembly module
module, _ = wasm.Compile(wasmBytes)
// Read the WASI version required for this module
wasiVersion = wasm.WasiGetVersion(module)
// Create an import object
importObject := wasm.NewDefaultWasiImportObjectForVersion(wasiVersion)
// Extend the import object with the imports
imports, _ := wasm.NewImports().Namespace("env").AppendFunction("sum", sum, C.sum)
_ = importObject.Extend(*imports)
// Instantiate the module with the import object
instante, _ := module.InstantiateWithImportObject(importObject)
InstanceContext
supports user data with any reference types or types that include any reference types or other Go pointers (#85 and #94 by @AdamSLevy)
type logMessageContext struct {
message string
slice []string // that wasn't possible before
ptr *string // that wasn't possible before
}
str := "test"
contextData = logMessageContext {
message: "first",
slice: []string{str, str},
ptr: &str,
}
instance.SetContextData(&contextData)
WASI is now supported (#72 by @MarkMcCaskey)
// Compile a WebAssembly module
module, _ = wasm.Compile(wasmBytes)
// Declare imports as usual
imports, _ := wasm.NewImports().Namespace("env").AppendFunction("sum", sum, C.sum)
// Create an import object
importObject := wasm.NewDefaultWasiImportObject()
// Extend the import object with the imports
_ = importObject.Extend(*imports)
// Instantiate the module with the import object
instance, _ = wasm.InstantiateWithImportObject(importObject)
defer instance.Close()
// Run the module
instance.Exports["_start"]()
Instance
supports optional memory, i.e. a WebAssembly module that does not have an exported memory, and provides a new HasMemory
method (#63 by @Hywan)
cli
package version to v2 (#110 by @d0iasm)int64_t
to long long
in cgo (#67 by @ethanfrey)Published by Hywan over 5 years ago
#55 Add the Memory.Grow
method (@Hywan)
#42 Improve error messages when instantiating (@Hywan)
#38 Support import descriptors (@Hywan)
var module, _ = wasm.Compile(bytes)
assert.Equal(…, "log_message", module.Imports[0].Name)
assert.Equal(…, "env", module.Imports[0].Namespace)
#37 Support export descriptors (@Hywan)
var module, _ = wasm.Compile(bytes)
assert.Equal(…, "sum", module.Exports[7].Name)
#34 Support module serialization and deserialization (@Hywan)
// Compiles the bytes into a WebAssembly module.
module1, _ := wasm.Compile(GetBytes())
defer module1.Close()
// Serializes the module into a sequence of bytes.
serialization, _ := module1.Serialize()
// Do something with `serialization`.
// Then later…
// Deserializes the module.
module2, _ := wasm.DeserializeModule(serialization)
defer module2.Close()
// And enjoy!
// Instantiates the WebAssembly module.
instance, _ := module2.Instantiate()
defer instance.Close()
// Gets an exported function.
sum, functionExists := instance.Exports["sum"]
fmt.Println(functionExists)
// Calls the `sum` exported function with Go values.
result, _ := sum(1, 2)
fmt.Println(result)
// Output:
// true
// 3
#33 Add Compile
, Module.Instantiate*
and Module.Close
(@Hywan)
#30 Support instance context data (@Hywan)
//export logMessageWithContextData
func logMessageWithContextData(context unsafe.Pointer, pointer int32, length int32) {
var instanceContext = wasm.IntoInstanceContext(context)
var memory = instanceContext.Memory().Data()
var logMessage = (*logMessageContext)(instanceContext.Data())
logMessage.message = string(memory[pointer : pointer+length])
}
type logMessageContext struct {
message string
}
func testImportInstanceContextData(t *testing.T) {
imports, err := wasm.NewImports().Append("log_message", logMessageWithContextData, C.logMessageWithContextData)
assert.NoError(t, err)
instance, err := wasm.NewInstanceWithImports(getImportedFunctionBytes("log.wasm"), imports)
assert.NoError(t, err)
defer instance.Close()
contextData := logMessageContext{message: "first"}
instance.SetContextData(unsafe.Pointer(&contextData))
doSomething := instance.Exports["do_something"]
result, err := doSomething()
assert.NoError(t, err)
assert.Equal(t, wasm.TypeVoid, result.GetType())
assert.Equal(t, "hello", contextData.message)
}
#29 Add Imports.Namespace
to set the current import namespace (@Hywan)
// By default, the namespace is `"env"`. Change it to `"ns"`.
wasm.NewImports().Namespace("ns").Append("f", f, C.f)
#26 Support instance context API (@Hywan)
cgocheck
level (@Hywan)bridge.go
(@Hywan)