Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added Node support

  • Loading branch information...
commit 50d0e8aea05f8560d1e98bbdb26a8f4c4a1dfa95 1 parent 49122cc
Alex Young authored
Showing with 57 additions and 19 deletions.
  1. +8 −2 README.textile
  2. +45 −16 riot.js
  3. +4 −1 test.js
View
10 README.textile
@@ -1,6 +1,6 @@
This is a JavaScript implementation of "Riot":http://github.com/thumblemonks/riot/.
-It will run in a browser or in Rhino.
+It will run in a browser, Rhino, or Node.
h3. Example
@@ -62,7 +62,13 @@ h3. Writing a test for a browser AND interpreter
My current pattern looks like this:
<pre>
-load('riot.js');
+var Riot;
+if (typeof load !== 'undefined') {
+ load('riot.js');
+} else if (typeof require !== 'undefined') {
+ Riot = require('./riot').Riot;
+}
+
Riot.require('../turing.core.js');
Riot.require('../turing.oo.js');
Riot.require('fixtures/example_classes.js');
View
61 riot.js
@@ -1,6 +1,6 @@
/*jslint white: false plusplus: false onevar: false browser: true evil: true*/
-/*global window: true*/
-(function(global) {
+/*riotGlobal window: true*/
+(function(riotGlobal) {
var Riot = {
results: [],
contexts: [],
@@ -19,6 +19,12 @@
java.lang.System.exit(Riot.exitCode);
break;
+ case 'node':
+ Riot.formatter = new Riot.Formatters.Text();
+ Riot.runAndReport(tests);
+ // TODO: exit with exit code from riot
+ break;
+
case 'non-browser-interpreter':
Riot.formatter = new Riot.Formatters.Text();
Riot.runAndReport(tests);
@@ -115,6 +121,10 @@
case 'non-browser-interpreter':
load(arguments[0]);
break;
+ case 'node':
+ // Evaluate the required code in the global context, like load() would
+ global.eval.call(global, Riot.node.fs.readFileSync(arguments[0]).toString());
+ break;
case 'browser':
var script = document.createElement('script'),
head = document.getElementsByTagName('head');
@@ -150,15 +160,32 @@
return this.env;
}
- if (typeof XPCOMCore !== 'undefined') {
- return 'xpcomcore';
- } else if (typeof window === 'undefined' && typeof java !== 'undefined') {
- return 'rhino';
- } else if (typeof window === 'undefined') {
- return 'non-browser-interpreter';
- } else {
- return 'browser';
- }
+ this.env = (function() {
+ if (typeof XPCOMCore !== 'undefined') {
+ Riot.puts = print;
+ return 'xpcomcore';
+ } else if (typeof window === 'undefined' && typeof java !== 'undefined') {
+ Riot.puts = print;
+ return 'rhino';
+ } else if (typeof exports !== 'undefined') {
+ // TODO: Node should be checked more thoroughly
+ Riot.node = {
+ fs: require('fs'),
+ sys: require('sys')
+ }
+
+ Riot.puts = Riot.node.sys.puts;
+
+ return 'node';
+ } else if (typeof window === 'undefined') {
+ Riot.puts = print;
+ return 'non-browser-interpreter';
+ } else {
+ return 'browser';
+ }
+ })();
+
+ return this.env;
},
runAndReport: function(tests) {
@@ -307,7 +334,7 @@
Text: function() {
function display(text) {
- print(text);
+ Riot.puts(text);
}
this.line = function(text) {
@@ -584,11 +611,13 @@
}
};
- if (typeof global.Riot === 'undefined') {
- global.Riot = Riot;
+ if (typeof exports !== 'undefined') {
+ exports.Riot = Riot;
+ } else if (typeof riotGlobal.Riot === 'undefined') {
+ riotGlobal.Riot = Riot;
- if (typeof global.load === 'undefined') {
- global.load = function() { };
+ if (typeof riotGlobal.load === 'undefined') {
+ riotGlobal.load = function() { };
}
}
})(typeof window === 'undefined' ? this : window);
View
5 test.js
@@ -1,5 +1,8 @@
-if (typeof load != 'undefined') {
+var Riot;
+if (typeof load !== 'undefined') {
load('riot.js');
+} else if (typeof require !== 'undefined') {
+ Riot = require('./riot').Riot;
}
Riot.run(function() {
Please sign in to comment.
Something went wrong with that request. Please try again.