Skip to content

Commit 1e31827

Browse files
committed
majorly cleaned up the CoffeeScript that defines the Narwhal integration
1 parent e595dbf commit 1e31827

File tree

4 files changed

+81
-88
lines changed

4 files changed

+81
-88
lines changed

lib-js/coffee-script.js

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,22 @@
11
(function(){
2-
var File = require('file');
2+
3+
// This (javascript) file is generated from lib/coffee_script/narwhal/coffee-script.cs Executes the `coffee-script` Ruby program to convert from CoffeeScript
4+
// to Javascript. Eventually this will hopefully happen entirely within JS. Require external dependencies.
35
var OS = require('os');
6+
var File = require('file');
7+
var Readline = require('readline');
8+
// The path to the CoffeeScript Compiler.
9+
var coffeePath = File.path(module.path).dirname().dirname().join('bin', 'coffee-script');
10+
// Our general-purpose error handler.
11+
var checkForErrors = function(coffeeProcess) {
12+
if (coffeeProcess.wait() === 0) {
13+
return true;
14+
}
15+
system.stderr.print(coffeeProcess.stderr.read());
16+
throw new Error("coffee-script compile error");
17+
};
18+
// Run a simple REPL, round-tripping to the CoffeeScript compiler for every
19+
// command.
420
exports.run = function(args) {
521
args.shift();
622
if (args.length) {
@@ -9,7 +25,7 @@
925
while (true) {
1026
try {
1127
system.stdout.write('cs> ').flush();
12-
var result = exports.cs_eval(require('readline').readline());
28+
var result = exports.evalCS(Readline.readline());
1329
if (result !== undefined) {
1430
print(result);
1531
}
@@ -18,34 +34,25 @@
1834
}
1935
}
2036
};
21-
// executes the coffee-script Ruby program to convert from CoffeeScript to Objective-J.
22-
// eventually this will hopefully be replaced by a JavaScript program.
23-
var coffeePath = File.path(module.path).dirname().dirname().join('bin', 'coffee-script');
37+
// Compile a given CoffeeScript file into JavaScript.
2438
exports.compileFile = function(path) {
2539
var coffee = OS.popen([coffeePath, "--print", "--no-wrap", path]);
26-
if (coffee.wait() !== 0) {
27-
system.stderr.print(coffee.stderr.read());
28-
throw new Error("coffee-script compile error");
29-
}
40+
checkForErrors(coffee);
3041
return coffee.stdout.read();
3142
};
43+
// Compile a string of CoffeeScript into JavaScript.
3244
exports.compile = function(source) {
3345
var coffee = OS.popen([coffeePath, "--eval", "--no-wrap"]);
3446
coffee.stdin.write(source).flush().close();
35-
if (coffee.wait() !== 0) {
36-
system.stderr.print(coffee.stderr.read());
37-
throw new Error("coffee-script compile error");
38-
}
47+
checkForErrors(coffee);
3948
return coffee.stdout.read();
4049
};
41-
// these two functions are equivalent to objective-j's objj_eval/make_narwhal_factory.
42-
// implemented as a call to coffee and objj_eval/make_narwhal_factory
43-
exports.cs_eval = function(source) {
44-
init();
50+
// Evaluating a string of CoffeeScript first compiles it externally.
51+
exports.evalCS = function(source) {
4552
return eval(exports.compile(source));
4653
};
47-
exports.make_narwhal_factory = function(path) {
48-
init();
54+
// Make a factory for the CoffeeScript environment.
55+
exports.makeNarwhalFactory = function(path) {
4956
var code = exports.compileFile(path);
5057
var factoryText = "function(require,exports,module,system,print){" + code + "/**/\n}";
5158
if (system.engine === "rhino") {
@@ -55,10 +62,4 @@
5562
return eval("(" + factoryText + ")");
5663
}
5764
};
58-
var init = function() {
59-
// make sure it's only done once
60-
init = function() {
61-
};
62-
return init;
63-
};
6465
})();

lib-js/coffee-script/loader.js

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,20 @@
11
(function(){
2+
3+
// This (javascript) file is generated from lib/coffee_script/narwhal/loader.cs
24
var coffeescript = null;
3-
var CoffeeScriptLoader = function() {
4-
var loader = {
5-
};
6-
var factories = {
7-
};
8-
loader.reload = function(topId, path) {
5+
var factories = {
6+
};
7+
var loader = {
8+
// Reload the coffee-script environment from source.
9+
reload: function(topId, path) {
910
coffeescript = coffeescript || require('coffee-script');
10-
// print("loading objective-j: " + topId + " (" + path + ")");
11-
factories[topId] = coffeescript.make_narwhal_factory(path);
12-
return factories[topId];
13-
};
14-
loader.load = function(topId, path) {
15-
if (!(factories.hasOwnProperty(topId))) {
16-
loader.reload(topId, path);
17-
}
11+
factories[topId] = coffeescript.makeNarwhalFactory(path);
1812
return factories[topId];
19-
};
20-
return loader;
13+
},
14+
// Ensure that the coffee-script environment is loaded.
15+
load: function(topId, path) {
16+
return factories[topId] = factories[topId] || this.reload(topId, path);
17+
}
2118
};
22-
require.loader.loaders.unshift([".cs", CoffeeScriptLoader()]);
19+
require.loader.loaders.unshift([".cs", loader]);
2320
})();
Lines changed: 31 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,59 @@
11
# This (javascript) file is generated from lib/coffee_script/narwhal/coffee-script.cs
22

3-
File: require('file')
4-
OS: require('os')
3+
# Executes the `coffee-script` Ruby program to convert from CoffeeScript
4+
# to Javascript. Eventually this will hopefully happen entirely within JS.
55

6+
# Require external dependencies.
7+
OS: require('os')
8+
File: require('file')
9+
Readline: require('readline')
10+
11+
# The path to the CoffeeScript Compiler.
12+
coffeePath: File.path(module.path).dirname().dirname().join('bin', 'coffee-script')
13+
14+
# Our general-purpose error handler.
15+
checkForErrors: coffeeProcess =>
16+
return true if coffeeProcess.wait() is 0
17+
system.stderr.print(coffeeProcess.stderr.read())
18+
throw new Error("coffee-script compile error").
19+
20+
# Run a simple REPL, round-tripping to the CoffeeScript compiler for every
21+
# command.
622
exports.run: args =>
723
args.shift()
824
return require(File.absolute(args[0])) if args.length
925

1026
while true
1127
try
1228
system.stdout.write('cs> ').flush()
13-
result: exports.cs_eval(require('readline').readline())
29+
result: exports.evalCS(Readline.readline())
1430
print(result) if result isnt undefined
1531
catch e
1632
print(e)...
1733

18-
# executes the coffee-script Ruby program to convert from CoffeeScript to Objective-J.
19-
# eventually this will hopefully be replaced by a JavaScript program.
20-
coffeePath: File.path(module.path).dirname().dirname().join('bin', 'coffee-script')
21-
34+
# Compile a given CoffeeScript file into JavaScript.
2235
exports.compileFile: path =>
2336
coffee: OS.popen([coffeePath, "--print", "--no-wrap", path])
24-
25-
if coffee.wait() isnt 0
26-
system.stderr.print(coffee.stderr.read())
27-
throw new Error("coffee-script compile error").
28-
37+
checkForErrors(coffee)
2938
coffee.stdout.read().
3039

40+
# Compile a string of CoffeeScript into JavaScript.
3141
exports.compile: source =>
32-
coffee: OS.popen([coffeePath, "--eval", "--no-wrap"])
33-
34-
coffee.stdin.write(source).flush().close()
35-
36-
if coffee.wait() isnt 0
37-
system.stderr.print(coffee.stderr.read())
38-
throw new Error("coffee-script compile error").
39-
40-
coffee.stdout.read().
42+
coffee: OS.popen([coffeePath, "--eval", "--no-wrap"])
43+
coffee.stdin.write(source).flush().close()
44+
checkForErrors(coffee)
45+
coffee.stdout.read().
4146

42-
# these two functions are equivalent to objective-j's objj_eval/make_narwhal_factory.
43-
# implemented as a call to coffee and objj_eval/make_narwhal_factory
44-
exports.cs_eval: source =>
45-
init()
46-
eval(exports.compile(source)).
47+
# Evaluating a string of CoffeeScript first compiles it externally.
48+
exports.evalCS: source =>
49+
eval(exports.compile(source)).
4750

48-
exports.make_narwhal_factory: path =>
49-
init()
51+
# Make a factory for the CoffeeScript environment.
52+
exports.makeNarwhalFactory: path =>
5053
code: exports.compileFile(path)
51-
5254
factoryText: "function(require,exports,module,system,print){" + code + "/**/\n}"
53-
5455
if system.engine is "rhino"
5556
Packages.org.mozilla.javascript.Context.getCurrentContext().compileFunction(global, factoryText, path, 0, null)
5657
else
5758
# eval requires parenthesis, but parenthesis break compileFunction.
5859
eval("(" + factoryText + ")")..
59-
60-
61-
init: =>
62-
# make sure it's only done once
63-
init: => ..

lib/coffee_script/narwhal/loader.cs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
11
# This (javascript) file is generated from lib/coffee_script/narwhal/loader.cs
22

33
coffeescript: null
4+
factories: {}
45

5-
CoffeeScriptLoader: =>
6-
loader: {}
7-
factories: {}
6+
loader: {
87

9-
loader.reload: topId, path =>
8+
# Reload the coffee-script environment from source.
9+
reload: topId, path =>
1010
coffeescript ||: require('coffee-script')
11-
# print("loading objective-j: " + topId + " (" + path + ")");
12-
factories[topId]: coffeescript.make_narwhal_factory(path).
11+
factories[topId]: coffeescript.makeNarwhalFactory(path).
1312

14-
loader.load: topId, path =>
15-
loader.reload(topId, path) unless factories.hasOwnProperty(topId)
16-
factories[topId].
13+
# Ensure that the coffee-script environment is loaded.
14+
load: topId, path =>
15+
factories[topId] ||: this.reload(topId, path).
1716

18-
loader.
17+
}
1918

20-
require.loader.loaders.unshift([".cs", CoffeeScriptLoader()])
19+
require.loader.loaders.unshift([".cs", loader])

0 commit comments

Comments
 (0)