Skip to content
Browse files

Initial commit

  • Loading branch information...
0 parents commit 3a5178b626ccb224303a568584f29fdcf41fe1c5 @jbrantly committed Apr 12, 2010
Showing with 1,551 additions and 0 deletions.
  1. +67 −0 tests/modules1.0/index.html
  2. +1 −0 tests/modules1.0/tests/absolute/b.js
  3. +5 −0 tests/modules1.0/tests/absolute/program.js
  4. +3 −0 tests/modules1.0/tests/absolute/submodule/a.js
  5. +15 −0 tests/modules1.0/tests/absolute/test.js
  6. +4 −0 tests/modules1.0/tests/cyclic/a.js
  7. +4 −0 tests/modules1.0/tests/cyclic/b.js
  8. +10 −0 tests/modules1.0/tests/cyclic/program.js
  9. +15 −0 tests/modules1.0/tests/cyclic/test.js
  10. +3 −0 tests/modules1.0/tests/determinism/program.js
  11. +9 −0 tests/modules1.0/tests/determinism/submodule/a.js
  12. +2 −0 tests/modules1.0/tests/determinism/submodule/b.js
  13. +15 −0 tests/modules1.0/tests/determinism/test.js
  14. +3 −0 tests/modules1.0/tests/exactExports/a.js
  15. +4 −0 tests/modules1.0/tests/exactExports/program.js
  16. +15 −0 tests/modules1.0/tests/exactExports/test.js
  17. 0 tests/modules1.0/tests/hasOwnProperty/hasOwnProperty.js
  18. +4 −0 tests/modules1.0/tests/hasOwnProperty/program.js
  19. +15 −0 tests/modules1.0/tests/hasOwnProperty/test.js
  20. 0 tests/modules1.0/tests/hasOwnProperty/toString.js
  21. +12 −0 tests/modules1.0/tests/method/a.js
  22. +8 −0 tests/modules1.0/tests/method/program.js
  23. +15 −0 tests/modules1.0/tests/method/test.js
  24. +8 −0 tests/modules1.0/tests/missing/program.js
  25. +15 −0 tests/modules1.0/tests/missing/test.js
  26. +1 −0 tests/modules1.0/tests/monkeys/a.js
  27. +4 −0 tests/modules1.0/tests/monkeys/program.js
  28. +15 −0 tests/modules1.0/tests/monkeys/test.js
  29. +3 −0 tests/modules1.0/tests/nested/a/b/c/d.js
  30. +3 −0 tests/modules1.0/tests/nested/program.js
  31. +15 −0 tests/modules1.0/tests/nested/test.js
  32. +5 −0 tests/modules1.0/tests/relative/program.js
  33. +1 −0 tests/modules1.0/tests/relative/submodule/a.js
  34. +2 −0 tests/modules1.0/tests/relative/submodule/b.js
  35. +15 −0 tests/modules1.0/tests/relative/test.js
  36. +1 −0 tests/modules1.0/tests/transitive/a.js
  37. +1 −0 tests/modules1.0/tests/transitive/b.js
  38. +3 −0 tests/modules1.0/tests/transitive/c.js
  39. +3 −0 tests/modules1.0/tests/transitive/program.js
  40. +15 −0 tests/modules1.0/tests/transitive/test.js
  41. +68 −0 tests/modules1.0/wrappedIndex.html
  42. +6 −0 tests/modules1.0/wrappedTests/absolute/b.js
  43. +10 −0 tests/modules1.0/wrappedTests/absolute/program.js
  44. +8 −0 tests/modules1.0/wrappedTests/absolute/submodule/a.js
  45. +20 −0 tests/modules1.0/wrappedTests/absolute/test.js
  46. +9 −0 tests/modules1.0/wrappedTests/cyclic/a.js
  47. +9 −0 tests/modules1.0/wrappedTests/cyclic/b.js
  48. +15 −0 tests/modules1.0/wrappedTests/cyclic/program.js
  49. +20 −0 tests/modules1.0/wrappedTests/cyclic/test.js
  50. +8 −0 tests/modules1.0/wrappedTests/determinism/program.js
  51. +14 −0 tests/modules1.0/wrappedTests/determinism/submodule/a.js
  52. +7 −0 tests/modules1.0/wrappedTests/determinism/submodule/b.js
  53. +20 −0 tests/modules1.0/wrappedTests/determinism/test.js
  54. +8 −0 tests/modules1.0/wrappedTests/exactExports/a.js
  55. +9 −0 tests/modules1.0/wrappedTests/exactExports/program.js
  56. +20 −0 tests/modules1.0/wrappedTests/exactExports/test.js
  57. +5 −0 tests/modules1.0/wrappedTests/hasOwnProperty/hasOwnProperty.js
  58. +9 −0 tests/modules1.0/wrappedTests/hasOwnProperty/program.js
  59. +20 −0 tests/modules1.0/wrappedTests/hasOwnProperty/test.js
  60. +5 −0 tests/modules1.0/wrappedTests/hasOwnProperty/toString.js
  61. +17 −0 tests/modules1.0/wrappedTests/method/a.js
  62. +13 −0 tests/modules1.0/wrappedTests/method/program.js
  63. +20 −0 tests/modules1.0/wrappedTests/method/test.js
  64. +13 −0 tests/modules1.0/wrappedTests/missing/program.js
  65. +20 −0 tests/modules1.0/wrappedTests/missing/test.js
  66. +6 −0 tests/modules1.0/wrappedTests/monkeys/a.js
  67. +9 −0 tests/modules1.0/wrappedTests/monkeys/program.js
  68. +20 −0 tests/modules1.0/wrappedTests/monkeys/test.js
  69. +8 −0 tests/modules1.0/wrappedTests/nested/a/b/c/d.js
  70. +8 −0 tests/modules1.0/wrappedTests/nested/program.js
  71. +20 −0 tests/modules1.0/wrappedTests/nested/test.js
  72. +10 −0 tests/modules1.0/wrappedTests/relative/program.js
  73. +6 −0 tests/modules1.0/wrappedTests/relative/submodule/a.js
  74. +7 −0 tests/modules1.0/wrappedTests/relative/submodule/b.js
  75. +20 −0 tests/modules1.0/wrappedTests/relative/test.js
  76. +6 −0 tests/modules1.0/wrappedTests/transitive/a.js
  77. +6 −0 tests/modules1.0/wrappedTests/transitive/b.js
  78. +8 −0 tests/modules1.0/wrappedTests/transitive/c.js
  79. +8 −0 tests/modules1.0/wrappedTests/transitive/program.js
  80. +20 −0 tests/modules1.0/wrappedTests/transitive/test.js
  81. +11 −0 tests/modules1.0/yabbler.bat
  82. +33 −0 tests/print.js
  83. +452 −0 yabble.js
  84. +181 −0 yabbler.js
67 tests/modules1.0/index.html
@@ -0,0 +1,67 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
+<html>
+ <head>
+ <title></title>
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
+ <script type="text/javascript" src="../print.js"></script>
+ <script type="text/javascript" src="../../yabble.js"></script>
+ <script type="text/javascript">
+ require.setModuleRoot('tests/absolute');
+ require.run('program', function() {
+
+ require.reset();
+ require.setModuleRoot('tests/cyclic');
+ require.run('program', function() {
+
+ require.reset();
+ require.setModuleRoot('tests/determinism');
+ require.run('program', function() {
+
+ require.reset();
+ require.setModuleRoot('tests/exactExports');
+ require.run('program', function() {
+
+ require.reset();
+ require.setModuleRoot('tests/hasOwnProperty');
+ require.run('program', function() {
+
+ require.reset();
+ require.setModuleRoot('tests/method');
+ require.run('program', function() {
+
+ require.reset();
+ require.setModuleRoot('tests/missing');
+ require.run('program', function() {
+
+ require.reset();
+ require.setModuleRoot('tests/monkeys');
+ require.run('program', function() {
+
+ require.reset();
+ require.setModuleRoot('tests/nested');
+ require.run('program', function() {
+
+ require.reset();
+ require.setModuleRoot('tests/relative');
+ require.run('program', function() {
+
+ require.reset();
+ require.setModuleRoot('tests/transitive');
+ require.run('program', function() {
+
+ });
+ });
+ });
+ });
+ });
+ });
+ });
+ });
+ });
+ });
+ });
+ </script>
+ </head>
+ <body>
+ </body>
+</html>
1 tests/modules1.0/tests/absolute/b.js
@@ -0,0 +1 @@
+exports.foo = function() {};
5 tests/modules1.0/tests/absolute/program.js
@@ -0,0 +1,5 @@
+var test = require('test');
+var a = require('submodule/a');
+var b = require('b');
+test.assert(a.foo().foo === b.foo, 'require works with absolute identifiers');
+test.print('DONE', 'info');
3 tests/modules1.0/tests/absolute/submodule/a.js
@@ -0,0 +1,3 @@
+exports.foo = function () {
+ return require('b');
+};
15 tests/modules1.0/tests/absolute/test.js
@@ -0,0 +1,15 @@
+
+exports.print = typeof print !== "undefined" ? print : function () {
+ var system = require("system");
+ var stdio = system.stdio;
+ stdio.print.apply(stdio, arguments);
+};
+
+exports.assert = function (guard, message) {
+ if (guard) {
+ exports.print('PASS ' + message, 'pass');
+ } else {
+ exports.print('FAIL ' + message, 'fail');
+ }
+};
+
4 tests/modules1.0/tests/cyclic/a.js
@@ -0,0 +1,4 @@
+exports.a = function () {
+ return b;
+};
+var b = require('b');
4 tests/modules1.0/tests/cyclic/b.js
@@ -0,0 +1,4 @@
+var a = require('a');
+exports.b = function () {
+ return a;
+};
10 tests/modules1.0/tests/cyclic/program.js
@@ -0,0 +1,10 @@
+var test = require('test');
+var a = require('a');
+var b = require('b');
+
+test.assert(a.a, 'a exists');
+test.assert(b.b, 'b exists')
+test.assert(a.a().b === b.b, 'a gets b');
+test.assert(b.b().a === a.a, 'b gets a');
+
+test.print('DONE', 'info');
15 tests/modules1.0/tests/cyclic/test.js
@@ -0,0 +1,15 @@
+
+exports.print = typeof print !== "undefined" ? print : function () {
+ var system = require("system");
+ var stdio = system.stdio;
+ stdio.print.apply(stdio, arguments);
+};
+
+exports.assert = function (guard, message) {
+ if (guard) {
+ exports.print('PASS ' + message, 'pass');
+ } else {
+ exports.print('FAIL ' + message, 'fail');
+ }
+};
+
3 tests/modules1.0/tests/determinism/program.js
@@ -0,0 +1,3 @@
+var test = require('test');
+require('submodule/a');
+test.print('DONE', 'info');
9 tests/modules1.0/tests/determinism/submodule/a.js
@@ -0,0 +1,9 @@
+var test = require('test');
+var pass = false;
+var test = require('test');
+try {
+ require('a');
+} catch (exception) {
+ pass = true;
+}
+test.assert(pass, 'require does not fall back to relative modules when absolutes are not available.')
2 tests/modules1.0/tests/determinism/submodule/b.js
@@ -0,0 +1,2 @@
+
+
15 tests/modules1.0/tests/determinism/test.js
@@ -0,0 +1,15 @@
+
+exports.print = typeof print !== "undefined" ? print : function () {
+ var system = require("system");
+ var stdio = system.stdio;
+ stdio.print.apply(stdio, arguments);
+};
+
+exports.assert = function (guard, message) {
+ if (guard) {
+ exports.print('PASS ' + message, 'pass');
+ } else {
+ exports.print('FAIL ' + message, 'fail');
+ }
+};
+
3 tests/modules1.0/tests/exactExports/a.js
@@ -0,0 +1,3 @@
+exports.program = function () {
+ return require('program');
+};
4 tests/modules1.0/tests/exactExports/program.js
@@ -0,0 +1,4 @@
+var test = require('test');
+var a = require('a');
+test.assert(a.program() === exports, 'exact exports');
+test.print('DONE', 'info');
15 tests/modules1.0/tests/exactExports/test.js
@@ -0,0 +1,15 @@
+
+exports.print = typeof print !== "undefined" ? print : function () {
+ var system = require("system");
+ var stdio = system.stdio;
+ stdio.print.apply(stdio, arguments);
+};
+
+exports.assert = function (guard, message) {
+ if (guard) {
+ exports.print('PASS ' + message, 'pass');
+ } else {
+ exports.print('FAIL ' + message, 'fail');
+ }
+};
+
0 tests/modules1.0/tests/hasOwnProperty/hasOwnProperty.js
No changes.
4 tests/modules1.0/tests/hasOwnProperty/program.js
@@ -0,0 +1,4 @@
+var hasOwnProperty = require('hasOwnProperty');
+var toString = require('toString');
+var test = require('test');
+test.print('DONE', 'info');
15 tests/modules1.0/tests/hasOwnProperty/test.js
@@ -0,0 +1,15 @@
+
+exports.print = typeof print !== "undefined" ? print : function () {
+ var system = require("system");
+ var stdio = system.stdio;
+ stdio.print.apply(stdio, arguments);
+};
+
+exports.assert = function (guard, message) {
+ if (guard) {
+ exports.print('PASS ' + message, 'pass');
+ } else {
+ exports.print('FAIL ' + message, 'fail');
+ }
+};
+
0 tests/modules1.0/tests/hasOwnProperty/toString.js
No changes.
12 tests/modules1.0/tests/method/a.js
@@ -0,0 +1,12 @@
+exports.foo = function () {
+ return this;
+};
+exports.set = function (x) {
+ this.x = x;
+};
+exports.get = function () {
+ return this.x;
+};
+exports.getClosed = function () {
+ return exports.x;
+};
8 tests/modules1.0/tests/method/program.js
@@ -0,0 +1,8 @@
+var test = require('test');
+var a = require('a');
+var foo = a.foo;
+test.assert(a.foo() == a, 'calling a module member');
+test.assert(foo() == (function (){return this})(), 'members not implicitly bound');
+a.set(10);
+test.assert(a.get() == 10, 'get and set')
+test.print('DONE', 'info');
15 tests/modules1.0/tests/method/test.js
@@ -0,0 +1,15 @@
+
+exports.print = typeof print !== "undefined" ? print : function () {
+ var system = require("system");
+ var stdio = system.stdio;
+ stdio.print.apply(stdio, arguments);
+};
+
+exports.assert = function (guard, message) {
+ if (guard) {
+ exports.print('PASS ' + message, 'pass');
+ } else {
+ exports.print('FAIL ' + message, 'fail');
+ }
+};
+
8 tests/modules1.0/tests/missing/program.js
@@ -0,0 +1,8 @@
+var test = require('test');
+try {
+ require('bogus');
+ test.print('FAIL require throws error when module missing', 'fail');
+} catch (exception) {
+ test.print('PASS require throws error when module missing', 'pass');
+}
+test.print('DONE', 'info');
15 tests/modules1.0/tests/missing/test.js
@@ -0,0 +1,15 @@
+
+exports.print = typeof print !== "undefined" ? print : function () {
+ var system = require("system");
+ var stdio = system.stdio;
+ stdio.print.apply(stdio, arguments);
+};
+
+exports.assert = function (guard, message) {
+ if (guard) {
+ exports.print('PASS ' + message, 'pass');
+ } else {
+ exports.print('FAIL ' + message, 'fail');
+ }
+};
+
1 tests/modules1.0/tests/monkeys/a.js
@@ -0,0 +1 @@
+require('program').monkey = 10;
4 tests/modules1.0/tests/monkeys/program.js
@@ -0,0 +1,4 @@
+var a = require('a');
+var test = require('test');
+test.assert(exports.monkey == 10, 'monkeys permitted');
+test.print('DONE', 'info');
15 tests/modules1.0/tests/monkeys/test.js
@@ -0,0 +1,15 @@
+
+exports.print = typeof print !== "undefined" ? print : function () {
+ var system = require("system");
+ var stdio = system.stdio;
+ stdio.print.apply(stdio, arguments);
+};
+
+exports.assert = function (guard, message) {
+ if (guard) {
+ exports.print('PASS ' + message, 'pass');
+ } else {
+ exports.print('FAIL ' + message, 'fail');
+ }
+};
+
3 tests/modules1.0/tests/nested/a/b/c/d.js
@@ -0,0 +1,3 @@
+exports.foo = function () {
+ return 1;
+};
3 tests/modules1.0/tests/nested/program.js
@@ -0,0 +1,3 @@
+var test = require('test');
+test.assert(require('a/b/c/d').foo() == 1, 'nested module identifier');
+test.print('DONE', 'info');
15 tests/modules1.0/tests/nested/test.js
@@ -0,0 +1,15 @@
+
+exports.print = typeof print !== "undefined" ? print : function () {
+ var system = require("system");
+ var stdio = system.stdio;
+ stdio.print.apply(stdio, arguments);
+};
+
+exports.assert = function (guard, message) {
+ if (guard) {
+ exports.print('PASS ' + message, 'pass');
+ } else {
+ exports.print('FAIL ' + message, 'fail');
+ }
+};
+
5 tests/modules1.0/tests/relative/program.js
@@ -0,0 +1,5 @@
+var test = require('test');
+var a = require('submodule/a');
+var b = require('submodule/b');
+test.assert(a.foo == b.foo, 'a and b share foo through a relative require');
+test.print('DONE', 'info');
1 tests/modules1.0/tests/relative/submodule/a.js
@@ -0,0 +1 @@
+exports.foo = require('./b').foo;
2 tests/modules1.0/tests/relative/submodule/b.js
@@ -0,0 +1,2 @@
+exports.foo = function () {
+};
15 tests/modules1.0/tests/relative/test.js
@@ -0,0 +1,15 @@
+
+exports.print = typeof print !== "undefined" ? print : function () {
+ var system = require("system");
+ var stdio = system.stdio;
+ stdio.print.apply(stdio, arguments);
+};
+
+exports.assert = function (guard, message) {
+ if (guard) {
+ exports.print('PASS ' + message, 'pass');
+ } else {
+ exports.print('FAIL ' + message, 'fail');
+ }
+};
+
1 tests/modules1.0/tests/transitive/a.js
@@ -0,0 +1 @@
+exports.foo = require('b').foo;
1 tests/modules1.0/tests/transitive/b.js
@@ -0,0 +1 @@
+exports.foo = require('c').foo;
3 tests/modules1.0/tests/transitive/c.js
@@ -0,0 +1,3 @@
+exports.foo = function () {
+ return 1;
+};
3 tests/modules1.0/tests/transitive/program.js
@@ -0,0 +1,3 @@
+var test = require('test');
+test.assert(require('a').foo() == 1, 'transitive');
+test.print('DONE', 'info');
15 tests/modules1.0/tests/transitive/test.js
@@ -0,0 +1,15 @@
+
+exports.print = typeof print !== "undefined" ? print : function () {
+ var system = require("system");
+ var stdio = system.stdio;
+ stdio.print.apply(stdio, arguments);
+};
+
+exports.assert = function (guard, message) {
+ if (guard) {
+ exports.print('PASS ' + message, 'pass');
+ } else {
+ exports.print('FAIL ' + message, 'fail');
+ }
+};
+
68 tests/modules1.0/wrappedIndex.html
@@ -0,0 +1,68 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
+<html>
+ <head>
+ <title></title>
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
+ <script type="text/javascript" src="../print.js"></script>
+ <script type="text/javascript" src="../../yabble.js"></script>
+ <script type="text/javascript">
+ require.useScriptTags();
+ require.setModuleRoot('wrappedTests/absolute');
+ require.run('program', function() {
+
+ require.reset();
+ require.setModuleRoot('wrappedTests/cyclic');
+ require.run('program', function() {
+
+ require.reset();
+ require.setModuleRoot('wrappedTests/determinism');
+ require.run('program', function() {
+
+ require.reset();
+ require.setModuleRoot('wrappedTests/exactExports');
+ require.run('program', function() {
+
+ require.reset();
+ require.setModuleRoot('wrappedTests/hasOwnProperty');
+ require.run('program', function() {
+
+ require.reset();
+ require.setModuleRoot('wrappedTests/method');
+ require.run('program', function() {
+
+ require.reset();
+ require.setModuleRoot('wrappedTests/missing');
+ require.run('program', function() {
+
+ require.reset();
+ require.setModuleRoot('wrappedTests/monkeys');
+ require.run('program', function() {
+
+ require.reset();
+ require.setModuleRoot('wrappedTests/nested');
+ require.run('program', function() {
+
+ require.reset();
+ require.setModuleRoot('wrappedTests/relative');
+ require.run('program', function() {
+
+ require.reset();
+ require.setModuleRoot('wrappedTests/transitive');
+ require.run('program', function() {
+
+ });
+ });
+ });
+ });
+ });
+ });
+ });
+ });
+ });
+ });
+ });
+ </script>
+ </head>
+ <body>
+ </body>
+</html>
6 tests/modules1.0/wrappedTests/absolute/b.js
@@ -0,0 +1,6 @@
+/* This file has been generated by yabbler.js */
+require.define({
+"b": function(require, exports, module) {
+exports.foo = function() {};
+
+}}, []);
10 tests/modules1.0/wrappedTests/absolute/program.js
@@ -0,0 +1,10 @@
+/* This file has been generated by yabbler.js */
+require.define({
+"program": function(require, exports, module) {
+var test = require('test');
+var a = require('submodule/a');
+var b = require('b');
+test.assert(a.foo().foo === b.foo, 'require works with absolute identifiers');
+test.print('DONE', 'info');
+
+}}, ["test", "submodule/a", "b"]);
8 tests/modules1.0/wrappedTests/absolute/submodule/a.js
@@ -0,0 +1,8 @@
+/* This file has been generated by yabbler.js */
+require.define({
+"submodule/a": function(require, exports, module) {
+exports.foo = function () {
+ return require('b');
+};
+
+}}, ["b"]);
20 tests/modules1.0/wrappedTests/absolute/test.js
@@ -0,0 +1,20 @@
+/* This file has been generated by yabbler.js */
+require.define({
+"test": function(require, exports, module) {
+
+exports.print = typeof print !== "undefined" ? print : function () {
+ var system = require("system");
+ var stdio = system.stdio;
+ stdio.print.apply(stdio, arguments);
+};
+
+exports.assert = function (guard, message) {
+ if (guard) {
+ exports.print('PASS ' + message, 'pass');
+ } else {
+ exports.print('FAIL ' + message, 'fail');
+ }
+};
+
+
+}}, ["system"]);
9 tests/modules1.0/wrappedTests/cyclic/a.js
@@ -0,0 +1,9 @@
+/* This file has been generated by yabbler.js */
+require.define({
+"a": function(require, exports, module) {
+exports.a = function () {
+ return b;
+};
+var b = require('b');
+
+}}, ["b"]);
9 tests/modules1.0/wrappedTests/cyclic/b.js
@@ -0,0 +1,9 @@
+/* This file has been generated by yabbler.js */
+require.define({
+"b": function(require, exports, module) {
+var a = require('a');
+exports.b = function () {
+ return a;
+};
+
+}}, ["a"]);
15 tests/modules1.0/wrappedTests/cyclic/program.js
@@ -0,0 +1,15 @@
+/* This file has been generated by yabbler.js */
+require.define({
+"program": function(require, exports, module) {
+var test = require('test');
+var a = require('a');
+var b = require('b');
+
+test.assert(a.a, 'a exists');
+test.assert(b.b, 'b exists')
+test.assert(a.a().b === b.b, 'a gets b');
+test.assert(b.b().a === a.a, 'b gets a');
+
+test.print('DONE', 'info');
+
+}}, ["test", "a", "b"]);
20 tests/modules1.0/wrappedTests/cyclic/test.js
@@ -0,0 +1,20 @@
+/* This file has been generated by yabbler.js */
+require.define({
+"test": function(require, exports, module) {
+
+exports.print = typeof print !== "undefined" ? print : function () {
+ var system = require("system");
+ var stdio = system.stdio;
+ stdio.print.apply(stdio, arguments);
+};
+
+exports.assert = function (guard, message) {
+ if (guard) {
+ exports.print('PASS ' + message, 'pass');
+ } else {
+ exports.print('FAIL ' + message, 'fail');
+ }
+};
+
+
+}}, ["system"]);
8 tests/modules1.0/wrappedTests/determinism/program.js
@@ -0,0 +1,8 @@
+/* This file has been generated by yabbler.js */
+require.define({
+"program": function(require, exports, module) {
+var test = require('test');
+require('submodule/a');
+test.print('DONE', 'info');
+
+}}, ["test", "submodule/a"]);
14 tests/modules1.0/wrappedTests/determinism/submodule/a.js
@@ -0,0 +1,14 @@
+/* This file has been generated by yabbler.js */
+require.define({
+"submodule/a": function(require, exports, module) {
+var test = require('test');
+var pass = false;
+var test = require('test');
+try {
+ require('a');
+} catch (exception) {
+ pass = true;
+}
+test.assert(pass, 'require does not fall back to relative modules when absolutes are not available.')
+
+}}, ["test", "a"]);
7 tests/modules1.0/wrappedTests/determinism/submodule/b.js
@@ -0,0 +1,7 @@
+/* This file has been generated by yabbler.js */
+require.define({
+"submodule/b": function(require, exports, module) {
+
+
+
+}}, []);
20 tests/modules1.0/wrappedTests/determinism/test.js
@@ -0,0 +1,20 @@
+/* This file has been generated by yabbler.js */
+require.define({
+"test": function(require, exports, module) {
+
+exports.print = typeof print !== "undefined" ? print : function () {
+ var system = require("system");
+ var stdio = system.stdio;
+ stdio.print.apply(stdio, arguments);
+};
+
+exports.assert = function (guard, message) {
+ if (guard) {
+ exports.print('PASS ' + message, 'pass');
+ } else {
+ exports.print('FAIL ' + message, 'fail');
+ }
+};
+
+
+}}, ["system"]);
8 tests/modules1.0/wrappedTests/exactExports/a.js
@@ -0,0 +1,8 @@
+/* This file has been generated by yabbler.js */
+require.define({
+"a": function(require, exports, module) {
+exports.program = function () {
+ return require('program');
+};
+
+}}, ["program"]);
9 tests/modules1.0/wrappedTests/exactExports/program.js
@@ -0,0 +1,9 @@
+/* This file has been generated by yabbler.js */
+require.define({
+"program": function(require, exports, module) {
+var test = require('test');
+var a = require('a');
+test.assert(a.program() === exports, 'exact exports');
+test.print('DONE', 'info');
+
+}}, ["test", "a"]);
20 tests/modules1.0/wrappedTests/exactExports/test.js
@@ -0,0 +1,20 @@
+/* This file has been generated by yabbler.js */
+require.define({
+"test": function(require, exports, module) {
+
+exports.print = typeof print !== "undefined" ? print : function () {
+ var system = require("system");
+ var stdio = system.stdio;
+ stdio.print.apply(stdio, arguments);
+};
+
+exports.assert = function (guard, message) {
+ if (guard) {
+ exports.print('PASS ' + message, 'pass');
+ } else {
+ exports.print('FAIL ' + message, 'fail');
+ }
+};
+
+
+}}, ["system"]);
5 tests/modules1.0/wrappedTests/hasOwnProperty/hasOwnProperty.js
@@ -0,0 +1,5 @@
+/* This file has been generated by yabbler.js */
+require.define({
+"hasOwnProperty": function(require, exports, module) {
+
+}}, []);
9 tests/modules1.0/wrappedTests/hasOwnProperty/program.js
@@ -0,0 +1,9 @@
+/* This file has been generated by yabbler.js */
+require.define({
+"program": function(require, exports, module) {
+var hasOwnProperty = require('hasOwnProperty');
+var toString = require('toString');
+var test = require('test');
+test.print('DONE', 'info');
+
+}}, ["hasOwnProperty", "toString", "test"]);
20 tests/modules1.0/wrappedTests/hasOwnProperty/test.js
@@ -0,0 +1,20 @@
+/* This file has been generated by yabbler.js */
+require.define({
+"test": function(require, exports, module) {
+
+exports.print = typeof print !== "undefined" ? print : function () {
+ var system = require("system");
+ var stdio = system.stdio;
+ stdio.print.apply(stdio, arguments);
+};
+
+exports.assert = function (guard, message) {
+ if (guard) {
+ exports.print('PASS ' + message, 'pass');
+ } else {
+ exports.print('FAIL ' + message, 'fail');
+ }
+};
+
+
+}}, ["system"]);
5 tests/modules1.0/wrappedTests/hasOwnProperty/toString.js
@@ -0,0 +1,5 @@
+/* This file has been generated by yabbler.js */
+require.define({
+"toString": function(require, exports, module) {
+
+}}, []);
17 tests/modules1.0/wrappedTests/method/a.js
@@ -0,0 +1,17 @@
+/* This file has been generated by yabbler.js */
+require.define({
+"a": function(require, exports, module) {
+exports.foo = function () {
+ return this;
+};
+exports.set = function (x) {
+ this.x = x;
+};
+exports.get = function () {
+ return this.x;
+};
+exports.getClosed = function () {
+ return exports.x;
+};
+
+}}, []);
13 tests/modules1.0/wrappedTests/method/program.js
@@ -0,0 +1,13 @@
+/* This file has been generated by yabbler.js */
+require.define({
+"program": function(require, exports, module) {
+var test = require('test');
+var a = require('a');
+var foo = a.foo;
+test.assert(a.foo() == a, 'calling a module member');
+test.assert(foo() == (function (){return this})(), 'members not implicitly bound');
+a.set(10);
+test.assert(a.get() == 10, 'get and set')
+test.print('DONE', 'info');
+
+}}, ["test", "a"]);
20 tests/modules1.0/wrappedTests/method/test.js
@@ -0,0 +1,20 @@
+/* This file has been generated by yabbler.js */
+require.define({
+"test": function(require, exports, module) {
+
+exports.print = typeof print !== "undefined" ? print : function () {
+ var system = require("system");
+ var stdio = system.stdio;
+ stdio.print.apply(stdio, arguments);
+};
+
+exports.assert = function (guard, message) {
+ if (guard) {
+ exports.print('PASS ' + message, 'pass');
+ } else {
+ exports.print('FAIL ' + message, 'fail');
+ }
+};
+
+
+}}, ["system"]);
13 tests/modules1.0/wrappedTests/missing/program.js
@@ -0,0 +1,13 @@
+/* This file has been generated by yabbler.js */
+require.define({
+"program": function(require, exports, module) {
+var test = require('test');
+try {
+ require('bogus');
+ test.print('FAIL require throws error when module missing', 'fail');
+} catch (exception) {
+ test.print('PASS require throws error when module missing', 'pass');
+}
+test.print('DONE', 'info');
+
+}}, ["test", "bogus"]);
20 tests/modules1.0/wrappedTests/missing/test.js
@@ -0,0 +1,20 @@
+/* This file has been generated by yabbler.js */
+require.define({
+"test": function(require, exports, module) {
+
+exports.print = typeof print !== "undefined" ? print : function () {
+ var system = require("system");
+ var stdio = system.stdio;
+ stdio.print.apply(stdio, arguments);
+};
+
+exports.assert = function (guard, message) {
+ if (guard) {
+ exports.print('PASS ' + message, 'pass');
+ } else {
+ exports.print('FAIL ' + message, 'fail');
+ }
+};
+
+
+}}, ["system"]);
6 tests/modules1.0/wrappedTests/monkeys/a.js
@@ -0,0 +1,6 @@
+/* This file has been generated by yabbler.js */
+require.define({
+"a": function(require, exports, module) {
+require('program').monkey = 10;
+
+}}, ["program"]);
9 tests/modules1.0/wrappedTests/monkeys/program.js
@@ -0,0 +1,9 @@
+/* This file has been generated by yabbler.js */
+require.define({
+"program": function(require, exports, module) {
+var a = require('a');
+var test = require('test');
+test.assert(exports.monkey == 10, 'monkeys permitted');
+test.print('DONE', 'info');
+
+}}, ["a", "test"]);
20 tests/modules1.0/wrappedTests/monkeys/test.js
@@ -0,0 +1,20 @@
+/* This file has been generated by yabbler.js */
+require.define({
+"test": function(require, exports, module) {
+
+exports.print = typeof print !== "undefined" ? print : function () {
+ var system = require("system");
+ var stdio = system.stdio;
+ stdio.print.apply(stdio, arguments);
+};
+
+exports.assert = function (guard, message) {
+ if (guard) {
+ exports.print('PASS ' + message, 'pass');
+ } else {
+ exports.print('FAIL ' + message, 'fail');
+ }
+};
+
+
+}}, ["system"]);
8 tests/modules1.0/wrappedTests/nested/a/b/c/d.js
@@ -0,0 +1,8 @@
+/* This file has been generated by yabbler.js */
+require.define({
+"a/b/c/d": function(require, exports, module) {
+exports.foo = function () {
+ return 1;
+};
+
+}}, []);
8 tests/modules1.0/wrappedTests/nested/program.js
@@ -0,0 +1,8 @@
+/* This file has been generated by yabbler.js */
+require.define({
+"program": function(require, exports, module) {
+var test = require('test');
+test.assert(require('a/b/c/d').foo() == 1, 'nested module identifier');
+test.print('DONE', 'info');
+
+}}, ["test", "a/b/c/d"]);
20 tests/modules1.0/wrappedTests/nested/test.js
@@ -0,0 +1,20 @@
+/* This file has been generated by yabbler.js */
+require.define({
+"test": function(require, exports, module) {
+
+exports.print = typeof print !== "undefined" ? print : function () {
+ var system = require("system");
+ var stdio = system.stdio;
+ stdio.print.apply(stdio, arguments);
+};
+
+exports.assert = function (guard, message) {
+ if (guard) {
+ exports.print('PASS ' + message, 'pass');
+ } else {
+ exports.print('FAIL ' + message, 'fail');
+ }
+};
+
+
+}}, ["system"]);
10 tests/modules1.0/wrappedTests/relative/program.js
@@ -0,0 +1,10 @@
+/* This file has been generated by yabbler.js */
+require.define({
+"program": function(require, exports, module) {
+var test = require('test');
+var a = require('submodule/a');
+var b = require('submodule/b');
+test.assert(a.foo == b.foo, 'a and b share foo through a relative require');
+test.print('DONE', 'info');
+
+}}, ["test", "submodule/a", "submodule/b"]);
6 tests/modules1.0/wrappedTests/relative/submodule/a.js
@@ -0,0 +1,6 @@
+/* This file has been generated by yabbler.js */
+require.define({
+"submodule/a": function(require, exports, module) {
+exports.foo = require('./b').foo;
+
+}}, ["submodule/b"]);
7 tests/modules1.0/wrappedTests/relative/submodule/b.js
@@ -0,0 +1,7 @@
+/* This file has been generated by yabbler.js */
+require.define({
+"submodule/b": function(require, exports, module) {
+exports.foo = function () {
+};
+
+}}, []);
20 tests/modules1.0/wrappedTests/relative/test.js
@@ -0,0 +1,20 @@
+/* This file has been generated by yabbler.js */
+require.define({
+"test": function(require, exports, module) {
+
+exports.print = typeof print !== "undefined" ? print : function () {
+ var system = require("system");
+ var stdio = system.stdio;
+ stdio.print.apply(stdio, arguments);
+};
+
+exports.assert = function (guard, message) {
+ if (guard) {
+ exports.print('PASS ' + message, 'pass');
+ } else {
+ exports.print('FAIL ' + message, 'fail');
+ }
+};
+
+
+}}, ["system"]);
6 tests/modules1.0/wrappedTests/transitive/a.js
@@ -0,0 +1,6 @@
+/* This file has been generated by yabbler.js */
+require.define({
+"a": function(require, exports, module) {
+exports.foo = require('b').foo;
+
+}}, ["b"]);
6 tests/modules1.0/wrappedTests/transitive/b.js
@@ -0,0 +1,6 @@
+/* This file has been generated by yabbler.js */
+require.define({
+"b": function(require, exports, module) {
+exports.foo = require('c').foo;
+
+}}, ["c"]);
8 tests/modules1.0/wrappedTests/transitive/c.js
@@ -0,0 +1,8 @@
+/* This file has been generated by yabbler.js */
+require.define({
+"c": function(require, exports, module) {
+exports.foo = function () {
+ return 1;
+};
+
+}}, []);
8 tests/modules1.0/wrappedTests/transitive/program.js
@@ -0,0 +1,8 @@
+/* This file has been generated by yabbler.js */
+require.define({
+"program": function(require, exports, module) {
+var test = require('test');
+test.assert(require('a').foo() == 1, 'transitive');
+test.print('DONE', 'info');
+
+}}, ["test", "a"]);
20 tests/modules1.0/wrappedTests/transitive/test.js
@@ -0,0 +1,20 @@
+/* This file has been generated by yabbler.js */
+require.define({
+"test": function(require, exports, module) {
+
+exports.print = typeof print !== "undefined" ? print : function () {
+ var system = require("system");
+ var stdio = system.stdio;
+ stdio.print.apply(stdio, arguments);
+};
+
+exports.assert = function (guard, message) {
+ if (guard) {
+ exports.print('PASS ' + message, 'pass');
+ } else {
+ exports.print('FAIL ' + message, 'fail');
+ }
+};
+
+
+}}, ["system"]);
11 tests/modules1.0/yabbler.bat
@@ -0,0 +1,11 @@
+java -jar ../../js.jar ../../yabbler.js -i "tests/absolute" -o "wrappedTests/absolute"
+java -jar ../../js.jar ../../yabbler.js -i "tests/cyclic" -o "wrappedTests/cyclic"
+java -jar ../../js.jar ../../yabbler.js -i "tests/determinism" -o "wrappedTests/determinism"
+java -jar ../../js.jar ../../yabbler.js -i "tests/exactExports" -o "wrappedTests/exactExports"
+java -jar ../../js.jar ../../yabbler.js -i "tests/hasOwnProperty" -o "wrappedTests/hasOwnProperty"
+java -jar ../../js.jar ../../yabbler.js -i "tests/method" -o "wrappedTests/method"
+java -jar ../../js.jar ../../yabbler.js -i "tests/missing" -o "wrappedTests/missing"
+java -jar ../../js.jar ../../yabbler.js -i "tests/monkeys" -o "wrappedTests/monkeys"
+java -jar ../../js.jar ../../yabbler.js -i "tests/nested" -o "wrappedTests/nested"
+java -jar ../../js.jar ../../yabbler.js -i "tests/relative" -o "wrappedTests/relative"
+java -jar ../../js.jar ../../yabbler.js -i "tests/transitive" -o "wrappedTests/transitive"
33 tests/print.js
@@ -0,0 +1,33 @@
+(function() {
+
+ var messages = [];
+ var outputDiv;
+
+ window.onload = function() {
+ //console.log('onload', messages.length);
+ var body = document.getElementsByTagName('body')[0];
+ outputDiv = document.createElement('div');
+ body.appendChild(outputDiv);
+
+ for (var i = 0, n = messages.length; i<n; i++) {
+ print(messages[i]);
+ }
+ };
+
+
+ window.print = function() {
+ if (outputDiv) {
+ var div = document.createElement('div');
+ for (var i = 0, n = arguments.length; i<n; i++) {
+ var span = document.createElement('span');
+ span.appendChild(document.createTextNode(arguments[i]));
+ div.appendChild(span);
+ }
+ outputDiv.appendChild(div);
+ }
+ else {
+ messages.push(Array.prototype.slice(arguments, 0));
+ }
+ }
+
+})();
452 yabble.js
@@ -0,0 +1,452 @@
+ /*
+ * Copyright (c) 2010 James Brantly
+ *
+ * Permission is hereby granted, free of charge, to any person
+ * obtaining a copy of this software and associated documentation
+ * files (the "Software"), to deal in the Software without
+ * restriction, including without limitation the rights to use,
+ * copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following
+ * conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+ * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+ * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+(function(globalEval) {
+
+ var Yabble = function() {
+ throw "Synchronous require() is not supported.";
+ };
+
+ var _moduleRoot = '',
+ _modules,
+ _callbacks,
+ _fetchFunc,
+ _timeoutLength = 20000;
+
+
+ var head = document.getElementsByTagName('head')[0];
+
+ // Shortcut to native hasOwnProperty
+ var hasOwnProperty = Object.prototype.hasOwnProperty;
+
+ // A for..in implementation which uses hasOwnProperty and fixes IE non-enumerable issues
+ if ((function() {for (var prop in {hasOwnProperty: true}) { return prop; }})() == 'hasOwnProperty') {
+ var forIn = function(obj, func, ctx) {
+ for (var prop in obj) {
+ if (hasOwnProperty.call(obj, prop)) {
+ func.call(ctx, prop);
+ }
+ }
+ };
+ }
+ else {
+ var ieBadProps = [
+ 'isPrototypeOf',
+ 'hasOwnProperty',
+ 'toLocaleString',
+ 'toString',
+ 'valueOf'
+ ];
+
+ var forIn = function(obj, func, ctx) {
+ for (var prop in obj) {
+ if (hasOwnProperty.call(obj, prop)) {
+ func.call(ctx, prop);
+ }
+ }
+
+ for (var i = ieBadProps.length; i--;) {
+ var prop = ieBadProps[i];
+ if (hasOwnProperty.call(obj, prop)) {
+ func.call(ctx, prop);
+ }
+ }
+ };
+ }
+
+ // Takes a relative path to a module and resolves it according to the reference path
+ var resolveModuleId = function(relModuleId, refPath) {
+ if (relModuleId.charAt(0) != '.') {
+ return relModuleId;
+ }
+ else {
+ var relativeIdParts = relModuleId.split('/');
+ refPath = refPath || '';
+ if (refPath.length && refPath.charAt(refPath.length-1) != '/') {
+ refPath += '/';
+ }
+ var refPathParts = refPath.split('/');
+ refPathParts.pop();
+ var part;
+ while (part = relativeIdParts.shift()) {
+ if (part == '.') { continue; }
+ else if (part == '..'
+ && refPathParts.length
+ && refPathParts[refPathParts.length-1] != '..') { refPathParts.pop(); }
+ else { refPathParts.push(part); }
+ }
+ return refPathParts.join('/');
+ }
+ };
+
+ // Takes a module's ID and resolves a URI according to the module root path
+ var resolveModuleUri = function(moduleId) {
+ if (moduleId.charAt(0) != '.') {
+ return _moduleRoot+moduleId+'.js';
+ }
+ else {
+ return this._resolveModuleId(moduleId, _moduleRoot)+'.js';
+ }
+ };
+
+ // Returns a module object from the module ID
+ var getModule = function(moduleId) {
+ if (!hasOwnProperty.call(_modules, moduleId)) {
+ return null;
+ }
+ return _modules[moduleId];
+ };
+
+ // Adds a callback which is executed when all deep dependencies are loaded
+ var addCallback = function(deps, cb) {
+ _callbacks.push([deps.slice(0), cb]);
+ };
+
+ // Generic implementation of require.ensure() which takes a reference path to
+ // use when resolving relative module IDs
+ var ensureImpl = function(deps, cb, refPath) {
+ var unreadyModules = [];
+
+ for (var i = deps.length; i--;) {
+ var moduleId = resolveModuleId(deps[i], refPath),
+ module = getModule(moduleId);
+
+ if (!areDeepDepsDefined(module)) {
+ unreadyModules.push(moduleId);
+ }
+ }
+
+ if (unreadyModules.length) {
+ addCallback(unreadyModules, function() {
+ cb(createRequireFunc(refPath));
+ });
+ queueModules(unreadyModules);
+ }
+ else {
+ setTimeout(cb, 0);
+ }
+ };
+
+ // Creates a require function that is passed into module factory functions
+ // and require.ensure() callbacks. It is bound to a reference path for
+ // relative require()s
+ var createRequireFunc = function(refPath) {
+ var require = function(relModuleId) {
+ var moduleId = resolveModuleId(relModuleId, refPath),
+ module = getModule(moduleId);
+
+ if (!module) {
+ throw "Module not loaded";
+ }
+ else if (module.error) {
+ throw "Error loading module";
+ }
+
+ if (!module.exports) {
+ module.exports = {};
+ var moduleDir = moduleId.substring(0, moduleId.lastIndexOf('/')+1);
+ module.factory(createRequireFunc(moduleDir), module.exports);
+ }
+ return module.exports;
+ };
+
+ require.ensure = function(deps, cb) {
+ ensureImpl(deps, cb, refPath);
+ };
+
+ return require;
+ };
+
+ // Begins loading modules asynchronously
+ var queueModules = function(moduleIds) {
+ for (var i = moduleIds.length; i--;) {
+ var moduleId = moduleIds[i],
+ module = getModule(moduleId);
+
+ if (module == null) {
+ module = _modules[moduleId] = {};
+ _fetchFunc(moduleId);
+ }
+ }
+ };
+
+ // Returns true if all deep dependencies are satisfied (in other words,
+ // can more or less safely run the module factory function)
+ var areDeepDepsDefined = function(moduleId) {
+ var visitedModules = {};
+ var self = this;
+ var recurse = function(moduleId) {
+ if (visitedModules[moduleId] == true) { return true; }
+ visitedModules[moduleId] = true;
+ var module = getModule(moduleId);
+ if (!module || !module.defined) { return false; }
+ else {
+ var deps = module.deps || [];
+ for (var i = deps.length; i--;) {
+ if (!recurse(deps[i])) {
+ return false;
+ }
+ }
+ return true;
+ }
+ };
+ return recurse(moduleId);
+ };
+
+ // Checks dependency callbacks and fires as necessary
+ var fireCallbacks = function() {
+ var i = 0;
+ while (i<_callbacks.length) {
+ var deps = _callbacks[i][0],
+ func = _callbacks[i][1],
+ n = 0;
+ while (n<deps.length) {
+ if (areDeepDepsDefined(deps[n])) {
+ deps.splice(n, 1);
+ }
+ else {
+ n++;
+ }
+ }
+ if (!deps.length) {
+ _callbacks.splice(i, 1);
+ if (func != null) {
+ setTimeout(func, 0);
+ }
+ }
+ else {
+ i++;
+ }
+ }
+ };
+
+ // Load an unwrapped module using XHR and eval()
+ var loadModuleByEval = _fetchFunc = function(moduleId) {
+ var timeoutHandle;
+
+ var errorFunc = function() {
+ var module = getModule(moduleId);
+ if (!module.defined) {
+ module.defined = module.error = true;
+ fireCallbacks();
+ }
+ };
+
+ var xhr = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP');
+ xhr.open('GET', resolveModuleUri(moduleId), true);
+ xhr.onreadystatechange = function() {
+ if (xhr.readyState === 4) {
+ clearTimeout(timeoutHandle);
+ if (xhr.status == 200) {
+ var moduleCode = xhr.responseText,
+ deps = determineShallowDependencies(moduleCode),
+ moduleDir = moduleId.substring(0, moduleId.lastIndexOf('/')+1),
+ moduleDefs = {};
+ for (var i = deps.length; i--;) {
+ deps[i] = resolveModuleId(deps[i], moduleDir);
+ }
+
+ moduleDefs[moduleId] = globalEval('({fn: function(require, exports, module) {\r\n' + moduleCode + '\r\n}})').fn;
+
+ Yabble.define(moduleDefs, deps);
+ }
+ else {
+ errorFunc();
+ }
+ }
+ };
+
+ timeoutHandle = setTimeout(errorFunc, _timeoutLength);
+
+ xhr.send(null);
+ };
+
+ // Used by loadModuleByEval and by the packager. Determines shallow dependencies of
+ // a module via static analysis. This can currently break with require.ensure().
+ var determineShallowDependencies = function(moduleCode) {
+ // TODO: account for comments
+ var deps = {}, match, unique = {};
+
+ var requireRegex = /(?:^|[^\w\$_.])require\s*\(\s*("[^"\\]*(?:\\.[^"\\]*)*"|'[^'\\]*(?:\\.[^'\\]*)*')\s*\)/g;
+ while (match = requireRegex.exec(moduleCode)) {
+ var module = eval(match[1]);
+ if (!hasOwnProperty.call(deps, module)) {
+ deps[module] = true;
+ }
+ }
+
+ var ensureRegex = /(?:^|[^\w\$_.])require.ensure\s*\(\s*(\[("[^"\\]*(?:\\.[^"\\]*)*"|'[^'\\]*(?:\\.[^'\\]*)*'|\s*|,)*\])/g;
+ while (match = ensureRegex.exec(moduleCode)) {
+ var moduleArray = eval(match[1]);
+ for (var i = moduleArray.length; i--;) {
+ var module = moduleArray[i];
+ delete deps[module];
+ }
+ }
+
+ var depsArray = [];
+ forIn(deps, function(module) {
+ depsArray.push(module);
+ });
+
+ return depsArray;
+ };
+
+ // Load a wrapped module via script tags
+ var loadModuleByScript = function(moduleId) {
+ var scriptEl = document.createElement('script');
+ scriptEl.type = 'text/javascript';
+ scriptEl.src = resolveModuleUri(moduleId);
+
+ var useStandard = !!scriptEl.addEventListener,
+ timeoutHandle;
+
+ var errorFunc = function() {
+ postLoadFunc(false);
+ };
+
+ var loadFunc = function() {
+ if (useStandard || (scriptEl.readyState == 'complete' || scriptEl.readyState == 'loaded')) {
+ postLoadFunc(getModule(moduleId).defined);
+ }
+ };
+
+ var postLoadFunc = function(loaded) {
+ clearTimeout(timeoutHandle);
+
+ if (useStandard) {
+ scriptEl.removeEventListener('load', loadFunc, false);
+ scriptEl.removeEventListener('error', errorFunc, false);
+ }
+ else {
+ scriptEl.detachEvent('onreadystatechange', loadFunc);
+ }
+
+ if (!loaded) {
+ var module = getModule(moduleId);
+ if (!module.defined) {
+ module.defined = module.error = true;
+ fireCallbacks();
+ }
+ }
+ };
+
+ if (useStandard) {
+ scriptEl.addEventListener('load', loadFunc, false);
+ scriptEl.addEventListener('error', errorFunc, false);
+ }
+ else {
+ scriptEl.attachEvent('onreadystatechange', loadFunc);
+ }
+
+ timeoutHandle = setTimeout(errorFunc, _timeoutLength);
+
+ head.appendChild(scriptEl);
+ };
+
+ // Set the uri which forms the conceptual module namespace root
+ Yabble.setModuleRoot = function(path) {
+ if (path.length && path.charAt(path.length-1) != '/') {
+ path += '/';
+ }
+ _moduleRoot = path;
+ };
+
+ // Set a timeout period for async module loading
+ Yabble.setTimeoutLength = function(milliseconds) {
+ _timeoutLength = milliseconds;
+ };
+
+ // Use script tags with wrapped code instead of XHR+eval()
+ Yabble.useScriptTags = function() {
+ _fetchFunc = loadModuleByScript;
+ };
+
+ // Define a module per various transport specifications
+ Yabble.define = function(moduleDefs, deps) {
+
+ var unreadyModules = [],
+ definedModules = [];
+
+ deps = deps || [];
+
+ forIn(moduleDefs, function(moduleId) {
+ var module = getModule(moduleId);
+ if (!module) {
+ module = _modules[moduleId] = {};
+ }
+ module.defined = true;
+ module.deps = deps.slice(0);
+ module.factory = moduleDefs[moduleId];
+ definedModules.push(module);
+ }, this);
+
+ for (var i = deps.length; i--;) {
+ var moduleId = deps[i],
+ module = getModule(moduleId);
+
+ if (!module || !areDeepDepsDefined(moduleId)) {
+ unreadyModules.push(moduleId);
+ }
+ }
+
+ if (unreadyModules.length) {
+ queueModules(unreadyModules);
+ }
+
+ fireCallbacks();
+ };
+
+ // Do an async lazy-load of modules
+ Yabble.ensure = function(deps, cb) {
+ ensureImpl(deps, cb, '');
+ };
+
+ // Start an application via a main program module
+ Yabble.run = function(program, cb) {
+ Yabble.ensure([program], function(require) {
+ require(program);
+ if (cb != null) { cb(); }
+ });
+ };
+
+ // Reset internal state. Used mostly for unit tests.
+ Yabble.reset = function() {
+ _modules = {};
+ _callbacks = [];
+
+ // Built-in system module
+ Yabble.define({
+ 'system': function(require, exports, module) {}
+ });
+ };
+
+ Yabble.reset();
+
+ // Export to the require global
+ window.require = Yabble;
+})(function(code) {
+ return (window.eval || eval)(code, null);
+});
181 yabbler.js
@@ -0,0 +1,181 @@
+ /*
+ * Copyright (c) 2010 James Brantly
+ *
+ * Permission is hereby granted, free of charge, to any person
+ * obtaining a copy of this software and associated documentation
+ * files (the "Software"), to deal in the Software without
+ * restriction, including without limitation the rights to use,
+ * copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following
+ * conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+ * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+ * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+function createWrappedFile(inFileObj, outFileObj, referencePath) {
+ var moduleName = new String(inFileObj.getName());
+ moduleName = moduleName.substring(0, moduleName.length-3);
+
+ var moduleId = resolveModuleId('./'+moduleName, referencePath)
+ var moduleCode = readFile(inFileObj);
+ var deps = determineShallowDependencies(moduleCode);
+ deps = deps.map(function(dep) {
+ return '"' + resolveModuleId(dep, referencePath) +'"';
+ });
+
+ var newModuleCode = '/* This file has been generated by yabbler.js */';
+ newModuleCode += '\r\nrequire.define({';
+ newModuleCode += '\r\n"' + moduleId + '": function(require, exports, module) {';
+ newModuleCode += '\r\n'+moduleCode;
+ newModuleCode += '\r\n}}, [' + deps.join(', ') + ']);';
+
+ writeFile(outFileObj, newModuleCode);
+};
+
+function determineShallowDependencies(moduleCode) {
+ // need to account for comments
+ var deps = {}, match, unique = {};
+
+ var requireRegex = /(?:^|[^\w\$_.])require\s*\(\s*("[^"\\]*(?:\\.[^"\\]*)*"|'[^'\\]*(?:\\.[^'\\]*)*')\s*\)/g;
+ while (match = requireRegex.exec(moduleCode)) {
+ var module = eval(match[1]);
+ if (!Object.prototype.hasOwnProperty.call(deps, module)) {
+ deps[module] = true;
+ }
+ }
+
+ var ensureRegex = /(?:^|[^\w\$_.])require.ensure\s*\(\s*(\[("[^"\\]*(?:\\.[^"\\]*)*"|'[^'\\]*(?:\\.[^'\\]*)*'|\s*|,)*\])/g;
+ while (match = ensureRegex.exec(moduleCode)) {
+ var moduleArray = eval(match[1]);
+ for (var i = moduleArray.length; i--;) {
+ var module = moduleArray[i];
+ delete deps[module];
+ }
+ }
+
+ var depsArray = [];
+ for (var module in deps) {
+ if (Object.prototype.hasOwnProperty.call(deps, module)) {
+ depsArray.push(module);
+ }
+ }
+
+ return depsArray;
+};
+
+function resolveModuleId(path, referencePath) {
+ if (path[0] != '.') {
+ return path;
+ }
+ else {
+ var pathParts = path.split('/');
+ referencePath = referencePath || '';
+ if (referencePath.length && referencePath[referencePath.length-1] != '/') {
+ referencePath += '/';
+ }
+ var referencePathParts = referencePath.split('/');
+ referencePathParts.pop();
+ var part;
+ while (part = pathParts.shift()) {
+ if (part == '.') { continue; }
+ else if (part == '..'
+ && referencePathParts.length
+ && referencePathParts[referencePathParts.length-1] != '..') { referencePathParts.pop(); }
+ else { referencePathParts.push(part); }
+ }
+ return referencePathParts.join('/');
+ }
+};
+
+function readFile(fileObj) {
+ var encoding = 'UTF-8';
+ var result = null;
+ try {
+ var inStream = new java.io.InputStreamReader(new java.io.FileInputStream(fileObj), encoding);
+ var writer = new java.io.StringWriter();
+
+ var n,
+ buffer = java.lang.reflect.Array.newInstance(java.lang.Character.TYPE, 4096);
+
+ while ((n = inStream.read(buffer, 0, buffer.length)) != -1) {
+ writer.write(buffer, 0, n);
+ }
+
+ result = new String(writer.toString());
+ }
+ finally {
+ if (inStream) {
+ inStream.close();
+ }
+ }
+
+ return result;
+};
+
+function writeFile(fileObj, contents) {
+ var encoding = 'UTF-8';
+ try {
+ fileObj.getParentFile().mkdirs();
+ var outStream = new java.io.FileOutputStream(fileObj);
+ outStream.write((new java.lang.String(contents)).getBytes(encoding));
+ }
+ finally {
+ if (outStream) {
+ outStream.close();
+ }
+ }
+};
+
+function processDirectory(inFileObj, outFileObj, referencePath) {
+ var jsFilter = new java.io.FileFilter() {
+ accept: function(fileObj) {
+ return fileObj.isFile() && fileObj.getName().endsWith('.js');
+ }
+ };
+
+ var jsFiles = inFileObj.listFiles(jsFilter);
+
+ jsFiles.forEach(function(jsFile) {
+ createWrappedFile(jsFile, new java.io.File(outFileObj, jsFile.getName()), referencePath);
+ });
+
+ var dirFilter = new java.io.FileFilter() {
+ accept: function(fileObj) {
+ return fileObj.isDirectory();
+ }
+ };
+
+ var subDirs = inFileObj.listFiles(dirFilter);
+
+ subDirs.forEach(function(subDir) {
+ processDirectory(subDir, new java.io.File(outFileObj, subDir.getName()), new String(referencePath+subDir.getName()+'/'));
+ });
+};
+
+var inputDir = 'source';
+var outputDir = 'build';
+
+for (var i = 0, n = arguments.length; i<n; i++) {
+ var arg = arguments[i];
+ if (arg == '-i') {
+ i++;
+ inputDir = arguments[i];
+ }
+ else if (arg == '-o') {
+ i++;
+ outputDir = arguments[i];
+ }
+}
+
+processDirectory(new java.io.File(inputDir), new java.io.File(outputDir), '');

0 comments on commit 3a5178b

Please sign in to comment.
Something went wrong with that request. Please try again.