-
Notifications
You must be signed in to change notification settings - Fork 0
/
loop_object.go
127 lines (115 loc) · 3.34 KB
/
loop_object.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package mjingo
import (
"fmt"
"slices"
"github.com/hnakamur/mjingo/internal/rustfmt"
"github.com/hnakamur/mjingo/option"
)
type loopObject struct {
len uint
idx uint
depth uint
valueTriple [3]option.Option[Value]
lastChangedValue []Value
}
var _ = (Object)((*loopObject)(nil))
var _ = (CallMethoder)((*loopObject)(nil))
var _ = (StructObject)((*loopObject)(nil))
var _ = (rustfmt.Formatter)((*loopObject)(nil))
func (l *loopObject) Kind() ObjectKind { return ObjectKindStruct }
func (l *loopObject) CallMethod(_ *State, name string, args []Value) (Value, error) {
switch name {
case "changed":
if slices.Equal(l.lastChangedValue, args) {
return valueFromBool(false), nil
}
l.lastChangedValue = make([]Value, len(args))
for i, arg := range args {
l.lastChangedValue[i] = arg
}
return valueFromBool(true), nil
case "cycle":
idx := l.idx % uint(len(args))
if idx < uint(len(args)) {
return args[idx].clone(), nil
}
return Undefined, nil
}
return Value{}, NewError(UnknownMethod, fmt.Sprintf("loop object has no method named %s", name))
}
func (*loopObject) StaticFields() option.Option[[]string] {
return option.Some([]string{
"index0",
"index",
"length",
"revindex",
"revindex0",
"first",
"last",
"depth",
"depth0",
"previtem",
"nextitem",
})
}
func (*loopObject) Fields() []string { return nil }
func (l *loopObject) GetField(name string) option.Option[Value] {
idx := l.idx
// if we never iterated, then all attributes are undefined.
// this can happen in some rare circumstances where the engine
// did not manage to iterate
if idx == ^uint(0) {
return option.Some[Value](Undefined)
}
switch name {
case "index0":
return option.Some[Value](valueFromI64(int64(idx)))
case "index":
return option.Some[Value](valueFromI64(int64(idx + 1)))
case "length":
return option.Some[Value](valueFromI64(int64(l.len)))
case "revindex":
return option.Some[Value](valueFromI64(int64(uintSaturatingSub(l.len, idx))))
case "revindex0":
return option.Some[Value](valueFromI64(int64(uintSaturatingSub(uintSaturatingSub(l.len, idx), 1))))
case "first":
return option.Some[Value](valueFromBool(idx == 0))
case "last":
return option.Some[Value](valueFromBool(l.len == 0 || idx == l.len-1))
case "depth":
return option.Some[Value](valueFromI64(int64(l.depth + 1)))
case "depth0":
return option.Some[Value](valueFromI64(int64(l.depth)))
case "previtem":
return option.Some[Value](l.valueTriple[0].UnwrapOr(Undefined).clone())
case "nextitem":
return option.Some[Value](l.valueTriple[2].UnwrapOr(Undefined).clone())
}
return option.None[Value]()
}
func (*loopObject) SupportsCustomVerb(verb rune) bool {
return verb == rustfmt.DebugVerb || verb == rustfmt.DisplayVerb
}
func (l *loopObject) Format(f fmt.State, verb rune) {
switch verb {
case rustfmt.DisplayVerb:
fmt.Fprintf(f, "<loop %d/%d>", l.idx, l.len)
case rustfmt.DebugVerb:
s := rustfmt.NewDebugStruct("Loop")
for _, attr := range l.StaticFields().Unwrap() {
s.Field(attr, l.GetField(attr).Unwrap())
}
s.Format(f, verb)
default:
// https://github.com/golang/go/issues/51195#issuecomment-1563538796
type hideMethods loopObject
type loopObject hideMethods
fmt.Fprintf(f, fmt.FormatString(f, verb), loopObject(*l))
}
}
func uintSaturatingSub(x, y uint) uint {
if y > x {
return 0
}
return x - y
}