1 |
|
2 | (function() {
|
3 | var Lexer, RESERVED, compile, fs, lexer, parser, path, stripBOM, vm, _ref,
|
4 | __hasProp = {}.hasOwnProperty;
|
5 |
|
6 | fs = require('fs');
|
7 |
|
8 | path = require('path');
|
9 |
|
10 | _ref = require('./lexer'), Lexer = _ref.Lexer, RESERVED = _ref.RESERVED;
|
11 |
|
12 | parser = require('./parser').parser;
|
13 |
|
14 | vm = require('vm');
|
15 |
|
16 | stripBOM = function(content) {
|
17 | if (content.charCodeAt(0) === 0xFEFF) {
|
18 | return content.substring(1);
|
19 | } else {
|
20 | return content;
|
21 | }
|
22 | };
|
23 |
|
24 | if (require.extensions) {
|
25 | require.extensions['.coffee'] = function(module, filename) {
|
26 | var content;
|
27 | content = compile(stripBOM(fs.readFileSync(filename, 'utf8')), {
|
28 | filename: filename
|
29 | });
|
30 | return module._compile(content, filename);
|
31 | };
|
32 | }
|
33 |
|
34 | exports.VERSION = '1.4.0';
|
35 |
|
36 | exports.RESERVED = RESERVED;
|
37 |
|
38 | exports.helpers = require('./helpers');
|
39 |
|
40 | exports.compile = compile = function(code, options) {
|
41 | var header, js, merge;
|
42 | if (options == null) {
|
43 | options = {};
|
44 | }
|
45 | merge = exports.helpers.merge;
|
46 | try {
|
47 | js = (parser.parse(lexer.tokenize(code))).compile(options);
|
48 | if (!options.header) {
|
49 | return js;
|
50 | }
|
51 | } catch (err) {
|
52 | if (options.filename) {
|
53 | err.message = "In " + options.filename + ", " + err.message;
|
54 | }
|
55 | throw err;
|
56 | }
|
57 | header = "Generated by CoffeeScript " + this.VERSION;
|
58 | return "// " + header + "\n" + js;
|
59 | };
|
60 |
|
61 | exports.tokens = function(code, options) {
|
62 | return lexer.tokenize(code, options);
|
63 | };
|
64 |
|
65 | exports.nodes = function(source, options) {
|
66 | if (typeof source === 'string') {
|
67 | return parser.parse(lexer.tokenize(source, options));
|
68 | } else {
|
69 | return parser.parse(source);
|
70 | }
|
71 | };
|
72 |
|
73 | exports.run = function(code, options) {
|
74 | var mainModule;
|
75 | if (options == null) {
|
76 | options = {};
|
77 | }
|
78 | mainModule = require.main;
|
79 | mainModule.filename = process.argv[1] = options.filename ? fs.realpathSync(options.filename) : '.';
|
80 | mainModule.moduleCache && (mainModule.moduleCache = {});
|
81 | mainModule.paths = require('module')._nodeModulePaths(path.dirname(fs.realpathSync(options.filename)));
|
82 | if (path.extname(mainModule.filename) !== '.coffee' || require.extensions) {
|
83 | return mainModule._compile(compile(code, options), mainModule.filename);
|
84 | } else {
|
85 | return mainModule._compile(code, mainModule.filename);
|
86 | }
|
87 | };
|
88 |
|
89 | exports["eval"] = function(code, options) {
|
90 | var Module, Script, js, k, o, r, sandbox, v, _i, _len, _module, _ref1, _ref2, _require;
|
91 | if (options == null) {
|
92 | options = {};
|
93 | }
|
94 | if (!(code = code.trim())) {
|
95 | return;
|
96 | }
|
97 | Script = vm.Script;
|
98 | if (Script) {
|
99 | if (options.sandbox != null) {
|
100 | if (options.sandbox instanceof Script.createContext().constructor) {
|
101 | sandbox = options.sandbox;
|
102 | } else {
|
103 | sandbox = Script.createContext();
|
104 | _ref1 = options.sandbox;
|
105 | for (k in _ref1) {
|
106 | if (!__hasProp.call(_ref1, k)) continue;
|
107 | v = _ref1[k];
|
108 | sandbox[k] = v;
|
109 | }
|
110 | }
|
111 | sandbox.global = sandbox.root = sandbox.GLOBAL = sandbox;
|
112 | } else {
|
113 | sandbox = global;
|
114 | }
|
115 | sandbox.__filename = options.filename || 'eval';
|
116 | sandbox.__dirname = path.dirname(sandbox.__filename);
|
117 | if (!(sandbox !== global || sandbox.module || sandbox.require)) {
|
118 | Module = require('module');
|
119 | sandbox.module = _module = new Module(options.modulename || 'eval');
|
120 | sandbox.require = _require = function(path) {
|
121 | return Module._load(path, _module, true);
|
122 | };
|
123 | _module.filename = sandbox.__filename;
|
124 | _ref2 = Object.getOwnPropertyNames(require);
|
125 | for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
|
126 | r = _ref2[_i];
|
127 | if (r !== 'paths') {
|
128 | _require[r] = require[r];
|
129 | }
|
130 | }
|
131 | _require.paths = _module.paths = Module._nodeModulePaths(process.cwd());
|
132 | _require.resolve = function(request) {
|
133 | return Module._resolveFilename(request, _module);
|
134 | };
|
135 | }
|
136 | }
|
137 | o = {};
|
138 | for (k in options) {
|
139 | if (!__hasProp.call(options, k)) continue;
|
140 | v = options[k];
|
141 | o[k] = v;
|
142 | }
|
143 | o.bare = true;
|
144 | js = compile(code, o);
|
145 | if (sandbox === global) {
|
146 | return vm.runInThisContext(js);
|
147 | } else {
|
148 | return vm.runInContext(js, sandbox);
|
149 | }
|
150 | };
|
151 |
|
152 | lexer = new Lexer;
|
153 |
|
154 | parser.lexer = {
|
155 | lex: function() {
|
156 | var tag, _ref1;
|
157 | _ref1 = this.tokens[this.pos++] || [''], tag = _ref1[0], this.yytext = _ref1[1], this.yylineno = _ref1[2];
|
158 | return tag;
|
159 | },
|
160 | setInput: function(tokens) {
|
161 | this.tokens = tokens;
|
162 | return this.pos = 0;
|
163 | },
|
164 | upcomingInput: function() {
|
165 | return "";
|
166 | }
|
167 | };
|
168 |
|
169 | parser.yy = require('./nodes');
|
170 |
|
171 | }).call(this);
|