-
Notifications
You must be signed in to change notification settings - Fork 0
/
path.go
174 lines (170 loc) · 5.12 KB
/
path.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
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
package builtin_libs
import (
"errors"
"io/fs"
"path/filepath"
"time"
"github.com/samber/lo"
. "github.com/zgg-lang/zgg-go/runtime"
)
func libPath(c *Context) ValueObject {
lib := NewObject()
pathSetBridge(lib, c, "abs", filepath.Abs, "path")
pathSetBridge(lib, c, "base", filepath.Base, "path")
pathSetBridge(lib, c, "clean", filepath.Clean, "path")
pathSetBridge(lib, c, "dir", filepath.Dir, "path")
pathSetBridge(lib, c, "evalSymlinks", filepath.EvalSymlinks, "path")
pathSetBridge(lib, c, "ext", filepath.Ext, "path")
pathSetBridge(lib, c, "fromSlash", filepath.FromSlash, "path")
lib.SetMember("glob", NewNativeFunction("path.glob", func(c *Context, _ Value, args []Value) Value {
var pattern ValueStr
EnsureFuncParams(c, "path.glob", args, ArgRuleRequired("pattern", TypeStr, &pattern))
matches, err := filepath.Glob(pattern.Value())
if err != nil {
c.RaiseRuntimeError("glob error %+v", err)
}
r := NewArray(len(matches))
for _, m := range matches {
r.PushBack(NewStr(m))
}
return r
}, "pattern"), nil)
pathSetBridge(lib, c, "isAbs", filepath.IsAbs, "path")
lib.SetMember("join", NewNativeFunction("path.join", func(c *Context, _ Value, args []Value) Value {
elem := make([]string, len(args))
for i, a := range args {
elem[i] = a.ToString(c)
}
joined := filepath.Join(elem...)
return NewStr(joined)
}), nil)
pathSetBridge(lib, c, "match", filepath.Match, "pattern", "name")
pathSetBridge(lib, c, "rel", filepath.Rel, "basepath", "targetpath")
pathSetBridge(lib, c, "toSlash", filepath.ToSlash, "path")
pathSetBridge(lib, c, "volumeName", filepath.VolumeName, "path")
lib.SetMember("walk", NewNativeFunction("path.walk", func(c *Context, _ Value, args []Value) Value {
var (
root ValueStr
cb ValueCallable
)
switch len(args) {
case 1:
EnsureFuncParams(c, "path.walk", args, ArgRuleRequired("root", TypeStr, &root))
ch := make(chan Value)
walking := true
go func(root string) {
defer func() {
walking = false
close(ch)
}()
filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if !walking {
return errors.New("end")
}
item := NewObject()
item.SetMember("path", NewStr(path), nil)
item.SetMember("name", NewStr(d.Name()), nil)
item.SetMember("isDir", NewBool(d.IsDir()), nil)
// 避免死锁,加上send超时
t := time.NewTicker(5 * time.Second)
defer t.Stop()
for walking {
select {
case ch <- item:
return nil
case <-t.C:
}
}
return nil
})
}(root.Value())
return MakeIterator(c,
func() Value {
item, ok := <-ch
return lo.Ternary(ok, item, nil)
},
func() {
walking = false
},
)
default:
EnsureFuncParams(c, "path.walk", args,
ArgRuleRequired("root", TypeStr, &root),
ArgRuleRequired("callback", TypeCallable, &cb),
)
filepath.WalkDir(root.Value(), func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
item := NewObject()
item.SetMember("path", NewStr(path), nil)
item.SetMember("name", NewStr(d.Name()), nil)
item.SetMember("isDir", NewBool(d.IsDir()), nil)
c.Invoke(cb, nil, Args(item))
return nil
})
return Undefined()
}
}, "root"), nil)
return lib
}
func pathSetBridge(lib ValueObject, c *Context, name string, f any, argNames ...string) {
fullname := "path." + name
var nf func(*Context, Value, []Value) Value
switch fv := f.(type) {
case func(string) string:
nf = func(c *Context, _ Value, args []Value) Value {
var a ValueStr
EnsureFuncParams(c, fullname, args, ArgRuleRequired(argNames[0], TypeStr, &a))
return NewStr(fv(a.Value()))
}
case func(string) (string, error):
nf = func(c *Context, _ Value, args []Value) Value {
var a ValueStr
EnsureFuncParams(c, fullname, args, ArgRuleRequired(argNames[0], TypeStr, &a))
r, err := fv(a.Value())
if err != nil {
c.RaiseRuntimeError("%s error %+v", fullname, err)
}
return NewStr(r)
}
case func(string) bool:
nf = func(c *Context, _ Value, args []Value) Value {
var a ValueStr
EnsureFuncParams(c, fullname, args, ArgRuleRequired(argNames[0], TypeStr, &a))
return NewBool(fv(a.Value()))
}
case func(string, string) (bool, error):
nf = func(c *Context, _ Value, args []Value) Value {
var a, b ValueStr
EnsureFuncParams(c, fullname, args,
ArgRuleRequired(argNames[0], TypeStr, &a),
ArgRuleRequired(argNames[1], TypeStr, &b),
)
r, err := fv(a.Value(), b.Value())
if err != nil {
c.RaiseRuntimeError("%s error %+v", fullname, err)
}
return NewBool(r)
}
case func(string, string) (string, error):
nf = func(c *Context, _ Value, args []Value) Value {
var a, b ValueStr
EnsureFuncParams(c, fullname, args,
ArgRuleRequired(argNames[0], TypeStr, &a),
ArgRuleRequired(argNames[1], TypeStr, &b),
)
r, err := fv(a.Value(), b.Value())
if err != nil {
c.RaiseRuntimeError("%s error %+v", fullname, err)
}
return NewStr(r)
}
default:
panic("invalid func type " + fullname)
}
lib.SetMember(name, NewNativeFunction(fullname, nf, argNames...), c)
}