-
Notifications
You must be signed in to change notification settings - Fork 80
/
tabletype.go
105 lines (93 loc) · 2.71 KB
/
tabletype.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package wasmtime
// #include <wasm.h>
import "C"
import "runtime"
// TableType is one of table types which classify tables over elements of element types within a size range.
type TableType struct {
_ptr *C.wasm_tabletype_t
_owner interface{}
}
// NewTableType creates a new `TableType` with the `element` type provided as
// well as limits on its size.
//
// The `min` value is the minimum size, in elements, of this table. The
// `has_max` boolean indicates whether a maximum size is present, and if so
// `max` is used as the maximum size of the table, in elements.
func NewTableType(element *ValType, min uint32, has_max bool, max uint32) *TableType {
valptr := C.wasm_valtype_new(C.wasm_valtype_kind(element.ptr()))
runtime.KeepAlive(element)
if !has_max {
max = 0xffffffff
}
limitsFFI := C.wasm_limits_t{
min: C.uint32_t(min),
max: C.uint32_t(max),
}
ptr := C.wasm_tabletype_new(valptr, &limitsFFI)
return mkTableType(ptr, nil)
}
func mkTableType(ptr *C.wasm_tabletype_t, owner interface{}) *TableType {
tabletype := &TableType{_ptr: ptr, _owner: owner}
if owner == nil {
runtime.SetFinalizer(tabletype, func(tabletype *TableType) {
tabletype.Close()
})
}
return tabletype
}
func (ty *TableType) ptr() *C.wasm_tabletype_t {
ret := ty._ptr
if ret == nil {
panic("object has been closed already")
}
maybeGC()
return ret
}
func (ty *TableType) owner() interface{} {
if ty._owner != nil {
return ty._owner
}
return ty
}
// Close will deallocate this type's state explicitly.
//
// For more information see the documentation for engine.Close()
func (ty *TableType) Close() {
if ty._ptr == nil || ty._owner != nil {
return
}
runtime.SetFinalizer(ty, nil)
C.wasm_tabletype_delete(ty._ptr)
ty._ptr = nil
}
// Element returns the type of value stored in this table
func (ty *TableType) Element() *ValType {
ptr := C.wasm_tabletype_element(ty.ptr())
return mkValType(ptr, ty.owner())
}
// Minimum returns the minimum size, in elements, of this table.
func (ty *TableType) Minimum() uint32 {
ptr := C.wasm_tabletype_limits(ty.ptr())
ret := uint32(ptr.min)
runtime.KeepAlive(ty)
return ret
}
// Maximum returns the maximum size, in elements, of this table.
//
// If no maximum size is listed then `(false, 0)` is returned, otherwise
// `(true, N)` is returned where `N` is the maximum size.
func (ty *TableType) Maximum() (bool, uint32) {
ptr := C.wasm_tabletype_limits(ty.ptr())
ret := uint32(ptr.max)
runtime.KeepAlive(ty)
if ret == 0xffffffff {
return false, 0
} else {
return true, ret
}
}
// AsExternType converts this type to an instance of `ExternType`
func (ty *TableType) AsExternType() *ExternType {
ptr := C.wasm_tabletype_as_externtype_const(ty.ptr())
return mkExternType(ptr, ty.owner())
}