Browse files

Adding in initial work to bring RequireJS support to Narcissus.

  • Loading branch information...
1 parent 34020e0 commit 0446ae8d65b1a6432cce57df02156637f471506e @jeresig committed Jan 10, 2012
Showing with 92 additions and 96 deletions.
  1. +3 −7 lib/decompiler.js
  2. +3 −4 lib/definitions.js
  3. +3 −5 lib/desugaring.js
  4. +8 −4 lib/jsbrowser.js
  5. +4 −5 lib/jsbytecode.js
  6. +5 −6 lib/jsdecomp.js
  7. +25 −23 lib/jsdefs.js
  8. +4 −5 lib/jsdesugar.js
  9. +11 −10 lib/jsexec.js
  10. +3 −4 lib/jslex.js
  11. +4 −4 lib/jsparse.js
  12. +5 −4 lib/jsresolve.js
  13. +3 −5 lib/lexer.js
  14. +3 −6 lib/parser.js
  15. +8 −4 main.js
View
10 lib/decompiler.js
@@ -41,10 +41,6 @@
*
* ***** END LICENSE BLOCK ***** */
-require("./jsdefs");
-require("./jslex");
-require("./jsparse");
-require("./jsdecomp");
-
-for (var exp in Narcissus.decompiler)
- exports[exp] = Narcissus.decompiler[exp];
+define([ "require", "./jsdefs", "./jslex", "./jsparse", "./jsdecomp" ], function( require ) {
+ return require("./jsdecomp");
+});
View
7 lib/definitions.js
@@ -41,7 +41,6 @@
*
* ***** END LICENSE BLOCK ***** */
-require("./jsdefs");
-
-for (var exp in Narcissus.definitions)
- exports[exp] = Narcissus.definitions[exp];
+define([ "require", "./jsdefs" ], function( require ) {
+ return require("./jsdefs").definitions;
+});
View
8 lib/desugaring.js
@@ -41,8 +41,6 @@
*
* ***** END LICENSE BLOCK ***** */
-require("./jsdefs");
-require("./jsdesugar");
-
-for (var exp in Narcissus.desugaring)
- exports[exp] = Narcissus.desugaring[exp];
+define([ "require", "./jsdefs", "./jsdesugar" ], function( require ) {
+ return require("./jsdesugar");
+});
View
12 lib/jsbrowser.js
@@ -43,23 +43,27 @@
* Browser-specific tweaks needed for Narcissus to execute properly
*/
+define([ "require", "./jsdefs", "./jsexec" ], function( require ) {
+
+var interpreter = require("./jsexec");
+
// Prevent setTimeout from breaking out to SpiderMonkey
-Narcissus.interpreter.globalBase.setTimeout = function(code, delay) {
+interpreter.globalBase.setTimeout = function(code, delay) {
var timeoutCode = (typeof code === "string") ?
function() { Narcissus.interpreter.evaluate(code); } :
code;
return setTimeout(timeoutCode, delay);
};
// Prevent setInterval from breaking out to SpiderMonkey
-Narcissus.interpreter.globalBase.setInterval = function(code, delay) {
+interpreter.globalBase.setInterval = function(code, delay) {
var timeoutCode = (typeof code === "string") ?
function() { Narcissus.interpreter.evaluate(code); } :
code;
return setInterval(timeoutCode, delay);
};
// Hack to avoid problems with the Image constructor in Narcissus.
-Narcissus.interpreter.globalBase.Image = function() {};
-
+interpreter.globalBase.Image = function() {};
+});
View
9 lib/jsbytecode.js
@@ -43,9 +43,9 @@
*
* ***** END LICENSE BLOCK ***** */
-Narcissus.bytecode = (function() {
-
- const definitions = Narcissus.definitions;
+define([ "require", "./jsdefs" ], function( require ) {
+
+ const definitions = require( "./jsdefs" ).definitions;
// Set constants in the local scope.
eval(definitions.consts);
@@ -1126,5 +1126,4 @@ Narcissus.bytecode = (function() {
GeneratorReturn: GeneratorReturn,
StopIteration: StopIteration
};
-
-})();
+});
View
11 lib/jsdecomp.js
@@ -44,11 +44,11 @@
* Decompiler and pretty-printer.
*/
-Narcissus.decompiler = (function() {
+define([ "require", "./jsdefs", "./jslex", "./jsparse" ], function( require ) {
- const lexer = Narcissus.lexer;
- const parser = Narcissus.parser;
- const definitions = Narcissus.definitions;
+ const lexer = require("./jslex");
+ const parser = require("./jsparse");
+ const definitions = require("./jsdefs");
const tokens = definitions.tokens;
// Set constants in the local scope.
@@ -552,5 +552,4 @@ Narcissus.decompiler = (function() {
return {
pp: pp
};
-
-}());
+});
View
48 lib/jsdefs.js
@@ -50,29 +50,27 @@
* done by SpiderMonkey.
*/
-(function() {
-
- var narcissus = {
- options: {
- version: 185,
- // Global variables to hide from the interpreter
- hiddenHostGlobals: { Narcissus: true },
- // Desugar SpiderMonkey language extensions?
- desugarExtensions: false,
- // Allow HTML comments?
- allowHTMLComments: false
- },
- hostSupportsEvalConst: (function() {
- try {
- return eval("(function(s) { eval(s); return x })('const x = true;')");
- } catch (e) {
- return false;
- }
- })(),
- hostGlobal: this
- };
- Narcissus = narcissus;
-})();
+define(function() {
+
+var Narcissus = {
+ options: {
+ version: 185,
+ // Global variables to hide from the interpreter
+ hiddenHostGlobals: { Narcissus: true },
+ // Desugar SpiderMonkey language extensions?
+ desugarExtensions: false,
+ // Allow HTML comments?
+ allowHTMLComments: false
+ },
+ hostSupportsEvalConst: (function() {
+ try {
+ return eval("(function(s) { eval(s); return x })('const x = true;')");
+ } catch (e) {
+ return false;
+ }
+ })(),
+ hostGlobal: this
+};
Narcissus.definitions = (function(hostGlobal) {
@@ -706,3 +704,7 @@ Narcissus.definitions = (function(hostGlobal) {
Stack: Stack
};
}(this));
+
+return Narcissus;
+
+});
View
9 lib/jsdesugar.js
@@ -79,10 +79,10 @@
* (( [ P1, ..., Pn ] )) = (( P1 )), ..., (( Pn ))
*/
-Narcissus.desugaring = (function() {
+define([ "require", "./jsdefs", "./jsparse" ], function( require ) {
- var definitions = Narcissus.definitions;
- var parser = Narcissus.parser;
+ var definitions = require("./jsdefs").definitions;
+ var parser = require("./jsparse");
const Dict = definitions.Dict;
@@ -1011,5 +1011,4 @@ Narcissus.desugaring = (function() {
return {
desugar: desugar
};
-
-})();
+});
View
21 lib/jsexec.js
@@ -54,13 +54,15 @@
* an extra level of prototype-based delegation.
*/
-Narcissus.interpreter = (function() {
+define([ "require", "./jsdefs", "./jsbytecode", "./jsresolve", "./jsdesugar", "./jsparser" ], function( require ) {
- var parser = Narcissus.parser;
+ var Narcissus = require("./jsdefs");
+ var parser = require("./jsparser");
var definitions = Narcissus.definitions;
- var resolver = Narcissus.resolver;
+ var resolver = require("./jsresolve");
var hostGlobal = Narcissus.hostGlobal;
- var desugaring = Narcissus.desugaring;
+ var desugaring = require("./jsdesugar");
+ var bytecode = require("./jsbytecode");
// Set constants in the local scope.
eval(definitions.consts);
@@ -323,8 +325,8 @@ Narcissus.interpreter = (function() {
if (v instanceof Reference) {
if (!v.base) {
// Hook needed for Zaphod
- if (Narcissus.interpreter.getValueHook)
- return Narcissus.interpreter.getValueHook(v.propertyName);
+ if (this.getValueHook)
+ return this.getValueHook(v.propertyName);
throw new ReferenceError(v.propertyName + " is not defined",
v.node.filename, v.node.lineno);
}
@@ -460,7 +462,7 @@ Narcissus.interpreter = (function() {
break;
case GENERATOR:
- u = new Narcissus.bytecode.Generator(x.functionInstance.getBytecode(), x);
+ u = new bytecode.Generator(x.functionInstance.getBytecode(), x);
x.thisGenerator = u;
x.result = u.userObject;
throw RETURN_SIGNAL;
@@ -1246,7 +1248,7 @@ Narcissus.interpreter = (function() {
getBytecode: function() {
var n = this.node;
if (!n.proc)
- n.proc = Narcissus.bytecode.compile(n.body.type === GENERATOR ? n.body : n);
+ n.proc = bytecode.compile(n.body.type === GENERATOR ? n.body : n);
return n.proc;
},
@@ -1434,5 +1436,4 @@ Narcissus.interpreter = (function() {
repl: repl,
test: test
};
-
-}());
+});
View
7 lib/jslex.js
@@ -48,8 +48,9 @@
* Lexical scanner.
*/
-Narcissus.lexer = (function() {
+define([ "require", "./jsdefs" ], function( require ) {
+ var Narcissus = require("./jsdefs");
var definitions = Narcissus.definitions;
// Set constants in the local scope.
@@ -579,10 +580,8 @@ Narcissus.lexer = (function() {
},
};
-
return {
isIdentifier: isIdentifier,
Tokenizer: Tokenizer
};
-
-}());
+});
View
8 lib/jsparse.js
@@ -49,9 +49,10 @@
* Parser.
*/
-Narcissus.parser = (function() {
+define([ "require", "./jsdefs", "./jslex" ], function( require ) {
- var lexer = Narcissus.lexer;
+ var Narcissus = require("./jsdefs");
+ var lexer = require("./jslex");
var definitions = Narcissus.definitions;
const Dict = definitions.Dict;
@@ -1917,5 +1918,4 @@ Narcissus.parser = (function() {
Module: Module,
Export: Export
};
-
-}());
+});
View
9 lib/jsresolve.js
@@ -44,9 +44,10 @@
* Static variable and module resolution for Harmony.
*/
-Narcissus.resolver = (function() {
- const definitions = Narcissus.definitions;
- const parser = Narcissus.parser;
+define([ "require", "./jsdefs", "./jsparse" ], function( require ) {
+
+ const definitions = require("./jsdef").definitions;
+ const parser = require("./jsparse");
const Dict = definitions.Dict;
const Module = parser.Module;
@@ -714,4 +715,4 @@ Narcissus.resolver = (function() {
StaticEnv: StaticEnv,
resolve: resolve
};
-}());
+});
View
8 lib/lexer.js
@@ -41,8 +41,6 @@
*
* ***** END LICENSE BLOCK ***** */
-require("./jsdefs");
-require("./jslex");
-
-for (var exp in Narcissus.lexer)
- exports[exp] = Narcissus.lexer[exp];
+define([ "require", "./jsdefs", "./jslex" ], function( require ) {
+ return require("./jslex");
+});
View
9 lib/parser.js
@@ -41,9 +41,6 @@
*
* ***** END LICENSE BLOCK ***** */
-require("./jsdefs");
-require("./jslex");
-require("./jsparse");
-
-for (var exp in Narcissus.parser)
- exports[exp] = Narcissus.parser[exp];
+define([ "require", "./jsdefs", "./jslex", "./jsparse" ], function( require ) {
+ return require("./jsparse");
+});
View
12 main.js
@@ -41,7 +41,11 @@
*
* ***** END LICENSE BLOCK ***** */
-exports.definitions = require("./lib/definitions");
-exports.lexer = require("./lib/lexer");
-exports.parser = require("./lib/parser");
-exports.decompiler = require("./lib/decompiler");
+define([ "require", "./lib/definitions", "./lib/lexer", "./lib/parser", "./lib/decompiler" ], function( require ) {
+ return {
+ definitions: require("./lib/definitions"),
+ lexer: require("./lib/lexer"),
+ parser: require("./lib/parser"),
+ decompiler: require("./lib/decompiler")
+ };
+});

0 comments on commit 0446ae8

Please sign in to comment.