Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
81146 views
1
// Generated by CoffeeScript 1.6.3
2
(function() {
3
var Lexer, Module, SourceMap, child_process, compile, ext, findExtension, fork, formatSourcePosition, fs, helpers, lexer, loadFile, parser, patchStackTrace, patched, path, sourceMaps, vm, _i, _len, _ref,
4
__hasProp = {}.hasOwnProperty;
5
6
fs = require('fs');
7
8
vm = require('vm');
9
10
path = require('path');
11
12
child_process = require('child_process');
13
14
Lexer = require('./lexer').Lexer;
15
16
parser = require('./parser').parser;
17
18
helpers = require('./helpers');
19
20
SourceMap = require('./sourcemap');
21
22
exports.VERSION = '1.6.3';
23
24
exports.helpers = helpers;
25
26
exports.compile = compile = function(code, options) {
27
var answer, currentColumn, currentLine, fragment, fragments, header, js, map, merge, newLines, _i, _len;
28
if (options == null) {
29
options = {};
30
}
31
merge = helpers.merge;
32
if (options.sourceMap) {
33
map = new SourceMap;
34
}
35
fragments = parser.parse(lexer.tokenize(code, options)).compileToFragments(options);
36
currentLine = 0;
37
if (options.header) {
38
currentLine += 1;
39
}
40
if (options.shiftLine) {
41
currentLine += 1;
42
}
43
currentColumn = 0;
44
js = "";
45
for (_i = 0, _len = fragments.length; _i < _len; _i++) {
46
fragment = fragments[_i];
47
if (options.sourceMap) {
48
if (fragment.locationData) {
49
map.add([fragment.locationData.first_line, fragment.locationData.first_column], [currentLine, currentColumn], {
50
noReplace: true
51
});
52
}
53
newLines = helpers.count(fragment.code, "\n");
54
currentLine += newLines;
55
currentColumn = fragment.code.length - (newLines ? fragment.code.lastIndexOf("\n") : 0);
56
}
57
js += fragment.code;
58
}
59
if (options.header) {
60
header = "Generated by CoffeeScript " + this.VERSION;
61
js = "// " + header + "\n" + js;
62
}
63
if (options.sourceMap) {
64
answer = {
65
js: js
66
};
67
answer.sourceMap = map;
68
answer.v3SourceMap = map.generate(options, code);
69
return answer;
70
} else {
71
return js;
72
}
73
};
74
75
exports.tokens = function(code, options) {
76
return lexer.tokenize(code, options);
77
};
78
79
exports.nodes = function(source, options) {
80
if (typeof source === 'string') {
81
return parser.parse(lexer.tokenize(source, options));
82
} else {
83
return parser.parse(source);
84
}
85
};
86
87
exports.run = function(code, options) {
88
var answer, mainModule;
89
if (options == null) {
90
options = {};
91
}
92
mainModule = require.main;
93
if (options.sourceMap == null) {
94
options.sourceMap = true;
95
}
96
mainModule.filename = process.argv[1] = options.filename ? fs.realpathSync(options.filename) : '.';
97
mainModule.moduleCache && (mainModule.moduleCache = {});
98
mainModule.paths = require('module')._nodeModulePaths(path.dirname(fs.realpathSync(options.filename || '.')));
99
if (!helpers.isCoffee(mainModule.filename) || require.extensions) {
100
answer = compile(code, options);
101
patchStackTrace();
102
sourceMaps[mainModule.filename] = answer.sourceMap;
103
return mainModule._compile(answer.js, mainModule.filename);
104
} else {
105
return mainModule._compile(code, mainModule.filename);
106
}
107
};
108
109
exports["eval"] = function(code, options) {
110
var Module, Script, js, k, o, r, sandbox, v, _i, _len, _module, _ref, _ref1, _require;
111
if (options == null) {
112
options = {};
113
}
114
if (!(code = code.trim())) {
115
return;
116
}
117
Script = vm.Script;
118
if (Script) {
119
if (options.sandbox != null) {
120
if (options.sandbox instanceof Script.createContext().constructor) {
121
sandbox = options.sandbox;
122
} else {
123
sandbox = Script.createContext();
124
_ref = options.sandbox;
125
for (k in _ref) {
126
if (!__hasProp.call(_ref, k)) continue;
127
v = _ref[k];
128
sandbox[k] = v;
129
}
130
}
131
sandbox.global = sandbox.root = sandbox.GLOBAL = sandbox;
132
} else {
133
sandbox = global;
134
}
135
sandbox.__filename = options.filename || 'eval';
136
sandbox.__dirname = path.dirname(sandbox.__filename);
137
if (!(sandbox !== global || sandbox.module || sandbox.require)) {
138
Module = require('module');
139
sandbox.module = _module = new Module(options.modulename || 'eval');
140
sandbox.require = _require = function(path) {
141
return Module._load(path, _module, true);
142
};
143
_module.filename = sandbox.__filename;
144
_ref1 = Object.getOwnPropertyNames(require);
145
for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
146
r = _ref1[_i];
147
if (r !== 'paths') {
148
_require[r] = require[r];
149
}
150
}
151
_require.paths = _module.paths = Module._nodeModulePaths(process.cwd());
152
_require.resolve = function(request) {
153
return Module._resolveFilename(request, _module);
154
};
155
}
156
}
157
o = {};
158
for (k in options) {
159
if (!__hasProp.call(options, k)) continue;
160
v = options[k];
161
o[k] = v;
162
}
163
o.bare = true;
164
js = compile(code, o);
165
if (sandbox === global) {
166
return vm.runInThisContext(js);
167
} else {
168
return vm.runInContext(js, sandbox);
169
}
170
};
171
172
loadFile = function(module, filename) {
173
var answer, raw, stripped;
174
raw = fs.readFileSync(filename, 'utf8');
175
stripped = raw.charCodeAt(0) === 0xFEFF ? raw.substring(1) : raw;
176
answer = compile(stripped, {
177
filename: filename,
178
sourceMap: true,
179
literate: helpers.isLiterate(filename)
180
});
181
sourceMaps[filename] = answer.sourceMap;
182
return module._compile(answer.js, filename);
183
};
184
185
if (require.extensions) {
186
_ref = ['.coffee', '.litcoffee', '.coffee.md'];
187
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
188
ext = _ref[_i];
189
require.extensions[ext] = loadFile;
190
}
191
Module = require('module');
192
findExtension = function(filename) {
193
var curExtension, extensions;
194
extensions = path.basename(filename).split('.');
195
if (extensions[0] === '') {
196
extensions.shift();
197
}
198
while (extensions.shift()) {
199
curExtension = '.' + extensions.join('.');
200
if (Module._extensions[curExtension]) {
201
return curExtension;
202
}
203
}
204
return '.js';
205
};
206
Module.prototype.load = function(filename) {
207
var extension;
208
this.filename = filename;
209
this.paths = Module._nodeModulePaths(path.dirname(filename));
210
extension = findExtension(filename);
211
Module._extensions[extension](this, filename);
212
return this.loaded = true;
213
};
214
}
215
216
if (child_process) {
217
fork = child_process.fork;
218
child_process.fork = function(path, args, options) {
219
var execPath;
220
if (args == null) {
221
args = [];
222
}
223
if (options == null) {
224
options = {};
225
}
226
execPath = helpers.isCoffee(path) ? 'coffee' : null;
227
if (!Array.isArray(args)) {
228
args = [];
229
options = args || {};
230
}
231
options.execPath || (options.execPath = execPath);
232
return fork(path, args, options);
233
};
234
}
235
236
lexer = new Lexer;
237
238
parser.lexer = {
239
lex: function() {
240
var tag, token;
241
token = this.tokens[this.pos++];
242
if (token) {
243
tag = token[0], this.yytext = token[1], this.yylloc = token[2];
244
this.yylineno = this.yylloc.first_line;
245
} else {
246
tag = '';
247
}
248
return tag;
249
},
250
setInput: function(tokens) {
251
this.tokens = tokens;
252
return this.pos = 0;
253
},
254
upcomingInput: function() {
255
return "";
256
}
257
};
258
259
parser.yy = require('./nodes');
260
261
parser.yy.parseError = function(message, _arg) {
262
var token;
263
token = _arg.token;
264
message = "unexpected " + (token === 1 ? 'end of input' : token);
265
return helpers.throwSyntaxError(message, parser.lexer.yylloc);
266
};
267
268
patched = false;
269
270
sourceMaps = {};
271
272
patchStackTrace = function() {
273
var mainModule;
274
if (patched) {
275
return;
276
}
277
patched = true;
278
mainModule = require.main;
279
return Error.prepareStackTrace = function(err, stack) {
280
var frame, frames, getSourceMapping, sourceFiles, _ref1;
281
sourceFiles = {};
282
getSourceMapping = function(filename, line, column) {
283
var answer, sourceMap;
284
sourceMap = sourceMaps[filename];
285
if (sourceMap) {
286
answer = sourceMap.sourceLocation([line - 1, column - 1]);
287
}
288
if (answer) {
289
return [answer[0] + 1, answer[1] + 1];
290
} else {
291
return null;
292
}
293
};
294
frames = (function() {
295
var _j, _len1, _results;
296
_results = [];
297
for (_j = 0, _len1 = stack.length; _j < _len1; _j++) {
298
frame = stack[_j];
299
if (frame.getFunction() === exports.run) {
300
break;
301
}
302
_results.push(" at " + (formatSourcePosition(frame, getSourceMapping)));
303
}
304
return _results;
305
})();
306
return "" + err.name + ": " + ((_ref1 = err.message) != null ? _ref1 : '') + "\n" + (frames.join('\n')) + "\n";
307
};
308
};
309
310
formatSourcePosition = function(frame, getSourceMapping) {
311
var as, column, fileLocation, fileName, functionName, isConstructor, isMethodCall, line, methodName, source, tp, typeName;
312
fileName = void 0;
313
fileLocation = '';
314
if (frame.isNative()) {
315
fileLocation = "native";
316
} else {
317
if (frame.isEval()) {
318
fileName = frame.getScriptNameOrSourceURL();
319
if (!fileName) {
320
fileLocation = "" + (frame.getEvalOrigin()) + ", ";
321
}
322
} else {
323
fileName = frame.getFileName();
324
}
325
fileName || (fileName = "<anonymous>");
326
line = frame.getLineNumber();
327
column = frame.getColumnNumber();
328
source = getSourceMapping(fileName, line, column);
329
fileLocation = source ? "" + fileName + ":" + source[0] + ":" + source[1] + ", <js>:" + line + ":" + column : "" + fileName + ":" + line + ":" + column;
330
}
331
functionName = frame.getFunctionName();
332
isConstructor = frame.isConstructor();
333
isMethodCall = !(frame.isToplevel() || isConstructor);
334
if (isMethodCall) {
335
methodName = frame.getMethodName();
336
typeName = frame.getTypeName();
337
if (functionName) {
338
tp = as = '';
339
if (typeName && functionName.indexOf(typeName)) {
340
tp = "" + typeName + ".";
341
}
342
if (methodName && functionName.indexOf("." + methodName) !== functionName.length - methodName.length - 1) {
343
as = " [as " + methodName + "]";
344
}
345
return "" + tp + functionName + as + " (" + fileLocation + ")";
346
} else {
347
return "" + typeName + "." + (methodName || '<anonymous>') + " (" + fileLocation + ")";
348
}
349
} else if (isConstructor) {
350
return "new " + (functionName || '<anonymous>') + " (" + fileLocation + ")";
351
} else if (functionName) {
352
return "" + functionName + " (" + fileLocation + ")";
353
} else {
354
return fileLocation;
355
}
356
};
357
358
}).call(this);
359
360