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 c9df5b9..b4a3712 100644 --- a/require.js +++ b/require.js @@ -1,126 +1,74 @@ -/** - * @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 require, exports, ActiveXObject, Response, Server, WScript */ /*jslint evil:true */ +// Globals + (function () { +var modules = {}, paths = []; + +// Base setup +// ============================================================================= + // Don't do anything if require is already there if (typeof require === "function") { return; } -/** Global exports object */ +// Global exports object if (typeof exports === "undefined") { exports = {}; } -/** A print function for use in logging */ -function print() { +// 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"; } +exports.platform = platform; + +// print function +exports.print = function () { 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; -} +return exports; +})({}); -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 "."; } -} +// File +// ============================================================================= +modules.file = (function (exports) { -function canonicalize(path) { - return path.replace(/[^\/]+\/\.\.\//g, "").replace(/([^\.])\.\//g, "$1"). - replace(/^\.\//g, "").replace(/\/\/+/g, "/"); -} +return exports; +})({}); -//////////////////////////////////////////////////////////////////////////////// - -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 require; } -/** The global require function */ -require = function (name) { return _require(name, ".", true); }; +// ============================================================================= +require = Sandbox({ loader: Loader({ paths: paths }) }); -require.paths = ["lib"]; -require.loaded = {}; -require.extensions = [".js"]; - -})(); +})(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);