mnemonica 1.4.1 → 1.4.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.
@@ -24,7 +24,8 @@ import { constants } from '../../constants';
24
24
 
25
25
  const {
26
26
  odp,
27
- SymbolConstructorName
27
+ SymbolConstructorName,
28
+ MNEMONICA
28
29
  } = constants;
29
30
 
30
31
  import { ErrorsTypes } from '../../descriptors/errors';
@@ -212,28 +213,42 @@ const postProcessing = function ( this: InstanceCreatorContext, continuationOf?:
212
213
  if ( madeOnName === undefined ) {
213
214
  msg = 'should inherit from some instance';
214
215
  } else {
215
- msg = `should inherit from ${self.type.parentType!.TypeName} but made on ${inheritedConstructor.name}`;
216
+ // the sentinel is internal, so the message names the type being
217
+ // made; a real outside parent (`.call(new Number(5))`) is named
218
+ const madeOn = madeOnName === MNEMONICA ? inheritedConstructor.name : madeOnName;
219
+ msg = `should inherit from ${self.type.parentType!.TypeName} but made on ${madeOn}`;
216
220
  }
217
221
  self.throwModificationError( new WRONG_MODIFICATION_PATTERN(
218
222
  msg,
219
223
  stack
220
224
  ) );
221
- }
222
- const parentName = prev!.constructor.name;
223
- const parentTypeName = self.type.parentType!.TypeName;
224
- if (parentName !== parentTypeName) {
225
- const msg = `should inherit from ${parentTypeName} but made on ${parentName}`;
226
- self.throwModificationError( new WRONG_MODIFICATION_PATTERN(
227
- msg,
228
- stack
229
- ) );
225
+ // throwModificationError throws unless a creationError hook
226
+ // suppressed it — a suppressed failure is already recorded on
227
+ // the errored instance: skip ONLY the remaining strictChain
228
+ // checks (they would read the stale null prev); the shared tail
229
+ // (_setSelf, the marked hook guard) still runs
230
+ } else {
231
+ const parentName = prev.constructor.name;
232
+ const parentTypeName = self.type.parentType!.TypeName;
233
+ if (parentName !== parentTypeName) {
234
+ const msg = `should inherit from ${parentTypeName} but made on ${parentName}`;
235
+ self.throwModificationError( new WRONG_MODIFICATION_PATTERN(
236
+ msg,
237
+ stack
238
+ ) );
239
+ }
230
240
  }
231
241
 
232
242
  }
233
243
 
234
244
  _setSelf(self.inheritedInstance);
235
245
 
236
- self.invokePostHooks();
246
+ // when throwModificationError already fired the post hooks for this
247
+ // construction (a suppressed failure that continued the pipeline), do
248
+ // not fire them again — one creationError per failed construction
249
+ if ( !self.postHooksFired ) {
250
+ self.invokePostHooks();
251
+ }
237
252
 
238
253
  };
239
254
 
@@ -19,22 +19,22 @@ export const makeInstanceModificator = ( self: InstanceCreatorContext ): Mnemoni
19
19
  config,
20
20
  } = self;
21
21
 
22
- // Class-handler prototypes need the FULL-descriptor copy: class body
23
- // methods are non-enumerable, and an enumerable-only Object.assign copy
24
- // silently dropped them — Type.define('X', class { m() {} }) lost m on
25
- // the subtype while lazy kept it. Function-handler prototypes keep the
26
- // historical enumerable-only copy: their non-enumerable props (legacy
27
- // opt-in instance methods, exception wiring) were never copied, and
28
- // pins depend on that (exception instances expose no bound methods)
29
- const ModificatorTypePrototype = config.asClass
30
- ? Object.defineProperties(
31
- {},
32
- Object.getOwnPropertyDescriptors( proto )
33
- )
34
- : Object.assign(
35
- {},
36
- proto
37
- );
22
+ // Copied by descriptor, never by Object.assign, which invokes getters and
23
+ // freezes them to a value.
24
+ // Class-handler prototypes copy every member: class body methods are
25
+ // non-enumerable, and an enumerable-only copy silently dropped them —
26
+ // Type.define('X', class { m() {} }) lost m on the subtype while lazy
27
+ // kept it. Function-handler prototypes copy the enumerable members only:
28
+ // their non-enumerable props (legacy opt-in instance methods, exception
29
+ // wiring) were never copied, and pins depend on that (exception instances
30
+ // expose no bound methods)
31
+ const ModificatorTypePrototype: { [index: string]: unknown } = {};
32
+ Reflect.ownKeys( proto ).forEach( ( key ) => {
33
+ const descriptor = Reflect.getOwnPropertyDescriptor( proto, key );
34
+ if ( descriptor && ( config.asClass || descriptor.enumerable ) ) {
35
+ Reflect.defineProperty( ModificatorTypePrototype, key, descriptor );
36
+ }
37
+ } );
38
38
 
39
39
  const result = ModificationConstructor.call(
40
40
  existentInstance,
@@ -26,6 +26,12 @@ const {
26
26
 
27
27
  import { InstanceCreator } from './InstanceCreator';
28
28
 
29
+ import { ErrorsTypes } from '../../descriptors/errors';
30
+ const {
31
+ WRONG_INSTANCE_INVOCATION,
32
+ WRONG_MODIFICATION_PATTERN
33
+ } = ErrorsTypes;
34
+
29
35
  import { gatherLazyDefinition } from './lazyDefinition';
30
36
 
31
37
  import {
@@ -103,8 +109,22 @@ const makeSubTypeProxy = function (subtype: SubtypeEntry, inheritedInstance: unk
103
109
  );
104
110
  return instanceResult;
105
111
  }
106
- const thisArg = this === undefined ? inheritedInstance : this;
107
- const existentInstance = reflectPrimitiveWrappers(thisArg);
112
+ // a call without `new` keeps its parent in `this`: `x.Sub()`, every
113
+ // link of `new Root().Sub().SubSub()`, or `.call(otherParent)`. With
114
+ // no `this` the subtype was detached from its instance
115
+ // (`const { Sub } = x; Sub()`) — that form must be `new Sub()`
116
+ if (this === undefined) {
117
+ throw new WRONG_INSTANCE_INVOCATION('detached subtype should be made with new keyword');
118
+ }
119
+ // a bare primitive parent (`x.Sub.call(5)`) is not boxed: strict
120
+ // mode keeps `this` primitive, and nothing can inherit from it —
121
+ // `null` and wrapper objects (`new Number(5)`) are handled by
122
+ // reflectPrimitiveWrappers, a primitive is rejected here
123
+ const thisType = typeof this;
124
+ if (this !== null && thisType !== 'object' && thisType !== 'function') {
125
+ throw new WRONG_MODIFICATION_PATTERN(`subtype should be made on an object, got ${thisType}`);
126
+ }
127
+ const existentInstance = reflectPrimitiveWrappers(this);
108
128
  gatherLazyDefinition(subtype, existentInstance);
109
129
  const instance = new InstanceCreator(
110
130
  subtype as TypeDef,
@@ -36,6 +36,14 @@ const nativeProps = new Set([
36
36
  '__self__',
37
37
  ]);
38
38
 
39
+ // a memory layer is a __props__ key that sits in an instance's chain: created
40
+ // by mnemonica per construction, so it is safe to re-link. (The props objects
41
+ // keyed there for setProps additions are never chain levels)
42
+ export const isMemoryLayer = (level: object): boolean => {
43
+ const result = __props__.has(level);
44
+ return result;
45
+ };
46
+
39
47
  export const _addProps = function (this: InstanceCreatorContext): void {
40
48
 
41
49
 
@@ -291,6 +299,7 @@ export type {
291
299
  };
292
300
 
293
301
  module.exports = {
302
+ isMemoryLayer,
294
303
  _addProps,
295
304
  _getProps,
296
305
  _setSelf,
@@ -56,10 +56,12 @@ export interface TypeProxyInstance extends TypeProxyGetHandler, TypeProxySetHand
56
56
  }
57
57
 
58
58
  export const TypeProxy = function (__type__: TypeProxyType, ancestor: unknown) {
59
- Object.assign(
59
+ odp(
60
60
  this,
61
+ '__type__',
61
62
  {
62
- __type__
63
+ value : __type__,
64
+ enumerable : true
63
65
  }
64
66
  );
65
67
  // for a lazy ROOT type the ancestor is not fixed at define time: it is
@@ -82,10 +84,12 @@ export const TypeProxy = function (__type__: TypeProxyType, ancestor: unknown) {
82
84
  }
83
85
  );
84
86
  } else {
85
- Object.assign(
87
+ odp(
86
88
  this,
89
+ 'ancestor',
87
90
  {
88
- ancestor
91
+ value : ancestor,
92
+ enumerable : true
89
93
  }
90
94
  );
91
95
  }
@@ -690,6 +690,10 @@ export interface InstanceCreatorContext {
690
690
  // constructor-body throw
691
691
  lazyFailed?: boolean;
692
692
  lazyError?: unknown;
693
+ // set by throwModificationError once the post hooks have fired for this
694
+ // construction; postProcessing reads it to avoid a second firing when a
695
+ // creationError hook suppressed the throw
696
+ postHooksFired?: boolean;
693
697
 
694
698
  getExistentAsyncStack(existentInstance: object): unknown;
695
699
  postProcessing(continuationOf?: TypeDef): void;