lang: funcs: structs: Add some extra safety checks

Not sure if these are even needed.
This commit is contained in:
James Shubin
2025-03-17 02:28:40 -04:00
parent e71b11f843
commit 1bb1e056c4
2 changed files with 20 additions and 0 deletions

View File

@@ -111,6 +111,16 @@ func (obj *ConstFunc) Stream(ctx context.Context) error {
// Call this function with the input args and return the value if it is possible
// to do so at this time.
func (obj *ConstFunc) Call(ctx context.Context, args []types.Value) (types.Value, error) {
if obj.Info() == nil {
return nil, fmt.Errorf("info is empty")
}
if obj.Info().Sig == nil {
return nil, fmt.Errorf("sig is empty")
}
if i, j := len(args), len(obj.Info().Sig.Ord); i != j {
return nil, fmt.Errorf("arg length doesn't match, got %d, exp: %d", i, j)
}
if obj.Value == nil {
return nil, fmt.Errorf("no value available from const")
}

View File

@@ -148,6 +148,16 @@ func (obj *IfFunc) Stream(ctx context.Context) error {
// to do so at this time.
// XXX: Is is correct to implement this here for this particular function?
func (obj *IfFunc) Call(ctx context.Context, args []types.Value) (types.Value, error) {
if obj.Info() == nil {
return nil, fmt.Errorf("info is empty")
}
if obj.Info().Sig == nil {
return nil, fmt.Errorf("sig is empty")
}
if i, j := len(args), len(obj.Info().Sig.Ord); i != j {
return nil, fmt.Errorf("arg length doesn't match, got %d, exp: %d", i, j)
}
if c := args[0].Bool(); c {
return args[1], nil // true branch
}