gopp/tengo.go

216 lines
4.5 KiB
Go
Raw Permalink Normal View History

package tpp
import (
"github.com/d5/tengo/v2"
"fmt"
"context"
"bytes"
"strconv"
)
type CompiledMap map[string] *Compiled
type Script = tengo.Script
type Compiled = tengo.Compiled
// The type describes
// customizable way to evaluate
// files.
type Tengo struct {
// Functions to modify
// preprocessor for
// more specific purposes.
preCode func(context.Context) []byte
postCode func(context.Context) []byte
preCompile func(context.Context, *Script)
}
// Returns the new Tengo preprocessor
// with
func NewTengo() *Tengo {
ret := &Tengo{}
return ret
}
func (tengo *Tengo) SetPreCode(fn func(context.Context) []byte) *Tengo {
tengo.preCode = fn
return tengo
}
func (tengo *Tengo) SetPostCode(fn func(context.Context) []byte) *Tengo {
tengo.postCode = fn
return tengo
}
func (tengo *Tengo) SetPreCompile(fn func(context.Context, *Script)) *Tengo {
tengo.preCompile = fn
return tengo
}
func (pp *Tengo) Eval(
ctx context.Context,
// File path to give it.
filePath string,
// Static text pieces.
2024-06-09 14:51:03 +03:00
texts [][]byte,
// The code that comes before any
// dynamic code.
2024-05-22 13:16:07 +03:00
preCode []byte,
// General code.
codes [][]byte,
) (*Compiled, []byte, error) {
2024-06-09 14:51:03 +03:00
var fullCodeBuf, retBuf bytes.Buffer
const retHead = `
context := {}
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__
})
`
const retSeparator = `
2024-06-09 14:51:03 +03:00
__separate__()
`
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-22 13:16:07 +03:00
if pp.preCode != nil {
fullCodeBuf.Write(pp.preCode(ctx))
2024-05-22 13:16:07 +03:00
fullCodeBuf.Write([]byte(retSeparator))
}
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-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
if pp.postCode != nil {
fullCodeBuf.Write(pp.postCode(ctx))
2024-05-22 13:16:07 +03:00
fullCodeBuf.Write([]byte(retSeparator))
}
script := tengo.NewScript(fullCodeBuf.Bytes())
if pp.preCompile != nil {
pp.preCompile(ctx, script)
}
2024-06-09 14:51:03 +03:00
script.Add("__pp_filepath__", filePath)
script.Add("__pp_printf__", &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])
}
2024-06-09 14:51:03 +03:00
fmt.Fprintf(&retBuf, format, gargs...)
return nil, nil
//return tengo.FromInterface([]byte(str))
},
})
2024-06-09 14:51:03 +03:00
script.Add("__pp_print__", &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])
}
2024-06-09 14:51:03 +03:00
fmt.Fprint(&retBuf, gargs...)
return nil, nil
//return tengo.FromInterface([]byte(str))
},
})
2024-06-09 14:51:03 +03:00
script.Add("__pp_println__", &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])
}
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{
Name: strconv.Itoa(i),
2024-06-09 14:51:03 +03:00
Expected: "string/bytes",
Found: o.TypeName(),
2024-06-09 14:51:03 +03:00
}
}
bt[i] = bts
}
for _, b := range bt {
retBuf.Write(b)
}
return nil, nil
},
})
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 {
return nil, tengo.ErrWrongNumArguments
}
bts, ok := tengo.ToByteSlice(args[0])
if !ok {
return nil, tengo.ErrInvalidArgumentType{
}
2024-06-09 14:51:03 +03:00
}*/
//rets = append(rets, retBuf.Bytes())
//retBuf.Reset()
return nil, nil
},
})
compiled, err := script.Compile()
if err != nil {
return nil, nil, err
}
// To keep everything of the changes.
compiled = compiled.Clone()
_, err = script.RunContext(ctx)
if err != nil {
return nil, nil, err
}
return compiled, retBuf.Bytes(), nil
}