mnemonica 1.4.8 → 1.4.9
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 +21 -20
- package/README.md +9 -8
- package/build/api/types/compileNewModificatorFunctionBody.d.ts +4 -2
- package/build/api/types/compileNewModificatorFunctionBody.js +16 -7
- package/build/api/types/index.js +3 -3
- package/build/api/utils/index.d.ts +1 -2
- package/build/api/utils/index.js +37 -13
- package/build/index.d.ts +4 -10
- package/build/index.js +11 -29
- package/build/types/index.d.ts +199 -209
- package/build/types/index.js +1 -1
- package/docs/UTILS.md +3 -5
- package/docs/async-constructors.md +14 -11
- package/docs/hott-correspondence.md +1 -1
- package/docs/typed-lookup.md +27 -33
- package/package.json +2 -2
- package/src/api/types/compileNewModificatorFunctionBody.ts +79 -18
- package/src/api/types/index.ts +24 -25
- package/src/api/utils/index.ts +5 -51
- package/src/index.ts +40 -135
- package/src/types/index.ts +482 -489
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mnemonica",
|
|
3
|
-
"version": "1.4.
|
|
3
|
+
"version": "1.4.9",
|
|
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",
|
|
@@ -62,7 +62,7 @@
|
|
|
62
62
|
"lint:fix": "npm run lint:src && npm run lint:test",
|
|
63
63
|
"pretest": "npm run decorate:ts",
|
|
64
64
|
"test": "mocha --allow-uncaught --enable-source-maps test/index.js",
|
|
65
|
-
"test:cov": "npm run build:all && nyc --reporter=text-summary --reporter=lcov mocha --allow-uncaught --enable-source-maps --ui bdd --reporter spec ./test/index.js",
|
|
65
|
+
"test:cov": "npm run build:all && nyc --check-coverage --statements 100 --branches 100 --functions 100 --lines 100 --reporter=text-summary --reporter=lcov mocha --allow-uncaught --enable-source-maps --ui bdd --reporter spec ./test/index.js",
|
|
66
66
|
"debug": "npx tsc --pretty --project tsconfig.json && npx mocha --allow-uncaught --enable-source-maps --reporter spec --inspect-brk test/index",
|
|
67
67
|
"test:jest": "npx jest",
|
|
68
68
|
"test:async_init": "mocha --allow-uncaught test_async/index.js",
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
import { hop } from '../../utils/hop';
|
|
4
4
|
import { ErrorsTypes } from '../../descriptors/errors';
|
|
5
|
+
import type { MnemonicaConstructor } from '../../types';
|
|
5
6
|
|
|
6
7
|
const { WRONG_MODIFICATION_PATTERN, } = ErrorsTypes;
|
|
7
8
|
/*
|
|
@@ -95,6 +96,58 @@ export function classifyConstructHandler (
|
|
|
95
96
|
}
|
|
96
97
|
}
|
|
97
98
|
|
|
99
|
+
// const isClass = ( functionPointer: CallableFunction ) => {
|
|
100
|
+
// const value = Function.prototype.toString.call( functionPointer );
|
|
101
|
+
// return /^\s*class\s+/.test( value.trim() );
|
|
102
|
+
// };
|
|
103
|
+
|
|
104
|
+
// accordingly to the gist from here:
|
|
105
|
+
// https://gist.github.com/wentout/ea3afe9c822a6b6ef32f9e4f3e98b1ba
|
|
106
|
+
// a type predicate: the non-writable own prototype it checks is what
|
|
107
|
+
// makes the handler a class, so the class view (construct signature)
|
|
108
|
+
// of that same function value is named by this check, not by a cast
|
|
109
|
+
export const isClass = (fn: ConstructHandler): fn is ConstructHandler & ClassConstructHandler => {
|
|
110
|
+
|
|
111
|
+
/*
|
|
112
|
+
const str = String(fn);
|
|
113
|
+
const result = str.indexOf('class ') === 0;
|
|
114
|
+
return result;
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
// not necessary to check fn for typeof
|
|
118
|
+
// because of other checks made before
|
|
119
|
+
// if (typeof fn !== 'function') {
|
|
120
|
+
// return false;
|
|
121
|
+
// }
|
|
122
|
+
|
|
123
|
+
// seemingly
|
|
124
|
+
// class prototype is always object
|
|
125
|
+
if (!(fn.prototype instanceof Object)) {
|
|
126
|
+
return false;
|
|
127
|
+
}
|
|
128
|
+
// but only if not augmented
|
|
129
|
+
|
|
130
|
+
// class prototype.constructor cannot be re-defined (usually)
|
|
131
|
+
// but for functions user may re-assign it easily
|
|
132
|
+
// just assigning the way .prototype = {}
|
|
133
|
+
// and they 99.9% do this, cause nobody cares
|
|
134
|
+
// but we are checking if this is class, so it works ...
|
|
135
|
+
if (fn.prototype.constructor !== fn) {
|
|
136
|
+
return false;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// and the most unknown thing is that,
|
|
140
|
+
// cause for functions it it writeable )))
|
|
141
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
142
|
+
return Reflect.getOwnPropertyDescriptor(fn, 'prototype')!.writable === false;
|
|
143
|
+
|
|
144
|
+
*/
|
|
145
|
+
|
|
146
|
+
const protoDesc = Reflect.getOwnPropertyDescriptor(fn, 'prototype');
|
|
147
|
+
const result = protoDesc !== undefined && protoDesc.writable === false;
|
|
148
|
+
return result;
|
|
149
|
+
};
|
|
150
|
+
|
|
98
151
|
const getClassConstructor = (
|
|
99
152
|
ConstructHandler: ClassConstructHandler,
|
|
100
153
|
CreationHandler: CreationHandler,
|
|
@@ -112,7 +165,10 @@ const getClassConstructor = (
|
|
|
112
165
|
return castResult;
|
|
113
166
|
}
|
|
114
167
|
};
|
|
115
|
-
|
|
168
|
+
// the class as the pipeline uses it: the SymbolConstructorName mark
|
|
169
|
+
// it lacks here is attached by the compiler right after
|
|
170
|
+
const newableResult = result as MnemonicaConstructor;
|
|
171
|
+
return newableResult;
|
|
116
172
|
};
|
|
117
173
|
|
|
118
174
|
const getFunctionConstructor = (
|
|
@@ -157,46 +213,51 @@ const getFunctionConstructor = (
|
|
|
157
213
|
}
|
|
158
214
|
const result = CreationHandler.call(
|
|
159
215
|
this,
|
|
160
|
-
answer
|
|
216
|
+
answer
|
|
161
217
|
);
|
|
162
218
|
return result;
|
|
163
219
|
};
|
|
164
|
-
|
|
220
|
+
// the compiled body is invoked with `new` by the pipeline;
|
|
221
|
+
// a function expression carries no construct signature, so
|
|
222
|
+
// the `new`-able view of the same value is named here
|
|
223
|
+
const newableResult = funcResult as FunctionModificationBody;
|
|
224
|
+
return newableResult;
|
|
165
225
|
};
|
|
166
226
|
|
|
167
|
-
|
|
227
|
+
// the function-branch body: the plain function above, as the pipeline
|
|
228
|
+
// uses it — invoked with `new`, constructing an object
|
|
229
|
+
interface FunctionModificationBody extends MnemonicaConstructor {
|
|
230
|
+
(this: object, ...args: unknown[]): unknown;
|
|
231
|
+
}
|
|
168
232
|
|
|
169
|
-
|
|
233
|
+
// what both branches produce and every caller stores: only ever invoked
|
|
234
|
+
// with `new` (a class body cannot be called without it)
|
|
235
|
+
type ModificationBody = MnemonicaConstructor;
|
|
236
|
+
|
|
237
|
+
const compileNewModificatorFunctionBody = function ( FunctionName: string ) {
|
|
170
238
|
const outerResult = function (
|
|
171
239
|
ConstructHandler: ConstructHandler,
|
|
172
240
|
CreationHandler: CreationHandler,
|
|
173
241
|
SymbolConstructorName: symbol
|
|
174
242
|
): () => ModificationBody {
|
|
243
|
+
// decided once per handler, here, not per construction: the class
|
|
244
|
+
// view of the handler when isClass() proves it, undefined otherwise
|
|
245
|
+
const ClassHandler = isClass( ConstructHandler ) ? ConstructHandler : undefined;
|
|
175
246
|
const innerResult = function (): ModificationBody {
|
|
176
247
|
let ModificationBody: ModificationBody;
|
|
177
|
-
if (
|
|
178
|
-
// this branch runs only when isClass() verified at define
|
|
179
|
-
// time that the handler is a class — the cast names the class
|
|
180
|
-
// view (construct signature) of that same function value
|
|
181
|
-
// ConstructHandler has no construct signature; the boolean cannot
|
|
182
|
-
// narrow it without a runtime guard or a changed public signature.
|
|
248
|
+
if ( ClassHandler !== undefined ) {
|
|
183
249
|
ModificationBody = getClassConstructor(
|
|
184
|
-
|
|
250
|
+
ClassHandler,
|
|
185
251
|
CreationHandler
|
|
186
252
|
);
|
|
187
253
|
} else {
|
|
188
254
|
// const ReNamedConstructHandler = {} as unknown;
|
|
189
255
|
// ReNamedConstructHandler[FunctionName] = ConstructHandler;
|
|
190
256
|
// ModificationBody = getFunctionConstructor(ReNamedConstructHandler[FunctionName], CreationHandler);
|
|
191
|
-
// the compiled body is invoked with `new` by the pipeline;
|
|
192
|
-
// a function expression carries no construct signature, so
|
|
193
|
-
// the `new`-able view of the same value is named here
|
|
194
|
-
// CreationHandler returns unknown; ModificationBody must construct
|
|
195
|
-
// object, so a direct return annotation cannot replace this cast.
|
|
196
257
|
ModificationBody = getFunctionConstructor(
|
|
197
258
|
ConstructHandler,
|
|
198
259
|
CreationHandler
|
|
199
|
-
)
|
|
260
|
+
);
|
|
200
261
|
}
|
|
201
262
|
ModificationBody.prototype.constructor = ModificationBody;
|
|
202
263
|
Object.defineProperty(
|
package/src/api/types/index.ts
CHANGED
|
@@ -22,7 +22,6 @@
|
|
|
22
22
|
*/
|
|
23
23
|
|
|
24
24
|
import {
|
|
25
|
-
_Internal_TC_,
|
|
26
25
|
TypeDescriptorInstance,
|
|
27
26
|
TypeClass,
|
|
28
27
|
TypeDef,
|
|
@@ -197,10 +196,7 @@ const installLazyDefinition = function (
|
|
|
197
196
|
return shimResult;
|
|
198
197
|
}
|
|
199
198
|
const { handler } = cell;
|
|
200
|
-
const modificatorBody = compileNewModificatorFunctionBody(
|
|
201
|
-
TypeName,
|
|
202
|
-
cell.asClass
|
|
203
|
-
);
|
|
199
|
+
const modificatorBody = compileNewModificatorFunctionBody(TypeName);
|
|
204
200
|
const wrappedBody = modificatorBody(
|
|
205
201
|
handler,
|
|
206
202
|
CreationHandler,
|
|
@@ -375,13 +371,22 @@ const typeDescriptorCreator = function (
|
|
|
375
371
|
|
|
376
372
|
};
|
|
377
373
|
|
|
378
|
-
// the creator is
|
|
379
|
-
// below)
|
|
380
|
-
//
|
|
381
|
-
// prototype
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
374
|
+
// the creator, as it is used: invoked with `new` (the createFrom* call
|
|
375
|
+
// sites below), it returns the TypeProxy it registers in the subtypes map
|
|
376
|
+
// — a Proxy materializing the TypeClass surface dynamically — while its
|
|
377
|
+
// prototype is the descriptor's TypeDescriptorInstance contract
|
|
378
|
+
type TypeDescriptorArgs = Parameters<typeof typeDescriptorCreator>;
|
|
379
|
+
interface TypeDescriptorConstructor extends NewableFunction {
|
|
380
|
+
new (...args: TypeDescriptorArgs): TypeClass;
|
|
381
|
+
(this: TypeDescriptorInstance, ...args: TypeDescriptorArgs): ReturnType<typeof typeDescriptorCreator>;
|
|
382
|
+
readonly prototype: TypeDescriptorInstance;
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
// the creator is only ever invoked with `new`; a function expression
|
|
386
|
+
// carries no construct signature, so it is named once as the constructor
|
|
387
|
+
// interface it is used through — the prototype contract is attached
|
|
388
|
+
// immediately below
|
|
389
|
+
const TypeDescriptor = typeDescriptorCreator as TypeDescriptorConstructor;
|
|
385
390
|
|
|
386
391
|
Object.assign(
|
|
387
392
|
TypeDescriptor.prototype,
|
|
@@ -550,8 +555,10 @@ const createFromDirectHandler = function (
|
|
|
550
555
|
defineOrigin: TypeAbsorber,
|
|
551
556
|
target: TypesMap,
|
|
552
557
|
name: string,
|
|
553
|
-
handler
|
|
554
|
-
|
|
558
|
+
handler: ConstructHandler | undefined,
|
|
559
|
+
// always an object here: the define() dispatcher replaces a missing
|
|
560
|
+
// config with {}, the lazy path passes its own
|
|
561
|
+
config: constructorOptions,
|
|
555
562
|
lazyGetter?: LazyDef<object>
|
|
556
563
|
) {
|
|
557
564
|
// for a lazy type the handler stays the empty shim: the real constructor
|
|
@@ -559,10 +566,7 @@ const createFromDirectHandler = function (
|
|
|
559
566
|
// installLazyDefinition above and lazyDefinition.ts)
|
|
560
567
|
handler = handler || function () { };
|
|
561
568
|
const asClass = isClass(handler);
|
|
562
|
-
const modificatorBody = compileNewModificatorFunctionBody(
|
|
563
|
-
name,
|
|
564
|
-
asClass
|
|
565
|
-
);
|
|
569
|
+
const modificatorBody = compileNewModificatorFunctionBody(name);
|
|
566
570
|
|
|
567
571
|
const makeConstructHandler = modificatorBody(
|
|
568
572
|
handler as ConstructHandler,
|
|
@@ -576,7 +580,7 @@ const createFromDirectHandler = function (
|
|
|
576
580
|
};
|
|
577
581
|
}
|
|
578
582
|
|
|
579
|
-
config
|
|
583
|
+
config.asClass = asClass;
|
|
580
584
|
|
|
581
585
|
const proto = (
|
|
582
586
|
hop(
|
|
@@ -606,12 +610,7 @@ const createFromDirectHandler = function (
|
|
|
606
610
|
proto,
|
|
607
611
|
config,
|
|
608
612
|
lazyGetter
|
|
609
|
-
|
|
610
|
-
// subtypes map — a Proxy materializing the TypeClass surface
|
|
611
|
-
// dynamically — so the public constructor contract is named here
|
|
612
|
-
// The declared descriptor result and dynamic proxy surface differ;
|
|
613
|
-
// changing the producing signature breaks descriptor prototype methods.
|
|
614
|
-
) as unknown as TypeClass;
|
|
613
|
+
);
|
|
615
614
|
return result;
|
|
616
615
|
};
|
|
617
616
|
|
package/src/api/utils/index.ts
CHANGED
|
@@ -22,7 +22,7 @@ import type {
|
|
|
22
22
|
TypeClass
|
|
23
23
|
} from '../../types';
|
|
24
24
|
|
|
25
|
-
import compileNewModificatorFunctionBody, {
|
|
25
|
+
import compileNewModificatorFunctionBody, { isClass } from '../types/compileNewModificatorFunctionBody';
|
|
26
26
|
|
|
27
27
|
// CreationHandler - handles constructor return values
|
|
28
28
|
// Moved to api/types/index.ts as per refactoring plan
|
|
@@ -213,54 +213,9 @@ const findSubTypeFromParent = (instance: parentSub | object | undefined, subType
|
|
|
213
213
|
return subtype;
|
|
214
214
|
};
|
|
215
215
|
|
|
216
|
-
//
|
|
217
|
-
//
|
|
218
|
-
//
|
|
219
|
-
// };
|
|
220
|
-
|
|
221
|
-
// accordingly to the gist from here:
|
|
222
|
-
// https://gist.github.com/wentout/ea3afe9c822a6b6ef32f9e4f3e98b1ba
|
|
223
|
-
const isClass = (fn: ConstructHandler) => {
|
|
224
|
-
|
|
225
|
-
/*
|
|
226
|
-
const str = String(fn);
|
|
227
|
-
const result = str.indexOf('class ') === 0;
|
|
228
|
-
return result;
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
// not necessary to check fn for typeof
|
|
232
|
-
// because of other checks made before
|
|
233
|
-
// if (typeof fn !== 'function') {
|
|
234
|
-
// return false;
|
|
235
|
-
// }
|
|
236
|
-
|
|
237
|
-
// seemingly
|
|
238
|
-
// class prototype is always object
|
|
239
|
-
if (!(fn.prototype instanceof Object)) {
|
|
240
|
-
return false;
|
|
241
|
-
}
|
|
242
|
-
// but only if not augmented
|
|
243
|
-
|
|
244
|
-
// class prototype.constructor cannot be re-defined (usually)
|
|
245
|
-
// but for functions user may re-assign it easily
|
|
246
|
-
// just assigning the way .prototype = {}
|
|
247
|
-
// and they 99.9% do this, cause nobody cares
|
|
248
|
-
// but we are checking if this is class, so it works ...
|
|
249
|
-
if (fn.prototype.constructor !== fn) {
|
|
250
|
-
return false;
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
// and the most unknown thing is that,
|
|
254
|
-
// cause for functions it it writeable )))
|
|
255
|
-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
256
|
-
return Reflect.getOwnPropertyDescriptor(fn, 'prototype')!.writable === false;
|
|
257
|
-
|
|
258
|
-
*/
|
|
259
|
-
|
|
260
|
-
const protoDesc = Reflect.getOwnPropertyDescriptor(fn, 'prototype');
|
|
261
|
-
const result = protoDesc !== undefined && protoDesc.writable === false;
|
|
262
|
-
return result;
|
|
263
|
-
};
|
|
216
|
+
// isClass lives beside the construct-handler shapes it narrows, in
|
|
217
|
+
// compileNewModificatorFunctionBody.ts (the compiler picks the class
|
|
218
|
+
// branch with it); re-exported here as part of TypesUtils
|
|
264
219
|
|
|
265
220
|
const makeErrorModificatorType = (
|
|
266
221
|
TypeName: string,
|
|
@@ -277,8 +232,7 @@ const makeErrorModificatorType = (
|
|
|
277
232
|
);
|
|
278
233
|
|
|
279
234
|
const result = modificatorType();
|
|
280
|
-
|
|
281
|
-
return constructorResult;
|
|
235
|
+
return result;
|
|
282
236
|
|
|
283
237
|
};
|
|
284
238
|
|
package/src/index.ts
CHANGED
|
@@ -18,13 +18,8 @@ import type {
|
|
|
18
18
|
MnemonicaModule,
|
|
19
19
|
InstanceResult,
|
|
20
20
|
Merge,
|
|
21
|
-
TypeLookup,
|
|
22
|
-
RegistryHolderBase,
|
|
23
|
-
RegistryEntry,
|
|
24
21
|
GlobalRegistry,
|
|
25
|
-
LookedUpConstructor,
|
|
26
22
|
FreeLookupResult,
|
|
27
|
-
WithHookRegister,
|
|
28
23
|
typedHook,
|
|
29
24
|
HookableConstructor,
|
|
30
25
|
DefineNewableOrCallable,
|
|
@@ -148,30 +143,6 @@ function checkThis(pointer: typeof mnemonica | typeof exports | unknown): boolea
|
|
|
148
143
|
|
|
149
144
|
|
|
150
145
|
// Define function using TypeAbsorber interface with proper type casting
|
|
151
|
-
export function define <
|
|
152
|
-
Reg extends object,
|
|
153
|
-
Parent extends object,
|
|
154
|
-
Path extends string,
|
|
155
|
-
const Name extends string,
|
|
156
|
-
N extends object,
|
|
157
|
-
Args extends unknown[] = unknown[],
|
|
158
|
-
F extends Proto<Parent, N> = Proto<Parent, N>,
|
|
159
|
-
ChildPath extends string = Path extends '' ? Name : `${Path}.${Name}`
|
|
160
|
-
>(
|
|
161
|
-
// explicit-source form: define(source, name, handler, config?)
|
|
162
|
-
// mirrors RegistryHolderBase.define; semantics follow the source object
|
|
163
|
-
source: RegistryHolderBase<Reg, Parent, Path>,
|
|
164
|
-
TypeName: Name,
|
|
165
|
-
constructHandler?: IDEF<N, Args>,
|
|
166
|
-
config?: constructorOptions
|
|
167
|
-
): IDefinitorInstance<
|
|
168
|
-
F,
|
|
169
|
-
InstanceResult<F>,
|
|
170
|
-
Reg & Record<ChildPath, RegistryEntry<F, ChildPath, Parent, Args>>,
|
|
171
|
-
ChildPath,
|
|
172
|
-
Parent,
|
|
173
|
-
Args
|
|
174
|
-
>;
|
|
175
146
|
export function define <
|
|
176
147
|
T extends object,
|
|
177
148
|
Args extends unknown[] = unknown[]
|
|
@@ -208,56 +179,28 @@ export function define <
|
|
|
208
179
|
constructHandler?: IDEF<T> | DefineNewableOrCallable | object | boolean,
|
|
209
180
|
config?: constructorOptions,
|
|
210
181
|
): R;
|
|
182
|
+
// `this` is the collection: defaultTypes for a plain call, the given one for
|
|
183
|
+
// define.call(collection, …); a single function or class names the type
|
|
184
|
+
// itself (TypeName from its own name)
|
|
211
185
|
export function define <T extends object>(
|
|
212
186
|
this: unknown,
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
configOrHandler?: constructorOptions | IDEF<T> | DefineNewableOrCallable | object | boolean,
|
|
187
|
+
TypeName?: string | DefineNewableOrCallable,
|
|
188
|
+
constructHandler?: IDEF<T> | DefineNewableOrCallable | object | boolean,
|
|
216
189
|
config?: constructorOptions,
|
|
217
190
|
): IDefinitorInstance<T> {
|
|
218
|
-
|
|
219
|
-
// explicit-source form: define(source, name, handler, config?)
|
|
220
|
-
// the source may be a TypesCollection (object) or a TypeProxy (function),
|
|
221
|
-
// so detection is: has a callable .define AND the next arg is a string name
|
|
222
|
-
const sourceCandidate = TypeNameOrSource as { define?: unknown };
|
|
223
|
-
const mayBeSource = (
|
|
224
|
-
(typeof TypeNameOrSource === 'object' && TypeNameOrSource !== null) ||
|
|
225
|
-
typeof TypeNameOrSource === 'function'
|
|
226
|
-
) && typeof sourceCandidate.define === 'function';
|
|
227
|
-
if (mayBeSource && typeof constructHandlerOrName === 'string') {
|
|
228
|
-
// .define was verified callable above; the cast only adds its signature
|
|
229
|
-
const source = sourceCandidate as { define: TypeAbsorber };
|
|
230
|
-
// The implementation returns the delegated constructor directly;
|
|
231
|
-
// caller-specific R views live only on the overloads.
|
|
232
|
-
const sourceDefineResult = source.define(
|
|
233
|
-
constructHandlerOrName,
|
|
234
|
-
configOrHandler as IDEF<T>,
|
|
235
|
-
config
|
|
236
|
-
);
|
|
237
|
-
return sourceDefineResult;
|
|
238
|
-
}
|
|
239
|
-
|
|
240
191
|
const types = checkThis(this) ? defaultTypes : this || defaultTypes;
|
|
241
192
|
// Type assertion needed because TypesCollectionProxy is a Proxy
|
|
242
193
|
// The implementation returns the delegated constructor directly.
|
|
243
194
|
const defineResult = (types as { define: TypeAbsorber })
|
|
244
195
|
.define(
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
196
|
+
TypeName as string,
|
|
197
|
+
constructHandler as IDEF<T>,
|
|
198
|
+
config
|
|
248
199
|
);
|
|
249
200
|
return defineResult;
|
|
250
201
|
}
|
|
251
202
|
|
|
252
203
|
export function lazy<T extends object, Args extends unknown[] = unknown[]>(
|
|
253
|
-
// explicit-source form: lazy(source, TypeName, getter, config?)
|
|
254
|
-
source: RegistryHolderBase<object, object, string>,
|
|
255
|
-
TypeName: string,
|
|
256
|
-
getter: LazyDef<T, Args>,
|
|
257
|
-
config?: constructorOptions
|
|
258
|
-
): IDefinitorInstance<T, InstanceResult<T>, GlobalRegistry, '', object, Args>;
|
|
259
|
-
export function lazy<T extends object, Args extends unknown[] = unknown[]>(
|
|
260
|
-
// named form: lazy(TypeName, getter, config?)
|
|
261
204
|
this: unknown,
|
|
262
205
|
TypeName: string,
|
|
263
206
|
getter: LazyDef<T, Args>,
|
|
@@ -265,36 +208,17 @@ export function lazy<T extends object, Args extends unknown[] = unknown[]>(
|
|
|
265
208
|
): IDefinitorInstance<T, InstanceResult<T>, GlobalRegistry, '', object, Args>;
|
|
266
209
|
export function lazy(
|
|
267
210
|
this: unknown,
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
config?: unknown
|
|
211
|
+
TypeName: string,
|
|
212
|
+
getter: unknown,
|
|
213
|
+
config?: constructorOptions
|
|
272
214
|
): unknown {
|
|
273
|
-
|
|
274
|
-
// explicit-source form: lazy(source, TypeName, getter, config?)
|
|
275
|
-
// the source may be a TypesCollection (object) or a TypeProxy (function),
|
|
276
|
-
// so detection is: has a callable .lazy
|
|
277
|
-
const mayBeSource = (
|
|
278
|
-
(typeof sourceOrTypeName === 'object' && sourceOrTypeName !== null) ||
|
|
279
|
-
typeof sourceOrTypeName === 'function'
|
|
280
|
-
) && typeof (sourceOrTypeName as { lazy?: unknown }).lazy === 'function';
|
|
281
|
-
if (mayBeSource) {
|
|
282
|
-
const source = sourceOrTypeName as { lazy: LazyAbsorber };
|
|
283
|
-
const sourceLazyResult = source.lazy(
|
|
284
|
-
TypeNameOrGetter as string,
|
|
285
|
-
getterOrConfig as LazyDef<object>,
|
|
286
|
-
config as constructorOptions
|
|
287
|
-
) as unknown;
|
|
288
|
-
return sourceLazyResult;
|
|
289
|
-
}
|
|
290
|
-
|
|
291
215
|
const types = checkThis(this) ? defaultTypes : this || defaultTypes;
|
|
292
216
|
// Type assertion needed because TypesCollectionProxy is a Proxy
|
|
293
217
|
const lazyResult = (types as { lazy: LazyAbsorber })
|
|
294
218
|
.lazy(
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
219
|
+
TypeName,
|
|
220
|
+
getter as LazyDef<object>,
|
|
221
|
+
config
|
|
298
222
|
) as unknown;
|
|
299
223
|
return lazyResult;
|
|
300
224
|
}
|
|
@@ -303,66 +227,48 @@ export function lookup<const K extends keyof TypeRegistry>(
|
|
|
303
227
|
this: unknown,
|
|
304
228
|
TypeNestedPath: K
|
|
305
229
|
): FreeLookupResult<K>;
|
|
306
|
-
export function lookup<Reg extends object, const K extends keyof Reg & string>(
|
|
307
|
-
// explicit-source form: lookup(source, path)
|
|
308
|
-
// resolves against the registry carried by a builder/collection value
|
|
309
|
-
source: { lookup: TypeLookup<Reg> },
|
|
310
|
-
TypeNestedPath: K
|
|
311
|
-
): WithHookRegister<LookedUpConstructor<Reg, K>>;
|
|
312
230
|
export function lookup(this: unknown, TypeNestedPath: string): TypeClass | undefined;
|
|
313
|
-
export function lookup(
|
|
314
|
-
source: LookupSource,
|
|
315
|
-
TypeNestedPath: string
|
|
316
|
-
): TypeClass | undefined;
|
|
317
231
|
export function lookup(
|
|
318
232
|
this: unknown,
|
|
319
|
-
|
|
320
|
-
TypeNestedPath?: unknown
|
|
233
|
+
TypeNestedPath: string
|
|
321
234
|
): unknown {
|
|
322
|
-
|
|
323
|
-
// explicit-source form: lookup(source, path)
|
|
324
|
-
if (typeof sourceOrPath !== 'string' && typeof TypeNestedPath === 'string') {
|
|
325
|
-
const source = sourceOrPath as LookupSource;
|
|
326
|
-
const sourceResult = source.lookup(TypeNestedPath);
|
|
327
|
-
return sourceResult;
|
|
328
|
-
}
|
|
329
|
-
|
|
330
235
|
const types = checkThis(this) ? defaultTypes : this || defaultTypes;
|
|
331
236
|
// Type assertion needed because TypesCollectionProxy is a Proxy
|
|
332
|
-
const lookupResult = (types as LookupSource).lookup(
|
|
237
|
+
const lookupResult = (types as LookupSource).lookup(TypeNestedPath);
|
|
333
238
|
return lookupResult;
|
|
334
239
|
}
|
|
335
240
|
|
|
336
241
|
|
|
337
|
-
|
|
242
|
+
// returns the constructed instance as `object`: apply/call/bind name it as
|
|
243
|
+
// their public merged result type
|
|
244
|
+
const $run = function <E extends object, T extends object, Args extends unknown[]>(
|
|
338
245
|
entity: E,
|
|
339
246
|
Ctor: CtorParameter<T, Args>,
|
|
340
247
|
args: unknown[]
|
|
341
|
-
): {
|
|
342
|
-
[key in keyof S]: S[key]
|
|
343
|
-
} {
|
|
248
|
+
): object {
|
|
344
249
|
|
|
345
250
|
// debugger;
|
|
346
251
|
// CtorParameter deliberately accepts constructors without a TypeName
|
|
347
|
-
// declaration; runtime mnemonica constructors carry it.
|
|
348
|
-
//
|
|
349
|
-
//
|
|
350
|
-
const
|
|
252
|
+
// declaration; runtime mnemonica constructors carry it. A constructor
|
|
253
|
+
// without one (a plain class or function) resolves no subtype and
|
|
254
|
+
// throws the same error below as an unknown TypeName
|
|
255
|
+
const TypeName: unknown = Reflect.get(
|
|
256
|
+
Ctor,
|
|
257
|
+
'TypeName'
|
|
258
|
+
);
|
|
351
259
|
// the returned SubTypeProxy is a plain function at runtime — constructable —
|
|
352
260
|
// so TypeConstructorBase (the named minimal constructor shape) is its
|
|
353
261
|
// honest static type here
|
|
354
|
-
const Cstr =
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
262
|
+
const Cstr = typeof TypeName === 'string' ?
|
|
263
|
+
prepareSubtypeForConstruction(
|
|
264
|
+
TypeName,
|
|
265
|
+
entity
|
|
266
|
+
) as TypeConstructorBase :
|
|
267
|
+
undefined;
|
|
358
268
|
if (Cstr === undefined) {
|
|
359
269
|
throw new WRONG_MODIFICATION_PATTERN(`[ ${TypeName} ] is not defined as a Type Constructor on used instance`);
|
|
360
270
|
}
|
|
361
271
|
const runResult = new Cstr(...args);
|
|
362
|
-
// new Cstr returns object here; S is the caller's merged entity/constructor
|
|
363
|
-
// shape and cannot be recovered by narrowing without changing the public
|
|
364
|
-
// result type, so the return stays suppressed.
|
|
365
|
-
// @ts-expect-error -- the merged result shape cannot be recovered by narrowing
|
|
366
272
|
return runResult;
|
|
367
273
|
};
|
|
368
274
|
|
|
@@ -370,31 +276,30 @@ const $run = function <E extends object, T extends object, S extends Proto<E, T>
|
|
|
370
276
|
export const apply: ApplyFunction = function <
|
|
371
277
|
E extends object,
|
|
372
278
|
T extends object,
|
|
373
|
-
S extends Proto<E, T>,
|
|
374
279
|
Args extends unknown[]
|
|
375
280
|
>(
|
|
376
281
|
entity: E,
|
|
377
282
|
Ctor: CtorParameter<T, Args>,
|
|
378
283
|
args: unknown[] = []
|
|
379
284
|
): InstanceResult<Merge<E, T>> {
|
|
380
|
-
const runResult = $run<E, T,
|
|
285
|
+
const runResult = $run<E, T, Args>(
|
|
381
286
|
entity,
|
|
382
287
|
Ctor,
|
|
383
288
|
args
|
|
384
289
|
);
|
|
385
|
-
//
|
|
386
|
-
// apply/call/bind promise — the named public result type
|
|
290
|
+
// the instance constructed from entity by Ctor carries the merged
|
|
291
|
+
// entity+ctor shape apply/call/bind promise — the named public result type
|
|
387
292
|
const result = runResult as InstanceResult<Merge<E, T>>;
|
|
388
293
|
return result;
|
|
389
294
|
};
|
|
390
295
|
|
|
391
296
|
// TODO: call type .by instance .with arguments
|
|
392
|
-
export const call = function <E extends object, T extends object,
|
|
297
|
+
export const call = function <E extends object, T extends object, Args extends unknown[]>(
|
|
393
298
|
entity: E,
|
|
394
299
|
Ctor: CtorParameter<T, Args>,
|
|
395
300
|
...args: NoInfer<Args>
|
|
396
301
|
): InstanceResult<Merge<E, T>> {
|
|
397
|
-
const runResult = $run<E, T,
|
|
302
|
+
const runResult = $run<E, T, Args>(
|
|
398
303
|
entity,
|
|
399
304
|
Ctor,
|
|
400
305
|
args
|
|
@@ -405,12 +310,12 @@ export const call = function <E extends object, T extends object, S extends Prot
|
|
|
405
310
|
};
|
|
406
311
|
|
|
407
312
|
// TODO: bind type .with instance → (...args)
|
|
408
|
-
export const bind = function <E extends object, T extends object,
|
|
313
|
+
export const bind = function <E extends object, T extends object, Args extends unknown[]>(
|
|
409
314
|
entity: E,
|
|
410
315
|
Ctor: CtorParameter<T, Args>
|
|
411
316
|
): (...args: Args) => InstanceResult<Merge<E, T>> {
|
|
412
317
|
const result = (...args: Args) => {
|
|
413
|
-
const runResult = $run<E, T,
|
|
318
|
+
const runResult = $run<E, T, Args>(
|
|
414
319
|
entity,
|
|
415
320
|
Ctor,
|
|
416
321
|
args
|