feat: screw the recompile, push it to the user.

This commit is contained in:
Andrey Parhomenko 2024-06-23 15:04:37 +05:00
parent 0e0950172d
commit ea31b462d8
3 changed files with 31 additions and 21 deletions

16
main.go
View file

@ -26,10 +26,9 @@ func New(tengo *Tengo ) *Preprocessor {
func (pp *Preprocessor) Process( func (pp *Preprocessor) Process(
ctx context.Context, ctx context.Context,
recompile bool,
filePath string, filePath string,
data []byte, data []byte,
) ([]byte, error) { ) (*Compiled, []byte, error) {
//var b bytes.Buffer //var b bytes.Buffer
preCode := []byte(nil) preCode := []byte(nil)
texts := [][]byte{} texts := [][]byte{}
@ -38,7 +37,7 @@ func (pp *Preprocessor) Process(
if bytes.HasPrefix(data, pref) { if bytes.HasPrefix(data, pref) {
idxEnd := bytes.Index(data, pp.tags[1]) idxEnd := bytes.Index(data, pp.tags[1])
if idxEnd < 0 { if idxEnd < 0 {
return nil, UnexpectedError{ return nil, nil, UnexpectedError{
What: "pre-code start tag", What: "pre-code start tag",
} }
} }
@ -52,14 +51,14 @@ func (pp *Preprocessor) Process(
//fmt.Printf("cock %d %d %d\n", last, idxStart, idxEnd) //fmt.Printf("cock %d %d %d\n", last, idxStart, idxEnd)
if idxStart < 0 { if idxStart < 0 {
if idxEnd >= 0 { if idxEnd >= 0 {
return nil, UnexpectedError{ return nil, nil, UnexpectedError{
What: "end tag", What: "end tag",
} }
} }
texts = append(texts, data) texts = append(texts, data)
break break
} else if idxEnd < 0 { } else if idxEnd < 0 {
return nil, UnexpectedError{ return nil, nil, UnexpectedError{
What: "start tag", What: "start tag",
} }
} }
@ -73,18 +72,17 @@ func (pp *Preprocessor) Process(
data = data[idxEnd + len(pp.tags[1]):] data = data[idxEnd + len(pp.tags[1]):]
} }
ret, err := pp.tengo.Eval( compiled, ret, err := pp.tengo.Eval(
ctx, ctx,
recompile,
filePath, filePath,
texts, texts,
preCode, preCode,
codes, codes,
) )
if err != nil { if err != nil {
return nil, err return nil, nil, err
} }
return ret, nil return compiled, ret, nil
} }

View file

@ -8,12 +8,15 @@ import (
"strconv" "strconv"
) )
type CompiledMap map[string] *Compiled
type Script = tengo.Script type Script = tengo.Script
type Compiled = tengo.Compiled type Compiled = tengo.Compiled
// The type describes
// customizable way to evaluate
// files.
type Tengo struct { type Tengo struct {
head string
compiledScripts map[string] *Compiled
// Functions to modify // Functions to modify
// preprocessor for // preprocessor for
// more specific purposes. // more specific purposes.
@ -44,15 +47,18 @@ func (tengo *Tengo) SetPreCompile(fn func(context.Context, *Script)) *Tengo {
return tengo return tengo
} }
// Simple Evaler implementation for the Tengo language
func (pp *Tengo) Eval( func (pp *Tengo) Eval(
ctx context.Context, ctx context.Context,
recompile bool, // File path to give it.
filePath string, filePath string,
// Static text pieces.
texts [][]byte, texts [][]byte,
// The code that comes before any
// dynamic code.
preCode []byte, preCode []byte,
// General code.
codes [][]byte, codes [][]byte,
) ([]byte, error) { ) (*Compiled, []byte, error) {
var fullCodeBuf, retBuf bytes.Buffer var fullCodeBuf, retBuf bytes.Buffer
const retHead = ` const retHead = `
context := {} context := {}
@ -68,8 +74,6 @@ func (pp *Tengo) Eval(
const retSeparator = ` const retSeparator = `
__separate__() __separate__()
` `
//rets := [][]byte{}
fmt.Fprint(&fullCodeBuf, retHead) fmt.Fprint(&fullCodeBuf, retHead)
@ -193,10 +197,19 @@ func (pp *Tengo) Eval(
}, },
}) })
_, err := script.RunContext(ctx) compiled, err := script.Compile()
if err != nil { if err != nil {
return nil, err 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 retBuf.Bytes(), nil return compiled, retBuf.Bytes(), nil
} }

View file

@ -42,9 +42,8 @@ var Tool = mtool.T("tpp").Func(func(flags *mtool.Flags){
log.Println("read error:", err) log.Println("read error:", err)
continue continue
} }
out, err := pp.Process( _, out, err := pp.Process(
context.Background(), context.Background(),
true,
pth, pth,
bts, bts,
) )