From 946c8020d205db4ca6c658a8c7965b4185bdc0c8 Mon Sep 17 00:00:00 2001 From: Rory Malcolm Date: Wed, 10 Apr 2024 22:13:47 +0100 Subject: [PATCH] Support Object.values() This adds support for `Object.values()`, alongside tests --- builtin_object.go | 11 +++++++++ inline.go | 38 +++++++++++++++++++++++++++++++ object_test.go | 34 +++++++++++++++++++++++++++ tools/gen-jscore/.gen-jscore.yaml | 2 ++ 4 files changed, 85 insertions(+) diff --git a/builtin_object.go b/builtin_object.go index 029a75f7..82da2cf9 100644 --- a/builtin_object.go +++ b/builtin_object.go @@ -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 { diff --git a/inline.go b/inline.go index 51f7f446..30e00bee 100644 --- a/inline.go +++ b/inline.go @@ -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", }, } diff --git a/object_test.go b/object_test.go index 3af01888..7ff3474e 100644 --- a/object_test.go +++ b/object_test.go @@ -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() diff --git a/tools/gen-jscore/.gen-jscore.yaml b/tools/gen-jscore/.gen-jscore.yaml index 524df5e1..5bca766f 100644 --- a/tools/gen-jscore/.gen-jscore.yaml +++ b/tools/gen-jscore/.gen-jscore.yaml @@ -32,6 +32,8 @@ types: function: 1 - name: keys function: 1 + - name: values + function: 1 - name: getOwnPropertyNames function: 1 prototype: