From c57a7f8f98f062eec4791d9166833181b5f27c54 Mon Sep 17 00:00:00 2001 From: Daniel Kang Date: Fri, 1 Feb 2019 17:32:05 -0800 Subject: [PATCH] Fix a bug where len() builtin does not take immutable array --- objects/builtin_len.go | 2 ++ runtime/vm_builtin_test.go | 8 ++++++++ 2 files changed, 10 insertions(+) diff --git a/objects/builtin_len.go b/objects/builtin_len.go index fd6529a..f749363 100644 --- a/objects/builtin_len.go +++ b/objects/builtin_len.go @@ -12,6 +12,8 @@ func builtinLen(args ...Object) (Object, error) { switch arg := args[0].(type) { case *Array: return &Int{Value: int64(len(arg.Value))}, nil + case *ImmutableArray: + return &Int{Value: int64(len(arg.Value))}, nil case *String: return &Int{Value: int64(len(arg.Value))}, nil case *Bytes: diff --git a/runtime/vm_builtin_test.go b/runtime/vm_builtin_test.go index aebbe78..0eef432 100644 --- a/runtime/vm_builtin_test.go +++ b/runtime/vm_builtin_test.go @@ -10,6 +10,14 @@ func TestBuiltinFunction(t *testing.T) { expect(t, `out = len("")`, 0) expect(t, `out = len("four")`, 4) expect(t, `out = len("hello world")`, 11) + expect(t, `out = len([])`, 0) + expect(t, `out = len([1, 2, 3])`, 3) + expect(t, `out = len({})`, 0) + expect(t, `out = len({a:1, b:2})`, 2) + expect(t, `out = len(immutable([]))`, 0) + expect(t, `out = len(immutable([1, 2, 3]))`, 3) + expect(t, `out = len(immutable({}))`, 0) + expect(t, `out = len(immutable({a:1, b:2}))`, 2) expectError(t, `len(1)`) expectError(t, `len("one", "two")`)