vweb_dynamic

package module
v0.0.0-...-c4bdc0a Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 10, 2026 License: Apache-2.0 Imports: 47 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

View Source
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

func ExtendPackage

func ExtendPackage(name string, deputy template.FuncMap)

ExtendPackage 扩展模板的包

name string				包名
deputy map[string]any 	函数集

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。

func (*Ixgo) Close

func (T *Ixgo) Close() error

Close 释放资源(当前 ixgo 无显式释放需求,预留接口)。 清空 cur 快照;已在执行的 Execute 继续持有旧快照直到完成。

func (*Ixgo) Execute

func (T *Ixgo) Execute(entryName string, out io.Writer, in ...any) (err error)

Execute 执行入口函数并将结果写入 out。 入口处加载一次 cur 快照并全程持有;Parse/Close 并发切换不影响本次调用。

func (*Ixgo) Parse

func (T *Ixgo) Parse(r io.Reader) (err error)

Parse 从 Reader 解析动态页面

func (*Ixgo) ParseFile

func (T *Ixgo) ParseFile(path string) error

ParseFile 从文件解析动态页面

func (*Ixgo) ParseText

func (T *Ixgo) ParseText(name, content string) error

ParseText 从字符串解析动态页面

func (*Ixgo) SetPath

func (T *Ixgo) SetPath(root, page string)

SetPath 设置根目录与页面路径

type Template

type Template struct {
	// contains filtered or unexported fields
}

Template 并发安全说明:

  • 同一 *Template 实例在 Parse / ParseFile / ParseText / SetPath 等写操作期间 不得被其他 goroutine 并发调用 Execute。
  • 解析完成后,多个 goroutine 可安全并发调用 Execute(text/template 本身支持)。

func (*Template) Execute

func (T *Template) Execute(out io.Writer, in any) error

func (*Template) Parse

func (T *Template) Parse(r io.Reader) error

func (*Template) ParseFile

func (T *Template) ParseFile(path string) error

func (*Template) ParseText

func (T *Template) ParseText(name, content string) error

func (*Template) SetEntryName

func (T *Template) SetEntryName(name string)

func (*Template) SetPath

func (T *Template) SetPath(root, page string)

type TemplateExtend

type TemplateExtend struct {
	*template.Template
}

TemplateExtend 这是个额外扩展,由于模板不能实现函数创建,只能在这里构造一个支持创建函数。 在创建的函数内部,需要使用 Args 方法读取参数,使用 Result 方法返回结果。 仅限用于template模板,自定义模板不支持

func (*TemplateExtend) Call

func (T *TemplateExtend) Call(f func([]reflect.Value) []reflect.Value, args ...any) []any

Call 执行函数

f func([]reflect.Value) []reflect.Value	由NewFunc创建的函数
args ...interface{}						可变参数
[]interface{}							返回结果

func (*TemplateExtend) ExecuteTemplate

func (T *TemplateExtend) ExecuteTemplate(out io.Writer, name string, in any) error

ExecuteTemplate 调用模板

out io.Writer	模板内容返回内容写入到out
name string		模板名称
in interface{}	模板点,带外模板的内容
error			执行语法错误

func (*TemplateExtend) NewFunc

func (T *TemplateExtend) NewFunc(name string) (f func([]reflect.Value) []reflect.Value, err error)

NewFunc 构建一个新的函数,仅限在template中使用

func([]reflect.Value) []reflect.Value)	新的函数

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

func (T *Wazero) Close() error

Close 关闭底层 Runtime 与编译缓存(幂等)。关闭后不应再调用 Execute。 会等待当前正在进行的 Execute 结束后再关闭,保证并发安全。

func (*Wazero) Execute

func (T *Wazero) Execute(entryName string, out io.Writer, in ...any) error

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 结构体注释)。

func (*Wazero) Parse

func (T *Wazero) Parse(r io.Reader) (err error)

Parse 从 io.Reader 解析并编译 WASM。 若此前已解析过,会先安全关闭旧 Runtime 再编译新模块(可重入)。

func (*Wazero) ParseFile

func (T *Wazero) ParseFile(p string) error

ParseFile 从文件路径解析并编译 WASM。

func (*Wazero) ParseText

func (T *Wazero) ParseText(name, content string) error

ParseText 从字符串内容解析(实现原先缺失的功能)。

func (*Wazero) SetPath

func (T *Wazero) SetPath(root, page string)

SetPath 设置根目录与页面路径(用于 FS 挂载与默认入口名)。 并发安全:可与 Execute 并发调用(读侧使用快照)。

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

func (T *Yaegi) Close() error

Close 关闭解释器(当前无状态资源,保留接口兼容性)。 关闭后 interpreRef 置空、mainFunc 缓存清空;已在执行中的 Execute 继续持有旧快照直到完成。

func (*Yaegi) Execute

func (T *Yaegi) Execute(entryName string, out io.Writer, in ...any) (err error)

Execute 执行指定入口函数,并将结果写入 out。 支持返回 string / []byte / io.Reader / fmt.Stringer / error 等常见类型。 若函数返回 (T, error) 且 error 非 nil,则直接返回该 error。

执行期间持有 interpreRef 的一次快照,Parse/Close 并发切换解释器不会影响本次调用; 旧解释器由本地 mainFunc 的闭包引用保持存活,直到本次调用结束。

func (*Yaegi) Parse

func (T *Yaegi) Parse(r io.Reader) (err error)

Parse 从 Reader 解析模板源码

func (*Yaegi) ParseFile

func (T *Yaegi) ParseFile(p string) error

ParseFile 从文件路径解析

func (*Yaegi) ParseText

func (T *Yaegi) ParseText(name, content string) error

ParseText 从字符串解析

func (*Yaegi) SetPath

func (T *Yaegi) SetPath(root, page string)

SetPath 设置根目录与当前页面路径

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL