-
Notifications
You must be signed in to change notification settings - Fork 90
/
edge.js
188 lines (169 loc) · 6.99 KB
/
edge.js
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
var fs = require('fs')
, path = require('path')
, builtEdge = path.resolve(__dirname, '../build/Release/' + (process.env.EDGE_USE_CORECLR || !fs.existsSync(path.resolve(__dirname, '../build/Release/edge_nativeclr.node')) ? 'edge_coreclr.node' : 'edge_nativeclr.node'))
, edge;
var versionMap = [
[ /^6\./, '6.5.0' ],
[ /^7.4\./, '7.4.0' ],
[ /^7.9\./, '7.9.0' ],
[ /^7\./, '7.9.0' ],
[ /^8.2\./, '8.2.1' ],
[ /^8.9\./, '8.9.3' ],
[ /^8\./, '8.9.3' ],
[ /^10.2\./, '10.2.0' ],
[ /^10.11\./, '10.11.0' ],
[ /^10\./, '10.11.0' ],
[ /^12.0\./, '12.0.0' ],
[ /^12.4\./, '12.4.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.');
}
var edgeNative;
if (process.env.EDGE_NATIVE) {
edgeNative = process.env.EDGE_NATIVE;
}
else if (fs.existsSync(builtEdge)) {
edgeNative = builtEdge;
}
else if (process.platform === 'win32') {
edgeNative = path.resolve(__dirname, './native/' + process.platform + '/' + process.arch + '/' + determineVersion() + '/' + (process.env.EDGE_USE_CORECLR ? 'edge_coreclr' : 'edge_nativeclr'));
}
else {
throw new Error('The edge native module is not available at ' + builtEdge
+ '. You can use EDGE_NATIVE environment variable to provide alternate location of edge.node. '
+ 'If you need to build edge.node, follow build instructions for your platform at https://github.com/tjanczuk/edge');
}
if (process.env.EDGE_DEBUG) {
console.log('Load edge native library from: ' + edgeNative);
}
if (edgeNative.match(/edge_coreclr\.node$/i)) {
// Propagate the choice between desktop and coreclr to edge-cs; this is used in deciding
// how to compile literal C# at https://github.com/tjanczuk/edge-cs/blob/master/lib/edge-cs.js
process.env.EDGE_USE_CORECLR = 1;
}
if (process.env.EDGE_USE_CORECLR && !process.env.EDGE_BOOTSTRAP_DIR && fs.existsSync(path.join(__dirname, 'bootstrap', 'bin', 'Release', 'netcoreapp1.1', 'bootstrap.dll'))) {
process.env.EDGE_BOOTSTRAP_DIR = path.join(__dirname, 'bootstrap', 'bin', 'Release', 'netcoreapp1.1');
}
process.env.EDGE_NATIVE = edgeNative;
if (process.versions['electron'] || process.versions['atom-shell'] || process.env.ELECTRON_RUN_AS_NODE) {
edge = require(edgeNative);
}
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') {
var originalPrepareStackTrace = Error.prepareStackTrace;
var stack;
try {
Error.prepareStackTrace = function(error, stack) {
return stack;
};
stack = new Error().stack;
}
finally
{
Error.prepareStackTrace = originalPrepareStackTrace;
}
options = { source: options, jsFileName: stack[1].getFileName(), jsLineNumber: stack[1].getLineNumber() };
}
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 package name or assembly.");
}
if (process.env.EDGE_USE_CORECLR) {
var defaultManifest = path.join(__dirname, 'bootstrap', 'bin', 'Release', 'netcoreapp1.1', 'bootstrap.deps.json');
var compilerManifest;
if(compiler.getBootstrapDependencyManifest){
compilerManifest = compiler.getBootstrapDependencyManifest();
}
options.bootstrapDependencyManifest =
compilerManifest && fs.existsSync(compilerManifest)
? compilerManifest
: defaultManifest;
}
}
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(/[^]*\/\*([^]*)\*\/\s*\}$/);
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);
};