forked from tjanczuk/edge
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathedge.js
More file actions
131 lines (114 loc) · 4.37 KB
/
Copy pathedge.js
File metadata and controls
131 lines (114 loc) · 4.37 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
var edge;
var versionMap = [
[ /^0\.6\./, '0.6.20' ],
[ /^0\.8\./, '0.8.22' ],
[ /^0\.10\./, '0.10.0' ]
];
function determineVersion() {
for (var i in versionMap) {
if (process.versions.node.match(versionMap[i][0])) {
return versionMap[i][1];
}
}
throw new Error('The edge module has not been pre-compiled for node.js version ' + process.version +
'. You must build a custom version of edge.node. Please refer to https://github.com/tjanczuk/edge ' +
'for building instructions.');
}
if (process.env.EDGE_NATIVE) {
edge = require(process.env.EDGE_NATIVE);
}
else if (process.platform === 'win32') {
edge = require('./native/' + process.platform + '/' + process.arch + '/' + determineVersion() + '/edge');
}
else {
throw new Error('The edge module is currently only supported on Windows. I do take contributions. ' +
'https://github.com/tjanczuk/edge');
}
exports.func = function(language, options) {
if (!options) {
options = language;
language = 'cs';
}
if (typeof options === 'string') {
if (options.match(/\.dll$/i)) {
options = { assemblyFile: options };
}
else {
options = { source: options };
}
}
else if (typeof options === 'function') {
options = { source: options };
}
else if (typeof options !== 'object') {
throw new Error('Specify the source code as string or provide an options object.');
}
if (typeof language !== 'string') {
throw new Error('The first argument must be a string identifying the language compiler to use.');
}
else if (!options.assemblyFile) {
var compilerName = 'edge-' + language.toLowerCase();
var compiler;
try {
compiler = require(compilerName);
}
catch (e) {
throw new Error("Unsupported language '" + language + "'. To compile script in language '" + language +
"' an npm module '" + compilerName + "' must be installed.");
}
try {
options.compiler = compiler.getCompiler();
}
catch (e) {
throw new Error("The '" + compilerName + "' module required to compile the '" + language + "' language " +
"does not contain getCompiler() function.");
}
if (typeof options.compiler !== 'string') {
throw new Error("The '" + compilerName + "' module required to compile the '" + language + "' language " +
"did not specify correct compiler assembly.");
}
}
if (!options.assemblyFile && !options.source) {
throw new Error('Provide DLL or source file name or .NET script literal as a string parmeter, or specify an options object '+
'with assemblyFile or source string property.');
}
else if (options.assemblyFile && options.source) {
throw new Error('Provide either an asseblyFile or source property, but not both.');
}
if (typeof options.source === 'function') {
var match = options.source.toString().match(/[^]*\/\*([^]*)\*\/\}$/);
if (match) {
options.source = match[1];
}
else {
throw new Error('If .NET source is provided as JavaScript function, function body must be a /* ... */ comment.');
}
}
if (options.references !== undefined) {
if (!Array.isArray(options.references)) {
throw new Error('The references property must be an array of strings.');
}
options.references.forEach(function (ref) {
if (typeof ref !== 'string') {
throw new Error('The references property must be an array of strings.');
}
});
}
if (options.assemblyFile) {
if (!options.typeName) {
var matched = options.assemblyFile.match(/([^\\\/]+)\.dll$/i);
if (!matched) {
throw new Error('Unable to determine the namespace name based on assembly file name. ' +
'Specify typeName parameter as a namespace qualified CLR type name of the application class.');
}
options.typeName = matched[1] + '.Startup';
}
}
else if (!options.typeName) {
options.typeName = "Startup";
}
if (!options.methodName) {
options.methodName = 'Invoke';
}
return edge.initializeClrFunc(options);
};