summaryrefslogtreecommitdiff
path: root/vendor/codeberg.org/gruf/go-structr/runtime.go
blob: 8a8d53ede030b9e53a8a452c2d3b769a5fcd1223 (plain)
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
package structr

import (
	"fmt"
	"reflect"
	"runtime"
	"strings"
	"unicode"
	"unicode/utf8"
	"unsafe"

	"codeberg.org/gruf/go-mangler/v2"
	"codeberg.org/gruf/go-xunsafe"
)

// struct_field contains pre-prepared type
// information about a struct's field member,
// including memory offset and hash function.
type struct_field struct {

	// struct field type mangling
	// (i.e. fast serializing) fn.
	mangle mangler.Mangler

	// zero value data, used when
	// nil encountered during ptr
	// offset following.
	zero unsafe.Pointer

	// mangled zero value string,
	// to check zero value keys.
	zerostr string

	// offsets defines whereabouts in
	// memory this field is located,
	// and after how many dereferences.
	offsets []next_offset
}

// next_offset defines a next offset location
// in a struct_field, first by the number of
// derefences required, then by offset from
// that final memory location.
type next_offset struct {
	derefs int
	offset uintptr
}

// get_type_iter returns a prepared xunsafe.TypeIter{} for generic parameter type,
// with flagIndir specifically set as we always take a reference to value type.
func get_type_iter[T any]() xunsafe.TypeIter {
	rtype := reflect.TypeOf((*T)(nil)).Elem()
	flags := xunsafe.Reflect_flag(xunsafe.Abi_Type_Kind(rtype))
	flags |= xunsafe.Reflect_flagIndir // always comes from unsafe ptr
	return xunsafe.ToTypeIter(rtype, flags)
}

// find_field will search for a struct field with given set of names,
// where names is a len > 0 slice of names account for struct nesting.
func find_field(t xunsafe.TypeIter, names []string) (sfield struct_field, ftype reflect.Type) {
	var (
		// is_exported returns whether name is exported
		// from a package; can be func or struct field.
		is_exported = func(name string) bool {
			r, _ := utf8.DecodeRuneInString(name)
			return unicode.IsUpper(r)
		}

		// pop_name pops the next name from
		// the provided slice of field names.
		pop_name = func() string {
			name := names[0]
			names = names[1:]
			if !is_exported(name) {
				panic(fmt.Sprintf("field is not exported: %s", name))
			}
			return name
		}

		// field is the iteratively searched
		// struct field value in below loop.
		field reflect.StructField
	)

	// Take reference
	// of parent iter.
	o := t

	for len(names) > 0 {
		// Pop next name.
		name := pop_name()

		var n int
		rtype := t.Type
		flags := t.Flag

		// Iteratively dereference pointer types.
		for rtype.Kind() == reflect.Pointer {

			// If this actual indirect memory,
			// increase dereferences counter.
			if flags&xunsafe.Reflect_flagIndir != 0 {
				n++
			}

			// Get next elem type.
			rtype = rtype.Elem()

			// Get next set of dereferenced element type flags.
			flags = xunsafe.ReflectPointerElemFlags(flags, rtype)

			// Update type iter info.
			t = t.Child(rtype, flags)
		}

		// Check for valid struct type.
		if rtype.Kind() != reflect.Struct {
			panic(fmt.Sprintf("field %s is not struct (or ptr-to): %s", rtype, name))
		}

		// Set offset info.
		var off next_offset
		off.derefs = n

		var ok bool

		// Look for the next field by name.
		field, ok = rtype.FieldByName(name)
		if !ok {
			panic(fmt.Sprintf("unknown field: %s", name))
		}

		// Set next offset value.
		off.offset = field.Offset
		sfield.offsets = append(sfield.offsets, off)

		// Calculate value flags, and set next nested field type.
		flags = xunsafe.ReflectStructFieldFlags(t.Flag, field.Type)
		t = t.Child(field.Type, flags)
	}

	// Set final field type.
	ftype = t.TypeInfo.Type

	// Get mangler from type info.
	sfield.mangle = mangler.Get(t)

	// Calculate zero value string.
	zptr := zero_value_field(o, sfield.offsets)
	zstr := string(sfield.mangle(nil, zptr))
	sfield.zerostr = zstr
	sfield.zero = zptr

	return
}

// zero_value ...
func zero_value(t xunsafe.TypeIter, offsets []next_offset) reflect.Value {
	v := reflect.New(t.Type).Elem()
	for _, offset := range offsets {
		for range offset.derefs {
			if v.IsNil() {
				new := reflect.New(v.Type().Elem())
				v.Set(new)
			}
			v = v.Elem()
		}
		for i := 0; i < v.NumField(); i++ {
			if v.Type().Field(i).Offset == offset.offset {
				v = v.Field(i)
				break
			}
		}
	}
	return v
}

// zero_value_field ...
func zero_value_field(t xunsafe.TypeIter, offsets []next_offset) unsafe.Pointer {
	return zero_value(t, offsets).Addr().UnsafePointer()
}

// extract_fields extracts given structfields from the provided value type,
// this is done using predetermined struct field memory offset locations.
func extract_fields(ptr unsafe.Pointer, fields []struct_field) []unsafe.Pointer {

	// Prepare slice of field value pointers.
	ptrs := make([]unsafe.Pointer, len(fields))
	if len(ptrs) != len(fields) {
		panic(assert("BCE"))
	}

	for i, field := range fields {
		// loop scope.
		fptr := ptr

		for _, offset := range field.offsets {
			// Dereference any ptrs to offset.
			fptr = deref(fptr, offset.derefs)
			if fptr == nil {
				break
			}

			// Jump forward by offset to next ptr.
			fptr = unsafe.Pointer(uintptr(fptr) +
				offset.offset)
		}

		if fptr == nil {
			// Use zero value.
			fptr = field.zero
		}

		// Set field ptr.
		ptrs[i] = fptr
	}

	return ptrs
}

// pkey_field contains pre-prepared type
// information about a primary key struct's
// field member, including memory offset.
type pkey_field struct {

	// zero value data, used when
	// nil encountered during ptr
	// offset following.
	zero unsafe.Pointer

	// offsets defines whereabouts in
	// memory this field is located.
	offsets []next_offset
}

// extract_pkey will extract a pointer from 'ptr', to
// the primary key struct field defined by 'field'.
func extract_pkey(ptr unsafe.Pointer, field pkey_field) unsafe.Pointer {
	for _, offset := range field.offsets {

		// Dereference any ptrs to offset.
		ptr = deref(ptr, offset.derefs)
		if ptr == nil {
			break
		}

		// Jump forward by offset to next ptr.
		ptr = unsafe.Pointer(uintptr(ptr) +
			offset.offset)
	}

	if ptr == nil {
		// Use zero value.
		ptr = field.zero
	}

	return ptr
}

// deref will dereference ptr 'n' times (or until nil).
func deref(p unsafe.Pointer, n int) unsafe.Pointer {
	for ; n > 0; n-- {
		if p == nil {
			return nil
		}
		p = *(*unsafe.Pointer)(p)
	}
	return p
}

// assert can be called to indicated a block
// of code should not be able to be reached,
// it returns a BUG report with callsite.
func assert(assert string) string {
	pcs := make([]uintptr, 1)
	_ = runtime.Callers(2, pcs)
	funcname := "go-structr" // by default use just our library name
	if frames := runtime.CallersFrames(pcs); frames != nil {
		frame, _ := frames.Next()
		funcname = frame.Function
		if i := strings.LastIndexByte(funcname, '/'); i != -1 {
			funcname = funcname[i+1:]
		}
	}
	var buf strings.Builder
	buf.Grow(32 + len(assert) + len(funcname))
	buf.WriteString("BUG: assertion \"")
	buf.WriteString(assert)
	buf.WriteString("\" failed in ")
	buf.WriteString(funcname)
	return buf.String()
}