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-06-11 11:46:20 +03:00
|
|
|
"strconv"
|
2024-02-26 21:04:47 +03:00
|
|
|
)
|
|
|
|
|
2024-06-23 13:04:37 +03:00
|
|
|
type CompiledMap map[string] *Compiled
|
|
|
|
|
2024-04-25 22:38:04 +03:00
|
|
|
type Script = tengo.Script
|
|
|
|
type Compiled = tengo.Compiled
|
|
|
|
|
2024-06-23 13:04:37 +03:00
|
|
|
// The type describes
|
|
|
|
// customizable way to evaluate
|
|
|
|
// files.
|
2024-02-26 21:04:47 +03:00
|
|
|
type Tengo struct {
|
2024-04-25 22:38:04 +03:00
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2024-04-25 22:38:04 +03:00
|
|
|
func (pp *Tengo) Eval(
|
|
|
|
ctx context.Context,
|
2024-06-23 13:04:37 +03:00
|
|
|
// File path to give it.
|
2024-04-25 22:38:04 +03:00
|
|
|
filePath string,
|
2024-06-23 13:04:37 +03:00
|
|
|
// Static text pieces.
|
2024-06-09 14:51:03 +03:00
|
|
|
texts [][]byte,
|
2024-06-23 13:04:37 +03:00
|
|
|
// The code that comes before any
|
|
|
|
// dynamic code.
|
2024-05-22 13:16:07 +03:00
|
|
|
preCode []byte,
|
2024-06-23 13:04:37 +03:00
|
|
|
// General code.
|
2024-04-25 22:38:04 +03:00
|
|
|
codes [][]byte,
|
2024-06-23 13:04:37 +03:00
|
|
|
) (*Compiled, []byte, error) {
|
2024-06-09 14:51:03 +03:00
|
|
|
var fullCodeBuf, retBuf bytes.Buffer
|
2024-02-26 21:04:47 +03:00
|
|
|
const retHead = `
|
2024-05-20 00:34:39 +03:00
|
|
|
context := {}
|
2024-05-17 20:46:34 +03:00
|
|
|
pp := immutable({
|
2024-06-09 14:51:03 +03:00
|
|
|
filepath: __pp_filepath__,
|
|
|
|
printf : __pp_printf__,
|
|
|
|
print : __pp_print__,
|
|
|
|
println : __pp_println__,
|
|
|
|
write_raw : __pp_write_raw__
|
2024-05-17 20:46:34 +03:00
|
|
|
})
|
2024-02-26 21:04:47 +03:00
|
|
|
`
|
|
|
|
|
|
|
|
const retSeparator = `
|
2024-06-09 14:51:03 +03:00
|
|
|
__separate__()
|
2024-02-26 21:04:47 +03:00
|
|
|
`
|
|
|
|
|
2024-04-25 22:38:04 +03:00
|
|
|
fmt.Fprint(&fullCodeBuf, retHead)
|
2024-06-09 14:51:03 +03:00
|
|
|
|
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-06-09 14:51:03 +03:00
|
|
|
for i, code := range codes {
|
|
|
|
fmt.Fprintf(
|
|
|
|
&fullCodeBuf,
|
|
|
|
"\n__pp_write_raw__(%q)\n",
|
|
|
|
texts[i],
|
|
|
|
)
|
2024-05-22 13:16:07 +03:00
|
|
|
fmt.Fprintln(
|
|
|
|
&fullCodeBuf,
|
|
|
|
"\n" + string(code) + retSeparator,
|
|
|
|
)
|
2024-04-25 22:38:04 +03:00
|
|
|
}
|
2024-06-09 14:51:03 +03:00
|
|
|
fmt.Fprintf(
|
|
|
|
&fullCodeBuf,
|
|
|
|
"\n__pp_write_raw__(%q)\n",
|
|
|
|
texts[len(texts)-1],
|
|
|
|
)
|
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-06-09 14:51:03 +03:00
|
|
|
script.Add("__pp_filepath__", filePath)
|
|
|
|
script.Add("__pp_printf__", &tengo.UserFunction{
|
2024-05-17 20:46:34 +03:00
|
|
|
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])
|
|
|
|
}
|
2024-06-09 14:51:03 +03:00
|
|
|
fmt.Fprintf(&retBuf, format, gargs...)
|
|
|
|
return nil, nil
|
|
|
|
//return tengo.FromInterface([]byte(str))
|
2024-02-26 21:04:47 +03:00
|
|
|
},
|
2024-05-17 20:46:34 +03:00
|
|
|
})
|
2024-06-09 14:51:03 +03:00
|
|
|
script.Add("__pp_print__", &tengo.UserFunction{
|
2024-05-17 20:46:34 +03:00
|
|
|
Value: func(args ...tengo.Object) (tengo.Object, error){
|
|
|
|
gargs := make([]any, len(args))
|
|
|
|
for i := range gargs {
|
|
|
|
gargs[i] = tengo.ToInterface(args[i])
|
|
|
|
}
|
2024-06-09 14:51:03 +03:00
|
|
|
fmt.Fprint(&retBuf, gargs...)
|
|
|
|
return nil, nil
|
|
|
|
//return tengo.FromInterface([]byte(str))
|
2024-02-26 21:04:47 +03:00
|
|
|
},
|
2024-05-17 20:46:34 +03:00
|
|
|
})
|
2024-06-09 14:51:03 +03:00
|
|
|
script.Add("__pp_println__", &tengo.UserFunction{
|
2024-05-17 20:46:34 +03:00
|
|
|
Value: func(args ...tengo.Object) (tengo.Object, error){
|
|
|
|
gargs := make([]any, len(args))
|
|
|
|
for i := range gargs {
|
|
|
|
gargs[i] = tengo.ToInterface(args[i])
|
|
|
|
}
|
2024-06-09 14:51:03 +03:00
|
|
|
fmt.Fprintln(&retBuf, gargs...)
|
|
|
|
return nil, nil
|
|
|
|
//return tengo.FromInterface([]byte(str))
|
|
|
|
},
|
|
|
|
})
|
|
|
|
script.Add("__pp_write_raw__", &tengo.UserFunction{
|
|
|
|
Value: func(args ...tengo.Object) (tengo.Object, error){
|
|
|
|
bt := make([][]byte, len(args))
|
|
|
|
for i, o := range args {
|
|
|
|
bts, ok := tengo.ToByteSlice(o)
|
|
|
|
if !ok {
|
|
|
|
return nil, tengo.ErrInvalidArgumentType{
|
2024-06-11 11:46:20 +03:00
|
|
|
Name: strconv.Itoa(i),
|
2024-06-09 14:51:03 +03:00
|
|
|
Expected: "string/bytes",
|
2024-06-11 11:46:20 +03:00
|
|
|
Found: o.TypeName(),
|
2024-06-09 14:51:03 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
bt[i] = bts
|
|
|
|
}
|
|
|
|
for _, b := range bt {
|
|
|
|
retBuf.Write(b)
|
|
|
|
}
|
|
|
|
return nil, nil
|
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){
|
2024-06-09 14:51:03 +03:00
|
|
|
return nil, nil
|
|
|
|
/*if len(args) < 1 {
|
2024-05-17 20:46:34 +03:00
|
|
|
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-06-09 14:51:03 +03:00
|
|
|
}*/
|
|
|
|
//rets = append(rets, retBuf.Bytes())
|
|
|
|
//retBuf.Reset()
|
2024-05-17 20:46:34 +03:00
|
|
|
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-06-23 13:04:37 +03:00
|
|
|
compiled, err := script.Compile()
|
2024-02-26 21:04:47 +03:00
|
|
|
if err != nil {
|
2024-06-23 13:04:37 +03:00
|
|
|
return nil, nil, err
|
2024-02-26 21:04:47 +03:00
|
|
|
}
|
2024-06-23 13:04:37 +03:00
|
|
|
// To keep everything of the changes.
|
|
|
|
compiled = compiled.Clone()
|
2024-02-26 21:04:47 +03:00
|
|
|
|
2024-06-23 13:04:37 +03:00
|
|
|
_, err = script.RunContext(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return compiled, retBuf.Bytes(), nil
|
2024-02-26 21:04:47 +03:00
|
|
|
}
|
2024-06-23 13:04:37 +03:00
|
|
|
|
|
|
|
|