fix scipt.Variable.Value()
bug (#139)
This commit is contained in:
parent
8707fd6e7f
commit
1921d907a6
5 changed files with 14 additions and 45 deletions
|
@ -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()
|
||||
}
|
||||
|
|
|
@ -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...)
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue