Browse Source

Squashed commit of the following:

commit 6c55d263e132defe7647f9110e6aa1d33b820eae
Author: Mansoor <[email protected]>
Date:   Mon Oct 24 18:47:35 2022 -0400

    inlined fromEntries add kv operation. updated iterator null function error message to match nodejs

commit a569a98e5d732d4ddb2532dac2d2658651d1b9a8
Author: Mansoor <[email protected]>
Date:   Sun Oct 23 03:33:39 2022 -0400

    Added hasOwn and fromEntries methods to Object
Dmitry Panov 3 years ago
parent
commit
936bcca637
3 changed files with 41 additions and 3 deletions
  1. 36 0
      builtin_object.go
  2. 5 1
      runtime.go
  3. 0 2
      tc39_test.go

+ 36 - 0
builtin_object.go

@@ -556,6 +556,40 @@ func (r *Runtime) object_setPrototypeOf(call FunctionCall) Value {
 	return o
 }
 
+func (r *Runtime) object_fromEntries(call FunctionCall) Value {
+	o := call.Argument(0)
+	r.checkObjectCoercible(o)
+
+	result := r.newBaseObject(r.global.ObjectPrototype, classObject).val
+
+	iter := r.getIterator(o, nil)
+	iter.iterate(func(nextValue Value) {
+		i0 := valueInt(0)
+		i1 := valueInt(1)
+
+		itemObj := r.toObject(nextValue)
+		k := itemObj.self.getIdx(i0, nil)
+		v := itemObj.self.getIdx(i1, nil)
+		key := toPropertyKey(k)
+
+		createDataPropertyOrThrow(result, key, v)
+	})
+
+	return result
+}
+
+func (r *Runtime) object_hasOwn(call FunctionCall) Value {
+	o := call.Argument(0)
+	obj := o.ToObject(r)
+	p := toPropertyKey(call.Argument(1))
+
+	if obj.hasOwnProperty(p) {
+		return valueTrue
+	} else {
+		return valueFalse
+	}
+}
+
 func (r *Runtime) initObject() {
 	o := r.global.ObjectPrototype.self
 	o._putProp("toString", r.newNativeFunc(r.objectproto_toString, nil, "toString", nil, 0), true, false, true)
@@ -593,6 +627,8 @@ func (r *Runtime) initObject() {
 	o._putProp("keys", r.newNativeFunc(r.object_keys, nil, "keys", nil, 1), true, false, true)
 	o._putProp("setPrototypeOf", r.newNativeFunc(r.object_setPrototypeOf, nil, "setPrototypeOf", nil, 2), true, false, true)
 	o._putProp("values", r.newNativeFunc(r.object_values, nil, "values", nil, 1), true, false, true)
+	o._putProp("fromEntries", r.newNativeFunc(r.object_fromEntries, nil, "fromEntries", nil, 1), true, false, true)
+	o._putProp("hasOwn", r.newNativeFunc(r.object_hasOwn, nil, "hasOwn", nil, 2), true, false, true)
 
 	r.addToGlobal("Object", r.global.Object)
 }

+ 5 - 1
runtime.go

@@ -2635,7 +2635,11 @@ func (r *Runtime) getIterator(obj Value, method func(FunctionCall) Value) *itera
 func (ir *iteratorRecord) iterate(step func(Value)) {
 	r := ir.iterator.runtime
 	for {
-		res := r.toObject(ir.next(FunctionCall{This: ir.iterator}))
+		if ir.next == nil {
+			panic(r.NewTypeError("object null is not a function"))
+		}
+		nextVal := ir.next(FunctionCall{This: ir.iterator})
+		res := r.toObject(nextVal)
 		if nilSafe(res.self.getStr("done", nil)).ToBoolean() {
 			break
 		}

+ 0 - 2
tc39_test.go

@@ -281,8 +281,6 @@ var (
 		"FinalizationRegistry",
 		"WeakRef",
 		"numeric-separator-literal",
-		"Object.fromEntries",
-		"Object.hasOwn",
 		"__getter__",
 		"__setter__",
 		"ShadowRealm",