From fae85000cf8de6d5f827212b25c9e9d34c405f54 Mon Sep 17 00:00:00 2001 From: Mike Bazuin Date: Fri, 18 Jan 2019 23:12:16 +0100 Subject: [PATCH] Convert returns second argument Object as it self in case of failure of conversion of first argument objects/builtin_convert.go - Changed type check of in case of args[0] failure to always return args[1] if set runtime/vm_builtin_test.go: - Changed tests according to changes in objects/builtin_convert.go --- objects/builtin_convert.go | 29 +++++------------------------ runtime/vm_builtin_test.go | 20 ++++++++++++-------- 2 files changed, 17 insertions(+), 32 deletions(-) diff --git a/objects/builtin_convert.go b/objects/builtin_convert.go index 460a921..2dfb7e2 100644 --- a/objects/builtin_convert.go +++ b/objects/builtin_convert.go @@ -16,9 +16,7 @@ func builtinString(args ...Object) (Object, error) { } if argsLen == 2 { - if _, ok := args[1].(*String); ok { - return args[1], nil - } + return args[1], nil } return UndefinedValue, nil @@ -40,9 +38,7 @@ func builtinInt(args ...Object) (Object, error) { } if argsLen == 2 { - if _, ok := args[1].(*Int); ok { - return args[1], nil - } + return args[1] } return UndefinedValue, nil @@ -64,12 +60,7 @@ func builtinFloat(args ...Object) (Object, error) { } if argsLen == 2 { - if _, ok := args[1].(*Float); ok { - return args[1], nil - } else if _, ok := args[1].(*Int); ok { - v, _ := ToFloat64(args[1]) - return &Float{Value: v}, nil - } + return args[1] } return UndefinedValue, nil @@ -108,9 +99,7 @@ func builtinChar(args ...Object) (Object, error) { } if argsLen == 2 { - if _, ok := args[1].(*Char); ok { - return args[1], nil - } + return args[1] } return UndefinedValue, nil @@ -133,15 +122,7 @@ func builtinBytes(args ...Object) (Object, error) { } if argsLen == 2 { - // bytes(N) => create a new bytes with given size N - if n, ok := args[1].(*Int); ok { - return &Bytes{Value: make([]byte, int(n.Value))}, nil - } - - v, ok = ToByteSlice(args[1]) - if ok { - return &Bytes{Value: v}, nil - } + return args[1] } return UndefinedValue, nil diff --git a/runtime/vm_builtin_test.go b/runtime/vm_builtin_test.go index 705834d..587269e 100644 --- a/runtime/vm_builtin_test.go +++ b/runtime/vm_builtin_test.go @@ -29,7 +29,8 @@ func TestBuiltinFunction(t *testing.T) { expect(t, `out = int(undefined)`, undefined()) expect(t, `out = int("-522", 1)`, -522) expect(t, `out = int(undefined, 1)`, 1) - expect(t, `out = int(undefined, 1.8)`, undefined()) + expect(t, `out = int(undefined, 1.8)`, 1.8) + expect(t, `out = int(undefined, string(1))`, "1") expect(t, `out = int(undefined, undefined)`, undefined()) expect(t, `out = string(1)`, "1") @@ -54,9 +55,10 @@ func TestBuiltinFunction(t *testing.T) { expect(t, `out = float({a: 1, b: "foo"})`, undefined()) expect(t, `out = float(undefined)`, undefined()) expect(t, `out = float("-52.2", 1.8)`, -52.2) - expect(t, `out = float(undefined, 1)`, 1.0) + expect(t, `out = float(undefined, 1)`, 1) expect(t, `out = float(undefined, 1.8)`, 1.8) - expect(t, `out = float(undefined, "-52.2")`, undefined()) + expect(t, `out = float(undefined, "-52.2")`, "-52.2") + expect(t, `out = float(undefined, char(56))`, '8') expect(t, `out = float(undefined, undefined)`, undefined()) expect(t, `out = char(56)`, '8') @@ -70,8 +72,9 @@ func TestBuiltinFunction(t *testing.T) { expect(t, `out = char(undefined)`, undefined()) expect(t, `out = char(56, 'a')`, '8') expect(t, `out = char(undefined, '8')`, '8') - expect(t, `out = char(undefined, 56)`, undefined()) - expect(t, `out = char(undefined, "-52.2")`, undefined()) + expect(t, `out = char(undefined, 56)`, 56) + expect(t, `out = char(undefined, "-52.2")`, "-52.2") + expect(t, `out = char(undefined, undefined)`, undefined()) expect(t, `out = bool(1)`, true) // non-zero integer: true expect(t, `out = bool(0)`, false) // zero: true @@ -99,9 +102,10 @@ func TestBuiltinFunction(t *testing.T) { expect(t, `out = bytes({a: 1})`, undefined()) expect(t, `out = bytes(undefined)`, undefined()) expect(t, `out = bytes("-522", ['8'])`, []byte{'-', '5', '2', '2'}) - expect(t, `out = bytes(undefined, "-522")`, []byte{'-', '5', '2', '2'}) - expect(t, `out = bytes(undefined, 1)`, []byte{0}) - expect(t, `out = bytes(undefined, 1.8)`, undefined()) + expect(t, `out = bytes(undefined, "-522")`, "-522") + expect(t, `out = bytes(undefined, 1)`, 1) + expect(t, `out = bytes(undefined, 1.8)`, 1.8) + expect(t, `out = bytes(undefined, int("-522"))`, -522) expect(t, `out = bytes(undefined, undefined)`, undefined()) expect(t, `out = is_error(error(1))`, true)