Created
March 30, 2010 23:17
-
-
Save konobi/349731 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var require; | |
(function(){ | |
var module = {}; | |
require = function(module_name, stack){ | |
if(!stack) stack = []; | |
var self = arguments.callee; | |
var resolved = resolve(module_name, stack); | |
var [use_string, breakup] = [resolved.identifier, resolved.breakup]; | |
if(self.moduleCache[use_string]) return self.moduleCache[use_string].exports; | |
self.moduleCache[use_string] = { id: use_string, exports: {} }; | |
if(breakup.length > 1){ | |
try { | |
system.filesystem.get(self.path + use_string + ".js"); | |
} catch (e) { | |
// If relative file doesn't exist, fall back to absolute | |
var resolved_again = resolve(breakup[breakup.length -1], stack); | |
[use_string, breakup] = [resolved_again.identifier, resolved_again.breakup]; | |
} | |
} | |
var new_require = function(new_module_name){ | |
return require(new_module_name, stack.concat( breakup.slice(0, -1) )); | |
}; | |
var func = load(use_string); | |
func.apply(func, [new_require, module, self.moduleCache[use_string].exports]); | |
return self.moduleCache[use_string].exports; | |
}; | |
require.moduleCache = {}; | |
require.path = ''; | |
var resolve = function(module_name, stack) { | |
var breakup = module_name.split('/'); | |
switch(breakup[0]){ | |
case '.': | |
breakup.shift(); break; | |
case '..': | |
if(stack.length == 0) throw new Error("Unable to load module '"+module_name+"', already at top level"); | |
stack.pop(); breakup.shift(); break; | |
default: | |
stack = []; break; | |
} | |
var [current_directory, relative_file] = [stack.join('/'), breakup.join('/')]; | |
var use_string = current_directory ? (current_directory + '/' + relative_file) : relative_file; | |
return { 'identifier': use_string, 'breakup': breakup }; | |
}; | |
var load = function(use_string){ | |
var load_this = require.path + use_string + '.js'; | |
var func = new Function(["require", "module", "exports"], system.filesystem.get(load_this).contents); | |
return func; | |
}; | |
})(); | |
var output = ''; | |
var print = function(message, label){ | |
output += " ====> "+message+" --- "+label+"\n"; | |
} | |
var tests = [ | |
'absolute', | |
'determinism', | |
'exactExports', | |
'hasOwnProperty', | |
'method', | |
'missing', | |
'nested', | |
'reflexive', | |
'relative', | |
'transitive', | |
'cyclic', | |
'monkeys' | |
]; | |
function main(req){ | |
try { | |
tests.forEach(function(test){ | |
require.moduleCache = {}; output += "\n"; | |
output += "#### TEST: "+test+"\n"; | |
require.path = 'interoperablejs/compliance/'+test+'/'; | |
require('program'); | |
}); | |
} catch(e) { | |
print("ERROR", e); | |
}; | |
return [200, ['Content-Type', 'text/plain'], output]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is currently only runnable on a development branch deployment.
Hopefully I'll be able to pull it in as a generic option shortly.