mnemonica 1.2.0 → 1.2.3
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/.ai/ONBOARDING.md +1 -1
- package/.ai/PROTOTYPE-CHAIN.md +15 -3
- package/.ai/rules-async-constructors.md +81 -1
- package/.ai/rules-define-patterns.md +39 -0
- package/.ai/rules-error-system.md +10 -0
- package/.ai/rules-instance-methods.md +1 -1
- package/AGENTS.md +14 -1
- package/CONTRIBUTING.md +4 -18
- package/FOR_HUMANS.md +128 -23
- package/README.md +44 -9
- package/SKILL.md +7 -2
- package/build/api/errors/exceptionConstructor.js +20 -47
- package/build/api/errors/index.js +7 -5
- package/build/api/errors/throwModificationError.js +26 -61
- package/build/api/index.d.ts +1 -0
- package/build/api/index.js +2 -1
- package/build/api/types/InstanceCreator.js +20 -5
- package/build/api/types/Mnemosyne.d.ts +3 -14
- package/build/api/types/Mnemosyne.js +21 -8
- package/build/api/types/Props.js +24 -11
- package/build/api/types/TypeProxy.d.ts +1 -1
- package/build/api/types/TypeProxy.js +9 -9
- package/build/api/types/compileNewModificatorFunctionBody.js +3 -5
- package/build/api/types/index.d.ts +5 -0
- package/build/api/types/index.js +70 -31
- package/build/api/utils/index.d.ts +1 -0
- package/build/api/utils/index.js +13 -3
- package/build/descriptors/types/index.js +46 -2
- package/build/index.d.ts +6 -2
- package/build/index.js +18 -3
- package/build/types/index.d.ts +31 -8
- package/build/types/index.js +1 -1
- package/build/utils/parent.js +35 -5
- package/docs/async-constructors.md +3 -1
- package/docs/empathy-in-ai.md +2 -3
- package/docs/prototype-chain.md +4 -0
- package/docs/typed-lookup.md +19 -2
- package/package.json +8 -8
- package/src/api/errors/exceptionConstructor.ts +40 -83
- package/src/api/errors/index.ts +10 -4
- package/src/api/errors/throwModificationError.ts +64 -104
- package/src/api/index.ts +2 -8
- package/src/api/types/InstanceCreator.ts +42 -6
- package/src/api/types/Mnemosyne.ts +34 -15
- package/src/api/types/Props.ts +57 -22
- package/src/api/types/TypeProxy.ts +10 -9
- package/src/api/types/compileNewModificatorFunctionBody.ts +12 -4
- package/src/api/types/index.ts +122 -65
- package/src/api/utils/index.ts +27 -9
- package/src/descriptors/types/index.ts +81 -3
- package/src/index.ts +66 -0
- package/src/types/index.ts +120 -15
- package/src/utils/parent.ts +48 -4
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
import type {
|
|
4
|
-
MnemonicaError, InstanceCreatorContext
|
|
4
|
+
MnemonicaError, ErrorProps, InstanceCreatorContext
|
|
5
5
|
} from '../../types';
|
|
6
6
|
|
|
7
7
|
import { constants } from '../../constants';
|
|
@@ -21,9 +21,9 @@ import {
|
|
|
21
21
|
import TypesUtils from '../utils';
|
|
22
22
|
const { makeErrorModificatorType } = TypesUtils;
|
|
23
23
|
|
|
24
|
-
import {
|
|
25
|
-
|
|
26
|
-
|
|
24
|
+
import {
|
|
25
|
+
getProps, setProps
|
|
26
|
+
} from '../types/Props';
|
|
27
27
|
|
|
28
28
|
import { makeInstanceModificator } from '../types/InstanceModificator';
|
|
29
29
|
|
|
@@ -45,51 +45,34 @@ export const throwModificationError = function ( this: InstanceCreatorContext, e
|
|
|
45
45
|
// debugger;
|
|
46
46
|
// }
|
|
47
47
|
|
|
48
|
-
|
|
48
|
+
// error data (exceptionReason, reasons, surplus, args, originalError,
|
|
49
|
+
// instance) is never defined on error objects — it lives in the external
|
|
50
|
+
// props storage (WeakMap), read it via getProps(error).
|
|
51
|
+
// NOTE: the public getProps is required here (not _getProps) because the
|
|
52
|
+
// error data sits in the additions slot of the record — _getProps returns
|
|
53
|
+
// the raw record only and would miss it.
|
|
54
|
+
const errorProps = getProps( error ) as unknown as ErrorProps | undefined;
|
|
55
|
+
|
|
56
|
+
const exceptionReason = (
|
|
57
|
+
errorProps !== undefined &&
|
|
58
|
+
errorProps.exceptionReason !== undefined
|
|
59
|
+
) ?
|
|
60
|
+
errorProps.exceptionReason :
|
|
61
|
+
error;
|
|
49
62
|
|
|
50
|
-
if (
|
|
63
|
+
if ( errorProps !== undefined && errorProps.exceptionReason !== undefined ) {
|
|
51
64
|
|
|
52
|
-
(
|
|
53
|
-
(
|
|
65
|
+
(errorProps.reasons as unknown[]).push( errorProps.exceptionReason );
|
|
66
|
+
(errorProps.surplus as unknown[]).push( error );
|
|
54
67
|
|
|
55
68
|
throw error;
|
|
56
69
|
|
|
57
70
|
}
|
|
58
71
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
get () {
|
|
64
|
-
return exceptionReason;
|
|
65
|
-
},
|
|
66
|
-
enumerable : true
|
|
67
|
-
}
|
|
68
|
-
);
|
|
69
|
-
|
|
70
|
-
const reasons: Error[] = [ exceptionReason ];
|
|
71
|
-
|
|
72
|
-
odp(
|
|
73
|
-
error,
|
|
74
|
-
'reasons',
|
|
75
|
-
{
|
|
76
|
-
get () {
|
|
77
|
-
return reasons;
|
|
78
|
-
},
|
|
79
|
-
enumerable : true
|
|
80
|
-
}
|
|
81
|
-
);
|
|
82
|
-
const surplus: Error[] = [];
|
|
83
|
-
odp(
|
|
84
|
-
error,
|
|
85
|
-
'surplus',
|
|
86
|
-
{
|
|
87
|
-
get () {
|
|
88
|
-
return surplus;
|
|
89
|
-
},
|
|
90
|
-
enumerable : true
|
|
91
|
-
}
|
|
92
|
-
);
|
|
72
|
+
// reasons/surplus may hold non-Error reason objects (bound method
|
|
73
|
+
// failures push { methodName, ... } shaped reasons), hence unknown[]
|
|
74
|
+
const reasons: unknown[] = [ exceptionReason ];
|
|
75
|
+
const surplus: unknown[] = [];
|
|
93
76
|
|
|
94
77
|
self.ModificatorType = makeErrorModificatorType( TypeName );
|
|
95
78
|
|
|
@@ -98,6 +81,17 @@ export const throwModificationError = function ( this: InstanceCreatorContext, e
|
|
|
98
81
|
// let erroredInstance = new self.InstanceModificator();
|
|
99
82
|
const erroredInstance = new self.InstanceModificator();
|
|
100
83
|
|
|
84
|
+
// the first portion of error data: stored against the failed
|
|
85
|
+
// construction layer props, so getProps(erroredInstance) exposes it
|
|
86
|
+
setProps(
|
|
87
|
+
erroredInstance,
|
|
88
|
+
{
|
|
89
|
+
exceptionReason,
|
|
90
|
+
reasons,
|
|
91
|
+
surplus
|
|
92
|
+
}
|
|
93
|
+
);
|
|
94
|
+
|
|
101
95
|
let errorProto: object | null = Reflect.getPrototypeOf( erroredInstance );
|
|
102
96
|
let isMnemonicaInstance = false;
|
|
103
97
|
while ( errorProto ) {
|
|
@@ -145,14 +139,26 @@ export const throwModificationError = function ( this: InstanceCreatorContext, e
|
|
|
145
139
|
|
|
146
140
|
const title = `\n<-- creation of [ ${TypeName} ] traced -->`;
|
|
147
141
|
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
142
|
+
if ( self.inheritedInstance instanceof Promise ) {
|
|
143
|
+
|
|
144
|
+
// async construction failure: this runs from makeAwaiter's .catch,
|
|
145
|
+
// long after the `new` call site has unwound — a fresh capture here
|
|
146
|
+
// would hold only rejection-processing frames. runAsyncHandling
|
|
147
|
+
// captured the creation stack at new-time instead; use it.
|
|
148
|
+
stack.push( ...(self.stack as string[]) );
|
|
149
|
+
|
|
150
|
+
} else {
|
|
151
|
+
|
|
152
|
+
getStack.call(
|
|
153
|
+
erroredInstance,
|
|
154
|
+
title,
|
|
155
|
+
[],
|
|
156
|
+
throwModificationError
|
|
157
|
+
);
|
|
154
158
|
|
|
155
|
-
|
|
159
|
+
stack.push( ...(erroredInstance as { stack: string[] }).stack );
|
|
160
|
+
|
|
161
|
+
}
|
|
156
162
|
|
|
157
163
|
const errorStack = (error.stack as string ).split( '\n' );
|
|
158
164
|
|
|
@@ -206,66 +212,20 @@ export const throwModificationError = function ( this: InstanceCreatorContext, e
|
|
|
206
212
|
|
|
207
213
|
// if hooks had some interception: stop
|
|
208
214
|
|
|
209
|
-
|
|
215
|
+
// the rest of error data, available only when the error is thrown
|
|
216
|
+
setProps(
|
|
210
217
|
erroredInstance,
|
|
211
|
-
'args',
|
|
212
218
|
{
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
219
|
+
args,
|
|
220
|
+
originalError : error,
|
|
221
|
+
instance : erroredInstance
|
|
216
222
|
}
|
|
217
223
|
);
|
|
218
224
|
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
get () {
|
|
224
|
-
return error;
|
|
225
|
-
}
|
|
226
|
-
}
|
|
227
|
-
);
|
|
228
|
-
|
|
229
|
-
odp(
|
|
230
|
-
erroredInstance,
|
|
231
|
-
'instance',
|
|
232
|
-
{
|
|
233
|
-
get () {
|
|
234
|
-
return erroredInstance;
|
|
235
|
-
}
|
|
236
|
-
}
|
|
237
|
-
);
|
|
238
|
-
|
|
239
|
-
odp(
|
|
240
|
-
erroredInstance,
|
|
241
|
-
'extract',
|
|
242
|
-
{
|
|
243
|
-
get () {
|
|
244
|
-
const extractGetter = () => {
|
|
245
|
-
// mnemonica instances always have a parent object,
|
|
246
|
-
// so the runtime value is guaranteed to be object here
|
|
247
|
-
const _parent = parent(erroredInstance) as object;
|
|
248
|
-
const extractResult = extract(_parent);
|
|
249
|
-
return extractResult;
|
|
250
|
-
};
|
|
251
|
-
return extractGetter;
|
|
252
|
-
}
|
|
253
|
-
}
|
|
254
|
-
);
|
|
255
|
-
|
|
256
|
-
odp(
|
|
257
|
-
erroredInstance,
|
|
258
|
-
'parse',
|
|
259
|
-
{
|
|
260
|
-
get () {
|
|
261
|
-
const parseGetter = () => {
|
|
262
|
-
const parseResult = parse( erroredInstance );
|
|
263
|
-
return parseResult;
|
|
264
|
-
};
|
|
265
|
-
return parseGetter;
|
|
266
|
-
}
|
|
267
|
-
}
|
|
268
|
-
);
|
|
225
|
+
// no bound .extract()/.parse() here: since v1.0.6 instances expose no
|
|
226
|
+
// methods, so errors follow — call utils.extract(...) / utils.parse(...)
|
|
227
|
+
// on the thrown error (or on utils.parent(error) for the pre-failure
|
|
228
|
+
// layer, which the old bound .extract() used to return)
|
|
269
229
|
}
|
|
270
230
|
|
|
271
231
|
throw erroredInstance;
|
package/src/api/index.ts
CHANGED
|
@@ -1,13 +1,5 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
// TODO :
|
|
4
|
-
|
|
5
|
-
// events
|
|
6
|
-
// handlers
|
|
7
|
-
// helpers
|
|
8
|
-
// loaders
|
|
9
|
-
// transforms
|
|
10
|
-
|
|
11
3
|
import {
|
|
12
4
|
invokeHook,
|
|
13
5
|
registerHook,
|
|
@@ -15,6 +7,7 @@ import {
|
|
|
15
7
|
} from './hooks';
|
|
16
8
|
import {
|
|
17
9
|
define,
|
|
10
|
+
lazy,
|
|
18
11
|
lookup,
|
|
19
12
|
} from './types';
|
|
20
13
|
|
|
@@ -28,5 +21,6 @@ export const hooks = {
|
|
|
28
21
|
|
|
29
22
|
export const types = {
|
|
30
23
|
define,
|
|
24
|
+
lazy,
|
|
31
25
|
lookup,
|
|
32
26
|
};
|
|
@@ -59,6 +59,12 @@ const invokePreHooks = function ( this: InstanceCreatorContext ) {
|
|
|
59
59
|
|
|
60
60
|
const { collection, } = type;
|
|
61
61
|
|
|
62
|
+
// fast path: nothing registered for preCreation on either side,
|
|
63
|
+
// so there is no hookData to build and no invokeHook to run
|
|
64
|
+
if (!type.hooks.preCreation && !collection.hooks.preCreation) {
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
|
|
62
68
|
const hookData = {
|
|
63
69
|
type,
|
|
64
70
|
TypeName : type.TypeName,
|
|
@@ -85,21 +91,32 @@ const invokePostHooks = function ( this: InstanceCreatorContext ) {
|
|
|
85
91
|
|
|
86
92
|
|
|
87
93
|
const creator = this;
|
|
94
|
+
const { type } = creator;
|
|
95
|
+
const { collection, } = type;
|
|
96
|
+
|
|
97
|
+
const hookType = creator.inheritedInstance instanceof Error ?
|
|
98
|
+
'creationError' : 'postCreation';
|
|
99
|
+
|
|
100
|
+
// fast path: nothing registered for this hookType on either side;
|
|
101
|
+
// creator.type is the same object props.__type__ would give,
|
|
102
|
+
// and it saves the _getProps chain walk entirely
|
|
103
|
+
if (!type.hooks[ hookType ] && !collection.hooks[ hookType ]) {
|
|
104
|
+
const emptyResult = {
|
|
105
|
+
type : new Set<unknown>(),
|
|
106
|
+
collection : new Set<unknown>(),
|
|
107
|
+
};
|
|
108
|
+
return emptyResult;
|
|
109
|
+
}
|
|
110
|
+
|
|
88
111
|
const { inheritedInstance, } = creator;
|
|
89
112
|
|
|
90
113
|
const props = _getProps(inheritedInstance) as Props;
|
|
91
114
|
|
|
92
115
|
const {
|
|
93
|
-
__type__: type,
|
|
94
116
|
__parent__: existentInstance,
|
|
95
117
|
__args__: args,
|
|
96
118
|
} = props;
|
|
97
119
|
|
|
98
|
-
const { collection, } = type;
|
|
99
|
-
|
|
100
|
-
const hookType = inheritedInstance instanceof Error ?
|
|
101
|
-
'creationError' : 'postCreation';
|
|
102
|
-
|
|
103
120
|
const hookData = {
|
|
104
121
|
type,
|
|
105
122
|
TypeName : type.TypeName,
|
|
@@ -371,6 +388,11 @@ const runBlockErrorsCheck = function ( self: InstanceCreatorContext, args: unkno
|
|
|
371
388
|
};
|
|
372
389
|
|
|
373
390
|
const runBuild = function ( self: InstanceCreatorContext, args: unknown[] ) {
|
|
391
|
+
// NOTE: makeInstanceModificator runs OUTSIDE the blockErrors guard below.
|
|
392
|
+
// That placement is pragmatic, not principled (too many other things were
|
|
393
|
+
// on fire at the time): a custom ModificationConstructor throwing during
|
|
394
|
+
// wiring propagates raw, because wrapping an error into an errored
|
|
395
|
+
// instance needs a valid instance context that does not exist yet here.
|
|
374
396
|
self.InstanceModificator = makeInstanceModificator( self );
|
|
375
397
|
|
|
376
398
|
if ( self.config.blockErrors ) {
|
|
@@ -388,6 +410,20 @@ const runBuild = function ( self: InstanceCreatorContext, args: unknown[] ) {
|
|
|
388
410
|
|
|
389
411
|
const runAsyncHandling = function ( self: InstanceCreatorContext, type: TypeDef ) {
|
|
390
412
|
if ( self.inheritedInstance instanceof Promise ) {
|
|
413
|
+
// Async construction: capture the creation stack NOW, while the `new`
|
|
414
|
+
// call site is still on the stack. When a rejection gets wrapped later
|
|
415
|
+
// (makeAwaiter .catch → throwModificationError), the call site has
|
|
416
|
+
// already unwound, and a fresh capture would hold nothing but
|
|
417
|
+
// rejection-processing frames. Skipped when runSetup already captured
|
|
418
|
+
// one (submitStack).
|
|
419
|
+
if ( !Array.isArray( self.stack ) ) {
|
|
420
|
+
const title = `\n<-- creation of [ ${type.TypeName} ] traced -->`;
|
|
421
|
+
getStack.call(
|
|
422
|
+
self,
|
|
423
|
+
title,
|
|
424
|
+
[]
|
|
425
|
+
);
|
|
426
|
+
}
|
|
391
427
|
const waiter = self.makeAwaiter( type );
|
|
392
428
|
odp(
|
|
393
429
|
waiter,
|
|
@@ -19,7 +19,7 @@ const {
|
|
|
19
19
|
|
|
20
20
|
import TypesUtils from '../utils';
|
|
21
21
|
const {
|
|
22
|
-
|
|
22
|
+
getCachedTypeChecker,
|
|
23
23
|
findSubTypeFromParent,
|
|
24
24
|
reflectPrimitiveWrappers
|
|
25
25
|
} = TypesUtils;
|
|
@@ -105,7 +105,7 @@ const makeSubTypeProxy = function (subtype: SubtypeEntry, inheritedInstance: unk
|
|
|
105
105
|
return instance;
|
|
106
106
|
};
|
|
107
107
|
|
|
108
|
-
const typeChecker =
|
|
108
|
+
const typeChecker = getCachedTypeChecker(subtype.TypeName);
|
|
109
109
|
Object.defineProperty(SubTypeProxy, Symbol.hasInstance, {
|
|
110
110
|
value : typeChecker
|
|
111
111
|
});
|
|
@@ -113,6 +113,11 @@ const makeSubTypeProxy = function (subtype: SubtypeEntry, inheritedInstance: unk
|
|
|
113
113
|
return SubTypeProxy;
|
|
114
114
|
};
|
|
115
115
|
|
|
116
|
+
// per-instance cache of subtype constructors: resolving instance.SubType
|
|
117
|
+
// used to allocate a fresh closure (and a type checker) on EVERY property
|
|
118
|
+
// access — the most expensive read path in the library by far
|
|
119
|
+
const SubTypeProxyCache = new WeakMap<object, Map<string, object>>();
|
|
120
|
+
|
|
116
121
|
const prepareSubtypeForConstruction = function (subtypeName: string, inheritedInstance: unknown) {
|
|
117
122
|
// prototype of proxy
|
|
118
123
|
const propInstance = Reflect.getPrototypeOf(inheritedInstance as object) as object;
|
|
@@ -139,10 +144,24 @@ const prepareSubtypeForConstruction = function (subtypeName: string, inheritedIn
|
|
|
139
144
|
subtypeName
|
|
140
145
|
);
|
|
141
146
|
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
147
|
+
if (!subtype) {
|
|
148
|
+
return undefined;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
let instanceCache = SubTypeProxyCache.get(inheritedInstance as object);
|
|
152
|
+
if (!instanceCache) {
|
|
153
|
+
instanceCache = new Map<string, object>();
|
|
154
|
+
SubTypeProxyCache.set(inheritedInstance as object, instanceCache);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
let result = instanceCache.get(subtypeName);
|
|
158
|
+
if (!result) {
|
|
159
|
+
result = makeSubTypeProxy(
|
|
160
|
+
subtype,
|
|
161
|
+
inheritedInstance
|
|
162
|
+
) as object;
|
|
163
|
+
instanceCache.set(subtypeName, result);
|
|
164
|
+
}
|
|
146
165
|
return result;
|
|
147
166
|
};
|
|
148
167
|
|
|
@@ -229,7 +248,7 @@ export const Mnemosyne = function (this: object, mnemonica: object) {
|
|
|
229
248
|
Symbol.hasInstance,
|
|
230
249
|
{
|
|
231
250
|
get () {
|
|
232
|
-
const result =
|
|
251
|
+
const result = getCachedTypeChecker(this.constructor.name);
|
|
233
252
|
return result;
|
|
234
253
|
}
|
|
235
254
|
}
|
|
@@ -245,18 +264,18 @@ export const Mnemosyne = function (this: object, mnemonica: object) {
|
|
|
245
264
|
|
|
246
265
|
} as _Internal_TC_<object>;
|
|
247
266
|
|
|
248
|
-
const createMnemosyne = function (
|
|
249
|
-
// const createMnemosyne = function (
|
|
250
|
-
// if (typeof
|
|
251
|
-
// const { __type__: type,
|
|
252
|
-
// console.log(type,
|
|
267
|
+
const createMnemosyne = function (ancestor: unknown) {
|
|
268
|
+
// const createMnemosyne = function (ancestor: unknown, typeProxy: unknown) {
|
|
269
|
+
// if (typeof ancestor === 'undefined') {
|
|
270
|
+
// const { __type__: type, ancestor: _ancestor } = typeProxy;
|
|
271
|
+
// console.log(type, _ancestor);
|
|
253
272
|
// // eslint-disable-next-line no-debugger
|
|
254
273
|
// debugger;
|
|
255
|
-
// throw new Error('createMnemosyne
|
|
274
|
+
// throw new Error('createMnemosyne ancestor is not defined for typeProxy.');
|
|
256
275
|
// }
|
|
257
276
|
|
|
258
|
-
const
|
|
259
|
-
const mnemosyne = new Mnemosyne(
|
|
277
|
+
const wrappedAncestor = reflectPrimitiveWrappers(ancestor);
|
|
278
|
+
const mnemosyne = new Mnemosyne(wrappedAncestor);
|
|
260
279
|
const mnemosyneProxy = new Proxy(
|
|
261
280
|
mnemosyne,
|
|
262
281
|
{
|
package/src/api/types/Props.ts
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
import { constants } from '../../constants';
|
|
4
4
|
import type {
|
|
5
|
-
CollectionDef, InstanceCreatorContext, Props as PropsType, TypeDef
|
|
5
|
+
CollectionDef, InstanceCreatorContext, Props as PropsType, TypeDef
|
|
6
6
|
} from '../../types';
|
|
7
7
|
|
|
8
8
|
const { odp, } = constants;
|
|
@@ -11,7 +11,17 @@ const { odp, } = constants;
|
|
|
11
11
|
// Instance metadata (__type__, __parent__, __args__, etc.) is stored
|
|
12
12
|
// against the prototype object, not the instance itself, so it never
|
|
13
13
|
// shows up in for...in, Object.keys, or JSON.stringify.
|
|
14
|
+
//
|
|
15
|
+
// Records may also be keyed by an object itself (not its prototype):
|
|
16
|
+
// this is how error instances carry their data (args, originalError,
|
|
17
|
+
// instance, exceptionReason, reasons, surplus) — nothing is defined
|
|
18
|
+
// on the error object, getProps(error) finds the record directly.
|
|
19
|
+
// That second key-space lives in a separate WeakMap: memory layer
|
|
20
|
+
// objects are themselves keys in __props__, so sharing one map would
|
|
21
|
+
// conflate the two spaces (a layer would "find" its own record when
|
|
22
|
+
// the caller meant to reach the parent layer's one).
|
|
14
23
|
const __props__ = new WeakMap();
|
|
24
|
+
const __object_props__ = new WeakMap();
|
|
15
25
|
|
|
16
26
|
const nativeProps = new Set([
|
|
17
27
|
'__proto_proto__',
|
|
@@ -28,7 +38,7 @@ const nativeProps = new Set([
|
|
|
28
38
|
|
|
29
39
|
export const _addProps = function (this: InstanceCreatorContext): void {
|
|
30
40
|
|
|
31
|
-
|
|
41
|
+
|
|
32
42
|
const self = this;
|
|
33
43
|
|
|
34
44
|
const {
|
|
@@ -143,8 +153,8 @@ export const _addProps = function (this: InstanceCreatorContext): void {
|
|
|
143
153
|
|
|
144
154
|
// __props__.set(self, value);
|
|
145
155
|
__props__.set(
|
|
146
|
-
proto!,
|
|
147
|
-
value
|
|
156
|
+
proto!,
|
|
157
|
+
value
|
|
148
158
|
);
|
|
149
159
|
|
|
150
160
|
};
|
|
@@ -157,6 +167,13 @@ const isObjectNature = (instance: unknown) => {
|
|
|
157
167
|
|
|
158
168
|
export const _getProps = (instance: object, base?: object): PropsType | undefined => {
|
|
159
169
|
if (!isObjectNature(instance)) return undefined;
|
|
170
|
+
// object-keyed records (error props etc.) take precedence over
|
|
171
|
+
// the prototype-layer records looked up below
|
|
172
|
+
const ownRecord = __object_props__.get(instance);
|
|
173
|
+
if (ownRecord !== undefined) {
|
|
174
|
+
const ownResult = ownRecord as PropsType;
|
|
175
|
+
return ownResult;
|
|
176
|
+
}
|
|
160
177
|
const proto = Reflect.getPrototypeOf(instance) as object;
|
|
161
178
|
if (
|
|
162
179
|
base !== undefined &&
|
|
@@ -185,6 +202,10 @@ export const _getProps = (instance: object, base?: object): PropsType | undefine
|
|
|
185
202
|
export const _setSelf = (instance: object): void => {
|
|
186
203
|
// const props = __props__.get(instance);
|
|
187
204
|
const props = _getProps(instance);
|
|
205
|
+
// __self__ is installed here, not in _addProps, because it is the
|
|
206
|
+
// only prop whose value is the instance itself — which does not
|
|
207
|
+
// exist yet when _addProps runs. It serves as the async completion
|
|
208
|
+
// marker (makeAwaiter) and the self-call detector (fork).
|
|
188
209
|
Object.defineProperty(
|
|
189
210
|
props,
|
|
190
211
|
'__self__',
|
|
@@ -194,10 +215,6 @@ export const _setSelf = (instance: object): void => {
|
|
|
194
215
|
}
|
|
195
216
|
}
|
|
196
217
|
);
|
|
197
|
-
__props__.set(
|
|
198
|
-
instance,
|
|
199
|
-
props
|
|
200
|
-
);
|
|
201
218
|
};
|
|
202
219
|
|
|
203
220
|
export const getProps = (instance: object): PropsType | undefined => {
|
|
@@ -227,32 +244,50 @@ export const getProps = (instance: object): PropsType | undefined => {
|
|
|
227
244
|
|
|
228
245
|
export const setProps = (instance: object, _values: object): string[] | false => {
|
|
229
246
|
const props = _getProps(instance);
|
|
247
|
+
const prevAdditions = props ? __props__.get(props) : undefined;
|
|
248
|
+
const values = Object.getOwnPropertyDescriptors(_values);
|
|
249
|
+
const written: string[] = [];
|
|
250
|
+
const allowed = {};
|
|
251
|
+
if (prevAdditions instanceof Object) {
|
|
252
|
+
// repeated setProps calls accumulate: previous additions are the
|
|
253
|
+
// base, new values override per key
|
|
254
|
+
Object.defineProperties(
|
|
255
|
+
allowed,
|
|
256
|
+
Object.getOwnPropertyDescriptors(prevAdditions)
|
|
257
|
+
);
|
|
258
|
+
}
|
|
259
|
+
Object.entries(values).forEach(([ name, value ]) => {
|
|
260
|
+
if (!nativeProps.has(name)) {
|
|
261
|
+
written.push(name);
|
|
262
|
+
Object.defineProperty(
|
|
263
|
+
allowed,
|
|
264
|
+
name,
|
|
265
|
+
value
|
|
266
|
+
);
|
|
267
|
+
}
|
|
268
|
+
});
|
|
230
269
|
if (props) {
|
|
231
|
-
const values = Object.getOwnPropertyDescriptors(_values);
|
|
232
|
-
const written: string[] = [];
|
|
233
|
-
const allowed = {};
|
|
234
|
-
Object.entries(values).forEach(([ name, value ]) => {
|
|
235
|
-
if (!nativeProps.has(name)) {
|
|
236
|
-
written.push(name);
|
|
237
|
-
Object.defineProperty(
|
|
238
|
-
allowed,
|
|
239
|
-
name,
|
|
240
|
-
value
|
|
241
|
-
);
|
|
242
|
-
}
|
|
243
|
-
});
|
|
244
270
|
__props__.set(
|
|
245
271
|
props,
|
|
246
272
|
allowed
|
|
247
273
|
);
|
|
248
274
|
return written;
|
|
249
275
|
}
|
|
276
|
+
if (isObjectNature(instance)) {
|
|
277
|
+
// no props record anywhere in the chain (plain objects, Error
|
|
278
|
+
// objects, library errors) — start one, keyed by the object itself
|
|
279
|
+
__object_props__.set(
|
|
280
|
+
instance,
|
|
281
|
+
allowed
|
|
282
|
+
);
|
|
283
|
+
return written;
|
|
284
|
+
}
|
|
250
285
|
return false;
|
|
251
286
|
};
|
|
252
287
|
|
|
253
288
|
// Re-export types from centralized types
|
|
254
289
|
export type {
|
|
255
|
-
CollectionDef, TypeDef, PropsType as Props
|
|
290
|
+
CollectionDef, TypeDef, PropsType as Props
|
|
256
291
|
};
|
|
257
292
|
|
|
258
293
|
module.exports = {
|
|
@@ -40,17 +40,18 @@ interface TypeProxyConstructHandler {
|
|
|
40
40
|
// Type for TypeProxy instance — data + traps
|
|
41
41
|
export interface TypeProxyInstance extends TypeProxyGetHandler, TypeProxySetHandler, TypeProxyConstructHandler {
|
|
42
42
|
__type__: TypeProxyType;
|
|
43
|
-
|
|
43
|
+
// the ancestor new instances descend from (root proto or apply-trap entity)
|
|
44
|
+
ancestor: unknown;
|
|
44
45
|
apply: typeof subTypeApply;
|
|
45
46
|
new (...args: unknown[]): object;
|
|
46
47
|
}
|
|
47
48
|
|
|
48
|
-
export const TypeProxy = function (__type__: TypeProxyType,
|
|
49
|
+
export const TypeProxy = function (__type__: TypeProxyType, ancestor: unknown) {
|
|
49
50
|
Object.assign(
|
|
50
51
|
this,
|
|
51
52
|
{
|
|
52
53
|
__type__,
|
|
53
|
-
|
|
54
|
+
ancestor
|
|
54
55
|
}
|
|
55
56
|
);
|
|
56
57
|
const typeProxy = new Proxy(
|
|
@@ -150,12 +151,12 @@ const primaryTypeApply = function (
|
|
|
150
151
|
this: TypeProxyInstance,
|
|
151
152
|
// proxy target
|
|
152
153
|
__: unknown,
|
|
153
|
-
|
|
154
|
+
thisArg: unknown,
|
|
154
155
|
args: [constructorOptions, ...unknown[]],
|
|
155
156
|
) {
|
|
156
157
|
const type = this.__type__;
|
|
157
158
|
// case of decorator like usage
|
|
158
|
-
if (
|
|
159
|
+
if (thisArg === undefined) {
|
|
159
160
|
const decorator = subTypeApply(
|
|
160
161
|
type,
|
|
161
162
|
args[ 0 ]
|
|
@@ -167,7 +168,7 @@ const primaryTypeApply = function (
|
|
|
167
168
|
// our PrimaryType whick is === instance of current TypeProxy
|
|
168
169
|
const InstanceCreatorProxy = new TypeProxy(
|
|
169
170
|
type,
|
|
170
|
-
|
|
171
|
+
thisArg
|
|
171
172
|
);
|
|
172
173
|
const instance = new InstanceCreatorProxy(...args);
|
|
173
174
|
return instance;
|
|
@@ -191,15 +192,15 @@ TypeProxy.prototype.construct = function (this: TypeProxyInstance, _target: unkn
|
|
|
191
192
|
|
|
192
193
|
const {
|
|
193
194
|
__type__: type,
|
|
194
|
-
|
|
195
|
+
ancestor
|
|
195
196
|
} = this;
|
|
196
197
|
|
|
197
198
|
// so this is a direct Sub-Type invocation
|
|
198
199
|
// having no existentInstance created earlier
|
|
199
200
|
// then we should rely on that somehow
|
|
200
|
-
const
|
|
201
|
+
const parentProto = type.isSubType ? getDefaultPrototype() : ancestor;
|
|
201
202
|
|
|
202
|
-
const mnemosyneProxy = createMnemosyne(
|
|
203
|
+
const mnemosyneProxy = createMnemosyne(parentProto);
|
|
203
204
|
const instance = new InstanceCreator(
|
|
204
205
|
type,
|
|
205
206
|
mnemosyneProxy,
|
|
@@ -92,12 +92,20 @@ const getFunctionConstructor = (
|
|
|
92
92
|
...args
|
|
93
93
|
);
|
|
94
94
|
} else {
|
|
95
|
-
|
|
95
|
+
// historical prototype swap kept for reference:
|
|
96
|
+
// const _proto = ConstructHandler.prototype;
|
|
96
97
|
// !!! it MUST be strict replacement !!!
|
|
97
98
|
// !!! this is the only way to keep Prototype Chain correct !!!
|
|
98
|
-
ConstructHandler.prototype = this.constructor.prototype;
|
|
99
|
-
answer = new (ConstructHandler as unknown as new (...args: unknown[]) => object)( ...args );
|
|
100
|
-
ConstructHandler.prototype = _proto;
|
|
99
|
+
// ConstructHandler.prototype = this.constructor.prototype;
|
|
100
|
+
// answer = new (ConstructHandler as unknown as new (...args: unknown[]) => object)( ...args );
|
|
101
|
+
// ConstructHandler.prototype = _proto;
|
|
102
|
+
|
|
103
|
+
const constructResult = Reflect.construct(
|
|
104
|
+
ConstructHandler,
|
|
105
|
+
args,
|
|
106
|
+
this.constructor
|
|
107
|
+
);
|
|
108
|
+
answer = constructResult;
|
|
101
109
|
}
|
|
102
110
|
const result = CreationHandler.call(
|
|
103
111
|
this,
|