This commit is contained in:
Andrey Parhomenko 2024-06-26 20:29:38 +05:00
parent ea31b462d8
commit c2f2c344c2

View file

@ -63,11 +63,12 @@ func (pp *Tengo) Eval(
const retHead = ` const retHead = `
context := {} context := {}
pp := immutable({ pp := immutable({
filepath: __pp_filepath__, fpath: __pp_filepath__,
printf : __pp_printf__, printf : __pp_printf__,
print : __pp_print__, print : __pp_print__,
println : __pp_println__, println : __pp_println__,
write_raw : __pp_write_raw__ write_raw : __pp_write_raw__,
include: __pp_include__
}) })
` `
@ -116,6 +117,27 @@ func (pp *Tengo) Eval(
pp.preCompile(ctx, script) pp.preCompile(ctx, script)
} }
script.Add("__pp_include__", &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])
}
fmt.Fprintf(&retBuf, format, gargs...)
return nil, nil
//return tengo.FromInterface([]byte(str))
},
})
script.Add("__pp_filepath__", filePath) script.Add("__pp_filepath__", filePath)
script.Add("__pp_printf__", &tengo.UserFunction{ script.Add("__pp_printf__", &tengo.UserFunction{
Value: func(args ...tengo.Object) (tengo.Object, error){ Value: func(args ...tengo.Object) (tengo.Object, error){