unreflect 0.0.3 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.mjs +52 -9
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -21,7 +21,29 @@ var ReflectionClass = class {
|
|
|
21
21
|
let braceDepth = 0;
|
|
22
22
|
let bracketDepth = 0;
|
|
23
23
|
let parenDepth = 0;
|
|
24
|
+
let inConstructor = false;
|
|
25
|
+
let constructorBraceDepth = 0;
|
|
24
26
|
for (const line of lines) {
|
|
27
|
+
if (/^\s*constructor\s*\(/.test(line)) {
|
|
28
|
+
inConstructor = true;
|
|
29
|
+
constructorBraceDepth = 0;
|
|
30
|
+
for (const ch of line) {
|
|
31
|
+
if (ch === "{") constructorBraceDepth++;
|
|
32
|
+
if (ch === "}") constructorBraceDepth--;
|
|
33
|
+
}
|
|
34
|
+
result.push(line.replace(/\{.*$/, "{}"));
|
|
35
|
+
if (constructorBraceDepth > 0) continue;
|
|
36
|
+
inConstructor = false;
|
|
37
|
+
continue;
|
|
38
|
+
}
|
|
39
|
+
if (inConstructor) {
|
|
40
|
+
for (const ch of line) {
|
|
41
|
+
if (ch === "{") constructorBraceDepth++;
|
|
42
|
+
if (ch === "}") constructorBraceDepth--;
|
|
43
|
+
}
|
|
44
|
+
if (constructorBraceDepth === 0) inConstructor = false;
|
|
45
|
+
continue;
|
|
46
|
+
}
|
|
25
47
|
const isMethod = /^\s*#\w+\s*\(/.test(line);
|
|
26
48
|
const fieldMatch = line.match(/^(\s*)(#\w+)/);
|
|
27
49
|
if (!isMethod && fieldMatch?.[1] !== void 0 && fieldMatch[2] && !skipUntilBalanced) {
|
|
@@ -90,7 +112,7 @@ var ReflectionClass = class {
|
|
|
90
112
|
const proto = Object.getPrototypeOf(obj);
|
|
91
113
|
const classSource = proto.constructor.toString();
|
|
92
114
|
if (!classSource.includes(`#${baseName}`)) return;
|
|
93
|
-
const modifiedSource = this.stripPrivateInitializers(classSource).replace(/^(class
|
|
115
|
+
const modifiedSource = this.stripPrivateInitializers(classSource).replace(/^(class(?:\s+\w+)?)\s*\{/, `$1 { static __reflectionSet__(obj, val) { obj.#${baseName} = val; } static __reflectionGet__(obj) { return obj.#${baseName}; }`);
|
|
94
116
|
const ModifiedClass = vm.runInThisContext(`(${modifiedSource})`);
|
|
95
117
|
Object.setPrototypeOf(obj, ModifiedClass.prototype);
|
|
96
118
|
const originalProps = Object.getOwnPropertyDescriptors(obj);
|
|
@@ -102,14 +124,35 @@ var ReflectionClass = class {
|
|
|
102
124
|
ModifiedClass
|
|
103
125
|
});
|
|
104
126
|
Object.setPrototypeOf(obj, proto);
|
|
105
|
-
const
|
|
106
|
-
for (const
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
value
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
127
|
+
const protoDescriptors = Object.getOwnPropertyDescriptors(proto);
|
|
128
|
+
for (const [propName, descriptor] of Object.entries(protoDescriptors)) {
|
|
129
|
+
if (propName === "constructor") continue;
|
|
130
|
+
if (typeof descriptor.value === "function") {
|
|
131
|
+
if (descriptor.value.toString().includes(`#${baseName}`)) {
|
|
132
|
+
const boundMethod = ModifiedClass.prototype[propName].bind(freshInstance);
|
|
133
|
+
Object.defineProperty(obj, propName, {
|
|
134
|
+
value: boundMethod,
|
|
135
|
+
writable: true,
|
|
136
|
+
configurable: true
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
if (descriptor.get || descriptor.set) {
|
|
141
|
+
const getterSource = descriptor.get?.toString() || "";
|
|
142
|
+
const setterSource = descriptor.set?.toString() || "";
|
|
143
|
+
if (getterSource.includes(`#${baseName}`) || setterSource.includes(`#${baseName}`)) {
|
|
144
|
+
const modifiedDescriptor = Object.getOwnPropertyDescriptor(ModifiedClass.prototype, propName);
|
|
145
|
+
if (modifiedDescriptor) {
|
|
146
|
+
const newDescriptor = {
|
|
147
|
+
configurable: true,
|
|
148
|
+
enumerable: descriptor.enumerable
|
|
149
|
+
};
|
|
150
|
+
if (modifiedDescriptor.get) newDescriptor.get = modifiedDescriptor.get.bind(freshInstance);
|
|
151
|
+
if (modifiedDescriptor.set) newDescriptor.set = modifiedDescriptor.set.bind(freshInstance);
|
|
152
|
+
Object.defineProperty(obj, propName, newDescriptor);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
}
|
|
113
156
|
}
|
|
114
157
|
}
|
|
115
158
|
async getProperty(name) {
|