From 3afc180451bed0e7f1e46aa4d80c7ec29542490c Mon Sep 17 00:00:00 2001 From: surdeus Date: Fri, 2 Aug 2024 02:55:30 +0500 Subject: [PATCH] forking. --- .gitignore | 5 +- builtins.go | 2 +- builtins_test.go | 4 +- bytecode.go | 4 +- bytecode_test.go | 8 +- cmd/bench/main.go | 4 +- cmd/{tengo => xgo}/main.go | 67 +- compiler.go | 10 +- compiler_test.go | 10 +- doc.go | 2 +- errors.go | 2 +- eval.go | 2 +- eval_test.go | 6 +- example_test.go | 4 +- examples/interoperability/main.go | 2 +- formatter.go | 2 +- go.mod | 4 +- instructions.go | 4 +- iterator.go | 2 +- LICENSE => license.txt | 0 modules.go | 2 +- objects.go | 6 +- objects_test.go | 8 +- parser/ast_test.go | 2 +- parser/expr.go | 2 +- parser/parser.go | 2 +- parser/parser_test.go | 6 +- parser/scanner.go | 2 +- parser/scanner_test.go | 6 +- parser/stmt.go | 2 +- README.md => readme.md | 0 require/require.go | 6 +- script.go | 4 +- script_test.go | 10 +- stdlib/base64.go | 20 +- stdlib/builtin_modules.go | 16 +- stdlib/cjson/dec.go | 108 +++ stdlib/cjson/main.go | 23 + stdlib/errors.go | 8 +- stdlib/fmt.go | 46 +- stdlib/func_typedefs.go | 634 +++++++++--------- stdlib/func_typedefs_test.go | 402 +++++------ stdlib/gensrcmods.go | 4 +- stdlib/hex.go | 8 +- stdlib/html/element.go | 136 ++++ stdlib/html/html.go | 101 +++ stdlib/http/http.go | 2 + stdlib/http/module.go | 70 ++ stdlib/http/request.go | 49 ++ stdlib/http/response.go | 49 ++ stdlib/json.go | 84 +-- stdlib/json/decode.go | 32 +- stdlib/json/encode.go | 30 +- stdlib/json/json_test.go | 12 +- stdlib/log/main.go | 76 +++ stdlib/math.go | 160 ++--- stdlib/os.go | 322 ++++----- stdlib/os_exec.go | 62 +- stdlib/os_file.go | 60 +- stdlib/os_process.go | 42 +- stdlib/os_test.go | 40 +- stdlib/paths/main.go | 46 ++ stdlib/rand.go | 74 +- stdlib/rand_test.go | 12 +- stdlib/source_modules.go | 8 +- stdlib/srcmod_enum.tengo | 127 ---- stdlib/srcmod_enum.xgo | 179 +++++ stdlib/stdlib.go | 6 +- stdlib/stdlib_test.go | 66 +- stdlib/text.go | 446 ++++++------ stdlib/text_regexp.go | 116 ++-- stdlib/text_test.go | 20 +- stdlib/times.go | 608 ++++++++--------- stdlib/times_test.go | 12 +- stdlib/url/url.go | 77 +++ symbol_table.go | 2 +- symbol_table_test.go | 6 +- tengo.go | 4 +- tengo_test.go | 8 +- testdata/cli/{one.tengo => one.xgo} | 0 testdata/cli/{test.tengo => testo.xgo} | 0 testdata/cli/{three.tengo => three.xgo} | 0 .../cli/two/five/{five.tengo => five.xgo} | 0 .../cli/two/four/{four.tengo => four.xgo} | 0 testdata/cli/two/{two.tengo => two.xgo} | 0 testdata/libs/array.tengo | 22 + testdata/libs/cjson.xgo | 20 + testdata/libs/data.cjson | 4 + testdata/libs/dogs.xgo | 22 + testdata/libs/html.xgo | 27 + testdata/libs/http.xgo | 19 + testdata/libs/import.xgo | 7 + testdata/libs/paths.xgo | 8 + variable.go | 2 +- variable_test.go | 6 +- vm.go | 6 +- vm_test.go | 12 +- 97 files changed, 2853 insertions(+), 1917 deletions(-) rename cmd/{tengo => xgo}/main.go (79%) rename LICENSE => license.txt (100%) rename README.md => readme.md (100%) create mode 100644 stdlib/cjson/dec.go create mode 100644 stdlib/cjson/main.go create mode 100644 stdlib/html/element.go create mode 100644 stdlib/html/html.go create mode 100644 stdlib/http/http.go create mode 100644 stdlib/http/module.go create mode 100644 stdlib/http/request.go create mode 100644 stdlib/http/response.go create mode 100644 stdlib/log/main.go create mode 100644 stdlib/paths/main.go delete mode 100644 stdlib/srcmod_enum.tengo create mode 100644 stdlib/srcmod_enum.xgo create mode 100644 stdlib/url/url.go rename testdata/cli/{one.tengo => one.xgo} (100%) rename testdata/cli/{test.tengo => testo.xgo} (100%) rename testdata/cli/{three.tengo => three.xgo} (100%) rename testdata/cli/two/five/{five.tengo => five.xgo} (100%) rename testdata/cli/two/four/{four.tengo => four.xgo} (100%) rename testdata/cli/two/{two.tengo => two.xgo} (100%) create mode 100644 testdata/libs/array.tengo create mode 100644 testdata/libs/cjson.xgo create mode 100644 testdata/libs/data.cjson create mode 100644 testdata/libs/dogs.xgo create mode 100644 testdata/libs/html.xgo create mode 100644 testdata/libs/http.xgo create mode 100644 testdata/libs/import.xgo create mode 100644 testdata/libs/paths.xgo diff --git a/.gitignore b/.gitignore index 37eb85e..0193b7e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +/bin +/xgo dist/ - -.idea \ No newline at end of file +.idea diff --git a/builtins.go b/builtins.go index b954d07..5e04904 100644 --- a/builtins.go +++ b/builtins.go @@ -1,4 +1,4 @@ -package tengo +package xgo var builtinFuncs = []*BuiltinFunction{ { diff --git a/builtins_test.go b/builtins_test.go index eca2d5b..087a09f 100644 --- a/builtins_test.go +++ b/builtins_test.go @@ -1,11 +1,11 @@ -package tengo_test +package xgo_test import ( "errors" "reflect" "testing" - "github.com/d5/tengo/v2" + "surdeus.su/core/xgo/v2" ) func Test_builtinDelete(t *testing.T) { diff --git a/bytecode.go b/bytecode.go index f3049ce..789ce67 100644 --- a/bytecode.go +++ b/bytecode.go @@ -1,4 +1,4 @@ -package tengo +package xgo import ( "encoding/gob" @@ -6,7 +6,7 @@ import ( "io" "reflect" - "github.com/d5/tengo/v2/parser" + "surdeus.su/core/xgo/v2/parser" ) // Bytecode is a compiled instructions and constants. diff --git a/bytecode_test.go b/bytecode_test.go index 7d1a500..cfba41c 100644 --- a/bytecode_test.go +++ b/bytecode_test.go @@ -1,13 +1,13 @@ -package tengo_test +package xgo_test import ( "bytes" "testing" "time" - "github.com/d5/tengo/v2" - "github.com/d5/tengo/v2/parser" - "github.com/d5/tengo/v2/require" + "surdeus.su/core/xgo/v2" + "surdeus.su/core/xgo/v2/parser" + "surdeus.su/core/xgo/v2/require" ) type srcfile struct { diff --git a/cmd/bench/main.go b/cmd/bench/main.go index 0371d87..6aa7bf4 100644 --- a/cmd/bench/main.go +++ b/cmd/bench/main.go @@ -4,8 +4,8 @@ import ( "fmt" "time" - "github.com/d5/tengo/v2" - "github.com/d5/tengo/v2/parser" + "surdeus.su/core/xgo/v2" + "surdeus.su/core/xgo/v2/parser" ) func main() { diff --git a/cmd/tengo/main.go b/cmd/xgo/main.go similarity index 79% rename from cmd/tengo/main.go rename to cmd/xgo/main.go index a8e1498..9672e44 100644 --- a/cmd/tengo/main.go +++ b/cmd/xgo/main.go @@ -6,18 +6,17 @@ import ( "flag" "fmt" "io" - "io/ioutil" "os" "path/filepath" "strings" - "github.com/d5/tengo/v2" - "github.com/d5/tengo/v2/parser" - "github.com/d5/tengo/v2/stdlib" + "surdeus.su/core/xgo/v2" + "surdeus.su/core/xgo/v2/parser" + "surdeus.su/core/xgo/v2/stdlib" ) const ( - sourceFileExt = ".tengo" + sourceFileExt = ".xgo" replPrompt = ">> " ) @@ -55,7 +54,7 @@ func main() { return } - inputData, err := ioutil.ReadFile(inputFile) + inputData, err := os.ReadFile(inputFile) if err != nil { _, _ = fmt.Fprintf(os.Stderr, "Error reading input file: %s\n", err.Error()) @@ -96,7 +95,7 @@ func main() { // CompileOnly compiles the source code and writes the compiled binary into // outputFile. func CompileOnly( - modules *tengo.ModuleMap, + modules *xgo.ModuleMap, data []byte, inputFile, outputFile string, ) (err error) { @@ -131,7 +130,7 @@ func CompileOnly( // CompileAndRun compiles the source code and executes it. func CompileAndRun( - modules *tengo.ModuleMap, + modules *xgo.ModuleMap, data []byte, inputFile string, ) (err error) { @@ -140,45 +139,45 @@ func CompileAndRun( return } - machine := tengo.NewVM(bytecode, nil, -1) + machine := xgo.NewVM(bytecode, nil, -1) err = machine.Run() return } // RunCompiled reads the compiled binary from file and executes it. -func RunCompiled(modules *tengo.ModuleMap, data []byte) (err error) { - bytecode := &tengo.Bytecode{} +func RunCompiled(modules *xgo.ModuleMap, data []byte) (err error) { + bytecode := &xgo.Bytecode{} err = bytecode.Decode(bytes.NewReader(data), modules) if err != nil { return } - machine := tengo.NewVM(bytecode, nil, -1) + machine := xgo.NewVM(bytecode, nil, -1) err = machine.Run() return } // RunREPL starts REPL. -func RunREPL(modules *tengo.ModuleMap, in io.Reader, out io.Writer) { +func RunREPL(modules *xgo.ModuleMap, in io.Reader, out io.Writer) { stdin := bufio.NewScanner(in) fileSet := parser.NewFileSet() - globals := make([]tengo.Object, tengo.GlobalsSize) - symbolTable := tengo.NewSymbolTable() - for idx, fn := range tengo.GetAllBuiltinFunctions() { + globals := make([]xgo.Object, xgo.GlobalsSize) + symbolTable := xgo.NewSymbolTable() + for idx, fn := range xgo.GetAllBuiltinFunctions() { symbolTable.DefineBuiltin(idx, fn.Name) } // embed println function symbol := symbolTable.Define("__repl_println__") - globals[symbol.Index] = &tengo.UserFunction{ + globals[symbol.Index] = &xgo.UserFunction{ Name: "println", - Value: func(args ...tengo.Object) (ret tengo.Object, err error) { + Value: func(args ...xgo.Object) (ret xgo.Object, err error) { var printArgs []interface{} for _, arg := range args { - if _, isUndefined := arg.(*tengo.Undefined); isUndefined { + if _, isUndefined := arg.(*xgo.Undefined); isUndefined { printArgs = append(printArgs, "") } else { - s, _ := tengo.ToString(arg) + s, _ := xgo.ToString(arg) printArgs = append(printArgs, s) } } @@ -188,7 +187,7 @@ func RunREPL(modules *tengo.ModuleMap, in io.Reader, out io.Writer) { }, } - var constants []tengo.Object + var constants []xgo.Object for { _, _ = fmt.Fprint(out, replPrompt) scanned := stdin.Scan() @@ -206,14 +205,14 @@ func RunREPL(modules *tengo.ModuleMap, in io.Reader, out io.Writer) { } file = addPrints(file) - c := tengo.NewCompiler(srcFile, symbolTable, constants, modules, nil) + c := xgo.NewCompiler(srcFile, symbolTable, constants, modules, nil) if err := c.Compile(file); err != nil { _, _ = fmt.Fprintln(out, err.Error()) continue } bytecode := c.Bytecode() - machine := tengo.NewVM(bytecode, globals, -1) + machine := xgo.NewVM(bytecode, globals, -1) if err := machine.Run(); err != nil { _, _ = fmt.Fprintln(out, err.Error()) continue @@ -223,10 +222,10 @@ func RunREPL(modules *tengo.ModuleMap, in io.Reader, out io.Writer) { } func compileSrc( - modules *tengo.ModuleMap, + modules *xgo.ModuleMap, src []byte, inputFile string, -) (*tengo.Bytecode, error) { +) (*xgo.Bytecode, error) { fileSet := parser.NewFileSet() srcFile := fileSet.AddFile(filepath.Base(inputFile), -1, len(src)) @@ -236,7 +235,7 @@ func compileSrc( return nil, err } - c := tengo.NewCompiler(srcFile, nil, nil, modules, nil) + c := xgo.NewCompiler(srcFile, nil, nil, modules, nil) c.EnableFileImport(true) if resolvePath { c.SetImportDir(filepath.Dir(inputFile)) @@ -254,7 +253,7 @@ func compileSrc( func doHelp() { fmt.Println("Usage:") fmt.Println() - fmt.Println(" tengo [flags] {input-file}") + fmt.Println(" xgo [flags] {input-file}") fmt.Println() fmt.Println("Flags:") fmt.Println() @@ -263,20 +262,20 @@ func doHelp() { fmt.Println() fmt.Println("Examples:") fmt.Println() - fmt.Println(" tengo") + fmt.Println(" xgo") fmt.Println() fmt.Println(" Start Tengo REPL") fmt.Println() - fmt.Println(" tengo myapp.tengo") + fmt.Println(" xgo myapp.xgo") fmt.Println() - fmt.Println(" Compile and run source file (myapp.tengo)") - fmt.Println(" Source file must have .tengo extension") + fmt.Println(" Compile and run source file (myapp.xgo)") + fmt.Println(" Source file must have .xgo extension") fmt.Println() - fmt.Println(" tengo -o myapp myapp.tengo") + fmt.Println(" xgo -o myapp myapp.xgo") fmt.Println() - fmt.Println(" Compile source file (myapp.tengo) into bytecode file (myapp)") + fmt.Println(" Compile source file (myapp.xgo) into bytecode file (myapp)") fmt.Println() - fmt.Println(" tengo myapp") + fmt.Println(" xgo myapp") fmt.Println() fmt.Println(" Run bytecode file (myapp)") fmt.Println() diff --git a/compiler.go b/compiler.go index f5fc553..3927956 100644 --- a/compiler.go +++ b/compiler.go @@ -1,4 +1,4 @@ -package tengo +package xgo import ( "errors" @@ -10,8 +10,8 @@ import ( "reflect" "strings" - "github.com/d5/tengo/v2/parser" - "github.com/d5/tengo/v2/token" + "surdeus.su/core/xgo/v2/parser" + "surdeus.su/core/xgo/v2/token" ) // compilationScope represents a compiled instructions and the last two @@ -631,8 +631,8 @@ func (c *Compiler) SetImportDir(dir string) { // // Use this method if you want other source file extension than ".tengo". // -// // this will search for *.tengo, *.foo, *.bar -// err := c.SetImportFileExt(".tengo", ".foo", ".bar") +// // this will search for *.tengo, *.foo, *.bar +// err := c.SetImportFileExt(".tengo", ".foo", ".bar") // // This function requires at least one argument, since it will replace the // current list of extension name. diff --git a/compiler_test.go b/compiler_test.go index 5caf2b0..650f817 100644 --- a/compiler_test.go +++ b/compiler_test.go @@ -1,4 +1,4 @@ -package tengo_test +package xgo_test import ( "fmt" @@ -7,10 +7,10 @@ import ( "strings" "testing" - "github.com/d5/tengo/v2" - "github.com/d5/tengo/v2/parser" - "github.com/d5/tengo/v2/require" - "github.com/d5/tengo/v2/stdlib" + "surdeus.su/core/xgo/v2" + "surdeus.su/core/xgo/v2/parser" + "surdeus.su/core/xgo/v2/require" + "surdeus.su/core/xgo/v2/stdlib" ) func TestCompiler_Compile(t *testing.T) { diff --git a/doc.go b/doc.go index 05b47de..3c4d418 100644 --- a/doc.go +++ b/doc.go @@ -1,3 +1,3 @@ // tengo is a small, dynamic, fast, secure script language for Go. -package tengo +package xgo diff --git a/errors.go b/errors.go index 8ef610a..dd7af12 100644 --- a/errors.go +++ b/errors.go @@ -1,4 +1,4 @@ -package tengo +package xgo import ( "errors" diff --git a/eval.go b/eval.go index 9ce9b2a..b549e16 100644 --- a/eval.go +++ b/eval.go @@ -1,4 +1,4 @@ -package tengo +package xgo import ( "context" diff --git a/eval_test.go b/eval_test.go index ac9dc28..e5bad99 100644 --- a/eval_test.go +++ b/eval_test.go @@ -1,11 +1,11 @@ -package tengo_test +package xgo_test import ( "context" "testing" - "github.com/d5/tengo/v2" - "github.com/d5/tengo/v2/require" + "surdeus.su/core/xgo/v2" + "surdeus.su/core/xgo/v2/require" ) func TestEval(t *testing.T) { diff --git a/example_test.go b/example_test.go index 5f074d8..77ffa1f 100644 --- a/example_test.go +++ b/example_test.go @@ -1,10 +1,10 @@ -package tengo_test +package xgo_test import ( "context" "fmt" - "github.com/d5/tengo/v2" + "surdeus.su/core/xgo/v2" ) func Example() { diff --git a/examples/interoperability/main.go b/examples/interoperability/main.go index b62e2bf..4576664 100644 --- a/examples/interoperability/main.go +++ b/examples/interoperability/main.go @@ -11,7 +11,7 @@ import ( "sync" "time" - "github.com/d5/tengo/v2" + "surdeus.su/core/xgo/v2" ) // CallArgs holds function name to be executed and its required parameters with diff --git a/formatter.go b/formatter.go index 0dbf71c..7871bae 100644 --- a/formatter.go +++ b/formatter.go @@ -1,4 +1,4 @@ -package tengo +package xgo import ( "strconv" diff --git a/go.mod b/go.mod index 732ff14..8d6fc52 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,3 @@ -module github.com/d5/tengo/v2 +module surdeus.su/core/xgo/v2 -go 1.13 +go 1.18 diff --git a/instructions.go b/instructions.go index 27f7af3..44aa0f8 100644 --- a/instructions.go +++ b/instructions.go @@ -1,9 +1,9 @@ -package tengo +package xgo import ( "fmt" - "github.com/d5/tengo/v2/parser" + "surdeus.su/core/xgo/v2/parser" ) // MakeInstruction returns a bytecode for an opcode and the operands. diff --git a/iterator.go b/iterator.go index 13adbba..872502e 100644 --- a/iterator.go +++ b/iterator.go @@ -1,4 +1,4 @@ -package tengo +package xgo // Iterator represents an iterator for underlying data type. type Iterator interface { diff --git a/LICENSE b/license.txt similarity index 100% rename from LICENSE rename to license.txt diff --git a/modules.go b/modules.go index dadd5a3..ad7a0f6 100644 --- a/modules.go +++ b/modules.go @@ -1,4 +1,4 @@ -package tengo +package xgo // Importable interface represents importable module instance. type Importable interface { diff --git a/objects.go b/objects.go index 2574558..980fa1c 100644 --- a/objects.go +++ b/objects.go @@ -1,4 +1,4 @@ -package tengo +package xgo import ( "bytes" @@ -8,8 +8,8 @@ import ( "strings" "time" - "github.com/d5/tengo/v2/parser" - "github.com/d5/tengo/v2/token" + "surdeus.su/core/xgo/v2/parser" + "surdeus.su/core/xgo/v2/token" ) var ( diff --git a/objects_test.go b/objects_test.go index 29a0b7d..6f1dc1e 100644 --- a/objects_test.go +++ b/objects_test.go @@ -1,11 +1,11 @@ -package tengo_test +package xgo_test import ( "testing" - "github.com/d5/tengo/v2" - "github.com/d5/tengo/v2/require" - "github.com/d5/tengo/v2/token" + "surdeus.su/core/xgo/v2" + "surdeus.su/core/xgo/v2/require" + "surdeus.su/core/xgo/v2/token" ) func TestObject_TypeName(t *testing.T) { diff --git a/parser/ast_test.go b/parser/ast_test.go index eb0eb93..e73dcb7 100644 --- a/parser/ast_test.go +++ b/parser/ast_test.go @@ -3,7 +3,7 @@ package parser_test import ( "testing" - "github.com/d5/tengo/v2/parser" + "surdeus.su/core/xgo/v2/parser" ) func TestIdentListString(t *testing.T) { diff --git a/parser/expr.go b/parser/expr.go index ea4b8b1..60dcbed 100644 --- a/parser/expr.go +++ b/parser/expr.go @@ -3,7 +3,7 @@ package parser import ( "strings" - "github.com/d5/tengo/v2/token" + "surdeus.su/core/xgo/v2/token" ) // Expr represents an expression node in the AST. diff --git a/parser/parser.go b/parser/parser.go index a12af61..1f7627f 100644 --- a/parser/parser.go +++ b/parser/parser.go @@ -6,7 +6,7 @@ import ( "sort" "strconv" - "github.com/d5/tengo/v2/token" + "surdeus.su/core/xgo/v2/token" ) type bailout struct{} diff --git a/parser/parser_test.go b/parser/parser_test.go index b0aaa87..707779f 100644 --- a/parser/parser_test.go +++ b/parser/parser_test.go @@ -8,9 +8,9 @@ import ( "strings" "testing" - . "github.com/d5/tengo/v2/parser" - "github.com/d5/tengo/v2/require" - "github.com/d5/tengo/v2/token" + . "surdeus.su/core/xgo/v2/parser" + "surdeus.su/core/xgo/v2/require" + "surdeus.su/core/xgo/v2/token" ) func TestParserError(t *testing.T) { diff --git a/parser/scanner.go b/parser/scanner.go index 5f79770..3b825ee 100644 --- a/parser/scanner.go +++ b/parser/scanner.go @@ -5,7 +5,7 @@ import ( "unicode" "unicode/utf8" - "github.com/d5/tengo/v2/token" + "surdeus.su/core/xgo/v2/token" ) // byte order mark diff --git a/parser/scanner_test.go b/parser/scanner_test.go index 1f6ca56..5d1c4e7 100644 --- a/parser/scanner_test.go +++ b/parser/scanner_test.go @@ -7,9 +7,9 @@ import ( "testing" "time" - "github.com/d5/tengo/v2/parser" - "github.com/d5/tengo/v2/require" - "github.com/d5/tengo/v2/token" + "surdeus.su/core/xgo/v2/parser" + "surdeus.su/core/xgo/v2/require" + "surdeus.su/core/xgo/v2/token" ) var testFileSet = parser.NewFileSet() diff --git a/parser/stmt.go b/parser/stmt.go index c0848c4..4051e49 100644 --- a/parser/stmt.go +++ b/parser/stmt.go @@ -3,7 +3,7 @@ package parser import ( "strings" - "github.com/d5/tengo/v2/token" + "surdeus.su/core/xgo/v2/token" ) // Stmt represents a statement in the AST. diff --git a/README.md b/readme.md similarity index 100% rename from README.md rename to readme.md diff --git a/require/require.go b/require/require.go index 99c1a7c..953458a 100644 --- a/require/require.go +++ b/require/require.go @@ -10,9 +10,9 @@ import ( "unicode" "unicode/utf8" - "github.com/d5/tengo/v2" - "github.com/d5/tengo/v2/parser" - "github.com/d5/tengo/v2/token" + "surdeus.su/core/xgo/v2" + "surdeus.su/core/xgo/v2/parser" + "surdeus.su/core/xgo/v2/token" ) // NoError asserts err is not an error. diff --git a/script.go b/script.go index 16f2944..a19883e 100644 --- a/script.go +++ b/script.go @@ -1,4 +1,4 @@ -package tengo +package xgo import ( "context" @@ -6,7 +6,7 @@ import ( "path/filepath" "sync" - "github.com/d5/tengo/v2/parser" + "surdeus.su/core/xgo/v2/parser" ) // Script can simplify compilation and execution of embedded scripts. diff --git a/script_test.go b/script_test.go index a84941c..ce67a62 100644 --- a/script_test.go +++ b/script_test.go @@ -1,4 +1,4 @@ -package tengo_test +package xgo_test import ( "context" @@ -11,10 +11,10 @@ import ( "testing" "time" - "github.com/d5/tengo/v2" - "github.com/d5/tengo/v2/require" - "github.com/d5/tengo/v2/stdlib" - "github.com/d5/tengo/v2/token" + "surdeus.su/core/xgo/v2" + "surdeus.su/core/xgo/v2/require" + "surdeus.su/core/xgo/v2/stdlib" + "surdeus.su/core/xgo/v2/token" ) func TestScript_Add(t *testing.T) { diff --git a/stdlib/base64.go b/stdlib/base64.go index b4c5b56..cedd073 100644 --- a/stdlib/base64.go +++ b/stdlib/base64.go @@ -3,32 +3,32 @@ package stdlib import ( "encoding/base64" - "github.com/d5/tengo/v2" + "surdeus.su/core/xgo/v2" ) -var base64Module = map[string]tengo.Object{ - "encode": &tengo.UserFunction{ +var base64Module = map[string]xgo.Object{ + "encode": &xgo.UserFunction{ Value: FuncAYRS(base64.StdEncoding.EncodeToString), }, - "decode": &tengo.UserFunction{ + "decode": &xgo.UserFunction{ Value: FuncASRYE(base64.StdEncoding.DecodeString), }, - "raw_encode": &tengo.UserFunction{ + "raw_encode": &xgo.UserFunction{ Value: FuncAYRS(base64.RawStdEncoding.EncodeToString), }, - "raw_decode": &tengo.UserFunction{ + "raw_decode": &xgo.UserFunction{ Value: FuncASRYE(base64.RawStdEncoding.DecodeString), }, - "url_encode": &tengo.UserFunction{ + "url_encode": &xgo.UserFunction{ Value: FuncAYRS(base64.URLEncoding.EncodeToString), }, - "url_decode": &tengo.UserFunction{ + "url_decode": &xgo.UserFunction{ Value: FuncASRYE(base64.URLEncoding.DecodeString), }, - "raw_url_encode": &tengo.UserFunction{ + "raw_url_encode": &xgo.UserFunction{ Value: FuncAYRS(base64.RawURLEncoding.EncodeToString), }, - "raw_url_decode": &tengo.UserFunction{ + "raw_url_decode": &xgo.UserFunction{ Value: FuncASRYE(base64.RawURLEncoding.DecodeString), }, } diff --git a/stdlib/builtin_modules.go b/stdlib/builtin_modules.go index cf0e962..8baa75c 100644 --- a/stdlib/builtin_modules.go +++ b/stdlib/builtin_modules.go @@ -1,11 +1,16 @@ package stdlib import ( - "github.com/d5/tengo/v2" + "surdeus.su/core/xgo/v2" + "surdeus.su/core/xgo/v2/stdlib/http" + "surdeus.su/core/xgo/v2/stdlib/paths" + "surdeus.su/core/xgo/v2/stdlib/html" + "surdeus.su/core/xgo/v2/stdlib/log" + "surdeus.su/core/xgo/v2/stdlib/cjson" ) // BuiltinModules are builtin type standard library modules. -var BuiltinModules = map[string]map[string]tengo.Object{ +var BuiltinModules = map[string]map[string]xgo.Object{ "math": mathModule, "os": osModule, "text": textModule, @@ -15,4 +20,11 @@ var BuiltinModules = map[string]map[string]tengo.Object{ "json": jsonModule, "base64": base64Module, "hex": hexModule, + // Implement later. + "url": nil, + "http": http.Module, + "paths": paths.Module, + "html": html.Module, + "log": log.Module, + "cjson": cjson.Module, } diff --git a/stdlib/cjson/dec.go b/stdlib/cjson/dec.go new file mode 100644 index 0000000..a619e0d --- /dev/null +++ b/stdlib/cjson/dec.go @@ -0,0 +1,108 @@ +package cjson + +import "surdeus.su/core/xgo/v2" +import tjson "surdeus.su/core/xgo/v2/stdlib/json" + +import "encoding/json" +import "io" +import "errors" +import "os" + +type Decoder struct { + xgo.ObjectImpl + dec *json.Decoder + end bool + inputName string +} + +func (d *Decoder) TypeName() string { + return "cjson.Decoder" +} + +func (d *Decoder) String() string { + return "cjson.Decoder{...}" +} + +func NewDecoder(args ...xgo.Object) (xgo.Object, error) { + var inputName string + if len(args) == 0 { + inputName = StrStdin + } else if len(args) > 1 { + return nil, xgo.ErrWrongNumArguments + } else { + var ok bool + inputObject := args[0] + inputName, ok = xgo.ToString(inputObject) + if !ok { + return nil, xgo.ErrInvalidArgumentType{ + Name: "first", + Expected: "stringer", + Found: inputObject.TypeName(), + } + } + } + + var reader io.Reader + var err error + switch { + case inputName == StrStdin: + reader = os.Stdin + default: + reader, err = os.Open(inputName) + if err != nil { + return nil, err + } + } + + ret := &Decoder{} + ret.dec = json.NewDecoder(reader) + + return ret, nil +} + +func (d *Decoder) IndexGet( + index xgo.Object, +) (xgo.Object, error) { + key, ok := xgo.ToString(index) + if !ok { + return nil, xgo.ErrInvalidIndexValueType + } + + switch key { + case "decode": + return &xgo.UserFunction{ + Name: "decode", + Value: d.Decode, + }, nil + } + + // Nothing found. + return nil, nil +} + +func (d *Decoder) Decode( + args ...xgo.Object, +) (xgo.Object, error) { + if len(args) > 0 { + return nil, xgo.ErrWrongNumArguments + } + if d.end { + return nil, nil + } + + var v any + err := d.dec.Decode(&v) + if err != nil { + if errors.Is(err, io.EOF) { + d.end = true + } else { + return nil, err + } + } + + bts, err := json.Marshal(v) + if err != nil { + return nil, err + } + return tjson.Decode(bts) +} diff --git a/stdlib/cjson/main.go b/stdlib/cjson/main.go new file mode 100644 index 0000000..c573fb4 --- /dev/null +++ b/stdlib/cjson/main.go @@ -0,0 +1,23 @@ +package cjson + +import "surdeus.su/core/xgo/v2" +import "io" +import "encoding/json" + +//import "github.com/d5/xgo/v2/stdlib" + +const ( + StrStdin = "" +) + +var Module = map[string]xgo.Object{ + "Decoder": &xgo.BuiltinFunction{ + Name: "Decoder", + Value: NewDecoder, + }, +} + +type Encoder struct { + enc *json.Encoder + output io.Writer +} diff --git a/stdlib/errors.go b/stdlib/errors.go index ad83c6f..7c48e15 100644 --- a/stdlib/errors.go +++ b/stdlib/errors.go @@ -1,12 +1,12 @@ package stdlib import ( - "github.com/d5/tengo/v2" + "surdeus.su/core/xgo/v2" ) -func wrapError(err error) tengo.Object { +func wrapError(err error) xgo.Object { if err == nil { - return tengo.TrueValue + return xgo.TrueValue } - return &tengo.Error{Value: &tengo.String{Value: err.Error()}} + return &xgo.Error{Value: &xgo.String{Value: err.Error()}} } diff --git a/stdlib/fmt.go b/stdlib/fmt.go index 9945277..ea93c3a 100644 --- a/stdlib/fmt.go +++ b/stdlib/fmt.go @@ -3,17 +3,17 @@ package stdlib import ( "fmt" - "github.com/d5/tengo/v2" + "surdeus.su/core/xgo/v2" ) -var fmtModule = map[string]tengo.Object{ - "print": &tengo.UserFunction{Name: "print", Value: fmtPrint}, - "printf": &tengo.UserFunction{Name: "printf", Value: fmtPrintf}, - "println": &tengo.UserFunction{Name: "println", Value: fmtPrintln}, - "sprintf": &tengo.UserFunction{Name: "sprintf", Value: fmtSprintf}, +var fmtModule = map[string]xgo.Object{ + "print": &xgo.UserFunction{Name: "print", Value: fmtPrint}, + "printf": &xgo.UserFunction{Name: "printf", Value: fmtPrintf}, + "println": &xgo.UserFunction{Name: "println", Value: fmtPrintln}, + "sprintf": &xgo.UserFunction{Name: "sprintf", Value: fmtSprintf}, } -func fmtPrint(args ...tengo.Object) (ret tengo.Object, err error) { +func fmtPrint(args ...xgo.Object) (ret xgo.Object, err error) { printArgs, err := getPrintArgs(args...) if err != nil { return nil, err @@ -22,15 +22,15 @@ func fmtPrint(args ...tengo.Object) (ret tengo.Object, err error) { return nil, nil } -func fmtPrintf(args ...tengo.Object) (ret tengo.Object, err error) { +func fmtPrintf(args ...xgo.Object) (ret xgo.Object, err error) { numArgs := len(args) if numArgs == 0 { - return nil, tengo.ErrWrongNumArguments + return nil, xgo.ErrWrongNumArguments } - format, ok := args[0].(*tengo.String) + format, ok := args[0].(*xgo.String) if !ok { - return nil, tengo.ErrInvalidArgumentType{ + return nil, xgo.ErrInvalidArgumentType{ Name: "format", Expected: "string", Found: args[0].TypeName(), @@ -41,7 +41,7 @@ func fmtPrintf(args ...tengo.Object) (ret tengo.Object, err error) { return nil, nil } - s, err := tengo.Format(format.Value, args[1:]...) + s, err := xgo.Format(format.Value, args[1:]...) if err != nil { return nil, err } @@ -49,7 +49,7 @@ func fmtPrintf(args ...tengo.Object) (ret tengo.Object, err error) { return nil, nil } -func fmtPrintln(args ...tengo.Object) (ret tengo.Object, err error) { +func fmtPrintln(args ...xgo.Object) (ret xgo.Object, err error) { printArgs, err := getPrintArgs(args...) if err != nil { return nil, err @@ -59,15 +59,15 @@ func fmtPrintln(args ...tengo.Object) (ret tengo.Object, err error) { return nil, nil } -func fmtSprintf(args ...tengo.Object) (ret tengo.Object, err error) { +func fmtSprintf(args ...xgo.Object) (ret xgo.Object, err error) { numArgs := len(args) if numArgs == 0 { - return nil, tengo.ErrWrongNumArguments + return nil, xgo.ErrWrongNumArguments } - format, ok := args[0].(*tengo.String) + format, ok := args[0].(*xgo.String) if !ok { - return nil, tengo.ErrInvalidArgumentType{ + return nil, xgo.ErrInvalidArgumentType{ Name: "format", Expected: "string", Found: args[0].TypeName(), @@ -77,22 +77,22 @@ func fmtSprintf(args ...tengo.Object) (ret tengo.Object, err error) { // okay to return 'format' directly as String is immutable return format, nil } - s, err := tengo.Format(format.Value, args[1:]...) + s, err := xgo.Format(format.Value, args[1:]...) if err != nil { return nil, err } - return &tengo.String{Value: s}, nil + return &xgo.String{Value: s}, nil } -func getPrintArgs(args ...tengo.Object) ([]interface{}, error) { +func getPrintArgs(args ...xgo.Object) ([]interface{}, error) { var printArgs []interface{} l := 0 for _, arg := range args { - s, _ := tengo.ToString(arg) + s, _ := xgo.ToString(arg) slen := len(s) // make sure length does not exceed the limit - if l+slen > tengo.MaxStringLen { - return nil, tengo.ErrStringLimit + if l+slen > xgo.MaxStringLen { + return nil, xgo.ErrStringLimit } l += slen printArgs = append(printArgs, s) diff --git a/stdlib/func_typedefs.go b/stdlib/func_typedefs.go index fdac933..ec59700 100644 --- a/stdlib/func_typedefs.go +++ b/stdlib/func_typedefs.go @@ -3,103 +3,103 @@ package stdlib import ( "fmt" - "github.com/d5/tengo/v2" + "surdeus.su/core/xgo/v2" ) // FuncAR transform a function of 'func()' signature into CallableFunc type. -func FuncAR(fn func()) tengo.CallableFunc { - return func(args ...tengo.Object) (ret tengo.Object, err error) { +func FuncAR(fn func()) xgo.CallableFunc { + return func(args ...xgo.Object) (ret xgo.Object, err error) { if len(args) != 0 { - return nil, tengo.ErrWrongNumArguments + return nil, xgo.ErrWrongNumArguments } fn() - return tengo.UndefinedValue, nil + return xgo.UndefinedValue, nil } } // FuncARI transform a function of 'func() int' signature into CallableFunc // type. -func FuncARI(fn func() int) tengo.CallableFunc { - return func(args ...tengo.Object) (ret tengo.Object, err error) { +func FuncARI(fn func() int) xgo.CallableFunc { + return func(args ...xgo.Object) (ret xgo.Object, err error) { if len(args) != 0 { - return nil, tengo.ErrWrongNumArguments + return nil, xgo.ErrWrongNumArguments } - return &tengo.Int{Value: int64(fn())}, nil + return &xgo.Int{Value: int64(fn())}, nil } } // FuncARI64 transform a function of 'func() int64' signature into CallableFunc // type. -func FuncARI64(fn func() int64) tengo.CallableFunc { - return func(args ...tengo.Object) (ret tengo.Object, err error) { +func FuncARI64(fn func() int64) xgo.CallableFunc { + return func(args ...xgo.Object) (ret xgo.Object, err error) { if len(args) != 0 { - return nil, tengo.ErrWrongNumArguments + return nil, xgo.ErrWrongNumArguments } - return &tengo.Int{Value: fn()}, nil + return &xgo.Int{Value: fn()}, nil } } // FuncAI64RI64 transform a function of 'func(int64) int64' signature into // CallableFunc type. -func FuncAI64RI64(fn func(int64) int64) tengo.CallableFunc { - return func(args ...tengo.Object) (ret tengo.Object, err error) { +func FuncAI64RI64(fn func(int64) int64) xgo.CallableFunc { + return func(args ...xgo.Object) (ret xgo.Object, err error) { if len(args) != 1 { - return nil, tengo.ErrWrongNumArguments + return nil, xgo.ErrWrongNumArguments } - i1, ok := tengo.ToInt64(args[0]) + i1, ok := xgo.ToInt64(args[0]) if !ok { - return nil, tengo.ErrInvalidArgumentType{ + return nil, xgo.ErrInvalidArgumentType{ Name: "first", Expected: "int(compatible)", Found: args[0].TypeName(), } } - return &tengo.Int{Value: fn(i1)}, nil + return &xgo.Int{Value: fn(i1)}, nil } } // FuncAI64R transform a function of 'func(int64)' signature into CallableFunc // type. -func FuncAI64R(fn func(int64)) tengo.CallableFunc { - return func(args ...tengo.Object) (ret tengo.Object, err error) { +func FuncAI64R(fn func(int64)) xgo.CallableFunc { + return func(args ...xgo.Object) (ret xgo.Object, err error) { if len(args) != 1 { - return nil, tengo.ErrWrongNumArguments + return nil, xgo.ErrWrongNumArguments } - i1, ok := tengo.ToInt64(args[0]) + i1, ok := xgo.ToInt64(args[0]) if !ok { - return nil, tengo.ErrInvalidArgumentType{ + return nil, xgo.ErrInvalidArgumentType{ Name: "first", Expected: "int(compatible)", Found: args[0].TypeName(), } } fn(i1) - return tengo.UndefinedValue, nil + return xgo.UndefinedValue, nil } } // FuncARB transform a function of 'func() bool' signature into CallableFunc // type. -func FuncARB(fn func() bool) tengo.CallableFunc { - return func(args ...tengo.Object) (ret tengo.Object, err error) { +func FuncARB(fn func() bool) xgo.CallableFunc { + return func(args ...xgo.Object) (ret xgo.Object, err error) { if len(args) != 0 { - return nil, tengo.ErrWrongNumArguments + return nil, xgo.ErrWrongNumArguments } if fn() { - return tengo.TrueValue, nil + return xgo.TrueValue, nil } - return tengo.FalseValue, nil + return xgo.FalseValue, nil } } // FuncARE transform a function of 'func() error' signature into CallableFunc // type. -func FuncARE(fn func() error) tengo.CallableFunc { - return func(args ...tengo.Object) (ret tengo.Object, err error) { +func FuncARE(fn func() error) xgo.CallableFunc { + return func(args ...xgo.Object) (ret xgo.Object, err error) { if len(args) != 0 { - return nil, tengo.ErrWrongNumArguments + return nil, xgo.ErrWrongNumArguments } return wrapError(fn()), nil } @@ -107,79 +107,79 @@ func FuncARE(fn func() error) tengo.CallableFunc { // FuncARS transform a function of 'func() string' signature into CallableFunc // type. -func FuncARS(fn func() string) tengo.CallableFunc { - return func(args ...tengo.Object) (ret tengo.Object, err error) { +func FuncARS(fn func() string) xgo.CallableFunc { + return func(args ...xgo.Object) (ret xgo.Object, err error) { if len(args) != 0 { - return nil, tengo.ErrWrongNumArguments + return nil, xgo.ErrWrongNumArguments } s := fn() - if len(s) > tengo.MaxStringLen { - return nil, tengo.ErrStringLimit + if len(s) > xgo.MaxStringLen { + return nil, xgo.ErrStringLimit } - return &tengo.String{Value: s}, nil + return &xgo.String{Value: s}, nil } } // FuncARSE transform a function of 'func() (string, error)' signature into // CallableFunc type. -func FuncARSE(fn func() (string, error)) tengo.CallableFunc { - return func(args ...tengo.Object) (ret tengo.Object, err error) { +func FuncARSE(fn func() (string, error)) xgo.CallableFunc { + return func(args ...xgo.Object) (ret xgo.Object, err error) { if len(args) != 0 { - return nil, tengo.ErrWrongNumArguments + return nil, xgo.ErrWrongNumArguments } res, err := fn() if err != nil { return wrapError(err), nil } - if len(res) > tengo.MaxStringLen { - return nil, tengo.ErrStringLimit + if len(res) > xgo.MaxStringLen { + return nil, xgo.ErrStringLimit } - return &tengo.String{Value: res}, nil + return &xgo.String{Value: res}, nil } } // FuncARYE transform a function of 'func() ([]byte, error)' signature into // CallableFunc type. -func FuncARYE(fn func() ([]byte, error)) tengo.CallableFunc { - return func(args ...tengo.Object) (ret tengo.Object, err error) { +func FuncARYE(fn func() ([]byte, error)) xgo.CallableFunc { + return func(args ...xgo.Object) (ret xgo.Object, err error) { if len(args) != 0 { - return nil, tengo.ErrWrongNumArguments + return nil, xgo.ErrWrongNumArguments } res, err := fn() if err != nil { return wrapError(err), nil } - if len(res) > tengo.MaxBytesLen { - return nil, tengo.ErrBytesLimit + if len(res) > xgo.MaxBytesLen { + return nil, xgo.ErrBytesLimit } - return &tengo.Bytes{Value: res}, nil + return &xgo.Bytes{Value: res}, nil } } // FuncARF transform a function of 'func() float64' signature into CallableFunc // type. -func FuncARF(fn func() float64) tengo.CallableFunc { - return func(args ...tengo.Object) (ret tengo.Object, err error) { +func FuncARF(fn func() float64) xgo.CallableFunc { + return func(args ...xgo.Object) (ret xgo.Object, err error) { if len(args) != 0 { - return nil, tengo.ErrWrongNumArguments + return nil, xgo.ErrWrongNumArguments } - return &tengo.Float{Value: fn()}, nil + return &xgo.Float{Value: fn()}, nil } } // FuncARSs transform a function of 'func() []string' signature into // CallableFunc type. -func FuncARSs(fn func() []string) tengo.CallableFunc { - return func(args ...tengo.Object) (ret tengo.Object, err error) { +func FuncARSs(fn func() []string) xgo.CallableFunc { + return func(args ...xgo.Object) (ret xgo.Object, err error) { if len(args) != 0 { - return nil, tengo.ErrWrongNumArguments + return nil, xgo.ErrWrongNumArguments } - arr := &tengo.Array{} + arr := &xgo.Array{} for _, elem := range fn() { - if len(elem) > tengo.MaxStringLen { - return nil, tengo.ErrStringLimit + if len(elem) > xgo.MaxStringLen { + return nil, xgo.ErrStringLimit } - arr.Value = append(arr.Value, &tengo.String{Value: elem}) + arr.Value = append(arr.Value, &xgo.String{Value: elem}) } return arr, nil } @@ -187,18 +187,18 @@ func FuncARSs(fn func() []string) tengo.CallableFunc { // FuncARIsE transform a function of 'func() ([]int, error)' signature into // CallableFunc type. -func FuncARIsE(fn func() ([]int, error)) tengo.CallableFunc { - return func(args ...tengo.Object) (ret tengo.Object, err error) { +func FuncARIsE(fn func() ([]int, error)) xgo.CallableFunc { + return func(args ...xgo.Object) (ret xgo.Object, err error) { if len(args) != 0 { - return nil, tengo.ErrWrongNumArguments + return nil, xgo.ErrWrongNumArguments } res, err := fn() if err != nil { return wrapError(err), nil } - arr := &tengo.Array{} + arr := &xgo.Array{} for _, v := range res { - arr.Value = append(arr.Value, &tengo.Int{Value: int64(v)}) + arr.Value = append(arr.Value, &xgo.Int{Value: int64(v)}) } return arr, nil } @@ -206,23 +206,23 @@ func FuncARIsE(fn func() ([]int, error)) tengo.CallableFunc { // FuncAIRIs transform a function of 'func(int) []int' signature into // CallableFunc type. -func FuncAIRIs(fn func(int) []int) tengo.CallableFunc { - return func(args ...tengo.Object) (ret tengo.Object, err error) { +func FuncAIRIs(fn func(int) []int) xgo.CallableFunc { + return func(args ...xgo.Object) (ret xgo.Object, err error) { if len(args) != 1 { - return nil, tengo.ErrWrongNumArguments + return nil, xgo.ErrWrongNumArguments } - i1, ok := tengo.ToInt(args[0]) + i1, ok := xgo.ToInt(args[0]) if !ok { - return nil, tengo.ErrInvalidArgumentType{ + return nil, xgo.ErrInvalidArgumentType{ Name: "first", Expected: "int(compatible)", Found: args[0].TypeName(), } } res := fn(i1) - arr := &tengo.Array{} + arr := &xgo.Array{} for _, v := range res { - arr.Value = append(arr.Value, &tengo.Int{Value: int64(v)}) + arr.Value = append(arr.Value, &xgo.Int{Value: int64(v)}) } return arr, nil } @@ -230,259 +230,259 @@ func FuncAIRIs(fn func(int) []int) tengo.CallableFunc { // FuncAFRF transform a function of 'func(float64) float64' signature into // CallableFunc type. -func FuncAFRF(fn func(float64) float64) tengo.CallableFunc { - return func(args ...tengo.Object) (ret tengo.Object, err error) { +func FuncAFRF(fn func(float64) float64) xgo.CallableFunc { + return func(args ...xgo.Object) (ret xgo.Object, err error) { if len(args) != 1 { - return nil, tengo.ErrWrongNumArguments + return nil, xgo.ErrWrongNumArguments } - f1, ok := tengo.ToFloat64(args[0]) + f1, ok := xgo.ToFloat64(args[0]) if !ok { - return nil, tengo.ErrInvalidArgumentType{ + return nil, xgo.ErrInvalidArgumentType{ Name: "first", Expected: "float(compatible)", Found: args[0].TypeName(), } } - return &tengo.Float{Value: fn(f1)}, nil + return &xgo.Float{Value: fn(f1)}, nil } } // FuncAIR transform a function of 'func(int)' signature into CallableFunc type. -func FuncAIR(fn func(int)) tengo.CallableFunc { - return func(args ...tengo.Object) (ret tengo.Object, err error) { +func FuncAIR(fn func(int)) xgo.CallableFunc { + return func(args ...xgo.Object) (ret xgo.Object, err error) { if len(args) != 1 { - return nil, tengo.ErrWrongNumArguments + return nil, xgo.ErrWrongNumArguments } - i1, ok := tengo.ToInt(args[0]) + i1, ok := xgo.ToInt(args[0]) if !ok { - return nil, tengo.ErrInvalidArgumentType{ + return nil, xgo.ErrInvalidArgumentType{ Name: "first", Expected: "int(compatible)", Found: args[0].TypeName(), } } fn(i1) - return tengo.UndefinedValue, nil + return xgo.UndefinedValue, nil } } // FuncAIRF transform a function of 'func(int) float64' signature into // CallableFunc type. -func FuncAIRF(fn func(int) float64) tengo.CallableFunc { - return func(args ...tengo.Object) (ret tengo.Object, err error) { +func FuncAIRF(fn func(int) float64) xgo.CallableFunc { + return func(args ...xgo.Object) (ret xgo.Object, err error) { if len(args) != 1 { - return nil, tengo.ErrWrongNumArguments + return nil, xgo.ErrWrongNumArguments } - i1, ok := tengo.ToInt(args[0]) + i1, ok := xgo.ToInt(args[0]) if !ok { - return nil, tengo.ErrInvalidArgumentType{ + return nil, xgo.ErrInvalidArgumentType{ Name: "first", Expected: "int(compatible)", Found: args[0].TypeName(), } } - return &tengo.Float{Value: fn(i1)}, nil + return &xgo.Float{Value: fn(i1)}, nil } } // FuncAFRI transform a function of 'func(float64) int' signature into // CallableFunc type. -func FuncAFRI(fn func(float64) int) tengo.CallableFunc { - return func(args ...tengo.Object) (ret tengo.Object, err error) { +func FuncAFRI(fn func(float64) int) xgo.CallableFunc { + return func(args ...xgo.Object) (ret xgo.Object, err error) { if len(args) != 1 { - return nil, tengo.ErrWrongNumArguments + return nil, xgo.ErrWrongNumArguments } - f1, ok := tengo.ToFloat64(args[0]) + f1, ok := xgo.ToFloat64(args[0]) if !ok { - return nil, tengo.ErrInvalidArgumentType{ + return nil, xgo.ErrInvalidArgumentType{ Name: "first", Expected: "float(compatible)", Found: args[0].TypeName(), } } - return &tengo.Int{Value: int64(fn(f1))}, nil + return &xgo.Int{Value: int64(fn(f1))}, nil } } // FuncAFFRF transform a function of 'func(float64, float64) float64' signature // into CallableFunc type. -func FuncAFFRF(fn func(float64, float64) float64) tengo.CallableFunc { - return func(args ...tengo.Object) (ret tengo.Object, err error) { +func FuncAFFRF(fn func(float64, float64) float64) xgo.CallableFunc { + return func(args ...xgo.Object) (ret xgo.Object, err error) { if len(args) != 2 { - return nil, tengo.ErrWrongNumArguments + return nil, xgo.ErrWrongNumArguments } - f1, ok := tengo.ToFloat64(args[0]) + f1, ok := xgo.ToFloat64(args[0]) if !ok { - return nil, tengo.ErrInvalidArgumentType{ + return nil, xgo.ErrInvalidArgumentType{ Name: "first", Expected: "float(compatible)", Found: args[0].TypeName(), } } - f2, ok := tengo.ToFloat64(args[1]) + f2, ok := xgo.ToFloat64(args[1]) if !ok { - return nil, tengo.ErrInvalidArgumentType{ + return nil, xgo.ErrInvalidArgumentType{ Name: "second", Expected: "float(compatible)", Found: args[1].TypeName(), } } - return &tengo.Float{Value: fn(f1, f2)}, nil + return &xgo.Float{Value: fn(f1, f2)}, nil } } // FuncAIFRF transform a function of 'func(int, float64) float64' signature // into CallableFunc type. -func FuncAIFRF(fn func(int, float64) float64) tengo.CallableFunc { - return func(args ...tengo.Object) (ret tengo.Object, err error) { +func FuncAIFRF(fn func(int, float64) float64) xgo.CallableFunc { + return func(args ...xgo.Object) (ret xgo.Object, err error) { if len(args) != 2 { - return nil, tengo.ErrWrongNumArguments + return nil, xgo.ErrWrongNumArguments } - i1, ok := tengo.ToInt(args[0]) + i1, ok := xgo.ToInt(args[0]) if !ok { - return nil, tengo.ErrInvalidArgumentType{ + return nil, xgo.ErrInvalidArgumentType{ Name: "first", Expected: "int(compatible)", Found: args[0].TypeName(), } } - f2, ok := tengo.ToFloat64(args[1]) + f2, ok := xgo.ToFloat64(args[1]) if !ok { - return nil, tengo.ErrInvalidArgumentType{ + return nil, xgo.ErrInvalidArgumentType{ Name: "second", Expected: "float(compatible)", Found: args[1].TypeName(), } } - return &tengo.Float{Value: fn(i1, f2)}, nil + return &xgo.Float{Value: fn(i1, f2)}, nil } } // FuncAFIRF transform a function of 'func(float64, int) float64' signature // into CallableFunc type. -func FuncAFIRF(fn func(float64, int) float64) tengo.CallableFunc { - return func(args ...tengo.Object) (ret tengo.Object, err error) { +func FuncAFIRF(fn func(float64, int) float64) xgo.CallableFunc { + return func(args ...xgo.Object) (ret xgo.Object, err error) { if len(args) != 2 { - return nil, tengo.ErrWrongNumArguments + return nil, xgo.ErrWrongNumArguments } - f1, ok := tengo.ToFloat64(args[0]) + f1, ok := xgo.ToFloat64(args[0]) if !ok { - return nil, tengo.ErrInvalidArgumentType{ + return nil, xgo.ErrInvalidArgumentType{ Name: "first", Expected: "float(compatible)", Found: args[0].TypeName(), } } - i2, ok := tengo.ToInt(args[1]) + i2, ok := xgo.ToInt(args[1]) if !ok { - return nil, tengo.ErrInvalidArgumentType{ + return nil, xgo.ErrInvalidArgumentType{ Name: "second", Expected: "int(compatible)", Found: args[1].TypeName(), } } - return &tengo.Float{Value: fn(f1, i2)}, nil + return &xgo.Float{Value: fn(f1, i2)}, nil } } // FuncAFIRB transform a function of 'func(float64, int) bool' signature // into CallableFunc type. -func FuncAFIRB(fn func(float64, int) bool) tengo.CallableFunc { - return func(args ...tengo.Object) (ret tengo.Object, err error) { +func FuncAFIRB(fn func(float64, int) bool) xgo.CallableFunc { + return func(args ...xgo.Object) (ret xgo.Object, err error) { if len(args) != 2 { - return nil, tengo.ErrWrongNumArguments + return nil, xgo.ErrWrongNumArguments } - f1, ok := tengo.ToFloat64(args[0]) + f1, ok := xgo.ToFloat64(args[0]) if !ok { - return nil, tengo.ErrInvalidArgumentType{ + return nil, xgo.ErrInvalidArgumentType{ Name: "first", Expected: "float(compatible)", Found: args[0].TypeName(), } } - i2, ok := tengo.ToInt(args[1]) + i2, ok := xgo.ToInt(args[1]) if !ok { - return nil, tengo.ErrInvalidArgumentType{ + return nil, xgo.ErrInvalidArgumentType{ Name: "second", Expected: "int(compatible)", Found: args[1].TypeName(), } } if fn(f1, i2) { - return tengo.TrueValue, nil + return xgo.TrueValue, nil } - return tengo.FalseValue, nil + return xgo.FalseValue, nil } } // FuncAFRB transform a function of 'func(float64) bool' signature // into CallableFunc type. -func FuncAFRB(fn func(float64) bool) tengo.CallableFunc { - return func(args ...tengo.Object) (ret tengo.Object, err error) { +func FuncAFRB(fn func(float64) bool) xgo.CallableFunc { + return func(args ...xgo.Object) (ret xgo.Object, err error) { if len(args) != 1 { - return nil, tengo.ErrWrongNumArguments + return nil, xgo.ErrWrongNumArguments } - f1, ok := tengo.ToFloat64(args[0]) + f1, ok := xgo.ToFloat64(args[0]) if !ok { - return nil, tengo.ErrInvalidArgumentType{ + return nil, xgo.ErrInvalidArgumentType{ Name: "first", Expected: "float(compatible)", Found: args[0].TypeName(), } } if fn(f1) { - return tengo.TrueValue, nil + return xgo.TrueValue, nil } - return tengo.FalseValue, nil + return xgo.FalseValue, nil } } // FuncASRS transform a function of 'func(string) string' signature into // CallableFunc type. User function will return 'true' if underlying native // function returns nil. -func FuncASRS(fn func(string) string) tengo.CallableFunc { - return func(args ...tengo.Object) (tengo.Object, error) { +func FuncASRS(fn func(string) string) xgo.CallableFunc { + return func(args ...xgo.Object) (xgo.Object, error) { if len(args) != 1 { - return nil, tengo.ErrWrongNumArguments + return nil, xgo.ErrWrongNumArguments } - s1, ok := tengo.ToString(args[0]) + s1, ok := xgo.ToString(args[0]) if !ok { - return nil, tengo.ErrInvalidArgumentType{ + return nil, xgo.ErrInvalidArgumentType{ Name: "first", Expected: "string(compatible)", Found: args[0].TypeName(), } } s := fn(s1) - if len(s) > tengo.MaxStringLen { - return nil, tengo.ErrStringLimit + if len(s) > xgo.MaxStringLen { + return nil, xgo.ErrStringLimit } - return &tengo.String{Value: s}, nil + return &xgo.String{Value: s}, nil } } // FuncASRSs transform a function of 'func(string) []string' signature into // CallableFunc type. -func FuncASRSs(fn func(string) []string) tengo.CallableFunc { - return func(args ...tengo.Object) (tengo.Object, error) { +func FuncASRSs(fn func(string) []string) xgo.CallableFunc { + return func(args ...xgo.Object) (xgo.Object, error) { if len(args) != 1 { - return nil, tengo.ErrWrongNumArguments + return nil, xgo.ErrWrongNumArguments } - s1, ok := tengo.ToString(args[0]) + s1, ok := xgo.ToString(args[0]) if !ok { - return nil, tengo.ErrInvalidArgumentType{ + return nil, xgo.ErrInvalidArgumentType{ Name: "first", Expected: "string(compatible)", Found: args[0].TypeName(), } } res := fn(s1) - arr := &tengo.Array{} + arr := &xgo.Array{} for _, elem := range res { - if len(elem) > tengo.MaxStringLen { - return nil, tengo.ErrStringLimit + if len(elem) > xgo.MaxStringLen { + return nil, xgo.ErrStringLimit } - arr.Value = append(arr.Value, &tengo.String{Value: elem}) + arr.Value = append(arr.Value, &xgo.String{Value: elem}) } return arr, nil } @@ -491,14 +491,14 @@ func FuncASRSs(fn func(string) []string) tengo.CallableFunc { // FuncASRSE transform a function of 'func(string) (string, error)' signature // into CallableFunc type. User function will return 'true' if underlying // native function returns nil. -func FuncASRSE(fn func(string) (string, error)) tengo.CallableFunc { - return func(args ...tengo.Object) (tengo.Object, error) { +func FuncASRSE(fn func(string) (string, error)) xgo.CallableFunc { + return func(args ...xgo.Object) (xgo.Object, error) { if len(args) != 1 { - return nil, tengo.ErrWrongNumArguments + return nil, xgo.ErrWrongNumArguments } - s1, ok := tengo.ToString(args[0]) + s1, ok := xgo.ToString(args[0]) if !ok { - return nil, tengo.ErrInvalidArgumentType{ + return nil, xgo.ErrInvalidArgumentType{ Name: "first", Expected: "string(compatible)", Found: args[0].TypeName(), @@ -508,24 +508,24 @@ func FuncASRSE(fn func(string) (string, error)) tengo.CallableFunc { if err != nil { return wrapError(err), nil } - if len(res) > tengo.MaxStringLen { - return nil, tengo.ErrStringLimit + if len(res) > xgo.MaxStringLen { + return nil, xgo.ErrStringLimit } - return &tengo.String{Value: res}, nil + return &xgo.String{Value: res}, nil } } // FuncASRE transform a function of 'func(string) error' signature into // CallableFunc type. User function will return 'true' if underlying native // function returns nil. -func FuncASRE(fn func(string) error) tengo.CallableFunc { - return func(args ...tengo.Object) (tengo.Object, error) { +func FuncASRE(fn func(string) error) xgo.CallableFunc { + return func(args ...xgo.Object) (xgo.Object, error) { if len(args) != 1 { - return nil, tengo.ErrWrongNumArguments + return nil, xgo.ErrWrongNumArguments } - s1, ok := tengo.ToString(args[0]) + s1, ok := xgo.ToString(args[0]) if !ok { - return nil, tengo.ErrInvalidArgumentType{ + return nil, xgo.ErrInvalidArgumentType{ Name: "first", Expected: "string(compatible)", Found: args[0].TypeName(), @@ -538,22 +538,22 @@ func FuncASRE(fn func(string) error) tengo.CallableFunc { // FuncASSRE transform a function of 'func(string, string) error' signature // into CallableFunc type. User function will return 'true' if underlying // native function returns nil. -func FuncASSRE(fn func(string, string) error) tengo.CallableFunc { - return func(args ...tengo.Object) (tengo.Object, error) { +func FuncASSRE(fn func(string, string) error) xgo.CallableFunc { + return func(args ...xgo.Object) (xgo.Object, error) { if len(args) != 2 { - return nil, tengo.ErrWrongNumArguments + return nil, xgo.ErrWrongNumArguments } - s1, ok := tengo.ToString(args[0]) + s1, ok := xgo.ToString(args[0]) if !ok { - return nil, tengo.ErrInvalidArgumentType{ + return nil, xgo.ErrInvalidArgumentType{ Name: "first", Expected: "string(compatible)", Found: args[0].TypeName(), } } - s2, ok := tengo.ToString(args[1]) + s2, ok := xgo.ToString(args[1]) if !ok { - return nil, tengo.ErrInvalidArgumentType{ + return nil, xgo.ErrInvalidArgumentType{ Name: "second", Expected: "string(compatible)", Found: args[1].TypeName(), @@ -565,33 +565,33 @@ func FuncASSRE(fn func(string, string) error) tengo.CallableFunc { // FuncASSRSs transform a function of 'func(string, string) []string' // signature into CallableFunc type. -func FuncASSRSs(fn func(string, string) []string) tengo.CallableFunc { - return func(args ...tengo.Object) (tengo.Object, error) { +func FuncASSRSs(fn func(string, string) []string) xgo.CallableFunc { + return func(args ...xgo.Object) (xgo.Object, error) { if len(args) != 2 { - return nil, tengo.ErrWrongNumArguments + return nil, xgo.ErrWrongNumArguments } - s1, ok := tengo.ToString(args[0]) + s1, ok := xgo.ToString(args[0]) if !ok { - return nil, tengo.ErrInvalidArgumentType{ + return nil, xgo.ErrInvalidArgumentType{ Name: "first", Expected: "string(compatible)", Found: args[0].TypeName(), } } - s2, ok := tengo.ToString(args[1]) + s2, ok := xgo.ToString(args[1]) if !ok { - return nil, tengo.ErrInvalidArgumentType{ + return nil, xgo.ErrInvalidArgumentType{ Name: "first", Expected: "string(compatible)", Found: args[1].TypeName(), } } - arr := &tengo.Array{} + arr := &xgo.Array{} for _, res := range fn(s1, s2) { - if len(res) > tengo.MaxStringLen { - return nil, tengo.ErrStringLimit + if len(res) > xgo.MaxStringLen { + return nil, xgo.ErrStringLimit } - arr.Value = append(arr.Value, &tengo.String{Value: res}) + arr.Value = append(arr.Value, &xgo.String{Value: res}) } return arr, nil } @@ -599,41 +599,41 @@ func FuncASSRSs(fn func(string, string) []string) tengo.CallableFunc { // FuncASSIRSs transform a function of 'func(string, string, int) []string' // signature into CallableFunc type. -func FuncASSIRSs(fn func(string, string, int) []string) tengo.CallableFunc { - return func(args ...tengo.Object) (tengo.Object, error) { +func FuncASSIRSs(fn func(string, string, int) []string) xgo.CallableFunc { + return func(args ...xgo.Object) (xgo.Object, error) { if len(args) != 3 { - return nil, tengo.ErrWrongNumArguments + return nil, xgo.ErrWrongNumArguments } - s1, ok := tengo.ToString(args[0]) + s1, ok := xgo.ToString(args[0]) if !ok { - return nil, tengo.ErrInvalidArgumentType{ + return nil, xgo.ErrInvalidArgumentType{ Name: "first", Expected: "string(compatible)", Found: args[0].TypeName(), } } - s2, ok := tengo.ToString(args[1]) + s2, ok := xgo.ToString(args[1]) if !ok { - return nil, tengo.ErrInvalidArgumentType{ + return nil, xgo.ErrInvalidArgumentType{ Name: "second", Expected: "string(compatible)", Found: args[1].TypeName(), } } - i3, ok := tengo.ToInt(args[2]) + i3, ok := xgo.ToInt(args[2]) if !ok { - return nil, tengo.ErrInvalidArgumentType{ + return nil, xgo.ErrInvalidArgumentType{ Name: "third", Expected: "int(compatible)", Found: args[2].TypeName(), } } - arr := &tengo.Array{} + arr := &xgo.Array{} for _, res := range fn(s1, s2, i3) { - if len(res) > tengo.MaxStringLen { - return nil, tengo.ErrStringLimit + if len(res) > xgo.MaxStringLen { + return nil, xgo.ErrStringLimit } - arr.Value = append(arr.Value, &tengo.String{Value: res}) + arr.Value = append(arr.Value, &xgo.String{Value: res}) } return arr, nil } @@ -641,106 +641,106 @@ func FuncASSIRSs(fn func(string, string, int) []string) tengo.CallableFunc { // FuncASSRI transform a function of 'func(string, string) int' signature into // CallableFunc type. -func FuncASSRI(fn func(string, string) int) tengo.CallableFunc { - return func(args ...tengo.Object) (tengo.Object, error) { +func FuncASSRI(fn func(string, string) int) xgo.CallableFunc { + return func(args ...xgo.Object) (xgo.Object, error) { if len(args) != 2 { - return nil, tengo.ErrWrongNumArguments + return nil, xgo.ErrWrongNumArguments } - s1, ok := tengo.ToString(args[0]) + s1, ok := xgo.ToString(args[0]) if !ok { - return nil, tengo.ErrInvalidArgumentType{ + return nil, xgo.ErrInvalidArgumentType{ Name: "first", Expected: "string(compatible)", Found: args[0].TypeName(), } } - s2, ok := tengo.ToString(args[1]) + s2, ok := xgo.ToString(args[1]) if !ok { - return nil, tengo.ErrInvalidArgumentType{ + return nil, xgo.ErrInvalidArgumentType{ Name: "second", Expected: "string(compatible)", Found: args[0].TypeName(), } } - return &tengo.Int{Value: int64(fn(s1, s2))}, nil + return &xgo.Int{Value: int64(fn(s1, s2))}, nil } } // FuncASSRS transform a function of 'func(string, string) string' signature // into CallableFunc type. -func FuncASSRS(fn func(string, string) string) tengo.CallableFunc { - return func(args ...tengo.Object) (tengo.Object, error) { +func FuncASSRS(fn func(string, string) string) xgo.CallableFunc { + return func(args ...xgo.Object) (xgo.Object, error) { if len(args) != 2 { - return nil, tengo.ErrWrongNumArguments + return nil, xgo.ErrWrongNumArguments } - s1, ok := tengo.ToString(args[0]) + s1, ok := xgo.ToString(args[0]) if !ok { - return nil, tengo.ErrInvalidArgumentType{ + return nil, xgo.ErrInvalidArgumentType{ Name: "first", Expected: "string(compatible)", Found: args[0].TypeName(), } } - s2, ok := tengo.ToString(args[1]) + s2, ok := xgo.ToString(args[1]) if !ok { - return nil, tengo.ErrInvalidArgumentType{ + return nil, xgo.ErrInvalidArgumentType{ Name: "second", Expected: "string(compatible)", Found: args[1].TypeName(), } } s := fn(s1, s2) - if len(s) > tengo.MaxStringLen { - return nil, tengo.ErrStringLimit + if len(s) > xgo.MaxStringLen { + return nil, xgo.ErrStringLimit } - return &tengo.String{Value: s}, nil + return &xgo.String{Value: s}, nil } } // FuncASSRB transform a function of 'func(string, string) bool' signature // into CallableFunc type. -func FuncASSRB(fn func(string, string) bool) tengo.CallableFunc { - return func(args ...tengo.Object) (tengo.Object, error) { +func FuncASSRB(fn func(string, string) bool) xgo.CallableFunc { + return func(args ...xgo.Object) (xgo.Object, error) { if len(args) != 2 { - return nil, tengo.ErrWrongNumArguments + return nil, xgo.ErrWrongNumArguments } - s1, ok := tengo.ToString(args[0]) + s1, ok := xgo.ToString(args[0]) if !ok { - return nil, tengo.ErrInvalidArgumentType{ + return nil, xgo.ErrInvalidArgumentType{ Name: "first", Expected: "string(compatible)", Found: args[0].TypeName(), } } - s2, ok := tengo.ToString(args[1]) + s2, ok := xgo.ToString(args[1]) if !ok { - return nil, tengo.ErrInvalidArgumentType{ + return nil, xgo.ErrInvalidArgumentType{ Name: "second", Expected: "string(compatible)", Found: args[1].TypeName(), } } if fn(s1, s2) { - return tengo.TrueValue, nil + return xgo.TrueValue, nil } - return tengo.FalseValue, nil + return xgo.FalseValue, nil } } // FuncASsSRS transform a function of 'func([]string, string) string' signature // into CallableFunc type. -func FuncASsSRS(fn func([]string, string) string) tengo.CallableFunc { - return func(args ...tengo.Object) (tengo.Object, error) { +func FuncASsSRS(fn func([]string, string) string) xgo.CallableFunc { + return func(args ...xgo.Object) (xgo.Object, error) { if len(args) != 2 { - return nil, tengo.ErrWrongNumArguments + return nil, xgo.ErrWrongNumArguments } var ss1 []string switch arg0 := args[0].(type) { - case *tengo.Array: + case *xgo.Array: for idx, a := range arg0.Value { - as, ok := tengo.ToString(a) + as, ok := xgo.ToString(a) if !ok { - return nil, tengo.ErrInvalidArgumentType{ + return nil, xgo.ErrInvalidArgumentType{ Name: fmt.Sprintf("first[%d]", idx), Expected: "string(compatible)", Found: a.TypeName(), @@ -748,11 +748,11 @@ func FuncASsSRS(fn func([]string, string) string) tengo.CallableFunc { } ss1 = append(ss1, as) } - case *tengo.ImmutableArray: + case *xgo.ImmutableArray: for idx, a := range arg0.Value { - as, ok := tengo.ToString(a) + as, ok := xgo.ToString(a) if !ok { - return nil, tengo.ErrInvalidArgumentType{ + return nil, xgo.ErrInvalidArgumentType{ Name: fmt.Sprintf("first[%d]", idx), Expected: "string(compatible)", Found: a.TypeName(), @@ -761,46 +761,46 @@ func FuncASsSRS(fn func([]string, string) string) tengo.CallableFunc { ss1 = append(ss1, as) } default: - return nil, tengo.ErrInvalidArgumentType{ + return nil, xgo.ErrInvalidArgumentType{ Name: "first", Expected: "array", Found: args[0].TypeName(), } } - s2, ok := tengo.ToString(args[1]) + s2, ok := xgo.ToString(args[1]) if !ok { - return nil, tengo.ErrInvalidArgumentType{ + return nil, xgo.ErrInvalidArgumentType{ Name: "second", Expected: "string(compatible)", Found: args[1].TypeName(), } } s := fn(ss1, s2) - if len(s) > tengo.MaxStringLen { - return nil, tengo.ErrStringLimit + if len(s) > xgo.MaxStringLen { + return nil, xgo.ErrStringLimit } - return &tengo.String{Value: s}, nil + return &xgo.String{Value: s}, nil } } // FuncASI64RE transform a function of 'func(string, int64) error' signature // into CallableFunc type. -func FuncASI64RE(fn func(string, int64) error) tengo.CallableFunc { - return func(args ...tengo.Object) (ret tengo.Object, err error) { +func FuncASI64RE(fn func(string, int64) error) xgo.CallableFunc { + return func(args ...xgo.Object) (ret xgo.Object, err error) { if len(args) != 2 { - return nil, tengo.ErrWrongNumArguments + return nil, xgo.ErrWrongNumArguments } - s1, ok := tengo.ToString(args[0]) + s1, ok := xgo.ToString(args[0]) if !ok { - return nil, tengo.ErrInvalidArgumentType{ + return nil, xgo.ErrInvalidArgumentType{ Name: "first", Expected: "string(compatible)", Found: args[0].TypeName(), } } - i2, ok := tengo.ToInt64(args[1]) + i2, ok := xgo.ToInt64(args[1]) if !ok { - return nil, tengo.ErrInvalidArgumentType{ + return nil, xgo.ErrInvalidArgumentType{ Name: "second", Expected: "int(compatible)", Found: args[1].TypeName(), @@ -812,22 +812,22 @@ func FuncASI64RE(fn func(string, int64) error) tengo.CallableFunc { // FuncAIIRE transform a function of 'func(int, int) error' signature // into CallableFunc type. -func FuncAIIRE(fn func(int, int) error) tengo.CallableFunc { - return func(args ...tengo.Object) (ret tengo.Object, err error) { +func FuncAIIRE(fn func(int, int) error) xgo.CallableFunc { + return func(args ...xgo.Object) (ret xgo.Object, err error) { if len(args) != 2 { - return nil, tengo.ErrWrongNumArguments + return nil, xgo.ErrWrongNumArguments } - i1, ok := tengo.ToInt(args[0]) + i1, ok := xgo.ToInt(args[0]) if !ok { - return nil, tengo.ErrInvalidArgumentType{ + return nil, xgo.ErrInvalidArgumentType{ Name: "first", Expected: "int(compatible)", Found: args[0].TypeName(), } } - i2, ok := tengo.ToInt(args[1]) + i2, ok := xgo.ToInt(args[1]) if !ok { - return nil, tengo.ErrInvalidArgumentType{ + return nil, xgo.ErrInvalidArgumentType{ Name: "second", Expected: "int(compatible)", Found: args[1].TypeName(), @@ -839,61 +839,61 @@ func FuncAIIRE(fn func(int, int) error) tengo.CallableFunc { // FuncASIRS transform a function of 'func(string, int) string' signature // into CallableFunc type. -func FuncASIRS(fn func(string, int) string) tengo.CallableFunc { - return func(args ...tengo.Object) (ret tengo.Object, err error) { +func FuncASIRS(fn func(string, int) string) xgo.CallableFunc { + return func(args ...xgo.Object) (ret xgo.Object, err error) { if len(args) != 2 { - return nil, tengo.ErrWrongNumArguments + return nil, xgo.ErrWrongNumArguments } - s1, ok := tengo.ToString(args[0]) + s1, ok := xgo.ToString(args[0]) if !ok { - return nil, tengo.ErrInvalidArgumentType{ + return nil, xgo.ErrInvalidArgumentType{ Name: "first", Expected: "string(compatible)", Found: args[0].TypeName(), } } - i2, ok := tengo.ToInt(args[1]) + i2, ok := xgo.ToInt(args[1]) if !ok { - return nil, tengo.ErrInvalidArgumentType{ + return nil, xgo.ErrInvalidArgumentType{ Name: "second", Expected: "int(compatible)", Found: args[1].TypeName(), } } s := fn(s1, i2) - if len(s) > tengo.MaxStringLen { - return nil, tengo.ErrStringLimit + if len(s) > xgo.MaxStringLen { + return nil, xgo.ErrStringLimit } - return &tengo.String{Value: s}, nil + return &xgo.String{Value: s}, nil } } // FuncASIIRE transform a function of 'func(string, int, int) error' signature // into CallableFunc type. -func FuncASIIRE(fn func(string, int, int) error) tengo.CallableFunc { - return func(args ...tengo.Object) (ret tengo.Object, err error) { +func FuncASIIRE(fn func(string, int, int) error) xgo.CallableFunc { + return func(args ...xgo.Object) (ret xgo.Object, err error) { if len(args) != 3 { - return nil, tengo.ErrWrongNumArguments + return nil, xgo.ErrWrongNumArguments } - s1, ok := tengo.ToString(args[0]) + s1, ok := xgo.ToString(args[0]) if !ok { - return nil, tengo.ErrInvalidArgumentType{ + return nil, xgo.ErrInvalidArgumentType{ Name: "first", Expected: "string(compatible)", Found: args[0].TypeName(), } } - i2, ok := tengo.ToInt(args[1]) + i2, ok := xgo.ToInt(args[1]) if !ok { - return nil, tengo.ErrInvalidArgumentType{ + return nil, xgo.ErrInvalidArgumentType{ Name: "second", Expected: "int(compatible)", Found: args[1].TypeName(), } } - i3, ok := tengo.ToInt(args[2]) + i3, ok := xgo.ToInt(args[2]) if !ok { - return nil, tengo.ErrInvalidArgumentType{ + return nil, xgo.ErrInvalidArgumentType{ Name: "third", Expected: "int(compatible)", Found: args[2].TypeName(), @@ -905,14 +905,14 @@ func FuncASIIRE(fn func(string, int, int) error) tengo.CallableFunc { // FuncAYRIE transform a function of 'func([]byte) (int, error)' signature // into CallableFunc type. -func FuncAYRIE(fn func([]byte) (int, error)) tengo.CallableFunc { - return func(args ...tengo.Object) (ret tengo.Object, err error) { +func FuncAYRIE(fn func([]byte) (int, error)) xgo.CallableFunc { + return func(args ...xgo.Object) (ret xgo.Object, err error) { if len(args) != 1 { - return nil, tengo.ErrWrongNumArguments + return nil, xgo.ErrWrongNumArguments } - y1, ok := tengo.ToByteSlice(args[0]) + y1, ok := xgo.ToByteSlice(args[0]) if !ok { - return nil, tengo.ErrInvalidArgumentType{ + return nil, xgo.ErrInvalidArgumentType{ Name: "first", Expected: "bytes(compatible)", Found: args[0].TypeName(), @@ -922,40 +922,40 @@ func FuncAYRIE(fn func([]byte) (int, error)) tengo.CallableFunc { if err != nil { return wrapError(err), nil } - return &tengo.Int{Value: int64(res)}, nil + return &xgo.Int{Value: int64(res)}, nil } } // FuncAYRS transform a function of 'func([]byte) string' signature into // CallableFunc type. -func FuncAYRS(fn func([]byte) string) tengo.CallableFunc { - return func(args ...tengo.Object) (ret tengo.Object, err error) { +func FuncAYRS(fn func([]byte) string) xgo.CallableFunc { + return func(args ...xgo.Object) (ret xgo.Object, err error) { if len(args) != 1 { - return nil, tengo.ErrWrongNumArguments + return nil, xgo.ErrWrongNumArguments } - y1, ok := tengo.ToByteSlice(args[0]) + y1, ok := xgo.ToByteSlice(args[0]) if !ok { - return nil, tengo.ErrInvalidArgumentType{ + return nil, xgo.ErrInvalidArgumentType{ Name: "first", Expected: "bytes(compatible)", Found: args[0].TypeName(), } } res := fn(y1) - return &tengo.String{Value: res}, nil + return &xgo.String{Value: res}, nil } } // FuncASRIE transform a function of 'func(string) (int, error)' signature // into CallableFunc type. -func FuncASRIE(fn func(string) (int, error)) tengo.CallableFunc { - return func(args ...tengo.Object) (ret tengo.Object, err error) { +func FuncASRIE(fn func(string) (int, error)) xgo.CallableFunc { + return func(args ...xgo.Object) (ret xgo.Object, err error) { if len(args) != 1 { - return nil, tengo.ErrWrongNumArguments + return nil, xgo.ErrWrongNumArguments } - s1, ok := tengo.ToString(args[0]) + s1, ok := xgo.ToString(args[0]) if !ok { - return nil, tengo.ErrInvalidArgumentType{ + return nil, xgo.ErrInvalidArgumentType{ Name: "first", Expected: "string(compatible)", Found: args[0].TypeName(), @@ -965,20 +965,20 @@ func FuncASRIE(fn func(string) (int, error)) tengo.CallableFunc { if err != nil { return wrapError(err), nil } - return &tengo.Int{Value: int64(res)}, nil + return &xgo.Int{Value: int64(res)}, nil } } // FuncASRYE transform a function of 'func(string) ([]byte, error)' signature // into CallableFunc type. -func FuncASRYE(fn func(string) ([]byte, error)) tengo.CallableFunc { - return func(args ...tengo.Object) (ret tengo.Object, err error) { +func FuncASRYE(fn func(string) ([]byte, error)) xgo.CallableFunc { + return func(args ...xgo.Object) (ret xgo.Object, err error) { if len(args) != 1 { - return nil, tengo.ErrWrongNumArguments + return nil, xgo.ErrWrongNumArguments } - s1, ok := tengo.ToString(args[0]) + s1, ok := xgo.ToString(args[0]) if !ok { - return nil, tengo.ErrInvalidArgumentType{ + return nil, xgo.ErrInvalidArgumentType{ Name: "first", Expected: "string(compatible)", Found: args[0].TypeName(), @@ -988,23 +988,23 @@ func FuncASRYE(fn func(string) ([]byte, error)) tengo.CallableFunc { if err != nil { return wrapError(err), nil } - if len(res) > tengo.MaxBytesLen { - return nil, tengo.ErrBytesLimit + if len(res) > xgo.MaxBytesLen { + return nil, xgo.ErrBytesLimit } - return &tengo.Bytes{Value: res}, nil + return &xgo.Bytes{Value: res}, nil } } // FuncAIRSsE transform a function of 'func(int) ([]string, error)' signature // into CallableFunc type. -func FuncAIRSsE(fn func(int) ([]string, error)) tengo.CallableFunc { - return func(args ...tengo.Object) (ret tengo.Object, err error) { +func FuncAIRSsE(fn func(int) ([]string, error)) xgo.CallableFunc { + return func(args ...xgo.Object) (ret xgo.Object, err error) { if len(args) != 1 { - return nil, tengo.ErrWrongNumArguments + return nil, xgo.ErrWrongNumArguments } - i1, ok := tengo.ToInt(args[0]) + i1, ok := xgo.ToInt(args[0]) if !ok { - return nil, tengo.ErrInvalidArgumentType{ + return nil, xgo.ErrInvalidArgumentType{ Name: "first", Expected: "int(compatible)", Found: args[0].TypeName(), @@ -1014,12 +1014,12 @@ func FuncAIRSsE(fn func(int) ([]string, error)) tengo.CallableFunc { if err != nil { return wrapError(err), nil } - arr := &tengo.Array{} + arr := &xgo.Array{} for _, r := range res { - if len(r) > tengo.MaxStringLen { - return nil, tengo.ErrStringLimit + if len(r) > xgo.MaxStringLen { + return nil, xgo.ErrStringLimit } - arr.Value = append(arr.Value, &tengo.String{Value: r}) + arr.Value = append(arr.Value, &xgo.String{Value: r}) } return arr, nil } @@ -1027,23 +1027,23 @@ func FuncAIRSsE(fn func(int) ([]string, error)) tengo.CallableFunc { // FuncAIRS transform a function of 'func(int) string' signature into // CallableFunc type. -func FuncAIRS(fn func(int) string) tengo.CallableFunc { - return func(args ...tengo.Object) (ret tengo.Object, err error) { +func FuncAIRS(fn func(int) string) xgo.CallableFunc { + return func(args ...xgo.Object) (ret xgo.Object, err error) { if len(args) != 1 { - return nil, tengo.ErrWrongNumArguments + return nil, xgo.ErrWrongNumArguments } - i1, ok := tengo.ToInt(args[0]) + i1, ok := xgo.ToInt(args[0]) if !ok { - return nil, tengo.ErrInvalidArgumentType{ + return nil, xgo.ErrInvalidArgumentType{ Name: "first", Expected: "int(compatible)", Found: args[0].TypeName(), } } s := fn(i1) - if len(s) > tengo.MaxStringLen { - return nil, tengo.ErrStringLimit + if len(s) > xgo.MaxStringLen { + return nil, xgo.ErrStringLimit } - return &tengo.String{Value: s}, nil + return &xgo.String{Value: s}, nil } } diff --git a/stdlib/func_typedefs_test.go b/stdlib/func_typedefs_test.go index 091c5c4..7bb716a 100644 --- a/stdlib/func_typedefs_test.go +++ b/stdlib/func_typedefs_test.go @@ -6,51 +6,51 @@ import ( "strings" "testing" - "github.com/d5/tengo/v2" - "github.com/d5/tengo/v2/require" - "github.com/d5/tengo/v2/stdlib" + "surdeus.su/core/xgo/v2" + "surdeus.su/core/xgo/v2/require" + "surdeus.su/core/xgo/v2/stdlib" ) func TestFuncAIR(t *testing.T) { uf := stdlib.FuncAIR(func(int) {}) - ret, err := funcCall(uf, &tengo.Int{Value: 10}) + ret, err := funcCall(uf, &xgo.Int{Value: 10}) require.NoError(t, err) - require.Equal(t, tengo.UndefinedValue, ret) + require.Equal(t, xgo.UndefinedValue, ret) _, err = funcCall(uf) - require.Equal(t, tengo.ErrWrongNumArguments, err) + require.Equal(t, xgo.ErrWrongNumArguments, err) } func TestFuncAR(t *testing.T) { uf := stdlib.FuncAR(func() {}) ret, err := funcCall(uf) require.NoError(t, err) - require.Equal(t, tengo.UndefinedValue, ret) - _, err = funcCall(uf, tengo.TrueValue) - require.Equal(t, tengo.ErrWrongNumArguments, err) + require.Equal(t, xgo.UndefinedValue, ret) + _, err = funcCall(uf, xgo.TrueValue) + require.Equal(t, xgo.ErrWrongNumArguments, err) } func TestFuncARI(t *testing.T) { uf := stdlib.FuncARI(func() int { return 10 }) ret, err := funcCall(uf) require.NoError(t, err) - require.Equal(t, &tengo.Int{Value: 10}, ret) - _, err = funcCall(uf, tengo.TrueValue) - require.Equal(t, tengo.ErrWrongNumArguments, err) + require.Equal(t, &xgo.Int{Value: 10}, ret) + _, err = funcCall(uf, xgo.TrueValue) + require.Equal(t, xgo.ErrWrongNumArguments, err) } func TestFuncARE(t *testing.T) { uf := stdlib.FuncARE(func() error { return nil }) ret, err := funcCall(uf) require.NoError(t, err) - require.Equal(t, tengo.TrueValue, ret) + require.Equal(t, xgo.TrueValue, ret) uf = stdlib.FuncARE(func() error { return errors.New("some error") }) ret, err = funcCall(uf) require.NoError(t, err) - require.Equal(t, &tengo.Error{ - Value: &tengo.String{Value: "some error"}, + require.Equal(t, &xgo.Error{ + Value: &xgo.String{Value: "some error"}, }, ret) - _, err = funcCall(uf, tengo.TrueValue) - require.Equal(t, tengo.ErrWrongNumArguments, err) + _, err = funcCall(uf, xgo.TrueValue) + require.Equal(t, xgo.ErrWrongNumArguments, err) } func TestFuncARIsE(t *testing.T) { @@ -59,364 +59,364 @@ func TestFuncARIsE(t *testing.T) { }) ret, err := funcCall(uf) require.NoError(t, err) - require.Equal(t, array(&tengo.Int{Value: 1}, - &tengo.Int{Value: 2}, &tengo.Int{Value: 3}), ret) + require.Equal(t, array(&xgo.Int{Value: 1}, + &xgo.Int{Value: 2}, &xgo.Int{Value: 3}), ret) uf = stdlib.FuncARIsE(func() ([]int, error) { return nil, errors.New("some error") }) ret, err = funcCall(uf) require.NoError(t, err) - require.Equal(t, &tengo.Error{ - Value: &tengo.String{Value: "some error"}, + require.Equal(t, &xgo.Error{ + Value: &xgo.String{Value: "some error"}, }, ret) - _, err = funcCall(uf, tengo.TrueValue) - require.Equal(t, tengo.ErrWrongNumArguments, err) + _, err = funcCall(uf, xgo.TrueValue) + require.Equal(t, xgo.ErrWrongNumArguments, err) } func TestFuncARS(t *testing.T) { uf := stdlib.FuncARS(func() string { return "foo" }) ret, err := funcCall(uf) require.NoError(t, err) - require.Equal(t, &tengo.String{Value: "foo"}, ret) - _, err = funcCall(uf, tengo.TrueValue) - require.Equal(t, tengo.ErrWrongNumArguments, err) + require.Equal(t, &xgo.String{Value: "foo"}, ret) + _, err = funcCall(uf, xgo.TrueValue) + require.Equal(t, xgo.ErrWrongNumArguments, err) } func TestFuncARSE(t *testing.T) { uf := stdlib.FuncARSE(func() (string, error) { return "foo", nil }) ret, err := funcCall(uf) require.NoError(t, err) - require.Equal(t, &tengo.String{Value: "foo"}, ret) + require.Equal(t, &xgo.String{Value: "foo"}, ret) uf = stdlib.FuncARSE(func() (string, error) { return "", errors.New("some error") }) ret, err = funcCall(uf) require.NoError(t, err) - require.Equal(t, &tengo.Error{ - Value: &tengo.String{Value: "some error"}, + require.Equal(t, &xgo.Error{ + Value: &xgo.String{Value: "some error"}, }, ret) - _, err = funcCall(uf, tengo.TrueValue) - require.Equal(t, tengo.ErrWrongNumArguments, err) + _, err = funcCall(uf, xgo.TrueValue) + require.Equal(t, xgo.ErrWrongNumArguments, err) } func TestFuncARSs(t *testing.T) { uf := stdlib.FuncARSs(func() []string { return []string{"foo", "bar"} }) ret, err := funcCall(uf) require.NoError(t, err) - require.Equal(t, array(&tengo.String{Value: "foo"}, - &tengo.String{Value: "bar"}), ret) - _, err = funcCall(uf, tengo.TrueValue) - require.Equal(t, tengo.ErrWrongNumArguments, err) + require.Equal(t, array(&xgo.String{Value: "foo"}, + &xgo.String{Value: "bar"}), ret) + _, err = funcCall(uf, xgo.TrueValue) + require.Equal(t, xgo.ErrWrongNumArguments, err) } func TestFuncASRE(t *testing.T) { uf := stdlib.FuncASRE(func(a string) error { return nil }) - ret, err := funcCall(uf, &tengo.String{Value: "foo"}) + ret, err := funcCall(uf, &xgo.String{Value: "foo"}) require.NoError(t, err) - require.Equal(t, tengo.TrueValue, ret) + require.Equal(t, xgo.TrueValue, ret) uf = stdlib.FuncASRE(func(a string) error { return errors.New("some error") }) - ret, err = funcCall(uf, &tengo.String{Value: "foo"}) + ret, err = funcCall(uf, &xgo.String{Value: "foo"}) require.NoError(t, err) - require.Equal(t, &tengo.Error{ - Value: &tengo.String{Value: "some error"}, + require.Equal(t, &xgo.Error{ + Value: &xgo.String{Value: "some error"}, }, ret) _, err = funcCall(uf) - require.Equal(t, tengo.ErrWrongNumArguments, err) + require.Equal(t, xgo.ErrWrongNumArguments, err) } func TestFuncASRS(t *testing.T) { uf := stdlib.FuncASRS(func(a string) string { return a }) - ret, err := funcCall(uf, &tengo.String{Value: "foo"}) + ret, err := funcCall(uf, &xgo.String{Value: "foo"}) require.NoError(t, err) - require.Equal(t, &tengo.String{Value: "foo"}, ret) + require.Equal(t, &xgo.String{Value: "foo"}, ret) _, err = funcCall(uf) - require.Equal(t, tengo.ErrWrongNumArguments, err) + require.Equal(t, xgo.ErrWrongNumArguments, err) } func TestFuncASRSs(t *testing.T) { uf := stdlib.FuncASRSs(func(a string) []string { return []string{a} }) - ret, err := funcCall(uf, &tengo.String{Value: "foo"}) + ret, err := funcCall(uf, &xgo.String{Value: "foo"}) require.NoError(t, err) - require.Equal(t, array(&tengo.String{Value: "foo"}), ret) + require.Equal(t, array(&xgo.String{Value: "foo"}), ret) _, err = funcCall(uf) - require.Equal(t, tengo.ErrWrongNumArguments, err) + require.Equal(t, xgo.ErrWrongNumArguments, err) } func TestFuncASI64RE(t *testing.T) { uf := stdlib.FuncASI64RE(func(a string, b int64) error { return nil }) - ret, err := funcCall(uf, &tengo.String{Value: "foo"}, &tengo.Int{Value: 5}) + ret, err := funcCall(uf, &xgo.String{Value: "foo"}, &xgo.Int{Value: 5}) require.NoError(t, err) - require.Equal(t, tengo.TrueValue, ret) + require.Equal(t, xgo.TrueValue, ret) uf = stdlib.FuncASI64RE(func(a string, b int64) error { return errors.New("some error") }) - ret, err = funcCall(uf, &tengo.String{Value: "foo"}, &tengo.Int{Value: 5}) + ret, err = funcCall(uf, &xgo.String{Value: "foo"}, &xgo.Int{Value: 5}) require.NoError(t, err) require.Equal(t, - &tengo.Error{Value: &tengo.String{Value: "some error"}}, ret) + &xgo.Error{Value: &xgo.String{Value: "some error"}}, ret) _, err = funcCall(uf) - require.Equal(t, tengo.ErrWrongNumArguments, err) + require.Equal(t, xgo.ErrWrongNumArguments, err) } func TestFuncAIIRE(t *testing.T) { uf := stdlib.FuncAIIRE(func(a, b int) error { return nil }) - ret, err := funcCall(uf, &tengo.Int{Value: 5}, &tengo.Int{Value: 7}) + ret, err := funcCall(uf, &xgo.Int{Value: 5}, &xgo.Int{Value: 7}) require.NoError(t, err) - require.Equal(t, tengo.TrueValue, ret) + require.Equal(t, xgo.TrueValue, ret) uf = stdlib.FuncAIIRE(func(a, b int) error { return errors.New("some error") }) - ret, err = funcCall(uf, &tengo.Int{Value: 5}, &tengo.Int{Value: 7}) + ret, err = funcCall(uf, &xgo.Int{Value: 5}, &xgo.Int{Value: 7}) require.NoError(t, err) require.Equal(t, - &tengo.Error{Value: &tengo.String{Value: "some error"}}, ret) + &xgo.Error{Value: &xgo.String{Value: "some error"}}, ret) _, err = funcCall(uf) - require.Equal(t, tengo.ErrWrongNumArguments, err) + require.Equal(t, xgo.ErrWrongNumArguments, err) } func TestFuncASIIRE(t *testing.T) { uf := stdlib.FuncASIIRE(func(a string, b, c int) error { return nil }) - ret, err := funcCall(uf, &tengo.String{Value: "foo"}, &tengo.Int{Value: 5}, - &tengo.Int{Value: 7}) + ret, err := funcCall(uf, &xgo.String{Value: "foo"}, &xgo.Int{Value: 5}, + &xgo.Int{Value: 7}) require.NoError(t, err) - require.Equal(t, tengo.TrueValue, ret) + require.Equal(t, xgo.TrueValue, ret) uf = stdlib.FuncASIIRE(func(a string, b, c int) error { return errors.New("some error") }) - ret, err = funcCall(uf, &tengo.String{Value: "foo"}, &tengo.Int{Value: 5}, - &tengo.Int{Value: 7}) + ret, err = funcCall(uf, &xgo.String{Value: "foo"}, &xgo.Int{Value: 5}, + &xgo.Int{Value: 7}) require.NoError(t, err) require.Equal(t, - &tengo.Error{Value: &tengo.String{Value: "some error"}}, ret) + &xgo.Error{Value: &xgo.String{Value: "some error"}}, ret) _, err = funcCall(uf) - require.Equal(t, tengo.ErrWrongNumArguments, err) + require.Equal(t, xgo.ErrWrongNumArguments, err) } func TestFuncASRSE(t *testing.T) { uf := stdlib.FuncASRSE(func(a string) (string, error) { return a, nil }) - ret, err := funcCall(uf, &tengo.String{Value: "foo"}) + ret, err := funcCall(uf, &xgo.String{Value: "foo"}) require.NoError(t, err) - require.Equal(t, &tengo.String{Value: "foo"}, ret) + require.Equal(t, &xgo.String{Value: "foo"}, ret) uf = stdlib.FuncASRSE(func(a string) (string, error) { return a, errors.New("some error") }) - ret, err = funcCall(uf, &tengo.String{Value: "foo"}) + ret, err = funcCall(uf, &xgo.String{Value: "foo"}) require.NoError(t, err) require.Equal(t, - &tengo.Error{Value: &tengo.String{Value: "some error"}}, ret) + &xgo.Error{Value: &xgo.String{Value: "some error"}}, ret) _, err = funcCall(uf) - require.Equal(t, tengo.ErrWrongNumArguments, err) + require.Equal(t, xgo.ErrWrongNumArguments, err) } func TestFuncASSRE(t *testing.T) { uf := stdlib.FuncASSRE(func(a, b string) error { return nil }) - ret, err := funcCall(uf, &tengo.String{Value: "foo"}, - &tengo.String{Value: "bar"}) + ret, err := funcCall(uf, &xgo.String{Value: "foo"}, + &xgo.String{Value: "bar"}) require.NoError(t, err) - require.Equal(t, tengo.TrueValue, ret) + require.Equal(t, xgo.TrueValue, ret) uf = stdlib.FuncASSRE(func(a, b string) error { return errors.New("some error") }) - ret, err = funcCall(uf, &tengo.String{Value: "foo"}, - &tengo.String{Value: "bar"}) + ret, err = funcCall(uf, &xgo.String{Value: "foo"}, + &xgo.String{Value: "bar"}) require.NoError(t, err) require.Equal(t, - &tengo.Error{Value: &tengo.String{Value: "some error"}}, ret) - _, err = funcCall(uf, &tengo.String{Value: "foo"}) - require.Equal(t, tengo.ErrWrongNumArguments, err) + &xgo.Error{Value: &xgo.String{Value: "some error"}}, ret) + _, err = funcCall(uf, &xgo.String{Value: "foo"}) + require.Equal(t, xgo.ErrWrongNumArguments, err) } func TestFuncASsRS(t *testing.T) { uf := stdlib.FuncASsSRS(func(a []string, b string) string { return strings.Join(a, b) }) - ret, err := funcCall(uf, array(&tengo.String{Value: "foo"}, - &tengo.String{Value: "bar"}), &tengo.String{Value: " "}) + ret, err := funcCall(uf, array(&xgo.String{Value: "foo"}, + &xgo.String{Value: "bar"}), &xgo.String{Value: " "}) require.NoError(t, err) - require.Equal(t, &tengo.String{Value: "foo bar"}, ret) - _, err = funcCall(uf, &tengo.String{Value: "foo"}) - require.Equal(t, tengo.ErrWrongNumArguments, err) + require.Equal(t, &xgo.String{Value: "foo bar"}, ret) + _, err = funcCall(uf, &xgo.String{Value: "foo"}) + require.Equal(t, xgo.ErrWrongNumArguments, err) } func TestFuncARF(t *testing.T) { uf := stdlib.FuncARF(func() float64 { return 10.0 }) ret, err := funcCall(uf) require.NoError(t, err) - require.Equal(t, &tengo.Float{Value: 10.0}, ret) - _, err = funcCall(uf, tengo.TrueValue) - require.Equal(t, tengo.ErrWrongNumArguments, err) + require.Equal(t, &xgo.Float{Value: 10.0}, ret) + _, err = funcCall(uf, xgo.TrueValue) + require.Equal(t, xgo.ErrWrongNumArguments, err) } func TestFuncAFRF(t *testing.T) { uf := stdlib.FuncAFRF(func(a float64) float64 { return a }) - ret, err := funcCall(uf, &tengo.Float{Value: 10.0}) + ret, err := funcCall(uf, &xgo.Float{Value: 10.0}) require.NoError(t, err) - require.Equal(t, &tengo.Float{Value: 10.0}, ret) + require.Equal(t, &xgo.Float{Value: 10.0}, ret) _, err = funcCall(uf) - require.Equal(t, tengo.ErrWrongNumArguments, err) - _, err = funcCall(uf, tengo.TrueValue, tengo.TrueValue) - require.Equal(t, tengo.ErrWrongNumArguments, err) + require.Equal(t, xgo.ErrWrongNumArguments, err) + _, err = funcCall(uf, xgo.TrueValue, xgo.TrueValue) + require.Equal(t, xgo.ErrWrongNumArguments, err) } func TestFuncAIRF(t *testing.T) { uf := stdlib.FuncAIRF(func(a int) float64 { return float64(a) }) - ret, err := funcCall(uf, &tengo.Int{Value: 10.0}) + ret, err := funcCall(uf, &xgo.Int{Value: 10.0}) require.NoError(t, err) - require.Equal(t, &tengo.Float{Value: 10.0}, ret) + require.Equal(t, &xgo.Float{Value: 10.0}, ret) _, err = funcCall(uf) - require.Equal(t, tengo.ErrWrongNumArguments, err) - _, err = funcCall(uf, tengo.TrueValue, tengo.TrueValue) - require.Equal(t, tengo.ErrWrongNumArguments, err) + require.Equal(t, xgo.ErrWrongNumArguments, err) + _, err = funcCall(uf, xgo.TrueValue, xgo.TrueValue) + require.Equal(t, xgo.ErrWrongNumArguments, err) } func TestFuncAFRI(t *testing.T) { uf := stdlib.FuncAFRI(func(a float64) int { return int(a) }) - ret, err := funcCall(uf, &tengo.Float{Value: 10.5}) + ret, err := funcCall(uf, &xgo.Float{Value: 10.5}) require.NoError(t, err) - require.Equal(t, &tengo.Int{Value: 10}, ret) + require.Equal(t, &xgo.Int{Value: 10}, ret) _, err = funcCall(uf) - require.Equal(t, tengo.ErrWrongNumArguments, err) - _, err = funcCall(uf, tengo.TrueValue, tengo.TrueValue) - require.Equal(t, tengo.ErrWrongNumArguments, err) + require.Equal(t, xgo.ErrWrongNumArguments, err) + _, err = funcCall(uf, xgo.TrueValue, xgo.TrueValue) + require.Equal(t, xgo.ErrWrongNumArguments, err) } func TestFuncAFRB(t *testing.T) { uf := stdlib.FuncAFRB(func(a float64) bool { return a > 0.0 }) - ret, err := funcCall(uf, &tengo.Float{Value: 0.1}) + ret, err := funcCall(uf, &xgo.Float{Value: 0.1}) require.NoError(t, err) - require.Equal(t, tengo.TrueValue, ret) + require.Equal(t, xgo.TrueValue, ret) _, err = funcCall(uf) - require.Equal(t, tengo.ErrWrongNumArguments, err) - _, err = funcCall(uf, tengo.TrueValue, tengo.TrueValue) - require.Equal(t, tengo.ErrWrongNumArguments, err) + require.Equal(t, xgo.ErrWrongNumArguments, err) + _, err = funcCall(uf, xgo.TrueValue, xgo.TrueValue) + require.Equal(t, xgo.ErrWrongNumArguments, err) } func TestFuncAFFRF(t *testing.T) { uf := stdlib.FuncAFFRF(func(a, b float64) float64 { return a + b }) - ret, err := funcCall(uf, &tengo.Float{Value: 10.0}, - &tengo.Float{Value: 20.0}) + ret, err := funcCall(uf, &xgo.Float{Value: 10.0}, + &xgo.Float{Value: 20.0}) require.NoError(t, err) - require.Equal(t, &tengo.Float{Value: 30.0}, ret) + require.Equal(t, &xgo.Float{Value: 30.0}, ret) _, err = funcCall(uf) - require.Equal(t, tengo.ErrWrongNumArguments, err) - _, err = funcCall(uf, tengo.TrueValue) - require.Equal(t, tengo.ErrWrongNumArguments, err) + require.Equal(t, xgo.ErrWrongNumArguments, err) + _, err = funcCall(uf, xgo.TrueValue) + require.Equal(t, xgo.ErrWrongNumArguments, err) } func TestFuncASIRS(t *testing.T) { uf := stdlib.FuncASIRS(func(a string, b int) string { return strings.Repeat(a, b) }) - ret, err := funcCall(uf, &tengo.String{Value: "ab"}, &tengo.Int{Value: 2}) + ret, err := funcCall(uf, &xgo.String{Value: "ab"}, &xgo.Int{Value: 2}) require.NoError(t, err) - require.Equal(t, &tengo.String{Value: "abab"}, ret) + require.Equal(t, &xgo.String{Value: "abab"}, ret) _, err = funcCall(uf) - require.Equal(t, tengo.ErrWrongNumArguments, err) - _, err = funcCall(uf, tengo.TrueValue) - require.Equal(t, tengo.ErrWrongNumArguments, err) + require.Equal(t, xgo.ErrWrongNumArguments, err) + _, err = funcCall(uf, xgo.TrueValue) + require.Equal(t, xgo.ErrWrongNumArguments, err) } func TestFuncAIFRF(t *testing.T) { uf := stdlib.FuncAIFRF(func(a int, b float64) float64 { return float64(a) + b }) - ret, err := funcCall(uf, &tengo.Int{Value: 10}, &tengo.Float{Value: 20.0}) + ret, err := funcCall(uf, &xgo.Int{Value: 10}, &xgo.Float{Value: 20.0}) require.NoError(t, err) - require.Equal(t, &tengo.Float{Value: 30.0}, ret) + require.Equal(t, &xgo.Float{Value: 30.0}, ret) _, err = funcCall(uf) - require.Equal(t, tengo.ErrWrongNumArguments, err) - _, err = funcCall(uf, tengo.TrueValue) - require.Equal(t, tengo.ErrWrongNumArguments, err) + require.Equal(t, xgo.ErrWrongNumArguments, err) + _, err = funcCall(uf, xgo.TrueValue) + require.Equal(t, xgo.ErrWrongNumArguments, err) } func TestFuncAFIRF(t *testing.T) { uf := stdlib.FuncAFIRF(func(a float64, b int) float64 { return a + float64(b) }) - ret, err := funcCall(uf, &tengo.Float{Value: 10.0}, &tengo.Int{Value: 20}) + ret, err := funcCall(uf, &xgo.Float{Value: 10.0}, &xgo.Int{Value: 20}) require.NoError(t, err) - require.Equal(t, &tengo.Float{Value: 30.0}, ret) + require.Equal(t, &xgo.Float{Value: 30.0}, ret) _, err = funcCall(uf) - require.Equal(t, tengo.ErrWrongNumArguments, err) - _, err = funcCall(uf, tengo.TrueValue) - require.Equal(t, tengo.ErrWrongNumArguments, err) + require.Equal(t, xgo.ErrWrongNumArguments, err) + _, err = funcCall(uf, xgo.TrueValue) + require.Equal(t, xgo.ErrWrongNumArguments, err) } func TestFuncAFIRB(t *testing.T) { uf := stdlib.FuncAFIRB(func(a float64, b int) bool { return a < float64(b) }) - ret, err := funcCall(uf, &tengo.Float{Value: 10.0}, &tengo.Int{Value: 20}) + ret, err := funcCall(uf, &xgo.Float{Value: 10.0}, &xgo.Int{Value: 20}) require.NoError(t, err) - require.Equal(t, tengo.TrueValue, ret) + require.Equal(t, xgo.TrueValue, ret) _, err = funcCall(uf) - require.Equal(t, tengo.ErrWrongNumArguments, err) - _, err = funcCall(uf, tengo.TrueValue) - require.Equal(t, tengo.ErrWrongNumArguments, err) + require.Equal(t, xgo.ErrWrongNumArguments, err) + _, err = funcCall(uf, xgo.TrueValue) + require.Equal(t, xgo.ErrWrongNumArguments, err) } func TestFuncAIRSsE(t *testing.T) { uf := stdlib.FuncAIRSsE(func(a int) ([]string, error) { return []string{"foo", "bar"}, nil }) - ret, err := funcCall(uf, &tengo.Int{Value: 10}) + ret, err := funcCall(uf, &xgo.Int{Value: 10}) require.NoError(t, err) - require.Equal(t, array(&tengo.String{Value: "foo"}, - &tengo.String{Value: "bar"}), ret) + require.Equal(t, array(&xgo.String{Value: "foo"}, + &xgo.String{Value: "bar"}), ret) uf = stdlib.FuncAIRSsE(func(a int) ([]string, error) { return nil, errors.New("some error") }) - ret, err = funcCall(uf, &tengo.Int{Value: 10}) + ret, err = funcCall(uf, &xgo.Int{Value: 10}) require.NoError(t, err) require.Equal(t, - &tengo.Error{Value: &tengo.String{Value: "some error"}}, ret) + &xgo.Error{Value: &xgo.String{Value: "some error"}}, ret) _, err = funcCall(uf) - require.Equal(t, tengo.ErrWrongNumArguments, err) + require.Equal(t, xgo.ErrWrongNumArguments, err) } func TestFuncASSRSs(t *testing.T) { uf := stdlib.FuncASSRSs(func(a, b string) []string { return []string{a, b} }) - ret, err := funcCall(uf, &tengo.String{Value: "foo"}, - &tengo.String{Value: "bar"}) + ret, err := funcCall(uf, &xgo.String{Value: "foo"}, + &xgo.String{Value: "bar"}) require.NoError(t, err) - require.Equal(t, array(&tengo.String{Value: "foo"}, - &tengo.String{Value: "bar"}), ret) + require.Equal(t, array(&xgo.String{Value: "foo"}, + &xgo.String{Value: "bar"}), ret) _, err = funcCall(uf) - require.Equal(t, tengo.ErrWrongNumArguments, err) + require.Equal(t, xgo.ErrWrongNumArguments, err) } func TestFuncASSIRSs(t *testing.T) { uf := stdlib.FuncASSIRSs(func(a, b string, c int) []string { return []string{a, b, strconv.Itoa(c)} }) - ret, err := funcCall(uf, &tengo.String{Value: "foo"}, - &tengo.String{Value: "bar"}, &tengo.Int{Value: 5}) + ret, err := funcCall(uf, &xgo.String{Value: "foo"}, + &xgo.String{Value: "bar"}, &xgo.Int{Value: 5}) require.NoError(t, err) - require.Equal(t, array(&tengo.String{Value: "foo"}, - &tengo.String{Value: "bar"}, &tengo.String{Value: "5"}), ret) + require.Equal(t, array(&xgo.String{Value: "foo"}, + &xgo.String{Value: "bar"}, &xgo.String{Value: "5"}), ret) _, err = funcCall(uf) - require.Equal(t, tengo.ErrWrongNumArguments, err) + require.Equal(t, xgo.ErrWrongNumArguments, err) } func TestFuncARB(t *testing.T) { uf := stdlib.FuncARB(func() bool { return true }) ret, err := funcCall(uf) require.NoError(t, err) - require.Equal(t, tengo.TrueValue, ret) - _, err = funcCall(uf, tengo.TrueValue) - require.Equal(t, tengo.ErrWrongNumArguments, err) + require.Equal(t, xgo.TrueValue, ret) + _, err = funcCall(uf, xgo.TrueValue) + require.Equal(t, xgo.ErrWrongNumArguments, err) } func TestFuncARYE(t *testing.T) { @@ -425,114 +425,114 @@ func TestFuncARYE(t *testing.T) { }) ret, err := funcCall(uf) require.NoError(t, err) - require.Equal(t, &tengo.Bytes{Value: []byte("foo bar")}, ret) + require.Equal(t, &xgo.Bytes{Value: []byte("foo bar")}, ret) uf = stdlib.FuncARYE(func() ([]byte, error) { return nil, errors.New("some error") }) ret, err = funcCall(uf) require.NoError(t, err) require.Equal(t, - &tengo.Error{Value: &tengo.String{Value: "some error"}}, ret) - _, err = funcCall(uf, tengo.TrueValue) - require.Equal(t, tengo.ErrWrongNumArguments, err) + &xgo.Error{Value: &xgo.String{Value: "some error"}}, ret) + _, err = funcCall(uf, xgo.TrueValue) + require.Equal(t, xgo.ErrWrongNumArguments, err) } func TestFuncASRIE(t *testing.T) { uf := stdlib.FuncASRIE(func(a string) (int, error) { return 5, nil }) - ret, err := funcCall(uf, &tengo.String{Value: "foo"}) + ret, err := funcCall(uf, &xgo.String{Value: "foo"}) require.NoError(t, err) - require.Equal(t, &tengo.Int{Value: 5}, ret) + require.Equal(t, &xgo.Int{Value: 5}, ret) uf = stdlib.FuncASRIE(func(a string) (int, error) { return 0, errors.New("some error") }) - ret, err = funcCall(uf, &tengo.String{Value: "foo"}) + ret, err = funcCall(uf, &xgo.String{Value: "foo"}) require.NoError(t, err) require.Equal(t, - &tengo.Error{Value: &tengo.String{Value: "some error"}}, ret) + &xgo.Error{Value: &xgo.String{Value: "some error"}}, ret) _, err = funcCall(uf) - require.Equal(t, tengo.ErrWrongNumArguments, err) + require.Equal(t, xgo.ErrWrongNumArguments, err) } func TestFuncAYRIE(t *testing.T) { uf := stdlib.FuncAYRIE(func(a []byte) (int, error) { return 5, nil }) - ret, err := funcCall(uf, &tengo.Bytes{Value: []byte("foo")}) + ret, err := funcCall(uf, &xgo.Bytes{Value: []byte("foo")}) require.NoError(t, err) - require.Equal(t, &tengo.Int{Value: 5}, ret) + require.Equal(t, &xgo.Int{Value: 5}, ret) uf = stdlib.FuncAYRIE(func(a []byte) (int, error) { return 0, errors.New("some error") }) - ret, err = funcCall(uf, &tengo.Bytes{Value: []byte("foo")}) + ret, err = funcCall(uf, &xgo.Bytes{Value: []byte("foo")}) require.NoError(t, err) require.Equal(t, - &tengo.Error{Value: &tengo.String{Value: "some error"}}, ret) + &xgo.Error{Value: &xgo.String{Value: "some error"}}, ret) _, err = funcCall(uf) - require.Equal(t, tengo.ErrWrongNumArguments, err) + require.Equal(t, xgo.ErrWrongNumArguments, err) } func TestFuncASSRI(t *testing.T) { uf := stdlib.FuncASSRI(func(a, b string) int { return len(a) + len(b) }) ret, err := funcCall(uf, - &tengo.String{Value: "foo"}, &tengo.String{Value: "bar"}) + &xgo.String{Value: "foo"}, &xgo.String{Value: "bar"}) require.NoError(t, err) - require.Equal(t, &tengo.Int{Value: 6}, ret) - _, err = funcCall(uf, &tengo.String{Value: "foo"}) - require.Equal(t, tengo.ErrWrongNumArguments, err) + require.Equal(t, &xgo.Int{Value: 6}, ret) + _, err = funcCall(uf, &xgo.String{Value: "foo"}) + require.Equal(t, xgo.ErrWrongNumArguments, err) } func TestFuncASSRS(t *testing.T) { uf := stdlib.FuncASSRS(func(a, b string) string { return a + b }) ret, err := funcCall(uf, - &tengo.String{Value: "foo"}, &tengo.String{Value: "bar"}) + &xgo.String{Value: "foo"}, &xgo.String{Value: "bar"}) require.NoError(t, err) - require.Equal(t, &tengo.String{Value: "foobar"}, ret) - _, err = funcCall(uf, &tengo.String{Value: "foo"}) - require.Equal(t, tengo.ErrWrongNumArguments, err) + require.Equal(t, &xgo.String{Value: "foobar"}, ret) + _, err = funcCall(uf, &xgo.String{Value: "foo"}) + require.Equal(t, xgo.ErrWrongNumArguments, err) } func TestFuncASSRB(t *testing.T) { uf := stdlib.FuncASSRB(func(a, b string) bool { return len(a) > len(b) }) ret, err := funcCall(uf, - &tengo.String{Value: "123"}, &tengo.String{Value: "12"}) + &xgo.String{Value: "123"}, &xgo.String{Value: "12"}) require.NoError(t, err) - require.Equal(t, tengo.TrueValue, ret) - _, err = funcCall(uf, &tengo.String{Value: "foo"}) - require.Equal(t, tengo.ErrWrongNumArguments, err) + require.Equal(t, xgo.TrueValue, ret) + _, err = funcCall(uf, &xgo.String{Value: "foo"}) + require.Equal(t, xgo.ErrWrongNumArguments, err) } func TestFuncAIRS(t *testing.T) { uf := stdlib.FuncAIRS(func(a int) string { return strconv.Itoa(a) }) - ret, err := funcCall(uf, &tengo.Int{Value: 55}) + ret, err := funcCall(uf, &xgo.Int{Value: 55}) require.NoError(t, err) - require.Equal(t, &tengo.String{Value: "55"}, ret) + require.Equal(t, &xgo.String{Value: "55"}, ret) _, err = funcCall(uf) - require.Equal(t, tengo.ErrWrongNumArguments, err) + require.Equal(t, xgo.ErrWrongNumArguments, err) } func TestFuncAIRIs(t *testing.T) { uf := stdlib.FuncAIRIs(func(a int) []int { return []int{a, a} }) - ret, err := funcCall(uf, &tengo.Int{Value: 55}) + ret, err := funcCall(uf, &xgo.Int{Value: 55}) require.NoError(t, err) - require.Equal(t, array(&tengo.Int{Value: 55}, &tengo.Int{Value: 55}), ret) + require.Equal(t, array(&xgo.Int{Value: 55}, &xgo.Int{Value: 55}), ret) _, err = funcCall(uf) - require.Equal(t, tengo.ErrWrongNumArguments, err) + require.Equal(t, xgo.ErrWrongNumArguments, err) } func TestFuncAI64R(t *testing.T) { uf := stdlib.FuncAIR(func(a int) {}) - ret, err := funcCall(uf, &tengo.Int{Value: 55}) + ret, err := funcCall(uf, &xgo.Int{Value: 55}) require.NoError(t, err) - require.Equal(t, tengo.UndefinedValue, ret) + require.Equal(t, xgo.UndefinedValue, ret) _, err = funcCall(uf) - require.Equal(t, tengo.ErrWrongNumArguments, err) + require.Equal(t, xgo.ErrWrongNumArguments, err) } func TestFuncARI64(t *testing.T) { uf := stdlib.FuncARI64(func() int64 { return 55 }) ret, err := funcCall(uf) require.NoError(t, err) - require.Equal(t, &tengo.Int{Value: 55}, ret) - _, err = funcCall(uf, &tengo.Int{Value: 55}) - require.Equal(t, tengo.ErrWrongNumArguments, err) + require.Equal(t, &xgo.Int{Value: 55}, ret) + _, err = funcCall(uf, &xgo.Int{Value: 55}) + require.Equal(t, xgo.ErrWrongNumArguments, err) } func TestFuncASsSRS(t *testing.T) { @@ -540,31 +540,31 @@ func TestFuncASsSRS(t *testing.T) { return strings.Join(a, b) }) ret, err := funcCall(uf, - array(&tengo.String{Value: "abc"}, &tengo.String{Value: "def"}), - &tengo.String{Value: "-"}) + array(&xgo.String{Value: "abc"}, &xgo.String{Value: "def"}), + &xgo.String{Value: "-"}) require.NoError(t, err) - require.Equal(t, &tengo.String{Value: "abc-def"}, ret) + require.Equal(t, &xgo.String{Value: "abc-def"}, ret) _, err = funcCall(uf) - require.Equal(t, tengo.ErrWrongNumArguments, err) + require.Equal(t, xgo.ErrWrongNumArguments, err) } func TestFuncAI64RI64(t *testing.T) { uf := stdlib.FuncAI64RI64(func(a int64) int64 { return a * 2 }) - ret, err := funcCall(uf, &tengo.Int{Value: 55}) + ret, err := funcCall(uf, &xgo.Int{Value: 55}) require.NoError(t, err) - require.Equal(t, &tengo.Int{Value: 110}, ret) + require.Equal(t, &xgo.Int{Value: 110}, ret) _, err = funcCall(uf) - require.Equal(t, tengo.ErrWrongNumArguments, err) + require.Equal(t, xgo.ErrWrongNumArguments, err) } func funcCall( - fn tengo.CallableFunc, - args ...tengo.Object, -) (tengo.Object, error) { - userFunc := &tengo.UserFunction{Value: fn} + fn xgo.CallableFunc, + args ...xgo.Object, +) (xgo.Object, error) { + userFunc := &xgo.UserFunction{Value: fn} return userFunc.Call(args...) } -func array(elements ...tengo.Object) *tengo.Array { - return &tengo.Array{Value: elements} +func array(elements ...xgo.Object) *xgo.Array { + return &xgo.Array{Value: elements} } diff --git a/stdlib/gensrcmods.go b/stdlib/gensrcmods.go index bdd057f..2e6b8ef 100644 --- a/stdlib/gensrcmods.go +++ b/stdlib/gensrcmods.go @@ -10,7 +10,7 @@ import ( "strconv" ) -var tengoModFileRE = regexp.MustCompile(`^srcmod_(\w+).tengo$`) +var xgoModFileRE = regexp.MustCompile(`^srcmod_(\w+).xgo$`) func main() { modules := make(map[string]string) @@ -21,7 +21,7 @@ func main() { log.Fatal(err) } for _, file := range files { - m := tengoModFileRE.FindStringSubmatch(file.Name()) + m := xgoModFileRE.FindStringSubmatch(file.Name()) if m != nil { modName := m[1] diff --git a/stdlib/hex.go b/stdlib/hex.go index 981da69..ec6c25c 100644 --- a/stdlib/hex.go +++ b/stdlib/hex.go @@ -3,10 +3,10 @@ package stdlib import ( "encoding/hex" - "github.com/d5/tengo/v2" + "surdeus.su/core/xgo/v2" ) -var hexModule = map[string]tengo.Object{ - "encode": &tengo.UserFunction{Value: FuncAYRS(hex.EncodeToString)}, - "decode": &tengo.UserFunction{Value: FuncASRYE(hex.DecodeString)}, +var hexModule = map[string]xgo.Object{ + "encode": &xgo.UserFunction{Value: FuncAYRS(hex.EncodeToString)}, + "decode": &xgo.UserFunction{Value: FuncASRYE(hex.DecodeString)}, } diff --git a/stdlib/html/element.go b/stdlib/html/element.go new file mode 100644 index 0000000..cd26f1b --- /dev/null +++ b/stdlib/html/element.go @@ -0,0 +1,136 @@ +package html + +import ( + "surdeus.su/core/xgo/v2" + "strings" + "html" + "fmt" +) + +const RawTag = "raw" + +// The type implements basic +// way to structrize HTML elements. +type Element struct { + xgo.ObjectImpl + Tag string + Attr map[string]string + Children []*Element + // The value makes sense only if + // the tag is the "raw" + Content string + Final bool +} + +func (el *Element) TypeName() string { + return "*HTMLElement" +} + +// The method renders the element to it's +// HTML representation. +func (el *Element) String() string { + if el.Tag == RawTag { + return html.EscapeString(el.Content) + } + + var b strings.Builder + + fmt.Fprintf(&b, "<%s", el.Tag) + for k, v := range el.Attr { + fmt.Fprintf(&b, " %s=%q", k, v) + } + fmt.Fprint(&b, ">") + + if el.Final { + return b.String() + } + + for _, child := range el.Children { + if child == nil { + continue + } + fmt.Fprint(&b, child.String()) + } + + fmt.Fprintf(&b, "", el.Tag) + return b.String() +} + +func MakeElements(args ...xgo.Object) ([]*Element, error) { + s := []*Element{} + for _, arg := range args { + el, ok := arg.(*Element) + if !ok { + str, ok := xgo.ToString(arg) + if ok { + s = append(s, &Element{ + Tag: RawTag, + Content: str, + }) + } + continue + } + s = append(s, el) + } + return s, nil +} + +func (el *Element) SetBody( + args ...xgo.Object, +) (xgo.Object, error) { + els, err := MakeElements(args...) + if err != nil { + return nil, err + } + el.Children = els + return el, nil +} + +func (el *Element) Add( + args ...xgo.Object, +) (xgo.Object, error) { + s, err := MakeElements(args...) + if err != nil { + return nil, err + } + el.Children = append(el.Children, s...) + return el, nil +} + +func (el *Element) SetFinal( + args ...xgo.Object, +) (xgo.Object, error) { + if len(args) > 0 { + return nil, xgo.ErrWrongNumArguments + } + el.Final = true + return el, nil +} + +func (el *Element) IndexGet( + index xgo.Object, +) (xgo.Object, error) { + arg, ok := xgo.ToString(index) + if !ok { + return nil, xgo.ErrInvalidIndexValueType + } + + switch arg { + case "body": + return &xgo.UserFunction{ + Name: "element.body", + Value: el.SetBody, + }, nil + case "final": + return &xgo.UserFunction{ + Name: "element.final", + Value: el.SetFinal, + }, nil + case "add": + return &xgo.UserFunction{ + Name: "element.add", + Value: el.Add, + }, nil + } + return nil, nil +} diff --git a/stdlib/html/html.go b/stdlib/html/html.go new file mode 100644 index 0000000..c9f648d --- /dev/null +++ b/stdlib/html/html.go @@ -0,0 +1,101 @@ +package html + +import "surdeus.su/core/xgo/v2" + +var Module = map[string]xgo.Object{ + "Renderer": &xgo.BuiltinFunction{ + Name: "Renderer", + Value: func(args ...xgo.Object) (xgo.Object, error) { + ret := &Renderer{} + return ret, nil + }, + }, +} + +type Renderer struct { + xgo.ObjectImpl +} + +func (html *Renderer) TypeName() string { + return "html.Renderer" +} + +func (html *Renderer) String() string { + return "html.Renderer(...)" +} + +/* + +html.div({ + id: "some-el-id", + value: "shit value" +}).body( + html.raw("cock "), + html.strong("something") +) + +*/ + +func (html *Renderer) IndexGet( + index xgo.Object, +) (xgo.Object, error) { + str, ok := xgo.ToString(index) + if !ok { + return nil, xgo.ErrInvalidIndexValueType + } + + fn := func(args ...xgo.Object) (xgo.Object, error) { + if len(args) > 1 { + return nil, xgo.ErrWrongNumArguments + } + var arg xgo.Object + if len(args) == 1 { + arg = args[0] + } + + if arg == nil { + return &Element{ + Tag: str, + }, nil + } + + if can := arg.CanIterate(); !can { + return nil, xgo.ErrInvalidArgumentType{ + Name: "first", + Expected: "iterable", + Found: arg.TypeName(), + } + } + attr := map[string]string{} + iter := arg.Iterate() + for iter.Next() { + key, val := iter.Key(), iter.Value() + skey, ok := xgo.ToString(key) + if !ok { + return nil, xgo.ErrInvalidArgumentType{ + Name: "attribute(key)", + Expected: "stringer", + Found: key.TypeName(), + } + } + sval, ok := xgo.ToString(val) + if !ok { + return nil, xgo.ErrInvalidArgumentType{ + Name: "attribute(value)", + Expected: "stringer", + Found: val.TypeName(), + } + } + attr[skey] = sval + } + return &Element{ + Tag: str, + Attr: attr, + }, nil + } + + return &xgo.UserFunction{ + Name: str, + Value: fn, + }, nil +} diff --git a/stdlib/http/http.go b/stdlib/http/http.go new file mode 100644 index 0000000..0e49196 --- /dev/null +++ b/stdlib/http/http.go @@ -0,0 +1,2 @@ +package http + diff --git a/stdlib/http/module.go b/stdlib/http/module.go new file mode 100644 index 0000000..cc728d4 --- /dev/null +++ b/stdlib/http/module.go @@ -0,0 +1,70 @@ +package http + +import "surdeus.su/core/xgo/v2" +import "net/http" +import "bytes" +import tjson "surdeus.su/core/xgo/v2/stdlib/json" + +var Module = map[string]xgo.Object{ + "get": &xgo.BuiltinFunction{ + Name: "get", + Value: func( + args ...xgo.Object, + ) (xgo.Object, error) { + if len(args) != 1 { + return nil, xgo.ErrWrongNumArguments + } + url, ok := xgo.ToString(args[0]) + if !ok { + return nil, xgo.ErrInvalidArgumentType{ + Name: "first", + Expected: "string", + Found: args[0].TypeName(), + } + } + resp, err := http.Get(url) + if err != nil { + return nil, err + } + return &Response{ + Response: resp, + }, nil + }, + }, + "post_json": &xgo.BuiltinFunction{ + Name: "post", + Value: func( + args ...xgo.Object, + ) (xgo.Object, error) { + if len(args) < 2 { + return nil, xgo.ErrWrongNumArguments + } + url, ok := xgo.ToString(args[0]) + if !ok { + return nil, xgo.ErrInvalidArgumentType{ + Name: "first", + Expected: "string", + Found: args[0].TypeName(), + } + } + + var body bytes.Buffer + ret, err := tjson.Encode(args[1]) + if err != nil { + return nil, err + } + body.Write(ret) + + resp, err := http.Post( + url, "application/json", + &body, + ) + if err != nil { + return nil, err + } + return &Response{ + Response: resp, + }, nil + }, + }, +} diff --git a/stdlib/http/request.go b/stdlib/http/request.go new file mode 100644 index 0000000..af02713 --- /dev/null +++ b/stdlib/http/request.go @@ -0,0 +1,49 @@ +package http + +import ( + "surdeus.su/core/xgo/v2" + "net/http" + "io" + //"log" + xurl "surdeus.su/core/xgo/v2/stdlib/url" +) + +type Request struct { + *http.Request + xgo.ObjectImpl +} + +func (r *Request) TypeName() string { + return "http.Request" +} + +func (r *Request) String() string { + return "http.Request{...}" +} + +func (r *Request) IndexGet( + index xgo.Object, +) (xgo.Object, error) { + key, ok := xgo.ToString(index) + if !ok { + return nil, xgo.ErrInvalidIndexValueType + } + + switch key { + case "url": + return &xurl.URL{ + URL: r.URL, + }, nil + case "body": + bts, err := io.ReadAll(r.Body) + if err != nil { + return nil, err + } + return &xgo.Bytes{ + Value: bts, + }, nil + } + + // Nothing found. + return nil, nil +} diff --git a/stdlib/http/response.go b/stdlib/http/response.go new file mode 100644 index 0000000..da3de87 --- /dev/null +++ b/stdlib/http/response.go @@ -0,0 +1,49 @@ +package http + +import "net/http" +import "surdeus.su/core/xgo/v2" +import "io" +import tjson "surdeus.su/core/xgo/v2/stdlib/json" + +type Response struct { + *http.Response + xgo.ObjectImpl +} + +func (r *Response) TypeName() string { + return "http.Response" +} + +func (r *Response) String() string { + return "http.Response{...}" +} + +func (r *Response) IndexGet( + index xgo.Object, +) (xgo.Object, error) { + key, ok := xgo.ToString(index) + if !ok { + return nil, xgo.ErrInvalidIndexValueType + } + + switch key { + case "body": + bts, err := io.ReadAll(r.Body) + if err != nil { + return nil, err + } + return &xgo.Bytes{ + Value: bts, + }, nil + case "body_json": + bts, err := io.ReadAll(r.Body) + if err != nil { + return nil, err + } + + return tjson.Decode(bts) + } + + // Nothing found. + return nil, nil +} diff --git a/stdlib/json.go b/stdlib/json.go index be2185d..743c8cc 100644 --- a/stdlib/json.go +++ b/stdlib/json.go @@ -4,53 +4,53 @@ import ( "bytes" gojson "encoding/json" - "github.com/d5/tengo/v2" - "github.com/d5/tengo/v2/stdlib/json" + "surdeus.su/core/xgo/v2" + "surdeus.su/core/xgo/v2/stdlib/json" ) -var jsonModule = map[string]tengo.Object{ - "decode": &tengo.UserFunction{ +var jsonModule = map[string]xgo.Object{ + "decode": &xgo.UserFunction{ Name: "decode", Value: jsonDecode, }, - "encode": &tengo.UserFunction{ + "encode": &xgo.UserFunction{ Name: "encode", Value: jsonEncode, }, - "indent": &tengo.UserFunction{ + "indent": &xgo.UserFunction{ Name: "encode", Value: jsonIndent, }, - "html_escape": &tengo.UserFunction{ + "html_escape": &xgo.UserFunction{ Name: "html_escape", Value: jsonHTMLEscape, }, } -func jsonDecode(args ...tengo.Object) (ret tengo.Object, err error) { +func jsonDecode(args ...xgo.Object) (ret xgo.Object, err error) { if len(args) != 1 { - return nil, tengo.ErrWrongNumArguments + return nil, xgo.ErrWrongNumArguments } switch o := args[0].(type) { - case *tengo.Bytes: + case *xgo.Bytes: v, err := json.Decode(o.Value) if err != nil { - return &tengo.Error{ - Value: &tengo.String{Value: err.Error()}, + return &xgo.Error{ + Value: &xgo.String{Value: err.Error()}, }, nil } return v, nil - case *tengo.String: + case *xgo.String: v, err := json.Decode([]byte(o.Value)) if err != nil { - return &tengo.Error{ - Value: &tengo.String{Value: err.Error()}, + return &xgo.Error{ + Value: &xgo.String{Value: err.Error()}, }, nil } return v, nil default: - return nil, tengo.ErrInvalidArgumentType{ + return nil, xgo.ErrInvalidArgumentType{ Name: "first", Expected: "bytes/string", Found: args[0].TypeName(), @@ -58,36 +58,36 @@ func jsonDecode(args ...tengo.Object) (ret tengo.Object, err error) { } } -func jsonEncode(args ...tengo.Object) (ret tengo.Object, err error) { +func jsonEncode(args ...xgo.Object) (ret xgo.Object, err error) { if len(args) != 1 { - return nil, tengo.ErrWrongNumArguments + return nil, xgo.ErrWrongNumArguments } b, err := json.Encode(args[0]) if err != nil { - return &tengo.Error{Value: &tengo.String{Value: err.Error()}}, nil + return &xgo.Error{Value: &xgo.String{Value: err.Error()}}, nil } - return &tengo.Bytes{Value: b}, nil + return &xgo.Bytes{Value: b}, nil } -func jsonIndent(args ...tengo.Object) (ret tengo.Object, err error) { +func jsonIndent(args ...xgo.Object) (ret xgo.Object, err error) { if len(args) != 3 { - return nil, tengo.ErrWrongNumArguments + return nil, xgo.ErrWrongNumArguments } - prefix, ok := tengo.ToString(args[1]) + prefix, ok := xgo.ToString(args[1]) if !ok { - return nil, tengo.ErrInvalidArgumentType{ + return nil, xgo.ErrInvalidArgumentType{ Name: "prefix", Expected: "string(compatible)", Found: args[1].TypeName(), } } - indent, ok := tengo.ToString(args[2]) + indent, ok := xgo.ToString(args[2]) if !ok { - return nil, tengo.ErrInvalidArgumentType{ + return nil, xgo.ErrInvalidArgumentType{ Name: "indent", Expected: "string(compatible)", Found: args[2].TypeName(), @@ -95,26 +95,26 @@ func jsonIndent(args ...tengo.Object) (ret tengo.Object, err error) { } switch o := args[0].(type) { - case *tengo.Bytes: + case *xgo.Bytes: var dst bytes.Buffer err := gojson.Indent(&dst, o.Value, prefix, indent) if err != nil { - return &tengo.Error{ - Value: &tengo.String{Value: err.Error()}, + return &xgo.Error{ + Value: &xgo.String{Value: err.Error()}, }, nil } - return &tengo.Bytes{Value: dst.Bytes()}, nil - case *tengo.String: + return &xgo.Bytes{Value: dst.Bytes()}, nil + case *xgo.String: var dst bytes.Buffer err := gojson.Indent(&dst, []byte(o.Value), prefix, indent) if err != nil { - return &tengo.Error{ - Value: &tengo.String{Value: err.Error()}, + return &xgo.Error{ + Value: &xgo.String{Value: err.Error()}, }, nil } - return &tengo.Bytes{Value: dst.Bytes()}, nil + return &xgo.Bytes{Value: dst.Bytes()}, nil default: - return nil, tengo.ErrInvalidArgumentType{ + return nil, xgo.ErrInvalidArgumentType{ Name: "first", Expected: "bytes/string", Found: args[0].TypeName(), @@ -122,22 +122,22 @@ func jsonIndent(args ...tengo.Object) (ret tengo.Object, err error) { } } -func jsonHTMLEscape(args ...tengo.Object) (ret tengo.Object, err error) { +func jsonHTMLEscape(args ...xgo.Object) (ret xgo.Object, err error) { if len(args) != 1 { - return nil, tengo.ErrWrongNumArguments + return nil, xgo.ErrWrongNumArguments } switch o := args[0].(type) { - case *tengo.Bytes: + case *xgo.Bytes: var dst bytes.Buffer gojson.HTMLEscape(&dst, o.Value) - return &tengo.Bytes{Value: dst.Bytes()}, nil - case *tengo.String: + return &xgo.Bytes{Value: dst.Bytes()}, nil + case *xgo.String: var dst bytes.Buffer gojson.HTMLEscape(&dst, []byte(o.Value)) - return &tengo.Bytes{Value: dst.Bytes()}, nil + return &xgo.Bytes{Value: dst.Bytes()}, nil default: - return nil, tengo.ErrInvalidArgumentType{ + return nil, xgo.ErrInvalidArgumentType{ Name: "first", Expected: "bytes/string", Found: args[0].TypeName(), diff --git a/stdlib/json/decode.go b/stdlib/json/decode.go index 557fb9e..20d7184 100644 --- a/stdlib/json/decode.go +++ b/stdlib/json/decode.go @@ -12,11 +12,11 @@ import ( "unicode/utf16" "unicode/utf8" - "github.com/d5/tengo/v2" + "surdeus.su/core/xgo/v2" ) // Decode parses the JSON-encoded data and returns the result object. -func Decode(data []byte) (tengo.Object, error) { +func Decode(data []byte) (xgo.Object, error) { var d decodeState err := checkValid(data, &d.scan) if err != nil { @@ -82,7 +82,7 @@ func (d *decodeState) scanWhile(op int) (isFloat bool) { return } -func (d *decodeState) value() (tengo.Object, error) { +func (d *decodeState) value() (xgo.Object, error) { switch d.opcode { default: panic(phasePanicMsg) @@ -105,8 +105,8 @@ func (d *decodeState) value() (tengo.Object, error) { } } -func (d *decodeState) array() (tengo.Object, error) { - var arr []tengo.Object +func (d *decodeState) array() (xgo.Object, error) { + var arr []xgo.Object for { // Look ahead for ] - can only happen on first iteration. d.scanWhile(scanSkipSpace) @@ -130,11 +130,11 @@ func (d *decodeState) array() (tengo.Object, error) { panic(phasePanicMsg) } } - return &tengo.Array{Value: arr}, nil + return &xgo.Array{Value: arr}, nil } -func (d *decodeState) object() (tengo.Object, error) { - m := make(map[string]tengo.Object) +func (d *decodeState) object() (xgo.Object, error) { + m := make(map[string]xgo.Object) for { // Read opening " of string key or closing }. d.scanWhile(scanSkipSpace) @@ -183,10 +183,10 @@ func (d *decodeState) object() (tengo.Object, error) { panic(phasePanicMsg) } } - return &tengo.Map{Value: m}, nil + return &xgo.Map{Value: m}, nil } -func (d *decodeState) literal() (tengo.Object, error) { +func (d *decodeState) literal() (xgo.Object, error) { // All bytes inside literal return scanContinue op code. start := d.readIndex() isFloat := d.scanWhile(scanContinue) @@ -195,20 +195,20 @@ func (d *decodeState) literal() (tengo.Object, error) { switch c := item[0]; c { case 'n': // null - return tengo.UndefinedValue, nil + return xgo.UndefinedValue, nil case 't', 'f': // true, false if c == 't' { - return tengo.TrueValue, nil + return xgo.TrueValue, nil } - return tengo.FalseValue, nil + return xgo.FalseValue, nil case '"': // string s, ok := unquote(item) if !ok { panic(phasePanicMsg) } - return &tengo.String{Value: s}, nil + return &xgo.String{Value: s}, nil default: // number if c != '-' && (c < '0' || c > '9') { @@ -216,10 +216,10 @@ func (d *decodeState) literal() (tengo.Object, error) { } if isFloat { n, _ := strconv.ParseFloat(string(item), 10) - return &tengo.Float{Value: n}, nil + return &xgo.Float{Value: n}, nil } n, _ := strconv.ParseInt(string(item), 10, 64) - return &tengo.Int{Value: n}, nil + return &xgo.Int{Value: n}, nil } } diff --git a/stdlib/json/encode.go b/stdlib/json/encode.go index 10805b0..9e2fd8c 100644 --- a/stdlib/json/encode.go +++ b/stdlib/json/encode.go @@ -14,7 +14,7 @@ import ( "strconv" "unicode/utf8" - "github.com/d5/tengo/v2" + "surdeus.su/core/xgo/v2" ) // safeSet holds the value true if the ASCII character with the given array @@ -125,11 +125,11 @@ var safeSet = [utf8.RuneSelf]bool{ var hex = "0123456789abcdef" // Encode returns the JSON encoding of the object. -func Encode(o tengo.Object) ([]byte, error) { +func Encode(o xgo.Object) ([]byte, error) { var b []byte switch o := o.(type) { - case *tengo.Array: + case *xgo.Array: b = append(b, '[') len1 := len(o.Value) - 1 for idx, elem := range o.Value { @@ -143,7 +143,7 @@ func Encode(o tengo.Object) ([]byte, error) { } } b = append(b, ']') - case *tengo.ImmutableArray: + case *xgo.ImmutableArray: b = append(b, '[') len1 := len(o.Value) - 1 for idx, elem := range o.Value { @@ -157,7 +157,7 @@ func Encode(o tengo.Object) ([]byte, error) { } } b = append(b, ']') - case *tengo.Map: + case *xgo.Map: b = append(b, '{') len1 := len(o.Value) - 1 idx := 0 @@ -175,7 +175,7 @@ func Encode(o tengo.Object) ([]byte, error) { idx++ } b = append(b, '}') - case *tengo.ImmutableMap: + case *xgo.ImmutableMap: b = append(b, '{') len1 := len(o.Value) - 1 idx := 0 @@ -193,22 +193,22 @@ func Encode(o tengo.Object) ([]byte, error) { idx++ } b = append(b, '}') - case *tengo.Bool: + case *xgo.Bool: if o.IsFalsy() { b = strconv.AppendBool(b, false) } else { b = strconv.AppendBool(b, true) } - case *tengo.Bytes: + case *xgo.Bytes: b = append(b, '"') encodedLen := base64.StdEncoding.EncodedLen(len(o.Value)) dst := make([]byte, encodedLen) base64.StdEncoding.Encode(dst, o.Value) b = append(b, dst...) b = append(b, '"') - case *tengo.Char: + case *xgo.Char: b = strconv.AppendInt(b, int64(o.Value), 10) - case *tengo.Float: + case *xgo.Float: var y []byte f := o.Value @@ -236,19 +236,19 @@ func Encode(o tengo.Object) ([]byte, error) { } b = append(b, y...) - case *tengo.Int: + case *xgo.Int: b = strconv.AppendInt(b, o.Value, 10) - case *tengo.String: + case *xgo.String: // string encoding bug is fixed with newly introduced function - // encodeString(). See: https://github.com/d5/tengo/issues/268 + // encodeString(). See: https://github.com/d5/xgo/issues/268 b = encodeString(b, o.Value) - case *tengo.Time: + case *xgo.Time: y, err := o.Value.MarshalJSON() if err != nil { return nil, err } b = append(b, y...) - case *tengo.Undefined: + case *xgo.Undefined: b = append(b, "null"...) default: // unknown type: ignore diff --git a/stdlib/json/json_test.go b/stdlib/json/json_test.go index 031307c..9085709 100644 --- a/stdlib/json/json_test.go +++ b/stdlib/json/json_test.go @@ -4,9 +4,9 @@ import ( gojson "encoding/json" "testing" - "github.com/d5/tengo/v2" - "github.com/d5/tengo/v2/require" - "github.com/d5/tengo/v2/stdlib/json" + "surdeus.su/core/xgo/v2" + "surdeus.su/core/xgo/v2/require" + "surdeus.su/core/xgo/v2/stdlib/json" ) type ARR = []interface{} @@ -31,7 +31,7 @@ func TestJSON(t *testing.T) { testJSONEncodeDecode(t, "foo") testJSONEncodeDecode(t, "foo bar") testJSONEncodeDecode(t, "foo \"bar\"") - // See: https://github.com/d5/tengo/issues/268 + // See: https://github.com/d5/xgo/issues/268 testJSONEncodeDecode(t, "1\u001C04") testJSONEncodeDecode(t, "çığöşü") testJSONEncodeDecode(t, "ç1\u001C04IĞÖŞÜ") @@ -92,7 +92,7 @@ func testDecodeError(t *testing.T, input string) { } func testJSONEncodeDecode(t *testing.T, v interface{}) { - o, err := tengo.FromInterface(v) + o, err := xgo.FromInterface(v) require.NoError(t, err) b, err := json.Encode(o) @@ -104,7 +104,7 @@ func testJSONEncodeDecode(t *testing.T, v interface{}) { vj, err := gojson.Marshal(v) require.NoError(t, err) - aj, err := gojson.Marshal(tengo.ToInterface(a)) + aj, err := gojson.Marshal(xgo.ToInterface(a)) require.NoError(t, err) require.Equal(t, vj, aj) diff --git a/stdlib/log/main.go b/stdlib/log/main.go new file mode 100644 index 0000000..78fab5d --- /dev/null +++ b/stdlib/log/main.go @@ -0,0 +1,76 @@ +package log + +import "surdeus.su/core/xgo/v2" +import "log" + +var Module = map[string]xgo.Object{ + "printf": &xgo.BuiltinFunction{ + Name: "printf", + Value: func( + args ...xgo.Object, + ) (xgo.Object, error) { + if len(args) < 1 { + return nil, xgo.ErrWrongNumArguments + } + + formatObject := args[0] + format, hasFormat := xgo.ToString( + formatObject, + ) + if !hasFormat { + return nil, xgo.ErrInvalidArgumentType{ + Name: "first", + Expected: "string", + Found: formatObject.TypeName(), + } + } + + /*v := make([]any, len(args[1:])) + for i, o := range args[1:] { + v[i] = xgo.ToInterface(o) + }*/ + + str, err := xgo.Format(format, args...) + if err != nil { + return nil, err + } + log.Print(str) + + return nil, nil + }, + }, + "print": &xgo.BuiltinFunction{ + Name: "print", + Value: func( + args ...xgo.Object, + ) (xgo.Object, error) { + v := make([]any, len(args)) + for i, o := range args { + var ok bool + v[i], ok = xgo.ToString(o) + if !ok { + v[i] = xgo.Undefined{} + } + } + log.Print(v...) + return nil, nil + }, + }, + "println": &xgo.BuiltinFunction{ + Name: "println", + Value: func( + args ...xgo.Object, + ) (xgo.Object, error) { + v := make([]any, len(args)) + for i, o := range args { + var ok bool + v[i], ok = xgo.ToString(o) + if !ok { + v[i] = xgo.Undefined{} + } + } + log.Println(v...) + return nil, nil + }, + }, +} diff --git a/stdlib/math.go b/stdlib/math.go index 1bd9ba6..311c323 100644 --- a/stdlib/math.go +++ b/stdlib/math.go @@ -3,244 +3,244 @@ package stdlib import ( "math" - "github.com/d5/tengo/v2" + "surdeus.su/core/xgo/v2" ) -var mathModule = map[string]tengo.Object{ - "e": &tengo.Float{Value: math.E}, - "pi": &tengo.Float{Value: math.Pi}, - "phi": &tengo.Float{Value: math.Phi}, - "sqrt2": &tengo.Float{Value: math.Sqrt2}, - "sqrtE": &tengo.Float{Value: math.SqrtE}, - "sqrtPi": &tengo.Float{Value: math.SqrtPi}, - "sqrtPhi": &tengo.Float{Value: math.SqrtPhi}, - "ln2": &tengo.Float{Value: math.Ln2}, - "log2E": &tengo.Float{Value: math.Log2E}, - "ln10": &tengo.Float{Value: math.Ln10}, - "log10E": &tengo.Float{Value: math.Log10E}, - "maxFloat32": &tengo.Float{Value: math.MaxFloat32}, - "smallestNonzeroFloat32": &tengo.Float{Value: math.SmallestNonzeroFloat32}, - "maxFloat64": &tengo.Float{Value: math.MaxFloat64}, - "smallestNonzeroFloat64": &tengo.Float{Value: math.SmallestNonzeroFloat64}, - "maxInt": &tengo.Int{Value: math.MaxInt}, - "minInt": &tengo.Int{Value: math.MinInt}, - "maxInt8": &tengo.Int{Value: math.MaxInt8}, - "minInt8": &tengo.Int{Value: math.MinInt8}, - "maxInt16": &tengo.Int{Value: math.MaxInt16}, - "minInt16": &tengo.Int{Value: math.MinInt16}, - "maxInt32": &tengo.Int{Value: math.MaxInt32}, - "minInt32": &tengo.Int{Value: math.MinInt32}, - "maxInt64": &tengo.Int{Value: math.MaxInt64}, - "minInt64": &tengo.Int{Value: math.MinInt64}, - "abs": &tengo.UserFunction{ +var mathModule = map[string]xgo.Object{ + "e": &xgo.Float{Value: math.E}, + "pi": &xgo.Float{Value: math.Pi}, + "phi": &xgo.Float{Value: math.Phi}, + "sqrt2": &xgo.Float{Value: math.Sqrt2}, + "sqrtE": &xgo.Float{Value: math.SqrtE}, + "sqrtPi": &xgo.Float{Value: math.SqrtPi}, + "sqrtPhi": &xgo.Float{Value: math.SqrtPhi}, + "ln2": &xgo.Float{Value: math.Ln2}, + "log2E": &xgo.Float{Value: math.Log2E}, + "ln10": &xgo.Float{Value: math.Ln10}, + "log10E": &xgo.Float{Value: math.Log10E}, + "maxFloat32": &xgo.Float{Value: math.MaxFloat32}, + "smallestNonzeroFloat32": &xgo.Float{Value: math.SmallestNonzeroFloat32}, + "maxFloat64": &xgo.Float{Value: math.MaxFloat64}, + "smallestNonzeroFloat64": &xgo.Float{Value: math.SmallestNonzeroFloat64}, + "maxInt": &xgo.Int{Value: math.MaxInt}, + "minInt": &xgo.Int{Value: math.MinInt}, + "maxInt8": &xgo.Int{Value: math.MaxInt8}, + "minInt8": &xgo.Int{Value: math.MinInt8}, + "maxInt16": &xgo.Int{Value: math.MaxInt16}, + "minInt16": &xgo.Int{Value: math.MinInt16}, + "maxInt32": &xgo.Int{Value: math.MaxInt32}, + "minInt32": &xgo.Int{Value: math.MinInt32}, + "maxInt64": &xgo.Int{Value: math.MaxInt64}, + "minInt64": &xgo.Int{Value: math.MinInt64}, + "abs": &xgo.UserFunction{ Name: "abs", Value: FuncAFRF(math.Abs), }, - "acos": &tengo.UserFunction{ + "acos": &xgo.UserFunction{ Name: "acos", Value: FuncAFRF(math.Acos), }, - "acosh": &tengo.UserFunction{ + "acosh": &xgo.UserFunction{ Name: "acosh", Value: FuncAFRF(math.Acosh), }, - "asin": &tengo.UserFunction{ + "asin": &xgo.UserFunction{ Name: "asin", Value: FuncAFRF(math.Asin), }, - "asinh": &tengo.UserFunction{ + "asinh": &xgo.UserFunction{ Name: "asinh", Value: FuncAFRF(math.Asinh), }, - "atan": &tengo.UserFunction{ + "atan": &xgo.UserFunction{ Name: "atan", Value: FuncAFRF(math.Atan), }, - "atan2": &tengo.UserFunction{ + "atan2": &xgo.UserFunction{ Name: "atan2", Value: FuncAFFRF(math.Atan2), }, - "atanh": &tengo.UserFunction{ + "atanh": &xgo.UserFunction{ Name: "atanh", Value: FuncAFRF(math.Atanh), }, - "cbrt": &tengo.UserFunction{ + "cbrt": &xgo.UserFunction{ Name: "cbrt", Value: FuncAFRF(math.Cbrt), }, - "ceil": &tengo.UserFunction{ + "ceil": &xgo.UserFunction{ Name: "ceil", Value: FuncAFRF(math.Ceil), }, - "copysign": &tengo.UserFunction{ + "copysign": &xgo.UserFunction{ Name: "copysign", Value: FuncAFFRF(math.Copysign), }, - "cos": &tengo.UserFunction{ + "cos": &xgo.UserFunction{ Name: "cos", Value: FuncAFRF(math.Cos), }, - "cosh": &tengo.UserFunction{ + "cosh": &xgo.UserFunction{ Name: "cosh", Value: FuncAFRF(math.Cosh), }, - "dim": &tengo.UserFunction{ + "dim": &xgo.UserFunction{ Name: "dim", Value: FuncAFFRF(math.Dim), }, - "erf": &tengo.UserFunction{ + "erf": &xgo.UserFunction{ Name: "erf", Value: FuncAFRF(math.Erf), }, - "erfc": &tengo.UserFunction{ + "erfc": &xgo.UserFunction{ Name: "erfc", Value: FuncAFRF(math.Erfc), }, - "exp": &tengo.UserFunction{ + "exp": &xgo.UserFunction{ Name: "exp", Value: FuncAFRF(math.Exp), }, - "exp2": &tengo.UserFunction{ + "exp2": &xgo.UserFunction{ Name: "exp2", Value: FuncAFRF(math.Exp2), }, - "expm1": &tengo.UserFunction{ + "expm1": &xgo.UserFunction{ Name: "expm1", Value: FuncAFRF(math.Expm1), }, - "floor": &tengo.UserFunction{ + "floor": &xgo.UserFunction{ Name: "floor", Value: FuncAFRF(math.Floor), }, - "gamma": &tengo.UserFunction{ + "gamma": &xgo.UserFunction{ Name: "gamma", Value: FuncAFRF(math.Gamma), }, - "hypot": &tengo.UserFunction{ + "hypot": &xgo.UserFunction{ Name: "hypot", Value: FuncAFFRF(math.Hypot), }, - "ilogb": &tengo.UserFunction{ + "ilogb": &xgo.UserFunction{ Name: "ilogb", Value: FuncAFRI(math.Ilogb), }, - "inf": &tengo.UserFunction{ + "inf": &xgo.UserFunction{ Name: "inf", Value: FuncAIRF(math.Inf), }, - "is_inf": &tengo.UserFunction{ + "is_inf": &xgo.UserFunction{ Name: "is_inf", Value: FuncAFIRB(math.IsInf), }, - "is_nan": &tengo.UserFunction{ + "is_nan": &xgo.UserFunction{ Name: "is_nan", Value: FuncAFRB(math.IsNaN), }, - "j0": &tengo.UserFunction{ + "j0": &xgo.UserFunction{ Name: "j0", Value: FuncAFRF(math.J0), }, - "j1": &tengo.UserFunction{ + "j1": &xgo.UserFunction{ Name: "j1", Value: FuncAFRF(math.J1), }, - "jn": &tengo.UserFunction{ + "jn": &xgo.UserFunction{ Name: "jn", Value: FuncAIFRF(math.Jn), }, - "ldexp": &tengo.UserFunction{ + "ldexp": &xgo.UserFunction{ Name: "ldexp", Value: FuncAFIRF(math.Ldexp), }, - "log": &tengo.UserFunction{ + "log": &xgo.UserFunction{ Name: "log", Value: FuncAFRF(math.Log), }, - "log10": &tengo.UserFunction{ + "log10": &xgo.UserFunction{ Name: "log10", Value: FuncAFRF(math.Log10), }, - "log1p": &tengo.UserFunction{ + "log1p": &xgo.UserFunction{ Name: "log1p", Value: FuncAFRF(math.Log1p), }, - "log2": &tengo.UserFunction{ + "log2": &xgo.UserFunction{ Name: "log2", Value: FuncAFRF(math.Log2), }, - "logb": &tengo.UserFunction{ + "logb": &xgo.UserFunction{ Name: "logb", Value: FuncAFRF(math.Logb), }, - "max": &tengo.UserFunction{ + "max": &xgo.UserFunction{ Name: "max", Value: FuncAFFRF(math.Max), }, - "min": &tengo.UserFunction{ + "min": &xgo.UserFunction{ Name: "min", Value: FuncAFFRF(math.Min), }, - "mod": &tengo.UserFunction{ + "mod": &xgo.UserFunction{ Name: "mod", Value: FuncAFFRF(math.Mod), }, - "nan": &tengo.UserFunction{ + "nan": &xgo.UserFunction{ Name: "nan", Value: FuncARF(math.NaN), }, - "nextafter": &tengo.UserFunction{ + "nextafter": &xgo.UserFunction{ Name: "nextafter", Value: FuncAFFRF(math.Nextafter), }, - "pow": &tengo.UserFunction{ + "pow": &xgo.UserFunction{ Name: "pow", Value: FuncAFFRF(math.Pow), }, - "pow10": &tengo.UserFunction{ + "pow10": &xgo.UserFunction{ Name: "pow10", Value: FuncAIRF(math.Pow10), }, - "remainder": &tengo.UserFunction{ + "remainder": &xgo.UserFunction{ Name: "remainder", Value: FuncAFFRF(math.Remainder), }, - "signbit": &tengo.UserFunction{ + "signbit": &xgo.UserFunction{ Name: "signbit", Value: FuncAFRB(math.Signbit), }, - "sin": &tengo.UserFunction{ + "sin": &xgo.UserFunction{ Name: "sin", Value: FuncAFRF(math.Sin), }, - "sinh": &tengo.UserFunction{ + "sinh": &xgo.UserFunction{ Name: "sinh", Value: FuncAFRF(math.Sinh), }, - "sqrt": &tengo.UserFunction{ + "sqrt": &xgo.UserFunction{ Name: "sqrt", Value: FuncAFRF(math.Sqrt), }, - "tan": &tengo.UserFunction{ + "tan": &xgo.UserFunction{ Name: "tan", Value: FuncAFRF(math.Tan), }, - "tanh": &tengo.UserFunction{ + "tanh": &xgo.UserFunction{ Name: "tanh", Value: FuncAFRF(math.Tanh), }, - "trunc": &tengo.UserFunction{ + "trunc": &xgo.UserFunction{ Name: "trunc", Value: FuncAFRF(math.Trunc), }, - "y0": &tengo.UserFunction{ + "y0": &xgo.UserFunction{ Name: "y0", Value: FuncAFRF(math.Y0), }, - "y1": &tengo.UserFunction{ + "y1": &xgo.UserFunction{ Name: "y1", Value: FuncAFRF(math.Y1), }, - "yn": &tengo.UserFunction{ + "yn": &xgo.UserFunction{ Name: "yn", Value: FuncAIFRF(math.Yn), }, diff --git a/stdlib/os.go b/stdlib/os.go index 5af4b60..aa96042 100644 --- a/stdlib/os.go +++ b/stdlib/os.go @@ -8,208 +8,208 @@ import ( "os/exec" "runtime" - "github.com/d5/tengo/v2" + "surdeus.su/core/xgo/v2" ) -var osModule = map[string]tengo.Object{ - "platform": &tengo.String{Value: runtime.GOOS}, - "arch": &tengo.String{Value: runtime.GOARCH}, - "o_rdonly": &tengo.Int{Value: int64(os.O_RDONLY)}, - "o_wronly": &tengo.Int{Value: int64(os.O_WRONLY)}, - "o_rdwr": &tengo.Int{Value: int64(os.O_RDWR)}, - "o_append": &tengo.Int{Value: int64(os.O_APPEND)}, - "o_create": &tengo.Int{Value: int64(os.O_CREATE)}, - "o_excl": &tengo.Int{Value: int64(os.O_EXCL)}, - "o_sync": &tengo.Int{Value: int64(os.O_SYNC)}, - "o_trunc": &tengo.Int{Value: int64(os.O_TRUNC)}, - "mode_dir": &tengo.Int{Value: int64(os.ModeDir)}, - "mode_append": &tengo.Int{Value: int64(os.ModeAppend)}, - "mode_exclusive": &tengo.Int{Value: int64(os.ModeExclusive)}, - "mode_temporary": &tengo.Int{Value: int64(os.ModeTemporary)}, - "mode_symlink": &tengo.Int{Value: int64(os.ModeSymlink)}, - "mode_device": &tengo.Int{Value: int64(os.ModeDevice)}, - "mode_named_pipe": &tengo.Int{Value: int64(os.ModeNamedPipe)}, - "mode_socket": &tengo.Int{Value: int64(os.ModeSocket)}, - "mode_setuid": &tengo.Int{Value: int64(os.ModeSetuid)}, - "mode_setgui": &tengo.Int{Value: int64(os.ModeSetgid)}, - "mode_char_device": &tengo.Int{Value: int64(os.ModeCharDevice)}, - "mode_sticky": &tengo.Int{Value: int64(os.ModeSticky)}, - "mode_type": &tengo.Int{Value: int64(os.ModeType)}, - "mode_perm": &tengo.Int{Value: int64(os.ModePerm)}, - "path_separator": &tengo.Char{Value: os.PathSeparator}, - "path_list_separator": &tengo.Char{Value: os.PathListSeparator}, - "dev_null": &tengo.String{Value: os.DevNull}, - "seek_set": &tengo.Int{Value: int64(io.SeekStart)}, - "seek_cur": &tengo.Int{Value: int64(io.SeekCurrent)}, - "seek_end": &tengo.Int{Value: int64(io.SeekEnd)}, - "args": &tengo.UserFunction{ +var osModule = map[string]xgo.Object{ + "platform": &xgo.String{Value: runtime.GOOS}, + "arch": &xgo.String{Value: runtime.GOARCH}, + "o_rdonly": &xgo.Int{Value: int64(os.O_RDONLY)}, + "o_wronly": &xgo.Int{Value: int64(os.O_WRONLY)}, + "o_rdwr": &xgo.Int{Value: int64(os.O_RDWR)}, + "o_append": &xgo.Int{Value: int64(os.O_APPEND)}, + "o_create": &xgo.Int{Value: int64(os.O_CREATE)}, + "o_excl": &xgo.Int{Value: int64(os.O_EXCL)}, + "o_sync": &xgo.Int{Value: int64(os.O_SYNC)}, + "o_trunc": &xgo.Int{Value: int64(os.O_TRUNC)}, + "mode_dir": &xgo.Int{Value: int64(os.ModeDir)}, + "mode_append": &xgo.Int{Value: int64(os.ModeAppend)}, + "mode_exclusive": &xgo.Int{Value: int64(os.ModeExclusive)}, + "mode_temporary": &xgo.Int{Value: int64(os.ModeTemporary)}, + "mode_symlink": &xgo.Int{Value: int64(os.ModeSymlink)}, + "mode_device": &xgo.Int{Value: int64(os.ModeDevice)}, + "mode_named_pipe": &xgo.Int{Value: int64(os.ModeNamedPipe)}, + "mode_socket": &xgo.Int{Value: int64(os.ModeSocket)}, + "mode_setuid": &xgo.Int{Value: int64(os.ModeSetuid)}, + "mode_setgui": &xgo.Int{Value: int64(os.ModeSetgid)}, + "mode_char_device": &xgo.Int{Value: int64(os.ModeCharDevice)}, + "mode_sticky": &xgo.Int{Value: int64(os.ModeSticky)}, + "mode_type": &xgo.Int{Value: int64(os.ModeType)}, + "mode_perm": &xgo.Int{Value: int64(os.ModePerm)}, + "path_separator": &xgo.Char{Value: os.PathSeparator}, + "path_list_separator": &xgo.Char{Value: os.PathListSeparator}, + "dev_null": &xgo.String{Value: os.DevNull}, + "seek_set": &xgo.Int{Value: int64(io.SeekStart)}, + "seek_cur": &xgo.Int{Value: int64(io.SeekCurrent)}, + "seek_end": &xgo.Int{Value: int64(io.SeekEnd)}, + "args": &xgo.UserFunction{ Name: "args", Value: osArgs, }, // args() => array(string) - "chdir": &tengo.UserFunction{ + "chdir": &xgo.UserFunction{ Name: "chdir", Value: FuncASRE(os.Chdir), }, // chdir(dir string) => error "chmod": osFuncASFmRE("chmod", os.Chmod), // chmod(name string, mode int) => error - "chown": &tengo.UserFunction{ + "chown": &xgo.UserFunction{ Name: "chown", Value: FuncASIIRE(os.Chown), }, // chown(name string, uid int, gid int) => error - "clearenv": &tengo.UserFunction{ + "clearenv": &xgo.UserFunction{ Name: "clearenv", Value: FuncAR(os.Clearenv), }, // clearenv() - "environ": &tengo.UserFunction{ + "environ": &xgo.UserFunction{ Name: "environ", Value: FuncARSs(os.Environ), }, // environ() => array(string) - "exit": &tengo.UserFunction{ + "exit": &xgo.UserFunction{ Name: "exit", Value: FuncAIR(os.Exit), }, // exit(code int) - "expand_env": &tengo.UserFunction{ + "expand_env": &xgo.UserFunction{ Name: "expand_env", Value: osExpandEnv, }, // expand_env(s string) => string - "getegid": &tengo.UserFunction{ + "getegid": &xgo.UserFunction{ Name: "getegid", Value: FuncARI(os.Getegid), }, // getegid() => int - "getenv": &tengo.UserFunction{ + "getenv": &xgo.UserFunction{ Name: "getenv", Value: FuncASRS(os.Getenv), }, // getenv(s string) => string - "geteuid": &tengo.UserFunction{ + "geteuid": &xgo.UserFunction{ Name: "geteuid", Value: FuncARI(os.Geteuid), }, // geteuid() => int - "getgid": &tengo.UserFunction{ + "getgid": &xgo.UserFunction{ Name: "getgid", Value: FuncARI(os.Getgid), }, // getgid() => int - "getgroups": &tengo.UserFunction{ + "getgroups": &xgo.UserFunction{ Name: "getgroups", Value: FuncARIsE(os.Getgroups), }, // getgroups() => array(string)/error - "getpagesize": &tengo.UserFunction{ + "getpagesize": &xgo.UserFunction{ Name: "getpagesize", Value: FuncARI(os.Getpagesize), }, // getpagesize() => int - "getpid": &tengo.UserFunction{ + "getpid": &xgo.UserFunction{ Name: "getpid", Value: FuncARI(os.Getpid), }, // getpid() => int - "getppid": &tengo.UserFunction{ + "getppid": &xgo.UserFunction{ Name: "getppid", Value: FuncARI(os.Getppid), }, // getppid() => int - "getuid": &tengo.UserFunction{ + "getuid": &xgo.UserFunction{ Name: "getuid", Value: FuncARI(os.Getuid), }, // getuid() => int - "getwd": &tengo.UserFunction{ + "getwd": &xgo.UserFunction{ Name: "getwd", Value: FuncARSE(os.Getwd), }, // getwd() => string/error - "hostname": &tengo.UserFunction{ + "hostname": &xgo.UserFunction{ Name: "hostname", Value: FuncARSE(os.Hostname), }, // hostname() => string/error - "lchown": &tengo.UserFunction{ + "lchown": &xgo.UserFunction{ Name: "lchown", Value: FuncASIIRE(os.Lchown), }, // lchown(name string, uid int, gid int) => error - "link": &tengo.UserFunction{ + "link": &xgo.UserFunction{ Name: "link", Value: FuncASSRE(os.Link), }, // link(oldname string, newname string) => error - "lookup_env": &tengo.UserFunction{ + "lookup_env": &xgo.UserFunction{ Name: "lookup_env", Value: osLookupEnv, }, // lookup_env(key string) => string/false "mkdir": osFuncASFmRE("mkdir", os.Mkdir), // mkdir(name string, perm int) => error "mkdir_all": osFuncASFmRE("mkdir_all", os.MkdirAll), // mkdir_all(name string, perm int) => error - "readlink": &tengo.UserFunction{ + "readlink": &xgo.UserFunction{ Name: "readlink", Value: FuncASRSE(os.Readlink), }, // readlink(name string) => string/error - "remove": &tengo.UserFunction{ + "remove": &xgo.UserFunction{ Name: "remove", Value: FuncASRE(os.Remove), }, // remove(name string) => error - "remove_all": &tengo.UserFunction{ + "remove_all": &xgo.UserFunction{ Name: "remove_all", Value: FuncASRE(os.RemoveAll), }, // remove_all(name string) => error - "rename": &tengo.UserFunction{ + "rename": &xgo.UserFunction{ Name: "rename", Value: FuncASSRE(os.Rename), }, // rename(oldpath string, newpath string) => error - "setenv": &tengo.UserFunction{ + "setenv": &xgo.UserFunction{ Name: "setenv", Value: FuncASSRE(os.Setenv), }, // setenv(key string, value string) => error - "symlink": &tengo.UserFunction{ + "symlink": &xgo.UserFunction{ Name: "symlink", Value: FuncASSRE(os.Symlink), }, // symlink(oldname string newname string) => error - "temp_dir": &tengo.UserFunction{ + "temp_dir": &xgo.UserFunction{ Name: "temp_dir", Value: FuncARS(os.TempDir), }, // temp_dir() => string - "truncate": &tengo.UserFunction{ + "truncate": &xgo.UserFunction{ Name: "truncate", Value: FuncASI64RE(os.Truncate), }, // truncate(name string, size int) => error - "unsetenv": &tengo.UserFunction{ + "unsetenv": &xgo.UserFunction{ Name: "unsetenv", Value: FuncASRE(os.Unsetenv), }, // unsetenv(key string) => error - "create": &tengo.UserFunction{ + "create": &xgo.UserFunction{ Name: "create", Value: osCreate, }, // create(name string) => imap(file)/error - "open": &tengo.UserFunction{ + "open": &xgo.UserFunction{ Name: "open", Value: osOpen, }, // open(name string) => imap(file)/error - "open_file": &tengo.UserFunction{ + "open_file": &xgo.UserFunction{ Name: "open_file", Value: osOpenFile, }, // open_file(name string, flag int, perm int) => imap(file)/error - "find_process": &tengo.UserFunction{ + "find_process": &xgo.UserFunction{ Name: "find_process", Value: osFindProcess, }, // find_process(pid int) => imap(process)/error - "start_process": &tengo.UserFunction{ + "start_process": &xgo.UserFunction{ Name: "start_process", Value: osStartProcess, }, // start_process(name string, argv array(string), dir string, env array(string)) => imap(process)/error - "exec_look_path": &tengo.UserFunction{ + "exec_look_path": &xgo.UserFunction{ Name: "exec_look_path", Value: FuncASRSE(exec.LookPath), }, // exec_look_path(file) => string/error - "exec": &tengo.UserFunction{ + "exec": &xgo.UserFunction{ Name: "exec", Value: osExec, }, // exec(name, args...) => command - "stat": &tengo.UserFunction{ + "stat": &xgo.UserFunction{ Name: "stat", Value: osStat, }, // stat(name) => imap(fileinfo)/error - "read_file": &tengo.UserFunction{ + "read_file": &xgo.UserFunction{ Name: "read_file", Value: osReadFile, }, // readfile(name) => array(byte)/error } -func osReadFile(args ...tengo.Object) (ret tengo.Object, err error) { +func osReadFile(args ...xgo.Object) (ret xgo.Object, err error) { if len(args) != 1 { - return nil, tengo.ErrWrongNumArguments + return nil, xgo.ErrWrongNumArguments } - fname, ok := tengo.ToString(args[0]) + fname, ok := xgo.ToString(args[0]) if !ok { - return nil, tengo.ErrInvalidArgumentType{ + return nil, xgo.ErrInvalidArgumentType{ Name: "first", Expected: "string(compatible)", Found: args[0].TypeName(), @@ -219,19 +219,19 @@ func osReadFile(args ...tengo.Object) (ret tengo.Object, err error) { if err != nil { return wrapError(err), nil } - if len(bytes) > tengo.MaxBytesLen { - return nil, tengo.ErrBytesLimit + if len(bytes) > xgo.MaxBytesLen { + return nil, xgo.ErrBytesLimit } - return &tengo.Bytes{Value: bytes}, nil + return &xgo.Bytes{Value: bytes}, nil } -func osStat(args ...tengo.Object) (ret tengo.Object, err error) { +func osStat(args ...xgo.Object) (ret xgo.Object, err error) { if len(args) != 1 { - return nil, tengo.ErrWrongNumArguments + return nil, xgo.ErrWrongNumArguments } - fname, ok := tengo.ToString(args[0]) + fname, ok := xgo.ToString(args[0]) if !ok { - return nil, tengo.ErrInvalidArgumentType{ + return nil, xgo.ErrInvalidArgumentType{ Name: "first", Expected: "string(compatible)", Found: args[0].TypeName(), @@ -241,29 +241,29 @@ func osStat(args ...tengo.Object) (ret tengo.Object, err error) { if err != nil { return wrapError(err), nil } - fstat := &tengo.ImmutableMap{ - Value: map[string]tengo.Object{ - "name": &tengo.String{Value: stat.Name()}, - "mtime": &tengo.Time{Value: stat.ModTime()}, - "size": &tengo.Int{Value: stat.Size()}, - "mode": &tengo.Int{Value: int64(stat.Mode())}, + fstat := &xgo.ImmutableMap{ + Value: map[string]xgo.Object{ + "name": &xgo.String{Value: stat.Name()}, + "mtime": &xgo.Time{Value: stat.ModTime()}, + "size": &xgo.Int{Value: stat.Size()}, + "mode": &xgo.Int{Value: int64(stat.Mode())}, }, } if stat.IsDir() { - fstat.Value["directory"] = tengo.TrueValue + fstat.Value["directory"] = xgo.TrueValue } else { - fstat.Value["directory"] = tengo.FalseValue + fstat.Value["directory"] = xgo.FalseValue } return fstat, nil } -func osCreate(args ...tengo.Object) (tengo.Object, error) { +func osCreate(args ...xgo.Object) (xgo.Object, error) { if len(args) != 1 { - return nil, tengo.ErrWrongNumArguments + return nil, xgo.ErrWrongNumArguments } - s1, ok := tengo.ToString(args[0]) + s1, ok := xgo.ToString(args[0]) if !ok { - return nil, tengo.ErrInvalidArgumentType{ + return nil, xgo.ErrInvalidArgumentType{ Name: "first", Expected: "string(compatible)", Found: args[0].TypeName(), @@ -276,13 +276,13 @@ func osCreate(args ...tengo.Object) (tengo.Object, error) { return makeOSFile(res), nil } -func osOpen(args ...tengo.Object) (tengo.Object, error) { +func osOpen(args ...xgo.Object) (xgo.Object, error) { if len(args) != 1 { - return nil, tengo.ErrWrongNumArguments + return nil, xgo.ErrWrongNumArguments } - s1, ok := tengo.ToString(args[0]) + s1, ok := xgo.ToString(args[0]) if !ok { - return nil, tengo.ErrInvalidArgumentType{ + return nil, xgo.ErrInvalidArgumentType{ Name: "first", Expected: "string(compatible)", Found: args[0].TypeName(), @@ -295,29 +295,29 @@ func osOpen(args ...tengo.Object) (tengo.Object, error) { return makeOSFile(res), nil } -func osOpenFile(args ...tengo.Object) (tengo.Object, error) { +func osOpenFile(args ...xgo.Object) (xgo.Object, error) { if len(args) != 3 { - return nil, tengo.ErrWrongNumArguments + return nil, xgo.ErrWrongNumArguments } - s1, ok := tengo.ToString(args[0]) + s1, ok := xgo.ToString(args[0]) if !ok { - return nil, tengo.ErrInvalidArgumentType{ + return nil, xgo.ErrInvalidArgumentType{ Name: "first", Expected: "string(compatible)", Found: args[0].TypeName(), } } - i2, ok := tengo.ToInt(args[1]) + i2, ok := xgo.ToInt(args[1]) if !ok { - return nil, tengo.ErrInvalidArgumentType{ + return nil, xgo.ErrInvalidArgumentType{ Name: "second", Expected: "int(compatible)", Found: args[1].TypeName(), } } - i3, ok := tengo.ToInt(args[2]) + i3, ok := xgo.ToInt(args[2]) if !ok { - return nil, tengo.ErrInvalidArgumentType{ + return nil, xgo.ErrInvalidArgumentType{ Name: "third", Expected: "int(compatible)", Found: args[2].TypeName(), @@ -330,16 +330,16 @@ func osOpenFile(args ...tengo.Object) (tengo.Object, error) { return makeOSFile(res), nil } -func osArgs(args ...tengo.Object) (tengo.Object, error) { +func osArgs(args ...xgo.Object) (xgo.Object, error) { if len(args) != 0 { - return nil, tengo.ErrWrongNumArguments + return nil, xgo.ErrWrongNumArguments } - arr := &tengo.Array{} + arr := &xgo.Array{} for _, osArg := range os.Args { - if len(osArg) > tengo.MaxStringLen { - return nil, tengo.ErrStringLimit + if len(osArg) > xgo.MaxStringLen { + return nil, xgo.ErrStringLimit } - arr.Value = append(arr.Value, &tengo.String{Value: osArg}) + arr.Value = append(arr.Value, &xgo.String{Value: osArg}) } return arr, nil } @@ -347,24 +347,24 @@ func osArgs(args ...tengo.Object) (tengo.Object, error) { func osFuncASFmRE( name string, fn func(string, os.FileMode) error, -) *tengo.UserFunction { - return &tengo.UserFunction{ +) *xgo.UserFunction { + return &xgo.UserFunction{ Name: name, - Value: func(args ...tengo.Object) (tengo.Object, error) { + Value: func(args ...xgo.Object) (xgo.Object, error) { if len(args) != 2 { - return nil, tengo.ErrWrongNumArguments + return nil, xgo.ErrWrongNumArguments } - s1, ok := tengo.ToString(args[0]) + s1, ok := xgo.ToString(args[0]) if !ok { - return nil, tengo.ErrInvalidArgumentType{ + return nil, xgo.ErrInvalidArgumentType{ Name: "first", Expected: "string(compatible)", Found: args[0].TypeName(), } } - i2, ok := tengo.ToInt64(args[1]) + i2, ok := xgo.ToInt64(args[1]) if !ok { - return nil, tengo.ErrInvalidArgumentType{ + return nil, xgo.ErrInvalidArgumentType{ Name: "second", Expected: "int(compatible)", Found: args[1].TypeName(), @@ -375,13 +375,13 @@ func osFuncASFmRE( } } -func osLookupEnv(args ...tengo.Object) (tengo.Object, error) { +func osLookupEnv(args ...xgo.Object) (xgo.Object, error) { if len(args) != 1 { - return nil, tengo.ErrWrongNumArguments + return nil, xgo.ErrWrongNumArguments } - s1, ok := tengo.ToString(args[0]) + s1, ok := xgo.ToString(args[0]) if !ok { - return nil, tengo.ErrInvalidArgumentType{ + return nil, xgo.ErrInvalidArgumentType{ Name: "first", Expected: "string(compatible)", Found: args[0].TypeName(), @@ -389,21 +389,21 @@ func osLookupEnv(args ...tengo.Object) (tengo.Object, error) { } res, ok := os.LookupEnv(s1) if !ok { - return tengo.FalseValue, nil + return xgo.FalseValue, nil } - if len(res) > tengo.MaxStringLen { - return nil, tengo.ErrStringLimit + if len(res) > xgo.MaxStringLen { + return nil, xgo.ErrStringLimit } - return &tengo.String{Value: res}, nil + return &xgo.String{Value: res}, nil } -func osExpandEnv(args ...tengo.Object) (tengo.Object, error) { +func osExpandEnv(args ...xgo.Object) (xgo.Object, error) { if len(args) != 1 { - return nil, tengo.ErrWrongNumArguments + return nil, xgo.ErrWrongNumArguments } - s1, ok := tengo.ToString(args[0]) + s1, ok := xgo.ToString(args[0]) if !ok { - return nil, tengo.ErrInvalidArgumentType{ + return nil, xgo.ErrInvalidArgumentType{ Name: "first", Expected: "string(compatible)", Found: args[0].TypeName(), @@ -420,25 +420,25 @@ func osExpandEnv(args ...tengo.Object) (tengo.Object, error) { // this does not count the other texts that are not being replaced // but the code checks the final length at the end vlen += len(v) - if vlen > tengo.MaxStringLen { + if vlen > xgo.MaxStringLen { failed = true return "" } return v }) - if failed || len(s) > tengo.MaxStringLen { - return nil, tengo.ErrStringLimit + if failed || len(s) > xgo.MaxStringLen { + return nil, xgo.ErrStringLimit } - return &tengo.String{Value: s}, nil + return &xgo.String{Value: s}, nil } -func osExec(args ...tengo.Object) (tengo.Object, error) { +func osExec(args ...xgo.Object) (xgo.Object, error) { if len(args) == 0 { - return nil, tengo.ErrWrongNumArguments + return nil, xgo.ErrWrongNumArguments } - name, ok := tengo.ToString(args[0]) + name, ok := xgo.ToString(args[0]) if !ok { - return nil, tengo.ErrInvalidArgumentType{ + return nil, xgo.ErrInvalidArgumentType{ Name: "first", Expected: "string(compatible)", Found: args[0].TypeName(), @@ -446,9 +446,9 @@ func osExec(args ...tengo.Object) (tengo.Object, error) { } var execArgs []string for idx, arg := range args[1:] { - execArg, ok := tengo.ToString(arg) + execArg, ok := xgo.ToString(arg) if !ok { - return nil, tengo.ErrInvalidArgumentType{ + return nil, xgo.ErrInvalidArgumentType{ Name: fmt.Sprintf("args[%d]", idx), Expected: "string(compatible)", Found: args[1+idx].TypeName(), @@ -459,13 +459,13 @@ func osExec(args ...tengo.Object) (tengo.Object, error) { return makeOSExecCommand(exec.Command(name, execArgs...)), nil } -func osFindProcess(args ...tengo.Object) (tengo.Object, error) { +func osFindProcess(args ...xgo.Object) (xgo.Object, error) { if len(args) != 1 { - return nil, tengo.ErrWrongNumArguments + return nil, xgo.ErrWrongNumArguments } - i1, ok := tengo.ToInt(args[0]) + i1, ok := xgo.ToInt(args[0]) if !ok { - return nil, tengo.ErrInvalidArgumentType{ + return nil, xgo.ErrInvalidArgumentType{ Name: "first", Expected: "int(compatible)", Found: args[0].TypeName(), @@ -478,13 +478,13 @@ func osFindProcess(args ...tengo.Object) (tengo.Object, error) { return makeOSProcess(proc), nil } -func osStartProcess(args ...tengo.Object) (tengo.Object, error) { +func osStartProcess(args ...xgo.Object) (xgo.Object, error) { if len(args) != 4 { - return nil, tengo.ErrWrongNumArguments + return nil, xgo.ErrWrongNumArguments } - name, ok := tengo.ToString(args[0]) + name, ok := xgo.ToString(args[0]) if !ok { - return nil, tengo.ErrInvalidArgumentType{ + return nil, xgo.ErrInvalidArgumentType{ Name: "first", Expected: "string(compatible)", Found: args[0].TypeName(), @@ -493,27 +493,27 @@ func osStartProcess(args ...tengo.Object) (tengo.Object, error) { var argv []string var err error switch arg1 := args[1].(type) { - case *tengo.Array: + case *xgo.Array: argv, err = stringArray(arg1.Value, "second") if err != nil { return nil, err } - case *tengo.ImmutableArray: + case *xgo.ImmutableArray: argv, err = stringArray(arg1.Value, "second") if err != nil { return nil, err } default: - return nil, tengo.ErrInvalidArgumentType{ + return nil, xgo.ErrInvalidArgumentType{ Name: "second", Expected: "array", Found: arg1.TypeName(), } } - dir, ok := tengo.ToString(args[2]) + dir, ok := xgo.ToString(args[2]) if !ok { - return nil, tengo.ErrInvalidArgumentType{ + return nil, xgo.ErrInvalidArgumentType{ Name: "third", Expected: "string(compatible)", Found: args[2].TypeName(), @@ -522,18 +522,18 @@ func osStartProcess(args ...tengo.Object) (tengo.Object, error) { var env []string switch arg3 := args[3].(type) { - case *tengo.Array: + case *xgo.Array: env, err = stringArray(arg3.Value, "fourth") if err != nil { return nil, err } - case *tengo.ImmutableArray: + case *xgo.ImmutableArray: env, err = stringArray(arg3.Value, "fourth") if err != nil { return nil, err } default: - return nil, tengo.ErrInvalidArgumentType{ + return nil, xgo.ErrInvalidArgumentType{ Name: "fourth", Expected: "array", Found: arg3.TypeName(), @@ -550,12 +550,12 @@ func osStartProcess(args ...tengo.Object) (tengo.Object, error) { return makeOSProcess(proc), nil } -func stringArray(arr []tengo.Object, argName string) ([]string, error) { +func stringArray(arr []xgo.Object, argName string) ([]string, error) { var sarr []string for idx, elem := range arr { - str, ok := elem.(*tengo.String) + str, ok := elem.(*xgo.String) if !ok { - return nil, tengo.ErrInvalidArgumentType{ + return nil, xgo.ErrInvalidArgumentType{ Name: fmt.Sprintf("%s[%d]", argName, idx), Expected: "string", Found: elem.TypeName(), diff --git a/stdlib/os_exec.go b/stdlib/os_exec.go index 7ee5c1c..1c9c0b3 100644 --- a/stdlib/os_exec.go +++ b/stdlib/os_exec.go @@ -3,113 +3,113 @@ package stdlib import ( "os/exec" - "github.com/d5/tengo/v2" + "surdeus.su/core/xgo/v2" ) -func makeOSExecCommand(cmd *exec.Cmd) *tengo.ImmutableMap { - return &tengo.ImmutableMap{ - Value: map[string]tengo.Object{ +func makeOSExecCommand(cmd *exec.Cmd) *xgo.ImmutableMap { + return &xgo.ImmutableMap{ + Value: map[string]xgo.Object{ // combined_output() => bytes/error - "combined_output": &tengo.UserFunction{ + "combined_output": &xgo.UserFunction{ Name: "combined_output", Value: FuncARYE(cmd.CombinedOutput), }, // output() => bytes/error - "output": &tengo.UserFunction{ + "output": &xgo.UserFunction{ Name: "output", Value: FuncARYE(cmd.Output), }, // // run() => error - "run": &tengo.UserFunction{ + "run": &xgo.UserFunction{ Name: "run", Value: FuncARE(cmd.Run), }, // // start() => error - "start": &tengo.UserFunction{ + "start": &xgo.UserFunction{ Name: "start", Value: FuncARE(cmd.Start), }, // // wait() => error - "wait": &tengo.UserFunction{ + "wait": &xgo.UserFunction{ Name: "wait", Value: FuncARE(cmd.Wait), }, // // set_path(path string) - "set_path": &tengo.UserFunction{ + "set_path": &xgo.UserFunction{ Name: "set_path", - Value: func(args ...tengo.Object) (tengo.Object, error) { + Value: func(args ...xgo.Object) (xgo.Object, error) { if len(args) != 1 { - return nil, tengo.ErrWrongNumArguments + return nil, xgo.ErrWrongNumArguments } - s1, ok := tengo.ToString(args[0]) + s1, ok := xgo.ToString(args[0]) if !ok { - return nil, tengo.ErrInvalidArgumentType{ + return nil, xgo.ErrInvalidArgumentType{ Name: "first", Expected: "string(compatible)", Found: args[0].TypeName(), } } cmd.Path = s1 - return tengo.UndefinedValue, nil + return xgo.UndefinedValue, nil }, }, // set_dir(dir string) - "set_dir": &tengo.UserFunction{ + "set_dir": &xgo.UserFunction{ Name: "set_dir", - Value: func(args ...tengo.Object) (tengo.Object, error) { + Value: func(args ...xgo.Object) (xgo.Object, error) { if len(args) != 1 { - return nil, tengo.ErrWrongNumArguments + return nil, xgo.ErrWrongNumArguments } - s1, ok := tengo.ToString(args[0]) + s1, ok := xgo.ToString(args[0]) if !ok { - return nil, tengo.ErrInvalidArgumentType{ + return nil, xgo.ErrInvalidArgumentType{ Name: "first", Expected: "string(compatible)", Found: args[0].TypeName(), } } cmd.Dir = s1 - return tengo.UndefinedValue, nil + return xgo.UndefinedValue, nil }, }, // set_env(env array(string)) - "set_env": &tengo.UserFunction{ + "set_env": &xgo.UserFunction{ Name: "set_env", - Value: func(args ...tengo.Object) (tengo.Object, error) { + Value: func(args ...xgo.Object) (xgo.Object, error) { if len(args) != 1 { - return nil, tengo.ErrWrongNumArguments + return nil, xgo.ErrWrongNumArguments } var env []string var err error switch arg0 := args[0].(type) { - case *tengo.Array: + case *xgo.Array: env, err = stringArray(arg0.Value, "first") if err != nil { return nil, err } - case *tengo.ImmutableArray: + case *xgo.ImmutableArray: env, err = stringArray(arg0.Value, "first") if err != nil { return nil, err } default: - return nil, tengo.ErrInvalidArgumentType{ + return nil, xgo.ErrInvalidArgumentType{ Name: "first", Expected: "array", Found: arg0.TypeName(), } } cmd.Env = env - return tengo.UndefinedValue, nil + return xgo.UndefinedValue, nil }, }, // process() => imap(process) - "process": &tengo.UserFunction{ + "process": &xgo.UserFunction{ Name: "process", - Value: func(args ...tengo.Object) (tengo.Object, error) { + Value: func(args ...xgo.Object) (xgo.Object, error) { if len(args) != 0 { - return nil, tengo.ErrWrongNumArguments + return nil, xgo.ErrWrongNumArguments } return makeOSProcess(cmd.Process), nil }, diff --git a/stdlib/os_file.go b/stdlib/os_file.go index 4f59b4c..b2acdcf 100644 --- a/stdlib/os_file.go +++ b/stdlib/os_file.go @@ -3,67 +3,67 @@ package stdlib import ( "os" - "github.com/d5/tengo/v2" + "surdeus.su/core/xgo/v2" ) -func makeOSFile(file *os.File) *tengo.ImmutableMap { - return &tengo.ImmutableMap{ - Value: map[string]tengo.Object{ +func makeOSFile(file *os.File) *xgo.ImmutableMap { + return &xgo.ImmutableMap{ + Value: map[string]xgo.Object{ // chdir() => true/error - "chdir": &tengo.UserFunction{ + "chdir": &xgo.UserFunction{ Name: "chdir", Value: FuncARE(file.Chdir), }, // // chown(uid int, gid int) => true/error - "chown": &tengo.UserFunction{ + "chown": &xgo.UserFunction{ Name: "chown", Value: FuncAIIRE(file.Chown), }, // // close() => error - "close": &tengo.UserFunction{ + "close": &xgo.UserFunction{ Name: "close", Value: FuncARE(file.Close), }, // // name() => string - "name": &tengo.UserFunction{ + "name": &xgo.UserFunction{ Name: "name", Value: FuncARS(file.Name), }, // // readdirnames(n int) => array(string)/error - "readdirnames": &tengo.UserFunction{ + "readdirnames": &xgo.UserFunction{ Name: "readdirnames", Value: FuncAIRSsE(file.Readdirnames), }, // // sync() => error - "sync": &tengo.UserFunction{ + "sync": &xgo.UserFunction{ Name: "sync", Value: FuncARE(file.Sync), }, // // write(bytes) => int/error - "write": &tengo.UserFunction{ + "write": &xgo.UserFunction{ Name: "write", Value: FuncAYRIE(file.Write), }, // // write(string) => int/error - "write_string": &tengo.UserFunction{ + "write_string": &xgo.UserFunction{ Name: "write_string", Value: FuncASRIE(file.WriteString), }, // // read(bytes) => int/error - "read": &tengo.UserFunction{ + "read": &xgo.UserFunction{ Name: "read", Value: FuncAYRIE(file.Read), }, // // chmod(mode int) => error - "chmod": &tengo.UserFunction{ + "chmod": &xgo.UserFunction{ Name: "chmod", - Value: func(args ...tengo.Object) (tengo.Object, error) { + Value: func(args ...xgo.Object) (xgo.Object, error) { if len(args) != 1 { - return nil, tengo.ErrWrongNumArguments + return nil, xgo.ErrWrongNumArguments } - i1, ok := tengo.ToInt64(args[0]) + i1, ok := xgo.ToInt64(args[0]) if !ok { - return nil, tengo.ErrInvalidArgumentType{ + return nil, xgo.ErrInvalidArgumentType{ Name: "first", Expected: "int(compatible)", Found: args[0].TypeName(), @@ -73,23 +73,23 @@ func makeOSFile(file *os.File) *tengo.ImmutableMap { }, }, // seek(offset int, whence int) => int/error - "seek": &tengo.UserFunction{ + "seek": &xgo.UserFunction{ Name: "seek", - Value: func(args ...tengo.Object) (tengo.Object, error) { + Value: func(args ...xgo.Object) (xgo.Object, error) { if len(args) != 2 { - return nil, tengo.ErrWrongNumArguments + return nil, xgo.ErrWrongNumArguments } - i1, ok := tengo.ToInt64(args[0]) + i1, ok := xgo.ToInt64(args[0]) if !ok { - return nil, tengo.ErrInvalidArgumentType{ + return nil, xgo.ErrInvalidArgumentType{ Name: "first", Expected: "int(compatible)", Found: args[0].TypeName(), } } - i2, ok := tengo.ToInt(args[1]) + i2, ok := xgo.ToInt(args[1]) if !ok { - return nil, tengo.ErrInvalidArgumentType{ + return nil, xgo.ErrInvalidArgumentType{ Name: "second", Expected: "int(compatible)", Found: args[1].TypeName(), @@ -99,17 +99,17 @@ func makeOSFile(file *os.File) *tengo.ImmutableMap { if err != nil { return wrapError(err), nil } - return &tengo.Int{Value: res}, nil + return &xgo.Int{Value: res}, nil }, }, // stat() => imap(fileinfo)/error - "stat": &tengo.UserFunction{ + "stat": &xgo.UserFunction{ Name: "stat", - Value: func(args ...tengo.Object) (tengo.Object, error) { + Value: func(args ...xgo.Object) (xgo.Object, error) { if len(args) != 0 { - return nil, tengo.ErrWrongNumArguments + return nil, xgo.ErrWrongNumArguments } - return osStat(&tengo.String{Value: file.Name()}) + return osStat(&xgo.String{Value: file.Name()}) }, }, }, diff --git a/stdlib/os_process.go b/stdlib/os_process.go index 7fcf27a..68b42b4 100644 --- a/stdlib/os_process.go +++ b/stdlib/os_process.go @@ -4,25 +4,25 @@ import ( "os" "syscall" - "github.com/d5/tengo/v2" + "surdeus.su/core/xgo/v2" ) -func makeOSProcessState(state *os.ProcessState) *tengo.ImmutableMap { - return &tengo.ImmutableMap{ - Value: map[string]tengo.Object{ - "exited": &tengo.UserFunction{ +func makeOSProcessState(state *os.ProcessState) *xgo.ImmutableMap { + return &xgo.ImmutableMap{ + Value: map[string]xgo.Object{ + "exited": &xgo.UserFunction{ Name: "exited", Value: FuncARB(state.Exited), }, - "pid": &tengo.UserFunction{ + "pid": &xgo.UserFunction{ Name: "pid", Value: FuncARI(state.Pid), }, - "string": &tengo.UserFunction{ + "string": &xgo.UserFunction{ Name: "string", Value: FuncARS(state.String), }, - "success": &tengo.UserFunction{ + "success": &xgo.UserFunction{ Name: "success", Value: FuncARB(state.Success), }, @@ -30,26 +30,26 @@ func makeOSProcessState(state *os.ProcessState) *tengo.ImmutableMap { } } -func makeOSProcess(proc *os.Process) *tengo.ImmutableMap { - return &tengo.ImmutableMap{ - Value: map[string]tengo.Object{ - "kill": &tengo.UserFunction{ +func makeOSProcess(proc *os.Process) *xgo.ImmutableMap { + return &xgo.ImmutableMap{ + Value: map[string]xgo.Object{ + "kill": &xgo.UserFunction{ Name: "kill", Value: FuncARE(proc.Kill), }, - "release": &tengo.UserFunction{ + "release": &xgo.UserFunction{ Name: "release", Value: FuncARE(proc.Release), }, - "signal": &tengo.UserFunction{ + "signal": &xgo.UserFunction{ Name: "signal", - Value: func(args ...tengo.Object) (tengo.Object, error) { + Value: func(args ...xgo.Object) (xgo.Object, error) { if len(args) != 1 { - return nil, tengo.ErrWrongNumArguments + return nil, xgo.ErrWrongNumArguments } - i1, ok := tengo.ToInt64(args[0]) + i1, ok := xgo.ToInt64(args[0]) if !ok { - return nil, tengo.ErrInvalidArgumentType{ + return nil, xgo.ErrInvalidArgumentType{ Name: "first", Expected: "int(compatible)", Found: args[0].TypeName(), @@ -58,11 +58,11 @@ func makeOSProcess(proc *os.Process) *tengo.ImmutableMap { return wrapError(proc.Signal(syscall.Signal(i1))), nil }, }, - "wait": &tengo.UserFunction{ + "wait": &xgo.UserFunction{ Name: "wait", - Value: func(args ...tengo.Object) (tengo.Object, error) { + Value: func(args ...xgo.Object) (xgo.Object, error) { if len(args) != 0 { - return nil, tengo.ErrWrongNumArguments + return nil, xgo.ErrWrongNumArguments } state, err := proc.Wait() if err != nil { diff --git a/stdlib/os_test.go b/stdlib/os_test.go index 46c7f60..e868f0d 100644 --- a/stdlib/os_test.go +++ b/stdlib/os_test.go @@ -5,8 +5,8 @@ import ( "os" "testing" - "github.com/d5/tengo/v2" - "github.com/d5/tengo/v2/require" + "surdeus.su/core/xgo/v2" + "surdeus.su/core/xgo/v2/require" ) func TestReadFile(t *testing.T) { @@ -20,7 +20,7 @@ func TestReadFile(t *testing.T) { _ = tf.Close() module(t, "os").call("read_file", tf.Name()). - expect(&tengo.Bytes{Value: content}) + expect(&xgo.Bytes{Value: content}) } func TestReadFileArgs(t *testing.T) { @@ -46,13 +46,13 @@ func TestFileStatFile(t *testing.T) { return } - module(t, "os").call("stat", tf.Name()).expect(&tengo.ImmutableMap{ - Value: map[string]tengo.Object{ - "name": &tengo.String{Value: stat.Name()}, - "mtime": &tengo.Time{Value: stat.ModTime()}, - "size": &tengo.Int{Value: stat.Size()}, - "mode": &tengo.Int{Value: int64(stat.Mode())}, - "directory": tengo.FalseValue, + module(t, "os").call("stat", tf.Name()).expect(&xgo.ImmutableMap{ + Value: map[string]xgo.Object{ + "name": &xgo.String{Value: stat.Name()}, + "mtime": &xgo.Time{Value: stat.ModTime()}, + "size": &xgo.Int{Value: stat.Size()}, + "mode": &xgo.Int{Value: int64(stat.Mode())}, + "directory": xgo.FalseValue, }, }) } @@ -65,21 +65,21 @@ func TestFileStatDir(t *testing.T) { stat, err := os.Stat(td) require.NoError(t, err) - module(t, "os").call("stat", td).expect(&tengo.ImmutableMap{ - Value: map[string]tengo.Object{ - "name": &tengo.String{Value: stat.Name()}, - "mtime": &tengo.Time{Value: stat.ModTime()}, - "size": &tengo.Int{Value: stat.Size()}, - "mode": &tengo.Int{Value: int64(stat.Mode())}, - "directory": tengo.TrueValue, + module(t, "os").call("stat", td).expect(&xgo.ImmutableMap{ + Value: map[string]xgo.Object{ + "name": &xgo.String{Value: stat.Name()}, + "mtime": &xgo.Time{Value: stat.ModTime()}, + "size": &xgo.Int{Value: stat.Size()}, + "mode": &xgo.Int{Value: int64(stat.Mode())}, + "directory": xgo.TrueValue, }, }) } func TestOSExpandEnv(t *testing.T) { - curMaxStringLen := tengo.MaxStringLen - defer func() { tengo.MaxStringLen = curMaxStringLen }() - tengo.MaxStringLen = 12 + curMaxStringLen := xgo.MaxStringLen + defer func() { xgo.MaxStringLen = curMaxStringLen }() + xgo.MaxStringLen = 12 _ = os.Setenv("TENGO", "FOO BAR") module(t, "os").call("expand_env", "$TENGO").expect("FOO BAR") diff --git a/stdlib/paths/main.go b/stdlib/paths/main.go new file mode 100644 index 0000000..4815d44 --- /dev/null +++ b/stdlib/paths/main.go @@ -0,0 +1,46 @@ +package paths + +import ( + "surdeus.su/core/xgo/v2" + "path" +) + +var Module = map[string]xgo.Object{ + "base": &xgo.UserFunction{ + Name: "base", + Value: wrapOneArg(path.Base), + }, + "ext": &xgo.UserFunction{ + Name: "ext", + Value: wrapOneArg(path.Ext), + }, + "dir": &xgo.UserFunction{ + Name: "dir", + Value: wrapOneArg(path.Dir), + }, + "clean": &xgo.UserFunction{ + Name: "clean", + Value: wrapOneArg(path.Clean), + }, +} + +func wrapOneArg( + fn func(string) string, +) func(...xgo.Object) (xgo.Object, error) { + return func(args ...xgo.Object) (xgo.Object, error) { + if len(args) != 1 { + return nil, xgo.ErrWrongNumArguments + } + str, ok := xgo.ToString(args[0]) + if !ok { + return nil, xgo.ErrInvalidArgumentType{ + Name: "first", + Expected: "string(compatible)", + Found: args[0].TypeName(), + } + } + return &xgo.String{ + Value: fn(str), + }, nil + } +} diff --git a/stdlib/rand.go b/stdlib/rand.go index 5d21e1d..b07a3b9 100644 --- a/stdlib/rand.go +++ b/stdlib/rand.go @@ -3,47 +3,47 @@ package stdlib import ( "math/rand" - "github.com/d5/tengo/v2" + "surdeus.su/core/xgo/v2" ) -var randModule = map[string]tengo.Object{ - "int": &tengo.UserFunction{ +var randModule = map[string]xgo.Object{ + "int": &xgo.UserFunction{ Name: "int", Value: FuncARI64(rand.Int63), }, - "float": &tengo.UserFunction{ + "float": &xgo.UserFunction{ Name: "float", Value: FuncARF(rand.Float64), }, - "intn": &tengo.UserFunction{ + "intn": &xgo.UserFunction{ Name: "intn", Value: FuncAI64RI64(rand.Int63n), }, - "exp_float": &tengo.UserFunction{ + "exp_float": &xgo.UserFunction{ Name: "exp_float", Value: FuncARF(rand.ExpFloat64), }, - "norm_float": &tengo.UserFunction{ + "norm_float": &xgo.UserFunction{ Name: "norm_float", Value: FuncARF(rand.NormFloat64), }, - "perm": &tengo.UserFunction{ + "perm": &xgo.UserFunction{ Name: "perm", Value: FuncAIRIs(rand.Perm), }, - "seed": &tengo.UserFunction{ + "seed": &xgo.UserFunction{ Name: "seed", Value: FuncAI64R(rand.Seed), }, - "read": &tengo.UserFunction{ + "read": &xgo.UserFunction{ Name: "read", - Value: func(args ...tengo.Object) (ret tengo.Object, err error) { + Value: func(args ...xgo.Object) (ret xgo.Object, err error) { if len(args) != 1 { - return nil, tengo.ErrWrongNumArguments + return nil, xgo.ErrWrongNumArguments } - y1, ok := args[0].(*tengo.Bytes) + y1, ok := args[0].(*xgo.Bytes) if !ok { - return nil, tengo.ErrInvalidArgumentType{ + return nil, xgo.ErrInvalidArgumentType{ Name: "first", Expected: "bytes", Found: args[0].TypeName(), @@ -54,18 +54,18 @@ var randModule = map[string]tengo.Object{ ret = wrapError(err) return } - return &tengo.Int{Value: int64(res)}, nil + return &xgo.Int{Value: int64(res)}, nil }, }, - "rand": &tengo.UserFunction{ + "rand": &xgo.UserFunction{ Name: "rand", - Value: func(args ...tengo.Object) (tengo.Object, error) { + Value: func(args ...xgo.Object) (xgo.Object, error) { if len(args) != 1 { - return nil, tengo.ErrWrongNumArguments + return nil, xgo.ErrWrongNumArguments } - i1, ok := tengo.ToInt64(args[0]) + i1, ok := xgo.ToInt64(args[0]) if !ok { - return nil, tengo.ErrInvalidArgumentType{ + return nil, xgo.ErrInvalidArgumentType{ Name: "first", Expected: "int(compatible)", Found: args[0].TypeName(), @@ -77,49 +77,49 @@ var randModule = map[string]tengo.Object{ }, } -func randRand(r *rand.Rand) *tengo.ImmutableMap { - return &tengo.ImmutableMap{ - Value: map[string]tengo.Object{ - "int": &tengo.UserFunction{ +func randRand(r *rand.Rand) *xgo.ImmutableMap { + return &xgo.ImmutableMap{ + Value: map[string]xgo.Object{ + "int": &xgo.UserFunction{ Name: "int", Value: FuncARI64(r.Int63), }, - "float": &tengo.UserFunction{ + "float": &xgo.UserFunction{ Name: "float", Value: FuncARF(r.Float64), }, - "intn": &tengo.UserFunction{ + "intn": &xgo.UserFunction{ Name: "intn", Value: FuncAI64RI64(r.Int63n), }, - "exp_float": &tengo.UserFunction{ + "exp_float": &xgo.UserFunction{ Name: "exp_float", Value: FuncARF(r.ExpFloat64), }, - "norm_float": &tengo.UserFunction{ + "norm_float": &xgo.UserFunction{ Name: "norm_float", Value: FuncARF(r.NormFloat64), }, - "perm": &tengo.UserFunction{ + "perm": &xgo.UserFunction{ Name: "perm", Value: FuncAIRIs(r.Perm), }, - "seed": &tengo.UserFunction{ + "seed": &xgo.UserFunction{ Name: "seed", Value: FuncAI64R(r.Seed), }, - "read": &tengo.UserFunction{ + "read": &xgo.UserFunction{ Name: "read", - Value: func(args ...tengo.Object) ( - ret tengo.Object, + Value: func(args ...xgo.Object) ( + ret xgo.Object, err error, ) { if len(args) != 1 { - return nil, tengo.ErrWrongNumArguments + return nil, xgo.ErrWrongNumArguments } - y1, ok := args[0].(*tengo.Bytes) + y1, ok := args[0].(*xgo.Bytes) if !ok { - return nil, tengo.ErrInvalidArgumentType{ + return nil, xgo.ErrInvalidArgumentType{ Name: "first", Expected: "bytes", Found: args[0].TypeName(), @@ -130,7 +130,7 @@ func randRand(r *rand.Rand) *tengo.ImmutableMap { ret = wrapError(err) return } - return &tengo.Int{Value: int64(res)}, nil + return &xgo.Int{Value: int64(res)}, nil }, }, }, diff --git a/stdlib/rand_test.go b/stdlib/rand_test.go index eed3e8c..d649d4b 100644 --- a/stdlib/rand_test.go +++ b/stdlib/rand_test.go @@ -4,15 +4,15 @@ import ( "math/rand" "testing" - "github.com/d5/tengo/v2" - "github.com/d5/tengo/v2/require" + "surdeus.su/core/xgo/v2" + "surdeus.su/core/xgo/v2/require" ) func TestRand(t *testing.T) { var seed int64 = 1234 r := rand.New(rand.NewSource(seed)) - module(t, "rand").call("seed", seed).expect(tengo.UndefinedValue) + module(t, "rand").call("seed", seed).expect(xgo.UndefinedValue) module(t, "rand").call("int").expect(r.Int63()) module(t, "rand").call("float").expect(r.Float64()) module(t, "rand").call("intn", 111).expect(r.Int63n(111)) @@ -21,7 +21,7 @@ func TestRand(t *testing.T) { module(t, "rand").call("perm", 10).expect(r.Perm(10)) buf1 := make([]byte, 10) - buf2 := &tengo.Bytes{Value: make([]byte, 10)} + buf2 := &xgo.Bytes{Value: make([]byte, 10)} n, _ := r.Read(buf1) module(t, "rand").call("read", buf2).expect(n) require.Equal(t, buf1, buf2.Value) @@ -29,7 +29,7 @@ func TestRand(t *testing.T) { seed = 9191 r = rand.New(rand.NewSource(seed)) randObj := module(t, "rand").call("rand", seed) - randObj.call("seed", seed).expect(tengo.UndefinedValue) + randObj.call("seed", seed).expect(xgo.UndefinedValue) randObj.call("int").expect(r.Int63()) randObj.call("float").expect(r.Float64()) randObj.call("intn", 111).expect(r.Int63n(111)) @@ -38,7 +38,7 @@ func TestRand(t *testing.T) { randObj.call("perm", 10).expect(r.Perm(10)) buf1 = make([]byte, 12) - buf2 = &tengo.Bytes{Value: make([]byte, 12)} + buf2 = &xgo.Bytes{Value: make([]byte, 12)} n, _ = r.Read(buf1) randObj.call("read", buf2).expect(n) require.Equal(t, buf1, buf2.Value) diff --git a/stdlib/source_modules.go b/stdlib/source_modules.go index 50e0928..6c5535e 100644 --- a/stdlib/source_modules.go +++ b/stdlib/source_modules.go @@ -2,7 +2,13 @@ package stdlib +import "embed" +var _ = embed.FS{} + +//go:embed srcmod_enum.xgo +var srcmodEnum string + // SourceModules are source type standard library modules. var SourceModules = map[string]string{ - "enum": "is_enumerable := func(x) {\n return is_array(x) || is_map(x) || is_immutable_array(x) || is_immutable_map(x)\n}\n\nis_array_like := func(x) {\n return is_array(x) || is_immutable_array(x)\n}\n\nexport {\n // all returns true if the given function `fn` evaluates to a truthy value on\n // all of the items in `x`. It returns undefined if `x` is not enumerable.\n all: func(x, fn) {\n if !is_enumerable(x) { return undefined }\n\n for k, v in x {\n if !fn(k, v) { return false }\n }\n\n return true\n },\n // any returns true if the given function `fn` evaluates to a truthy value on\n // any of the items in `x`. It returns undefined if `x` is not enumerable.\n any: func(x, fn) {\n if !is_enumerable(x) { return undefined }\n\n for k, v in x {\n if fn(k, v) { return true }\n }\n\n return false\n },\n // chunk returns an array of elements split into groups the length of size.\n // If `x` can't be split evenly, the final chunk will be the remaining elements.\n // It returns undefined if `x` is not array.\n chunk: func(x, size) {\n if !is_array_like(x) || !size { return undefined }\n\n numElements := len(x)\n if !numElements { return [] }\n\n res := []\n idx := 0\n for idx < numElements {\n res = append(res, x[idx:idx+size])\n idx += size\n }\n\n return res\n },\n // at returns an element at the given index (if `x` is array) or\n // key (if `x` is map). It returns undefined if `x` is not enumerable.\n at: func(x, key) {\n if !is_enumerable(x) { return undefined }\n\n if is_array_like(x) {\n if !is_int(key) { return undefined }\n } else {\n if !is_string(key) { return undefined }\n }\n\n return x[key]\n },\n // each iterates over elements of `x` and invokes `fn` for each element. `fn` is\n // invoked with two arguments: `key` and `value`. `key` is an int index\n // if `x` is array. `key` is a string key if `x` is map. It does not iterate\n // and returns undefined if `x` is not enumerable.\n each: func(x, fn) {\n if !is_enumerable(x) { return undefined }\n\n for k, v in x {\n fn(k, v)\n }\n },\n // filter iterates over elements of `x`, returning an array of all elements `fn`\n // returns truthy for. `fn` is invoked with two arguments: `key` and `value`.\n // `key` is an int index if `x` is array. It returns undefined if `x` is not array.\n filter: func(x, fn) {\n if !is_array_like(x) { return undefined }\n\n dst := []\n for k, v in x {\n if fn(k, v) { dst = append(dst, v) }\n }\n\n return dst\n },\n // find iterates over elements of `x`, returning value of the first element `fn`\n // returns truthy for. `fn` is invoked with two arguments: `key` and `value`.\n // `key` is an int index if `x` is array. `key` is a string key if `x` is map.\n // It returns undefined if `x` is not enumerable.\n find: func(x, fn) {\n if !is_enumerable(x) { return undefined }\n\n for k, v in x {\n if fn(k, v) { return v }\n }\n },\n // find_key iterates over elements of `x`, returning key or index of the first\n // element `fn` returns truthy for. `fn` is invoked with two arguments: `key`\n // and `value`. `key` is an int index if `x` is array. `key` is a string key if\n // `x` is map. It returns undefined if `x` is not enumerable.\n find_key: func(x, fn) {\n if !is_enumerable(x) { return undefined }\n\n for k, v in x {\n if fn(k, v) { return k }\n }\n },\n // map creates an array of values by running each element in `x` through `fn`.\n // `fn` is invoked with two arguments: `key` and `value`. `key` is an int index\n // if `x` is array. `key` is a string key if `x` is map. It returns undefined\n // if `x` is not enumerable.\n map: func(x, fn) {\n if !is_enumerable(x) { return undefined }\n\n dst := []\n for k, v in x {\n dst = append(dst, fn(k, v))\n }\n\n return dst\n },\n // key returns the first argument.\n key: func(k, _) { return k },\n // value returns the second argument.\n value: func(_, v) { return v }\n}\n", + "enum": srcmodEnum, } diff --git a/stdlib/srcmod_enum.tengo b/stdlib/srcmod_enum.tengo deleted file mode 100644 index 1db13fe..0000000 --- a/stdlib/srcmod_enum.tengo +++ /dev/null @@ -1,127 +0,0 @@ -is_enumerable := func(x) { - return is_array(x) || is_map(x) || is_immutable_array(x) || is_immutable_map(x) -} - -is_array_like := func(x) { - return is_array(x) || is_immutable_array(x) -} - -export { - // all returns true if the given function `fn` evaluates to a truthy value on - // all of the items in `x`. It returns undefined if `x` is not enumerable. - all: func(x, fn) { - if !is_enumerable(x) { return undefined } - - for k, v in x { - if !fn(k, v) { return false } - } - - return true - }, - // any returns true if the given function `fn` evaluates to a truthy value on - // any of the items in `x`. It returns undefined if `x` is not enumerable. - any: func(x, fn) { - if !is_enumerable(x) { return undefined } - - for k, v in x { - if fn(k, v) { return true } - } - - return false - }, - // chunk returns an array of elements split into groups the length of size. - // If `x` can't be split evenly, the final chunk will be the remaining elements. - // It returns undefined if `x` is not array. - chunk: func(x, size) { - if !is_array_like(x) || !size { return undefined } - - numElements := len(x) - if !numElements { return [] } - - res := [] - idx := 0 - for idx < numElements { - res = append(res, x[idx:idx+size]) - idx += size - } - - return res - }, - // at returns an element at the given index (if `x` is array) or - // key (if `x` is map). It returns undefined if `x` is not enumerable. - at: func(x, key) { - if !is_enumerable(x) { return undefined } - - if is_array_like(x) { - if !is_int(key) { return undefined } - } else { - if !is_string(key) { return undefined } - } - - return x[key] - }, - // each iterates over elements of `x` and invokes `fn` for each element. `fn` is - // invoked with two arguments: `key` and `value`. `key` is an int index - // if `x` is array. `key` is a string key if `x` is map. It does not iterate - // and returns undefined if `x` is not enumerable. - each: func(x, fn) { - if !is_enumerable(x) { return undefined } - - for k, v in x { - fn(k, v) - } - }, - // filter iterates over elements of `x`, returning an array of all elements `fn` - // returns truthy for. `fn` is invoked with two arguments: `key` and `value`. - // `key` is an int index if `x` is array. It returns undefined if `x` is not array. - filter: func(x, fn) { - if !is_array_like(x) { return undefined } - - dst := [] - for k, v in x { - if fn(k, v) { dst = append(dst, v) } - } - - return dst - }, - // find iterates over elements of `x`, returning value of the first element `fn` - // returns truthy for. `fn` is invoked with two arguments: `key` and `value`. - // `key` is an int index if `x` is array. `key` is a string key if `x` is map. - // It returns undefined if `x` is not enumerable. - find: func(x, fn) { - if !is_enumerable(x) { return undefined } - - for k, v in x { - if fn(k, v) { return v } - } - }, - // find_key iterates over elements of `x`, returning key or index of the first - // element `fn` returns truthy for. `fn` is invoked with two arguments: `key` - // and `value`. `key` is an int index if `x` is array. `key` is a string key if - // `x` is map. It returns undefined if `x` is not enumerable. - find_key: func(x, fn) { - if !is_enumerable(x) { return undefined } - - for k, v in x { - if fn(k, v) { return k } - } - }, - // map creates an array of values by running each element in `x` through `fn`. - // `fn` is invoked with two arguments: `key` and `value`. `key` is an int index - // if `x` is array. `key` is a string key if `x` is map. It returns undefined - // if `x` is not enumerable. - map: func(x, fn) { - if !is_enumerable(x) { return undefined } - - dst := [] - for k, v in x { - dst = append(dst, fn(k, v)) - } - - return dst - }, - // key returns the first argument. - key: func(k, _) { return k }, - // value returns the second argument. - value: func(_, v) { return v } -} diff --git a/stdlib/srcmod_enum.xgo b/stdlib/srcmod_enum.xgo new file mode 100644 index 0000000..d82b427 --- /dev/null +++ b/stdlib/srcmod_enum.xgo @@ -0,0 +1,179 @@ +is_enumerable := func(x) { + return is_array(x) || is_map(x) || is_immutable_array(x) || is_immutable_map(x) +} + +is_array_like := func(x) { + return is_array(x) || is_immutable_array(x) +} +_swap := func(arr, left, right) { + if arr == undefined || len(arr) <= 1 || left < 0 || left >= len(arr) || right < 0 || right >= len(arr) || left >= right { + return + } + temp := arr[right] + arr[right] = arr[left] + arr[left] = temp +} + +_sort := func(arr, left, right, less) { + if arr == undefined || len(arr) <= 1 || left < 0 || left >= len(arr) || right < 0 || right >= len(arr) || left >= right { + return arr + } + idx := left + for i := left; i < right; i++ { + if less(arr[i], arr[right]) { + _swap(arr, idx, i) + idx++ + } + } + _swap(arr, idx, right) + _sort(arr, left, idx-1, less) + _sort(arr, idx+1, right, less) + return arr +} + +export { + keys: func(x){ + ret := [] + for k, _ in x { + ret += [k] + } + return ret + }, + values: func(x){ + ret := [] + for _, v in x { + ret += [v] + } + return ret + }, + sort: func(arr, ...less) { + if !less { + less = func(a, b){ + return a < b + } + } else { + less = less[0] + } + if arr == undefined || len(arr) <= 1 { + return arr + } + return _sort(arr, 0, len(arr)-1, less) + }, + // all returns true if the given function `fn` evaluates to a truthy value on + // all of the items in `x`. It returns undefined if `x` is not enumerable. + all: func(x, fn) { + if !is_enumerable(x) { return undefined } + + for k, v in x { + if !fn(k, v) { return false } + } + + return true + }, + // any returns true if the given function `fn` evaluates to a truthy value on + // any of the items in `x`. It returns undefined if `x` is not enumerable. + any: func(x, fn) { + if !is_enumerable(x) { return undefined } + + for k, v in x { + if fn(k, v) { return true } + } + + return false + }, + // chunk returns an array of elements split into groups the length of size. + // If `x` can't be split evenly, the final chunk will be the remaining elements. + // It returns undefined if `x` is not array. + chunk: func(x, size) { + if !is_array_like(x) || !size { return undefined } + + numElements := len(x) + if !numElements { return [] } + + res := [] + idx := 0 + for idx < numElements { + res = append(res, x[idx:idx+size]) + idx += size + } + + return res + }, + // at returns an element at the given index (if `x` is array) or + // key (if `x` is map). It returns undefined if `x` is not enumerable. + at: func(x, key) { + if !is_enumerable(x) { return undefined } + + if is_array_like(x) { + if !is_int(key) { return undefined } + } else { + if !is_string(key) { return undefined } + } + + return x[key] + }, + // each iterates over elements of `x` and invokes `fn` for each element. `fn` is + // invoked with two arguments: `key` and `value`. `key` is an int index + // if `x` is array. `key` is a string key if `x` is map. It does not iterate + // and returns undefined if `x` is not enumerable. + each: func(x, fn) { + if !is_enumerable(x) { return undefined } + + for k, v in x { + fn(k, v) + } + }, + // filter iterates over elements of `x`, returning an array of all elements `fn` + // returns truthy for. `fn` is invoked with two arguments: `key` and `value`. + // `key` is an int index if `x` is array. It returns undefined if `x` is not array. + filter: func(x, fn) { + if !is_array_like(x) { return undefined } + + dst := [] + for k, v in x { + if fn(k, v) { dst = append(dst, v) } + } + + return dst + }, + // find iterates over elements of `x`, returning value of the first element `fn` + // returns truthy for. `fn` is invoked with two arguments: `key` and `value`. + // `key` is an int index if `x` is array. `key` is a string key if `x` is map. + // It returns undefined if `x` is not enumerable. + find: func(x, fn) { + if !is_enumerable(x) { return undefined } + + for k, v in x { + if fn(k, v) { return v } + } + }, + // find_key iterates over elements of `x`, returning key or index of the first + // element `fn` returns truthy for. `fn` is invoked with two arguments: `key` + // and `value`. `key` is an int index if `x` is array. `key` is a string key if + // `x` is map. It returns undefined if `x` is not enumerable. + find_key: func(x, fn) { + if !is_enumerable(x) { return undefined } + + for k, v in x { + if fn(k, v) { return k } + } + }, + // map creates an array of values by running each element in `x` through `fn`. + // `fn` is invoked with two arguments: `key` and `value`. `key` is an int index + // if `x` is array. `key` is a string key if `x` is map. It returns undefined + // if `x` is not enumerable. + map: func(x, fn) { + if !is_enumerable(x) { return undefined } + + dst := [] + for k, v in x { + dst = append(dst, fn(k, v)) + } + + return dst + }, + // key returns the first argument. + key: func(k, _) { return k }, + // value returns the second argument. + value: func(_, v) { return v } +} diff --git a/stdlib/stdlib.go b/stdlib/stdlib.go index 16c369a..b04429c 100644 --- a/stdlib/stdlib.go +++ b/stdlib/stdlib.go @@ -3,7 +3,7 @@ package stdlib //go:generate go run gensrcmods.go import ( - "github.com/d5/tengo/v2" + "surdeus.su/core/xgo/v2" ) // AllModuleNames returns a list of all default module names. @@ -20,8 +20,8 @@ func AllModuleNames() []string { // GetModuleMap returns the module map that includes all modules // for the given module names. -func GetModuleMap(names ...string) *tengo.ModuleMap { - modules := tengo.NewModuleMap() +func GetModuleMap(names ...string) *xgo.ModuleMap { + modules := xgo.NewModuleMap() for _, name := range names { if mod := BuiltinModules[name]; mod != nil { modules.AddBuiltinModule(name, mod) diff --git a/stdlib/stdlib_test.go b/stdlib/stdlib_test.go index ecd5978..276c2ae 100644 --- a/stdlib/stdlib_test.go +++ b/stdlib/stdlib_test.go @@ -5,9 +5,9 @@ import ( "testing" "time" - "github.com/d5/tengo/v2" - "github.com/d5/tengo/v2/require" - "github.com/d5/tengo/v2/stdlib" + "surdeus.su/core/xgo/v2" + "surdeus.su/core/xgo/v2/require" + "surdeus.su/core/xgo/v2/stdlib" ) type ARR = []interface{} @@ -105,20 +105,20 @@ func (c callres) call(funcName string, args ...interface{}) callres { return c } - var oargs []tengo.Object + var oargs []xgo.Object for _, v := range args { oargs = append(oargs, object(v)) } switch o := c.o.(type) { - case *tengo.BuiltinModule: + case *xgo.BuiltinModule: m, ok := o.Attrs[funcName] if !ok { return callres{t: c.t, e: fmt.Errorf( "function not found: %s", funcName)} } - f, ok := m.(*tengo.UserFunction) + f, ok := m.(*xgo.UserFunction) if !ok { return callres{t: c.t, e: fmt.Errorf( "non-callable: %s", funcName)} @@ -126,16 +126,16 @@ func (c callres) call(funcName string, args ...interface{}) callres { res, err := f.Value(oargs...) return callres{t: c.t, o: res, e: err} - case *tengo.UserFunction: + case *xgo.UserFunction: res, err := o.Value(oargs...) return callres{t: c.t, o: res, e: err} - case *tengo.ImmutableMap: + case *xgo.ImmutableMap: m, ok := o.Value[funcName] if !ok { return callres{t: c.t, e: fmt.Errorf("function not found: %s", funcName)} } - f, ok := m.(*tengo.UserFunction) + f, ok := m.(*xgo.UserFunction) if !ok { return callres{t: c.t, e: fmt.Errorf("non-callable: %s", funcName)} } @@ -165,73 +165,73 @@ func module(t *testing.T, moduleName string) callres { return callres{t: t, o: mod} } -func object(v interface{}) tengo.Object { +func object(v interface{}) xgo.Object { switch v := v.(type) { - case tengo.Object: + case xgo.Object: return v case string: - return &tengo.String{Value: v} + return &xgo.String{Value: v} case int64: - return &tengo.Int{Value: v} + return &xgo.Int{Value: v} case int: // for convenience - return &tengo.Int{Value: int64(v)} + return &xgo.Int{Value: int64(v)} case bool: if v { - return tengo.TrueValue + return xgo.TrueValue } - return tengo.FalseValue + return xgo.FalseValue case rune: - return &tengo.Char{Value: v} + return &xgo.Char{Value: v} case byte: // for convenience - return &tengo.Char{Value: rune(v)} + return &xgo.Char{Value: rune(v)} case float64: - return &tengo.Float{Value: v} + return &xgo.Float{Value: v} case []byte: - return &tengo.Bytes{Value: v} + return &xgo.Bytes{Value: v} case MAP: - objs := make(map[string]tengo.Object) + objs := make(map[string]xgo.Object) for k, v := range v { objs[k] = object(v) } - return &tengo.Map{Value: objs} + return &xgo.Map{Value: objs} case ARR: - var objs []tengo.Object + var objs []xgo.Object for _, e := range v { objs = append(objs, object(e)) } - return &tengo.Array{Value: objs} + return &xgo.Array{Value: objs} case IMAP: - objs := make(map[string]tengo.Object) + objs := make(map[string]xgo.Object) for k, v := range v { objs[k] = object(v) } - return &tengo.ImmutableMap{Value: objs} + return &xgo.ImmutableMap{Value: objs} case IARR: - var objs []tengo.Object + var objs []xgo.Object for _, e := range v { objs = append(objs, object(e)) } - return &tengo.ImmutableArray{Value: objs} + return &xgo.ImmutableArray{Value: objs} case time.Time: - return &tengo.Time{Value: v} + return &xgo.Time{Value: v} case []int: - var objs []tengo.Object + var objs []xgo.Object for _, e := range v { - objs = append(objs, &tengo.Int{Value: int64(e)}) + objs = append(objs, &xgo.Int{Value: int64(e)}) } - return &tengo.Array{Value: objs} + return &xgo.Array{Value: objs} } panic(fmt.Errorf("unknown type: %T", v)) } func expect(t *testing.T, input string, expected interface{}) { - s := tengo.NewScript([]byte(input)) + s := xgo.NewScript([]byte(input)) s.SetImports(stdlib.GetModuleMap(stdlib.AllModuleNames()...)) c, err := s.Run() require.NoError(t, err) diff --git a/stdlib/text.go b/stdlib/text.go index d7d5d1d..eb1b5e8 100644 --- a/stdlib/text.go +++ b/stdlib/text.go @@ -7,209 +7,209 @@ import ( "strings" "unicode/utf8" - "github.com/d5/tengo/v2" + "surdeus.su/core/xgo/v2" ) -var textModule = map[string]tengo.Object{ - "re_match": &tengo.UserFunction{ +var textModule = map[string]xgo.Object{ + "re_match": &xgo.UserFunction{ Name: "re_match", Value: textREMatch, }, // re_match(pattern, text) => bool/error - "re_find": &tengo.UserFunction{ + "re_find": &xgo.UserFunction{ Name: "re_find", Value: textREFind, }, // re_find(pattern, text, count) => [[{text:,begin:,end:}]]/undefined - "re_replace": &tengo.UserFunction{ + "re_replace": &xgo.UserFunction{ Name: "re_replace", Value: textREReplace, }, // re_replace(pattern, text, repl) => string/error - "re_split": &tengo.UserFunction{ + "re_split": &xgo.UserFunction{ Name: "re_split", Value: textRESplit, }, // re_split(pattern, text, count) => [string]/error - "re_compile": &tengo.UserFunction{ + "re_compile": &xgo.UserFunction{ Name: "re_compile", Value: textRECompile, }, // re_compile(pattern) => Regexp/error - "compare": &tengo.UserFunction{ + "compare": &xgo.UserFunction{ Name: "compare", Value: FuncASSRI(strings.Compare), }, // compare(a, b) => int - "contains": &tengo.UserFunction{ + "contains": &xgo.UserFunction{ Name: "contains", Value: FuncASSRB(strings.Contains), }, // contains(s, substr) => bool - "contains_any": &tengo.UserFunction{ + "contains_any": &xgo.UserFunction{ Name: "contains_any", Value: FuncASSRB(strings.ContainsAny), }, // contains_any(s, chars) => bool - "count": &tengo.UserFunction{ + "count": &xgo.UserFunction{ Name: "count", Value: FuncASSRI(strings.Count), }, // count(s, substr) => int - "equal_fold": &tengo.UserFunction{ + "equal_fold": &xgo.UserFunction{ Name: "equal_fold", Value: FuncASSRB(strings.EqualFold), }, // "equal_fold(s, t) => bool - "fields": &tengo.UserFunction{ + "fields": &xgo.UserFunction{ Name: "fields", Value: FuncASRSs(strings.Fields), }, // fields(s) => [string] - "has_prefix": &tengo.UserFunction{ + "has_prefix": &xgo.UserFunction{ Name: "has_prefix", Value: FuncASSRB(strings.HasPrefix), }, // has_prefix(s, prefix) => bool - "has_suffix": &tengo.UserFunction{ + "has_suffix": &xgo.UserFunction{ Name: "has_suffix", Value: FuncASSRB(strings.HasSuffix), }, // has_suffix(s, suffix) => bool - "index": &tengo.UserFunction{ + "index": &xgo.UserFunction{ Name: "index", Value: FuncASSRI(strings.Index), }, // index(s, substr) => int - "index_any": &tengo.UserFunction{ + "index_any": &xgo.UserFunction{ Name: "index_any", Value: FuncASSRI(strings.IndexAny), }, // index_any(s, chars) => int - "join": &tengo.UserFunction{ + "join": &xgo.UserFunction{ Name: "join", Value: textJoin, }, // join(arr, sep) => string - "last_index": &tengo.UserFunction{ + "last_index": &xgo.UserFunction{ Name: "last_index", Value: FuncASSRI(strings.LastIndex), }, // last_index(s, substr) => int - "last_index_any": &tengo.UserFunction{ + "last_index_any": &xgo.UserFunction{ Name: "last_index_any", Value: FuncASSRI(strings.LastIndexAny), }, // last_index_any(s, chars) => int - "repeat": &tengo.UserFunction{ + "repeat": &xgo.UserFunction{ Name: "repeat", Value: textRepeat, }, // repeat(s, count) => string - "replace": &tengo.UserFunction{ + "replace": &xgo.UserFunction{ Name: "replace", Value: textReplace, }, // replace(s, old, new, n) => string - "substr": &tengo.UserFunction{ + "substr": &xgo.UserFunction{ Name: "substr", Value: textSubstring, }, // substr(s, lower, upper) => string - "split": &tengo.UserFunction{ + "split": &xgo.UserFunction{ Name: "split", Value: FuncASSRSs(strings.Split), }, // split(s, sep) => [string] - "split_after": &tengo.UserFunction{ + "split_after": &xgo.UserFunction{ Name: "split_after", Value: FuncASSRSs(strings.SplitAfter), }, // split_after(s, sep) => [string] - "split_after_n": &tengo.UserFunction{ + "split_after_n": &xgo.UserFunction{ Name: "split_after_n", Value: FuncASSIRSs(strings.SplitAfterN), }, // split_after_n(s, sep, n) => [string] - "split_n": &tengo.UserFunction{ + "split_n": &xgo.UserFunction{ Name: "split_n", Value: FuncASSIRSs(strings.SplitN), }, // split_n(s, sep, n) => [string] - "title": &tengo.UserFunction{ + "title": &xgo.UserFunction{ Name: "title", Value: FuncASRS(strings.Title), }, // title(s) => string - "to_lower": &tengo.UserFunction{ + "to_lower": &xgo.UserFunction{ Name: "to_lower", Value: FuncASRS(strings.ToLower), }, // to_lower(s) => string - "to_title": &tengo.UserFunction{ + "to_title": &xgo.UserFunction{ Name: "to_title", Value: FuncASRS(strings.ToTitle), }, // to_title(s) => string - "to_upper": &tengo.UserFunction{ + "to_upper": &xgo.UserFunction{ Name: "to_upper", Value: FuncASRS(strings.ToUpper), }, // to_upper(s) => string - "pad_left": &tengo.UserFunction{ + "pad_left": &xgo.UserFunction{ Name: "pad_left", Value: textPadLeft, }, // pad_left(s, pad_len, pad_with) => string - "pad_right": &tengo.UserFunction{ + "pad_right": &xgo.UserFunction{ Name: "pad_right", Value: textPadRight, }, // pad_right(s, pad_len, pad_with) => string - "trim": &tengo.UserFunction{ + "trim": &xgo.UserFunction{ Name: "trim", Value: FuncASSRS(strings.Trim), }, // trim(s, cutset) => string - "trim_left": &tengo.UserFunction{ + "trim_left": &xgo.UserFunction{ Name: "trim_left", Value: FuncASSRS(strings.TrimLeft), }, // trim_left(s, cutset) => string - "trim_prefix": &tengo.UserFunction{ + "trim_prefix": &xgo.UserFunction{ Name: "trim_prefix", Value: FuncASSRS(strings.TrimPrefix), }, // trim_prefix(s, prefix) => string - "trim_right": &tengo.UserFunction{ + "trim_right": &xgo.UserFunction{ Name: "trim_right", Value: FuncASSRS(strings.TrimRight), }, // trim_right(s, cutset) => string - "trim_space": &tengo.UserFunction{ + "trim_space": &xgo.UserFunction{ Name: "trim_space", Value: FuncASRS(strings.TrimSpace), }, // trim_space(s) => string - "trim_suffix": &tengo.UserFunction{ + "trim_suffix": &xgo.UserFunction{ Name: "trim_suffix", Value: FuncASSRS(strings.TrimSuffix), }, // trim_suffix(s, suffix) => string - "atoi": &tengo.UserFunction{ + "atoi": &xgo.UserFunction{ Name: "atoi", Value: FuncASRIE(strconv.Atoi), }, // atoi(str) => int/error - "format_bool": &tengo.UserFunction{ + "format_bool": &xgo.UserFunction{ Name: "format_bool", Value: textFormatBool, }, // format_bool(b) => string - "format_float": &tengo.UserFunction{ + "format_float": &xgo.UserFunction{ Name: "format_float", Value: textFormatFloat, }, // format_float(f, fmt, prec, bits) => string - "format_int": &tengo.UserFunction{ + "format_int": &xgo.UserFunction{ Name: "format_int", Value: textFormatInt, }, // format_int(i, base) => string - "itoa": &tengo.UserFunction{ + "itoa": &xgo.UserFunction{ Name: "itoa", Value: FuncAIRS(strconv.Itoa), }, // itoa(i) => string - "parse_bool": &tengo.UserFunction{ + "parse_bool": &xgo.UserFunction{ Name: "parse_bool", Value: textParseBool, }, // parse_bool(str) => bool/error - "parse_float": &tengo.UserFunction{ + "parse_float": &xgo.UserFunction{ Name: "parse_float", Value: textParseFloat, }, // parse_float(str, bits) => float/error - "parse_int": &tengo.UserFunction{ + "parse_int": &xgo.UserFunction{ Name: "parse_int", Value: textParseInt, }, // parse_int(str, base, bits) => int/error - "quote": &tengo.UserFunction{ + "quote": &xgo.UserFunction{ Name: "quote", Value: FuncASRS(strconv.Quote), }, // quote(str) => string - "unquote": &tengo.UserFunction{ + "unquote": &xgo.UserFunction{ Name: "unquote", Value: FuncASRSE(strconv.Unquote), }, // unquote(str) => string/error } -func textREMatch(args ...tengo.Object) (ret tengo.Object, err error) { +func textREMatch(args ...xgo.Object) (ret xgo.Object, err error) { if len(args) != 2 { - err = tengo.ErrWrongNumArguments + err = xgo.ErrWrongNumArguments return } - s1, ok := tengo.ToString(args[0]) + s1, ok := xgo.ToString(args[0]) if !ok { - err = tengo.ErrInvalidArgumentType{ + err = xgo.ErrInvalidArgumentType{ Name: "first", Expected: "string(compatible)", Found: args[0].TypeName(), @@ -217,9 +217,9 @@ func textREMatch(args ...tengo.Object) (ret tengo.Object, err error) { return } - s2, ok := tengo.ToString(args[1]) + s2, ok := xgo.ToString(args[1]) if !ok { - err = tengo.ErrInvalidArgumentType{ + err = xgo.ErrInvalidArgumentType{ Name: "second", Expected: "string(compatible)", Found: args[1].TypeName(), @@ -234,24 +234,24 @@ func textREMatch(args ...tengo.Object) (ret tengo.Object, err error) { } if matched { - ret = tengo.TrueValue + ret = xgo.TrueValue } else { - ret = tengo.FalseValue + ret = xgo.FalseValue } return } -func textREFind(args ...tengo.Object) (ret tengo.Object, err error) { +func textREFind(args ...xgo.Object) (ret xgo.Object, err error) { numArgs := len(args) if numArgs != 2 && numArgs != 3 { - err = tengo.ErrWrongNumArguments + err = xgo.ErrWrongNumArguments return } - s1, ok := tengo.ToString(args[0]) + s1, ok := xgo.ToString(args[0]) if !ok { - err = tengo.ErrInvalidArgumentType{ + err = xgo.ErrInvalidArgumentType{ Name: "first", Expected: "string(compatible)", Found: args[0].TypeName(), @@ -265,9 +265,9 @@ func textREFind(args ...tengo.Object) (ret tengo.Object, err error) { return } - s2, ok := tengo.ToString(args[1]) + s2, ok := xgo.ToString(args[1]) if !ok { - err = tengo.ErrInvalidArgumentType{ + err = xgo.ErrInvalidArgumentType{ Name: "second", Expected: "string(compatible)", Found: args[1].TypeName(), @@ -278,28 +278,28 @@ func textREFind(args ...tengo.Object) (ret tengo.Object, err error) { if numArgs < 3 { m := re.FindStringSubmatchIndex(s2) if m == nil { - ret = tengo.UndefinedValue + ret = xgo.UndefinedValue return } - arr := &tengo.Array{} + arr := &xgo.Array{} for i := 0; i < len(m); i += 2 { arr.Value = append(arr.Value, - &tengo.ImmutableMap{Value: map[string]tengo.Object{ - "text": &tengo.String{Value: s2[m[i]:m[i+1]]}, - "begin": &tengo.Int{Value: int64(m[i])}, - "end": &tengo.Int{Value: int64(m[i+1])}, + &xgo.ImmutableMap{Value: map[string]xgo.Object{ + "text": &xgo.String{Value: s2[m[i]:m[i+1]]}, + "begin": &xgo.Int{Value: int64(m[i])}, + "end": &xgo.Int{Value: int64(m[i+1])}, }}) } - ret = &tengo.Array{Value: []tengo.Object{arr}} + ret = &xgo.Array{Value: []xgo.Object{arr}} return } - i3, ok := tengo.ToInt(args[2]) + i3, ok := xgo.ToInt(args[2]) if !ok { - err = tengo.ErrInvalidArgumentType{ + err = xgo.ErrInvalidArgumentType{ Name: "third", Expected: "int(compatible)", Found: args[2].TypeName(), @@ -308,19 +308,19 @@ func textREFind(args ...tengo.Object) (ret tengo.Object, err error) { } m := re.FindAllStringSubmatchIndex(s2, i3) if m == nil { - ret = tengo.UndefinedValue + ret = xgo.UndefinedValue return } - arr := &tengo.Array{} + arr := &xgo.Array{} for _, m := range m { - subMatch := &tengo.Array{} + subMatch := &xgo.Array{} for i := 0; i < len(m); i += 2 { subMatch.Value = append(subMatch.Value, - &tengo.ImmutableMap{Value: map[string]tengo.Object{ - "text": &tengo.String{Value: s2[m[i]:m[i+1]]}, - "begin": &tengo.Int{Value: int64(m[i])}, - "end": &tengo.Int{Value: int64(m[i+1])}, + &xgo.ImmutableMap{Value: map[string]xgo.Object{ + "text": &xgo.String{Value: s2[m[i]:m[i+1]]}, + "begin": &xgo.Int{Value: int64(m[i])}, + "end": &xgo.Int{Value: int64(m[i+1])}, }}) } @@ -332,15 +332,15 @@ func textREFind(args ...tengo.Object) (ret tengo.Object, err error) { return } -func textREReplace(args ...tengo.Object) (ret tengo.Object, err error) { +func textREReplace(args ...xgo.Object) (ret xgo.Object, err error) { if len(args) != 3 { - err = tengo.ErrWrongNumArguments + err = xgo.ErrWrongNumArguments return } - s1, ok := tengo.ToString(args[0]) + s1, ok := xgo.ToString(args[0]) if !ok { - err = tengo.ErrInvalidArgumentType{ + err = xgo.ErrInvalidArgumentType{ Name: "first", Expected: "string(compatible)", Found: args[0].TypeName(), @@ -348,9 +348,9 @@ func textREReplace(args ...tengo.Object) (ret tengo.Object, err error) { return } - s2, ok := tengo.ToString(args[1]) + s2, ok := xgo.ToString(args[1]) if !ok { - err = tengo.ErrInvalidArgumentType{ + err = xgo.ErrInvalidArgumentType{ Name: "second", Expected: "string(compatible)", Found: args[1].TypeName(), @@ -358,9 +358,9 @@ func textREReplace(args ...tengo.Object) (ret tengo.Object, err error) { return } - s3, ok := tengo.ToString(args[2]) + s3, ok := xgo.ToString(args[2]) if !ok { - err = tengo.ErrInvalidArgumentType{ + err = xgo.ErrInvalidArgumentType{ Name: "third", Expected: "string(compatible)", Found: args[2].TypeName(), @@ -374,25 +374,25 @@ func textREReplace(args ...tengo.Object) (ret tengo.Object, err error) { } else { s, ok := doTextRegexpReplace(re, s2, s3) if !ok { - return nil, tengo.ErrStringLimit + return nil, xgo.ErrStringLimit } - ret = &tengo.String{Value: s} + ret = &xgo.String{Value: s} } return } -func textRESplit(args ...tengo.Object) (ret tengo.Object, err error) { +func textRESplit(args ...xgo.Object) (ret xgo.Object, err error) { numArgs := len(args) if numArgs != 2 && numArgs != 3 { - err = tengo.ErrWrongNumArguments + err = xgo.ErrWrongNumArguments return } - s1, ok := tengo.ToString(args[0]) + s1, ok := xgo.ToString(args[0]) if !ok { - err = tengo.ErrInvalidArgumentType{ + err = xgo.ErrInvalidArgumentType{ Name: "first", Expected: "string(compatible)", Found: args[0].TypeName(), @@ -400,9 +400,9 @@ func textRESplit(args ...tengo.Object) (ret tengo.Object, err error) { return } - s2, ok := tengo.ToString(args[1]) + s2, ok := xgo.ToString(args[1]) if !ok { - err = tengo.ErrInvalidArgumentType{ + err = xgo.ErrInvalidArgumentType{ Name: "second", Expected: "string(compatible)", Found: args[1].TypeName(), @@ -412,9 +412,9 @@ func textRESplit(args ...tengo.Object) (ret tengo.Object, err error) { var i3 = -1 if numArgs > 2 { - i3, ok = tengo.ToInt(args[2]) + i3, ok = xgo.ToInt(args[2]) if !ok { - err = tengo.ErrInvalidArgumentType{ + err = xgo.ErrInvalidArgumentType{ Name: "third", Expected: "int(compatible)", Found: args[2].TypeName(), @@ -429,9 +429,9 @@ func textRESplit(args ...tengo.Object) (ret tengo.Object, err error) { return } - arr := &tengo.Array{} + arr := &xgo.Array{} for _, s := range re.Split(s2, i3) { - arr.Value = append(arr.Value, &tengo.String{Value: s}) + arr.Value = append(arr.Value, &xgo.String{Value: s}) } ret = arr @@ -439,15 +439,15 @@ func textRESplit(args ...tengo.Object) (ret tengo.Object, err error) { return } -func textRECompile(args ...tengo.Object) (ret tengo.Object, err error) { +func textRECompile(args ...xgo.Object) (ret xgo.Object, err error) { if len(args) != 1 { - err = tengo.ErrWrongNumArguments + err = xgo.ErrWrongNumArguments return } - s1, ok := tengo.ToString(args[0]) + s1, ok := xgo.ToString(args[0]) if !ok { - err = tengo.ErrInvalidArgumentType{ + err = xgo.ErrInvalidArgumentType{ Name: "first", Expected: "string(compatible)", Found: args[0].TypeName(), @@ -465,15 +465,15 @@ func textRECompile(args ...tengo.Object) (ret tengo.Object, err error) { return } -func textReplace(args ...tengo.Object) (ret tengo.Object, err error) { +func textReplace(args ...xgo.Object) (ret xgo.Object, err error) { if len(args) != 4 { - err = tengo.ErrWrongNumArguments + err = xgo.ErrWrongNumArguments return } - s1, ok := tengo.ToString(args[0]) + s1, ok := xgo.ToString(args[0]) if !ok { - err = tengo.ErrInvalidArgumentType{ + err = xgo.ErrInvalidArgumentType{ Name: "first", Expected: "string(compatible)", Found: args[0].TypeName(), @@ -481,9 +481,9 @@ func textReplace(args ...tengo.Object) (ret tengo.Object, err error) { return } - s2, ok := tengo.ToString(args[1]) + s2, ok := xgo.ToString(args[1]) if !ok { - err = tengo.ErrInvalidArgumentType{ + err = xgo.ErrInvalidArgumentType{ Name: "second", Expected: "string(compatible)", Found: args[1].TypeName(), @@ -491,9 +491,9 @@ func textReplace(args ...tengo.Object) (ret tengo.Object, err error) { return } - s3, ok := tengo.ToString(args[2]) + s3, ok := xgo.ToString(args[2]) if !ok { - err = tengo.ErrInvalidArgumentType{ + err = xgo.ErrInvalidArgumentType{ Name: "third", Expected: "string(compatible)", Found: args[2].TypeName(), @@ -501,9 +501,9 @@ func textReplace(args ...tengo.Object) (ret tengo.Object, err error) { return } - i4, ok := tengo.ToInt(args[3]) + i4, ok := xgo.ToInt(args[3]) if !ok { - err = tengo.ErrInvalidArgumentType{ + err = xgo.ErrInvalidArgumentType{ Name: "fourth", Expected: "int(compatible)", Found: args[3].TypeName(), @@ -513,25 +513,25 @@ func textReplace(args ...tengo.Object) (ret tengo.Object, err error) { s, ok := doTextReplace(s1, s2, s3, i4) if !ok { - err = tengo.ErrStringLimit + err = xgo.ErrStringLimit return } - ret = &tengo.String{Value: s} + ret = &xgo.String{Value: s} return } -func textSubstring(args ...tengo.Object) (ret tengo.Object, err error) { +func textSubstring(args ...xgo.Object) (ret xgo.Object, err error) { argslen := len(args) if argslen != 2 && argslen != 3 { - err = tengo.ErrWrongNumArguments + err = xgo.ErrWrongNumArguments return } - s1, ok := tengo.ToString(args[0]) + s1, ok := xgo.ToString(args[0]) if !ok { - err = tengo.ErrInvalidArgumentType{ + err = xgo.ErrInvalidArgumentType{ Name: "first", Expected: "string(compatible)", Found: args[0].TypeName(), @@ -539,9 +539,9 @@ func textSubstring(args ...tengo.Object) (ret tengo.Object, err error) { return } - i2, ok := tengo.ToInt(args[1]) + i2, ok := xgo.ToInt(args[1]) if !ok { - err = tengo.ErrInvalidArgumentType{ + err = xgo.ErrInvalidArgumentType{ Name: "second", Expected: "int(compatible)", Found: args[1].TypeName(), @@ -552,9 +552,9 @@ func textSubstring(args ...tengo.Object) (ret tengo.Object, err error) { strlen := len(s1) i3 := strlen if argslen == 3 { - i3, ok = tengo.ToInt(args[2]) + i3, ok = xgo.ToInt(args[2]) if !ok { - err = tengo.ErrInvalidArgumentType{ + err = xgo.ErrInvalidArgumentType{ Name: "third", Expected: "int(compatible)", Found: args[2].TypeName(), @@ -564,7 +564,7 @@ func textSubstring(args ...tengo.Object) (ret tengo.Object, err error) { } if i2 > i3 { - err = tengo.ErrInvalidIndexType + err = xgo.ErrInvalidIndexType return } @@ -580,21 +580,21 @@ func textSubstring(args ...tengo.Object) (ret tengo.Object, err error) { i3 = strlen } - ret = &tengo.String{Value: s1[i2:i3]} + ret = &xgo.String{Value: s1[i2:i3]} return } -func textPadLeft(args ...tengo.Object) (ret tengo.Object, err error) { +func textPadLeft(args ...xgo.Object) (ret xgo.Object, err error) { argslen := len(args) if argslen != 2 && argslen != 3 { - err = tengo.ErrWrongNumArguments + err = xgo.ErrWrongNumArguments return } - s1, ok := tengo.ToString(args[0]) + s1, ok := xgo.ToString(args[0]) if !ok { - err = tengo.ErrInvalidArgumentType{ + err = xgo.ErrInvalidArgumentType{ Name: "first", Expected: "string(compatible)", Found: args[0].TypeName(), @@ -602,9 +602,9 @@ func textPadLeft(args ...tengo.Object) (ret tengo.Object, err error) { return } - i2, ok := tengo.ToInt(args[1]) + i2, ok := xgo.ToInt(args[1]) if !ok { - err = tengo.ErrInvalidArgumentType{ + err = xgo.ErrInvalidArgumentType{ Name: "second", Expected: "int(compatible)", Found: args[1].TypeName(), @@ -612,21 +612,21 @@ func textPadLeft(args ...tengo.Object) (ret tengo.Object, err error) { return } - if i2 > tengo.MaxStringLen { - return nil, tengo.ErrStringLimit + if i2 > xgo.MaxStringLen { + return nil, xgo.ErrStringLimit } sLen := len(s1) if sLen >= i2 { - ret = &tengo.String{Value: s1} + ret = &xgo.String{Value: s1} return } s3 := " " if argslen == 3 { - s3, ok = tengo.ToString(args[2]) + s3, ok = xgo.ToString(args[2]) if !ok { - err = tengo.ErrInvalidArgumentType{ + err = xgo.ErrInvalidArgumentType{ Name: "third", Expected: "string(compatible)", Found: args[2].TypeName(), @@ -637,27 +637,27 @@ func textPadLeft(args ...tengo.Object) (ret tengo.Object, err error) { padStrLen := len(s3) if padStrLen == 0 { - ret = &tengo.String{Value: s1} + ret = &xgo.String{Value: s1} return } padCount := ((i2 - padStrLen) / padStrLen) + 1 retStr := strings.Repeat(s3, padCount) + s1 - ret = &tengo.String{Value: retStr[len(retStr)-i2:]} + ret = &xgo.String{Value: retStr[len(retStr)-i2:]} return } -func textPadRight(args ...tengo.Object) (ret tengo.Object, err error) { +func textPadRight(args ...xgo.Object) (ret xgo.Object, err error) { argslen := len(args) if argslen != 2 && argslen != 3 { - err = tengo.ErrWrongNumArguments + err = xgo.ErrWrongNumArguments return } - s1, ok := tengo.ToString(args[0]) + s1, ok := xgo.ToString(args[0]) if !ok { - err = tengo.ErrInvalidArgumentType{ + err = xgo.ErrInvalidArgumentType{ Name: "first", Expected: "string(compatible)", Found: args[0].TypeName(), @@ -665,9 +665,9 @@ func textPadRight(args ...tengo.Object) (ret tengo.Object, err error) { return } - i2, ok := tengo.ToInt(args[1]) + i2, ok := xgo.ToInt(args[1]) if !ok { - err = tengo.ErrInvalidArgumentType{ + err = xgo.ErrInvalidArgumentType{ Name: "second", Expected: "int(compatible)", Found: args[1].TypeName(), @@ -675,21 +675,21 @@ func textPadRight(args ...tengo.Object) (ret tengo.Object, err error) { return } - if i2 > tengo.MaxStringLen { - return nil, tengo.ErrStringLimit + if i2 > xgo.MaxStringLen { + return nil, xgo.ErrStringLimit } sLen := len(s1) if sLen >= i2 { - ret = &tengo.String{Value: s1} + ret = &xgo.String{Value: s1} return } s3 := " " if argslen == 3 { - s3, ok = tengo.ToString(args[2]) + s3, ok = xgo.ToString(args[2]) if !ok { - err = tengo.ErrInvalidArgumentType{ + err = xgo.ErrInvalidArgumentType{ Name: "third", Expected: "string(compatible)", Found: args[2].TypeName(), @@ -700,60 +700,60 @@ func textPadRight(args ...tengo.Object) (ret tengo.Object, err error) { padStrLen := len(s3) if padStrLen == 0 { - ret = &tengo.String{Value: s1} + ret = &xgo.String{Value: s1} return } padCount := ((i2 - padStrLen) / padStrLen) + 1 retStr := s1 + strings.Repeat(s3, padCount) - ret = &tengo.String{Value: retStr[:i2]} + ret = &xgo.String{Value: retStr[:i2]} return } -func textRepeat(args ...tengo.Object) (ret tengo.Object, err error) { +func textRepeat(args ...xgo.Object) (ret xgo.Object, err error) { if len(args) != 2 { - return nil, tengo.ErrWrongNumArguments + return nil, xgo.ErrWrongNumArguments } - s1, ok := tengo.ToString(args[0]) + s1, ok := xgo.ToString(args[0]) if !ok { - return nil, tengo.ErrInvalidArgumentType{ + return nil, xgo.ErrInvalidArgumentType{ Name: "first", Expected: "string(compatible)", Found: args[0].TypeName(), } } - i2, ok := tengo.ToInt(args[1]) + i2, ok := xgo.ToInt(args[1]) if !ok { - return nil, tengo.ErrInvalidArgumentType{ + return nil, xgo.ErrInvalidArgumentType{ Name: "second", Expected: "int(compatible)", Found: args[1].TypeName(), } } - if len(s1)*i2 > tengo.MaxStringLen { - return nil, tengo.ErrStringLimit + if len(s1)*i2 > xgo.MaxStringLen { + return nil, xgo.ErrStringLimit } - return &tengo.String{Value: strings.Repeat(s1, i2)}, nil + return &xgo.String{Value: strings.Repeat(s1, i2)}, nil } -func textJoin(args ...tengo.Object) (ret tengo.Object, err error) { +func textJoin(args ...xgo.Object) (ret xgo.Object, err error) { if len(args) != 2 { - return nil, tengo.ErrWrongNumArguments + return nil, xgo.ErrWrongNumArguments } var slen int var ss1 []string switch arg0 := args[0].(type) { - case *tengo.Array: + case *xgo.Array: for idx, a := range arg0.Value { - as, ok := tengo.ToString(a) + as, ok := xgo.ToString(a) if !ok { - return nil, tengo.ErrInvalidArgumentType{ + return nil, xgo.ErrInvalidArgumentType{ Name: fmt.Sprintf("first[%d]", idx), Expected: "string(compatible)", Found: a.TypeName(), @@ -762,11 +762,11 @@ func textJoin(args ...tengo.Object) (ret tengo.Object, err error) { slen += len(as) ss1 = append(ss1, as) } - case *tengo.ImmutableArray: + case *xgo.ImmutableArray: for idx, a := range arg0.Value { - as, ok := tengo.ToString(a) + as, ok := xgo.ToString(a) if !ok { - return nil, tengo.ErrInvalidArgumentType{ + return nil, xgo.ErrInvalidArgumentType{ Name: fmt.Sprintf("first[%d]", idx), Expected: "string(compatible)", Found: a.TypeName(), @@ -776,16 +776,16 @@ func textJoin(args ...tengo.Object) (ret tengo.Object, err error) { ss1 = append(ss1, as) } default: - return nil, tengo.ErrInvalidArgumentType{ + return nil, xgo.ErrInvalidArgumentType{ Name: "first", Expected: "array", Found: args[0].TypeName(), } } - s2, ok := tengo.ToString(args[1]) + s2, ok := xgo.ToString(args[1]) if !ok { - return nil, tengo.ErrInvalidArgumentType{ + return nil, xgo.ErrInvalidArgumentType{ Name: "second", Expected: "string(compatible)", Found: args[1].TypeName(), @@ -793,22 +793,22 @@ func textJoin(args ...tengo.Object) (ret tengo.Object, err error) { } // make sure output length does not exceed the limit - if slen+len(s2)*(len(ss1)-1) > tengo.MaxStringLen { - return nil, tengo.ErrStringLimit + if slen+len(s2)*(len(ss1)-1) > xgo.MaxStringLen { + return nil, xgo.ErrStringLimit } - return &tengo.String{Value: strings.Join(ss1, s2)}, nil + return &xgo.String{Value: strings.Join(ss1, s2)}, nil } -func textFormatBool(args ...tengo.Object) (ret tengo.Object, err error) { +func textFormatBool(args ...xgo.Object) (ret xgo.Object, err error) { if len(args) != 1 { - err = tengo.ErrWrongNumArguments + err = xgo.ErrWrongNumArguments return } - b1, ok := args[0].(*tengo.Bool) + b1, ok := args[0].(*xgo.Bool) if !ok { - err = tengo.ErrInvalidArgumentType{ + err = xgo.ErrInvalidArgumentType{ Name: "first", Expected: "bool", Found: args[0].TypeName(), @@ -816,24 +816,24 @@ func textFormatBool(args ...tengo.Object) (ret tengo.Object, err error) { return } - if b1 == tengo.TrueValue { - ret = &tengo.String{Value: "true"} + if b1 == xgo.TrueValue { + ret = &xgo.String{Value: "true"} } else { - ret = &tengo.String{Value: "false"} + ret = &xgo.String{Value: "false"} } return } -func textFormatFloat(args ...tengo.Object) (ret tengo.Object, err error) { +func textFormatFloat(args ...xgo.Object) (ret xgo.Object, err error) { if len(args) != 4 { - err = tengo.ErrWrongNumArguments + err = xgo.ErrWrongNumArguments return } - f1, ok := args[0].(*tengo.Float) + f1, ok := args[0].(*xgo.Float) if !ok { - err = tengo.ErrInvalidArgumentType{ + err = xgo.ErrInvalidArgumentType{ Name: "first", Expected: "float", Found: args[0].TypeName(), @@ -841,9 +841,9 @@ func textFormatFloat(args ...tengo.Object) (ret tengo.Object, err error) { return } - s2, ok := tengo.ToString(args[1]) + s2, ok := xgo.ToString(args[1]) if !ok { - err = tengo.ErrInvalidArgumentType{ + err = xgo.ErrInvalidArgumentType{ Name: "second", Expected: "string(compatible)", Found: args[1].TypeName(), @@ -851,9 +851,9 @@ func textFormatFloat(args ...tengo.Object) (ret tengo.Object, err error) { return } - i3, ok := tengo.ToInt(args[2]) + i3, ok := xgo.ToInt(args[2]) if !ok { - err = tengo.ErrInvalidArgumentType{ + err = xgo.ErrInvalidArgumentType{ Name: "third", Expected: "int(compatible)", Found: args[2].TypeName(), @@ -861,9 +861,9 @@ func textFormatFloat(args ...tengo.Object) (ret tengo.Object, err error) { return } - i4, ok := tengo.ToInt(args[3]) + i4, ok := xgo.ToInt(args[3]) if !ok { - err = tengo.ErrInvalidArgumentType{ + err = xgo.ErrInvalidArgumentType{ Name: "fourth", Expected: "int(compatible)", Found: args[3].TypeName(), @@ -871,20 +871,20 @@ func textFormatFloat(args ...tengo.Object) (ret tengo.Object, err error) { return } - ret = &tengo.String{Value: strconv.FormatFloat(f1.Value, s2[0], i3, i4)} + ret = &xgo.String{Value: strconv.FormatFloat(f1.Value, s2[0], i3, i4)} return } -func textFormatInt(args ...tengo.Object) (ret tengo.Object, err error) { +func textFormatInt(args ...xgo.Object) (ret xgo.Object, err error) { if len(args) != 2 { - err = tengo.ErrWrongNumArguments + err = xgo.ErrWrongNumArguments return } - i1, ok := args[0].(*tengo.Int) + i1, ok := args[0].(*xgo.Int) if !ok { - err = tengo.ErrInvalidArgumentType{ + err = xgo.ErrInvalidArgumentType{ Name: "first", Expected: "int", Found: args[0].TypeName(), @@ -892,9 +892,9 @@ func textFormatInt(args ...tengo.Object) (ret tengo.Object, err error) { return } - i2, ok := tengo.ToInt(args[1]) + i2, ok := xgo.ToInt(args[1]) if !ok { - err = tengo.ErrInvalidArgumentType{ + err = xgo.ErrInvalidArgumentType{ Name: "second", Expected: "int(compatible)", Found: args[1].TypeName(), @@ -902,20 +902,20 @@ func textFormatInt(args ...tengo.Object) (ret tengo.Object, err error) { return } - ret = &tengo.String{Value: strconv.FormatInt(i1.Value, i2)} + ret = &xgo.String{Value: strconv.FormatInt(i1.Value, i2)} return } -func textParseBool(args ...tengo.Object) (ret tengo.Object, err error) { +func textParseBool(args ...xgo.Object) (ret xgo.Object, err error) { if len(args) != 1 { - err = tengo.ErrWrongNumArguments + err = xgo.ErrWrongNumArguments return } - s1, ok := args[0].(*tengo.String) + s1, ok := args[0].(*xgo.String) if !ok { - err = tengo.ErrInvalidArgumentType{ + err = xgo.ErrInvalidArgumentType{ Name: "first", Expected: "string", Found: args[0].TypeName(), @@ -930,23 +930,23 @@ func textParseBool(args ...tengo.Object) (ret tengo.Object, err error) { } if parsed { - ret = tengo.TrueValue + ret = xgo.TrueValue } else { - ret = tengo.FalseValue + ret = xgo.FalseValue } return } -func textParseFloat(args ...tengo.Object) (ret tengo.Object, err error) { +func textParseFloat(args ...xgo.Object) (ret xgo.Object, err error) { if len(args) != 2 { - err = tengo.ErrWrongNumArguments + err = xgo.ErrWrongNumArguments return } - s1, ok := args[0].(*tengo.String) + s1, ok := args[0].(*xgo.String) if !ok { - err = tengo.ErrInvalidArgumentType{ + err = xgo.ErrInvalidArgumentType{ Name: "first", Expected: "string", Found: args[0].TypeName(), @@ -954,9 +954,9 @@ func textParseFloat(args ...tengo.Object) (ret tengo.Object, err error) { return } - i2, ok := tengo.ToInt(args[1]) + i2, ok := xgo.ToInt(args[1]) if !ok { - err = tengo.ErrInvalidArgumentType{ + err = xgo.ErrInvalidArgumentType{ Name: "second", Expected: "int(compatible)", Found: args[1].TypeName(), @@ -970,20 +970,20 @@ func textParseFloat(args ...tengo.Object) (ret tengo.Object, err error) { return } - ret = &tengo.Float{Value: parsed} + ret = &xgo.Float{Value: parsed} return } -func textParseInt(args ...tengo.Object) (ret tengo.Object, err error) { +func textParseInt(args ...xgo.Object) (ret xgo.Object, err error) { if len(args) != 3 { - err = tengo.ErrWrongNumArguments + err = xgo.ErrWrongNumArguments return } - s1, ok := args[0].(*tengo.String) + s1, ok := args[0].(*xgo.String) if !ok { - err = tengo.ErrInvalidArgumentType{ + err = xgo.ErrInvalidArgumentType{ Name: "first", Expected: "string", Found: args[0].TypeName(), @@ -991,9 +991,9 @@ func textParseInt(args ...tengo.Object) (ret tengo.Object, err error) { return } - i2, ok := tengo.ToInt(args[1]) + i2, ok := xgo.ToInt(args[1]) if !ok { - err = tengo.ErrInvalidArgumentType{ + err = xgo.ErrInvalidArgumentType{ Name: "second", Expected: "int(compatible)", Found: args[1].TypeName(), @@ -1001,9 +1001,9 @@ func textParseInt(args ...tengo.Object) (ret tengo.Object, err error) { return } - i3, ok := tengo.ToInt(args[2]) + i3, ok := xgo.ToInt(args[2]) if !ok { - err = tengo.ErrInvalidArgumentType{ + err = xgo.ErrInvalidArgumentType{ Name: "third", Expected: "int(compatible)", Found: args[2].TypeName(), @@ -1017,7 +1017,7 @@ func textParseInt(args ...tengo.Object) (ret tengo.Object, err error) { return } - ret = &tengo.Int{Value: parsed} + ret = &xgo.Int{Value: parsed} return } @@ -1052,7 +1052,7 @@ func doTextReplace(s, old, new string, n int) (string, bool) { } ssj := s[start:j] - if w+len(ssj)+len(new) > tengo.MaxStringLen { + if w+len(ssj)+len(new) > xgo.MaxStringLen { return "", false } @@ -1062,7 +1062,7 @@ func doTextReplace(s, old, new string, n int) (string, bool) { } ss := s[start:] - if w+len(ss) > tengo.MaxStringLen { + if w+len(ss) > xgo.MaxStringLen { return "", false } diff --git a/stdlib/text_regexp.go b/stdlib/text_regexp.go index 1a7ecf0..0712d45 100644 --- a/stdlib/text_regexp.go +++ b/stdlib/text_regexp.go @@ -3,26 +3,26 @@ package stdlib import ( "regexp" - "github.com/d5/tengo/v2" + "surdeus.su/core/xgo/v2" ) -func makeTextRegexp(re *regexp.Regexp) *tengo.ImmutableMap { - return &tengo.ImmutableMap{ - Value: map[string]tengo.Object{ +func makeTextRegexp(re *regexp.Regexp) *xgo.ImmutableMap { + return &xgo.ImmutableMap{ + Value: map[string]xgo.Object{ // match(text) => bool - "match": &tengo.UserFunction{ - Value: func(args ...tengo.Object) ( - ret tengo.Object, + "match": &xgo.UserFunction{ + Value: func(args ...xgo.Object) ( + ret xgo.Object, err error, ) { if len(args) != 1 { - err = tengo.ErrWrongNumArguments + err = xgo.ErrWrongNumArguments return } - s1, ok := tengo.ToString(args[0]) + s1, ok := xgo.ToString(args[0]) if !ok { - err = tengo.ErrInvalidArgumentType{ + err = xgo.ErrInvalidArgumentType{ Name: "first", Expected: "string(compatible)", Found: args[0].TypeName(), @@ -31,9 +31,9 @@ func makeTextRegexp(re *regexp.Regexp) *tengo.ImmutableMap { } if re.MatchString(s1) { - ret = tengo.TrueValue + ret = xgo.TrueValue } else { - ret = tengo.FalseValue + ret = xgo.FalseValue } return @@ -42,20 +42,20 @@ func makeTextRegexp(re *regexp.Regexp) *tengo.ImmutableMap { // find(text) => array(array({text:,begin:,end:}))/undefined // find(text, maxCount) => array(array({text:,begin:,end:}))/undefined - "find": &tengo.UserFunction{ - Value: func(args ...tengo.Object) ( - ret tengo.Object, + "find": &xgo.UserFunction{ + Value: func(args ...xgo.Object) ( + ret xgo.Object, err error, ) { numArgs := len(args) if numArgs != 1 && numArgs != 2 { - err = tengo.ErrWrongNumArguments + err = xgo.ErrWrongNumArguments return } - s1, ok := tengo.ToString(args[0]) + s1, ok := xgo.ToString(args[0]) if !ok { - err = tengo.ErrInvalidArgumentType{ + err = xgo.ErrInvalidArgumentType{ Name: "first", Expected: "string(compatible)", Found: args[0].TypeName(), @@ -66,35 +66,35 @@ func makeTextRegexp(re *regexp.Regexp) *tengo.ImmutableMap { if numArgs == 1 { m := re.FindStringSubmatchIndex(s1) if m == nil { - ret = tengo.UndefinedValue + ret = xgo.UndefinedValue return } - arr := &tengo.Array{} + arr := &xgo.Array{} for i := 0; i < len(m); i += 2 { arr.Value = append(arr.Value, - &tengo.ImmutableMap{ - Value: map[string]tengo.Object{ - "text": &tengo.String{ + &xgo.ImmutableMap{ + Value: map[string]xgo.Object{ + "text": &xgo.String{ Value: s1[m[i]:m[i+1]], }, - "begin": &tengo.Int{ + "begin": &xgo.Int{ Value: int64(m[i]), }, - "end": &tengo.Int{ + "end": &xgo.Int{ Value: int64(m[i+1]), }, }}) } - ret = &tengo.Array{Value: []tengo.Object{arr}} + ret = &xgo.Array{Value: []xgo.Object{arr}} return } - i2, ok := tengo.ToInt(args[1]) + i2, ok := xgo.ToInt(args[1]) if !ok { - err = tengo.ErrInvalidArgumentType{ + err = xgo.ErrInvalidArgumentType{ Name: "second", Expected: "int(compatible)", Found: args[1].TypeName(), @@ -103,24 +103,24 @@ func makeTextRegexp(re *regexp.Regexp) *tengo.ImmutableMap { } m := re.FindAllStringSubmatchIndex(s1, i2) if m == nil { - ret = tengo.UndefinedValue + ret = xgo.UndefinedValue return } - arr := &tengo.Array{} + arr := &xgo.Array{} for _, m := range m { - subMatch := &tengo.Array{} + subMatch := &xgo.Array{} for i := 0; i < len(m); i += 2 { subMatch.Value = append(subMatch.Value, - &tengo.ImmutableMap{ - Value: map[string]tengo.Object{ - "text": &tengo.String{ + &xgo.ImmutableMap{ + Value: map[string]xgo.Object{ + "text": &xgo.String{ Value: s1[m[i]:m[i+1]], }, - "begin": &tengo.Int{ + "begin": &xgo.Int{ Value: int64(m[i]), }, - "end": &tengo.Int{ + "end": &xgo.Int{ Value: int64(m[i+1]), }, }}) @@ -136,19 +136,19 @@ func makeTextRegexp(re *regexp.Regexp) *tengo.ImmutableMap { }, // replace(src, repl) => string - "replace": &tengo.UserFunction{ - Value: func(args ...tengo.Object) ( - ret tengo.Object, + "replace": &xgo.UserFunction{ + Value: func(args ...xgo.Object) ( + ret xgo.Object, err error, ) { if len(args) != 2 { - err = tengo.ErrWrongNumArguments + err = xgo.ErrWrongNumArguments return } - s1, ok := tengo.ToString(args[0]) + s1, ok := xgo.ToString(args[0]) if !ok { - err = tengo.ErrInvalidArgumentType{ + err = xgo.ErrInvalidArgumentType{ Name: "first", Expected: "string(compatible)", Found: args[0].TypeName(), @@ -156,9 +156,9 @@ func makeTextRegexp(re *regexp.Regexp) *tengo.ImmutableMap { return } - s2, ok := tengo.ToString(args[1]) + s2, ok := xgo.ToString(args[1]) if !ok { - err = tengo.ErrInvalidArgumentType{ + err = xgo.ErrInvalidArgumentType{ Name: "second", Expected: "string(compatible)", Found: args[1].TypeName(), @@ -168,10 +168,10 @@ func makeTextRegexp(re *regexp.Regexp) *tengo.ImmutableMap { s, ok := doTextRegexpReplace(re, s1, s2) if !ok { - return nil, tengo.ErrStringLimit + return nil, xgo.ErrStringLimit } - ret = &tengo.String{Value: s} + ret = &xgo.String{Value: s} return }, @@ -179,20 +179,20 @@ func makeTextRegexp(re *regexp.Regexp) *tengo.ImmutableMap { // split(text) => array(string) // split(text, maxCount) => array(string) - "split": &tengo.UserFunction{ - Value: func(args ...tengo.Object) ( - ret tengo.Object, + "split": &xgo.UserFunction{ + Value: func(args ...xgo.Object) ( + ret xgo.Object, err error, ) { numArgs := len(args) if numArgs != 1 && numArgs != 2 { - err = tengo.ErrWrongNumArguments + err = xgo.ErrWrongNumArguments return } - s1, ok := tengo.ToString(args[0]) + s1, ok := xgo.ToString(args[0]) if !ok { - err = tengo.ErrInvalidArgumentType{ + err = xgo.ErrInvalidArgumentType{ Name: "first", Expected: "string(compatible)", Found: args[0].TypeName(), @@ -202,9 +202,9 @@ func makeTextRegexp(re *regexp.Regexp) *tengo.ImmutableMap { var i2 = -1 if numArgs > 1 { - i2, ok = tengo.ToInt(args[1]) + i2, ok = xgo.ToInt(args[1]) if !ok { - err = tengo.ErrInvalidArgumentType{ + err = xgo.ErrInvalidArgumentType{ Name: "second", Expected: "int(compatible)", Found: args[1].TypeName(), @@ -213,10 +213,10 @@ func makeTextRegexp(re *regexp.Regexp) *tengo.ImmutableMap { } } - arr := &tengo.Array{} + arr := &xgo.Array{} for _, s := range re.Split(s1, i2) { arr.Value = append(arr.Value, - &tengo.String{Value: s}) + &xgo.String{Value: s}) } ret = arr @@ -235,14 +235,14 @@ func doTextRegexpReplace(re *regexp.Regexp, src, repl string) (string, bool) { for _, m := range re.FindAllStringSubmatchIndex(src, -1) { var exp []byte exp = re.ExpandString(exp, repl, src, m) - if len(out)+m[0]-idx+len(exp) > tengo.MaxStringLen { + if len(out)+m[0]-idx+len(exp) > xgo.MaxStringLen { return "", false } out += src[idx:m[0]] + string(exp) idx = m[1] } if idx < len(src) { - if len(out)+len(src)-idx > tengo.MaxStringLen { + if len(out)+len(src)-idx > xgo.MaxStringLen { return "", false } out += src[idx:] diff --git a/stdlib/text_test.go b/stdlib/text_test.go index e3674c7..97ec5bb 100644 --- a/stdlib/text_test.go +++ b/stdlib/text_test.go @@ -4,7 +4,7 @@ import ( "regexp" "testing" - "github.com/d5/tengo/v2" + "surdeus.su/core/xgo/v2" ) func TestTextRE(t *testing.T) { @@ -33,7 +33,7 @@ func TestTextRE(t *testing.T) { text string expected interface{} }{ - {"a(b)", "", tengo.UndefinedValue}, + {"a(b)", "", xgo.UndefinedValue}, {"a(b)", "ab", ARR{ ARR{ IMAP{"text": "ab", "begin": 0, "end": 2}, @@ -67,7 +67,7 @@ func TestTextRE(t *testing.T) { count int expected interface{} }{ - {"a(b)", "", -1, tengo.UndefinedValue}, + {"a(b)", "", -1, xgo.UndefinedValue}, {"a(b)", "ab", -1, ARR{ ARR{ IMAP{"text": "ab", "begin": 0, "end": 2}, @@ -96,7 +96,7 @@ func TestTextRE(t *testing.T) { IMAP{"text": "c", "begin": 9, "end": 10}, }, }}, - {"(a)b(c)d", "abcdefgabcd", 0, tengo.UndefinedValue}, + {"(a)b(c)d", "abcdefgabcd", 0, xgo.UndefinedValue}, {"(a)b(c)d", "abcdefgabcd", 1, ARR{ ARR{ IMAP{"text": "abcd", "begin": 0, "end": 4}, @@ -232,9 +232,9 @@ func TestText(t *testing.T) { } func TestReplaceLimit(t *testing.T) { - curMaxStringLen := tengo.MaxStringLen - defer func() { tengo.MaxStringLen = curMaxStringLen }() - tengo.MaxStringLen = 12 + curMaxStringLen := xgo.MaxStringLen + defer func() { xgo.MaxStringLen = curMaxStringLen }() + xgo.MaxStringLen = 12 module(t, "text").call("replace", "123456789012", "1", "x", -1). expect("x234567890x2") @@ -264,9 +264,9 @@ func TestReplaceLimit(t *testing.T) { } func TestTextRepeat(t *testing.T) { - curMaxStringLen := tengo.MaxStringLen - defer func() { tengo.MaxStringLen = curMaxStringLen }() - tengo.MaxStringLen = 12 + curMaxStringLen := xgo.MaxStringLen + defer func() { xgo.MaxStringLen = curMaxStringLen }() + xgo.MaxStringLen = 12 module(t, "text").call("repeat", "1234", "3"). expect("123412341234") diff --git a/stdlib/times.go b/stdlib/times.go index dc9a1a5..4f46c55 100644 --- a/stdlib/times.go +++ b/stdlib/times.go @@ -3,198 +3,198 @@ package stdlib import ( "time" - "github.com/d5/tengo/v2" + "surdeus.su/core/xgo/v2" ) -var timesModule = map[string]tengo.Object{ - "format_ansic": &tengo.String{Value: time.ANSIC}, - "format_unix_date": &tengo.String{Value: time.UnixDate}, - "format_ruby_date": &tengo.String{Value: time.RubyDate}, - "format_rfc822": &tengo.String{Value: time.RFC822}, - "format_rfc822z": &tengo.String{Value: time.RFC822Z}, - "format_rfc850": &tengo.String{Value: time.RFC850}, - "format_rfc1123": &tengo.String{Value: time.RFC1123}, - "format_rfc1123z": &tengo.String{Value: time.RFC1123Z}, - "format_rfc3339": &tengo.String{Value: time.RFC3339}, - "format_rfc3339_nano": &tengo.String{Value: time.RFC3339Nano}, - "format_kitchen": &tengo.String{Value: time.Kitchen}, - "format_stamp": &tengo.String{Value: time.Stamp}, - "format_stamp_milli": &tengo.String{Value: time.StampMilli}, - "format_stamp_micro": &tengo.String{Value: time.StampMicro}, - "format_stamp_nano": &tengo.String{Value: time.StampNano}, - "nanosecond": &tengo.Int{Value: int64(time.Nanosecond)}, - "microsecond": &tengo.Int{Value: int64(time.Microsecond)}, - "millisecond": &tengo.Int{Value: int64(time.Millisecond)}, - "second": &tengo.Int{Value: int64(time.Second)}, - "minute": &tengo.Int{Value: int64(time.Minute)}, - "hour": &tengo.Int{Value: int64(time.Hour)}, - "january": &tengo.Int{Value: int64(time.January)}, - "february": &tengo.Int{Value: int64(time.February)}, - "march": &tengo.Int{Value: int64(time.March)}, - "april": &tengo.Int{Value: int64(time.April)}, - "may": &tengo.Int{Value: int64(time.May)}, - "june": &tengo.Int{Value: int64(time.June)}, - "july": &tengo.Int{Value: int64(time.July)}, - "august": &tengo.Int{Value: int64(time.August)}, - "september": &tengo.Int{Value: int64(time.September)}, - "october": &tengo.Int{Value: int64(time.October)}, - "november": &tengo.Int{Value: int64(time.November)}, - "december": &tengo.Int{Value: int64(time.December)}, - "sleep": &tengo.UserFunction{ +var timesModule = map[string]xgo.Object{ + "format_ansic": &xgo.String{Value: time.ANSIC}, + "format_unix_date": &xgo.String{Value: time.UnixDate}, + "format_ruby_date": &xgo.String{Value: time.RubyDate}, + "format_rfc822": &xgo.String{Value: time.RFC822}, + "format_rfc822z": &xgo.String{Value: time.RFC822Z}, + "format_rfc850": &xgo.String{Value: time.RFC850}, + "format_rfc1123": &xgo.String{Value: time.RFC1123}, + "format_rfc1123z": &xgo.String{Value: time.RFC1123Z}, + "format_rfc3339": &xgo.String{Value: time.RFC3339}, + "format_rfc3339_nano": &xgo.String{Value: time.RFC3339Nano}, + "format_kitchen": &xgo.String{Value: time.Kitchen}, + "format_stamp": &xgo.String{Value: time.Stamp}, + "format_stamp_milli": &xgo.String{Value: time.StampMilli}, + "format_stamp_micro": &xgo.String{Value: time.StampMicro}, + "format_stamp_nano": &xgo.String{Value: time.StampNano}, + "nanosecond": &xgo.Int{Value: int64(time.Nanosecond)}, + "microsecond": &xgo.Int{Value: int64(time.Microsecond)}, + "millisecond": &xgo.Int{Value: int64(time.Millisecond)}, + "second": &xgo.Int{Value: int64(time.Second)}, + "minute": &xgo.Int{Value: int64(time.Minute)}, + "hour": &xgo.Int{Value: int64(time.Hour)}, + "january": &xgo.Int{Value: int64(time.January)}, + "february": &xgo.Int{Value: int64(time.February)}, + "march": &xgo.Int{Value: int64(time.March)}, + "april": &xgo.Int{Value: int64(time.April)}, + "may": &xgo.Int{Value: int64(time.May)}, + "june": &xgo.Int{Value: int64(time.June)}, + "july": &xgo.Int{Value: int64(time.July)}, + "august": &xgo.Int{Value: int64(time.August)}, + "september": &xgo.Int{Value: int64(time.September)}, + "october": &xgo.Int{Value: int64(time.October)}, + "november": &xgo.Int{Value: int64(time.November)}, + "december": &xgo.Int{Value: int64(time.December)}, + "sleep": &xgo.UserFunction{ Name: "sleep", Value: timesSleep, }, // sleep(int) - "parse_duration": &tengo.UserFunction{ + "parse_duration": &xgo.UserFunction{ Name: "parse_duration", Value: timesParseDuration, }, // parse_duration(str) => int - "since": &tengo.UserFunction{ + "since": &xgo.UserFunction{ Name: "since", Value: timesSince, }, // since(time) => int - "until": &tengo.UserFunction{ + "until": &xgo.UserFunction{ Name: "until", Value: timesUntil, }, // until(time) => int - "duration_hours": &tengo.UserFunction{ + "duration_hours": &xgo.UserFunction{ Name: "duration_hours", Value: timesDurationHours, }, // duration_hours(int) => float - "duration_minutes": &tengo.UserFunction{ + "duration_minutes": &xgo.UserFunction{ Name: "duration_minutes", Value: timesDurationMinutes, }, // duration_minutes(int) => float - "duration_nanoseconds": &tengo.UserFunction{ + "duration_nanoseconds": &xgo.UserFunction{ Name: "duration_nanoseconds", Value: timesDurationNanoseconds, }, // duration_nanoseconds(int) => int - "duration_seconds": &tengo.UserFunction{ + "duration_seconds": &xgo.UserFunction{ Name: "duration_seconds", Value: timesDurationSeconds, }, // duration_seconds(int) => float - "duration_string": &tengo.UserFunction{ + "duration_string": &xgo.UserFunction{ Name: "duration_string", Value: timesDurationString, }, // duration_string(int) => string - "month_string": &tengo.UserFunction{ + "month_string": &xgo.UserFunction{ Name: "month_string", Value: timesMonthString, }, // month_string(int) => string - "date": &tengo.UserFunction{ + "date": &xgo.UserFunction{ Name: "date", Value: timesDate, }, // date(year, month, day, hour, min, sec, nsec) => time - "now": &tengo.UserFunction{ + "now": &xgo.UserFunction{ Name: "now", Value: timesNow, }, // now() => time - "parse": &tengo.UserFunction{ + "parse": &xgo.UserFunction{ Name: "parse", Value: timesParse, }, // parse(format, str) => time - "unix": &tengo.UserFunction{ + "unix": &xgo.UserFunction{ Name: "unix", Value: timesUnix, }, // unix(sec, nsec) => time - "add": &tengo.UserFunction{ + "add": &xgo.UserFunction{ Name: "add", Value: timesAdd, }, // add(time, int) => time - "add_date": &tengo.UserFunction{ + "add_date": &xgo.UserFunction{ Name: "add_date", Value: timesAddDate, }, // add_date(time, years, months, days) => time - "sub": &tengo.UserFunction{ + "sub": &xgo.UserFunction{ Name: "sub", Value: timesSub, }, // sub(t time, u time) => int - "after": &tengo.UserFunction{ + "after": &xgo.UserFunction{ Name: "after", Value: timesAfter, }, // after(t time, u time) => bool - "before": &tengo.UserFunction{ + "before": &xgo.UserFunction{ Name: "before", Value: timesBefore, }, // before(t time, u time) => bool - "time_year": &tengo.UserFunction{ + "time_year": &xgo.UserFunction{ Name: "time_year", Value: timesTimeYear, }, // time_year(time) => int - "time_month": &tengo.UserFunction{ + "time_month": &xgo.UserFunction{ Name: "time_month", Value: timesTimeMonth, }, // time_month(time) => int - "time_day": &tengo.UserFunction{ + "time_day": &xgo.UserFunction{ Name: "time_day", Value: timesTimeDay, }, // time_day(time) => int - "time_weekday": &tengo.UserFunction{ + "time_weekday": &xgo.UserFunction{ Name: "time_weekday", Value: timesTimeWeekday, }, // time_weekday(time) => int - "time_hour": &tengo.UserFunction{ + "time_hour": &xgo.UserFunction{ Name: "time_hour", Value: timesTimeHour, }, // time_hour(time) => int - "time_minute": &tengo.UserFunction{ + "time_minute": &xgo.UserFunction{ Name: "time_minute", Value: timesTimeMinute, }, // time_minute(time) => int - "time_second": &tengo.UserFunction{ + "time_second": &xgo.UserFunction{ Name: "time_second", Value: timesTimeSecond, }, // time_second(time) => int - "time_nanosecond": &tengo.UserFunction{ + "time_nanosecond": &xgo.UserFunction{ Name: "time_nanosecond", Value: timesTimeNanosecond, }, // time_nanosecond(time) => int - "time_unix": &tengo.UserFunction{ + "time_unix": &xgo.UserFunction{ Name: "time_unix", Value: timesTimeUnix, }, // time_unix(time) => int - "time_unix_nano": &tengo.UserFunction{ + "time_unix_nano": &xgo.UserFunction{ Name: "time_unix_nano", Value: timesTimeUnixNano, }, // time_unix_nano(time) => int - "time_format": &tengo.UserFunction{ + "time_format": &xgo.UserFunction{ Name: "time_format", Value: timesTimeFormat, }, // time_format(time, format) => string - "time_location": &tengo.UserFunction{ + "time_location": &xgo.UserFunction{ Name: "time_location", Value: timesTimeLocation, }, // time_location(time) => string - "time_string": &tengo.UserFunction{ + "time_string": &xgo.UserFunction{ Name: "time_string", Value: timesTimeString, }, // time_string(time) => string - "is_zero": &tengo.UserFunction{ + "is_zero": &xgo.UserFunction{ Name: "is_zero", Value: timesIsZero, }, // is_zero(time) => bool - "to_local": &tengo.UserFunction{ + "to_local": &xgo.UserFunction{ Name: "to_local", Value: timesToLocal, }, // to_local(time) => time - "to_utc": &tengo.UserFunction{ + "to_utc": &xgo.UserFunction{ Name: "to_utc", Value: timesToUTC, }, // to_utc(time) => time - "in_location": &tengo.UserFunction{ + "in_location": &xgo.UserFunction{ Name: "in_location", Value: timesInLocation, }, // in_location(time, location) => time } -func timesSleep(args ...tengo.Object) (ret tengo.Object, err error) { +func timesSleep(args ...xgo.Object) (ret xgo.Object, err error) { if len(args) != 1 { - err = tengo.ErrWrongNumArguments + err = xgo.ErrWrongNumArguments return } - i1, ok := tengo.ToInt64(args[0]) + i1, ok := xgo.ToInt64(args[0]) if !ok { - err = tengo.ErrInvalidArgumentType{ + err = xgo.ErrInvalidArgumentType{ Name: "first", Expected: "int(compatible)", Found: args[0].TypeName(), @@ -203,23 +203,23 @@ func timesSleep(args ...tengo.Object) (ret tengo.Object, err error) { } time.Sleep(time.Duration(i1)) - ret = tengo.UndefinedValue + ret = xgo.UndefinedValue return } -func timesParseDuration(args ...tengo.Object) ( - ret tengo.Object, +func timesParseDuration(args ...xgo.Object) ( + ret xgo.Object, err error, ) { if len(args) != 1 { - err = tengo.ErrWrongNumArguments + err = xgo.ErrWrongNumArguments return } - s1, ok := tengo.ToString(args[0]) + s1, ok := xgo.ToString(args[0]) if !ok { - err = tengo.ErrInvalidArgumentType{ + err = xgo.ErrInvalidArgumentType{ Name: "first", Expected: "string(compatible)", Found: args[0].TypeName(), @@ -233,23 +233,23 @@ func timesParseDuration(args ...tengo.Object) ( return } - ret = &tengo.Int{Value: int64(dur)} + ret = &xgo.Int{Value: int64(dur)} return } -func timesSince(args ...tengo.Object) ( - ret tengo.Object, +func timesSince(args ...xgo.Object) ( + ret xgo.Object, err error, ) { if len(args) != 1 { - err = tengo.ErrWrongNumArguments + err = xgo.ErrWrongNumArguments return } - t1, ok := tengo.ToTime(args[0]) + t1, ok := xgo.ToTime(args[0]) if !ok { - err = tengo.ErrInvalidArgumentType{ + err = xgo.ErrInvalidArgumentType{ Name: "first", Expected: "time(compatible)", Found: args[0].TypeName(), @@ -257,23 +257,23 @@ func timesSince(args ...tengo.Object) ( return } - ret = &tengo.Int{Value: int64(time.Since(t1))} + ret = &xgo.Int{Value: int64(time.Since(t1))} return } -func timesUntil(args ...tengo.Object) ( - ret tengo.Object, +func timesUntil(args ...xgo.Object) ( + ret xgo.Object, err error, ) { if len(args) != 1 { - err = tengo.ErrWrongNumArguments + err = xgo.ErrWrongNumArguments return } - t1, ok := tengo.ToTime(args[0]) + t1, ok := xgo.ToTime(args[0]) if !ok { - err = tengo.ErrInvalidArgumentType{ + err = xgo.ErrInvalidArgumentType{ Name: "first", Expected: "time(compatible)", Found: args[0].TypeName(), @@ -281,23 +281,23 @@ func timesUntil(args ...tengo.Object) ( return } - ret = &tengo.Int{Value: int64(time.Until(t1))} + ret = &xgo.Int{Value: int64(time.Until(t1))} return } -func timesDurationHours(args ...tengo.Object) ( - ret tengo.Object, +func timesDurationHours(args ...xgo.Object) ( + ret xgo.Object, err error, ) { if len(args) != 1 { - err = tengo.ErrWrongNumArguments + err = xgo.ErrWrongNumArguments return } - i1, ok := tengo.ToInt64(args[0]) + i1, ok := xgo.ToInt64(args[0]) if !ok { - err = tengo.ErrInvalidArgumentType{ + err = xgo.ErrInvalidArgumentType{ Name: "first", Expected: "int(compatible)", Found: args[0].TypeName(), @@ -305,23 +305,23 @@ func timesDurationHours(args ...tengo.Object) ( return } - ret = &tengo.Float{Value: time.Duration(i1).Hours()} + ret = &xgo.Float{Value: time.Duration(i1).Hours()} return } -func timesDurationMinutes(args ...tengo.Object) ( - ret tengo.Object, +func timesDurationMinutes(args ...xgo.Object) ( + ret xgo.Object, err error, ) { if len(args) != 1 { - err = tengo.ErrWrongNumArguments + err = xgo.ErrWrongNumArguments return } - i1, ok := tengo.ToInt64(args[0]) + i1, ok := xgo.ToInt64(args[0]) if !ok { - err = tengo.ErrInvalidArgumentType{ + err = xgo.ErrInvalidArgumentType{ Name: "first", Expected: "int(compatible)", Found: args[0].TypeName(), @@ -329,23 +329,23 @@ func timesDurationMinutes(args ...tengo.Object) ( return } - ret = &tengo.Float{Value: time.Duration(i1).Minutes()} + ret = &xgo.Float{Value: time.Duration(i1).Minutes()} return } -func timesDurationNanoseconds(args ...tengo.Object) ( - ret tengo.Object, +func timesDurationNanoseconds(args ...xgo.Object) ( + ret xgo.Object, err error, ) { if len(args) != 1 { - err = tengo.ErrWrongNumArguments + err = xgo.ErrWrongNumArguments return } - i1, ok := tengo.ToInt64(args[0]) + i1, ok := xgo.ToInt64(args[0]) if !ok { - err = tengo.ErrInvalidArgumentType{ + err = xgo.ErrInvalidArgumentType{ Name: "first", Expected: "int(compatible)", Found: args[0].TypeName(), @@ -353,23 +353,23 @@ func timesDurationNanoseconds(args ...tengo.Object) ( return } - ret = &tengo.Int{Value: time.Duration(i1).Nanoseconds()} + ret = &xgo.Int{Value: time.Duration(i1).Nanoseconds()} return } -func timesDurationSeconds(args ...tengo.Object) ( - ret tengo.Object, +func timesDurationSeconds(args ...xgo.Object) ( + ret xgo.Object, err error, ) { if len(args) != 1 { - err = tengo.ErrWrongNumArguments + err = xgo.ErrWrongNumArguments return } - i1, ok := tengo.ToInt64(args[0]) + i1, ok := xgo.ToInt64(args[0]) if !ok { - err = tengo.ErrInvalidArgumentType{ + err = xgo.ErrInvalidArgumentType{ Name: "first", Expected: "int(compatible)", Found: args[0].TypeName(), @@ -377,23 +377,23 @@ func timesDurationSeconds(args ...tengo.Object) ( return } - ret = &tengo.Float{Value: time.Duration(i1).Seconds()} + ret = &xgo.Float{Value: time.Duration(i1).Seconds()} return } -func timesDurationString(args ...tengo.Object) ( - ret tengo.Object, +func timesDurationString(args ...xgo.Object) ( + ret xgo.Object, err error, ) { if len(args) != 1 { - err = tengo.ErrWrongNumArguments + err = xgo.ErrWrongNumArguments return } - i1, ok := tengo.ToInt64(args[0]) + i1, ok := xgo.ToInt64(args[0]) if !ok { - err = tengo.ErrInvalidArgumentType{ + err = xgo.ErrInvalidArgumentType{ Name: "first", Expected: "int(compatible)", Found: args[0].TypeName(), @@ -401,23 +401,23 @@ func timesDurationString(args ...tengo.Object) ( return } - ret = &tengo.String{Value: time.Duration(i1).String()} + ret = &xgo.String{Value: time.Duration(i1).String()} return } -func timesMonthString(args ...tengo.Object) ( - ret tengo.Object, +func timesMonthString(args ...xgo.Object) ( + ret xgo.Object, err error, ) { if len(args) != 1 { - err = tengo.ErrWrongNumArguments + err = xgo.ErrWrongNumArguments return } - i1, ok := tengo.ToInt64(args[0]) + i1, ok := xgo.ToInt64(args[0]) if !ok { - err = tengo.ErrInvalidArgumentType{ + err = xgo.ErrInvalidArgumentType{ Name: "first", Expected: "int(compatible)", Found: args[0].TypeName(), @@ -425,77 +425,77 @@ func timesMonthString(args ...tengo.Object) ( return } - ret = &tengo.String{Value: time.Month(i1).String()} + ret = &xgo.String{Value: time.Month(i1).String()} return } -func timesDate(args ...tengo.Object) ( - ret tengo.Object, +func timesDate(args ...xgo.Object) ( + ret xgo.Object, err error, ) { if len(args) < 7 || len(args) > 8 { - err = tengo.ErrWrongNumArguments + err = xgo.ErrWrongNumArguments return } - i1, ok := tengo.ToInt(args[0]) + i1, ok := xgo.ToInt(args[0]) if !ok { - err = tengo.ErrInvalidArgumentType{ + err = xgo.ErrInvalidArgumentType{ Name: "first", Expected: "int(compatible)", Found: args[0].TypeName(), } return } - i2, ok := tengo.ToInt(args[1]) + i2, ok := xgo.ToInt(args[1]) if !ok { - err = tengo.ErrInvalidArgumentType{ + err = xgo.ErrInvalidArgumentType{ Name: "second", Expected: "int(compatible)", Found: args[1].TypeName(), } return } - i3, ok := tengo.ToInt(args[2]) + i3, ok := xgo.ToInt(args[2]) if !ok { - err = tengo.ErrInvalidArgumentType{ + err = xgo.ErrInvalidArgumentType{ Name: "third", Expected: "int(compatible)", Found: args[2].TypeName(), } return } - i4, ok := tengo.ToInt(args[3]) + i4, ok := xgo.ToInt(args[3]) if !ok { - err = tengo.ErrInvalidArgumentType{ + err = xgo.ErrInvalidArgumentType{ Name: "fourth", Expected: "int(compatible)", Found: args[3].TypeName(), } return } - i5, ok := tengo.ToInt(args[4]) + i5, ok := xgo.ToInt(args[4]) if !ok { - err = tengo.ErrInvalidArgumentType{ + err = xgo.ErrInvalidArgumentType{ Name: "fifth", Expected: "int(compatible)", Found: args[4].TypeName(), } return } - i6, ok := tengo.ToInt(args[5]) + i6, ok := xgo.ToInt(args[5]) if !ok { - err = tengo.ErrInvalidArgumentType{ + err = xgo.ErrInvalidArgumentType{ Name: "sixth", Expected: "int(compatible)", Found: args[5].TypeName(), } return } - i7, ok := tengo.ToInt(args[6]) + i7, ok := xgo.ToInt(args[6]) if !ok { - err = tengo.ErrInvalidArgumentType{ + err = xgo.ErrInvalidArgumentType{ Name: "seventh", Expected: "int(compatible)", Found: args[6].TypeName(), @@ -505,9 +505,9 @@ func timesDate(args ...tengo.Object) ( var loc *time.Location if len(args) == 8 { - i8, ok := tengo.ToString(args[7]) + i8, ok := xgo.ToString(args[7]) if !ok { - err = tengo.ErrInvalidArgumentType{ + err = xgo.ErrInvalidArgumentType{ Name: "eighth", Expected: "string(compatible)", Found: args[7].TypeName(), @@ -523,7 +523,7 @@ func timesDate(args ...tengo.Object) ( loc = time.Now().Location() } - ret = &tengo.Time{ + ret = &xgo.Time{ Value: time.Date(i1, time.Month(i2), i3, i4, i5, i6, i7, loc), } @@ -531,26 +531,26 @@ func timesDate(args ...tengo.Object) ( return } -func timesNow(args ...tengo.Object) (ret tengo.Object, err error) { +func timesNow(args ...xgo.Object) (ret xgo.Object, err error) { if len(args) != 0 { - err = tengo.ErrWrongNumArguments + err = xgo.ErrWrongNumArguments return } - ret = &tengo.Time{Value: time.Now()} + ret = &xgo.Time{Value: time.Now()} return } -func timesParse(args ...tengo.Object) (ret tengo.Object, err error) { +func timesParse(args ...xgo.Object) (ret xgo.Object, err error) { if len(args) != 2 { - err = tengo.ErrWrongNumArguments + err = xgo.ErrWrongNumArguments return } - s1, ok := tengo.ToString(args[0]) + s1, ok := xgo.ToString(args[0]) if !ok { - err = tengo.ErrInvalidArgumentType{ + err = xgo.ErrInvalidArgumentType{ Name: "first", Expected: "string(compatible)", Found: args[0].TypeName(), @@ -558,9 +558,9 @@ func timesParse(args ...tengo.Object) (ret tengo.Object, err error) { return } - s2, ok := tengo.ToString(args[1]) + s2, ok := xgo.ToString(args[1]) if !ok { - err = tengo.ErrInvalidArgumentType{ + err = xgo.ErrInvalidArgumentType{ Name: "second", Expected: "string(compatible)", Found: args[1].TypeName(), @@ -574,20 +574,20 @@ func timesParse(args ...tengo.Object) (ret tengo.Object, err error) { return } - ret = &tengo.Time{Value: parsed} + ret = &xgo.Time{Value: parsed} return } -func timesUnix(args ...tengo.Object) (ret tengo.Object, err error) { +func timesUnix(args ...xgo.Object) (ret xgo.Object, err error) { if len(args) != 2 { - err = tengo.ErrWrongNumArguments + err = xgo.ErrWrongNumArguments return } - i1, ok := tengo.ToInt64(args[0]) + i1, ok := xgo.ToInt64(args[0]) if !ok { - err = tengo.ErrInvalidArgumentType{ + err = xgo.ErrInvalidArgumentType{ Name: "first", Expected: "int(compatible)", Found: args[0].TypeName(), @@ -595,9 +595,9 @@ func timesUnix(args ...tengo.Object) (ret tengo.Object, err error) { return } - i2, ok := tengo.ToInt64(args[1]) + i2, ok := xgo.ToInt64(args[1]) if !ok { - err = tengo.ErrInvalidArgumentType{ + err = xgo.ErrInvalidArgumentType{ Name: "second", Expected: "int(compatible)", Found: args[1].TypeName(), @@ -605,20 +605,20 @@ func timesUnix(args ...tengo.Object) (ret tengo.Object, err error) { return } - ret = &tengo.Time{Value: time.Unix(i1, i2)} + ret = &xgo.Time{Value: time.Unix(i1, i2)} return } -func timesAdd(args ...tengo.Object) (ret tengo.Object, err error) { +func timesAdd(args ...xgo.Object) (ret xgo.Object, err error) { if len(args) != 2 { - err = tengo.ErrWrongNumArguments + err = xgo.ErrWrongNumArguments return } - t1, ok := tengo.ToTime(args[0]) + t1, ok := xgo.ToTime(args[0]) if !ok { - err = tengo.ErrInvalidArgumentType{ + err = xgo.ErrInvalidArgumentType{ Name: "first", Expected: "time(compatible)", Found: args[0].TypeName(), @@ -626,9 +626,9 @@ func timesAdd(args ...tengo.Object) (ret tengo.Object, err error) { return } - i2, ok := tengo.ToInt64(args[1]) + i2, ok := xgo.ToInt64(args[1]) if !ok { - err = tengo.ErrInvalidArgumentType{ + err = xgo.ErrInvalidArgumentType{ Name: "second", Expected: "int(compatible)", Found: args[1].TypeName(), @@ -636,20 +636,20 @@ func timesAdd(args ...tengo.Object) (ret tengo.Object, err error) { return } - ret = &tengo.Time{Value: t1.Add(time.Duration(i2))} + ret = &xgo.Time{Value: t1.Add(time.Duration(i2))} return } -func timesSub(args ...tengo.Object) (ret tengo.Object, err error) { +func timesSub(args ...xgo.Object) (ret xgo.Object, err error) { if len(args) != 2 { - err = tengo.ErrWrongNumArguments + err = xgo.ErrWrongNumArguments return } - t1, ok := tengo.ToTime(args[0]) + t1, ok := xgo.ToTime(args[0]) if !ok { - err = tengo.ErrInvalidArgumentType{ + err = xgo.ErrInvalidArgumentType{ Name: "first", Expected: "time(compatible)", Found: args[0].TypeName(), @@ -657,9 +657,9 @@ func timesSub(args ...tengo.Object) (ret tengo.Object, err error) { return } - t2, ok := tengo.ToTime(args[1]) + t2, ok := xgo.ToTime(args[1]) if !ok { - err = tengo.ErrInvalidArgumentType{ + err = xgo.ErrInvalidArgumentType{ Name: "second", Expected: "time(compatible)", Found: args[1].TypeName(), @@ -667,20 +667,20 @@ func timesSub(args ...tengo.Object) (ret tengo.Object, err error) { return } - ret = &tengo.Int{Value: int64(t1.Sub(t2))} + ret = &xgo.Int{Value: int64(t1.Sub(t2))} return } -func timesAddDate(args ...tengo.Object) (ret tengo.Object, err error) { +func timesAddDate(args ...xgo.Object) (ret xgo.Object, err error) { if len(args) != 4 { - err = tengo.ErrWrongNumArguments + err = xgo.ErrWrongNumArguments return } - t1, ok := tengo.ToTime(args[0]) + t1, ok := xgo.ToTime(args[0]) if !ok { - err = tengo.ErrInvalidArgumentType{ + err = xgo.ErrInvalidArgumentType{ Name: "first", Expected: "time(compatible)", Found: args[0].TypeName(), @@ -688,9 +688,9 @@ func timesAddDate(args ...tengo.Object) (ret tengo.Object, err error) { return } - i2, ok := tengo.ToInt(args[1]) + i2, ok := xgo.ToInt(args[1]) if !ok { - err = tengo.ErrInvalidArgumentType{ + err = xgo.ErrInvalidArgumentType{ Name: "second", Expected: "int(compatible)", Found: args[1].TypeName(), @@ -698,9 +698,9 @@ func timesAddDate(args ...tengo.Object) (ret tengo.Object, err error) { return } - i3, ok := tengo.ToInt(args[2]) + i3, ok := xgo.ToInt(args[2]) if !ok { - err = tengo.ErrInvalidArgumentType{ + err = xgo.ErrInvalidArgumentType{ Name: "third", Expected: "int(compatible)", Found: args[2].TypeName(), @@ -708,9 +708,9 @@ func timesAddDate(args ...tengo.Object) (ret tengo.Object, err error) { return } - i4, ok := tengo.ToInt(args[3]) + i4, ok := xgo.ToInt(args[3]) if !ok { - err = tengo.ErrInvalidArgumentType{ + err = xgo.ErrInvalidArgumentType{ Name: "fourth", Expected: "int(compatible)", Found: args[3].TypeName(), @@ -718,20 +718,20 @@ func timesAddDate(args ...tengo.Object) (ret tengo.Object, err error) { return } - ret = &tengo.Time{Value: t1.AddDate(i2, i3, i4)} + ret = &xgo.Time{Value: t1.AddDate(i2, i3, i4)} return } -func timesAfter(args ...tengo.Object) (ret tengo.Object, err error) { +func timesAfter(args ...xgo.Object) (ret xgo.Object, err error) { if len(args) != 2 { - err = tengo.ErrWrongNumArguments + err = xgo.ErrWrongNumArguments return } - t1, ok := tengo.ToTime(args[0]) + t1, ok := xgo.ToTime(args[0]) if !ok { - err = tengo.ErrInvalidArgumentType{ + err = xgo.ErrInvalidArgumentType{ Name: "first", Expected: "time(compatible)", Found: args[0].TypeName(), @@ -739,9 +739,9 @@ func timesAfter(args ...tengo.Object) (ret tengo.Object, err error) { return } - t2, ok := tengo.ToTime(args[1]) + t2, ok := xgo.ToTime(args[1]) if !ok { - err = tengo.ErrInvalidArgumentType{ + err = xgo.ErrInvalidArgumentType{ Name: "second", Expected: "time(compatible)", Found: args[1].TypeName(), @@ -750,23 +750,23 @@ func timesAfter(args ...tengo.Object) (ret tengo.Object, err error) { } if t1.After(t2) { - ret = tengo.TrueValue + ret = xgo.TrueValue } else { - ret = tengo.FalseValue + ret = xgo.FalseValue } return } -func timesBefore(args ...tengo.Object) (ret tengo.Object, err error) { +func timesBefore(args ...xgo.Object) (ret xgo.Object, err error) { if len(args) != 2 { - err = tengo.ErrWrongNumArguments + err = xgo.ErrWrongNumArguments return } - t1, ok := tengo.ToTime(args[0]) + t1, ok := xgo.ToTime(args[0]) if !ok { - err = tengo.ErrInvalidArgumentType{ + err = xgo.ErrInvalidArgumentType{ Name: "first", Expected: "time(compatible)", Found: args[0].TypeName(), @@ -774,9 +774,9 @@ func timesBefore(args ...tengo.Object) (ret tengo.Object, err error) { return } - t2, ok := tengo.ToTime(args[1]) + t2, ok := xgo.ToTime(args[1]) if !ok { - err = tengo.ErrInvalidArgumentType{ + err = xgo.ErrInvalidArgumentType{ Name: "second", Expected: "time(compatible)", Found: args[0].TypeName(), @@ -785,23 +785,23 @@ func timesBefore(args ...tengo.Object) (ret tengo.Object, err error) { } if t1.Before(t2) { - ret = tengo.TrueValue + ret = xgo.TrueValue } else { - ret = tengo.FalseValue + ret = xgo.FalseValue } return } -func timesTimeYear(args ...tengo.Object) (ret tengo.Object, err error) { +func timesTimeYear(args ...xgo.Object) (ret xgo.Object, err error) { if len(args) != 1 { - err = tengo.ErrWrongNumArguments + err = xgo.ErrWrongNumArguments return } - t1, ok := tengo.ToTime(args[0]) + t1, ok := xgo.ToTime(args[0]) if !ok { - err = tengo.ErrInvalidArgumentType{ + err = xgo.ErrInvalidArgumentType{ Name: "first", Expected: "time(compatible)", Found: args[0].TypeName(), @@ -809,20 +809,20 @@ func timesTimeYear(args ...tengo.Object) (ret tengo.Object, err error) { return } - ret = &tengo.Int{Value: int64(t1.Year())} + ret = &xgo.Int{Value: int64(t1.Year())} return } -func timesTimeMonth(args ...tengo.Object) (ret tengo.Object, err error) { +func timesTimeMonth(args ...xgo.Object) (ret xgo.Object, err error) { if len(args) != 1 { - err = tengo.ErrWrongNumArguments + err = xgo.ErrWrongNumArguments return } - t1, ok := tengo.ToTime(args[0]) + t1, ok := xgo.ToTime(args[0]) if !ok { - err = tengo.ErrInvalidArgumentType{ + err = xgo.ErrInvalidArgumentType{ Name: "first", Expected: "time(compatible)", Found: args[0].TypeName(), @@ -830,20 +830,20 @@ func timesTimeMonth(args ...tengo.Object) (ret tengo.Object, err error) { return } - ret = &tengo.Int{Value: int64(t1.Month())} + ret = &xgo.Int{Value: int64(t1.Month())} return } -func timesTimeDay(args ...tengo.Object) (ret tengo.Object, err error) { +func timesTimeDay(args ...xgo.Object) (ret xgo.Object, err error) { if len(args) != 1 { - err = tengo.ErrWrongNumArguments + err = xgo.ErrWrongNumArguments return } - t1, ok := tengo.ToTime(args[0]) + t1, ok := xgo.ToTime(args[0]) if !ok { - err = tengo.ErrInvalidArgumentType{ + err = xgo.ErrInvalidArgumentType{ Name: "first", Expected: "time(compatible)", Found: args[0].TypeName(), @@ -851,20 +851,20 @@ func timesTimeDay(args ...tengo.Object) (ret tengo.Object, err error) { return } - ret = &tengo.Int{Value: int64(t1.Day())} + ret = &xgo.Int{Value: int64(t1.Day())} return } -func timesTimeWeekday(args ...tengo.Object) (ret tengo.Object, err error) { +func timesTimeWeekday(args ...xgo.Object) (ret xgo.Object, err error) { if len(args) != 1 { - err = tengo.ErrWrongNumArguments + err = xgo.ErrWrongNumArguments return } - t1, ok := tengo.ToTime(args[0]) + t1, ok := xgo.ToTime(args[0]) if !ok { - err = tengo.ErrInvalidArgumentType{ + err = xgo.ErrInvalidArgumentType{ Name: "first", Expected: "time(compatible)", Found: args[0].TypeName(), @@ -872,20 +872,20 @@ func timesTimeWeekday(args ...tengo.Object) (ret tengo.Object, err error) { return } - ret = &tengo.Int{Value: int64(t1.Weekday())} + ret = &xgo.Int{Value: int64(t1.Weekday())} return } -func timesTimeHour(args ...tengo.Object) (ret tengo.Object, err error) { +func timesTimeHour(args ...xgo.Object) (ret xgo.Object, err error) { if len(args) != 1 { - err = tengo.ErrWrongNumArguments + err = xgo.ErrWrongNumArguments return } - t1, ok := tengo.ToTime(args[0]) + t1, ok := xgo.ToTime(args[0]) if !ok { - err = tengo.ErrInvalidArgumentType{ + err = xgo.ErrInvalidArgumentType{ Name: "first", Expected: "time(compatible)", Found: args[0].TypeName(), @@ -893,20 +893,20 @@ func timesTimeHour(args ...tengo.Object) (ret tengo.Object, err error) { return } - ret = &tengo.Int{Value: int64(t1.Hour())} + ret = &xgo.Int{Value: int64(t1.Hour())} return } -func timesTimeMinute(args ...tengo.Object) (ret tengo.Object, err error) { +func timesTimeMinute(args ...xgo.Object) (ret xgo.Object, err error) { if len(args) != 1 { - err = tengo.ErrWrongNumArguments + err = xgo.ErrWrongNumArguments return } - t1, ok := tengo.ToTime(args[0]) + t1, ok := xgo.ToTime(args[0]) if !ok { - err = tengo.ErrInvalidArgumentType{ + err = xgo.ErrInvalidArgumentType{ Name: "first", Expected: "time(compatible)", Found: args[0].TypeName(), @@ -914,20 +914,20 @@ func timesTimeMinute(args ...tengo.Object) (ret tengo.Object, err error) { return } - ret = &tengo.Int{Value: int64(t1.Minute())} + ret = &xgo.Int{Value: int64(t1.Minute())} return } -func timesTimeSecond(args ...tengo.Object) (ret tengo.Object, err error) { +func timesTimeSecond(args ...xgo.Object) (ret xgo.Object, err error) { if len(args) != 1 { - err = tengo.ErrWrongNumArguments + err = xgo.ErrWrongNumArguments return } - t1, ok := tengo.ToTime(args[0]) + t1, ok := xgo.ToTime(args[0]) if !ok { - err = tengo.ErrInvalidArgumentType{ + err = xgo.ErrInvalidArgumentType{ Name: "first", Expected: "time(compatible)", Found: args[0].TypeName(), @@ -935,23 +935,23 @@ func timesTimeSecond(args ...tengo.Object) (ret tengo.Object, err error) { return } - ret = &tengo.Int{Value: int64(t1.Second())} + ret = &xgo.Int{Value: int64(t1.Second())} return } -func timesTimeNanosecond(args ...tengo.Object) ( - ret tengo.Object, +func timesTimeNanosecond(args ...xgo.Object) ( + ret xgo.Object, err error, ) { if len(args) != 1 { - err = tengo.ErrWrongNumArguments + err = xgo.ErrWrongNumArguments return } - t1, ok := tengo.ToTime(args[0]) + t1, ok := xgo.ToTime(args[0]) if !ok { - err = tengo.ErrInvalidArgumentType{ + err = xgo.ErrInvalidArgumentType{ Name: "first", Expected: "time(compatible)", Found: args[0].TypeName(), @@ -959,20 +959,20 @@ func timesTimeNanosecond(args ...tengo.Object) ( return } - ret = &tengo.Int{Value: int64(t1.Nanosecond())} + ret = &xgo.Int{Value: int64(t1.Nanosecond())} return } -func timesTimeUnix(args ...tengo.Object) (ret tengo.Object, err error) { +func timesTimeUnix(args ...xgo.Object) (ret xgo.Object, err error) { if len(args) != 1 { - err = tengo.ErrWrongNumArguments + err = xgo.ErrWrongNumArguments return } - t1, ok := tengo.ToTime(args[0]) + t1, ok := xgo.ToTime(args[0]) if !ok { - err = tengo.ErrInvalidArgumentType{ + err = xgo.ErrInvalidArgumentType{ Name: "first", Expected: "time(compatible)", Found: args[0].TypeName(), @@ -980,23 +980,23 @@ func timesTimeUnix(args ...tengo.Object) (ret tengo.Object, err error) { return } - ret = &tengo.Int{Value: t1.Unix()} + ret = &xgo.Int{Value: t1.Unix()} return } -func timesTimeUnixNano(args ...tengo.Object) ( - ret tengo.Object, +func timesTimeUnixNano(args ...xgo.Object) ( + ret xgo.Object, err error, ) { if len(args) != 1 { - err = tengo.ErrWrongNumArguments + err = xgo.ErrWrongNumArguments return } - t1, ok := tengo.ToTime(args[0]) + t1, ok := xgo.ToTime(args[0]) if !ok { - err = tengo.ErrInvalidArgumentType{ + err = xgo.ErrInvalidArgumentType{ Name: "first", Expected: "time(compatible)", Found: args[0].TypeName(), @@ -1004,20 +1004,20 @@ func timesTimeUnixNano(args ...tengo.Object) ( return } - ret = &tengo.Int{Value: t1.UnixNano()} + ret = &xgo.Int{Value: t1.UnixNano()} return } -func timesTimeFormat(args ...tengo.Object) (ret tengo.Object, err error) { +func timesTimeFormat(args ...xgo.Object) (ret xgo.Object, err error) { if len(args) != 2 { - err = tengo.ErrWrongNumArguments + err = xgo.ErrWrongNumArguments return } - t1, ok := tengo.ToTime(args[0]) + t1, ok := xgo.ToTime(args[0]) if !ok { - err = tengo.ErrInvalidArgumentType{ + err = xgo.ErrInvalidArgumentType{ Name: "first", Expected: "time(compatible)", Found: args[0].TypeName(), @@ -1025,9 +1025,9 @@ func timesTimeFormat(args ...tengo.Object) (ret tengo.Object, err error) { return } - s2, ok := tengo.ToString(args[1]) + s2, ok := xgo.ToString(args[1]) if !ok { - err = tengo.ErrInvalidArgumentType{ + err = xgo.ErrInvalidArgumentType{ Name: "second", Expected: "string(compatible)", Found: args[1].TypeName(), @@ -1036,25 +1036,25 @@ func timesTimeFormat(args ...tengo.Object) (ret tengo.Object, err error) { } s := t1.Format(s2) - if len(s) > tengo.MaxStringLen { + if len(s) > xgo.MaxStringLen { - return nil, tengo.ErrStringLimit + return nil, xgo.ErrStringLimit } - ret = &tengo.String{Value: s} + ret = &xgo.String{Value: s} return } -func timesIsZero(args ...tengo.Object) (ret tengo.Object, err error) { +func timesIsZero(args ...xgo.Object) (ret xgo.Object, err error) { if len(args) != 1 { - err = tengo.ErrWrongNumArguments + err = xgo.ErrWrongNumArguments return } - t1, ok := tengo.ToTime(args[0]) + t1, ok := xgo.ToTime(args[0]) if !ok { - err = tengo.ErrInvalidArgumentType{ + err = xgo.ErrInvalidArgumentType{ Name: "first", Expected: "time(compatible)", Found: args[0].TypeName(), @@ -1063,23 +1063,23 @@ func timesIsZero(args ...tengo.Object) (ret tengo.Object, err error) { } if t1.IsZero() { - ret = tengo.TrueValue + ret = xgo.TrueValue } else { - ret = tengo.FalseValue + ret = xgo.FalseValue } return } -func timesToLocal(args ...tengo.Object) (ret tengo.Object, err error) { +func timesToLocal(args ...xgo.Object) (ret xgo.Object, err error) { if len(args) != 1 { - err = tengo.ErrWrongNumArguments + err = xgo.ErrWrongNumArguments return } - t1, ok := tengo.ToTime(args[0]) + t1, ok := xgo.ToTime(args[0]) if !ok { - err = tengo.ErrInvalidArgumentType{ + err = xgo.ErrInvalidArgumentType{ Name: "first", Expected: "time(compatible)", Found: args[0].TypeName(), @@ -1087,20 +1087,20 @@ func timesToLocal(args ...tengo.Object) (ret tengo.Object, err error) { return } - ret = &tengo.Time{Value: t1.Local()} + ret = &xgo.Time{Value: t1.Local()} return } -func timesToUTC(args ...tengo.Object) (ret tengo.Object, err error) { +func timesToUTC(args ...xgo.Object) (ret xgo.Object, err error) { if len(args) != 1 { - err = tengo.ErrWrongNumArguments + err = xgo.ErrWrongNumArguments return } - t1, ok := tengo.ToTime(args[0]) + t1, ok := xgo.ToTime(args[0]) if !ok { - err = tengo.ErrInvalidArgumentType{ + err = xgo.ErrInvalidArgumentType{ Name: "first", Expected: "time(compatible)", Found: args[0].TypeName(), @@ -1108,23 +1108,23 @@ func timesToUTC(args ...tengo.Object) (ret tengo.Object, err error) { return } - ret = &tengo.Time{Value: t1.UTC()} + ret = &xgo.Time{Value: t1.UTC()} return } -func timesTimeLocation(args ...tengo.Object) ( - ret tengo.Object, +func timesTimeLocation(args ...xgo.Object) ( + ret xgo.Object, err error, ) { if len(args) != 1 { - err = tengo.ErrWrongNumArguments + err = xgo.ErrWrongNumArguments return } - t1, ok := tengo.ToTime(args[0]) + t1, ok := xgo.ToTime(args[0]) if !ok { - err = tengo.ErrInvalidArgumentType{ + err = xgo.ErrInvalidArgumentType{ Name: "first", Expected: "time(compatible)", Found: args[0].TypeName(), @@ -1132,23 +1132,23 @@ func timesTimeLocation(args ...tengo.Object) ( return } - ret = &tengo.String{Value: t1.Location().String()} + ret = &xgo.String{Value: t1.Location().String()} return } -func timesInLocation(args ...tengo.Object) ( - ret tengo.Object, +func timesInLocation(args ...xgo.Object) ( + ret xgo.Object, err error, ) { if len(args) != 2 { - err = tengo.ErrWrongNumArguments + err = xgo.ErrWrongNumArguments return } - t1, ok := tengo.ToTime(args[0]) + t1, ok := xgo.ToTime(args[0]) if !ok { - err = tengo.ErrInvalidArgumentType{ + err = xgo.ErrInvalidArgumentType{ Name: "first", Expected: "time(compatible)", Found: args[0].TypeName(), @@ -1156,9 +1156,9 @@ func timesInLocation(args ...tengo.Object) ( return } - s2, ok := tengo.ToString(args[1]) + s2, ok := xgo.ToString(args[1]) if !ok { - err = tengo.ErrInvalidArgumentType{ + err = xgo.ErrInvalidArgumentType{ Name: "second", Expected: "string(compatible)", Found: args[1].TypeName(), @@ -1172,20 +1172,20 @@ func timesInLocation(args ...tengo.Object) ( return } - ret = &tengo.Time{Value: t1.In(location)} + ret = &xgo.Time{Value: t1.In(location)} return } -func timesTimeString(args ...tengo.Object) (ret tengo.Object, err error) { +func timesTimeString(args ...xgo.Object) (ret xgo.Object, err error) { if len(args) != 1 { - err = tengo.ErrWrongNumArguments + err = xgo.ErrWrongNumArguments return } - t1, ok := tengo.ToTime(args[0]) + t1, ok := xgo.ToTime(args[0]) if !ok { - err = tengo.ErrInvalidArgumentType{ + err = xgo.ErrInvalidArgumentType{ Name: "first", Expected: "time(compatible)", Found: args[0].TypeName(), @@ -1193,7 +1193,7 @@ func timesTimeString(args ...tengo.Object) (ret tengo.Object, err error) { return } - ret = &tengo.String{Value: t1.String()} + ret = &xgo.String{Value: t1.String()} return } diff --git a/stdlib/times_test.go b/stdlib/times_test.go index afe34bd..a747176 100644 --- a/stdlib/times_test.go +++ b/stdlib/times_test.go @@ -4,8 +4,8 @@ import ( "testing" "time" - "github.com/d5/tengo/v2" - "github.com/d5/tengo/v2/require" + "surdeus.su/core/xgo/v2" + "surdeus.su/core/xgo/v2/require" ) func TestTimes(t *testing.T) { @@ -14,14 +14,14 @@ func TestTimes(t *testing.T) { location, _ := time.LoadLocation("Pacific/Auckland") time3 := time.Date(1982, 9, 28, 19, 21, 44, 999, location) - module(t, "times").call("sleep", 1).expect(tengo.UndefinedValue) + module(t, "times").call("sleep", 1).expect(xgo.UndefinedValue) require.True(t, module(t, "times"). call("since", time.Now().Add(-time.Hour)). - o.(*tengo.Int).Value > 3600000000000) + o.(*xgo.Int).Value > 3600000000000) require.True(t, module(t, "times"). call("until", time.Now().Add(time.Hour)). - o.(*tengo.Int).Value < 3600000000000) + o.(*xgo.Int).Value < 3600000000000) module(t, "times").call("parse_duration", "1ns").expect(1) module(t, "times").call("parse_duration", "1ms").expect(1000000) @@ -41,7 +41,7 @@ func TestTimes(t *testing.T) { expect(time3) nowD := time.Until(module(t, "times").call("now"). - o.(*tengo.Time).Value).Nanoseconds() + o.(*xgo.Time).Value).Nanoseconds() require.True(t, 0 > nowD && nowD > -100000000) // within 100ms parsed, _ := time.Parse(time.RFC3339, "1982-09-28T19:21:44+07:00") module(t, "times"). diff --git a/stdlib/url/url.go b/stdlib/url/url.go new file mode 100644 index 0000000..c2d21d5 --- /dev/null +++ b/stdlib/url/url.go @@ -0,0 +1,77 @@ +package url + +import ( + "surdeus.su/core/xgo/v2" + "net/url" + "fmt" +) + +var _ = xgo.Object(&Values{}) + +type Values struct { + xgo.ObjectImpl + url.Values +} + +func (vs *Values) TypeName() string { + return "*url.Values" +} + +func (vs *Values) String() string { + return fmt.Sprintf("%v", vs.Values) +} + +func (vs *Values) IndexGet( + index xgo.Object, +) (xgo.Object, error) { + key, ok := xgo.ToString(index) + if !ok { + return nil, xgo.ErrInvalidIndexValueType + } + + val, ok := vs.Values[key] + if !ok { + return nil, nil + } + + arr := make([]xgo.Object, len(val)) + for i, v := range val { + arr[i], _ = xgo.FromInterface(v) + } + + return &xgo.Array{Value: arr}, nil +} + +type URL struct { + xgo.ObjectImpl + *url.URL +} + +func (u *URL) TypeName() string { + return "" +} + +func (u *URL) String() string { + return u.URL.String() +} + +func (u *URL) IndexGet( + index xgo.Object, +) (xgo.Object, error) { + key, ok := xgo.ToString(index) + if !ok { + return nil, xgo.ErrInvalidIndexValueType + } + + switch key { + case "path": + return xgo.FromInterface(u.Path) + case "query": + return &Values{ + Values: u.Query(), + }, nil + } + + // Nothing found. + return nil, nil +} diff --git a/symbol_table.go b/symbol_table.go index 73aaad3..391755f 100644 --- a/symbol_table.go +++ b/symbol_table.go @@ -1,4 +1,4 @@ -package tengo +package xgo // SymbolScope represents a symbol scope. type SymbolScope string diff --git a/symbol_table_test.go b/symbol_table_test.go index e46eb3a..2b077b1 100644 --- a/symbol_table_test.go +++ b/symbol_table_test.go @@ -1,10 +1,10 @@ -package tengo_test +package xgo_test import ( "testing" - "github.com/d5/tengo/v2" - "github.com/d5/tengo/v2/require" + "surdeus.su/core/xgo/v2" + "surdeus.su/core/xgo/v2/require" ) func TestSymbolTable(t *testing.T) { diff --git a/tengo.go b/tengo.go index 490e9ae..fbc8a5e 100644 --- a/tengo.go +++ b/tengo.go @@ -1,4 +1,4 @@ -package tengo +package xgo import ( "errors" @@ -28,7 +28,7 @@ const ( MaxFrames = 1024 // SourceFileExtDefault is the default extension for source files. - SourceFileExtDefault = ".tengo" + SourceFileExtDefault = ".xgo" ) // CallableFunc is a function signature for the callable functions. diff --git a/tengo_test.go b/tengo_test.go index d1f5e8a..077b72a 100644 --- a/tengo_test.go +++ b/tengo_test.go @@ -1,13 +1,13 @@ -package tengo_test +package xgo_test import ( "strings" "testing" "time" - "github.com/d5/tengo/v2" - "github.com/d5/tengo/v2/parser" - "github.com/d5/tengo/v2/require" + "surdeus.su/core/xgo/v2" + "surdeus.su/core/xgo/v2/parser" + "surdeus.su/core/xgo/v2/require" ) func TestInstructions_String(t *testing.T) { diff --git a/testdata/cli/one.tengo b/testdata/cli/one.xgo similarity index 100% rename from testdata/cli/one.tengo rename to testdata/cli/one.xgo diff --git a/testdata/cli/test.tengo b/testdata/cli/testo.xgo similarity index 100% rename from testdata/cli/test.tengo rename to testdata/cli/testo.xgo diff --git a/testdata/cli/three.tengo b/testdata/cli/three.xgo similarity index 100% rename from testdata/cli/three.tengo rename to testdata/cli/three.xgo diff --git a/testdata/cli/two/five/five.tengo b/testdata/cli/two/five/five.xgo similarity index 100% rename from testdata/cli/two/five/five.tengo rename to testdata/cli/two/five/five.xgo diff --git a/testdata/cli/two/four/four.tengo b/testdata/cli/two/four/four.xgo similarity index 100% rename from testdata/cli/two/four/four.tengo rename to testdata/cli/two/four/four.xgo diff --git a/testdata/cli/two/two.tengo b/testdata/cli/two/two.xgo similarity index 100% rename from testdata/cli/two/two.tengo rename to testdata/cli/two/two.xgo diff --git a/testdata/libs/array.tengo b/testdata/libs/array.tengo new file mode 100644 index 0000000..bac2c2e --- /dev/null +++ b/testdata/libs/array.tengo @@ -0,0 +1,22 @@ +enum := import("enumx") +fmt := import("log") + +object := { + key1: "cock", + key2: "dick", + key3: "check", + key4: "die", + key5: "wasd" +} +keys := enum.keys(object) +values := enum.values(object) + +sorted_values := enum.sort(values, func(a, b){ + return b < a +}) + +ia := [5, 100, 531, 7537, 1, 2, -100, 1.535] +fmt.println(enum.sort(ia)) +fmt.println(sorted_values) + + diff --git a/testdata/libs/cjson.xgo b/testdata/libs/cjson.xgo new file mode 100644 index 0000000..f3cf77b --- /dev/null +++ b/testdata/libs/cjson.xgo @@ -0,0 +1,20 @@ +cjson := import("cjson") +fmt := import("fmt") +log := import("log") +dec := cjson.Decoder("") + +log.print("cock") +log.print("die") + +log.println("some log") +for { + v := dec.decode() + if is_error(v) { + break + } + if !v { + break + } + log.println("got one more value") + fmt.println(v) +} diff --git a/testdata/libs/data.cjson b/testdata/libs/data.cjson new file mode 100644 index 0000000..187ebe4 --- /dev/null +++ b/testdata/libs/data.cjson @@ -0,0 +1,4 @@ +{"somename": { + "value": 1 +}} +["array",135,"integers", 135, 135] diff --git a/testdata/libs/dogs.xgo b/testdata/libs/dogs.xgo new file mode 100644 index 0000000..218e84e --- /dev/null +++ b/testdata/libs/dogs.xgo @@ -0,0 +1,22 @@ +objects := import("objects") +fmt := import("fmt") + +new := func(name) { + ret := objects.new() + ret.name = name + + ret.def("bark", func(self, n){ + fmt.print(self.name, ": ") + for i:=0 ; i>>shit" + ) + ), + added + ) +) diff --git a/testdata/libs/http.xgo b/testdata/libs/http.xgo new file mode 100644 index 0000000..3b7c42f --- /dev/null +++ b/testdata/libs/http.xgo @@ -0,0 +1,19 @@ +fmt := import("fmt") +http := import("http") +os := import("os") + +resp := http.post_json( + "https://jsonplaceholder.typicode.com/posts", { + title: "Hello, World!", + body: "Checking shit out!", + userId: 1 + } +) +if is_error(resp) { + fmt.println(resp) + os.exit(1) +} + +body := resp.body_json +fmt.println(body) + diff --git a/testdata/libs/import.xgo b/testdata/libs/import.xgo new file mode 100644 index 0000000..159c42b --- /dev/null +++ b/testdata/libs/import.xgo @@ -0,0 +1,7 @@ +fmt := import("fmt") +dogs := import("./tests/dogs") + +dog := dogs.new("check") +//fmt.println(dog) +dog.bark(10) + diff --git a/testdata/libs/paths.xgo b/testdata/libs/paths.xgo new file mode 100644 index 0000000..fcd8721 --- /dev/null +++ b/testdata/libs/paths.xgo @@ -0,0 +1,8 @@ +fmt := import("fmt") +paths := import("paths") +os := import("os") + +for path in os.args()[2:] { + fmt.println(paths.base(path)) +} + diff --git a/variable.go b/variable.go index 481b36b..de920cc 100644 --- a/variable.go +++ b/variable.go @@ -1,4 +1,4 @@ -package tengo +package xgo import ( "errors" diff --git a/variable_test.go b/variable_test.go index f39c9c9..8a8ea2a 100644 --- a/variable_test.go +++ b/variable_test.go @@ -1,10 +1,10 @@ -package tengo_test +package xgo_test import ( "testing" - "github.com/d5/tengo/v2" - "github.com/d5/tengo/v2/require" + "surdeus.su/core/xgo/v2" + "surdeus.su/core/xgo/v2/require" ) type VariableTest struct { diff --git a/vm.go b/vm.go index 74b7742..1c4d014 100644 --- a/vm.go +++ b/vm.go @@ -1,11 +1,11 @@ -package tengo +package xgo import ( "fmt" "sync/atomic" - "github.com/d5/tengo/v2/parser" - "github.com/d5/tengo/v2/token" + "surdeus.su/core/xgo/v2/parser" + "surdeus.su/core/xgo/v2/token" ) // frame represents a function call frame. diff --git a/vm_test.go b/vm_test.go index 55a9289..07f9c3c 100644 --- a/vm_test.go +++ b/vm_test.go @@ -1,4 +1,4 @@ -package tengo_test +package xgo_test import ( "errors" @@ -10,11 +10,11 @@ import ( "strings" "testing" - "github.com/d5/tengo/v2" - "github.com/d5/tengo/v2/parser" - "github.com/d5/tengo/v2/require" - "github.com/d5/tengo/v2/stdlib" - "github.com/d5/tengo/v2/token" + "surdeus.su/core/xgo/v2" + "surdeus.su/core/xgo/v2/parser" + "surdeus.su/core/xgo/v2/require" + "surdeus.su/core/xgo/v2/stdlib" + "surdeus.su/core/xgo/v2/token" ) const testOut = "out"