mnemonica 1.3.9 → 1.4.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/FOR_HUMANS.md +83 -76
- package/README.md +2 -2
- package/build/api/hooks/HookInvocation.d.ts +2 -2
- package/build/api/hooks/HookInvocation.js +1 -1
- package/build/api/index.d.ts +1 -1
- package/build/api/types/InstanceCreator.js +14 -3
- package/build/api/types/Mnemosyne.js +4 -1
- package/build/api/types/TypeProxy.js +30 -5
- package/build/api/types/compileNewModificatorFunctionBody.d.ts +4 -1
- package/build/api/types/compileNewModificatorFunctionBody.js +12 -7
- package/build/api/types/index.d.ts +2 -6
- package/build/api/types/index.js +103 -77
- package/build/api/types/lazyDefinition.d.ts +22 -0
- package/build/api/types/lazyDefinition.js +49 -0
- package/build/descriptors/types/index.js +3 -21
- package/build/index.d.ts +1 -4
- package/build/index.js +8 -11
- package/build/types/index.d.ts +14 -11
- package/build/types/index.js +1 -1
- package/build/utils/index.js +10 -8
- package/build/utils/parse.js +5 -2
- package/docs/UTILS.md +3 -2
- package/docs/async-constructors.md +13 -9
- package/docs/empathy-in-ai.md +2 -2
- package/docs/prototype-chain.md +1 -1
- package/docs/typed-lookup.md +6 -3
- package/package.json +17 -14
- package/src/api/hooks/HookInvocation.ts +4 -4
- package/src/api/types/InstanceCreator.ts +56 -4
- package/src/api/types/Mnemosyne.ts +9 -0
- package/src/api/types/TypeProxy.ts +63 -7
- package/src/api/types/compileNewModificatorFunctionBody.ts +18 -4
- package/src/api/types/index.ts +247 -154
- package/src/api/types/lazyDefinition.ts +104 -0
- package/src/descriptors/types/index.ts +13 -34
- package/src/index.ts +13 -32
- package/src/types/index.ts +62 -43
- package/src/utils/index.ts +15 -14
- package/src/utils/parse.ts +8 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mnemonica",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.4.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",
|
|
@@ -43,9 +43,9 @@
|
|
|
43
43
|
"build:all": "npm run build && npm run decorate:ts",
|
|
44
44
|
"verify": "npm run build && npm run lint:check",
|
|
45
45
|
"example": "npm run build && node ./test/example.js",
|
|
46
|
-
"example:async": "npm run build && node ./
|
|
47
|
-
"example:rename": "npm run build && node ./
|
|
48
|
-
"example:v8bug": "npm run build && node ./
|
|
46
|
+
"example:async": "npm run build && node ./test-patterns/AsyncNewTest.js",
|
|
47
|
+
"example:rename": "npm run build && node ./test-patterns/ClassReName.js",
|
|
48
|
+
"example:v8bug": "npm run build && node ./test-patterns/v8bug.js",
|
|
49
49
|
"example:ts": "npx tsc --ignoreConfig --noCheck --experimentalDecorators --target ES2021 --module CommonJS ./test-ts/test-decorate.ts ./test-ts/test-decorate-typings.ts ./test-ts/test-example.ts ./test-ts/test-no-types.ts ./test-ts/typed-collections.ts ./test-ts/typed-utils.ts ./test-ts/typed-utils-registry.ts && npx eslint --fix ./test-ts/*.js && node ./test-ts/test-no-types.js && node ./test-ts/test-decorate-typings.js",
|
|
50
50
|
"test:ts:strict": "npx tsc -p ./test-ts/tsconfig.strict.json",
|
|
51
51
|
"test:ts:consumers": "node scripts/check-consumers.js",
|
|
@@ -98,11 +98,13 @@
|
|
|
98
98
|
"devDependencies": {
|
|
99
99
|
"@eslint/eslintrc": "^3.3.7",
|
|
100
100
|
"@eslint/js": "^9.21.0",
|
|
101
|
-
"@mnemonica/
|
|
101
|
+
"@mnemonica/lethe": "^0.1.0",
|
|
102
|
+
"@mnemonica/tactica": "^0.4.7",
|
|
102
103
|
"@types/jest": "^29.5.14",
|
|
103
|
-
"@types/node": "^22.20.
|
|
104
|
-
"@typescript-eslint/eslint-plugin": "^8.
|
|
105
|
-
"@typescript-eslint/parser": "^8.
|
|
104
|
+
"@types/node": "^22.20.5",
|
|
105
|
+
"@typescript-eslint/eslint-plugin": "^8.71.2",
|
|
106
|
+
"@typescript-eslint/parser": "^8.71.2",
|
|
107
|
+
"ajv": "^8.20.0",
|
|
106
108
|
"chai": "^5.3.3",
|
|
107
109
|
"eslint": "^9.39.5",
|
|
108
110
|
"eslint-plugin-import": "^2.32.0",
|
|
@@ -118,11 +120,9 @@
|
|
|
118
120
|
"mocha-lcov-reporter": "^1.3.0",
|
|
119
121
|
"nyc": "^17.1.0",
|
|
120
122
|
"oxlint": "^0.16.12",
|
|
121
|
-
"ts-jest": "^29.4.
|
|
122
|
-
"typeomatica": "^0.3.
|
|
123
|
-
"typescript": "^6.0.3"
|
|
124
|
-
"@mnemonica/lethe": "^0.1.0",
|
|
125
|
-
"ajv": "^8.17.1"
|
|
123
|
+
"ts-jest": "^29.4.15",
|
|
124
|
+
"typeomatica": "^0.3.64",
|
|
125
|
+
"typescript": "^6.0.3"
|
|
126
126
|
},
|
|
127
127
|
"engines": {
|
|
128
128
|
"node": ">=18 <26"
|
|
@@ -130,7 +130,10 @@
|
|
|
130
130
|
"overrides": {
|
|
131
131
|
"serialize-javascript": "^7.0.5",
|
|
132
132
|
"uuid": "^11.1.1",
|
|
133
|
-
"diff": "^9.0.0"
|
|
133
|
+
"diff": "^9.0.0",
|
|
134
|
+
"@istanbuljs/load-nyc-config": {
|
|
135
|
+
"js-yaml": "^4.1.0"
|
|
136
|
+
}
|
|
134
137
|
},
|
|
135
138
|
"lint-staged": {
|
|
136
139
|
"src/**/*.ts": "eslint --fix",
|
|
@@ -2,10 +2,10 @@
|
|
|
2
2
|
|
|
3
3
|
import type {
|
|
4
4
|
ThrowModificationError,
|
|
5
|
-
|
|
5
|
+
deliveredHookOpts, TypeDef
|
|
6
6
|
} from '../../types';
|
|
7
7
|
|
|
8
|
-
//
|
|
8
|
+
// deliveredHookOpts is used in build() return type
|
|
9
9
|
|
|
10
10
|
/**
|
|
11
11
|
* Builder for hook invocation arguments.
|
|
@@ -47,8 +47,8 @@ export class HookInvocation {
|
|
|
47
47
|
return this;
|
|
48
48
|
}
|
|
49
49
|
|
|
50
|
-
build ():
|
|
51
|
-
const result:
|
|
50
|
+
build (): deliveredHookOpts {
|
|
51
|
+
const result: deliveredHookOpts = {
|
|
52
52
|
type : this.type,
|
|
53
53
|
TypeName : this.TypeName,
|
|
54
54
|
existentInstance : this.existentInstance,
|
|
@@ -46,6 +46,12 @@ import {
|
|
|
46
46
|
import { makeInstanceModificator } from './InstanceModificator';
|
|
47
47
|
import { parent } from '../../utils/parent';
|
|
48
48
|
|
|
49
|
+
import {
|
|
50
|
+
gatherLazyDefinition,
|
|
51
|
+
readLazyDefinitionFailure,
|
|
52
|
+
resetLazyDefinition
|
|
53
|
+
} from './lazyDefinition';
|
|
54
|
+
|
|
49
55
|
// import { Mnemosyne } from './Mnemosyne';
|
|
50
56
|
|
|
51
57
|
const invokePreHooks = function ( this: InstanceCreatorContext ) {
|
|
@@ -249,6 +255,13 @@ const addThen = function ( this: InstanceCreatorContext, then: ThenSpec ) {
|
|
|
249
255
|
|
|
250
256
|
self.inheritedInstance = (self.inheritedInstance as Promise<object>)
|
|
251
257
|
.then( () => {
|
|
258
|
+
// async chain link: the lazy callback runs here, at the start of
|
|
259
|
+
// THIS link's construction — once the previous link resolved,
|
|
260
|
+
// not when the link was queued; a no-op for direct types, and
|
|
261
|
+
// skipped when the resolved previous link is an Error with
|
|
262
|
+
// blockErrors on (the circuit breaker then throws, as on the
|
|
263
|
+
// body path)
|
|
264
|
+
gatherLazyDefinition( then.subtype, self.inheritedInstance );
|
|
252
265
|
self.inheritedInstance =
|
|
253
266
|
new InstanceCreator(
|
|
254
267
|
then.subtype as TypeDef,
|
|
@@ -365,13 +378,28 @@ const runSetup = function (
|
|
|
365
378
|
TypeName
|
|
366
379
|
} = type;
|
|
367
380
|
|
|
381
|
+
// the facade destructuring above and this call must happen BEFORE
|
|
382
|
+
// ModificationConstructor() below: for a lazy type they resolve the
|
|
383
|
+
// current construction's gathered handler, and ModificationConstructor
|
|
384
|
+
// is user code — a nested construction of the same lazy type inside it
|
|
385
|
+
// would replace the gathered result
|
|
386
|
+
const ModificatorType = constructHandler();
|
|
387
|
+
|
|
388
|
+
// lazy types: snapshot the gather outcome (the failed flag AND the error
|
|
389
|
+
// value — a callback may throw undefined) alongside the facades above,
|
|
390
|
+
// then return the cell to its define-time state — everything the rest
|
|
391
|
+
// of this construction needs now lives on self, a nested construction
|
|
392
|
+
// cannot clobber it, and between constructions every facade read sees
|
|
393
|
+
// the define-time state (X.prototype stays the shim's empty prototype)
|
|
394
|
+
const lazyFailure = readLazyDefinitionFailure(type);
|
|
395
|
+
resetLazyDefinition(type);
|
|
396
|
+
|
|
368
397
|
const {
|
|
369
398
|
ModificationConstructor,
|
|
370
399
|
submitStack
|
|
371
400
|
} = config;
|
|
372
401
|
|
|
373
402
|
const mc = ModificationConstructor!();
|
|
374
|
-
const ModificatorType = constructHandler();
|
|
375
403
|
|
|
376
404
|
Object.assign(
|
|
377
405
|
self,
|
|
@@ -385,8 +413,10 @@ const runSetup = function (
|
|
|
385
413
|
ModificationConstructor : mc,
|
|
386
414
|
ModificatorType,
|
|
387
415
|
config,
|
|
388
|
-
proto
|
|
389
|
-
|
|
416
|
+
proto,
|
|
417
|
+
lazyFailed : lazyFailure === undefined ? false : lazyFailure.failed,
|
|
418
|
+
lazyError : lazyFailure === undefined ? undefined : lazyFailure.error
|
|
419
|
+
}
|
|
390
420
|
);
|
|
391
421
|
|
|
392
422
|
const title = `\n<-- creation of [ ${TypeName} ] traced -->`;
|
|
@@ -495,9 +525,31 @@ const instanceCreatorImpl = function (
|
|
|
495
525
|
// Phase 2: Circuit breaker — if parent is Error and blockErrors, abort early
|
|
496
526
|
runBlockErrorsCheck(
|
|
497
527
|
self,
|
|
498
|
-
args
|
|
528
|
+
args
|
|
499
529
|
);
|
|
500
530
|
|
|
531
|
+
// Phase 2.5: lazy gather failure — a lazy type whose callback threw
|
|
532
|
+
// (anything at all, undefined included), returned a non-function, or a
|
|
533
|
+
// rejected shape is a construction error: route it through the regular
|
|
534
|
+
// error path so creationError hooks fire with the existent instance and
|
|
535
|
+
// the construction args, with preCreation never running. It must sit
|
|
536
|
+
// AFTER runBlockErrorsCheck: throwModificationError splices the original
|
|
537
|
+
// error into the chain that contains the existent instance, so a later
|
|
538
|
+
// circuit-breaker check would see "parent is an Error" and mis-fire.
|
|
539
|
+
// Reads the runSetup snapshot, never the cell. If a creationError hook
|
|
540
|
+
// suppresses the throw, mirror the body-failure path: the pipeline
|
|
541
|
+
// continues from the errored instance exactly as if the build had
|
|
542
|
+
// failed and been suppressed there.
|
|
543
|
+
if ( self.lazyFailed === true ) {
|
|
544
|
+
// the same passthrough idiom as runBuild's catch: whatever the
|
|
545
|
+
// callback threw reaches the error path (a thrown non-Error breaks
|
|
546
|
+
// the prototype splice there exactly as it does for a constructor
|
|
547
|
+
// body)
|
|
548
|
+
self.throwModificationError( self.lazyError as MnemonicaError );
|
|
549
|
+
self.postProcessing( type );
|
|
550
|
+
return self.inheritedInstance;
|
|
551
|
+
}
|
|
552
|
+
|
|
501
553
|
// Phase 3: Pre-hooks — invoke preCreation hooks on collection and type
|
|
502
554
|
self.invokePreHooks();
|
|
503
555
|
|
|
@@ -26,6 +26,8 @@ const {
|
|
|
26
26
|
|
|
27
27
|
import { InstanceCreator } from './InstanceCreator';
|
|
28
28
|
|
|
29
|
+
import { gatherLazyDefinition } from './lazyDefinition';
|
|
30
|
+
|
|
29
31
|
import {
|
|
30
32
|
_getProps, Props
|
|
31
33
|
} from './Props';
|
|
@@ -87,7 +89,13 @@ const staticProps = [
|
|
|
87
89
|
const makeSubTypeProxy = function (subtype: SubtypeEntry, inheritedInstance: unknown) {
|
|
88
90
|
|
|
89
91
|
const SubTypeProxy = function (this: unknown, ..._args: unknown[]) {
|
|
92
|
+
// the lazy callback runs exactly ONCE per construction, here at the
|
|
93
|
+
// entry — before InstanceCreator's setup reads the facades; a no-op
|
|
94
|
+
// for direct types, and skipped when the parent instance is an Error
|
|
95
|
+
// with blockErrors on (the circuit breaker then throws, as on the
|
|
96
|
+
// body path)
|
|
90
97
|
if (new.target) {
|
|
98
|
+
gatherLazyDefinition(subtype, inheritedInstance);
|
|
91
99
|
const instanceResult = new InstanceCreator(
|
|
92
100
|
subtype as TypeDef,
|
|
93
101
|
inheritedInstance as object,
|
|
@@ -97,6 +105,7 @@ const makeSubTypeProxy = function (subtype: SubtypeEntry, inheritedInstance: unk
|
|
|
97
105
|
}
|
|
98
106
|
const thisArg = this === undefined ? inheritedInstance : this;
|
|
99
107
|
const existentInstance = reflectPrimitiveWrappers(thisArg);
|
|
108
|
+
gatherLazyDefinition(subtype, existentInstance);
|
|
100
109
|
const instance = new InstanceCreator(
|
|
101
110
|
subtype as TypeDef,
|
|
102
111
|
existentInstance as object,
|
|
@@ -9,6 +9,15 @@ const { checkProto, } = TypesUtils;
|
|
|
9
9
|
|
|
10
10
|
import { hop } from '../../utils/hop';
|
|
11
11
|
|
|
12
|
+
import { constants } from '../../constants';
|
|
13
|
+
const { odp } = constants;
|
|
14
|
+
|
|
15
|
+
import {
|
|
16
|
+
readLazyDefinition,
|
|
17
|
+
gatherLazyDefinition,
|
|
18
|
+
setLazyDefinition
|
|
19
|
+
} from './lazyDefinition';
|
|
20
|
+
|
|
12
21
|
import { ErrorsTypes } from '../../descriptors/errors';
|
|
13
22
|
const { WRONG_TYPE_DEFINITION, } = ErrorsTypes;
|
|
14
23
|
|
|
@@ -50,14 +59,49 @@ export const TypeProxy = function (__type__: TypeProxyType, ancestor: unknown) {
|
|
|
50
59
|
Object.assign(
|
|
51
60
|
this,
|
|
52
61
|
{
|
|
53
|
-
__type__
|
|
54
|
-
ancestor
|
|
62
|
+
__type__
|
|
55
63
|
}
|
|
56
64
|
);
|
|
65
|
+
// for a lazy ROOT type the ancestor is not fixed at define time: it is
|
|
66
|
+
// the current construction's gathered prototype, so it is a facade over
|
|
67
|
+
// the lazy cell. An explicit ancestor (the .call/.apply/.bind path)
|
|
68
|
+
// keeps winning as a plain value, exactly as for direct types
|
|
69
|
+
const lazy = readLazyDefinition(__type__);
|
|
70
|
+
if (lazy !== undefined && ancestor === undefined && !__type__.isSubType) {
|
|
71
|
+
const { cell } = lazy;
|
|
72
|
+
odp(
|
|
73
|
+
this,
|
|
74
|
+
'ancestor',
|
|
75
|
+
{
|
|
76
|
+
get () {
|
|
77
|
+
const result = cell.proto;
|
|
78
|
+
return result;
|
|
79
|
+
},
|
|
80
|
+
enumerable : true,
|
|
81
|
+
configurable : true
|
|
82
|
+
}
|
|
83
|
+
);
|
|
84
|
+
} else {
|
|
85
|
+
Object.assign(
|
|
86
|
+
this,
|
|
87
|
+
{
|
|
88
|
+
ancestor
|
|
89
|
+
}
|
|
90
|
+
);
|
|
91
|
+
}
|
|
57
92
|
const typeProxy = new Proxy(
|
|
58
93
|
InstanceCreator,
|
|
59
94
|
this
|
|
60
95
|
);
|
|
96
|
+
if (lazy !== undefined) {
|
|
97
|
+
// the subtype construction entries (makeSubTypeProxy, addThen) hold
|
|
98
|
+
// this proxy, not the descriptor — register it against the same
|
|
99
|
+
// definition so both keys resolve the one cell
|
|
100
|
+
setLazyDefinition(
|
|
101
|
+
typeProxy,
|
|
102
|
+
lazy
|
|
103
|
+
);
|
|
104
|
+
}
|
|
61
105
|
return typeProxy;
|
|
62
106
|
} as _Internal_TC_<TypeProxyInstance>;
|
|
63
107
|
|
|
@@ -199,17 +243,29 @@ TypeProxy.prototype.construct = function (this: TypeProxyInstance, _target: unkn
|
|
|
199
243
|
|
|
200
244
|
// new.target id equal with target here
|
|
201
245
|
|
|
202
|
-
const {
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
} = this;
|
|
246
|
+
const { __type__: type } = this;
|
|
247
|
+
|
|
248
|
+
const { ancestor } = this;
|
|
206
249
|
|
|
207
250
|
// so this is a direct Sub-Type invocation
|
|
208
251
|
// having no existentInstance created earlier
|
|
209
252
|
// then we should rely on that somehow
|
|
210
253
|
const parentProto = type.isSubType ? getDefaultPrototype() : ancestor;
|
|
211
254
|
|
|
212
|
-
|
|
255
|
+
// the lazy callback runs exactly ONCE per construction, here at the
|
|
256
|
+
// entry — unless the construction is about to be blocked (an errored
|
|
257
|
+
// parent with blockErrors on): the body path never runs user code for
|
|
258
|
+
// a blocked construction, so the callback must not run either. The
|
|
259
|
+
// pre-gather parentProto is enough for that check: on this path the
|
|
260
|
+
// facade still serves the define-time prototype (never an Error); only
|
|
261
|
+
// an explicit apply-path ancestor can be one
|
|
262
|
+
gatherLazyDefinition(type, parentProto);
|
|
263
|
+
|
|
264
|
+
// re-read after the gather: for a lazy root the ancestor facade now
|
|
265
|
+
// serves THIS construction's gathered prototype
|
|
266
|
+
const gatheredParentProto = type.isSubType ? getDefaultPrototype() : this.ancestor;
|
|
267
|
+
|
|
268
|
+
const mnemosyneProxy = createMnemosyne(gatheredParentProto);
|
|
213
269
|
const instance = new InstanceCreator(
|
|
214
270
|
type,
|
|
215
271
|
mnemosyneProxy,
|
|
@@ -50,6 +50,16 @@ export interface CreationHandler extends CallableFunction {
|
|
|
50
50
|
(this: object, answer: unknown): unknown;
|
|
51
51
|
}
|
|
52
52
|
|
|
53
|
+
// what a typeof === 'function' check proves — callability and nothing
|
|
54
|
+
// more; the SHAPE checks in classifyConstructHandler below are what make
|
|
55
|
+
// a candidate a ConstructHandler
|
|
56
|
+
export interface CallableCandidate extends CallableFunction {}
|
|
57
|
+
|
|
58
|
+
export const isCallableCandidate = ( value: unknown ): value is CallableCandidate => {
|
|
59
|
+
const result = typeof value === 'function';
|
|
60
|
+
return result;
|
|
61
|
+
};
|
|
62
|
+
|
|
53
63
|
// Classify the construct handler ONCE at define time from two cheap
|
|
54
64
|
// facts only — constructor.name and own prototype — NO toString()
|
|
55
65
|
// (overhead, fragile under transpilers). Async forms are NOT
|
|
@@ -59,10 +69,14 @@ export interface CreationHandler extends CallableFunction {
|
|
|
59
69
|
// function — the three are indistinguishable here and all are
|
|
60
70
|
// rejected with the readable error (the docs: write constructors as
|
|
61
71
|
// regular functions or classes).
|
|
62
|
-
|
|
63
|
-
|
|
72
|
+
// a function declaration: assertion signatures require one (TS2775)
|
|
73
|
+
export function classifyConstructHandler (
|
|
74
|
+
FunctionName: string,
|
|
75
|
+
handler: CallableCandidate
|
|
76
|
+
): asserts handler is ConstructHandler {
|
|
77
|
+
const handlerKind = handler.constructor.name;
|
|
64
78
|
const hasOwnPrototype = hop(
|
|
65
|
-
|
|
79
|
+
handler,
|
|
66
80
|
'prototype'
|
|
67
81
|
);
|
|
68
82
|
|
|
@@ -79,7 +93,7 @@ export const classifyConstructHandler = ( FunctionName: string, ConstructHandler
|
|
|
79
93
|
'(arrow functions, methods and bound functions are not supported)';
|
|
80
94
|
throw new WRONG_MODIFICATION_PATTERN( msg );
|
|
81
95
|
}
|
|
82
|
-
}
|
|
96
|
+
}
|
|
83
97
|
|
|
84
98
|
const getClassConstructor = (
|
|
85
99
|
ConstructHandler: ClassConstructHandler,
|