From c17c31e949d6fab37dc261e78b00159358de29f7 Mon Sep 17 00:00:00 2001 From: Jacob Beard Date: Sun, 8 Jan 2012 17:37:14 -0800 Subject: [PATCH 1/2] * Added .gitignore to ignore vim swap files. * Added support for 'spartan' shell environments. These are defined to be environments that expose global print() and load() functions, and that expose command-line arguments on top-level Arguments array. This now works for spidermonkey (Mozilla), jsc (Webkit), and v8 (Chrome) implementations. v8 must be patched to expose command-line arguments as top-level Arguments array. Rhino can also be considered a spartan shell environment. * Improved logger module to accept multiple arguments, like console.log. * Added quit() module for node, rhino, and spartan shell environments. --- .gitignore | 1 + build/jslib/env.js | 4 ++- build/jslib/logger.js | 24 ++++++++--------- build/jslib/node/print.js | 6 +---- build/jslib/node/quit.js | 4 +++ build/jslib/rhino/quit.js | 3 +++ build/jslib/spartan.js | 1 + build/jslib/spartan/args.js | 1 + build/jslib/spartan/file.js | 48 +++++++++++++++++++++++++++++++++ build/jslib/spartan/load.js | 1 + build/jslib/spartan/optimize.js | 1 + build/jslib/spartan/print.js | 1 + build/jslib/spartan/quit.js | 1 + build/jslib/x.js | 44 ++++++++++++++++++++++++------ dist.js | 3 ++- 15 files changed, 116 insertions(+), 27 deletions(-) create mode 100644 build/jslib/node/quit.js create mode 100644 build/jslib/rhino/quit.js create mode 120000 build/jslib/spartan.js create mode 120000 build/jslib/spartan/args.js create mode 100644 build/jslib/spartan/file.js create mode 120000 build/jslib/spartan/load.js create mode 120000 build/jslib/spartan/optimize.js create mode 120000 build/jslib/spartan/print.js create mode 120000 build/jslib/spartan/quit.js diff --git a/.gitignore b/.gitignore index c020ba2f..a80b4113 100644 --- a/.gitignore +++ b/.gitignore @@ -22,3 +22,4 @@ build/tests/override/node_modules build/tests/override/one-built.js tests/node/node_modules tests/node/embedded/node_modules +*.sw* diff --git a/build/jslib/env.js b/build/jslib/env.js index f43a2ab1..cd5b5ad5 100644 --- a/build/jslib/env.js +++ b/build/jslib/env.js @@ -22,6 +22,8 @@ env = 'node'; } else if (typeof window !== "undefined" && navigator && document) { env = 'browser'; + } else if (typeof load === 'function' && typeof print === 'function'){ + env = 'spartan'; } define({ @@ -44,4 +46,4 @@ }); } }); -}()); \ No newline at end of file +}()); diff --git a/build/jslib/logger.js b/build/jslib/logger.js index 57dea64b..0e9a913b 100644 --- a/build/jslib/logger.js +++ b/build/jslib/logger.js @@ -21,36 +21,36 @@ define(['env!env/print'], function (print) { this.level = level; }, - trace: function (message) { + trace: function () { if (this.level <= this.TRACE) { - this._print(message); + this._print(arguments); } }, - info: function (message) { + info: function () { if (this.level <= this.INFO) { - this._print(message); + this._print(arguments); } }, - warn: function (message) { + warn: function () { if (this.level <= this.WARN) { - this._print(message); + this._print(arguments); } }, - error: function (message) { + error: function () { if (this.level <= this.ERROR) { - this._print(message); + this._print(arguments); } }, - _print: function (message) { - this._sysPrint((this.logPrefix ? (this.logPrefix + " ") : "") + message); + _print: function (args) { + this._sysPrint(this.logPrefix ? (this.logPrefix + " ") : "", Array.prototype.slice.apply(args)); }, - _sysPrint: function (message) { - print(message); + _sysPrint: function (logPrefix,args) { + print.apply(this,[logPrefix].concat(args)); } }; diff --git a/build/jslib/node/print.js b/build/jslib/node/print.js index 2835926a..1dd06737 100644 --- a/build/jslib/node/print.js +++ b/build/jslib/node/print.js @@ -8,9 +8,5 @@ /*global define: false, console: false */ define(function () { - function print(msg) { - console.log(msg); - } - - return print; + return console.log; }); diff --git a/build/jslib/node/quit.js b/build/jslib/node/quit.js new file mode 100644 index 00000000..23eeac69 --- /dev/null +++ b/build/jslib/node/quit.js @@ -0,0 +1,4 @@ +define(function(){ + return process.exit; +}); + diff --git a/build/jslib/rhino/quit.js b/build/jslib/rhino/quit.js new file mode 100644 index 00000000..210c614a --- /dev/null +++ b/build/jslib/rhino/quit.js @@ -0,0 +1,3 @@ +define(function(){ + return quit; +}); diff --git a/build/jslib/spartan.js b/build/jslib/spartan.js new file mode 120000 index 00000000..6fcf24dc --- /dev/null +++ b/build/jslib/spartan.js @@ -0,0 +1 @@ +rhino.js \ No newline at end of file diff --git a/build/jslib/spartan/args.js b/build/jslib/spartan/args.js new file mode 120000 index 00000000..5053b135 --- /dev/null +++ b/build/jslib/spartan/args.js @@ -0,0 +1 @@ +../rhino/args.js \ No newline at end of file diff --git a/build/jslib/spartan/file.js b/build/jslib/spartan/file.js new file mode 100644 index 00000000..ca894483 --- /dev/null +++ b/build/jslib/spartan/file.js @@ -0,0 +1,48 @@ +//file API's may not be supported in spartan shell environments +define({ + getLineSeparator: function () { + }, + + exists: function (fileName) { + }, + + parent: function (fileName) { + }, + + normalize: function (fileName) { + }, + + isFile: function (path) { + }, + + isDirectory: function (path) { + }, + + absPath: function (fileObj) { + }, + + getFilteredFileList: function (/*String*/startDir, /*RegExp*/regExpFilters, /*boolean?*/makeUnixPaths, /*boolean?*/startDirIsJavaObject) { + }, + + copyDir: function (/*String*/srcDir, /*String*/destDir, /*RegExp?*/regExpFilter, /*boolean?*/onlyCopyNew) { + }, + + copyFile: function (/*String*/srcFileName, /*String*/destFileName, /*boolean?*/onlyCopyNew) { + }, + + renameFile: function (from, to) { + }, + + readFile: function (/*String*/path, /*String?*/encoding) { + }, + + saveUtf8File: function (/*String*/fileName, /*String*/fileContents) { + }, + + saveFile: function (/*String*/fileName, /*String*/fileContents, /*String?*/encoding) { + }, + + deleteFile: function (/*String*/fileName) { + } +}); + diff --git a/build/jslib/spartan/load.js b/build/jslib/spartan/load.js new file mode 120000 index 00000000..4d998436 --- /dev/null +++ b/build/jslib/spartan/load.js @@ -0,0 +1 @@ +../rhino/load.js \ No newline at end of file diff --git a/build/jslib/spartan/optimize.js b/build/jslib/spartan/optimize.js new file mode 120000 index 00000000..def93589 --- /dev/null +++ b/build/jslib/spartan/optimize.js @@ -0,0 +1 @@ +../node/optimize.js \ No newline at end of file diff --git a/build/jslib/spartan/print.js b/build/jslib/spartan/print.js new file mode 120000 index 00000000..b6e767b5 --- /dev/null +++ b/build/jslib/spartan/print.js @@ -0,0 +1 @@ +../rhino/print.js \ No newline at end of file diff --git a/build/jslib/spartan/quit.js b/build/jslib/spartan/quit.js new file mode 120000 index 00000000..1e99e390 --- /dev/null +++ b/build/jslib/spartan/quit.js @@ -0,0 +1 @@ +../rhino/quit.js \ No newline at end of file diff --git a/build/jslib/x.js b/build/jslib/x.js index fe64df6c..19d01dc7 100644 --- a/build/jslib/x.js +++ b/build/jslib/x.js @@ -98,7 +98,28 @@ var requirejs, require, define; commandOption = fileName.substring(1); fileName = process.argv[3]; } - } + } else if (typeof load === 'function' && typeof print === 'function'){ + env = 'spartan'; + + fileName = args[0]; + + if (fileName && fileName.indexOf('-') === 0) { + commandOption = fileName.substring(1); + fileName = args[1]; + } + + exec = eval; + + //Define a console.log for easier logging. Don't + //get fancy though. + if (typeof console === 'undefined') { + console = { + log: function () { + print.apply(undefined, arguments); + } + }; + } + } //INSERT require.js @@ -115,6 +136,9 @@ var requirejs, require, define; //INSERT build/jslib/node.js + } else if (env === 'spartan'){ + + //INSERT build/jslib/spartan.js } //Support a default file name to execute. Useful for hosted envs @@ -231,13 +255,17 @@ var requirejs, require, define; setBaseUrl(fileName); - if (exists(fileName)) { - exec(readFile(fileName), fileName); - } else { - showHelp(); + if(env === 'spartan'){ + load(fileName); + } else{ + if (exists(fileName)) { + exec(readFile(fileName), fileName); + } else { + showHelp(); + } } } -}((typeof console !== 'undefined' ? console : undefined), - (typeof Packages !== 'undefined' ? Array.prototype.slice.call(arguments, 0) : []), - (typeof readFile !== 'undefined' ? readFile : undefined))); +})((typeof console !== 'undefined' ? console : undefined), + ((typeof Packages !== 'undefined' || (typeof load === 'function' && typeof print === 'function'))? Array.prototype.slice.call(arguments, 0) : []), + (typeof readFile !== 'undefined' ? readFile : undefined)); diff --git a/dist.js b/dist.js index 1c551918..b10bfa14 100644 --- a/dist.js +++ b/dist.js @@ -22,7 +22,7 @@ var fs = require('fs'), loadRegExp = /\/\/INSERT ([\w\/\.]+)/g, moduleNameRegExp = /build\/jslib\/([\w\/\-]+)\.js$/, defRegExp = /define\s*\(/, - envs = ['node', 'rhino'], + envs = ['node', 'rhino', 'spartan'], //Update this list of files by running the optimizer against //build/jslib/opto.build.js, //but then remove any jslib/node entries and make sure there is @@ -36,6 +36,7 @@ var fs = require('fs'), 'env!env/file', 'build/jslib/lang.js', 'env!env/print', + 'env!env/quit', 'build/jslib/logger.js', 'build/jslib/blank.js', 'build/jslib/blank.js', From 1dd826d3bf5ee67991f3d21c66f87d6ed3f637a0 Mon Sep 17 00:00:00 2001 From: Jacob Beard Date: Wed, 11 Jan 2012 22:32:14 -0500 Subject: [PATCH 2/2] Added basic browser support for r.js. --- build/jslib/browser/args.js | 1 + build/jslib/browser/file.js | 1 + build/jslib/browser/load.js | 2 ++ build/jslib/browser/optimize.js | 1 + build/jslib/browser/print.js | 6 ++++++ build/jslib/browser/quit.js | 1 + build/jslib/x.js | 9 ++++++--- dist.js | 2 +- 8 files changed, 19 insertions(+), 4 deletions(-) create mode 100644 build/jslib/browser/args.js create mode 100644 build/jslib/browser/file.js create mode 100644 build/jslib/browser/load.js create mode 100644 build/jslib/browser/optimize.js create mode 100644 build/jslib/browser/print.js create mode 100644 build/jslib/browser/quit.js diff --git a/build/jslib/browser/args.js b/build/jslib/browser/args.js new file mode 100644 index 00000000..35452720 --- /dev/null +++ b/build/jslib/browser/args.js @@ -0,0 +1 @@ +define(function(){return function(){};}); diff --git a/build/jslib/browser/file.js b/build/jslib/browser/file.js new file mode 100644 index 00000000..35452720 --- /dev/null +++ b/build/jslib/browser/file.js @@ -0,0 +1 @@ +define(function(){return function(){};}); diff --git a/build/jslib/browser/load.js b/build/jslib/browser/load.js new file mode 100644 index 00000000..57324c8b --- /dev/null +++ b/build/jslib/browser/load.js @@ -0,0 +1,2 @@ +//nothing to do here +define(function(){return function(){};}); diff --git a/build/jslib/browser/optimize.js b/build/jslib/browser/optimize.js new file mode 100644 index 00000000..35452720 --- /dev/null +++ b/build/jslib/browser/optimize.js @@ -0,0 +1 @@ +define(function(){return function(){};}); diff --git a/build/jslib/browser/print.js b/build/jslib/browser/print.js new file mode 100644 index 00000000..63757243 --- /dev/null +++ b/build/jslib/browser/print.js @@ -0,0 +1,6 @@ +define(function(){ + return function(){ + console.log.apply(console,Array.prototype.slice.apply(arguments)); + }; +}); + diff --git a/build/jslib/browser/quit.js b/build/jslib/browser/quit.js new file mode 100644 index 00000000..35452720 --- /dev/null +++ b/build/jslib/browser/quit.js @@ -0,0 +1 @@ +define(function(){return function(){};}); diff --git a/build/jslib/x.js b/build/jslib/x.js index 19d01dc7..1e9ef9ee 100644 --- a/build/jslib/x.js +++ b/build/jslib/x.js @@ -98,6 +98,8 @@ var requirejs, require, define; commandOption = fileName.substring(1); fileName = process.argv[3]; } + } else if (typeof window !== "undefined" && navigator && document) { + env = 'browser'; } else if (typeof load === 'function' && typeof print === 'function'){ env = 'spartan'; @@ -119,7 +121,7 @@ var requirejs, require, define; } }; } - } + } //INSERT require.js @@ -249,7 +251,8 @@ var requirejs, require, define; //Just run an app //Load the bundled libraries for use in the app. - if (commandOption === 'lib') { + //browser loads libs by default + if (commandOption === 'lib' || env === 'browser') { loadLib(); } @@ -257,7 +260,7 @@ var requirejs, require, define; if(env === 'spartan'){ load(fileName); - } else{ + } else if (env !== 'browser'){ if (exists(fileName)) { exec(readFile(fileName), fileName); } else { diff --git a/dist.js b/dist.js index b10bfa14..674fbd90 100644 --- a/dist.js +++ b/dist.js @@ -22,7 +22,7 @@ var fs = require('fs'), loadRegExp = /\/\/INSERT ([\w\/\.]+)/g, moduleNameRegExp = /build\/jslib\/([\w\/\-]+)\.js$/, defRegExp = /define\s*\(/, - envs = ['node', 'rhino', 'spartan'], + envs = ['node', 'rhino', 'spartan', 'browser'], //Update this list of files by running the optimizer against //build/jslib/opto.build.js, //but then remove any jslib/node entries and make sure there is