Skip to content

Commit

Permalink
Added Object.Keys() method (closes dop251#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
dop251 committed Mar 13, 2017
1 parent 8fa3e12 commit 38df25a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
26 changes: 26 additions & 0 deletions runtime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package goja

import (
"errors"
"reflect"
"testing"
"time"
)
Expand Down Expand Up @@ -759,6 +760,31 @@ func TestNilCallArg(t *testing.T) {
}
}

func TestObjectKeys(t *testing.T) {
const SCRIPT = `
var o = { a: 1, b: 2, c: 3, d: 4 };
o;
`

vm := New()
prg, err := Compile("test.js", SCRIPT, false)
if err != nil {
t.Fatal(err)
}

res, err := vm.RunProgram(prg)
if err != nil {
t.Fatal(err)
}

if o, ok := res.(*Object); ok {
keys := o.Keys()
if !reflect.DeepEqual(keys, []string{"a", "b", "c", "d"}) {
t.Fatalf("Unexpected keys: %v", keys)
}
}
}

/*
func TestArrayConcatSparse(t *testing.T) {
function foo(a,b,c)
Expand Down
8 changes: 8 additions & 0 deletions value.go
Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,14 @@ func (o *Object) Get(name string) Value {
return o.self.getStr(name)
}

func (o *Object) Keys() (keys []string) {
for item, f := o.self.enumerate(false, false)(); f != nil; item, f = f() {
keys = append(keys, item.name)
}

return
}

func (o *Object) Set(name string, value interface{}) (err error) {
defer func() {
if x := recover(); x != nil {
Expand Down

0 comments on commit 38df25a

Please sign in to comment.