Skip to content

Instantly share code, notes, and snippets.

@dherman
Created January 22, 2012 22:31
Show Gist options
  • Save dherman/1659153 to your computer and use it in GitHub Desktop.
Save dherman/1659153 to your computer and use it in GitHub Desktop.
require shim for Narcissus to run in SpiderMonkey
// 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;
}
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