feat: support Object.values() (#518)

Adds support for `Object.values()`
This commit is contained in:
rory malcolm
2024-04-13 18:15:49 +01:00
committed by GitHub
parent 98effe01d8
commit 1ca7723e8a
4 changed files with 85 additions and 0 deletions

View File

@@ -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 {

View File

@@ -902,6 +902,43 @@ func (rt *runtime) newContext() {
},
},
},
"values": {
mode: 0o101,
value: Value{
kind: valueObject,
value: &object{
runtime: rt,
class: classFunctionName,
objectClass: classObject,
prototype: rt.global.FunctionPrototype,
extensible: true,
property: map[string]property{
propertyLength: {
mode: 0,
value: Value{
kind: valueNumber,
value: 1,
},
},
propertyName: {
mode: 0,
value: Value{
kind: valueString,
value: "values",
},
},
},
propertyOrder: []string{
propertyLength,
propertyName,
},
value: nativeFunctionObject{
name: "values",
call: builtinObjectValues,
},
},
},
},
"getOwnPropertyNames": {
mode: 0o101,
value: Value{
@@ -955,6 +992,7 @@ func (rt *runtime) newContext() {
"isFrozen",
"freeze",
"keys",
"values",
"getOwnPropertyNames",
},
}

View File

@@ -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()

View File

@@ -32,6 +32,8 @@ types:
function: 1
- name: keys
function: 1
- name: values
function: 1
- name: getOwnPropertyNames
function: 1
prototype: