Fixes #2737, fix fork() for both .js and .coffee, revised.

This commit is contained in:
Jeremy Ashkenas 2013-03-18 11:46:54 +08:00
commit 4f815ea978
4 changed files with 102 additions and 51 deletions

View File

@ -1,46 +1,30 @@
// Generated by CoffeeScript 1.6.1
(function() {
var Lexer, compile, ext, formatSourcePosition, fs, helpers, lexer, loadFile, parser, patchStackTrace, patched, path, sourcemap, vm, _i, _len, _ref,
var Lexer, child_process, compile, ext, fork, formatSourcePosition, fs, helpers, lexer, loadFile, parser, patchStackTrace, patched, path, sourcemap, vm, _i, _len, _ref,
__hasProp = {}.hasOwnProperty;
fs = require('fs');
vm = require('vm');
path = require('path');
child_process = require('child_process');
Lexer = require('./lexer').Lexer;
parser = require('./parser').parser;
helpers = require('./helpers');
vm = require('vm');
sourcemap = require('./sourcemap');
loadFile = function(module, filename) {
var raw, stripped;
raw = fs.readFileSync(filename, 'utf8');
stripped = raw.charCodeAt(0) === 0xFEFF ? raw.substring(1) : raw;
return module._compile(compile(stripped, {
filename: filename,
literate: helpers.isLiterate(filename)
}), filename);
};
if (require.extensions) {
_ref = ['.coffee', '.litcoffee', '.md', '.coffee.md'];
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
ext = _ref[_i];
require.extensions[ext] = loadFile;
}
}
exports.VERSION = '1.6.1';
exports.helpers = helpers;
exports.compile = compile = function(code, options) {
var answer, currentColumn, currentLine, fragment, fragments, header, js, merge, newLines, sourceMap, _j, _len1;
var answer, currentColumn, currentLine, fragment, fragments, header, js, merge, newLines, sourceMap, _i, _len;
if (options == null) {
options = {};
}
@ -55,8 +39,8 @@
}
currentColumn = 0;
js = "";
for (_j = 0, _len1 = fragments.length; _j < _len1; _j++) {
fragment = fragments[_j];
for (_i = 0, _len = fragments.length; _i < _len; _i++) {
fragment = fragments[_i];
if (sourceMap) {
if (fragment.locationData) {
sourceMap.addMapping([fragment.locationData.first_line, fragment.locationData.first_column], [currentLine, currentColumn], {
@ -100,12 +84,12 @@
};
exports.run = function(code, options) {
var answer, mainModule, _ref1;
var answer, mainModule, _ref;
if (options == null) {
options = {};
}
mainModule = require.main;
if ((_ref1 = options.sourceMap) == null) {
if ((_ref = options.sourceMap) == null) {
options.sourceMap = true;
}
mainModule.filename = process.argv[1] = options.filename ? fs.realpathSync(options.filename) : '.';
@ -122,7 +106,7 @@
};
exports["eval"] = function(code, options) {
var Module, Script, js, k, o, r, sandbox, v, _j, _len1, _module, _ref1, _ref2, _require;
var Module, Script, js, k, o, r, sandbox, v, _i, _len, _module, _ref, _ref1, _require;
if (options == null) {
options = {};
}
@ -136,10 +120,10 @@
sandbox = options.sandbox;
} else {
sandbox = Script.createContext();
_ref1 = options.sandbox;
for (k in _ref1) {
if (!__hasProp.call(_ref1, k)) continue;
v = _ref1[k];
_ref = options.sandbox;
for (k in _ref) {
if (!__hasProp.call(_ref, k)) continue;
v = _ref[k];
sandbox[k] = v;
}
}
@ -156,9 +140,9 @@
return Module._load(path, _module, true);
};
_module.filename = sandbox.__filename;
_ref2 = Object.getOwnPropertyNames(require);
for (_j = 0, _len1 = _ref2.length; _j < _len1; _j++) {
r = _ref2[_j];
_ref1 = Object.getOwnPropertyNames(require);
for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
r = _ref1[_i];
if (r !== 'paths') {
_require[r] = require[r];
}
@ -184,6 +168,43 @@
}
};
loadFile = function(module, filename) {
var raw, stripped;
raw = fs.readFileSync(filename, 'utf8');
stripped = raw.charCodeAt(0) === 0xFEFF ? raw.substring(1) : raw;
return module._compile(compile(stripped, {
filename: filename,
literate: helpers.isLiterate(filename)
}), filename);
};
if (require.extensions) {
_ref = ['.coffee', '.litcoffee', '.coffee.md'];
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
ext = _ref[_i];
require.extensions[ext] = loadFile;
}
}
fork = child_process.fork;
child_process.fork = function(path, args, options) {
var execPath;
if (args == null) {
args = [];
}
if (options == null) {
options = {};
}
execPath = helpers.isCoffee(path) ? 'coffee' : null;
if (!Array.isArray(args)) {
args = [];
options = args || {};
}
options.execPath || (options.execPath = execPath);
return fork(path, args, options);
};
lexer = new Lexer;
parser.lexer = {

View File

@ -6,23 +6,14 @@
# If included on a webpage, it will automatically sniff out, compile, and
# execute all scripts present in `text/coffeescript` tags.
fs = require 'fs'
path = require 'path'
{Lexer} = require './lexer'
{parser} = require './parser'
helpers = require './helpers'
vm = require 'vm'
sourcemap = require './sourcemap'
# Load and run a CoffeeScript file for Node, stripping any `BOM`s.
loadFile = (module, filename) ->
raw = fs.readFileSync filename, 'utf8'
stripped = if raw.charCodeAt(0) is 0xFEFF then raw.substring 1 else raw
module._compile compile(stripped, {filename, literate: helpers.isLiterate filename}), filename
if require.extensions
for ext in ['.coffee', '.litcoffee', '.md', '.coffee.md']
require.extensions[ext] = loadFile
fs = require 'fs'
vm = require 'vm'
path = require 'path'
child_process = require 'child_process'
{Lexer} = require './lexer'
{parser} = require './parser'
helpers = require './helpers'
sourcemap = require './sourcemap'
# The current CoffeeScript version number.
exports.VERSION = '1.6.1'
@ -154,6 +145,29 @@ exports.eval = (code, options = {}) ->
else
vm.runInContext js, sandbox
# Load and run a CoffeeScript file for Node, stripping any `BOM`s.
loadFile = (module, filename) ->
raw = fs.readFileSync filename, 'utf8'
stripped = if raw.charCodeAt(0) is 0xFEFF then raw.substring 1 else raw
module._compile compile(stripped, {filename, literate: helpers.isLiterate filename}), filename
# If the installed version of Node supports `require.extensions`, register
# CoffeeScript as an extension.
if require.extensions
for ext in ['.coffee', '.litcoffee', '.coffee.md']
require.extensions[ext] = loadFile
# Patch `child_process.fork` so that Coffee scripts are able to fork both
# CoffeeScript files, and JavaScript files, directly.
{fork} = child_process
child_process.fork = (path, args = [], options = {}) ->
execPath = if helpers.isCoffee(path) then 'coffee' else null
if not Array.isArray args
args = []
options = args or {}
options.execPath or= execPath
fork path, args, options
# Instantiate a Lexer for our use here.
lexer = new Lexer

15
test/cluster.coffee Normal file
View File

@ -0,0 +1,15 @@
# Cluster Module
# ---------
return if testingBrowser?
cluster = require 'cluster'
if cluster.isMaster
test "#2737 - cluster module can spawn workers from a coffeescript process", ->
cluster.once 'exit', (worker, code) ->
eq code, 0
cluster.fork()
else
process.exit 0

View File

@ -88,6 +88,7 @@
'assignment'
'booleans'
'classes'
'cluster'
'comments'
'compilation'
'comprehensions'