From a1339df793ee293078f98915bd3335544f883fe0 Mon Sep 17 00:00:00 2001 From: Nathan L Smith Date: Sun, 2 May 2010 23:47:53 -0500 Subject: [PATCH 1/2] set up framework --- require.js | 149 +++++++++++++++-------------------------------------- 1 file changed, 42 insertions(+), 107 deletions(-) diff --git a/require.js b/require.js index c9df5b9..1006705 100644 --- a/require.js +++ b/require.js @@ -1,126 +1,61 @@ -/** - * @fileOverview An implentation of JavaScript modules for use in an ASP or - * WScript environment. This implementation is based on the one used in - * Narhwal - * - * @see ServerJS/Modules/SecurableModules - * @see Narwhal - * @author Nathan L Smith - * @date February 21, 2009 - */ - -/*global ActiveXObject, Response, Server, WScript, exports, require */ +// commonjscript require + +/*global ActiveXObject, Response, Server, WScript */ /*jslint evil:true */ -(function () { +(function (global) { + +var platform, fs = {}, paths = []; + +// Base setup +// ============================================================================= // Don't do anything if require is already there -if (typeof require === "function") { return; } +if (typeof global.require === "function") { return; } -/** Global exports object */ -if (typeof exports === "undefined") { exports = {}; } +// Global exports object +if (typeof global.exports === "undefined") { global.exports = {}; } -/** A print function for use in logging */ +// WScript or ASP? +if (typeof Response === "object" && typeof Response.write !== "undefined") { + platform = "asp"; +} else if (typeof WScript === "object") { platform = "wscript"; +} else { platform = "unknown"; } + +// alias for Server.mapPath on ASP +function m(path) { return platform === "asp" ? Server.mapPath(path) : path; } + +// print function function print() { var out = Array.prototype.slice.call(arguments).join(" "); - if (typeof WScript === "object") { - WScript.echo(out); - } else if (Response && typeof Response.write !== "undefined") { - Response.write(out + "
"); - } + if (platform === "wscript") { WScript.echo(out); } + else if (platform === "asp") { Response.write(out + "
"); } } -//////////////////////////////////////////////////////////////////////////////// -// Internal functions for file manipulation -//////////////////////////////////////////////////////////////////////////////// - -/** - * An implentation of readFile, which opens a file from the filesystem and - * returns its contents as a string - * @private - */ -function readFile(fileName) { - var contents, - fileSystem = new ActiveXObject("Scripting.FileSystemObject"), - mapPath = function mapPath(path) { - var isASP = typeof Server === "object" && - typeof Server.mapPath !== "undefined"; - return isASP ? Server.mapPath(path) : path; - }; - fileName = mapPath(fileName); - - if (fileSystem.fileExists(fileName)) { - try { // JScript will throw an error if the file is empty - contents = fileSystem.openTextFile(fileName).readAll(); - } catch (e) { contents = ""; } - } else { throw new Error("File " + fileName + " does not exist"); } - return contents; -} +// Filesystem +// ============================================================================= -function dirname(path) { - var raw = String(path), - match = raw.match(/^(.*)\/[^\/]+\/?$/); - if (match && match[1]) { return match[1]; } - else if (raw.charAt(0) == "/") { return "/"; } - else { return "."; } -} +fs = { + // Filesystem object + o: new ActiveXObject("Scripting.FileSystemObject") -function canonicalize(path) { - return path.replace(/[^\/]+\/\.\.\//g, "").replace(/([^\.])\.\//g, "$1"). - replace(/^\.\//g, "").replace(/\/\/+/g, "/"); -} +}; -//////////////////////////////////////////////////////////////////////////////// - -function _require(name, parentPath, loadOnce) { - var result, pwd, extensions, paths, path, searchDirectory, ext; - if (name.charAt(0) === "/") { - result = _attemptLoad(name, name, loadOnce); - if (result) { return result; } - } else { - pwd = dirname(parentPath); - extensions = (/\.\w+$/).test(name) ? [""] : require.extensions; - paths = ["."].concat(require.paths); - for (var j = 0; j < extensions.length; j++) { - ext = extensions[j]; - for (var i = 0; i < paths.length; i++) { - searchDirectory = (paths[i] === ".") ? pwd : paths[i]; - path = searchDirectory + "/" + name + ext; - result = _attemptLoad(name, path, loadOnce); - if (result) { return result; } - } - } - } - throw new Error("couldn't find " + name); -} +// Loader +// ============================================================================= + +function Loader() { -function _requireFactory(path, loadOnce) { - return function(name) { - return _require(name, path, loadOnce || false); - }; } -function _attemptLoad(name, path, loadOnce) { - path = canonicalize(path); - var module, moduleCode; - - // see if the module is already loaded - if (require.loaded[path] && loadOnce) { return require.loaded[path]; } - try { moduleCode = readFile(path); } catch (e) {} - if (typeof moduleCode !== "undefined") { - require.loaded[path] = {}; - module = new Function("require", "exports", moduleCode); - module(_requireFactory(path, true), require.loaded[path]); - return require.loaded[path]; - } - return false; +// Sandbox +// ============================================================================= + +function Sandbox() { + return function () {}; } -/** The global require function */ -require = function (name) { return _require(name, ".", true); }; +// ============================================================================ +global.require = Sandbox({ loader: Loader({ paths: paths }) }); -require.paths = ["lib"]; -require.loaded = {}; -require.extensions = [".js"]; - -})(); +})(this); From f1e62343b84de0545b13a606b1f070c7692decf4 Mon Sep 17 00:00:00 2001 From: Nathan L Smith Date: Tue, 4 May 2010 16:24:55 -0500 Subject: [PATCH 2/2] Started rewrite --- lib/browser/xhr.js | 6 ------ lib/system.js | 26 ----------------------- package.json | 2 +- require.js | 51 +++++++++++++++++++++++++++++----------------- test/test.js | 24 +++++++++++++--------- 5 files changed, 47 insertions(+), 62 deletions(-) delete mode 100644 lib/browser/xhr.js delete mode 100644 lib/system.js diff --git a/lib/browser/xhr.js b/lib/browser/xhr.js deleted file mode 100644 index 9574c8e..0000000 --- a/lib/browser/xhr.js +++ /dev/null @@ -1,6 +0,0 @@ -// commonjscript XMLHttpRequest client -// @see http://wiki.commonjs.org/wiki/HTTP_Client/B - -exports.XMLHttpRequest = function () { - return new ActiveXObject("MSXML2.ServerXMLHTTP"); -}; diff --git a/lib/system.js b/lib/system.js deleted file mode 100644 index e461a91..0000000 --- a/lib/system.js +++ /dev/null @@ -1,26 +0,0 @@ -// commonjscript system module - -/*global Response, WScript */ - -var platform; - -exports.engine = "jscript"; - -// The platform is either wscript (cli) or asp -if (typeof WScript === "object") { - platform = "wscript"; -} else if (typeof Response === "object" && - typeof Response.write !== "undefined") { - platform = "asp"; -} else { - platform = "unknown"; -} - -// print function -exports.print = function () { - var out = Array.prototype.slice.call(arguments).join(" "); - if (platform === "wscript") { WScript.echo(out); } - else if (platform === "asp") { Response.write(out + "
"); } -}; - -exports.stdio = { print: exports.print }; diff --git a/package.json b/package.json index c39920b..56be46b 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "commonjscript", "description": "An implementation of the CommonJS API for Classic ASP and Windows Script Host", - "version": "1.0.0", + "version": "0.0.0", "maintainers": [{ "name": "Nathan L Smith", "email": "nlloyds@gmail.com", diff --git a/require.js b/require.js index 1006705..b4a3712 100644 --- a/require.js +++ b/require.js @@ -1,45 +1,58 @@ // commonjscript require -/*global ActiveXObject, Response, Server, WScript */ +/*global require, exports, ActiveXObject, Response, Server, WScript */ /*jslint evil:true */ -(function (global) { +// Globals -var platform, fs = {}, paths = []; +(function () { + +var modules = {}, paths = []; // Base setup // ============================================================================= // Don't do anything if require is already there -if (typeof global.require === "function") { return; } +if (typeof require === "function") { return; } // Global exports object -if (typeof global.exports === "undefined") { global.exports = {}; } +if (typeof exports === "undefined") { exports = {}; } + +// Stand-in for require +require = function (id) { return modules[id]; }; + +// System +// ============================================================================= +modules.system = (function (exports) { + +var platform; + +exports.engine = "jscript"; +exports.os = "windows"; // WScript or ASP? if (typeof Response === "object" && typeof Response.write !== "undefined") { platform = "asp"; } else if (typeof WScript === "object") { platform = "wscript"; } else { platform = "unknown"; } - -// alias for Server.mapPath on ASP -function m(path) { return platform === "asp" ? Server.mapPath(path) : path; } +exports.platform = platform; // print function -function print() { +exports.print = function () { var out = Array.prototype.slice.call(arguments).join(" "); if (platform === "wscript") { WScript.echo(out); } else if (platform === "asp") { Response.write(out + "
"); } -} +}; -// Filesystem -// ============================================================================= +return exports; +})({}); -fs = { - // Filesystem object - o: new ActiveXObject("Scripting.FileSystemObject") +// File +// ============================================================================= +modules.file = (function (exports) { -}; +return exports; +})({}); // Loader // ============================================================================= @@ -52,10 +65,10 @@ function Loader() { // ============================================================================= function Sandbox() { - return function () {}; + return require; } -// ============================================================================ -global.require = Sandbox({ loader: Loader({ paths: paths }) }); +// ============================================================================= +require = Sandbox({ loader: Loader({ paths: paths }) }); })(this); diff --git a/test/test.js b/test/test.js index 26d619e..98d3224 100644 --- a/test/test.js +++ b/test/test.js @@ -1,7 +1,6 @@ // commonjscript test runner -require.paths.push("../lib"); // to get system module - +/* var print = require("system").print, modulesSpecVersion = "1.0"; tests = ["absolute", @@ -15,13 +14,18 @@ var print = require("system").print, "nested", "relative", "transitive"]; +*/ -function run(test) { - require.paths = ["commonjs/tests/modules/" + modulesSpecVersion + "/" + - test]; - print("-- " + test + "--") - require("program"); - print(""); -} +//function run(test) { + //require.paths = ["commonjs/tests/modules/" + modulesSpecVersion + "/" + + //test]; + //print("-- " + test + "--") + //require("program"); + //print(""); +//} -for (var i = 0; i < tests.length; i += 1) { run(tests[i]); } +//for (var i = 0; i < tests.length; i += 1) { run(tests[i]); } +//var print = require("system").print; +var print = require("system").print; +print("hi"); +//WScript.echo(typeof require("system").print);