Skip to content

Commit

Permalink
feat: 新增函数 loadRaw
Browse files Browse the repository at this point in the history
  • Loading branch information
fy0 committed Jul 19, 2024
1 parent 313e972 commit 42177c6
Showing 1 changed file with 30 additions and 6 deletions.
36 changes: 30 additions & 6 deletions builtin_functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,24 +128,44 @@ 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")
return nil
}

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 {
Expand Down Expand Up @@ -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()

Expand All @@ -203,6 +224,9 @@ func _init() bool {
// 因循环引用问题无法在上面声明
nfd, _ := builtinValues["load"].ReadNativeFunctionData()
nfd.NativeFunc = funcLoad

nfd, _ = builtinValues["loadRaw"].ReadNativeFunctionData()
nfd.NativeFunc = funcLoadRaw
return false
}

Expand Down

0 comments on commit 42177c6

Please sign in to comment.