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