From 42177c60a63c15758a8f3020ccdad03b9e5d2c88 Mon Sep 17 00:00:00 2001 From: fy Date: Fri, 19 Jul 2024 20:57:57 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9E=E5=87=BD=E6=95=B0=20?= =?UTF-8?q?loadRaw?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- builtin_functions.go | 36 ++++++++++++++++++++++++++++++------ 1 file changed, 30 insertions(+), 6 deletions(-) diff --git a/builtin_functions.go b/builtin_functions.go index 525e60a6..556f2d08 100644 --- a/builtin_functions.go +++ b/builtin_functions.go @@ -128,7 +128,7 @@ func funcTypeId(ctx *Context, this *VMValue, params []*VMValue) *VMValue { return NewIntVal(IntType(params[0].TypeId)) } -func funcLoad(ctx *Context, this *VMValue, params []*VMValue) *VMValue { +func funcLoadBase(ctx *Context, this *VMValue, params []*VMValue, isRaw bool) *VMValue { v := params[0] if v.TypeId != VMTypeString { ctx.Error = errors.New("(load)类型错误: 参数类型必须为str") @@ -136,16 +136,36 @@ func funcLoad(ctx *Context, this *VMValue, params []*VMValue) *VMValue { } name := v.Value.(string) - val := ctx.LoadName(name, false, true) + val := ctx.LoadName(name, true, true) if ctx.Error != nil { return nil } + // computed 回调 + if ctx.Config.HookFuncValueLoadOverwriteBeforeComputed != nil { + val = ctx.Config.HookFuncValueLoadOverwriteBeforeComputed(ctx, name, val) + } + + if !isRaw && val.TypeId == VMTypeComputedValue { + val = val.ComputedExecute(ctx, nil) + if ctx.Error != nil { + return nil + } + } + if ctx.Config.HookFuncValueLoadOverwrite != nil { - val = ctx.Config.HookFuncValueLoadOverwrite(ctx, name, val, nil) + val = ctx.Config.HookFuncValueLoadOverwrite(ctx, name, val, &BufferSpan{}) } - return val.Clone() + return val +} + +func funcLoad(ctx *Context, this *VMValue, params []*VMValue) *VMValue { + return funcLoadBase(ctx, this, params, false) +} + +func funcLoadRaw(ctx *Context, this *VMValue, params []*VMValue) *VMValue { + return funcLoadBase(ctx, this, params, true) } func funcDir(ctx *Context, this *VMValue, params []*VMValue) *VMValue { @@ -188,8 +208,9 @@ var builtinValues = map[string]*VMValue{ "str": nnf(&ndf{"str", []string{"value"}, nil, nil, funcStr}), "bool": nnf(&ndf{"bool", []string{"value"}, nil, nil, funcBool}), - "repr": nnf(&ndf{"repr", []string{"value"}, nil, nil, funcRepr}), - "load": nnf(&ndf{"load", []string{"value"}, nil, nil, nil}), + "repr": nnf(&ndf{"repr", []string{"value"}, nil, nil, funcRepr}), + "load": nnf(&ndf{"load", []string{"value"}, nil, nil, nil}), + "loadRaw": nnf(&ndf{"loadRaw", []string{"value"}, nil, nil, nil}), // TODO: roll() @@ -203,6 +224,9 @@ func _init() bool { // 因循环引用问题无法在上面声明 nfd, _ := builtinValues["load"].ReadNativeFunctionData() nfd.NativeFunc = funcLoad + + nfd, _ = builtinValues["loadRaw"].ReadNativeFunctionData() + nfd.NativeFunc = funcLoadRaw return false }