-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfunction-everywhere.go
More file actions
48 lines (38 loc) · 1.01 KB
/
function-everywhere.go
File metadata and controls
48 lines (38 loc) · 1.01 KB
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
package luajit
import (
"log"
"reflect"
)
type funcEverywhere struct {
v any
index_max int
}
func (L *State) function_prepare_everywhere() {
L.function_everywhere = make(map[reflect.Type]*funcEverywhere)
L.FuncEverywherePass(L, 0)
}
// FuncEverywherePass records TypeOf(v) and will pass it around in
// every Go functions needing one.
func (L *State) FuncEverywherePass(v any, type_in_index_max int) {
var (
typeOf = reflect.TypeOf(v)
)
switch {
case typeOf.Kind() == reflect.Pointer && typeOf.Elem().Kind() == reflect.Struct:
default:
log.Fatalf("(*State).FuncEverywherePass(%T) : only `*struct` accepted")
}
L.function_everywhere[typeOf] = &funcEverywhere{
v: v,
index_max: type_in_index_max,
}
}
func (L *State) funcEverywhereGet(typeOf reflect.Type, type_in_index int, flagMethod bool) (v any) {
if flagMethod {
type_in_index -= 1
}
if everywhere := L.function_everywhere[typeOf]; everywhere != nil && type_in_index <= everywhere.index_max {
return everywhere.v
}
return nil
}