configRhino.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. }
  24. }
  25. // provides timed callbacks using Java threads
  26. if(typeof setTimeout == "undefined" || typeof clearTimeout == "undefined"){
  27. var timeouts = [];
  28. clearTimeout = function(idx){
  29. if(!timeouts[idx]){ return; }
  30. timeouts[idx].stop();
  31. };
  32. setTimeout = function(func, delay){
  33. var def = {
  34. sleepTime:delay,
  35. hasSlept:false,
  36. run:function(){
  37. if(!this.hasSlept){
  38. this.hasSlept = true;
  39. java.lang.Thread.currentThread().sleep(this.sleepTime);
  40. }
  41. try{
  42. func();
  43. }catch(e){
  44. console.debug("Error running setTimeout thread:" + e);
  45. }
  46. }
  47. };
  48. var runnable = new java.lang.Runnable(def);
  49. var thread = new java.lang.Thread(runnable);
  50. thread.start();
  51. return timeouts.push(thread) - 1;
  52. };
  53. }
  54. var isLocal = function(url){
  55. return (new java.io.File(url)).exists();
  56. };
  57. // reset the has cache with node-appropriate values;
  58. var hasCache = {
  59. "host-rhino":1,
  60. "host-browser":0,
  61. "dom":0,
  62. "dojo-has-api":1,
  63. "dojo-xhr-factory":0,
  64. "dojo-inject-api":1,
  65. "dojo-timeout-api":0,
  66. "dojo-trace-api":1,
  67. "dojo-loader-catches":1,
  68. "dojo-dom-ready-api":0,
  69. "dojo-publish-privates":1,
  70. "dojo-sniff":0,
  71. "dojo-loader":1,
  72. "dojo-test-xd":0,
  73. "dojo-test-sniff":0
  74. };
  75. for(var p in hasCache){
  76. config.hasCache[p] = hasCache[p];
  77. }
  78. // reset some configuration switches with rhino-appropriate values
  79. var rhinoConfig = {
  80. baseUrl:baseUrl,
  81. commandLineArgs:rhinoArgs,
  82. deps:deps,
  83. timeout:0,
  84. locale:String(java.util.Locale.getDefault().toString().replace('_', '-').toLowerCase()),
  85. loaderPatch:{
  86. injectUrl: function(url, callback){
  87. try{
  88. if(isLocal(url)){
  89. load(url);
  90. }else{
  91. require.eval(readUrl(url, "UTF-8"));
  92. }
  93. callback();
  94. }catch(e){
  95. console.log("failed to load resource (" + url + ")");
  96. console.log(e);
  97. }
  98. },
  99. getText: function(url, sync, onLoad){
  100. // TODO: test https://bugzilla.mozilla.org/show_bug.cgi?id=471005; see v1.6 hostenv_rhino
  101. // note: async mode not supported in rhino
  102. onLoad(isLocal(url) ? readFile(url, "UTF-8") : readUrl(url, "UTF-8"));
  103. }
  104. }
  105. };
  106. for(p in rhinoConfig){
  107. config[p] = rhinoConfig[p];
  108. }
  109. }