mnemonica 1.1.0 → 1.2.1
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/AGENTS.md +31 -18
- package/.ai/DEBUG.md +7 -8
- package/.ai/ONBOARDING.md +24 -7
- package/.ai/PROTOTYPE-CHAIN.md +33 -2
- package/.ai/{ask/AGENTS.md → mode-ask.md} +1 -1
- package/.ai/{orchestrator/AGENTS.md → mode-orchestrator.md} +2 -19
- package/{docs → .ai}/performance-vs-security.md +1 -1
- package/.ai/{rules-skill/async-constructors.md → rules-async-constructors.md} +146 -0
- package/.ai/{rules/CODING.md → rules-coding.md} +19 -2
- package/.ai/{rules/CONTEXT-CONDENSING.md → rules-context-condensing.md} +2 -2
- package/.ai/{rules-skill/contributing.md → rules-contributing.md} +35 -0
- package/.ai/{rules-skill/define-patterns.md → rules-define-patterns.md} +39 -0
- package/.ai/{rules-skill/instance-methods.md → rules-instance-methods.md} +1 -1
- package/.ai/rules-reminders.md +19 -0
- package/.ai/{rules-skill/type-system.md → rules-type-system.md} +1 -1
- package/{docs → .ai}/theory-of-operations.md +4 -4
- package/AGENTS.md +76 -32
- package/CONTRIBUTING.md +4 -18
- package/FOR_HUMANS.md +123 -83
- package/README.md +65 -28
- package/SKILL.md +27 -22
- package/build/api/errors/index.js +7 -5
- package/build/api/index.d.ts +1 -0
- package/build/api/index.js +2 -1
- package/build/api/types/InstanceCreator.js +16 -5
- package/build/api/types/Mnemosyne.d.ts +3 -14
- package/build/api/types/Mnemosyne.js +21 -8
- package/build/api/types/Props.js +1 -2
- 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 +77 -33
- 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 +14 -3
- package/build/index.js +34 -7
- package/build/types/index.d.ts +24 -4
- package/build/types/index.js +1 -1
- package/build/utils/parent.js +35 -5
- package/docs/ai-learning-trajectory.md +2 -3
- package/docs/async-constructors.md +1 -1
- package/docs/decorate.md +123 -0
- package/docs/empathy-in-ai.md +2 -3
- package/docs/hott-primer.md +1 -1
- package/docs/prototype-chain.md +4 -0
- package/docs/purpose.md +3 -3
- package/{.ai/TACTICA-DEEP-DIVE.md → docs/tactica-deep-dive.md} +44 -1
- package/docs/tactica-pattern.md +1 -2
- package/docs/typed-lookup.md +183 -50
- package/package.json +8 -8
- package/src/api/errors/index.ts +10 -4
- package/src/api/index.ts +2 -8
- package/src/api/types/InstanceCreator.ts +28 -6
- package/src/api/types/Mnemosyne.ts +34 -15
- package/src/api/types/Props.ts +4 -4
- package/src/api/types/TypeProxy.ts +10 -9
- package/src/api/types/compileNewModificatorFunctionBody.ts +12 -4
- package/src/api/types/index.ts +136 -69
- package/src/api/utils/index.ts +27 -9
- package/src/descriptors/types/index.ts +81 -3
- package/src/index.ts +150 -7
- package/src/types/index.ts +120 -10
- package/src/utils/parent.ts +48 -4
- package/.ai/CODE.md +0 -111
- package/.ai/TACTICA-RULES.md +0 -103
- package/.ai/async_init.md +0 -94
- package/.ai/rules/REMINDERS.md +0 -63
- package/.ai/rules-skill/lookup-typed.md +0 -82
- package/.ai/rules-skill/proxy-architecture.md +0 -63
- package/.ai/task-templates/new-feature.md +0 -46
- /package/.ai/{rules-skill/code-style.md → rules-code-style.md} +0 -0
- /package/.ai/{rules-skill/ecosystem.md → rules-ecosystem.md} +0 -0
- /package/.ai/{rules-skill/error-system.md → rules-error-system.md} +0 -0
- /package/.ai/{rules-skill/hooks.md → rules-hooks.md} +0 -0
- /package/.ai/{rules-skill/philosophy.md → rules-philosophy.md} +0 -0
- /package/.ai/{rules-skill/testing.md → rules-testing.md} +0 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mnemonica",
|
|
3
|
-
"version": "1.1
|
|
3
|
+
"version": "1.2.1",
|
|
4
4
|
"description": "AI-agent ready instance inheritance system for JavaScript/TypeScript via prototype chains",
|
|
5
5
|
"type": "commonjs",
|
|
6
6
|
"main": "./build/index.js",
|
|
@@ -50,8 +50,8 @@
|
|
|
50
50
|
"example:v8bug": "npm run build && node ./examples/v8bug.js",
|
|
51
51
|
"example:ts": "npx tsc ./test-ts/*.ts && npx eslint --fix ./test-ts/*.js && node ./test-ts/test-no-types.js && node ./test-ts/test-with-types.js",
|
|
52
52
|
"deopt": "node --trace-deopt --trace-opt ./node_modules/mocha/bin/mocha.js ./test/index.js",
|
|
53
|
-
"decorate:ts": "npx tsc --ignoreConfig --target ES2021 --moduleResolution NodeNext --module NodeNext --sourceMap ./test/decorate.ts",
|
|
54
|
-
"lint:md": "find . -name '*.md' -not -path '*/node_modules/*' | xargs markdown-link-check -q --config .markdown-link-check.json",
|
|
53
|
+
"decorate:ts": "npx tsc --ignoreConfig --target ES2021 --moduleResolution NodeNext --module NodeNext --sourceMap ./test/decorate.ts ./test/decorate-builder.ts",
|
|
54
|
+
"lint:md": "node scripts/check-published-links.js && find . -name '*.md' -not -path '*/node_modules/*' | xargs markdown-link-check -q --config .markdown-link-check.json",
|
|
55
55
|
"lint": "npx eslint",
|
|
56
56
|
"lint:check": "npx eslint ./src ./test",
|
|
57
57
|
"lint:src": "npx eslint --fix ./src",
|
|
@@ -97,11 +97,11 @@
|
|
|
97
97
|
"devDependencies": {
|
|
98
98
|
"@eslint/eslintrc": "^3.3.6",
|
|
99
99
|
"@eslint/js": "^9.21.0",
|
|
100
|
-
"@mnemonica/tactica": "^0.1.
|
|
100
|
+
"@mnemonica/tactica": "^0.1.4",
|
|
101
101
|
"@types/jest": "^29.5.14",
|
|
102
102
|
"@types/node": "^22.20.1",
|
|
103
|
-
"@typescript-eslint/eslint-plugin": "^8.
|
|
104
|
-
"@typescript-eslint/parser": "^8.
|
|
103
|
+
"@typescript-eslint/eslint-plugin": "^8.65.0",
|
|
104
|
+
"@typescript-eslint/parser": "^8.65.0",
|
|
105
105
|
"chai": "^5.3.3",
|
|
106
106
|
"eslint": "^9.39.5",
|
|
107
107
|
"eslint-plugin-import": "^2.32.0",
|
|
@@ -112,12 +112,12 @@
|
|
|
112
112
|
"jest": "^29.7.0",
|
|
113
113
|
"json5": "^2.2.3",
|
|
114
114
|
"lint-staged": "^15.5.2",
|
|
115
|
-
"markdown-link-check": "^3.
|
|
115
|
+
"markdown-link-check": "^3.15.0",
|
|
116
116
|
"mocha": "^11.7.6",
|
|
117
117
|
"mocha-lcov-reporter": "^1.3.0",
|
|
118
118
|
"nyc": "^17.1.0",
|
|
119
119
|
"oxlint": "^0.16.12",
|
|
120
|
-
"ts-jest": "^29.4.
|
|
120
|
+
"ts-jest": "^29.4.12",
|
|
121
121
|
"typeomatica": "^0.3.59",
|
|
122
122
|
"typescript": "^6.0.3"
|
|
123
123
|
},
|
package/src/api/errors/index.ts
CHANGED
|
@@ -22,11 +22,17 @@ export interface StackableInstance {
|
|
|
22
22
|
export const cleanupStack = ( stack: string[] ) => {
|
|
23
23
|
const cleaned: string[] = stack.reduce(
|
|
24
24
|
( arr: string[], line: string ) => {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
25
|
+
// a line survives only when NO registered cleaner matches it;
|
|
26
|
+
// the previous logic pushed the line once per non-matching
|
|
27
|
+
// cleaner, duplicating lines and leaking matched ones
|
|
28
|
+
// as soon as two cleaners were registered
|
|
29
|
+
const keep = stackCleaners.every( cleanerRegExp => {
|
|
30
|
+
const noMatch = !cleanerRegExp.test( line );
|
|
31
|
+
return noMatch;
|
|
29
32
|
} );
|
|
33
|
+
if ( keep ) {
|
|
34
|
+
arr.push( line );
|
|
35
|
+
}
|
|
30
36
|
return arr;
|
|
31
37
|
},
|
|
32
38
|
[]
|
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 ) {
|
|
@@ -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
|
@@ -185,6 +185,10 @@ export const _getProps = (instance: object, base?: object): PropsType | undefine
|
|
|
185
185
|
export const _setSelf = (instance: object): void => {
|
|
186
186
|
// const props = __props__.get(instance);
|
|
187
187
|
const props = _getProps(instance);
|
|
188
|
+
// __self__ is installed here, not in _addProps, because it is the
|
|
189
|
+
// only prop whose value is the instance itself — which does not
|
|
190
|
+
// exist yet when _addProps runs. It serves as the async completion
|
|
191
|
+
// marker (makeAwaiter) and the self-call detector (fork).
|
|
188
192
|
Object.defineProperty(
|
|
189
193
|
props,
|
|
190
194
|
'__self__',
|
|
@@ -194,10 +198,6 @@ export const _setSelf = (instance: object): void => {
|
|
|
194
198
|
}
|
|
195
199
|
}
|
|
196
200
|
);
|
|
197
|
-
__props__.set(
|
|
198
|
-
instance,
|
|
199
|
-
props
|
|
200
|
-
);
|
|
201
201
|
};
|
|
202
202
|
|
|
203
203
|
export const getProps = (instance: object): PropsType | undefined => {
|
|
@@ -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,
|
package/src/api/types/index.ts
CHANGED
|
@@ -35,7 +35,7 @@ import {
|
|
|
35
35
|
|
|
36
36
|
// Lazy getter: a function that, when called, returns the actual constructor function.
|
|
37
37
|
// Used when define() receives an anonymous function wrapping the real constructor.
|
|
38
|
-
interface LazyTypeGetter extends CallableFunction {
|
|
38
|
+
export interface LazyTypeGetter extends CallableFunction {
|
|
39
39
|
(): ConstructHandler;
|
|
40
40
|
}
|
|
41
41
|
|
|
@@ -76,7 +76,7 @@ import compileNewModificatorFunctionBody, { ConstructHandler } from './compileNe
|
|
|
76
76
|
|
|
77
77
|
import TypesUtils, { CreationHandler } from '../utils';
|
|
78
78
|
const {
|
|
79
|
-
|
|
79
|
+
getCachedTypeChecker,
|
|
80
80
|
getTypeSplitPath,
|
|
81
81
|
checkTypeName,
|
|
82
82
|
isClass,
|
|
@@ -149,7 +149,22 @@ const TypeDescriptor = function (
|
|
|
149
149
|
|
|
150
150
|
config,
|
|
151
151
|
|
|
152
|
-
hooks : Object.create(null)
|
|
152
|
+
hooks : Object.create(null),
|
|
153
|
+
|
|
154
|
+
decorate : function (options?: object) {
|
|
155
|
+
const self = type;
|
|
156
|
+
const decorator = function (cstr: CallableFunction) {
|
|
157
|
+
const { name } = cstr;
|
|
158
|
+
const defineResult = self.define(
|
|
159
|
+
name,
|
|
160
|
+
cstr,
|
|
161
|
+
options
|
|
162
|
+
);
|
|
163
|
+
const decoratedResult = defineResult as unknown as CallableFunction;
|
|
164
|
+
return decoratedResult;
|
|
165
|
+
};
|
|
166
|
+
return decorator;
|
|
167
|
+
},
|
|
153
168
|
|
|
154
169
|
}
|
|
155
170
|
);
|
|
@@ -170,13 +185,13 @@ const TypeDescriptor = function (
|
|
|
170
185
|
}
|
|
171
186
|
);
|
|
172
187
|
|
|
173
|
-
// const
|
|
174
|
-
const
|
|
188
|
+
// const ancestor = isSubType ? Object.create(null) : proto;
|
|
189
|
+
const ancestor = isSubType ? undefined : proto;
|
|
175
190
|
types.set(
|
|
176
191
|
TypeName,
|
|
177
192
|
new TypeProxy(
|
|
178
193
|
type,
|
|
179
|
-
|
|
194
|
+
ancestor
|
|
180
195
|
)
|
|
181
196
|
);
|
|
182
197
|
|
|
@@ -208,22 +223,69 @@ TypeDescriptor.prototype.define = function (
|
|
|
208
223
|
return result;
|
|
209
224
|
};
|
|
210
225
|
|
|
226
|
+
TypeDescriptor.prototype.lazy = function (
|
|
227
|
+
this: TypeDescriptorInstance,
|
|
228
|
+
arg1: string | CallableFunction,
|
|
229
|
+
arg2?: CallableFunction | object,
|
|
230
|
+
arg3?: object
|
|
231
|
+
) {
|
|
232
|
+
let name: string | undefined;
|
|
233
|
+
let getter: LazyTypeGetter;
|
|
234
|
+
let config: object | undefined;
|
|
235
|
+
if (typeof arg1 === 'string') {
|
|
236
|
+
name = arg1;
|
|
237
|
+
getter = arg2 as LazyTypeGetter;
|
|
238
|
+
config = arg3;
|
|
239
|
+
} else {
|
|
240
|
+
getter = arg1 as LazyTypeGetter;
|
|
241
|
+
config = arg2 as object;
|
|
242
|
+
}
|
|
243
|
+
let result: TypeClass;
|
|
244
|
+
if (name) {
|
|
245
|
+
result = lazy.call(
|
|
246
|
+
this,
|
|
247
|
+
this.subtypes as TypesMap,
|
|
248
|
+
name,
|
|
249
|
+
getter as LazyTypeGetter,
|
|
250
|
+
config
|
|
251
|
+
);
|
|
252
|
+
} else {
|
|
253
|
+
result = lazy.call(
|
|
254
|
+
this,
|
|
255
|
+
this.subtypes as TypesMap,
|
|
256
|
+
getter as LazyTypeGetter,
|
|
257
|
+
config
|
|
258
|
+
);
|
|
259
|
+
}
|
|
260
|
+
return result;
|
|
261
|
+
};
|
|
262
|
+
|
|
211
263
|
TypeDescriptor.prototype.lookup = function (
|
|
212
264
|
this: TypeDescriptorInstance,
|
|
213
265
|
TypeNestedPath: string
|
|
214
266
|
) {
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
267
|
+
// relative first: resolve among this type's own subtypes,
|
|
268
|
+
// e.g. AdminType.lookup('SubType') or 'SubType.DeepSubType'
|
|
269
|
+
const relativeResult = lookup.call(
|
|
270
|
+
this.subtypes as TypesMap,
|
|
271
|
+
TypeNestedPath
|
|
218
272
|
);
|
|
219
|
-
|
|
273
|
+
if (relativeResult) {
|
|
274
|
+
return relativeResult;
|
|
275
|
+
}
|
|
276
|
+
// root fallback: resolve absolute registry paths from the
|
|
277
|
+
// collection, e.g. AdminType.lookup('UserType.AdminType'),
|
|
278
|
+
// which is what the TypeLookup<Registry> type contract promises
|
|
279
|
+
const collection = this.collection as CollectionDef;
|
|
280
|
+
const rootResult = collection.lookup(TypeNestedPath);
|
|
281
|
+
return rootResult;
|
|
220
282
|
};
|
|
221
283
|
|
|
222
284
|
odp(
|
|
223
285
|
TypeDescriptor.prototype,
|
|
224
286
|
Symbol.hasInstance, {
|
|
225
287
|
get (this: TypeDescriptorInstance) {
|
|
226
|
-
const result =
|
|
288
|
+
const result = getCachedTypeChecker(this.TypeName);
|
|
227
289
|
return result;
|
|
228
290
|
}
|
|
229
291
|
}
|
|
@@ -287,27 +349,6 @@ const checkDuplicate = function (target: TypesMap, name: string): void {
|
|
|
287
349
|
}
|
|
288
350
|
};
|
|
289
351
|
|
|
290
|
-
// ============================================================
|
|
291
|
-
// LAZY GETTER DETECTION
|
|
292
|
-
// A lazy getter is a function that, when called, returns a
|
|
293
|
-
// named constructor. Used for late-bound type definitions.
|
|
294
|
-
// If the call throws or returns a non-named function, it is
|
|
295
|
-
// treated as a direct handler instead.
|
|
296
|
-
// ============================================================
|
|
297
|
-
|
|
298
|
-
const isLazyGetter = function (handler: ConstructHandler | LazyTypeGetter): boolean {
|
|
299
|
-
try {
|
|
300
|
-
// Probe: try to call as a lazy getter. If it succeeds and returns a named
|
|
301
|
-
// function, it's a lazy getter. Otherwise (throws or returns non-function),
|
|
302
|
-
// it's a direct handler. The cast is the probe — runtime decides which it is.
|
|
303
|
-
const lazyResult = (handler as LazyTypeGetter)();
|
|
304
|
-
const result = lazyResult instanceof Function && lazyResult.name.length > 0;
|
|
305
|
-
return result;
|
|
306
|
-
} catch {
|
|
307
|
-
return false;
|
|
308
|
-
}
|
|
309
|
-
};
|
|
310
|
-
|
|
311
352
|
// ============================================================
|
|
312
353
|
// CONSTRUCTION STRATEGIES
|
|
313
354
|
// Two branches, same return type, no recursion.
|
|
@@ -364,25 +405,40 @@ const createFromDirectHandler = function (
|
|
|
364
405
|
const createFromLazyGetter = function (
|
|
365
406
|
defineOrigin: TypeAbsorber,
|
|
366
407
|
target: TypesMap,
|
|
367
|
-
name: string,
|
|
408
|
+
name: string | undefined,
|
|
368
409
|
getter: LazyTypeGetter,
|
|
369
410
|
config: constructorOptions
|
|
370
411
|
) {
|
|
412
|
+
|
|
371
413
|
const type = getter();
|
|
372
414
|
|
|
373
415
|
if (typeof type !== 'function') {
|
|
374
416
|
throw new HANDLER_MUST_BE_A_FUNCTION;
|
|
375
417
|
}
|
|
376
418
|
|
|
377
|
-
const TypeName =
|
|
419
|
+
const TypeName = name || type.name;
|
|
378
420
|
if (!TypeName) {
|
|
379
421
|
throw new TYPENAME_MUST_BE_A_STRING;
|
|
380
422
|
}
|
|
381
423
|
|
|
424
|
+
checkDuplicate(
|
|
425
|
+
target,
|
|
426
|
+
TypeName
|
|
427
|
+
);
|
|
428
|
+
|
|
382
429
|
const asClass = isClass(type);
|
|
383
430
|
|
|
431
|
+
const proto = (
|
|
432
|
+
hop(
|
|
433
|
+
type,
|
|
434
|
+
'prototype'
|
|
435
|
+
) &&
|
|
436
|
+
(typeof type.prototype === 'object')
|
|
437
|
+
) ? type.prototype : getDefaultPrototype();
|
|
438
|
+
|
|
384
439
|
const makeConstructHandler = () => {
|
|
385
440
|
const constructHandler = getter();
|
|
441
|
+
|
|
386
442
|
odp(
|
|
387
443
|
constructHandler,
|
|
388
444
|
SymbolConstructorName, {
|
|
@@ -401,7 +457,8 @@ const createFromLazyGetter = function (
|
|
|
401
457
|
constructHandler.prototype = getDefaultPrototype();
|
|
402
458
|
}
|
|
403
459
|
|
|
404
|
-
|
|
460
|
+
const handlerResult = constructHandler as MnemonicaConstructorFactory;
|
|
461
|
+
return handlerResult;
|
|
405
462
|
};
|
|
406
463
|
|
|
407
464
|
config = Object.assign(
|
|
@@ -416,7 +473,7 @@ const createFromLazyGetter = function (
|
|
|
416
473
|
target,
|
|
417
474
|
TypeName,
|
|
418
475
|
makeConstructHandler,
|
|
419
|
-
|
|
476
|
+
proto,
|
|
420
477
|
config
|
|
421
478
|
) as unknown as TypeClass;
|
|
422
479
|
return result;
|
|
@@ -458,18 +515,9 @@ export const define = function (
|
|
|
458
515
|
);
|
|
459
516
|
return defineResult;
|
|
460
517
|
}
|
|
461
|
-
// Anonymous function
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
: config as constructorOptions;
|
|
465
|
-
const lazyResult = createFromLazyGetter(
|
|
466
|
-
this as TypeAbsorber,
|
|
467
|
-
subtypes,
|
|
468
|
-
'',
|
|
469
|
-
fn as LazyTypeGetter,
|
|
470
|
-
lazyConfig
|
|
471
|
-
);
|
|
472
|
-
return lazyResult;
|
|
518
|
+
// Anonymous function as first arg is not a valid .define() form.
|
|
519
|
+
// Use .lazy(() => Constructor, config?) for lazy getters instead.
|
|
520
|
+
throw new TYPENAME_MUST_BE_A_STRING;
|
|
473
521
|
}
|
|
474
522
|
|
|
475
523
|
// --- Branch: string path passed as first arg ---
|
|
@@ -478,9 +526,9 @@ export const define = function (
|
|
|
478
526
|
}
|
|
479
527
|
checkTypeName(TypeOrTypeName);
|
|
480
528
|
|
|
481
|
-
let handler: ConstructHandler |
|
|
529
|
+
let handler: ConstructHandler | undefined;
|
|
482
530
|
if (typeof constructHandlerOrConfig === 'function') {
|
|
483
|
-
handler = constructHandlerOrConfig as ConstructHandler
|
|
531
|
+
handler = constructHandlerOrConfig as ConstructHandler;
|
|
484
532
|
} else if (typeof constructHandlerOrConfig === 'object') {
|
|
485
533
|
config = constructHandlerOrConfig as constructorOptions;
|
|
486
534
|
}
|
|
@@ -497,25 +545,6 @@ export const define = function (
|
|
|
497
545
|
throw new WRONG_TYPE_DEFINITION('definition is not provided');
|
|
498
546
|
}
|
|
499
547
|
|
|
500
|
-
// If the path resolved to an existing type and the handler
|
|
501
|
-
// is a lazy getter, define the getter's returned constructor
|
|
502
|
-
// as a subtype of the existing type.
|
|
503
|
-
if (parent && handler && isLazyGetter(handler)) {
|
|
504
|
-
const lazyType = (handler as LazyTypeGetter)();
|
|
505
|
-
checkDuplicate(
|
|
506
|
-
parent.subtypes as TypesMap,
|
|
507
|
-
lazyType.name
|
|
508
|
-
);
|
|
509
|
-
const lazyParentResult = createFromLazyGetter(
|
|
510
|
-
this as TypeAbsorber,
|
|
511
|
-
parent.subtypes as TypesMap,
|
|
512
|
-
lazyType.name,
|
|
513
|
-
handler as LazyTypeGetter,
|
|
514
|
-
config as constructorOptions
|
|
515
|
-
);
|
|
516
|
-
return lazyParentResult;
|
|
517
|
-
}
|
|
518
|
-
|
|
519
548
|
const result = createFromDirectHandler(
|
|
520
549
|
this as TypeAbsorber,
|
|
521
550
|
target,
|
|
@@ -526,6 +555,44 @@ lazyType.name
|
|
|
526
555
|
return result;
|
|
527
556
|
};
|
|
528
557
|
|
|
558
|
+
export const lazy = function (
|
|
559
|
+
this: unknown,
|
|
560
|
+
subtypes: TypesMap,
|
|
561
|
+
arg1: string | LazyTypeGetter | undefined,
|
|
562
|
+
arg2?: LazyTypeGetter | object,
|
|
563
|
+
arg3?: object
|
|
564
|
+
): TypeClass {
|
|
565
|
+
|
|
566
|
+
let name: string | undefined;
|
|
567
|
+
let getter: LazyTypeGetter;
|
|
568
|
+
let config: object | undefined;
|
|
569
|
+
if (typeof arg1 === 'string') {
|
|
570
|
+
name = arg1;
|
|
571
|
+
getter = arg2 as LazyTypeGetter;
|
|
572
|
+
config = arg3;
|
|
573
|
+
} else {
|
|
574
|
+
getter = arg1 as LazyTypeGetter;
|
|
575
|
+
config = arg2 as object;
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
if (typeof getter !== 'function') {
|
|
579
|
+
throw new HANDLER_MUST_BE_A_FUNCTION;
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
if (!config || (typeof config !== 'object' && typeof config !== 'function')) {
|
|
583
|
+
config = {};
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
const lazyResult = createFromLazyGetter(
|
|
587
|
+
this as TypeAbsorber,
|
|
588
|
+
subtypes,
|
|
589
|
+
name,
|
|
590
|
+
getter as LazyTypeGetter,
|
|
591
|
+
config as constructorOptions
|
|
592
|
+
);
|
|
593
|
+
return lazyResult;
|
|
594
|
+
};
|
|
595
|
+
|
|
529
596
|
export const lookup = function (
|
|
530
597
|
this: TypesMap,
|
|
531
598
|
TypeNestedPath: string
|