summaryrefslogtreecommitdiff
path: root/lib/dojo/_base/configRhino.js
blob: 2cbbf8887bf38d26c8f465155851692be117b076 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
function rhinoDojoConfig(config, baseUrl, rhinoArgs){
	// summary:
	//		This module provides bootstrap configuration for running dojo in rhino.

	// TODO: v1.6 tries to set dojo.doc and dojo.body in rhino; why?

	// get a minimal console up
	var log = function(hint, args){
		print((hint ? hint + ":" : "") + args[0]);
		for(var i = 1; i < args.length; i++){
			print(", " + args[i]);
		}
	};
	// intentionally define console in the global namespace
	console= {
		log: function(){ log(0, arguments); },
		error: function(){ log("ERROR", arguments); },
		warn: function(){ log("WARN", arguments); }
	};

	// any command line arguments with the load flag are pushed into deps
	for(var deps = [], i = 0; i < rhinoArgs.length; i++){
		var arg = (rhinoArgs[i] + "").split("=");
		if(arg[0] == "load"){
			deps.push(arg[1]);
		}
	}

	// provides timed callbacks using Java threads
	if(typeof setTimeout == "undefined" || typeof clearTimeout == "undefined"){
		var timeouts = [];
		clearTimeout = function(idx){
			if(!timeouts[idx]){ return; }
			timeouts[idx].stop();
		};

		setTimeout = function(func, delay){
			var def = {
				sleepTime:delay,
				hasSlept:false,

				run:function(){
					if(!this.hasSlept){
						this.hasSlept = true;
						java.lang.Thread.currentThread().sleep(this.sleepTime);
					}
					try{
						func();
					}catch(e){
						console.debug("Error running setTimeout thread:" + e);
					}
				}
			};

			var runnable = new java.lang.Runnable(def);
			var thread = new java.lang.Thread(runnable);
			thread.start();
			return timeouts.push(thread) - 1;
		};
	}

	var isLocal = function(url){
		return (new java.io.File(url)).exists();
	};

	// reset the has cache with node-appropriate values;
	var hasCache = {
		"host-rhino":1,
		"host-browser":0,
		"dom":0,
		"dojo-has-api":1,
		"dojo-xhr-factory":0,
		"dojo-inject-api":1,
		"dojo-timeout-api":0,
		"dojo-trace-api":1,
		"dojo-loader-catches":1,
		"dojo-dom-ready-api":0,
		"dojo-publish-privates":1,
		"dojo-sniff":0,
		"dojo-loader":1,
		"dojo-test-xd":0,
		"dojo-test-sniff":0
	};
	for(var p in hasCache){
		config.hasCache[p] = hasCache[p];
	}

	// reset some configuration switches with rhino-appropriate values
	var rhinoConfig = {
		baseUrl:baseUrl,
		commandLineArgs:rhinoArgs,
		deps:deps,
		timeout:0,
		locale:String(java.util.Locale.getDefault().toString().replace('_', '-').toLowerCase()),

		loaderPatch:{
			injectUrl: function(url, callback){
				try{
					if(isLocal(url)){
						load(url);
					}else{
						require.eval(readUrl(url, "UTF-8"));
					}
					callback();
				}catch(e){
					console.log("failed to load resource (" + url + ")");
					console.log(e);
				}
			},

			getText: function(url, sync, onLoad){
				// TODO: test https://bugzilla.mozilla.org/show_bug.cgi?id=471005; see v1.6 hostenv_rhino
				// note: async mode not supported in rhino
				onLoad(isLocal(url) ? readFile(url, "UTF-8") : readUrl(url, "UTF-8"));
			}
		}
	};
	for(p in rhinoConfig){
		config[p] = rhinoConfig[p];
	}
}