-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathRegistry.ts
192 lines (164 loc) · 4.95 KB
/
Registry.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import {
Assertion,
UnaryOperator,
BinaryOperator,
Result,
Message,
ContractFunction,
} from './model';
import {
AssertionBuilder,
OperatorBuilder,
ConnectorBuilder,
RuntimeBuilder,
} from './Builder';
import nodsl from './NoDsl';
/**
* @author Maciej Chałapuk ([email protected])
*/
export class Registry {
static readonly instance = new Registry();
contextProto = {
assertions: {},
operators: {},
connectors: {},
};
private traces : HashMap<any> = {};
private entities : HashMap<any> = {};
constructor() {
const { assertions, operators, connectors } = this.contextProto;
Object.setPrototypeOf(assertions, connectors);
Object.setPrototypeOf(operators, connectors);
// Fields names of `Result` interface are reserved
// as `OperatorBuilder` implements `Result`.
const trace = prepareTrace();
this.traces['success'] = this.traces['message'] = trace;
this.entities['success'] = this.entities['message'] = {};
}
addAssertion(assertions : HashMap<AssertionLike>) {
const newAssertions = this.filterAlreadyRegistered(assertions);
this.extendPrototype(
this.contextProto.assertions,
newAssertions,
function getAssertion<T>(this : RuntimeBuilder, assertion : Assertion<T>) {
return this.__pushAssertion(assertion);
},
);
return this;
}
addAssertionFactory(factories : HashMap<Assertion.Factory>) {
const newFactories = this.filterAlreadyRegistered(factories);
this.extendPrototype(
this.contextProto.assertions,
newFactories,
function getAssertionFactory(this : RuntimeBuilder, factory : Assertion.Factory) {
return (...args : any[]) => this.__pushAssertionFactory(factory, args);
},
);
return this;
}
addUnaryOperator(operators : HashMap<UnaryOperator>) {
const newOperators = this.filterAlreadyRegistered(operators);
// Unary operators must be added on prototype of `AssertionBuilder`.
this.extendPrototype(
this.contextProto.assertions,
newOperators,
function getUnaryOperator(this : RuntimeBuilder, operator : UnaryOperator) {
return this.__pushUnaryOperator(operator);
},
);
return this;
}
addBinaryOperator(operators : HashMap<BinaryOperator>) {
const newOperators = this.filterAlreadyRegistered(operators);
this.extendPrototype(
this.contextProto.operators,
newOperators,
function getBinaryOperator(this : RuntimeBuilder, operator : BinaryOperator) {
return this.__pushBinaryOperator(operator);
},
);
return this;
}
addConnectors(connectors : HashMap<any>) {
const newConnectors = this.filterAlreadyRegistered(connectors);
this.extendPrototype(
this.contextProto.connectors,
newConnectors,
function getConnector(this : RuntimeBuilder) {
// noop
return this;
},
);
return this;
}
private filterAlreadyRegistered<T>(entities : HashMap<T>) {
const trace = prepareTrace();
return Object.keys(entities)
.filter(name => {
nodsl.check(
name.length !== 0,
'name.length must be > 0 (got \'', name, '\')',
);
nodsl.check(
name[0] !== '_',
'name must not start with underscore (got \'', name, '\')',
);
const alreadyRegistered = this.entities[name];
if (alreadyRegistered === undefined) {
return true;
}
nodsl.check(
alreadyRegistered === entities[name],
'Entity of name ', name, ' already registered.\n',
'PREVIOUS REGISTRATION STACK TRACE:\n',
this.traces[name],
'CURRENT REGISTRATION STACK TRACE:\n'
);
return false;
})
.reduce(
(result, name) => {
this.traces[name] = trace;
this.entities[name] = entities[name];
result[name] = entities[name];
return result;
},
{} as HashMap<T>,
)
;
}
private extendPrototype<T>(proto : object, newElements : HashMap<T>, getter : (elem : T) => any) {
const enumerable = true;
const names = Object.keys(newElements);
names.forEach(name => {
function get(this : RuntimeBuilder) {
return getter.call(this, newElements[name]);
}
Object.defineProperty(proto, name, { get, enumerable });
});
}
}
export default Registry;
/**
* @author Maciej Chałapuk ([email protected])
*/
export interface AssertionLike {
assert(varName: string, testedValue: any, contract: ContractFunction) : Result;
}
/**
* @author Maciej Chałapuk ([email protected])
*/
export interface HashMap<T> {
[_ : string] : T | undefined;
}
function prepareTrace() {
const stack = new Error().stack as string;
const firstNewlineIndex = stack.indexOf('\n');
const secondNewlineIndex = stack.indexOf('\n', firstNewlineIndex + 1)
const trace = stack.substring(secondNewlineIndex + 1)
.split('\n')
.map(line => ` ${line}`)
.join('\n')
;
}