fix scipt.Variable.Value() bug (#139)

This commit is contained in:
Daniel 2019-03-13 03:07:06 -07:00 committed by GitHub
parent 8707fd6e7f
commit 1921d907a6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 14 additions and 45 deletions

View file

@ -12,7 +12,7 @@ func builtinToJSON(args ...Object) (Object, error) {
return nil, ErrWrongNumArguments return nil, ErrWrongNumArguments
} }
v := objectToInterface(args[0]) v := ToInterface(args[0])
if vErr, isErr := v.(error); isErr { if vErr, isErr := v.(error); isErr {
v = vErr.Error() v = vErr.Error()
} }

View file

@ -41,7 +41,7 @@ func builtinPrintf(args ...Object) (Object, error) {
formatArgs := make([]interface{}, numArgs-1, numArgs-1) formatArgs := make([]interface{}, numArgs-1, numArgs-1)
for idx, arg := range args[1:] { for idx, arg := range args[1:] {
formatArgs[idx] = objectToInterface(arg) formatArgs[idx] = ToInterface(arg)
} }
fmt.Printf(format.Value, formatArgs...) fmt.Printf(format.Value, formatArgs...)
@ -70,7 +70,7 @@ func builtinSprintf(args ...Object) (Object, error) {
formatArgs := make([]interface{}, numArgs-1, numArgs-1) formatArgs := make([]interface{}, numArgs-1, numArgs-1)
for idx, arg := range args[1:] { for idx, arg := range args[1:] {
formatArgs[idx] = objectToInterface(arg) formatArgs[idx] = ToInterface(arg)
} }
s := fmt.Sprintf(format.Value, formatArgs...) s := fmt.Sprintf(format.Value, formatArgs...)

View file

@ -159,8 +159,8 @@ func ToTime(o Object) (v time.Time, ok bool) {
return return
} }
// objectToInterface attempts to convert an object o to an interface{} value // ToInterface attempts to convert an object o to an interface{} value
func objectToInterface(o Object) (res interface{}) { func ToInterface(o Object) (res interface{}) {
switch o := o.(type) { switch o := o.(type) {
case *Int: case *Int:
res = o.Value res = o.Value
@ -177,27 +177,29 @@ func objectToInterface(o Object) (res interface{}) {
case *Array: case *Array:
res = make([]interface{}, len(o.Value)) res = make([]interface{}, len(o.Value))
for i, val := range o.Value { for i, val := range o.Value {
res.([]interface{})[i] = objectToInterface(val) res.([]interface{})[i] = ToInterface(val)
} }
case *ImmutableArray: case *ImmutableArray:
res = make([]interface{}, len(o.Value)) res = make([]interface{}, len(o.Value))
for i, val := range o.Value { for i, val := range o.Value {
res.([]interface{})[i] = objectToInterface(val) res.([]interface{})[i] = ToInterface(val)
} }
case *Map: case *Map:
res = make(map[string]interface{}) res = make(map[string]interface{})
for key, v := range o.Value { for key, v := range o.Value {
res.(map[string]interface{})[key] = objectToInterface(v) res.(map[string]interface{})[key] = ToInterface(v)
} }
case *ImmutableMap: case *ImmutableMap:
res = make(map[string]interface{}) res = make(map[string]interface{})
for key, v := range o.Value { for key, v := range o.Value {
res.(map[string]interface{})[key] = objectToInterface(v) res.(map[string]interface{})[key] = ToInterface(v)
} }
case *Time: case *Time:
res = o.Value res = o.Value
case *Error: case *Error:
res = errors.New(o.String()) res = errors.New(o.String())
case *Undefined:
res = nil
case Object: case Object:
return o return o
} }

View file

@ -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
}

View file

@ -32,7 +32,7 @@ func (v *Variable) Name() string {
// Value returns an empty interface of the variable value. // Value returns an empty interface of the variable value.
func (v *Variable) Value() interface{} { func (v *Variable) Value() interface{} {
return objectToInterface(*v.value) return objects.ToInterface(*v.value)
} }
// ValueType returns the name of the value type. // ValueType returns the name of the value type.
@ -87,7 +87,7 @@ func (v *Variable) Array() []interface{} {
case *objects.Array: case *objects.Array:
var arr []interface{} var arr []interface{}
for _, e := range val.Value { for _, e := range val.Value {
arr = append(arr, objectToInterface(e)) arr = append(arr, objects.ToInterface(e))
} }
return arr return arr
} }
@ -102,7 +102,7 @@ func (v *Variable) Map() map[string]interface{} {
case *objects.Map: case *objects.Map:
kv := make(map[string]interface{}) kv := make(map[string]interface{})
for mk, mv := range val.Value { for mk, mv := range val.Value {
kv[mk] = objectToInterface(mv) kv[mk] = objects.ToInterface(mv)
} }
return kv return kv
} }