From 06261559fc7b9a2eb61cf0faca42be26e4e327cf Mon Sep 17 00:00:00 2001 From: Francisco Ryan Tolmasky I Date: Thu, 28 Jan 2010 14:19:23 -0800 Subject: [PATCH] Improvements for CommonJS. Reviewed by me. --- Objective-J/DebugOptions.js | 6 +++++ Objective-J/Executable.js | 41 +++++++++++++++++++++++++------ Objective-J/HTTPRequest.js | 10 +++++--- Objective-J/Scope.js | 35 ++++++++++++-------------- Objective-J/StaticResourceNode.js | 40 +++++++++++++++++++++--------- 5 files changed, 89 insertions(+), 43 deletions(-) diff --git a/Objective-J/DebugOptions.js b/Objective-J/DebugOptions.js index affc401b4..66083b0d4 100644 --- a/Objective-J/DebugOptions.js +++ b/Objective-J/DebugOptions.js @@ -2,3 +2,9 @@ #define DEPENDENCY_LOGGING 1 #define STATIC_RESOURCE_LOGGING 1 #define EXECUTION_LOGGING 1 + +#if BROWSER +#define CPLog console.log +#else +#define CPLog print +#endif \ No newline at end of file diff --git a/Objective-J/Executable.js b/Objective-J/Executable.js index 4ae4972fb..5414d25b0 100644 --- a/Objective-J/Executable.js +++ b/Objective-J/Executable.js @@ -73,20 +73,43 @@ Executable.prototype.path = function() Executable.prototype.functionParameters = function() { - return exportedNames().concat("objj_executeFile", "objj_importFile", "__OBJJ_BUNDLE__"); + var functionParameters = exportedNames().concat("objj_executeFile", "objj_importFile"); + +#ifdef COMMONJS + functionParameters = functionParameters.concat("require", "exports", "module", "system", "print"); +#endif + + return functionParameters; } Executable.prototype.functionArguments = function() { - var path = this.path(); + var path = this.path() + functionArguments = exportedValues().concat(fileExecuterForPath(path), fileImporterForPath(path)); - return exportedValues().concat(fileExecuterForPath(path), fileImporterForPath(path)); +#ifdef COMMONJS + functionArguments = functionArguments.concat(Executable.commonJSArguments()); +#endif + + return functionArguments; } +#ifdef COMMONJS +Executable.setCommonJSArguments = function() +{ + this._commonJSArguments = Array.prototype.slice.call(arguments); +} + +Executable.commonJSArguments = function() +{ + return this._commonJSArguments || []; +} +#endif + Executable.prototype.execute = function() { #if EXECUTION_LOGGING - console.log("EXECUTION: " + this.path()); + CPLog("EXECUTION: " + this.path()); #endif var oldContextBundle = CONTEXT_BUNDLE; @@ -121,7 +144,7 @@ var globalIteration = 0; Executable.prototype.loadFileDependencies = function() { #if DEPENDENCY_LOGGING - console.log("DEPENDENCY: initiated by " + this.scope()); + CPLog("DEPENDENCY: initiated by " + this.scope()); #endif if (this._fileDependencyLoadStatus !== ExecutableUnloadedFileDependencies) return; @@ -203,12 +226,12 @@ Executable.prototype.loadFileDependencies = function() return; #else { - console.log("DEPENDENCY: more dependencies: "); - console.log(incompleteFileExecutableSearches.toString()); + CPLog("DEPENDENCY: more dependencies: "); + CPLog(incompleteFileExecutableSearches.toString()); return; } - console.log("DEPENDENCY: Ended"); + CPLog("DEPENDENCY: Ended"); #end var fileExecutablesNeedingEventDispatch = []; @@ -251,3 +274,5 @@ Executable.prototype.removeEventListener = function(/*String*/ anEventName, /*Fu { this._eventDispatcher.removeEventListener(anEventName, aListener); } + +exports.Executable = Executable; diff --git a/Objective-J/HTTPRequest.js b/Objective-J/HTTPRequest.js index 046a9857b..b5a3eae39 100644 --- a/Objective-J/HTTPRequest.js +++ b/Objective-J/HTTPRequest.js @@ -170,7 +170,7 @@ function determineAndDispatchHTTPRequestEvents(/*HTTPRequest*/ aRequest) function FileRequest(/*String*/ aFilePath, onsuccess, onfailure) { -#if BROWSER +#ifdef BROWSER var request = new HTTPRequest(); request.onsuccess = onsuccess; @@ -179,14 +179,16 @@ function FileRequest(/*String*/ aFilePath, onsuccess, onfailure) request.open("GET", aFilePath, YES); request.send(""); #else - if (!require("file").exists(aFilePath)) + if (!FILE.exists(aFilePath)) return onfailure(); + this._responseText = FILE.read(aFilePath, { charset: "UTF-8" }); + onsuccess({ type:"success", request:this }); #endif } -#if COMMONJS +#ifdef COMMONJS FileRequest.prototype.responseText = function() { return this._responseText; @@ -199,6 +201,6 @@ FileRequest.prototype.responseXML = function() FileRequest.prototype.responsePropertyList = function() { - return PropertyList.createFromString(responseText); + return PropertyList.createFromString(this.responseText()); } #endif diff --git a/Objective-J/Scope.js b/Objective-J/Scope.js index da119347e..0608efa55 100644 --- a/Objective-J/Scope.js +++ b/Objective-J/Scope.js @@ -73,9 +73,6 @@ function fileExecuterForPath(/*String*/ referencePath) if (0 && !fileExecutable.hasLoadedFileDependencies()) throw "No executable loaded for file at path " + aPath; - //console.log("executing " + aPath); - //console.log(aPath + " " + (isLocal ? 1 : 0) + " " + executable.isLoaded() + " " + executable.hasLoadedDependencies()); - fileExecutable.execute(shouldForce); } @@ -103,33 +100,31 @@ function fileImporterForPath(/*String*/ referencePath) var fileExecutableSearch = new FileExecutableSearch(aPath, isLocal); function searchComplete(/*FileExecutableSearch*/ aFileExecutableSearch) - {console.log("search complete: " + aFileExecutableSearch); + { var fileExecutable = aFileExecutableSearch.result(), - fileExecuter = fileExecuterForPath(referencePath); - - if (!fileExecutable.hasLoadedFileDependencies()) - { - fileExecutable.loadFileDependencies(); - - fileExecutable.addEventListener("dependenciesload", function() + fileExecuter = fileExecuterForPath(referencePath), + executeAndCallback = function () { fileExecuter(aPath, isLocal); - aCallback(); - }); + + if (aCallback) + aCallback(); + } + + if (!fileExecutable.hasLoadedFileDependencies()) + { + fileExecutable.addEventListener("dependenciesload", executeAndCallback); + fileExecutable.loadFileDependencies(); } else - { - fileExecuter(aPath, isLocal); - aCallback(); - } + executeAndCallback(); } if (fileExecutableSearch.isComplete()) - searchComplete(search); + searchComplete(fileExecutableSearch); else fileExecutableSearch.addEventListener("complete", function(/*Event*/ anEvent) { - console.log("completed..."); searchComplete(anEvent.fileExecutableSearch); }); } @@ -139,3 +134,5 @@ function fileImporterForPath(/*String*/ referencePath) return cachedImporter; } + +exports.fileImporterForPath = fileImporterForPath; diff --git a/Objective-J/StaticResourceNode.js b/Objective-J/StaticResourceNode.js index 55ed11902..5f73d9182 100644 --- a/Objective-J/StaticResourceNode.js +++ b/Objective-J/StaticResourceNode.js @@ -1,5 +1,8 @@ var FILE = +#ifdef COMMONJS +require("file"); +#else { absolute: function(/*String*/ aPath) { @@ -20,11 +23,7 @@ var FILE = cwd: function() { -#if BROWSER return FILE.dirname(window.location.pathname); -#else - return require("file").cwd(); -#endif }, normal: function(/*String*/ aPath) @@ -69,7 +68,12 @@ var FILE = dirname: function(/*String*/ aPath) { - var components = FILE.split(FILE.normal(aPath)); + var aPath = FILE.normal(aPath); + + if (aPath === "/") + return aPath; + + var components = FILE.split(aPath); return FILE.join.apply(FILE, components.slice(0, components.length - 1)); }, @@ -89,6 +93,7 @@ var FILE = return FILE.normal(aPath).split("/"); } } +#endif StaticResourceNode.FileType = 0; StaticResourceNode.DirectoryType = 1; @@ -182,7 +187,7 @@ StaticResourceNode.prototype.write = function(/*String*/ aString) } StaticResourceNode.prototype.resolveAsFile = function() -{console.log("NOW LOOKING FOR FILE!"); +{ var self = this; function onsuccess(/*anEvent*/ anEvent) @@ -194,7 +199,7 @@ StaticResourceNode.prototype.resolveAsFile = function() } function onfailure() - {console.log("FIALE"); + { self._type = StaticResourceNode.NotFoundType; resolveStaticResourceNode(self, YES); @@ -205,9 +210,16 @@ StaticResourceNode.prototype.resolveAsFile = function() StaticResourceNode.prototype.resolveSubPath = function(/*String*/ aPath, /*Type*/ aType, /*Function*/ aCallback) { - var components = FILE.split(FILE.normal(FILE.join(this.path(), aPath))), + aPath = FILE.normal(aPath); + + if (aPath === "/") + return aCallback(rootNode); + + if (!FILE.isAbsolute(aPath)) + aPath = FILE.join(this.path(), aPath); + + var components = FILE.split(aPath), index = this === this.rootNode() ? 1 : FILE.split(this.path()).length; -//console.log("request for " + FILE.normal(this.path() + "/" + aPath)); resolvePathComponents(this, aType, components, index, aCallback); } @@ -221,7 +233,8 @@ function resolvePathComponents(/*StaticResourceNode*/ startNode, /*Type*/aType, { var name = components[index], childNode = parentNode._childNodes[name]; -//console.log(name + " of " + parentNode.name() + " " + (childNode && childNode.name())); +//CPLog(index + " " + components + ":" + (childNode && childNode.isResolved()) + ":"); +//CPLog(name + " of " + parentNode.name() + " " + (childNode && childNode.name())); //console.log(parentNode._childNodes); // + "(" + components + ")" + " " + index + "/" + count + ":" + (childNode && childNode.name()) +">" + (childNode ? 1:0) + " " + (childNode && childNode.isResolved())); if (!childNode) @@ -247,7 +260,10 @@ function resolvePathComponents(/*StaticResourceNode*/ startNode, /*Type*/aType, { childNode = new StaticResourceNode(name, parentNode, StaticResourceNode.FileType, NO); - childNode.addEventListener("resolve", continueResolution); + if (childNode.isResolved()) + continueResolution(); + else + childNode.addEventListener("resolve", continueResolution); } return; @@ -258,7 +274,7 @@ function resolvePathComponents(/*StaticResourceNode*/ startNode, /*Type*/aType, // If we've already determined that this file doesn't exist... if (childNode.isNotFound()) return aCallback(null, new Error("File not found: " + components.join("/"))); - + // If we have no more path components... if (index + 1 >= count) return aCallback(childNode);