From d8b76f656ea7b1f85d8a35a17870fb8af8d13d56 Mon Sep 17 00:00:00 2001 From: Jeremy Ruston Date: Sat, 23 Mar 2013 16:15:45 +0000 Subject: [PATCH] Rearranging boot.js --- core/boot.js | 82 ++++++++++++++++++++++++++-------------------------- 1 file changed, 41 insertions(+), 41 deletions(-) diff --git a/core/boot.js b/core/boot.js index 1757facc6..37b4ea670 100644 --- a/core/boot.js +++ b/core/boot.js @@ -852,6 +852,47 @@ $tw.boot.decryptEncryptedTiddlers = function(callback) { callback(); }; +/* +Execute the module named 'moduleName'. The name can optionally be relative to the module named 'moduleRoot' +*/ +$tw.modules.execute = function(moduleName,moduleRoot) { + var name = moduleRoot ? $tw.utils.resolvePath(moduleName,moduleRoot) : moduleName, + moduleInfo = $tw.modules.titles[name], + tiddler = $tw.wiki.getTiddler(name), + sandbox = { + module: moduleInfo, + exports: {}, + console: console, + process: process, + $tw: $tw, + require: function(title) { + return $tw.modules.execute(title,name); + } + }; + if(!moduleInfo) { + // If we don't have a module with that name, let node.js try to find it + return require(moduleName); + } + // Execute the module if we haven't already done so + if(!moduleInfo.exports) { + try { + // Check the type of the definition + if(typeof moduleInfo.definition === "string") { // String + vm.runInNewContext(moduleInfo.definition,sandbox,tiddler.fields.title); + moduleInfo.exports = sandbox.exports; + } else if(typeof moduleInfo.definition === "function") { // Function + moduleInfo.exports = moduleInfo.definition(moduleInfo,sandbox.require,moduleInfo.exports); + } else { // Object + moduleInfo.exports = moduleInfo.definition; + } + } catch(e) { + throw "Error executing boot module " + name + ":\n" + e; + } + } + // Return the exports of the module + return moduleInfo.exports; +}; + /* Load the tiddlers contained in a particular file (and optionally extract fields from the accompanying .meta file) */ @@ -950,47 +991,6 @@ $tw.loadBundleFolder = function(filepath,excludeRegExp) { } : null; }; -/* -Execute the module named 'moduleName'. The name can optionally be relative to the module named 'moduleRoot' -*/ -$tw.modules.execute = function(moduleName,moduleRoot) { - var name = moduleRoot ? $tw.utils.resolvePath(moduleName,moduleRoot) : moduleName, - moduleInfo = $tw.modules.titles[name], - tiddler = $tw.wiki.getTiddler(name), - sandbox = { - module: moduleInfo, - exports: {}, - console: console, - process: process, - $tw: $tw, - require: function(title) { - return $tw.modules.execute(title,name); - } - }; - if(!moduleInfo) { - // If we don't have a module with that name, let node.js try to find it - return require(moduleName); - } - // Execute the module if we haven't already done so - if(!moduleInfo.exports) { - try { - // Check the type of the definition - if(typeof moduleInfo.definition === "string") { // String - vm.runInNewContext(moduleInfo.definition,sandbox,tiddler.fields.title); - moduleInfo.exports = sandbox.exports; - } else if(typeof moduleInfo.definition === "function") { // Function - moduleInfo.exports = moduleInfo.definition(moduleInfo,sandbox.require,moduleInfo.exports); - } else { // Object - moduleInfo.exports = moduleInfo.definition; - } - } catch(e) { - throw "Error executing boot module " + name + ":\n" + e; - } - } - // Return the exports of the module - return moduleInfo.exports; -}; - $tw.loadTiddlers = function() { // On the server, we load tiddlers from specified folders var folders = [