Upgrade to WasmEdge-Go v0.14.0
Due to the WasmEdge-Go API breaking changes, this document shows the guideline of programming with WasmEdge-Go API to upgrade from the v0.13.5 to the v0.14.0 version.
Concepts
wasmedge.ValTypeandwasmedge.RefTypeconst values are replaced by thewasmedge.ValTypestruct.Removed
wasmedge.ValTypeandwasmedge.RefTypeconst values, and introduced thewasmedge.ValTypestruct.- Added the
wasmedge.NewValTypeI32()API to replace thewasmedge.ValType_I32const value. - Added the
wasmedge.NewValTypeI64()API to replace thewasmedge.ValType_I64const value. - Added the
wasmedge.NewValTypeF32()API to replace thewasmedge.ValType_F32const value. - Added the
wasmedge.NewValTypeF64()API to replace thewasmedge.ValType_F64const value. - Added the
wasmedge.NewValTypeV128()API to replace thewasmedge.ValType_V128const value. - Added the
wasmedge.NewValTypeFuncRef()API to replace thewasmedge.ValType_FuncRefconst value. - Added the
wasmedge.NewValTypeExternRef()API to replace thewasmedge.ValType_ExterunRefconst value.
The
wasmedge.ValTypestruct provides the series member functions to check the value type.Besides, the APIs related to the value types are also updated:
wasmedge.NewFunctionType()accepts the new[]*wasmedge.ValTypefor parameters now.(*wasmedge.FunctionType).GetParameters()returns the new[]*wasmedge.ValTypenow.(*wasmedge.FunctionType).GetReturns()returns the new[]*wasmedge.ValTypenow.wasmedge.NewTableType()accepts the new*wasmedge.ValTypeinstead ofwasmedge.RefTypefor parameters now.(*wasmedge.TableType).GetRefType()returns the new*wasmedge.ValTypenow.wasmedge.NewGlobalType()accepts the new*wasmedge.ValTypefor parameters now.(*wasmedge.GlobalType).GetValType()returns the new*wasmedge.ValTypenow.
For the examples of the new
wasmedge.ValTypestruct and affacted APIs, please refer to the example below.- Added the
Developers should handle the error when calling
(*wasmedge.Global).SetValue()API.With the GC and typed-function references proposals, there are new reference types that accepts non-nullable values. Therefore, when setting value into a global instance, the error occurs if developers set a null value into a non-nullable value type global. Developers should detect and handle the error when setting value into a global instance now.
New wasmedge.ValType struct appied for all related APIs
Before the version v0.13.5, developers can use the const value of value types with function type APIs, and reference types with table type APIs:
// Create a function type: {i32, i64, funcref} -> {f32}
functype := wasmedge.NewFunctionType(
[]wasmedge.ValType{
wasmedge.ValType_I32,
wasmedge.ValType_I64,
wasmedge.ValType_FuncRef,
},
[]wasmedge.ValType{
wasmedge.ValType_F32,
})
// Get the parameter types
var ptypes []wasmedge.ValType = functype.GetParameters()
if ptypes[0] == wasmedge.ValType_I32 {
// This will be true here.
// ...
}
functype.Release()
// Create a table type: {min: 1}, externref
lim := wasmedge.NewLimit(1)
tabtype := wasmedge.NewTableType(wasmedge.RefType_ExternRef, lim)
// Get the reference type
if tabtype.GetRefType() == wasmedge.RefType_ExternRef {
// This will be true here.
// ...
}
tabtype.Release()
After v0.14.0, developers should use the wasmedge.ValType related APIs to create the value types.
// Create a function type: {i32, i64, funcref} -> {f32}
functype := wasmedge.NewFunctionType(
[]*wasmedge.ValType{
wasmedge.NewValTypeI32(),
wasmedge.NewValTypeI64(),
wasmedge.NewValTypeFuncRef(),
},
[]*wasmedge.ValType{
wasmedge.NewValTypeF32,
})
// Get the parameter types
var ptypes []*wasmedge.ValType = functype.GetParameters()
if ptypes[0].IsI32() {
// This will be true here.
// ...
}
functype.Release()
// Create a table type: {min: 1}, externref
lim := wasmedge.NewLimit(1)
tabtype := wasmedge.NewTableType(wasmedge.NewValTypeExternRef(), lim)
// Get the reference type
if tabtype.GetRefType().IsExternRef() {
// This will be true here.
// ...
}
tabtype.Release()