github.com/voedger/voedger@v0.0.0-20240520144910-273e84102129/pkg/exttinygo/internal/impl_state_tinygo.go (about) 1 /* 2 - Copyright (c) 2023-present unTill Software Development Group B.V. 3 @author Michael Saigachenko 4 */ 5 6 package internal 7 8 import ( 9 "unsafe" 10 11 safe "github.com/voedger/voedger/pkg/state/isafestateapi" 12 ) 13 14 const maxUint = ^uint64(0) 15 16 type hostSafeStateAPI struct{} 17 18 func (hostSafeStateAPI) KeyBuilder(storage, entity string) safe.TKeyBuilder { 19 return safe.TKeyBuilder(hostGetKey(uint32(uintptr(unsafe.Pointer(unsafe.StringData(storage)))), uint32(len(storage)), 20 uint32(uintptr(unsafe.Pointer(unsafe.StringData(entity)))), uint32(len(entity)))) 21 } 22 23 func (hostSafeStateAPI) MustGetValue(key safe.TKeyBuilder) safe.TValue { 24 return safe.TValue(hostGetValue(uint64(key))) 25 } 26 27 func (hostSafeStateAPI) QueryValue(key safe.TKeyBuilder) (safe.TValue, bool) { 28 id := hostQueryValue(uint64(key)) 29 if id != maxUint { 30 return safe.TValue(id), true 31 } 32 return safe.TValue(0), false 33 } 34 35 func (hostSafeStateAPI) NewValue(key safe.TKeyBuilder) safe.TIntent { 36 return safe.TIntent(hostNewValue(uint64(key))) 37 } 38 39 func (hostSafeStateAPI) UpdateValue(key safe.TKeyBuilder, existingValue safe.TValue) safe.TIntent { 40 return safe.TIntent(hostUpdateValue(uint64(key), uint64(existingValue))) 41 } 42 43 var CurrentReadCallback func(key safe.TKey, value safe.TValue) 44 45 func (hostSafeStateAPI) ReadValues(key safe.TKeyBuilder, callback func(key safe.TKey, value safe.TValue)) { 46 CurrentReadCallback = callback 47 hostReadValues(uint64(key)) 48 } 49 50 // Key Builder 51 func (hostSafeStateAPI) KeyBuilderPutInt32(key safe.TKeyBuilder, name string, value int32) { 52 hostRowWriterPutInt32(uint64(key), 0, uint32(uintptr(unsafe.Pointer(unsafe.StringData(name)))), uint32(len(name)), uint32(value)) 53 } 54 55 func (hostSafeStateAPI) KeyBuilderPutInt64(key safe.TKeyBuilder, name string, value int64) { 56 hostRowWriterPutInt64(uint64(key), 0, uint32(uintptr(unsafe.Pointer(unsafe.StringData(name)))), uint32(len(name)), uint64(value)) 57 } 58 59 func (hostSafeStateAPI) KeyBuilderPutFloat32(key safe.TKeyBuilder, name string, value float32) { 60 hostRowWriterPutFloat32(uint64(key), 0, uint32(uintptr(unsafe.Pointer(unsafe.StringData(name)))), uint32(len(name)), value) 61 } 62 63 func (hostSafeStateAPI) KeyBuilderPutFloat64(key safe.TKeyBuilder, name string, value float64) { 64 hostRowWriterPutFloat64(uint64(key), 0, uint32(uintptr(unsafe.Pointer(unsafe.StringData(name)))), uint32(len(name)), value) 65 } 66 67 func (hostSafeStateAPI) KeyBuilderPutString(key safe.TKeyBuilder, name string, value string) { 68 hostRowWriterPutString(uint64(key), 0, uint32(uintptr(unsafe.Pointer(unsafe.StringData(name)))), uint32(len(name)), uint32(uintptr(unsafe.Pointer(unsafe.StringData(value)))), uint32(len(value))) 69 } 70 71 func (hostSafeStateAPI) KeyBuilderPutBytes(key safe.TKeyBuilder, name string, value []byte) { 72 hostRowWriterPutBytes(uint64(key), 0, uint32(uintptr(unsafe.Pointer(unsafe.StringData(name)))), uint32(len(name)), uint32(uintptr(unsafe.Pointer(unsafe.SliceData(value)))), uint32(len(value))) 73 } 74 75 func (hostSafeStateAPI) KeyBuilderPutQName(key safe.TKeyBuilder, name string, value safe.QName) { 76 hostRowWriterPutQName(uint64(key), 0, uint32(uintptr(unsafe.Pointer(unsafe.StringData(name)))), uint32(len(name)), 77 uint32(uintptr(unsafe.Pointer(unsafe.StringData(value.FullPkgName)))), uint32(len(value.FullPkgName)), 78 uint32(uintptr(unsafe.Pointer(unsafe.StringData(value.Entity)))), uint32(len(value.Entity))) 79 } 80 81 func (hostSafeStateAPI) KeyBuilderPutBool(key safe.TKeyBuilder, name string, value bool) { 82 var v uint32 83 if value { 84 v = 1 85 } 86 hostRowWriterPutBool(uint64(key), 0, uint32(uintptr(unsafe.Pointer(unsafe.StringData(name)))), uint32(len(name)), v) 87 } 88 89 // Intent 90 91 func (hostSafeStateAPI) IntentPutInt64(i safe.TIntent, name string, value int64) { 92 hostRowWriterPutInt64(uint64(i), 1, uint32(uintptr(unsafe.Pointer(unsafe.StringData(name)))), uint32(len(name)), uint64(value)) 93 } 94 95 func (hostSafeStateAPI) IntentPutInt32(i safe.TIntent, name string, value int32) { 96 hostRowWriterPutInt32(uint64(i), 1, uint32(uintptr(unsafe.Pointer(unsafe.StringData(name)))), uint32(len(name)), uint32(value)) 97 } 98 99 func (hostSafeStateAPI) IntentPutFloat32(i safe.TIntent, name string, value float32) { 100 hostRowWriterPutFloat32(uint64(i), 1, uint32(uintptr(unsafe.Pointer(unsafe.StringData(name)))), uint32(len(name)), value) 101 } 102 103 func (hostSafeStateAPI) IntentPutFloat64(i safe.TIntent, name string, value float64) { 104 hostRowWriterPutFloat64(uint64(i), 1, uint32(uintptr(unsafe.Pointer(unsafe.StringData(name)))), uint32(len(name)), value) 105 } 106 107 func (hostSafeStateAPI) IntentPutString(i safe.TIntent, name string, value string) { 108 hostRowWriterPutString(uint64(i), 1, uint32(uintptr(unsafe.Pointer(unsafe.StringData(name)))), uint32(len(name)), uint32(uintptr(unsafe.Pointer(unsafe.StringData(value)))), uint32(len(value))) 109 } 110 111 func (hostSafeStateAPI) IntentPutBytes(i safe.TIntent, name string, value []byte) { 112 hostRowWriterPutBytes(uint64(i), 1, uint32(uintptr(unsafe.Pointer(unsafe.StringData(name)))), uint32(len(name)), uint32(uintptr(unsafe.Pointer(unsafe.SliceData(value)))), uint32(len(value))) 113 } 114 115 func (hostSafeStateAPI) IntentPutQName(i safe.TIntent, name string, value safe.QName) { 116 hostRowWriterPutQName(uint64(i), 1, 117 uint32(uintptr(unsafe.Pointer(unsafe.StringData(name)))), uint32(len(name)), 118 uint32(uintptr(unsafe.Pointer(unsafe.StringData(value.FullPkgName)))), uint32(len(value.FullPkgName)), 119 uint32(uintptr(unsafe.Pointer(unsafe.StringData(value.Entity)))), uint32(len(value.Entity)), 120 ) 121 } 122 123 func (hostSafeStateAPI) IntentPutBool(i safe.TIntent, name string, value bool) { 124 var v uint32 125 if value { 126 v = 1 127 } 128 hostRowWriterPutBool(uint64(i), 1, uint32(uintptr(unsafe.Pointer(unsafe.StringData(name)))), uint32(len(name)), v) 129 } 130 131 // Value 132 func (hostSafeStateAPI) ValueAsValue(v safe.TValue, name string) safe.TValue { 133 return safe.TValue(hostValueAsValue(uint64(v), uint32(uintptr(unsafe.Pointer(unsafe.StringData(name)))), uint32(len(name)))) 134 } 135 136 func (hostSafeStateAPI) ValueAsInt32(v safe.TValue, name string) int32 { 137 return int32(hostValueAsInt32(uint64(v), uint32(uintptr(unsafe.Pointer(unsafe.StringData(name)))), uint32(len(name)))) 138 } 139 140 func (hostSafeStateAPI) ValueAsInt64(v safe.TValue, name string) int64 { 141 return int64(hostValueAsInt64(uint64(v), uint32(uintptr(unsafe.Pointer(unsafe.StringData(name)))), uint32(len(name)))) 142 } 143 144 func (hostSafeStateAPI) ValueAsFloat32(v safe.TValue, name string) float32 { 145 return hostValueAsFloat32(uint64(v), uint32(uintptr(unsafe.Pointer(unsafe.StringData(name)))), uint32(len(name))) 146 } 147 148 func (hostSafeStateAPI) ValueAsFloat64(v safe.TValue, name string) float64 { 149 return hostValueAsFloat64(uint64(v), uint32(uintptr(unsafe.Pointer(unsafe.StringData(name)))), uint32(len(name))) 150 } 151 152 func (hostSafeStateAPI) ValueAsString(v safe.TValue, name string) string { 153 ptr := hostValueAsString(uint64(v), uint32(uintptr(unsafe.Pointer(unsafe.StringData(name)))), uint32(len(name))) 154 return decodeString(ptr) 155 } 156 157 func (hostSafeStateAPI) ValueAsBytes(v safe.TValue, name string) []byte { 158 ptr := hostValueAsBytes(uint64(v), uint32(uintptr(unsafe.Pointer(unsafe.StringData(name)))), uint32(len(name))) 159 return decodeSlice(ptr) 160 } 161 162 func (hostSafeStateAPI) ValueAsQName(v safe.TValue, name string) safe.QName { 163 pkgPtr := hostValueAsQNamePkg(uint64(v), uint32(uintptr(unsafe.Pointer(unsafe.StringData(name)))), uint32(len(name))) 164 entityPtr := hostValueAsQNameEntity(uint64(v), uint32(uintptr(unsafe.Pointer(unsafe.StringData(name)))), uint32(len(name))) 165 return safe.QName{ 166 FullPkgName: decodeString(pkgPtr), 167 Entity: decodeString(entityPtr), 168 } 169 } 170 171 func (hostSafeStateAPI) ValueAsBool(v safe.TValue, name string) bool { 172 return hostValueAsBool(uint64(v), uint32(uintptr(unsafe.Pointer(unsafe.StringData(name)))), uint32(len(name))) > 0 173 } 174 175 func (hostSafeStateAPI) ValueGetAsBytes(v safe.TValue, index int) []byte { 176 ptr := hostValueGetAsBytes(uint64(v), uint32(index)) 177 return decodeSlice(ptr) 178 } 179 180 func (hostSafeStateAPI) ValueGetAsString(v safe.TValue, index int) string { 181 ptr := hostValueGetAsString(uint64(v), uint32(index)) 182 return decodeString(ptr) 183 } 184 185 func (hostSafeStateAPI) ValueGetAsQName(v safe.TValue, index int) safe.QName { 186 pkgPtr := hostValueGetAsQNamePkg(uint64(v), uint32(index)) 187 entityPtr := hostValueGetAsQNameEntity(uint64(v), uint32(index)) 188 return safe.QName{ 189 FullPkgName: decodeString(pkgPtr), 190 Entity: decodeString(entityPtr), 191 } 192 } 193 194 func (hostSafeStateAPI) ValueGetAsBool(v safe.TValue, index int) bool { 195 return hostValueGetAsBool(uint64(v), uint32(index)) > 0 196 } 197 198 func (hostSafeStateAPI) ValueGetAsInt32(v safe.TValue, index int) int32 { 199 return int32(hostValueGetAsInt32(uint64(v), uint32(index))) 200 } 201 202 func (hostSafeStateAPI) ValueGetAsInt64(v safe.TValue, index int) int64 { 203 return int64(hostValueGetAsInt64(uint64(v), uint32(index))) 204 } 205 206 func (hostSafeStateAPI) ValueGetAsFloat32(v safe.TValue, index int) float32 { 207 return hostValueGetAsFloat32(uint64(v), uint32(index)) 208 } 209 210 func (hostSafeStateAPI) ValueGetAsFloat64(v safe.TValue, index int) float64 { 211 return hostValueGetAsFloat64(uint64(v), uint32(index)) 212 } 213 214 func (hostSafeStateAPI) ValueLen(v safe.TValue) int { 215 return int(hostValueLength(uint64(v))) 216 } 217 218 func (hostSafeStateAPI) ValueGetAsValue(v safe.TValue, index int) safe.TValue { 219 return safe.TValue(hostValueGetAsValue(uint64(v), uint32(index))) 220 } 221 222 // Key 223 func (hostSafeStateAPI) KeyAsInt32(k safe.TKey, name string) int32 { 224 return int32(hostKeyAsInt32(uint64(k), uint32(uintptr(unsafe.Pointer(unsafe.StringData(name)))), uint32(len(name)))) 225 } 226 227 func (hostSafeStateAPI) KeyAsInt64(k safe.TKey, name string) int64 { 228 return int64(hostKeyAsInt64(uint64(k), uint32(uintptr(unsafe.Pointer(unsafe.StringData(name)))), uint32(len(name)))) 229 } 230 231 func (hostSafeStateAPI) KeyAsFloat32(k safe.TKey, name string) float32 { 232 return hostKeyAsFloat32(uint64(k), uint32(uintptr(unsafe.Pointer(unsafe.StringData(name)))), uint32(len(name))) 233 } 234 235 func (hostSafeStateAPI) KeyAsFloat64(k safe.TKey, name string) float64 { 236 return hostKeyAsFloat64(uint64(k), uint32(uintptr(unsafe.Pointer(unsafe.StringData(name)))), uint32(len(name))) 237 } 238 239 func (hostSafeStateAPI) KeyAsBytes(k safe.TKey, name string) []byte { 240 return decodeSlice(hostKeyAsBytes(uint64(k), uint32(uintptr(unsafe.Pointer(unsafe.StringData(name)))), uint32(len(name)))) 241 } 242 243 func (hostSafeStateAPI) KeyAsString(k safe.TKey, name string) string { 244 return decodeString(hostKeyAsString(uint64(k), uint32(uintptr(unsafe.Pointer(unsafe.StringData(name)))), uint32(len(name)))) 245 } 246 247 func (hostSafeStateAPI) KeyAsQName(k safe.TKey, name string) safe.QName { 248 pkgPtr := hostKeyAsQNamePkg(uint64(k), uint32(uintptr(unsafe.Pointer(unsafe.StringData(name)))), uint32(len(name))) 249 entityPtr := hostKeyAsQNameEntity(uint64(k), uint32(uintptr(unsafe.Pointer(unsafe.StringData(name)))), uint32(len(name))) 250 return safe.QName{ 251 FullPkgName: decodeString(pkgPtr), 252 Entity: decodeString(entityPtr), 253 } 254 } 255 256 func (hostSafeStateAPI) KeyAsBool(k safe.TKey, name string) bool { 257 return hostKeyAsBool(uint64(k), uint32(uintptr(unsafe.Pointer(unsafe.StringData(name)))), uint32(len(name))) > 0 258 } 259 260 func decodeSlice(value uint64) []byte { 261 u := uintptr(uint32(value >> 32)) 262 s := uint32(value) 263 return unsafe.Slice((*byte)(unsafe.Pointer(u)), s) 264 } 265 266 func decodeString(value uint64) (ret string) { 267 u := uintptr(uint32(value >> 32)) 268 s := uint32(value) 269 return unsafe.String((*byte)(unsafe.Pointer(u)), s) 270 }