Go API v0.10.1 Documentation
The following are the guides to develop with the WasmEdge-Go SDK.
Developers can refer to here to upgrade to 0.11.0.
Table of Contents
Getting Started
The WasmEdge-go requires golang version >= 1.16. Please check your golang version before installation. Developers can download golang here.
$ go version
go version go1.16.5 linux/amd64
WasmEdge Installation
Developers must install the WasmEdge shared library with the same WasmEdge-go release or pre-release version.
curl -sSf https://raw.githubusercontent.com/WasmEdge/WasmEdge/master/utils/install.sh | bash -s -- -v 0.10.1
For the developers need the TensorFlow or Image extension for WasmEdge-go, please install the WasmEdge with extensions:
curl -sSf https://raw.githubusercontent.com/WasmEdge/WasmEdge/master/utils/install.sh | bash -s -- -e tf,image -v 0.10.1
Noticed that the TensorFlow and Image extensions are only for the Linux platforms. After installation, developers can use the source command to update the include and linking searching path.
Get WasmEdge-go
After the WasmEdge installation, developers can get the WasmEdge-go package and build it in your Go project directory.
go get github.com/second-state/WasmEdge-go/wasmedge@v0.10.1
go build
The WasmEdge-Go version number should match the installed WasmEdge version.
WasmEdge-go Extensions
By default, the WasmEdge-go only turns on the basic runtime.
WasmEdge-go has the following extensions (on the Linux platforms only):
- Tensorflow - This extension supports the host functions in WasmEdge-tensorflow. 
- The - TensorFlowand- TensorFlow-Liteextension when installing- WasmEdgeis required. Please install- WasmEdgewith the- -e tensorflowcommand.
- For using this extension, the tag - tensorflowwhen building is required:- go build -tags tensorflow
 
- Tensorflow-Lite ( - v0.10.1or upper only)- This extension supports the host functions in WasmEdge-tensorflow with only - TensorFlow-Lite.
- The - TensorFlow-Liteextension when installing- WasmEdgeis required. Please install- WasmEdgewith the- -e tensorflowcommand.
- THIS TAG CANNOT BE USED WITH THE - tensorflowTAG.
- For using this extension, the tag - tensorflowwhen building is required:- go build -tags tensorflowlite
 
- Image - This extension supports the host functions in WasmEdge-image. 
- The - Imageextension when installing- WasmEdgeis required. Please install- WasmEdgewith the- -e imagecommand.
- For using this extension, the tag - imagewhen building is required:- go build -tags image
 
Users can also turn on the multiple extensions when building:
go build -tags image,tensorflow
Example Repository
Developers can refer to the example repository for the WasmEdge-Go examples.
WasmEdge-go Basics
In this partition, we will introduce the utilities and concepts of WasmEdge-go APIs and data structures.
Version
The Version related APIs provide developers to check for the installed WasmEdge shared library version.
import "github.com/second-state/WasmEdge-go/wasmedge"
verstr := wasmedge.GetVersion() // Will be `string` of WasmEdge version.
vermajor := wasmedge.GetVersionMajor() // Will be `uint` of WasmEdge major version number.
verminor := wasmedge.GetVersionMinor() // Will be `uint` of WasmEdge minor version number.
verpatch := wasmedge.GetVersionPatch() // Will be `uint` of WasmEdge patch version number.
Logging Settings
The wasmedge.SetLogErrorLevel() and wasmedge.SetLogDebugLevel() APIs can set the logging system to debug level or error level. By default, the error level is set, and the debug info is hidden.
Value Types
In WasmEdge-go, the APIs will automatically do the conversion for the built-in types, and implement the data structure for the reference types.
- Number types: - i32,- i64,- f32, and- f64- Convert the uint32andint32toi32automatically when passing a value into WASM.
- Convert the uint64andint64toi64automatically when passing a value into WASM.
- Convert the uintandinttoi32automatically when passing a value into WASM in 32-bit system.
- Convert the uintandinttoi64automatically when passing a value into WASM in 64-bit system.
- Convert the float32tof32automatically when passing a value into WASM.
- Convert the float64tof64automatically when passing a value into WASM.
- Convert the i32from WASM toint32when getting a result.
- Convert the i64from WASM toint64when getting a result.
- Convert the f32from WASM tofloat32when getting a result.
- Convert the f64from WASM tofloat64when getting a result.
 
- Convert the 
- Number type: - v128for the- SIMDproposal- Developers should use the - wasmedge.NewV128()to generate a- v128value, and use the- wasmedge.GetV128()to get the value.- val := wasmedge.NewV128(uint64(1234), uint64(5678))
 high, low := val.GetVal()
 // `high` will be uint64(1234), `low` will be uint64(5678)
- Reference types: - FuncRefand- ExternReffor the- Reference-Typesproposal- var funccxt *wasmedge.Function = ... // Create or get function object.
 funcref := wasmedge.NewFuncRef(funccxt)
 // Create a `FuncRef` with the function object.
 num := 1234
 // `num` is a `int`.
 externref := wasmedge.NewExternRef(&num)
 // Create an `ExternRef` which reference to the `num`.
 num = 5678
 // Modify the `num` to 5678.
 numref := externref.GetRef().(*int)
 // Get the original reference from the `ExternRef`.
 fmt.Println(*numref)
 // Will print `5678`.
 numref.Release()
 // Should call the `Release` method.
Results
The Result object specifies the execution status. Developers can use the Error() function to get the error message.
// Assume that `vm` is a `wasmedge.VM` object.
res, err = vm.Execute(...) // Ignore the detail of parameters.
// Assume that `res, err` are the return values for executing a function with `vm`.
if err != nil {
  fmt.Println("Error message:", err.Error())
}
Contexts And Their Life Cycles
The objects, such as VM, Store, and Function, etc., are composed of Contexts in the WasmEdge shared library. All of the contexts can be created by calling the corresponding New APIs, developers should also call the corresponding Release functions of the contexts to release the resources. Noticed that it's not necessary to call the Release functions for the contexts which are retrieved from other contexts but not created from the New APIs.
// Create a Configure.
conf := wasmedge.NewConfigure()
// Release the `conf` immediately.
conf.Release()
The details of other contexts will be introduced later.
WASM Data Structures
The WASM data structures are used for creating instances or can be queried from instance contexts. The details of instances creation will be introduced in the Instances.
- Limit - The - Limitstruct presents the minimum and maximum value data structure.- lim1 := wasmedge.NewLimit(12)
 fmt.Println(lim1.HasMax())
 // Will print `false`.
 fmt.Println(lim1.GetMin())
 // Will print `12`.
 lim2 := wasmedge.NewLimitWithMax(15, 50)
 fmt.Println(lim2.HasMax())
 // Will print `true`.
 fmt.Println(lim2.GetMin())
 // Will print `15`.
 fmt.Println(lim2.GetMax())
 // Will print `50`.- For the thread proposal, the - Limitstruct also supports the shared memory description. (- v0.10.1or upper only)- lim3 := wasmedge.NewLimitShared(20)
 fmt.Println(lim3.HasMax())
 // Will print `false`.
 fmt.Println(lim3.IsShared())
 // Will print `true`.
 fmt.Println(lim3.GetMin())
 // Will print `20`.
 lim4 := wasmedge.NewLimitSharedWithMax(30, 40)
 fmt.Println(lim4.HasMax())
 // Will print `true`.
 fmt.Println(lim4.IsShared())
 // Will print `true`.
 fmt.Println(lim4.GetMin())
 // Will print `30`.
 fmt.Println(lim4.GetMax())
 // Will print `40`.
- Function type context - The - FunctionTypeis an object holds the function type context and used for the- Functioncreation, checking the value types of a- Functioninstance, or getting the function type with function name from VM. Developers can use the- FunctionTypeAPIs to get the parameter or return value types information.- functype := wasmedge.NewFunctionType(
 []wasmedge.ValType{
 wasmedge.ValType_ExternRef,
 wasmedge.ValType_I32,
 wasmedge.ValType_I64,
 }, []wasmedge.ValType{
 wasmedge.ValType_F32,
 wasmedge.ValType_F64,
 })
 plen := functype.GetParametersLength()
 // `plen` will be 3.
 rlen := functype.GetReturnsLength()
 // `rlen` will be 2.
 plist := functype.GetParameters()
 // `plist` will be `[]wasmedge.ValType{wasmedge.ValType_ExternRef, wasmedge.ValType_I32, wasmedge.ValType_I64}`.
 rlist := functype.GetReturns()
 // `rlist` will be `[]wasmedge.ValType{wasmedge.ValType_F32, wasmedge.ValType_F64}`.
 functype.Release()
- Table type context - The - TableTypeis an object holds the table type context and used for- Tableinstance creation or getting information from- Tableinstances.- lim := wasmedge.NewLimit(12)
 tabtype := wasmedge.NewTableType(wasmedge.RefType_ExternRef, lim)
 rtype := tabtype.GetRefType()
 // `rtype` will be `wasmedge.RefType_ExternRef`.
 getlim := tabtype.GetLimit()
 // `getlim` will be the same value as `lim`.
 tabtype.Release()
- Memory type context - The - MemoryTypeis an object holds the memory type context and used for- Memoryinstance creation or getting information from- Memoryinstances.- lim := wasmedge.NewLimit(1)
 memtype := wasmedge.NewMemoryType(lim)
 getlim := memtype.GetLimit()
 // `getlim` will be the same value as `lim`.
 memtype.Release()
- Global type context - The - GlobalTypeis an object holds the global type context and used for- Globalinstance creation or getting information from- Globalinstances.- globtype := wasmedge.NewGlobalType(wasmedge.ValType_F64, wasmedge.ValMut_Var)
 vtype := globtype.GetValType()
 // `vtype` will be `wasmedge.ValType_F64`.
 vmut := globtype.GetMutability()
 // `vmut` will be `wasmedge.ValMut_Var`.
 globtype.Release()
- Import type context - The - ImportTypeis an object holds the import type context and used for getting the imports information from a AST Module. Developers can get the external type (- function,- table,- memory, or- global), import module name, and external name from an- ImportTypeobject. The details about querying- ImportTypeobjects will be introduced in the AST Module.- var ast *wasmedge.AST = ...
 // Assume that `ast` is returned by the `Loader` for the result of loading a WASM file.
 imptypelist := ast.ListImports()
 // Assume that `imptypelist` is an array listed from the `ast` for the imports.
 for i, imptype := range imptypelist {
 exttype := imptype.GetExternalType()
 // The `exttype` must be one of `wasmedge.ExternType_Function`, `wasmedge.ExternType_Table`,
 // wasmedge.ExternType_Memory`, or `wasmedge.ExternType_Global`.
 modname := imptype.GetModuleName()
 extname := imptype.GetExternalName()
 // Get the module name and external name of the imports.
 extval := imptype.GetExternalValue()
 // The `extval` is the type of `interface{}` which indicates one of `*wasmedge.FunctionType`,
 // `*wasmedge.TableType`, `*wasmedge.MemoryType`, or `*wasmedge.GlobalType`.
 }
- Export type context - The - ExportTypeis an object holds the export type context is used for getting the exports information from a AST Module. Developers can get the external type (- function,- table,- memory, or- global) and external name from an- Export Typecontext. The details about querying- ExportTypeobjects will be introduced in the AST Module.- var ast *wasmedge.AST = ...
 // Assume that `ast` is returned by the `Loader` for the result of loading a WASM file.
 exptypelist := ast.ListExports()
 // Assume that `exptypelist` is an array listed from the `ast` for the exports.
 for i, exptype := range exptypelist {
 exttype := exptype.GetExternalType()
 // The `exttype` must be one of `wasmedge.ExternType_Function`, `wasmedge.ExternType_Table`,
 // wasmedge.ExternType_Memory`, or `wasmedge.ExternType_Global`.
 extname := exptype.GetExternalName()
 // Get the external name of the exports.
 extval := exptype.GetExternalValue()
 // The `extval` is the type of `interface{}` which indicates one of `*wasmedge.FunctionType`,
 // `*wasmedge.TableType`, `*wasmedge.MemoryType`, or `*wasmedge.GlobalType`.
 }
Async
After calling the asynchronous execution APIs, developers will get the wasmedge.Async object. Developers own the object and should call the (*Async).Release() API to release it.
- Get the execution result of the asynchronous execution - Developers can use the - (*Async).GetResult()API to block and wait for getting the return values. This function will block and wait for the execution. If the execution has finished, this function will return immediately. If the execution failed, this function will return an error.- async := ... // Ignored. Asynchronous execute a function.
 // Blocking and waiting for the execution and get the return values.
 res, err := async.GetResult()
 async.Release()
- Wait for the asynchronous execution with timeout settings - Besides waiting until the end of execution, developers can set the timeout to wait for. - async := ... // Ignored. Asynchronous execute a function.
 // Blocking and waiting for the execution with the timeout(ms).
 isend := async.WaitFor(1000)
 if isend {
 res, err := async.GetResult()
 // ...
 } else {
 async.Cancel()
 _, err := async.GetResult()
 // The error message in `err` will be "execution interrupted".
 }
 async.Release()
Configurations
The configuration object, wasmedge.Configure, manages the configurations for Loader, Validator, Executor, VM, and Compiler. Developers can adjust the settings about the proposals, VM host pre-registrations (such as WASI), and AOT compiler options, and then apply the Configure object to create other runtime objects.
- Proposals - WasmEdge supports turning on or off the WebAssembly proposals. This configuration is effective in any contexts created with the - Configureobject.- const (
 IMPORT_EXPORT_MUT_GLOBALS = Proposal(C.WasmEdge_Proposal_ImportExportMutGlobals)
 NON_TRAP_FLOAT_TO_INT_CONVERSIONS = Proposal(C.WasmEdge_Proposal_NonTrapFloatToIntConversions)
 SIGN_EXTENSION_OPERATORS = Proposal(C.WasmEdge_Proposal_SignExtensionOperators)
 MULTI_VALUE = Proposal(C.WasmEdge_Proposal_MultiValue)
 BULK_MEMORY_OPERATIONS = Proposal(C.WasmEdge_Proposal_BulkMemoryOperations)
 REFERENCE_TYPES = Proposal(C.WasmEdge_Proposal_ReferenceTypes)
 SIMD = Proposal(C.WasmEdge_Proposal_SIMD)
 TAIL_CALL = Proposal(C.WasmEdge_Proposal_TailCall)
 ANNOTATIONS = Proposal(C.WasmEdge_Proposal_Annotations)
 MEMORY64 = Proposal(C.WasmEdge_Proposal_Memory64)
 THREADS = Proposal(C.WasmEdge_Proposal_Threads)
 EXCEPTION_HANDLING = Proposal(C.WasmEdge_Proposal_ExceptionHandling)
 FUNCTION_REFERENCES = Proposal(C.WasmEdge_Proposal_FunctionReferences)
 )- Developers can add or remove the proposals into the - Configureobject.- // By default, the following proposals have turned on initially:
 // * IMPORT_EXPORT_MUT_GLOBALS
 // * NON_TRAP_FLOAT_TO_INT_CONVERSIONS
 // * SIGN_EXTENSION_OPERATORS
 // * MULTI_VALUE
 // * BULK_MEMORY_OPERATIONS
 // * REFERENCE_TYPES
 // * SIMD
 // For the current WasmEdge version, the following proposals are supported:
 // * TAIL_CALL
 // * MULTI_MEMORIES
 // * THREADS
 conf := wasmedge.NewConfigure()
 // Developers can also pass the proposals as parameters:
 // conf := wasmedge.NewConfigure(wasmedge.SIMD, wasmedge.BULK_MEMORY_OPERATIONS)
 conf.AddConfig(wasmedge.SIMD)
 conf.RemoveConfig(wasmedge.REFERENCE_TYPES)
 is_bulkmem := conf.HasConfig(wasmedge.BULK_MEMORY_OPERATIONS)
 // The `is_bulkmem` will be `true`.
 conf.Release()
- Host registrations - This configuration is used for the - VMcontext to turn on the- WASIor- wasmedge_processsupports and only effective in- VMobjects.- const (
 WASI = HostRegistration(C.WasmEdge_HostRegistration_Wasi)
 WasmEdge_PROCESS = HostRegistration(C.WasmEdge_HostRegistration_WasmEdge_Process)
 // The follows are `v0.10.1` or upper only.
 WasiNN = HostRegistration(C.WasmEdge_HostRegistration_WasiNN)
 WasiCrypto_Common = HostRegistration(C.WasmEdge_HostRegistration_WasiCrypto_Common)
 WasiCrypto_AsymmetricCommon = HostRegistration(C.WasmEdge_HostRegistration_WasiCrypto_AsymmetricCommon)
 WasiCrypto_Kx = HostRegistration(C.WasmEdge_HostRegistration_WasiCrypto_Kx)
 WasiCrypto_Signatures = HostRegistration(C.WasmEdge_HostRegistration_WasiCrypto_Signatures)
 WasiCrypto_Symmetric = HostRegistration(C.WasmEdge_HostRegistration_WasiCrypto_Symmetric)
 )- The details will be introduced in the preregistrations of VM context. - conf := wasmedge.NewConfigure()
 // Developers can also pass the proposals as parameters:
 // conf := wasmedge.NewConfigure(wasmedge.WASI)
 conf.AddConfig(wasmedge.WASI)
 conf.Release()
- Maximum memory pages - Developers can limit the page size of memory instances by this configuration. When growing the page size of memory instances in WASM execution and exceeding the limited size, the page growing will fail. This configuration is only effective in the - Executorand- VMobjects.- conf := wasmedge.NewConfigure()
 pagesize := conf.GetMaxMemoryPage()
 // By default, the maximum memory page size in each memory instances is 65536.
 conf.SetMaxMemoryPage(1234)
 pagesize := conf.GetMaxMemoryPage()
 // `pagesize` will be 1234.
 conf.Release()
- AOT compiler options - The AOT compiler options configure the behavior about optimization level, output format, dump IR, and generic binary. - const (
 // Disable as many optimizations as possible.
 CompilerOptLevel_O0 = CompilerOptimizationLevel(C.WasmEdge_CompilerOptimizationLevel_O0)
 // Optimize quickly without destroying debuggability.
 CompilerOptLevel_O1 = CompilerOptimizationLevel(C.WasmEdge_CompilerOptimizationLevel_O1)
 // Optimize for fast execution as much as possible without triggering significant incremental compile time or code size growth.
 CompilerOptLevel_O2 = CompilerOptimizationLevel(C.WasmEdge_CompilerOptimizationLevel_O2)
 // Optimize for fast execution as much as possible.
 CompilerOptLevel_O3 = CompilerOptimizationLevel(C.WasmEdge_CompilerOptimizationLevel_O3)
 // Optimize for small code size as much as possible without triggering significant incremental compile time or execution time slowdowns.
 CompilerOptLevel_Os = CompilerOptimizationLevel(C.WasmEdge_CompilerOptimizationLevel_Os)
 // Optimize for small code size as much as possible.
 CompilerOptLevel_Oz = CompilerOptimizationLevel(C.WasmEdge_CompilerOptimizationLevel_Oz)
 )
 const (
 // Native dynamic library format.
 CompilerOutputFormat_Native = CompilerOutputFormat(C.WasmEdge_CompilerOutputFormat_Native)
 // WebAssembly with AOT compiled codes in custom section.
 CompilerOutputFormat_Wasm = CompilerOutputFormat(C.WasmEdge_CompilerOutputFormat_Wasm)
 )- These configurations are only effective in - Compilercontexts.- conf := wasmedge.NewConfigure()
 // By default, the optimization level is O3.
 conf.SetCompilerOptimizationLevel(wasmedge.CompilerOptLevel_O2)
 // By default, the output format is universal WASM.
 conf.SetCompilerOutputFormat(wasmedge.CompilerOutputFormat_Native)
 // By default, the dump IR is `false`.
 conf.SetCompilerDumpIR(true)
 // By default, the generic binary is `false`.
 conf.SetCompilerGenericBinary(true)
 conf.Release()
- Statistics options - The statistics options configure the behavior about instruction counting, cost measuring, and time measuring in both runtime and AOT compiler. These configurations are effective in - Compiler,- VM, and- Executorobjects.- conf := wasmedge.NewConfigure()
 // By default, the instruction counting is `false` when running a compiled-WASM or a pure-WASM.
 conf.SetStatisticsInstructionCounting(true)
 // By default, the cost measurement is `false` when running a compiled-WASM or a pure-WASM.
 conf.SetStatisticsTimeMeasuring(true)
 // By default, the time measurement is `false` when running a compiled-WASM or a pure-WASM.
 conf.SetStatisticsCostMeasuring(true)
 conf.Release()
Statistics
The statistics object, wasmedge.Statistics, provides the instruction counter, cost summation, and cost limitation at runtime.
Before using statistics, the statistics configuration must be set. Otherwise, the return values of calling statistics are undefined behaviour.
- Instruction counter - The instruction counter can help developers to profile the performance of WASM running. Developers can retrieve the - Statisticsobject from the- VMobject, or create a new one for the- Executorcreation. The details will be introduced in the next partitions.- stat := wasmedge.NewStatistics()
 // ... After running the WASM functions with the `Statistics` object
 count := stat.GetInstrCount()
 ips := stat.GetInstrPerSecond()
 stat.Release()
- Cost table - The cost table is to accumulate the cost of instructions with their weights. Developers can set the cost table array (the indices are the byte code value of instructions, and the values are the cost of instructions) into the - Statisticsobject. If the cost limit value is set, the execution will return the- cost limit exceedederror immediately when exceeds the cost limit in runtime.- stat := wasmedge.NewStatistics()
 costtable := []uint64{
 0, 0,
 10, /* 0x02: Block */
 11, /* 0x03: Loop */
 12, /* 0x04: If */
 12, /* 0x05: Else */
 0, 0, 0, 0, 0, 0,
 20, /* 0x0C: Br */
 21, /* 0x0D: Br_if */
 22, /* 0x0E: Br_table */
 0,
 }
 // Developers can set the costs of each instruction. The value not covered will be 0.
 WasmEdge_StatisticsSetCostTable(StatCxt, CostTable, 16);
 stat.SetCostTable()
 stat.SetCostLimit(5000000)
 // ... After running the WASM functions with the `Statistics` object
 cost := stat.GetTotalCost()
 stat.Release()
Tools Driver (v0.10.1 or upper only)
Besides executing the wasmedge and wasmedgec CLI tools, developers can trigger the WasmEdge CLI tools in WasmEdge-Go. The API arguments are the same as the command line arguments of the CLI tools.
package main
import (
  "os"
  "github.com/second-state/WasmEdge-go/wasmedge"
)
func main() {
  wasmedge.RunWasmEdgeCLI(os.Args)
}
package main
import (
  "os"
  "github.com/second-state/WasmEdge-go/wasmedge"
)
func main() {
  wasmedge.RunWasmEdgeAOTCompilerCLI(os.Args)
}
WasmEdge VM
In this partition, we will introduce the functions of wasmedge.VM object and show examples of executing WASM functions.
WASM Execution Example With VM Object
The following shows the example of running the WASM for getting the Fibonacci. This example uses the WASM file fibonacci.wasm converted from the text format fibonacci.wat.
(module
  (export "fib" (func $fib))
  (func $fib (param $n i32) (result i32)
    (if
      (i32.lt_s (get_local $n)(i32.const 2))
      (return (i32.const 1))
    )
    (return
      (i32.add
        (call $fib (i32.sub (get_local $n)(i32.const 2)))
        (call $fib (i32.sub (get_local $n)(i32.const 1)))
      )
    )
  )
)
- Run WASM functions rapidly - Create a new Go project first: - mkdir wasmedge_test && cd wasmedge_test
 go mod init wasmedge_test- Assume that the WASM file - fibonacci.wasmfrom the text format fibonacci.wat is copied into the current- wasmedge_testdirectory, and create and edit the Go file- main.goas following:- package main
 import (
 "fmt"
 "github.com/second-state/WasmEdge-go/wasmedge"
 )
 func main() {
 // Set the logging level.
 wasmedge.SetLogErrorLevel()
 // Create the configure context and add the WASI support.
 // This step is not necessary unless you need WASI support.
 conf := wasmedge.NewConfigure(wasmedge.WASI)
 // Create VM with the configure.
 vm := wasmedge.NewVMWithConfig(conf)
 res, err := vm.RunWasmFile("fibonacci.wasm", "fib", uint32(21))
 if err == nil {
 fmt.Println("Get fibonacci[21]:", res[0].(int32))
 } else {
 fmt.Println("Run failed:", err.Error())
 }
 vm.Release()
 conf.Release()
 }- Then you can build and run the Golang application with the WasmEdge Golang SDK: (the 21 Fibonacci number is 17711 in 0-based index) - $ go get github.com/second-state/WasmEdge-go/wasmedge@v0.10.1
 $ go build
 $ ./wasmedge_test
 Get fibonacci[21]: 17711
- Instantiate and run WASM functions manually - Besides the above example, developers can run the WASM functions step-by-step with - VMobject APIs:- package main
 import (
 "fmt"
 "github.com/second-state/WasmEdge-go/wasmedge"
 )
 func main() {
 // Set the logging level.
 wasmedge.SetLogErrorLevel()
 // Create VM.
 vm := wasmedge.NewVM()
 var err error
 var res []interface{}
 // Step 1: Load WASM file.
 err = vm.LoadWasmFile("fibonacci.wasm")
 if err != nil {
 fmt.Println("Load WASM from file FAILED:", err.Error())
 return
 }
 // Step 2: Validate the WASM module.
 err = vm.Validate()
 if err != nil {
 fmt.Println("Validation FAILED:", err.Error())
 return
 }
 // Step 3: Instantiate the WASM module.
 err = vm.Instantiate()
 // Developers can load, validate, and instantiate another WASM module
 // to replace the instantiated one. In this case, the old module will
 // be cleared, but the registered modules are still kept.
 if err != nil {
 fmt.Println("Instantiation FAILED:", err.Error())
 return
 }
 // Step 4: Execute WASM functions. Parameters: (funcname, args...)
 res, err = vm.Execute("fib", uint32(25))
 // Developers can execute functions repeatedly after instantiation.
 if err == nil {
 fmt.Println("Get fibonacci[25]:", res[0].(int32))
 } else {
 fmt.Println("Run failed:", err.Error())
 }
 vm.Release()
 }- Then you can build and run: (the 25th Fibonacci number is 121393 in 0-based index) - $ go build
 $ ./wasmedge_test
 Get fibonacci[25]: 121393- The following graph explains the status of the - VMobject.- |========================|
 |------->| VM: Initiated |
 | |========================|
 | |
 | LoadWasm
 | |
 | v
 | |========================|
 |--------| VM: Loaded |<-------|
 | |========================| |
 | | ^ |
 | Validate | |
 Cleanup | LoadWasm |
 | v | LoadWasm
 | |========================| |
 |--------| VM: Validated | |
 | |========================| |
 | | ^ |
 | Instantiate | |
 | | RegisterModule |
 | v | |
 | |========================| |
 |--------| VM: Instantiated |--------|
 |========================|
 | ^
 | |
 --------------
 Instantiate, Execute, ExecuteRegistered,
 ExecuteBindgen, ExecuteBindgenRegistered- The status of the - VMcontext would be- Initedwhen created. After loading WASM successfully, the status will be- Loaded. After validating WASM successfully, the status will be- Validated. After instantiating WASM successfully, the status will be- Instantiated, and developers can invoke functions. Developers can register WASM or import objects in any status, but they should instantiate WASM again. Developers can also load WASM in any status, and they should validate and instantiate the WASM module before function invocation. When in the- Instantiatedstatus, developers can instantiate the WASM module again to reset the old WASM runtime structures.
VM Creations
The VM creation APIs accepts the Configure object and the Store object. Noticed that if the VM created with the outside Store object, the VM will execute WASM on that Store object. If the Store object is set into multiple VM objects, it may causes data conflict when in execution. The details of the Store object will be introduced in Store.
conf := wasmedge.NewConfigure()
store := wasmedge.NewStore()
// Create a VM with default configure and store.
vm := wasmedge.NewVM()
vm.Release()
// Create a VM with the specified configure and default store.
vm = wasmedge.NewVMWithConfig(conf)
vm.Release()
// Create a VM with the default configure and specified store.
vm = wasmedge.NewVMWithStore(store)
vm.Release()
// Create a VM with the specified configure and store.
vm = wasmedge.NewVMWithConfigAndStore(conf, store)
vm.Release()
conf.Release()
store.Release()
Preregistrations
WasmEdge provides the following built-in pre-registrations.
- WASI (WebAssembly System Interface) - Developers can turn on the WASI support for VM in the - Configureobject.- conf := wasmedge.NewConfigure(wasmedge.WASI)
 // Or you can set the `wasmedge.WASI` into the configure object through `(*Configure).AddConfig`.
 vm := wasmedge.NewVMWithConfig(conf)
 vm.Release()
 // The following API can retrieve the pre-registration import objects from the VM object.
 // This API will return `nil` if the corresponding pre-registration is not set into the configuration.
 wasiconf := conf.GetImportModule(wasmedge.WASI)
 // Initialize the WASI.
 wasiconf.InitWasi(/* ... ignored */)
 conf.Release()- And also can create the WASI import object from API. The details will be introduced in the Host Functions and the Host Module Registrations. 
- This pre-registration is for the process interface for WasmEdge on - Rustsources. After turning on this pre-registration, the VM will support the- wasmedge_processplugin.- conf := wasmedge.NewConfigure(wasmedge.WasmEdge_PROCESS)
 vm := wasmedge.NewVMWithConfig(conf)
 vm.Release()
 // The following API can retrieve the pre-registration import objects from the VM object.
 // This API will return `nil` if the corresponding pre-registration is not set into the configuration.
 procconf := conf.GetImportModule(wasmedge.WasmEdge_PROCESS)
 // Initialize the WasmEdge_Process.
 procconf.InitWasmEdgeProcess(/* ... ignored */)
 conf.Release()- And also can create the WasmEdge_Process import object from API. The details will be introduced in the Host Functions and the Host Module Registrations. 
- WASI-NN proposal ( - v0.10.1or upper only)- Developers can turn on the WASI-NN proposal support for VM in the - Configureobject.- Note: Please check that the dependencies and prerequests are satisfied. - conf := wasmedge.NewConfigure(wasmedge.WasiNN)
 vm := wasmedge.NewVMWithConfig(conf)
 vm.Release()
 // The following API can retrieve the pre-registration import objects from the VM object.
 // This API will return `nil` if the corresponding pre-registration is not set into the configuration.
 nnmodule := conf.GetImportModule(wasmedge.WasiNN)
 conf.Release()- And also can create the WASI-NN module instance from API. The details will be introduced in the Host Functions and the Host Module Registrations. 
- WASI-Crypto proposal ( - v0.10.1or upper only)- Developers can turn on the WASI-Crypto proposal support for VM in the - Configureobject.- Note: Please check that the dependencies and prerequests are satisfied. - conf := wasmedge.NewConfigure(wasmedge.WasiCrypto_Common, wasmedge.WasiCrypto_AsymmetricCommon, wasmedge.WasiCrypto_Kx, wasmedge.WasiCrypto_Signatures, wasmedge.WasiCrypto_Symmetric)
 vm := wasmedge.NewVMWithConfig(conf)
 vm.Release()
 // The following API can retrieve the pre-registration import objects from the VM object.
 // This API will return `nil` if the corresponding pre-registration is not set into the configuration.
 nnmodule := conf.GetImportModule(wasmedge.WasiCrypto_Common)
 conf.Release()- And also can create the WASI-Crypto module instance from API. The details will be introduced in the Host Functions and the Host Module Registrations. 
Host Module Registrations
Host functions are functions outside WebAssembly and passed to WASM modules as imports. In WasmEdge-go, the host functions are composed into host modules as Module objects with module names. Please refer to the Host Functions in WasmEdge Runtime for the details. In this chapter, we show the example for registering the host modules into a VM object.
vm := wasmedge.NewVM()
// You can also create and register the WASI host modules by this API.
wasiobj := wasmedge.NewWasiModule(/* ... ignored ... */)
res := vm.RegisterModule(wasiobj)
// The result status should be checked.
vm.Release()
// The created import objects should be released.
wasiobj.Release()
WASM Registrations And Executions
In WebAssembly, the instances in WASM modules can be exported and can be imported by other WASM modules. WasmEdge VM provides APIs for developers to register and export any WASM modules, and execute the functions or host functions in the registered WASM modules.
- Register the WASM modules with exported module names - Unless the import objects have already contained the module names, every WASM module should be named uniquely when registering. The following shows the example. - Create a new Go project first: - mkdir wasmedge_test && cd wasmedge_test
 go mod init wasmedge_test- Assume that the WASM file - fibonacci.wasmfrom the text format fibonacci.wat is copied into the current directory. Then create and edit the Go file- main.goas following:- package main
 import "github.com/second-state/WasmEdge-go/wasmedge"
 func main() {
 // Create VM.
 vm := wasmedge.NewVM()
 var err error
 err = vm.RegisterWasmFile("module_name", "fibonacci.wasm")
 // Developers can register the WASM module from `[]byte` with the
 // `(*VM).RegisterWasmBuffer` function, or from `AST` object with
 // the `(*VM).RegisterAST` function.
 // The result status should be checked. The error will occur if the
 // WASM module instantiation failed or the module name conflicts.
 vm.Release()
 }
- Execute the functions in registered WASM modules - Edit the Go file - main.goas following:- package main
 import (
 "fmt"
 "github.com/second-state/WasmEdge-go/wasmedge"
 )
 func main() {
 // Create VM.
 vm := wasmedge.NewVM()
 var res []interface{}
 var err error
 // Register the WASM module from file into VM with the module name "mod".
 err = vm.RegisterWasmFile("mod", "fibonacci.wasm")
 // Developers can register the WASM module from `[]byte` with the
 // `(*VM).RegisterWasmBuffer` function, or from `AST` object with
 // the `(*VM).RegisterAST` function.
 if err != nil {
 fmt.Println("WASM registration failed:", err.Error())
 return
 }
 // The function "fib" in the "fibonacci.wasm" was exported with the module
 // name "mod". As the same as host functions, other modules can import the
 // function `"mod" "fib"`.
 // Execute WASM functions in registered modules.
 // Unlike the execution of functions, the registered functions can be
 // invoked without `(*VM).Instantiate` because the WASM module was
 // instantiated when registering.
 // Developers can also invoke the host functions directly with this API.
 res, err = vm.ExecuteRegistered("mod", "fib", int32(25))
 if err == nil {
 fmt.Println("Get fibonacci[25]:", res[0].(int32))
 } else {
 fmt.Println("Run failed:", err.Error())
 }
 vm.Release()
 }- Then you can build and run: (the 25th Fibonacci number is 121393 in 0-based index) - $ go get github.com/second-state/WasmEdge-go/wasmedge@v0.10.1
 $ go build
 $ ./wasmedge_test
 Get fibonacci[25]: 121393
Asynchronous Execution
- Asynchronously run WASM functions rapidly - Assume that a new Go project is created as following: - mkdir wasmedge_test && cd wasmedge_test
 go mod init wasmedge_test- Then assume that the WASM file - fibonacci.wasmfrom the text format fibonacci.wat is copied into the current directory, and create and edit a Go file- main.go:- package main
 import (
 "fmt"
 "github.com/second-state/WasmEdge-go/wasmedge"
 )
 func main() {
 // Create VM.
 vm := wasmedge.NewVM()
 // Asynchronously run the WASM function from file and get the `wasmedge.Async` object.
 async := vm.AsyncRunWasmFile("fibonacci.wasm", "fib", uint32(20))
 // Block and wait for the execution and get the results.
 res, err := async.GetResult()
 if err == nil {
 fmt.Println("Get the result:", res[0].(int32))
 } else {
 fmt.Println("Error message:", err.Error())
 }
 async.Release()
 vm.Release()
 }- Then you can build and run: (the 20th Fibonacci number is 10946 in 0-based index) - $ go get github.com/second-state/WasmEdge-go/wasmedge@v0.10.1
 $ go build
 $ ./wasmedge_test
 Get the result: 10946
- Instantiate and asynchronously run WASM functions manually - Besides the above example, developers can run the WASM functions step-by-step with - VMcontext APIs:- package main
 import (
 "fmt"
 "github.com/second-state/WasmEdge-go/wasmedge"
 )
 func main() {
 var err error
 var res []interface{}
 // Create VM.
 vm := wasmedge.NewVM()
 // Step 1: Load WASM file.
 // Developers can load the WASM binary from buffer with the `(*VM).LoadWasmBuffer()` API,
 // or from `wasmedge.AST` object with the `(*VM).LoadWasmAST()` API.
 err := vm.LoadWasmFile("fibonacci.wasm")
 if err != nil {
 fmt.Println("Load WASM from file FAILED:", err.Error())
 return
 }
 // Step 2: Validate the WASM module.
 err = vm.Validate()
 if err != nil {
 fmt.Println("Validation FAILED:", err.Error())
 return
 }
 // Step 3: Instantiate the WASM module.
 err = vm.Instantiate()
 if err != nil {
 fmt.Println("Instantiation FAILED:", err.Error())
 return
 }
 // Step 4: Asynchronously execute the WASM function and get the `wasmedge.Async` object.
 async := vm.AsyncExecute("fib", uint32(25))
 // Block and wait for the execution and get the results.
 res, err := async.GetResult()
 if err == nil {
 fmt.Println("Get the result:", res[0].(int32))
 } else {
 fmt.Println("Error message:", err.Error())
 }
 async.Release()
 vm.Release()
 }- Then you can build and run: (the 25th Fibonacci number is 121393 in 0-based index) - $ go get github.com/second-state/WasmEdge-go/wasmedge@v0.10.1
 $ go build
 $ ./wasmedge_test
 Get the result: 121393
Instance Tracing
Sometimes the developers may have requirements to get the instances of the WASM runtime. The VM object supplies the APIs to retrieve the instances.
- Store - If the - VMobject is created without assigning a- Storeobject, the- VMcontext will allocate and own a- Store.- vm := wasmedge.NewVM()
 store := vm.GetStore()
 // The object should __NOT__ be deleted by calling `(*Store).Release`.
 vm.Release()- Developers can also create the - VMobject with a- Storeobject. In this case, developers should guarantee that the- Storeobject cannot be released before the- VMobject. Please refer to the Store Objects for the details about the- StoreAPIs.- store := wasmedge.NewStore()
 vm := wasmedge.NewVMWithStore(store)
 storemock := vm.GetStore()
 // The internal store context of the `store` and the `storemock` are the same.
 vm.Release()
 store.Release()
- List exported functions - After the WASM module instantiation, developers can use the - (*VM).Executefunction to invoke the exported WASM functions. For this purpose, developers may need information about the exported WASM function list. Please refer to the Instances in runtime for the details about the function types.- Assume that a new Go project is created as following: - mkdir wasmedge_test && cd wasmedge_test
 go mod init wasmedge_test- Then assume that the WASM file - fibonacci.wasmfrom the text format fibonacci.wat is copied into the current directory, and create and edit a Go file- main.go:- package main
 import (
 "fmt"
 "github.com/second-state/WasmEdge-go/wasmedge"
 )
 func main() {
 // Create VM.
 vm := wasmedge.NewVM()
 // Step 1: Load WASM file.
 err := vm.LoadWasmFile("fibonacci.wasm")
 if err != nil {
 fmt.Println("Load WASM from file FAILED:", err.Error())
 return
 }
 // Step 2: Validate the WASM module.
 err = vm.Validate()
 if err != nil {
 fmt.Println("Validation FAILED:", err.Error())
 return
 }
 // Step 3: Instantiate the WASM module.
 err = vm.Instantiate()
 if err != nil {
 fmt.Println("Instantiation FAILED:", err.Error())
 return
 }
 // List the exported functions for the names and function types.
 funcnames, functypes := vm.GetFunctionList()
 for _, fname := range funcnames {
 fmt.Println("Exported function name:", fname)
 }
 for _, ftype := range functypes {
 // `ftype` is the `FunctionType` object of the same index in the `funcnames` array.
 // Developers should __NOT__ call the `ftype.Release()`.
 }
 vm.Release()
 }- Then you can build and run: (the only exported function in - fibonacci.wasmis- fib)- $ go get github.com/second-state/WasmEdge-go/wasmedge@v0.10.1
 $ go build
 $ ./wasmedge_test
 Exported function name: fib- If developers want to get the exported function names in the registered WASM modules, please retrieve the - Storeobject from the- VMobject and refer to the APIs of Store Contexts to list the registered functions by the module name.
- Get function types - The - VMobject provides APIs to find the function type by function name. Please refer to the Instances in runtime for the details about the function types.- // Assume that a WASM module is instantiated in `vm` which is a `wasmedge.VM` object.
 functype := vm.GetFunctionType("fib")
 // Developers can get the function types of functions in the registered modules via the
 // `(*VM).GetFunctionTypeRegistered` API with the function name and the module name.
 // If the function is not found, these APIs will return `nil`.
 // Developers should __NOT__ call the `(*FunctionType).Release` function of the returned object.
- Get the active module - After the WASM module instantiation, an anonymous module is instantiated and owned by the - VMobject. Developers may need to retrieve it to get the instances beyond the module. Then developers can use the- (*VM).GetActiveModule()API to get that anonymous module instance. Please refer to the Module instance for the details about the module instance APIs.- // Assume that a WASM module is instantiated in `vm` which is a `wasmedge.VM` object.
 mod := vm.GetActiveModule()
 // If there's no WASM module instantiated, this API will return `nil`.
 // Developers should __NOT__ call the `(*Module).Release` function of the returned module instance.
- Get the components ( - v0.10.1or upper only)- The - VMobject is composed by the- Loader,- Validator, and- Executorobjects. For the developers who want to use these objects without creating another instances, these APIs can help developers to get them from the- VMobject. The get objects are owned by the- VMobject, and developers should not call their release functions.- loader := vm.GetLoader()
 // Developers should __NOT__ call the `(*Loader).Release` function of the returned object.
 validator := vm.GetValidator()
 // Developers should __NOT__ call the `(*Validator).Release` function of the returned object.
 executor := vm.GetExecutor()
 // Developers should __NOT__ call the `(*Executor).Release` function of the returned object.
WasmEdge Runtime
In this partition, we will introduce the objects of WasmEdge runtime manually.
WASM Execution Example Step-By-Step
Besides the WASM execution through the VM object rapidly, developers can execute the WASM functions or instantiate WASM modules step-by-step with the Loader, Validator, Executor, and Store objects.
Assume that a new Go project is created as following:
mkdir wasmedge_test && cd wasmedge_test
go mod init wasmedge_test
Then assume that the WASM file fibonacci.wasm from the text format fibonacci.wat is copied into the current directory, and create and edit a Go file main.go:
package main
import (
  "fmt"
  "github.com/second-state/WasmEdge-go/wasmedge"
)
func main() {
  // Set the logging level to debug to print the statistics info.
  wasmedge.SetLogDebugLevel()
  // Create the configure object. This is not necessary if developers use the default configuration.
  conf := wasmedge.NewConfigure()
  // Turn on the runtime instruction counting and time measuring.
  conf.SetStatisticsInstructionCounting(true)
  conf.SetStatisticsTimeMeasuring(true)
  // Create the statistics object. This is not necessary if the statistics in runtime is not needed.
  stat := wasmedge.NewStatistics()
  // Create the store object. The store object is the WASM runtime structure core.
  store := wasmedge.NewStore()
  var err error
  var res []interface{}
  var ast *wasmedge.AST
  var mod *wasmedge.Module
  // Create the loader object.
  // For loader creation with default configuration, you can use `wasmedge.NewLoader()` instead.
  loader := wasmedge.NewLoaderWithConfig(conf)
  // Create the validator object.
  // For validator creation with default configuration, you can use `wasmedge.NewValidator()` instead.
  validator := wasmedge.NewValidatorWithConfig(conf)
  // Create the executor object.
  // For executor creation with default configuration and without statistics, you can use `wasmedge.NewExecutor()` instead.
  executor := wasmedge.NewExecutorWithConfigAndStatistics(conf, stat)
  // Load the WASM file or the compiled-WASM file and convert into the AST module object.
  ast, err = loader.LoadFile("fibonacci.wasm")
  if err != nil {
    fmt.Println("Load WASM from file FAILED:", err.Error())
    return
  }
  // Validate the WASM module.
  err = validator.Validate(ast)
  if err != nil {
    fmt.Println("Validation FAILED:", err.Error())
    return
  }
  // Instantiate the WASM module and get the output module instance.
  mod, err = executor.Instantiate(store, ast)
  if err != nil {
    fmt.Println("Instantiation FAILED:", err.Error())
    return
  }
  // Try to list the exported functions of the instantiated WASM module.
  funcnames := mod.ListFunction()
  for _, fname := range funcnames {
    fmt.Println("Exported function name:", fname)
  }
  // Invoke the WASM function.
  funcinst := mod.FindFunction("fib")
  if funcinst == nil {
    fmt.Println("Run FAILED: Function name `fib` not found")
    return
  }
  res, err = executor.Invoke(store, funcinst, int32(30))
  if err == nil {
    fmt.Println("Get fibonacci[30]:", res[0].(int32))
  } else {
    fmt.Println("Run FAILED:", err.Error())
  }
  // Resources deallocations.
  conf.Release()
  stat.Release()
  ast.Release()
  loader.Release()
  validator.Release()
  executor.Release()
  store.Release()
  mod.Release()
}
Then you can build and run: (the 18th Fibonacci number is 1346269 in 30-based index)
$ go get github.com/second-state/WasmEdge-go/wasmedge@v0.10.1
$ go build
$ ./wasmedge_test
Exported function name: fib
[2021-11-24 18:53:01.451] [debug]  Execution succeeded.
[2021-11-24 18:53:01.452] [debug]
 ====================  Statistics  ====================
 Total execution time: 556372295 ns
 Wasm instructions execution time: 556372295 ns
 Host functions execution time: 0 ns
 Executed wasm instructions count: 28271634
 Gas costs: 0
 Instructions per second: 50814237
Get fibonacci[30]: 1346269
Loader
The Loader object loads the WASM binary from files or buffers. Both the WASM and the compiled-WASM from the WasmEdge AOT Compiler are supported.
var buf []byte
// ... Read the WASM code to the `buf`.
// Developers can adjust settings in the configure object.
conf := wasmedge.NewConfigure()
// Create the loader object.
// For loader creation with default configuration, you can use `wasmedge.NewLoader()` instead.
loader := wasmedge.NewLoaderWithConfig(conf)
conf.Release()
// Load WASM or compiled-WASM from the file.
ast, err := loader.LoadFile("fibonacci.wasm")
if err != nil {
  fmt.Println("Load WASM from file FAILED:", err.Error())
} else {
  // The output AST object should be released.
  ast.Release()
}
// Load WASM or compiled-WASM from the buffer
ast, err = loader.LoadBuffer(buf)
if err != nil {
  fmt.Println("Load WASM from buffer FAILED:", err.Error())
} else {
  // The output AST object should be released.
  ast.Release()
}
loader.Release()
Validator
The Validator object can validate the WASM module. Every WASM module should be validated before instantiation.
// ...
// Assume that the `ast` is the output `*wasmedge.AST` object from the loader context.
// Assume that the `conf` is the `*wasmedge.Configure` object.
// Create the validator context.
// For validator creation with default configuration, you can use `wasmedge.NewValidator()` instead.
validator := wasmedge.NewValidatorWithConfig(conf)
err := validator.Validate(ast)
if err != nil {
  fmt.Println("Validation FAILED:", err.Error())
}
validator.Release()
Executor
The Executor object is the executor for both WASM and compiled-WASM. This object should work base on the Store object. For the details of the Store object, please refer to the next chapter.
- Instantiate and register an - ASTobject as a named- Moduleinstance- As the same of registering host modules or importing WASM modules in - VMobjects, developers can instantiate and register an- ASTobjects into the- Storecontext as a named- Moduleinstance by the- ExecutorAPIs. After the registration, the result- Moduleinstance is exported with the given module name and can be linked when instantiating another module. For the details about the- Moduleinstances APIs, please refer to the Instances.- // ...
 // Assume that the `ast` is the output `*wasmedge.AST` object from the loader
 // and has passed the validation.
 // Assume that the `conf` is the `*wasmedge.Configure` object.
 // Create the statistics object. This step is not necessary if the statistics
 // is not needed.
 stat := wasmedge.NewStatistics()
 // Create the executor object.
 // For executor creation with default configuration and without statistics,
 // you can use `wasmedge.NewExecutor()` instead.
 executor := wasmedge.NewExecutorWithConfigAndStatistics(conf, stat)
 // Create the store object. The store is the WASM runtime structure core.
 store := wasmedge.NewStore()
 // Register the loaded WASM `ast` into store with the export module name "mod".
 mod, res := executor.Register(store, ast, "mod")
 if err != nil {
 fmt.Println("WASM registration FAILED:", err.Error())
 return
 }
 // ...
 // Resources deallocations.
 executor.Release()
 stat.Release()
 store.Release()
 mod.Release()
- Register an existing - Moduleinstance and export the module name- Besides instantiating and registering an - ASTobject, developers can register an existing- Moduleinstance into the store with exporting the module name (which is in the- Moduleinstance already). This case occurs when developers create a- Moduleinstance for the host functions and want to register it for linking. For the details about the construction of host functions in- Moduleinstances, please refer to the Host Functions.- // ...
 // Assume that the `ast` is the output `*wasmedge.AST` object from the loader
 // and has passed the validation.
 // Assume that the `conf` is the `*wasmedge.Configure` object.
 // Create the statistics object. This step is not necessary if the statistics
 // is not needed.
 stat := wasmedge.NewStatistics()
 // Create the executor object.
 // For executor creation with default configuration and without statistics,
 // you can use `wasmedge.NewExecutor()` instead.
 executor := wasmedge.NewExecutorWithConfigAndStatistics(conf, stat)
 // Create the store object. The store is the WASM runtime structure core.
 store := wasmedge.NewStore()
 // Create a module instance for host functions.
 mod := wasmedge.NewModule("mod")
 // ...
 // Create and add the host functions, tables, memories, and globals into the module instance.
 // ...
 // Register the module instance into store with the exported module name.
 // The export module name is in the module instance already.
 res := executor.RegisterImport(store, mod)
 if err != nil {
 fmt.Println("WASM registration FAILED:", err.Error())
 return
 }
 // ...
 // Resources deallocations.
 executor.Release()
 stat.Release()
 store.Release()
 mod.Release()
- Instantiate an - ASTobject to an anonymous- Moduleinstance- WASM or compiled-WASM modules should be instantiated before the function invocation. Before instantiating a WASM module, please check the import section for ensuring the imports are registered into the - Storeobject for linking.- // ...
 // Assume that the `ast` is the output `*wasmedge.AST` object from the loader
 // and has passed the validation.
 // Assume that the `conf` is the `*wasmedge.Configure` object.
 // Create the statistics object. This step is not necessary if the statistics
 // is not needed.
 stat := wasmedge.NewStatistics()
 // Create the executor object.
 // For executor creation with default configuration and without statistics,
 // you can use `wasmedge.NewExecutor()` instead.
 executor := wasmedge.NewExecutorWithConfigAndStatistics(conf, stat)
 // Create the store object. The store is the WASM runtime structure core.
 store := wasmedge.NewStore()
 // Instantiate the WASM module.
 mod, err := executor.Instantiate(stpre, ast)
 if err != nil {
 fmt.Println("WASM instantiation FAILED:", err.Error())
 return
 }
 executor.Release()
 stat.Release()
 store.Release()
 mod.Release()
- Invoke functions - After registering or instantiating and get the result - Moduleinstance, developers can retrieve the exported- Functioninstances from the- Moduleinstance for invocation. For the details about the- Moduleinstances APIs, please refer to the Instances. Please refer to the example above for the- Functioninstance invocation with the- (*Executor).InvokeAPI.
AST Module
The AST object presents the loaded structure from a WASM file or buffer. Developer will get this object after loading a WASM file or buffer from Loader. Before instantiation, developers can also query the imports and exports of an AST object.
ast := ...
// Assume that a WASM is loaded into an `*wasmedge.AST` object from loader.
// List the imports.
imports := ast.ListImports()
for _, import := range imports {
  fmt.Println("Import:", import.GetModuleName(), import.GetExternalName())
}
// List the exports.
exports := ast.ListExports()
for _, export := range exports {
  fmt.Println("Export:", export.GetExternalName())
}
ast.Release()
Store
Store is the runtime structure for the representation of all global state that can be manipulated by WebAssembly programs. The Store object in WasmEdge is an object to provide the instance exporting and importing when instantiating WASM modules. Developers can retrieve the named modules from the Store context.
store := wasmedge.NewStore()
// ...
// Register a WASM module via the executor object.
// ...
// Try to list the registered WASM modules.
modnames := store.ListModule()
// ...
// Find named module by name.
mod := store.FindModule("module")
// If the module with name not found, the `mod` will be `nil`.
store.Release()
Instances
The instances are the runtime structures of WASM. Developers can retrieve the Module instances from the Store contexts, and retrieve the other instances from the Module instances. A single instance can be allocated by its creation function. Developers can construct instances into an Module instance for registration. Please refer to the Host Functions for details. The instances created by their creation functions should be destroyed by developers, EXCEPT they are added into an Module instance.
- Module instance - After instantiating or registering an - ASTobject, developers will get a- Moduleinstance as the result, and have the responsibility to release it when not in use. A- Moduleinstance can also be created for the host module. Please refer to the host function for the details.- Moduleinstance provides APIs to list and find the exported instances in the module.- // ...
 // Instantiate a WASM module via the executor object and get the `mod` as the output module instance.
 // ...
 // List the exported instance of the instantiated WASM module.
 // Take the function instances for example here.
 funcnames := mod.ListFunction()
 // Try to find the exported instance of the instantiated WASM module.
 // Take the function instances for example here.
 funcinst := mod.FindFunction("fib")
 // `funcinst` will be `nil` if the function not found.
 // The returned instance is owned by the module instance and should __NOT__ be released.
- Function instance - Host functions are functions outside WebAssembly and passed to WASM modules as imports. In WasmEdge, developers can create the - Functionobjects for host functions and add them into an- Moduleinstance for registering into a- VMor a- Store. Developers can retrieve the- Function Typefrom the- Functionobjects through the API. For the details of the- Host Functionguide, please refer to the next chapter.- funcobj := ...
 // `funcobj` is the `*wasmedge.Function` retrieved from the module instance.
 functype := funcobj.GetFunctionType()
 // The `funcobj` retrieved from the module instance should __NOT__ be released.
 // The `functype` retrieved from the `funcobj` should __NOT__ be released.
 // For the function object creation, please refer to the `Host Function` guide.
- Table instance - In WasmEdge, developers can create the - Tableobjects and add them into an- wasmedge.Moduleobject for registering into a- VMor a- Store. The- Tableobjects supply APIs to control the data in table instances.- lim := wasmedge.NewLimitWithMax(10, 20)
 // Create the table type with limit and the `FuncRef` element type.
 tabtype := wasmedge.NewTableType(wasmedge.RefType_FuncRef, lim)
 // Create the table instance with table type.
 tabinst := wasmedge.NewTable(tabtype)
 // Delete the table type.
 tabtype.Release()
 gottabtype := tabinst.GetTableType()
 // The `gottabtype` got from table instance is owned by the `tabinst`
 // and should __NOT__ be released.
 reftype := gottabtype.GetRefType()
 // The `reftype` will be `wasmedge.RefType_FuncRef`.
 var gotdata interface{}
 data := wasmedge.NewFuncRef(5)
 err := tabinst.SetData(data, 3)
 // Set the function index 5 to the table[3].
 // The following line will get an "out of bounds table access" error
 // because the position (13) is out of the table size (10):
 // err = tabinst.SetData(data, 13)
 gotdata, err = tabinst.GetData(3)
 // Get the FuncRef value of the table[3].
 // The following line will get an "out of bounds table access" error
 // because the position (13) is out of the table size (10):
 // gotdata, err = tabinst.GetData(13)
 tabsize := tabinst.GetSize()
 // `tabsize` will be 10.
 err = tabinst.Grow(6)
 // Grow the table size of 6, the table size will be 16.
 // The following line will get an "out of bounds table access" error
 // because the size (16 + 6) will reach the table limit (20):
 // err = tabinst.Grow(6)
 tabinst.Release()
- Memory instance - In WasmEdge, developers can create the - Memoryobjects and add them into an- wasmedge.Moduleobject for registering into a- VMor a- Store. The- Memoryobjects supply APIs to control the data in memory instances.- lim := wasmedge.NewLimitWithMax(1, 5)
 // Create the memory type with limit. The memory page size is 64KiB.
 memtype := wasmedge.NewMemoryType(lim)
 // Create the memory instance with memory type.
 meminst := wasmedge.NewMemory(memtype)
 // Delete the memory type.
 memtype.Release()
 data := []byte("A quick brown fox jumps over the lazy dog")
 err := meminst.SetData(data, 0x1000, 10)
 // Set the data[0:9] to the memory[4096:4105].
 // The following line will get an "out of bounds memory access" error
 // because [65535:65544] is out of 1 page size (65536):
 // err = meminst.SetData(data, 0xFFFF, 10)
 var gotdata []byte
 gotdata, err = meminst.GetData(0x1000, 10)
 // Get the memory[4096:4105]. The `gotdata` will be `[]byte("A quick br").
 // The following line will get an "out of bounds memory access" error
 // because [65535:65544] is out of 1 page size (65536):
 // gotdata, err = meminst.Getdata(0xFFFF, 10)
 pagesize := meminst.GetPageSize()
 // `pagesize` will be 1.
 err = meminst.GrowPage(2)
 // Grow the page size of 2, the page size of the memory instance will be 3.
 // The following line will get an "out of bounds memory access" error
 // because the size (3 + 3) will reach the memory limit (5):
 // err = meminst.GetPageSize(3)
 meminst.Release()
- Global instance - In WasmEdge, developers can create the - Globalobjects and add them into an- wasmedge.Moduleobject for registering into a- VMor a- Store. The- Globalobjects supply APIs to control the value in global instances.- // Create the global type with value type and mutation.
 globtype := wasmedge.NewGlobalType(wasmedge.ValType_I64, wasmedge.ValMut_Var)
 // Create the global instance with value and global type.
 globinst := wasmedge.NewGlobal(globtype, uint64(1000))
 // Delete the global type.
 globtype.Release()
 gotglobtype := globinst.GetGlobalType()
 // The `gotglobtype` got from global instance is owned by the `globinst`
 // and should __NOT__ be released.
 valtype := gotglobtype.GetValType()
 // The `valtype` will be `wasmedge.ValType_I64`.
 valmut := gotglobtype.GetMutability()
 // The `valmut` will be `wasmedge.ValMut_Var`.
 globinst.SetValue(uint64(888))
 // Set the value u64(888) to the global.
 // This function will do nothing if the value type mismatched or the
 // global mutability is `wasmedge.ValMut_Const`.
 gotval := globinst.GetValue()
 // The `gotbal` will be `interface{}` which the type is `uint64` and
 // the value is 888.
 globinst.Release()
Host Functions
Host functions are functions outside WebAssembly and passed to WASM modules as imports. In WasmEdge, developers can create the Function, Memory, Table, and Global objects and add them into an wasmedge.Module object for registering into a VM or a Store.
- Host function allocation - Developers can define Go functions with the following function signature as the host function body: - type hostFunctionSignature func(
 data interface{}, mem *Memory, params []interface{}) ([]interface{}, Result)- The example of an - addhost function to add 2- i32values:- func host_add(data interface{}, mem *wasmedge.Memory, params []interface{}) ([]interface{}, wasmedge.Result) {
 // add: i32, i32 -> i32
 res := params[0].(int32) + params[1].(int32)
 // Set the returns
 returns := make([]interface{}, 1)
 returns[0] = res
 // Return
 return returns, wasmedge.Result_Success
 }- Then developers can create - Functionobject with the host function body and function type:- // Create a function type: {i32, i32} -> {i32}.
 functype := wasmedge.NewFunctionType(
 []wasmedge.ValType{wasmedge.ValType_I32, wasmedge.ValType_I32},
 []wasmedge.ValType{wasmedge.ValType_I32},
 )
 // Create a function context with the function type and host function body.
 // The third parameter is the pointer to the additional data.
 // Developers should guarantee the life cycle of the data, and it can be
 // `nil` if the external data is not needed.
 // The last parameter can be 0 if developers do not need the cost measuring.
 func_add := wasmedge.NewFunction(functype, host_add, nil, 0)
 // If the function object is not added into an module instance object, it should be released.
 func_add.Release()
 functype.Release()
- Construct a module instance with host instances - Besides creating a - Moduleinstance by registering or instantiating a WASM module, developers can create a- Moduleinstance with a module name and add the- Function,- Memory,- Table, and- Globalinstances into it with their exporting names.- // Host function body definition.
 func host_add(data interface{}, mem *wasmedge.Memory, params []interface{}) ([]interface{}, wasmedge.Result) {
 // add: i32, i32 -> i32
 res := params[0].(int32) + params[1].(int32)
 // Set the returns
 returns := make([]interface{}, 1)
 returns[0] = res
 // Return
 return returns, wasmedge.Result_Success
 }
 // Create a module instance with the module name "module".
 mod := wasmedge.NewModule("module")
 // Create and add a function instance into the module instance with export name "add".
 functype := wasmedge.NewFunctionType(
 []wasmedge.ValType{wasmedge.ValType_I32, wasmedge.ValType_I32},
 []wasmedge.ValType{wasmedge.ValType_I32},
 )
 hostfunc := wasmedge.NewFunction(functype, host_add, nil, 0)
 functype.Release()
 mod.AddFunction("add", hostfunc)
 // Create and add a table instance into the module instance with export name "table".
 tabtype := wasmedge.NewTableType(wasmedge.RefType_FuncRef ,wasmedge.NewLimitWithMax(10, 20))
 hosttab := wasmedge.NewTable(tabtype)
 tabtype.Release()
 mod.AddTable("table", hosttab)
 // Create and add a memory instance into the module instance with export name "memory".
 memtype := wasmedge.NewMemoryType(wasmedge.NewLimitWithMax(1, 2))
 hostmem := wasmedge.NewMemory(memtype)
 memtype.Release()
 mod.AddMemory("memory", hostmem)
 // Create and add a global instance into the module instance with export name "global".
 globtype := wasmedge.NewGlobalType(wasmedge.ValType_I32, wasmedge.ValMut_Var)
 hostglob := wasmedge.NewGlobal(globtype, uint32(666))
 globtype.Release()
 mod.AddGlobal("global", hostglob)
 // The module instances should be released.
 // Developers should __NOT__ release the instances added into the module instance objects.
 mod.Release()
- Specified module instance - wasmedge.NewWasiModule()API can create and initialize the- WASImodule instance.- wasmedge.NewWasiNNModule()API can create and initialize the- wasi_ephemeral_nnmodule instance for- WASI-NNplugin (- v0.10.1or upper only).- wasmedge.NewWasiCryptoCommonModule()API can create and initialize the- wasi_ephemeral_crypto_commonmodule instance for- WASI-Cryptoplugin (- v0.10.1or upper only).- wasmedge.NewWasiCryptoAsymmetricCommonModule()API can create and initialize the- wasi_ephemeral_crypto_asymmetric_commonmodule instance for- WASI-Cryptoplugin (- v0.10.1or upper only).- wasmedge.NewWasiCryptoKxModule()API can create and initialize the- wasi_ephemeral_crypto_kxmodule instance for- WASI-Cryptoplugin (- v0.10.1or upper only).- wasmedge.NewWasiCryptoSignaturesModule()API can create and initialize the- wasi_ephemeral_crypto_signaturesmodule instance for- WASI-Cryptoplugin (- v0.10.1or upper only).- wasmedge.NewWasiCryptoSymmetricModule()API can create and initialize the- wasi_ephemeral_crypto_symmetricmodule instance for- WASI-Cryptoplugin (- v0.10.1or upper only).- wasmedge.NewWasmEdgeProcessModule()API can create and initialize the- wasmedge_processmodule instance for- wasmedge_processplugin.- Developers can create these module instance objects and register them into the - Storeor- VMobjects rather than adjust the settings in the- Configureobjects.- Note: For the - WASI-NNplugin, please check that the dependencies and prerequests are satisfied. Note: For the- WASI-Cryptoplugin, please check that the dependencies and prerequests are satisfied. And the 5 modules are recommended to all be created and registered together.- wasiobj := wasmedge.NewWasiModule(
 os.Args[1:], // The args
 os.Environ(), // The envs
 []string{".:."}, // The mapping preopens
 )
 procobj := wasmedge.NewWasmEdgeProcessModule(
 []string{"ls", "echo"}, // The allowed commands
 false, // Not to allow all commands
 )
 // Register the WASI and WasmEdge_Process into the VM object.
 vm := wasmedge.NewVM()
 vm.RegisterImport(wasiobj)
 vm.RegisterImport(procobj)
 // ... Execute some WASM functions.
 // Get the WASI exit code.
 exitcode := wasiobj.WasiGetExitCode()
 // The `exitcode` will be 0 if the WASI function "_start" execution has no error.
 // Otherwise, it will return with the related exit code.
 vm.Release()
 // The import objects should be deleted.
 wasiobj.Release()
 procobj.Release()
- Example - Create a new Go project first: - mkdir wasmedge_test && cd wasmedge_test
 go mod init wasmedge_test- Assume that there is a simple WASM from the WAT as following: - (module
 (type $t0 (func (param i32 i32) (result i32)))
 (import "extern" "func-add" (func $f-add (type $t0)))
 (func (export "addTwo") (param i32 i32) (result i32)
 local.get 0
 local.get 1
 call $f-add)
 )- Create and edit the Go file - main.goas following:- package main
 import (
 "fmt"
 "github.com/second-state/WasmEdge-go/wasmedge"
 )
 // Host function body definition.
 func host_add(data interface{}, mem *wasmedge.Memory, params []interface{}) ([]interface{}, wasmedge.Result) {
 // add: i32, i32 -> i32
 res := params[0].(int32) + params[1].(int32)
 // Set the returns
 returns := make([]interface{}, 1)
 returns[0] = res
 // Return
 return returns, wasmedge.Result_Success
 }
 func main() {
 // Create the VM object.
 vm := wasmedge.NewVM()
 // The WASM module buffer.
 wasmbuf := []byte{
 /* WASM header */
 0x00, 0x61, 0x73, 0x6D, 0x01, 0x00, 0x00, 0x00,
 /* Type section */
 0x01, 0x07, 0x01,
 /* function type {i32, i32} -> {i32} */
 0x60, 0x02, 0x7F, 0x7F, 0x01, 0x7F,
 /* Import section */
 0x02, 0x13, 0x01,
 /* module name: "extern" */
 0x06, 0x65, 0x78, 0x74, 0x65, 0x72, 0x6E,
 /* extern name: "func-add" */
 0x08, 0x66, 0x75, 0x6E, 0x63, 0x2D, 0x61, 0x64, 0x64,
 /* import desc: func 0 */
 0x00, 0x00,
 /* Function section */
 0x03, 0x02, 0x01, 0x00,
 /* Export section */
 0x07, 0x0A, 0x01,
 /* export name: "addTwo" */
 0x06, 0x61, 0x64, 0x64, 0x54, 0x77, 0x6F,
 /* export desc: func 0 */
 0x00, 0x01,
 /* Code section */
 0x0A, 0x0A, 0x01,
 /* code body */
 0x08, 0x00, 0x20, 0x00, 0x20, 0x01, 0x10, 0x00, 0x0B,
 }
 // Create the module instance with the module name "extern".
 impmod := wasmedge.NewModule("extern")
 // Create and add a function instance into the module instance with export name "func-add".
 functype := wasmedge.NewFunctionType(
 []wasmedge.ValType{wasmedge.ValType_I32, wasmedge.ValType_I32},
 []wasmedge.ValType{wasmedge.ValType_I32},
 )
 hostfunc := wasmedge.NewFunction(functype, host_add, nil, 0)
 functype.Release()
 impmod.AddFunction("func-add", hostfunc)
 // Register the module instance into VM.
 vm.RegisterImport(impmod)
 res, err := vm.RunWasmBuffer(wasmbuf, "addTwo", uint32(1234), uint32(5678))
 if err == nil {
 fmt.Println("Get the result:", res[0].(int32))
 } else {
 fmt.Println("Error message:", err.Error())
 }
 impmod.Release()
 vm.Release()
 }- Then you can build and run the Golang application with the WasmEdge Golang SDK: - $ go get github.com/second-state/WasmEdge-go/wasmedge@v0.10.1
 $ go build
 $ ./wasmedge_test
 Get the result: 6912
- Host Data Example - Developers can set a external data object to the - Functionobject, and access to the object in the function body. Assume that edit the Go file- main.goabove:- package main
 import (
 "fmt"
 "github.com/second-state/WasmEdge-go/wasmedge"
 )
 // Host function body definition.
 func host_add(data interface{}, mem *wasmedge.Memory, params []interface{}) ([]interface{}, wasmedge.Result) {
 // add: i32, i32 -> i32
 res := params[0].(int32) + params[1].(int32)
 // Set the returns
 returns := make([]interface{}, 1)
 returns[0] = res
 // Also set the result to the data.
 *data.(*int32) = res
 // Return
 return returns, wasmedge.Result_Success
 }
 func main() {
 // Create the VM object.
 vm := wasmedge.NewVM()
 // The WASM module buffer.
 wasmbuf := []byte{
 /* WASM header */
 0x00, 0x61, 0x73, 0x6D, 0x01, 0x00, 0x00, 0x00,
 /* Type section */
 0x01, 0x07, 0x01,
 /* function type {i32, i32} -> {i32} */
 0x60, 0x02, 0x7F, 0x7F, 0x01, 0x7F,
 /* Import section */
 0x02, 0x13, 0x01,
 /* module name: "extern" */
 0x06, 0x65, 0x78, 0x74, 0x65, 0x72, 0x6E,
 /* extern name: "func-add" */
 0x08, 0x66, 0x75, 0x6E, 0x63, 0x2D, 0x61, 0x64, 0x64,
 /* import desc: func 0 */
 0x00, 0x00,
 /* Function section */
 0x03, 0x02, 0x01, 0x00,
 /* Export section */
 0x07, 0x0A, 0x01,
 /* export name: "addTwo" */
 0x06, 0x61, 0x64, 0x64, 0x54, 0x77, 0x6F,
 /* export desc: func 0 */
 0x00, 0x01,
 /* Code section */
 0x0A, 0x0A, 0x01,
 /* code body */
 0x08, 0x00, 0x20, 0x00, 0x20, 0x01, 0x10, 0x00, 0x0B,
 }
 // The additional data to set into the host function.
 var data int32 = 0
 // Create the module instance with the module name "extern".
 modinst := wasmedge.NewModule("extern")
 // Create and add a function instance into the module instance with export name "func-add".
 functype := wasmedge.NewFunctionType(
 []wasmedge.ValType{wasmedge.ValType_I32, wasmedge.ValType_I32},
 []wasmedge.ValType{wasmedge.ValType_I32},
 )
 hostfunc := wasmedge.NewFunction(functype, host_add, &data, 0)
 functype.Release()
 modinst.AddFunction("func-add", hostfunc)
 // Register the module instance into VM.
 vm.RegisterImport(modinst)
 res, err := vm.RunWasmBuffer(wasmbuf, "addTwo", uint32(1234), uint32(5678))
 if err == nil {
 fmt.Println("Get the result:", res[0].(int32))
 } else {
 fmt.Println("Error message:", err.Error())
 }
 fmt.Println("Data value:", data)
 modinst.Release()
 vm.Release()
 }- Then you can build and run the Golang application with the WasmEdge Golang SDK: - $ go get github.com/second-state/WasmEdge-go/wasmedge@v0.10.1
 $ go build
 $ ./wasmedge_test
 Get the result: 6912
 Data value: 6912
WasmEdge AOT Compiler
In this partition, we will introduce the WasmEdge AOT compiler and the options in Go. WasmEdge runs the WASM files in interpreter mode, and WasmEdge also supports the AOT (ahead-of-time) mode running without modifying any code. The WasmEdge AOT (ahead-of-time) compiler compiles the WASM files for running in AOT mode which is much faster than interpreter mode. Developers can compile the WASM files into the compiled-WASM files in shared library format for universal WASM format for the AOT mode execution.
Compilation Example
The go_WasmAOT example provide a tool for compiling a WASM file.
Compiler Options
Developers can set options for AOT compilers such as optimization level and output format:
const (
  // Disable as many optimizations as possible.
  CompilerOptLevel_O0 = CompilerOptimizationLevel(C.WasmEdge_CompilerOptimizationLevel_O0)
  // Optimize quickly without destroying debuggability.
  CompilerOptLevel_O1 = CompilerOptimizationLevel(C.WasmEdge_CompilerOptimizationLevel_O1)
  // Optimize for fast execution as much as possible without triggering significant incremental compile time or code size growth.
  CompilerOptLevel_O2 = CompilerOptimizationLevel(C.WasmEdge_CompilerOptimizationLevel_O2)
  // Optimize for fast execution as much as possible.
  CompilerOptLevel_O3 = CompilerOptimizationLevel(C.WasmEdge_CompilerOptimizationLevel_O3)
  // Optimize for small code size as much as possible without triggering significant incremental compile time or execution time slowdowns.
  CompilerOptLevel_Os = CompilerOptimizationLevel(C.WasmEdge_CompilerOptimizationLevel_Os)
  // Optimize for small code size as much as possible.
  CompilerOptLevel_Oz = CompilerOptimizationLevel(C.WasmEdge_CompilerOptimizationLevel_Oz)
)
const (
  // Native dynamic library format.
  CompilerOutputFormat_Native = CompilerOutputFormat(C.WasmEdge_CompilerOutputFormat_Native)
  // WebAssembly with AOT compiled codes in custom section.
  CompilerOutputFormat_Wasm = CompilerOutputFormat(C.WasmEdge_CompilerOutputFormat_Wasm)
)
Please refer to the AOT compiler options configuration for details.