Created
January 22, 2012 22:31
-
-
Save dherman/1659153 to your computer and use it in GitHub Desktop.
require shim for Narcissus to run in SpiderMonkey
This file contains hidden or 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
// main :: ([string], [string]) -> Narcissus | |
function main(moduleNames, moduleSources) { | |
var Narcissus = {__proto__: null}; | |
// make the global object available to the interpreter via require("./global") | |
Narcissus.global = (new Function("return this"))(); | |
moduleNames.forEach(function(name) { | |
Narcissus[name] = {}; | |
}); | |
function require(path) { | |
var name = path.replace(/^\.\//, ""); | |
if (!(name in Narcissus)) | |
throw new Error("unknown module: " + path); | |
return Narcissus[name]; | |
} | |
moduleNames.forEach(function(name, i) { | |
var exec = new Function("require", "exports", moduleSources[i]); | |
var mod = Narcissus[name]; | |
exec.call(mod, require, mod); | |
}); | |
return Narcissus; | |
} |
This file contains hidden or 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 names = [ | |
"options", | |
"definitions", | |
"lexer", | |
"parser", | |
"decompiler", | |
"resolver", | |
"desugaring", | |
"bytecode", | |
"interpreter" | |
]; | |
var sources = names.map(function(name) { | |
return read("./" + name + ".js"); | |
}); | |
main(names, sources); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment