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.
|
2024-05-06 21:08:56 +03:00
|
|
|
preCode func(context.Context) []byte
|
|
|
|
postCode func(context.Context) []byte
|
|
|
|
preCompile func(context.Context, *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
|
|
|
|
}
|
|
|
|
|
2024-05-06 21:08:56 +03:00
|
|
|
func (tengo *Tengo) SetPreCode(fn func(context.Context) []byte) *Tengo {
|
2024-04-25 22:38:04 +03:00
|
|
|
tengo.preCode = fn
|
|
|
|
return tengo
|
|
|
|
}
|
|
|
|
|
2024-05-06 21:08:56 +03:00
|
|
|
func (tengo *Tengo) SetPostCode(fn func(context.Context) []byte) *Tengo {
|
2024-04-25 22:38:04 +03:00
|
|
|
tengo.postCode = fn
|
|
|
|
return tengo
|
|
|
|
}
|
|
|
|
|
2024-05-06 21:08:56 +03:00
|
|
|
func (tengo *Tengo) SetPreCompile(fn func(context.Context, *Script)) *Tengo {
|
2024-04-25 22:38:04 +03:00
|
|
|
tengo.preCompile = fn
|
|
|
|
return tengo
|
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,
|
2024-05-22 13:16:07 +03:00
|
|
|
preCode []byte,
|
2024-04-25 22:38:04 +03:00
|
|
|
codes [][]byte,
|
|
|
|
) ([][]byte, error) {
|
|
|
|
var fullCodeBuf bytes.Buffer
|
2024-02-26 21:04:47 +03:00
|
|
|
const retHead = `
|
2024-05-20 00:34:39 +03:00
|
|
|
context := {}
|
2024-04-25 23:27:45 +03:00
|
|
|
__ret_one__ := bytes("")
|
2024-05-17 20:46:34 +03:00
|
|
|
pp := immutable({
|
|
|
|
filepath: __filepath__,
|
2024-04-25 23:27:45 +03:00
|
|
|
printf : func(format, ...vals) {
|
|
|
|
__ret_one__ += __sprintf__(format, vals...)
|
|
|
|
},
|
2024-02-26 21:04:47 +03:00
|
|
|
|
2024-04-25 23:27:45 +03:00
|
|
|
print : func(...vals) {
|
|
|
|
__ret_one__ += __sprint__(vals...)
|
|
|
|
},
|
|
|
|
println : func(...vals) {
|
|
|
|
__ret_one__ += __sprintln__(vals...)
|
|
|
|
},
|
|
|
|
write_raw : func(bts) {
|
|
|
|
__ret_one__ += bytes(bts)
|
|
|
|
}
|
2024-05-17 20:46:34 +03:00
|
|
|
})
|
2024-02-26 21:04:47 +03:00
|
|
|
`
|
|
|
|
|
|
|
|
const retSeparator = `
|
2024-04-25 23:27:45 +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)
|
2024-05-22 13:16:07 +03:00
|
|
|
if preCode != nil {
|
|
|
|
fmt.Fprintln(
|
|
|
|
&fullCodeBuf,
|
|
|
|
string(preCode)+"\n",
|
|
|
|
)
|
2024-05-17 23:55:02 +03:00
|
|
|
}
|
2024-05-22 13:16:07 +03:00
|
|
|
|
2024-04-25 22:38:04 +03:00
|
|
|
if pp.preCode != nil {
|
2024-05-06 21:08:56 +03:00
|
|
|
fullCodeBuf.Write(pp.preCode(ctx))
|
2024-05-22 13:16:07 +03:00
|
|
|
fullCodeBuf.Write([]byte(retSeparator))
|
2024-04-25 22:38:04 +03:00
|
|
|
}
|
2024-05-22 13:16:07 +03:00
|
|
|
|
2024-02-26 21:04:47 +03:00
|
|
|
for _, code := range codes {
|
2024-05-22 13:16:07 +03:00
|
|
|
fmt.Fprintln(
|
|
|
|
&fullCodeBuf,
|
|
|
|
"\n" + string(code) + retSeparator,
|
|
|
|
)
|
2024-04-25 22:38:04 +03:00
|
|
|
}
|
2024-05-22 13:16:07 +03:00
|
|
|
|
2024-04-25 22:38:04 +03:00
|
|
|
if pp.postCode != nil {
|
2024-05-06 21:08:56 +03:00
|
|
|
fullCodeBuf.Write(pp.postCode(ctx))
|
2024-05-22 13:16:07 +03:00
|
|
|
fullCodeBuf.Write([]byte(retSeparator))
|
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 {
|
2024-05-06 21:08:56 +03:00
|
|
|
pp.preCompile(ctx, script)
|
2024-04-25 22:38:04 +03:00
|
|
|
}
|
|
|
|
|
2024-05-17 20:46:34 +03:00
|
|
|
script.Add("__filepath__", filePath)
|
|
|
|
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",
|
2024-02-26 21:04:47 +03:00
|
|
|
}
|
2024-05-17 20:46:34 +03:00
|
|
|
}
|
|
|
|
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([]byte(str))
|
2024-02-26 21:04:47 +03:00
|
|
|
},
|
2024-05-17 20:46:34 +03:00
|
|
|
})
|
|
|
|
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([]byte(str))
|
2024-02-26 21:04:47 +03:00
|
|
|
},
|
2024-05-17 20:46:34 +03:00
|
|
|
})
|
|
|
|
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([]byte(str))
|
2024-02-26 21:04:47 +03:00
|
|
|
},
|
2024-05-17 20:46:34 +03:00
|
|
|
})
|
|
|
|
script.Add("__separate__", &tengo.UserFunction{
|
|
|
|
Value: func(args ...tengo.Object) (tengo.Object, error){
|
|
|
|
if len(args) < 1 {
|
|
|
|
return nil, tengo.ErrWrongNumArguments
|
|
|
|
}
|
|
|
|
bts, ok := tengo.ToByteSlice(args[0])
|
|
|
|
if !ok {
|
|
|
|
return nil, tengo.ErrInvalidArgumentType{
|
2024-02-26 21:04:47 +03:00
|
|
|
}
|
2024-05-17 20:46:34 +03:00
|
|
|
}
|
|
|
|
rets = append(rets, bts)
|
|
|
|
return nil, nil
|
2024-02-26 21:04:47 +03:00
|
|
|
},
|
2024-05-17 20:46:34 +03:00
|
|
|
})
|
2024-02-26 21:04:47 +03:00
|
|
|
|
2024-05-17 20:46:34 +03:00
|
|
|
_, err := script.RunContext(ctx)
|
2024-02-26 21:04:47 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return rets, nil
|
|
|
|
}
|