-
Notifications
You must be signed in to change notification settings - Fork 79
/
importtype.go
97 lines (84 loc) · 2.52 KB
/
importtype.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
package wasmtime
// #include <wasm.h>
import "C"
import "runtime"
// ImportType is one of the imports component
// A module defines a set of imports that are required for instantiation.
type ImportType struct {
_ptr *C.wasm_importtype_t
_owner interface{}
}
// NewImportType creates a new `ImportType` with the given `module` and `name` and the type
// provided.
func NewImportType(module, name string, ty AsExternType) *ImportType {
moduleVec := stringToByteVec(module)
nameVec := stringToByteVec(name)
// Creating an import type requires taking ownership, so create a copy
// so we don't have to invalidate pointers here. Shouldn't be too
// costly in theory anyway.
extern := ty.AsExternType()
ptr := C.wasm_externtype_copy(extern.ptr())
runtime.KeepAlive(extern)
// And once we've got all that create the import type!
importPtr := C.wasm_importtype_new(&moduleVec, &nameVec, ptr)
return mkImportType(importPtr, nil)
}
func mkImportType(ptr *C.wasm_importtype_t, owner interface{}) *ImportType {
importtype := &ImportType{_ptr: ptr, _owner: owner}
if owner == nil {
runtime.SetFinalizer(importtype, func(importtype *ImportType) {
importtype.Close()
})
}
return importtype
}
func (ty *ImportType) ptr() *C.wasm_importtype_t {
ret := ty._ptr
if ret == nil {
panic("object has been closed already")
}
maybeGC()
return ret
}
func (ty *ImportType) 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 *ImportType) Close() {
if ty._ptr == nil || ty._owner != nil {
return
}
runtime.SetFinalizer(ty, nil)
C.wasm_importtype_delete(ty._ptr)
ty._ptr = nil
}
// Module returns the name in the module this import type is importing
func (ty *ImportType) Module() string {
ptr := C.wasm_importtype_module(ty.ptr())
ret := C.GoStringN(ptr.data, C.int(ptr.size))
runtime.KeepAlive(ty)
return ret
}
// Name returns the name in the module this import type is importing.
//
// Note that the returned string may be `nil` with the module linking proposal
// where this field is optional in the import type.
func (ty *ImportType) Name() *string {
ptr := C.wasm_importtype_name(ty.ptr())
if ptr == nil {
return nil
}
ret := C.GoStringN(ptr.data, C.int(ptr.size))
runtime.KeepAlive(ty)
return &ret
}
// Type returns the type of item this import type expects
func (ty *ImportType) Type() *ExternType {
ptr := C.wasm_importtype_type(ty.ptr())
return mkExternType(ptr, ty.owner())
}