forked from DerivcoIpswich/dsharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTypeSystem.js
More file actions
231 lines (194 loc) · 6.04 KB
/
Copy pathTypeSystem.js
File metadata and controls
231 lines (194 loc) · 6.04 KB
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
var _modules = {};
var _genericConstructorCache = {};
var _classMarker = 'class';
var _interfaceMarker = 'interface';
function createType(typeName, typeInfo, typeRegistry) {
// The typeInfo is either an array of information representing
// classes and interfaces, or an object representing enums and resources
// or a function, representing a record factory.
if (Array.isArray(typeInfo)) {
var typeMarker = typeInfo[0];
var type = typeInfo[1];
var prototypeDescription = typeInfo[2];
var baseType = typeInfo[3];
// A class is minimally the class type and an object representing
// its prototype members, and optionally the base type, and references
// to interfaces implemented by the class.
if (typeMarker === _classMarker) {
if (baseType) {
// Chain the prototype of the base type (using an anonymous type
// in case the base class is not creatable, or has side-effects).
var anonymous = function () { };
anonymous.prototype = baseType.prototype;
type.prototype = new anonymous();
type.prototype.constructor = type;
}
// Add the type's prototype members if there are any
prototypeDescription && extendType(type.prototype, prototypeDescription);
type.$base = baseType || Object;
}
type.$name = typeName;
return typeRegistry[typeName] = type;
}
else if (typeInfo.constructor === Enum) {
return typeRegistry[typeName] = typeInfo;
}
return typeInfo;
}
function defineClass(type, prototypeDescription, constructorParams, baseType, interfaces) {
type.$type = _classMarker;
type.$constructorParams = constructorParams;
type.$interfaces = interfaces;
return [_classMarker, type, prototypeDescription, baseType];
}
function defineInterface(type, interfaces) {
type.$type = _interfaceMarker;
type.$interfaces = interfaces;
return [_interfaceMarker, type];
}
function isClass(fn) {
return fn.$type === _classMarker;
}
function isInterface(fn) {
return fn.$type === _interfaceMarker;
}
function typeOf(instance) {
var ctor;
// NOTE: We have to catch exceptions because the constructor
// cannot be looked up on native COM objects
try {
ctor = instance.constructor;
}
catch (ex) {
}
return ctor || Object;
}
function type(s) {
var nsIndex = s.indexOf('.');
var ns = nsIndex > 0 ? _modules[s.substr(0, nsIndex)] : self;
var name = nsIndex > 0 ? s.substr(nsIndex + 1) : s;
return ns ? ns[name] : null;
}
var _typeNames = [
Number, 'Number',
String, 'String',
Boolean, 'Boolean',
Array, 'Array',
Date, 'Date',
RegExp, 'RegExp',
Function, 'Function'
];
function typeName(type) {
if (!(type instanceof Function)) {
type = type.constructor;
}
if (type.$name) {
return type.$name;
}
if (type.name) {
return type.name;
}
for (var i = 0, len = _typeNames.length; i < len; i += 2) {
if (type === _typeNames[i]) {
return _typeNames[i + 1];
}
}
return 'Object';
}
function canAssign(type, otherType) {
// Checks if the specified type is equal to otherType,
// or is a parent of otherType
if ((type === Object) || (type === otherType)) {
return true;
}
// DateTime in CLR implements IEquatable and IComparable
if (otherType === Date && (
type === IEquatable_$1
|| type == IComparable_$1)) {
return true;
}
// Arrays in CLR implement IList and IList<T>
if (otherType === Array && (
type === IList
|| type == IList_$1)) {
return true;
}
if (type.$type === _classMarker) {
var baseType = otherType.$base;
while (baseType) {
if (type === baseType) {
return true;
}
baseType = baseType.$base;
}
}
else if (type.$type === _interfaceMarker) {
var baseType = otherType;
while (baseType) {
if (interfaceOf(baseType, type)) {
return true;
}
baseType = baseType.$base;
}
}
return false;
}
function interfaceOf(baseType, otherType) {
if (baseType === otherType || baseType["$name"] === otherType["$name"]) {
return true;
}
var interfaces = baseType.$interfaces;
if (interfaces) {
for (var i = 0, ln = interfaces.length; i < ln; ++i) {
if (interfaceOf(interfaces[i], otherType)) {
return true;
}
}
}
return false;
}
function instanceOf(type, instance) {
// Checks if the specified instance is of the specified type
if (!isValue(instance)) {
return false;
}
if ((type === Object) || (instance instanceof type)) {
return true;
}
var instanceType = typeOf(instance);
return canAssign(type, instanceType);
}
function canCast(instance, type) {
return instanceOf(type, instance);
}
function safeCast(instance, type) {
return instanceOf(type, instance) ? instance : null;
}
function module(name, implementation, exports) {
var registry = _modules[name] = { $name: name };
var api = {};
if (exports) {
for (var typeName in exports) {
api[typeName] = createType(typeName, exports[typeName], registry);
}
}
if (implementation) {
for (var typeName in implementation) {
createType(typeName, implementation[typeName], registry);
}
}
return api;
}
function baseProperty(type, propertyName) {
var baseType = type.$base;
return Object.getOwnPropertyDescriptor(baseType.prototype, propertyName) || baseProperty(baseType, propertyName);
}
function getConstructorParams(type) {
return type.$constructorParams;
}
function createInstance(type, parameters) {
var proto = type.prototype;
var instance = Object.create(proto);
proto.constructor.apply(instance, parameters);
return instance;
}