Documentation
¶
Index ¶
- Variables
- func ExtendPackage(name string, deputy template.FuncMap)
- type Ixgo
- type Template
- type TemplateExtend
- type TemplateHeader
- type Wazero
- func (T *Wazero) Close() error
- func (T *Wazero) Execute(entryName string, out io.Writer, in ...any) error
- func (T *Wazero) ExecuteContext(ctx context.Context, entryName string, out io.Writer, in ...any) (err error)
- func (T *Wazero) Parse(r io.Reader) (err error)
- func (T *Wazero) ParseFile(p string) error
- func (T *Wazero) ParseText(name, content string) error
- func (T *Wazero) SetPath(root, page string)
- type Yaegi
Constants ¶
This section is empty.
Variables ¶
var TemplateFunc = template.FuncMap{ "Import": importPkg, "ForMethod": vweb.ForMethod, "ForType": vweb.ForType, "Register": builtin.Register, "DepthField": builtin.DepthField, "CopyStruct": builtin.CopyStruct, "CopyStructDeep": builtin.CopyStructDeep, "Convert": builtin.Convert, "To": builtin.To, "Init": builtin.Init, "Value": builtin.Value, "Call": call, "CallMethod": callMethod, "Defer": func(f any, args ...any) func() { return func() { call(f, args...) } }, "DeferMethod": func(f any, name string, args ...any) func() { return func() { callMethod(f, name, args...) } }, "Go": func(f func()) { go f() }, "PtrTo": func(inf any) any { return reflect.ValueOf(inf).Elem().Interface() }, "ToPtr": func(inf any) any { return reflect.ValueOf(inf).Addr().Interface() }, "Nil": func() any { return nil }, "NotNil": func(inf any) bool { return inf != nil }, "IsNil": func(inf any) bool { return inf == nil }, "Type": builtin.Type, "MustType": builtin.MustType, "Panic": builtin.Panic, "New": builtin.New, "Make": builtin.Make, "Delete": builtin.Delete, "SetUnexported": builtin.SetUnexported, "Set": builtin.Set, "GetUnexported": builtin.GetUnexported, "Get": builtin.Get, "Len": builtin.Len, "Cap": builtin.Cap, "GetSlice": builtin.GetSlice, "SetSlice": builtin.SetSlice, "Copy": builtin.Copy, "Append": builtin.Append, "Compute": builtin.Compute, "Or": builtin.Or, "And": builtin.And, "Not": builtin.Not, "LT": builtin.LT, "GT": builtin.GT, "LE": builtin.LE, "GE": builtin.GE, "EQ": builtin.EQ, "NE": builtin.NE, "TrySend": builtin.TrySend, "TryRecv": builtin.TryRecv, "Send": builtin.Send, "Recv": builtin.Recv, "Close": builtin.Close, "Error": func(v any) bool { return templateFuncMapError(v) != nil }, "NotError": func(v any) bool { return templateFuncMapError(v) == nil }, }
TemplateFunc 模板函数映射(可导出)。只读使用,初始化后不再修改,并发读安全。
Functions ¶
Types ¶
type Ixgo ¶
type Ixgo struct {
// contains filtered or unexported fields
}
Ixgo 动态页面执行引擎(基于 ixgo 解释器)。
并发安全说明:
- SetPath 与 Execute 可并发;Execute 使用快照,不受路径切换影响。
- Parse / ParseFile / ParseText 成功后原子发布新快照;失败保留旧快照, 已开始的 Execute 继续持有旧快照直到完成,不受影响。
- Close 清空快照;已在执行的 Execute 继续持有旧快照直到完成。
- 同一快照上的多个 Execute 通过 ixgoRuntime.execMu 串行执行 RunFunc。
type Template ¶
type Template struct {
// contains filtered or unexported fields
}
Template 并发安全说明:
- 同一 *Template 实例在 Parse / ParseFile / ParseText / SetPath 等写操作期间 不得被其他 goroutine 并发调用 Execute。
- 解析完成后,多个 goroutine 可安全并发调用 Execute(text/template 本身支持)。
func (*Template) SetEntryName ¶
type TemplateExtend ¶
TemplateExtend 这是个额外扩展,由于模板不能实现函数创建,只能在这里构造一个支持创建函数。 在创建的函数内部,需要使用 Args 方法读取参数,使用 Result 方法返回结果。 仅限用于template模板,自定义模板不支持
func (*TemplateExtend) Call ¶
Call 执行函数
f func([]reflect.Value) []reflect.Value 由NewFunc创建的函数
args ...interface{} 可变参数
[]interface{} 返回结果
func (*TemplateExtend) ExecuteTemplate ¶
ExecuteTemplate 调用模板
out io.Writer 模板内容返回内容写入到out
name string 模板名称
in interface{} 模板点,带外模板的内容
error 执行语法错误
type TemplateHeader ¶
type TemplateHeader struct {
EntryName string // 入口函数名
File []string // 额外加载的文件路径(相对或绝对)
DelimLeft, DelimRight string // 语法识别符(保留供扩展使用)
}
TemplateHeader 标头-模本-处理动态页面文件 从文件头部 // 注释中解析配置。
func (*TemplateHeader) OpenFile ¶
func (T *TemplateHeader) OpenFile(rootPath, pagePath string) (map[string]string, error)
OpenFile 打开文件内容
rootPath string 根目录 pagePath string 页面路径 map[string][]byte 内容,map[文件名]文件内容 error 错误,如果文件不能打开读取
说明:无论 v 是否为绝对路径,最终都会以 rootPath 为根解析; 这与“沙箱内视图”的语义一致。若需真正访问宿主绝对路径,请显式配置 preopen。
type Wazero ¶
type Wazero struct {
// RootAsFirstArg 控制 Execute 的 in[0] 与 host.Call 的关系(默认 false)。
//
// false(默认,兼容旧路径):
// - in 全部传给 host.Call(须全部可被 witgo 扁平化:标量/string/slice 等)
// - 若 in[0] != nil,同时 box 为 root,供 tf_root_handle(旧 guest 不调用则无影响)
// - 若 in[0] 是 struct/map/任意复杂 any,host.Call 仍会扁平化失败——与改前行为一致
//
// true(模板点对象推荐):
// - in[0] 仅 box 为 root,不传给 host.Call
// - in[1:] 传给 host.Call(应为可扁平类型;无额外参数时入口为 0 形参)
// - guest 用 tf_root_handle() 取点对象
RootAsFirstArg bool
// contains filtered or unexported fields
}
Wazero 封装 wazero 运行时,用于执行 WASI / Component 风格的 WASM 模板模块。
并发模型:
- 同一实例上多个 Execute / ExecuteContext 可并发(各自独立 Module 实例)。
- Close / Parse 与 Execute* 互斥:Execute* 持读锁贯穿整个生命周期, Close/Parse 持写锁,保证不会对已关闭的 Runtime/CompiledModule 继续使用。
- Runtime 与 CompiledModule 本身支持并发 Instantiate。
func (*Wazero) Close ¶
Close 关闭底层 Runtime 与编译缓存(幂等)。关闭后不应再调用 Execute。 会等待当前正在进行的 Execute 结束后再关闭,保证并发安全。
func (*Wazero) Execute ¶
Execute 是 ExecuteContext 的便捷包装,使用 context.Background(), 无法从外部取消;执行期间不会被请求级超时或取消中断。
func (*Wazero) ExecuteContext ¶
func (T *Wazero) ExecuteContext(ctx context.Context, entryName string, out io.Writer, in ...any) (err error)
ExecuteContext 同 Execute,但支持请求级 ctx 取消与超时。
依赖 T.parse 中 RuntimeConfig.WithCloseOnContextDone(true): 当 ctx 被取消或超时时,wazero 会强制关闭当前 WASM 实例, 并让所有挂起的 Guest 调用返回 context.Canceled / context.DeadlineExceeded。
in[0] 的语义由 T.RootAsFirstArg 控制(见 Wazero 结构体注释)。
type Yaegi ¶
type Yaegi struct {
// contains filtered or unexported fields
}
Yaegi 动态页面解释器,基于 Yaegi 实现 Go 模板/脚本执行。
并发安全说明:
- 同一 *Yaegi 实例在 Parse / ParseFile / ParseText / Close 等写操作期间, 不得被其他 goroutine 并发调用 Execute。
- 解析完成后,多个 goroutine 可安全并发调用 Execute 执行不同入口函数: Execute 在入口处通过 atomic.Pointer 加载解释器快照,并持有该快照直到本次调用结束; 后续 Parse 替换解释器时,旧快照由 mainFunc 闭包引用保持存活,不会被提前回收。
- mainFunc 缓存与解释器快照强相关:Parse 更新 interpreRef 时同步清空 mainFunc, 确保新调用不会复用旧解释器产生的入口函数。
func (*Yaegi) Close ¶
Close 关闭解释器(当前无状态资源,保留接口兼容性)。 关闭后 interpreRef 置空、mainFunc 缓存清空;已在执行中的 Execute 继续持有旧快照直到完成。