configRhino.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. function rhinoDojoConfig(config, baseUrl, rhinoArgs){
  2. // summary:
  3. // This module provides bootstrap configuration for running dojo in rhino.
  4. // TODO: v1.6 tries to set dojo.doc and dojo.body in rhino; why?
  5. // get a minimal console up
  6. var log = function(hint, args){
  7. print((hint ? hint + ":" : "") + args[0]);
  8. for(var i = 1; i < args.length; i++){
  9. print(", " + args[i]);
  10. }
  11. };
  12. // intentionally define console in the global namespace
  13. console= {
  14. log: function(){ log(0, arguments); },
  15. error: function(){ log("ERROR", arguments); },
  16. warn: function(){ log("WARN", arguments); }
  17. };
  18. // any command line arguments with the load flag are pushed into deps
  19. for(var deps = [], i = 0; i < rhinoArgs.length; i++){
  20. var arg = (rhinoArgs[i] + "").split("=");
  21. if(arg[0] == "load"){
  22. deps.push(arg[1]);
  23. }else if(arg[0] == "mapPackage") {
  24. var parts = arg[1].split(":"),
  25. name = parts[0],
  26. location=parts[1],
  27. isPrexisting = false;
  28. for (var j = 0; j < config.packages.length; j++) {
  29. var pkg = config.packages[j];
  30. if (pkg.name === name) {
  31. pkg.location = location;
  32. isPrexisting = true;
  33. break;
  34. }
  35. }
  36. if (!isPrexisting) {
  37. config.packages.push({
  38. name: name,
  39. location: location
  40. });
  41. }
  42. }
  43. }
  44. // provides timed callbacks using Java threads
  45. if(typeof setTimeout == "undefined" || typeof clearTimeout == "undefined"){
  46. var timeouts = [];
  47. clearTimeout = function(idx){
  48. if(!timeouts[idx]){ return; }
  49. timeouts[idx].stop();
  50. };
  51. setTimeout = function(func, delay){
  52. var def = {
  53. sleepTime:delay,
  54. hasSlept:false,
  55. run:function(){
  56. if(!this.hasSlept){
  57. this.hasSlept = true;
  58. java.lang.Thread.currentThread().sleep(this.sleepTime);
  59. }
  60. try{
  61. func();
  62. }catch(e){
  63. console.debug("Error running setTimeout thread:" + e);
  64. }
  65. }
  66. };
  67. var runnable = new java.lang.Runnable(def);
  68. var thread = new java.lang.Thread(runnable);
  69. thread.start();
  70. return timeouts.push(thread) - 1;
  71. };
  72. }
  73. var isLocal = function(url){
  74. return (new java.io.File(url)).exists();
  75. };
  76. // reset the has cache with node-appropriate values;
  77. var hasCache = {
  78. "host-rhino":1,
  79. "host-browser":0,
  80. "dom":0,
  81. "dojo-has-api":1,
  82. "dojo-xhr-factory":0,
  83. "dojo-inject-api":1,
  84. "dojo-timeout-api":0,
  85. "dojo-trace-api":1,
  86. "dojo-loader-catches":1,
  87. "dojo-dom-ready-api":0,
  88. "dojo-publish-privates":1,
  89. "dojo-sniff":0,
  90. "dojo-loader":1,
  91. "dojo-test-xd":0,
  92. "dojo-test-sniff":0
  93. };
  94. for(var p in hasCache){
  95. config.hasCache[p] = hasCache[p];
  96. }
  97. // reset some configuration switches with rhino-appropriate values
  98. var rhinoConfig = {
  99. baseUrl:baseUrl,
  100. commandLineArgs:rhinoArgs,
  101. deps:deps,
  102. timeout:0,
  103. locale:String(java.util.Locale.getDefault().toString().replace('_', '-').toLowerCase()),
  104. loaderPatch:{
  105. injectUrl: function(url, callback){
  106. try{
  107. if(isLocal(url)){
  108. load(url);
  109. }else{
  110. require.eval(readUrl(url, "UTF-8"));
  111. }
  112. callback();
  113. }catch(e){
  114. console.log("failed to load resource (" + url + ")");
  115. console.log(e);
  116. }
  117. },
  118. getText: function(url, sync, onLoad){
  119. // TODO: test https://bugzilla.mozilla.org/show_bug.cgi?id=471005; see v1.6 hostenv_rhino
  120. // note: async mode not supported in rhino
  121. onLoad(isLocal(url) ? readFile(url, "UTF-8") : readUrl(url, "UTF-8"));
  122. }
  123. }
  124. };
  125. for(p in rhinoConfig){
  126. config[p] = rhinoConfig[p];
  127. }
  128. }