diff --git a/objects/builtin_json.go b/objects/builtin_json.go index d792212..411c50d 100644 --- a/objects/builtin_json.go +++ b/objects/builtin_json.go @@ -12,7 +12,7 @@ func builtinToJSON(args ...Object) (Object, error) { return nil, ErrWrongNumArguments } - v := objectToInterface(args[0]) + v := ToInterface(args[0]) if vErr, isErr := v.(error); isErr { v = vErr.Error() } diff --git a/objects/builtin_print.go b/objects/builtin_print.go index 58f2261..66f5e86 100644 --- a/objects/builtin_print.go +++ b/objects/builtin_print.go @@ -41,7 +41,7 @@ func builtinPrintf(args ...Object) (Object, error) { formatArgs := make([]interface{}, numArgs-1, numArgs-1) for idx, arg := range args[1:] { - formatArgs[idx] = objectToInterface(arg) + formatArgs[idx] = ToInterface(arg) } fmt.Printf(format.Value, formatArgs...) @@ -70,7 +70,7 @@ func builtinSprintf(args ...Object) (Object, error) { formatArgs := make([]interface{}, numArgs-1, numArgs-1) for idx, arg := range args[1:] { - formatArgs[idx] = objectToInterface(arg) + formatArgs[idx] = ToInterface(arg) } s := fmt.Sprintf(format.Value, formatArgs...) diff --git a/objects/conversion.go b/objects/conversion.go index 306fdb1..d7cb3a0 100644 --- a/objects/conversion.go +++ b/objects/conversion.go @@ -159,8 +159,8 @@ func ToTime(o Object) (v time.Time, ok bool) { return } -// objectToInterface attempts to convert an object o to an interface{} value -func objectToInterface(o Object) (res interface{}) { +// ToInterface attempts to convert an object o to an interface{} value +func ToInterface(o Object) (res interface{}) { switch o := o.(type) { case *Int: res = o.Value @@ -177,27 +177,29 @@ func objectToInterface(o Object) (res interface{}) { case *Array: res = make([]interface{}, len(o.Value)) for i, val := range o.Value { - res.([]interface{})[i] = objectToInterface(val) + res.([]interface{})[i] = ToInterface(val) } case *ImmutableArray: res = make([]interface{}, len(o.Value)) for i, val := range o.Value { - res.([]interface{})[i] = objectToInterface(val) + res.([]interface{})[i] = ToInterface(val) } case *Map: res = make(map[string]interface{}) for key, v := range o.Value { - res.(map[string]interface{})[key] = objectToInterface(v) + res.(map[string]interface{})[key] = ToInterface(v) } case *ImmutableMap: res = make(map[string]interface{}) for key, v := range o.Value { - res.(map[string]interface{})[key] = objectToInterface(v) + res.(map[string]interface{})[key] = ToInterface(v) } case *Time: res = o.Value case *Error: res = errors.New(o.String()) + case *Undefined: + res = nil case Object: return o } diff --git a/script/conversion.go b/script/conversion.go deleted file mode 100644 index c35c141..0000000 --- a/script/conversion.go +++ /dev/null @@ -1,33 +0,0 @@ -package script - -import ( - "github.com/d5/tengo/objects" -) - -func objectToInterface(o objects.Object) interface{} { - switch val := o.(type) { - case *objects.Array: - return val.Value - case *objects.Map: - return val.Value - case *objects.Int: - return val.Value - case *objects.Float: - return val.Value - case *objects.Bool: - if val == objects.TrueValue { - return true - } - return false - case *objects.Char: - return val.Value - case *objects.String: - return val.Value - case *objects.Bytes: - return val.Value - case *objects.Undefined: - return nil - } - - return o -} diff --git a/script/variable.go b/script/variable.go index c5e01bd..e20d3c0 100644 --- a/script/variable.go +++ b/script/variable.go @@ -32,7 +32,7 @@ func (v *Variable) Name() string { // Value returns an empty interface of the variable value. func (v *Variable) Value() interface{} { - return objectToInterface(*v.value) + return objects.ToInterface(*v.value) } // ValueType returns the name of the value type. @@ -87,7 +87,7 @@ func (v *Variable) Array() []interface{} { case *objects.Array: var arr []interface{} for _, e := range val.Value { - arr = append(arr, objectToInterface(e)) + arr = append(arr, objects.ToInterface(e)) } return arr } @@ -102,7 +102,7 @@ func (v *Variable) Map() map[string]interface{} { case *objects.Map: kv := make(map[string]interface{}) for mk, mv := range val.Value { - kv[mk] = objectToInterface(mv) + kv[mk] = objects.ToInterface(mv) } return kv }