]> Lady’s Gitweb - Pisces/commitdiff
Make getOwnPropertyDescriptor(s) safer
authorLady <redacted>
Fri, 24 Nov 2023 17:03:52 +0000 (12:03 -0500)
committerLady <redacted>
Fri, 24 Nov 2023 17:35:21 +0000 (12:35 -0500)
Return proxied property descriptor records from these functions (which
inherit from null) to prevent weirdness should somebody try to modify
`Object.prototype`.

object.js

index aa3538d28cf821d91c72f16244db4668f6f3fda7..9132e4b5d2cf2eb44677151adb828cb789ee44d1 100644 (file)
--- a/object.js
+++ b/object.js
@@ -15,7 +15,9 @@ import {
   toFunctionName,
   toLength,
   toPrimitive,
+  toPropertyDescriptor,
   type,
+  UNDEFINED,
 } from "./value.js";
 
 /**
@@ -143,6 +145,27 @@ export const {
    */
   frozenCopy,
 
+  /**
+   * Returns the property descriptor record for the own property with
+   * the provided property key on the provided object, or null if none
+   * exists.
+   *
+   * ※ This is effectively an alias for
+   * `Object.getOwnPropertyDescriptor`, but the return value is a
+   * proxied object with null prototype.
+   */
+  getOwnPropertyDescriptor,
+
+  /**
+   * Returns the property descriptors for the own properties on the
+   * provided object.
+   *
+   * ※ This is effectively an alias for
+   * `Object.getOwnPropertyDescriptors`, but the values on the
+   * resulting object are proxied objects with null prototypes.
+   */
+  getOwnPropertyDescriptors,
+
   /**
    * Returns whether the provided object is frozen.
    *
@@ -182,6 +205,7 @@ export const {
   const {
     create,
     defineProperties,
+    getOwnPropertyDescriptor: objectGetOwnPropertyDescriptor,
     getPrototypeOf,
     isFrozen,
     isSealed,
@@ -266,6 +290,27 @@ export const {
         );
       }
     },
+    getOwnPropertyDescriptor: (O, P) => {
+      const desc = objectGetOwnPropertyDescriptor(O, P);
+      return desc === UNDEFINED
+        ? UNDEFINED
+        : toPropertyDescriptor(desc);
+    },
+    getOwnPropertyDescriptors: (O) => {
+      const obj = toObject(O);
+      const ownKeys = getOwnPropertyKeys(obj);
+      const descriptors = {};
+      for (let k = 0; k < ownKeys.length; ++k) {
+        const key = ownKeys[k];
+        defineOwnProperty(descriptors, key, {
+          configurable: true,
+          enumerable: true,
+          value: getOwnPropertyDescriptor(O, key),
+          writable: true,
+        });
+      }
+      return descriptors;
+    },
     isUnfrozenObject: (O) => !isFrozen(O),
     isUnsealedObject: (O) => !isSealed(O),
     setPrototype: (O, proto) => {
@@ -413,29 +458,6 @@ export const getMethod = (V, P) => {
   }
 };
 
-/**
- * Returns the property descriptor for the own property with the
- * provided property key on the provided object, or null if none
- * exists.
- *
- * ※ This is effectively an alias for
- * `Object.getOwnPropertyDescriptor`.
- */
-export const getOwnPropertyDescriptor = createArrowFunction(
-  Object.getOwnPropertyDescriptor,
-);
-
-/**
- * Returns the property descriptors for the own properties on the
- * provided object.
- *
- * ※ This is effectively an alias for
- * `Object.getOwnPropertyDescriptors`.
- */
-export const getOwnPropertyDescriptors = createArrowFunction(
-  Object.getOwnPropertyDescriptors,
-);
-
 /**
  * Returns an array of string‐valued own property keys on the
  * provided object.
This page took 0.024932 seconds and 4 git commands to generate.