Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Object.values() #518

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions builtin_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,17 @@ func builtinObjectKeys(call FunctionCall) Value {
panic(call.runtime.panicTypeError("Object.Keys is nil"))
}

func builtinObjectValues(call FunctionCall) Value {
if obj, values := call.Argument(0).object(), []Value(nil); nil != obj {
obj.enumerate(false, func(name string) bool {
values = append(values, obj.get(name))
return true
})
return objectValue(call.runtime.newArrayOf(values))
}
panic(call.runtime.panicTypeError("Object.Values is nil"))
}

func builtinObjectGetOwnPropertyNames(call FunctionCall) Value {
if obj, propertyNames := call.Argument(0).object(), []Value(nil); nil != obj {
obj.enumerate(true, func(name string) bool {
Expand Down
38 changes: 38 additions & 0 deletions inline.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 34 additions & 0 deletions object_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,40 @@ func TestObject_keys(t *testing.T) {
})
}

func TestObject_values(t *testing.T) {
tt(t, func() {
test, _ := test()

test(`Object.values({ abc:"first_example", def:"second_example" })`, "first_example,second_example")

test(`
function abc() {
this.abc = "first_example";
this.def = "second_example";
}
Object.values(new abc())
`, "first_example,second_example")

test(`
function def() {
this.ghi = "third_example"
}
def.prototype = new abc();
Object.values(new def());
`, "third_example")

test(`
var arr = [1, 2, 3];
Object.values(arr);
`, "1,2,3")

test(`
var arr = [{"abc": "first_example"}, {"def": "second_example"}];
Object.values(arr);
`, "[object Object],[object Object]")
})
}

func TestObject_getOwnPropertyNames(t *testing.T) {
tt(t, func() {
test, _ := test()
Expand Down
2 changes: 2 additions & 0 deletions tools/gen-jscore/.gen-jscore.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ types:
function: 1
- name: keys
function: 1
- name: values
function: 1
- name: getOwnPropertyNames
function: 1
prototype:
Expand Down