ai_old/js/common.go

150 lines
3.1 KiB
Go
Raw Normal View History

2024-09-20 16:50:35 +08:00
package js
import (
"apigo.cc/ai/ai/goja"
2024-09-20 16:50:35 +08:00
"errors"
"github.com/ssgo/u"
2024-09-23 18:15:02 +08:00
"path/filepath"
2024-09-20 16:50:35 +08:00
)
//func toMap(data any) map[string]any {
// return u.UnJsonMap(u.FixedJson(data))
//}
2024-09-20 16:50:35 +08:00
type Args struct {
This goja.Value
Arguments []goja.Value
VM *goja.Runtime
}
func MakeArgs(args *goja.FunctionCall, vm *goja.Runtime) *Args {
return &Args{
This: args.This,
Arguments: args.Arguments,
VM: vm,
}
}
func (args *Args) Check(num int) *Args {
2024-09-20 16:50:35 +08:00
if len(args.Arguments) < num {
panic(args.VM.NewGoError(errors.New("arguments need " + u.String(num) + ", but given " + u.String(len(args.Arguments)))))
2024-09-20 16:50:35 +08:00
}
return args
2024-09-20 16:50:35 +08:00
}
func (args *Args) Int(index int) int {
if len(args.Arguments) > index {
return u.Int(args.Arguments[index].Export())
}
return 0
}
func (args *Args) Int64(index int) int64 {
if len(args.Arguments) > index {
return u.Int64(args.Arguments[index].Export())
}
return 0
}
2024-09-20 16:50:35 +08:00
func (args *Args) Any(index int) any {
if len(args.Arguments) > index {
return args.Arguments[index].Export()
}
return nil
}
func (args *Args) Str(index int) string {
if len(args.Arguments) > index {
return u.String(args.Arguments[index].Export())
}
return ""
}
func (args *Args) Bytes(index int) []byte {
if len(args.Arguments) > index {
return u.Bytes(args.Arguments[index].Export())
}
return []byte{}
}
func (args *Args) Bool(index int) bool {
if len(args.Arguments) > index {
return u.Bool(args.Arguments[index].Export())
}
return false
}
2024-09-20 16:50:35 +08:00
func (args *Args) Map(index int) map[string]any {
out := map[string]any{}
if len(args.Arguments) > index {
u.Convert(args.Arguments[index].Export(), &out)
}
return out
}
func (args *Args) StrArr(startIndex int) []string {
if len(args.Arguments) > startIndex {
a := make([]string, len(args.Arguments)-startIndex)
for index := startIndex; index < len(args.Arguments); index++ {
a[index-startIndex] = u.String(args.Arguments[index].Export())
}
return a
}
return make([]string, 0)
}
func (args *Args) Arr(startIndex int) []any {
if len(args.Arguments) > startIndex {
a := make([]any, len(args.Arguments)-startIndex)
for index := startIndex; index < len(args.Arguments); index++ {
a[index-startIndex] = u.String(args.Arguments[index].Export())
}
return a
}
return make([]any, 0)
}
2024-09-20 16:50:35 +08:00
func (args *Args) Map2StrArr(index int) []string {
headerMap := args.Map(index)
headers := make([]string, len(headerMap)*2)
i := 0
for k, v := range headerMap {
headers[i] = k
headers[i+1] = u.String(v)
i += 2
}
return headers
}
2024-09-23 18:15:02 +08:00
func (args *Args) Map2Arr(index int) []any {
arrMap := args.Map(index)
arr := make([]any, len(arrMap)*2)
i := 0
for k, v := range arrMap {
arr[i] = k
arr[i+1] = u.String(v)
i += 2
}
return arr
}
2024-09-23 18:15:02 +08:00
func findPath(vm *goja.Runtime, filename string) string {
if u.FileExists(filename) {
return filename
}
if !filepath.IsAbs(filename) {
startPath := u.String(vm.GoData["startPath"])
if startPath != "" {
tryFilename := filepath.Join(startPath, filename)
if u.FileExists(tryFilename) {
return tryFilename
}
}
}
return filename
}
func (args *Args) Path(index int) string {
return findPath(args.VM, args.Str(index))
}