From: Lady <redacted>
Date: Thu, 8 Sep 2022 05:20:46 +0000 (-0700)
Subject: Provide fake external constructor for Matcher
X-Git-Tag: 0.2.0~2
X-Git-Url: https://git.ladys.computer/Pisces/commitdiff_plain/cbc7b43d863a143313425e0df475d6f4201ad690

Provide fake external constructor for Matcher

Classes which depend on identity for behaviours should not be exposed
because this inheritance can be modified at runtime. This commit
provides a fake Matcher constructor which simply calls the real one.

This has a side‐effect of reducing the amount of prototype modification
which this class setup entailed, at the cost of needing to manually
redefine all the properties from the internal Matcher constructor
prototype onto the external one.
---

diff --git a/string.js b/string.js
index 28412fc..3dc39ac 100644
--- a/string.js
+++ b/string.js
@@ -10,6 +10,7 @@
 import { bind, call, identity, makeCallable } from "./function.js";
 import {
   defineOwnProperties,
+  getOwnPropertyDescriptors,
   getPrototype,
   objectCreate,
   setPrototype,
@@ -213,12 +214,25 @@ export const {
       return call(getUnicode, this.#regExp, []);
     }
   };
-  const matcherPrototype = setPrototype(
-    Matcher.prototype,
-    rePrototype,
+
+  const matcherConstructor = defineOwnProperties(
+    class extends RegExp {
+      constructor(...args) {
+        return new Matcher(...args);
+      }
+    },
+    {
+      name: { value: "Matcher" },
+      length: { value: 1 },
+    },
+  );
+  const matcherPrototype = defineOwnProperties(
+    matcherConstructor.prototype,
+    getOwnPropertyDescriptors(Matcher.prototype),
+    { constructor: { value: matcherConstructor } },
   );
 
-  return { Matcher };
+  return { Matcher: matcherConstructor };
 })();
 
 export const {