2024-04-25 22:38:04 +03:00
|
|
|
package tpp
|
2024-02-26 21:04:47 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/d5/tengo/v2"
|
|
|
|
"fmt"
|
|
|
|
"context"
|
2024-04-25 22:38:04 +03:00
|
|
|
"bytes"
|
2024-02-26 21:04:47 +03:00
|
|
|
)
|
|
|
|
|
2024-04-25 22:38:04 +03:00
|
|
|
type Script = tengo.Script
|
|
|
|
type Compiled = tengo.Compiled
|
|
|
|
|
2024-02-26 21:04:47 +03:00
|
|
|
type Tengo struct {
|
|
|
|
head string
|
2024-04-25 22:38:04 +03:00
|
|
|
compiledScripts map[string] *Compiled
|
|
|
|
// Functions to modify
|
|
|
|
// preprocessor for
|
|
|
|
// more specific purposes.
|
|
|
|
preCode func() []byte
|
|
|
|
postCode func() []byte
|
|
|
|
preCompile func(*Script)
|
2024-02-26 21:04:47 +03:00
|
|
|
}
|
|
|
|
|
2024-04-25 22:38:04 +03:00
|
|
|
// Returns the new Tengo preprocessor
|
|
|
|
// with
|
2024-02-26 21:04:47 +03:00
|
|
|
func NewTengo() *Tengo {
|
2024-04-25 22:38:04 +03:00
|
|
|
ret := &Tengo{}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tengo *Tengo) SetPreCode(fn func() []byte) *Tengo {
|
|
|
|
tengo.preCode = fn
|
|
|
|
return tengo
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tengo *Tengo) SetPostCode(fn func() []byte) *Tengo {
|
|
|
|
tengo.postCode = fn
|
|
|
|
return tengo
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tengo *Tengo) SetPreCompile(fn func(*Script)) *Tengo {
|
|
|
|
tengo.preCompile = fn
|
|
|
|
return tengo
|
2024-02-26 21:04:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (pp *Tengo) Tags() [2]string {
|
|
|
|
return [2]string{
|
2024-04-25 22:38:04 +03:00
|
|
|
"{{",
|
|
|
|
"}}",
|
2024-02-26 21:04:47 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Simple Evaler implementation for the Tengo language
|
2024-04-25 22:38:04 +03:00
|
|
|
func (pp *Tengo) Eval(
|
|
|
|
ctx context.Context,
|
|
|
|
recompile bool,
|
|
|
|
filePath string,
|
|
|
|
codes [][]byte,
|
|
|
|
) ([][]byte, error) {
|
|
|
|
var fullCodeBuf bytes.Buffer
|
2024-02-26 21:04:47 +03:00
|
|
|
const retHead = `
|
|
|
|
__ret_one__ := ""
|
|
|
|
printf := func(format, ...vals) {
|
|
|
|
__ret_one__ += sprintf(format, vals...)
|
|
|
|
}
|
|
|
|
|
|
|
|
print := func(...vals) {
|
|
|
|
__ret_one__ += sprint(vals...)
|
|
|
|
}
|
|
|
|
|
|
|
|
println := func(...vals) {
|
|
|
|
__ret_one__ += sprintln(vals...)
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
const retSeparator = `
|
2024-04-25 22:38:04 +03:00
|
|
|
__separate(__ret_one__)
|
2024-02-26 21:04:47 +03:00
|
|
|
__ret_one__ = ""
|
|
|
|
`
|
2024-04-25 22:38:04 +03:00
|
|
|
rets := [][]byte{}
|
2024-02-26 21:04:47 +03:00
|
|
|
|
|
|
|
|
2024-04-25 22:38:04 +03:00
|
|
|
fmt.Fprint(&fullCodeBuf, retHead)
|
|
|
|
if pp.preCode != nil {
|
|
|
|
fullCodeBuf.Write(pp.preCode())
|
|
|
|
}
|
2024-02-26 21:04:47 +03:00
|
|
|
for _, code := range codes {
|
2024-04-25 22:38:04 +03:00
|
|
|
fmt.Fprintln(&fullCodeBuf, "\n" + string(code) + retSeparator)
|
|
|
|
}
|
|
|
|
if pp.postCode != nil {
|
|
|
|
fullCodeBuf.Write(pp.postCode())
|
2024-02-26 21:04:47 +03:00
|
|
|
}
|
2024-04-25 22:38:04 +03:00
|
|
|
|
|
|
|
script := tengo.NewScript(fullCodeBuf.Bytes())
|
|
|
|
if pp.preCompile != nil {
|
|
|
|
pp.preCompile(script)
|
|
|
|
}
|
|
|
|
|
2024-02-26 21:04:47 +03:00
|
|
|
err := script.Add("sprintf", &tengo.UserFunction{
|
|
|
|
Value: func(args ...tengo.Object) (tengo.Object, error){
|
|
|
|
if len(args) < 1 {
|
|
|
|
return nil, tengo.ErrWrongNumArguments
|
|
|
|
}
|
|
|
|
format, ok := tengo.ToString(args[0])
|
|
|
|
if !ok {
|
|
|
|
return nil, tengo.ErrInvalidArgumentType{
|
|
|
|
Expected: "string",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
gargs := make([]any, len(args) - 1)
|
|
|
|
for i := range gargs {
|
|
|
|
gargs[i] = tengo.ToInterface(args[i+1])
|
|
|
|
//fmt.Printf("shit: %q\n", gargs[i])
|
|
|
|
}
|
|
|
|
str := fmt.Sprintf(format, gargs...)
|
|
|
|
return tengo.FromInterface(str)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = script.Add("sprint", &tengo.UserFunction{
|
|
|
|
Value: func(args ...tengo.Object) (tengo.Object, error){
|
|
|
|
gargs := make([]any, len(args))
|
|
|
|
for i := range gargs {
|
|
|
|
gargs[i] = tengo.ToInterface(args[i])
|
|
|
|
}
|
|
|
|
str := fmt.Sprint(gargs...)
|
|
|
|
return tengo.FromInterface(str)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = script.Add("sprintln", &tengo.UserFunction{
|
|
|
|
Value: func(args ...tengo.Object) (tengo.Object, error){
|
|
|
|
gargs := make([]any, len(args))
|
|
|
|
for i := range gargs {
|
|
|
|
gargs[i] = tengo.ToInterface(args[i])
|
|
|
|
}
|
|
|
|
str := fmt.Sprintln(gargs...)
|
|
|
|
return tengo.FromInterface(str)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-04-25 22:38:04 +03:00
|
|
|
err = script.Add("__separate", &tengo.UserFunction{
|
2024-02-26 21:04:47 +03:00
|
|
|
Value: func(args ...tengo.Object) (tengo.Object, error){
|
|
|
|
if len(args) < 1 {
|
|
|
|
return nil, tengo.ErrWrongNumArguments
|
|
|
|
}
|
2024-04-25 22:38:04 +03:00
|
|
|
str, ok := tengo.ToByteSlice(args[0])
|
2024-02-26 21:04:47 +03:00
|
|
|
if !ok {
|
|
|
|
return nil, tengo.ErrInvalidArgumentType{
|
|
|
|
}
|
|
|
|
}
|
|
|
|
rets = append(rets, str)
|
|
|
|
return nil, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-04-25 22:38:04 +03:00
|
|
|
_, err = script.RunContext(ctx)
|
2024-02-26 21:04:47 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return rets, nil
|
|
|
|
}
|