forked from DerivcoIpswich/dsharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenerics.js
More file actions
70 lines (61 loc) · 2.82 KB
/
Copy pathGenerics.js
File metadata and controls
70 lines (61 loc) · 2.82 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
function createGenericType(ctorMethod, typeArguments) {
var genericConstructor = getGenericConstructor(ctorMethod, typeArguments);
var args = [null].concat(Array.prototype.slice.call(arguments).splice(2));
return new (Function.prototype.bind.apply(genericConstructor, args));
}
function getGenericConstructor(ctorMethod, typeArguments) {
if (!isValue(ctorMethod)) {
return null;
}
var key = createGenericConstructorKey(ctorMethod, typeArguments);
var genericInstance = _genericConstructorCache[key];
if (!genericInstance) {
if (isInterface(ctorMethod)) {
genericInstance = function () { };
genericInstance.$type = _interfaceMarker;
genericInstance.$name = ctorMethod.$name;
genericInstance.$interfaces = ctorMethod.$interfaces;
genericInstance.$typeArguments = typeArguments || {};
}
else {
genericInstance = function () {
this.__proto__.constructor.$typeArguments = typeArguments || {};
this.__proto__.constructor.$base = this.__proto__.constructor.$base || ctorMethod.$base;
this.__proto__.constructor.$interfaces = this.__proto__.constructor.$interfaces || ctorMethod.$interfaces;
this.__proto__.constructor.$type = this.__proto__.constructor.$type || ctorMethod.$type;
this.__proto__.constructor.$name = this.__proto__.constructor.$name || ctorMethod.$name;
this.__proto__.constructor.$constructorParams = this.__proto__.constructor.$constructorParams || ctorMethod.$constructorParams;
ctorMethod.apply(this, Array.prototype.slice.call(arguments));
};
genericInstance.prototype = Object.create(ctorMethod.prototype);
genericInstance.prototype.constructor = genericInstance;
}
genericInstance.prototype = Object.create(ctorMethod.prototype);
genericInstance.prototype.constructor = genericInstance;
_genericConstructorCache[key] = genericInstance;
}
return genericInstance;
}
function createGenericConstructorKey(ctorMethod, typeArguments) {
var key = getTypeName(ctorMethod);
key += "<";
key += Object.getOwnPropertyNames(typeArguments)
.map(function (parameterKey) { return getTypeName(typeArguments[parameterKey]); })
.join(",");
key += ">";
return key;
}
function getTypeName(instance) {
try {
return instance["$name"] || instance.constructor.$name || instance["name"];
}
catch (ex) {
return instance.toString();
}
}
function getTypeArgument(instance, typeArgumentName) {
if (!isValue(instance) || emptyString(typeArgumentName) || !isValue(instance.constructor.$typeArguments)) {
return null;
}
return instance.constructor.$typeArguments[typeArgumentName];
}