forked from jeresig/env-js
/
rhino.js
461 lines (390 loc) · 12.5 KB
/
rhino.js
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
/*
* Envjs rhino-env.1.3.pre03
* Pure JavaScript Browser Environment
* By John Resig <http://ejohn.org/> and the Envjs Team
* Copyright 2008-2010 John Resig, under the MIT License
*/
var Envjs = Envjs ||
require('envjs/platform/core').Envjs;
require('local_settings');
var __context__ = Packages.org.mozilla.javascript.Context.getCurrentContext();
Envjs.platform = "Rhino";
Envjs.revision = "1.7.0.rc2";
Envjs.argv = [];
if(__argv__ && __argv__.length){
for(var i = 0; i < __argv__.length; i++){
Envjs.argv[i] = __argv__[i];
}
}
Envjs.exit = function(){
java.lang.System.exit(0);
};
/*
* Envjs rhino-env.1.3.pre03
* Pure JavaScript Browser Environment
* By John Resig <http://ejohn.org/> and the Envjs Team
* Copyright 2008-2010 John Resig, under the MIT License
*/
//CLOSURE_START
(function(){
/**
* @author john resig
*/
// Helper method for extending one object with another.
function __extend__(a,b) {
for ( var i in b ) {
if(b.hasOwnProperty(i)){
var g = b.__lookupGetter__(i), s = b.__lookupSetter__(i);
if ( g || s ) {
if ( g ) { a.__defineGetter__(i, g); }
if ( s ) { a.__defineSetter__(i, s); }
} else {
a[i] = b[i];
}
}
}
return a;
}
/**
* Writes message to system out.
*
* @param {Object} message
*/
(function(){
Envjs.log = print;
Envjs.lineSource = function(e){
return e&&e.rhinoException?e.rhinoException.lineSource():"(line ?)";
};
var $in, log;
Envjs.readConsole = function(){
log = log||Envjs.logger('Envjs.Rhino');
$in = $in||new java.io.BufferedReader(
new java.io.InputStreamReader(java.lang.System['in'])
);
return $in.readLine()+'';
};
Envjs.prompt = function(){
java.lang.System.out.print(Envjs.CURSOR+' ');
java.lang.System.out.flush();
};
}());
(function(){
var log = Envjs.logger('Envjs.HTML.Rhino');
Envjs.eval = function(context, source, name){
//console.log('evaluating javascript source %s', source.substring(0,64));
return __context__.evaluateString(
context,
source,
name,
0,
null
);
};
}());Envjs.renderSVG = function(svgstring, url){
//console.log("svg template url %s", templateSVG);
// Create a JPEG transcoder
var t = new Packages.org.apache.batik.transcoder.image.JPEGTranscoder();
// Set the transcoding hints.
t.addTranscodingHint(
Packages.org.apache.batik.transcoder.image.JPEGTranscoder.KEY_QUALITY,
new java.lang.Float(1.0));
// Create the transcoder input.
var input = new Packages.org.apache.batik.transcoder.TranscoderInput(
new java.io.StringReader(svgstring));
// Create the transcoder output.
var ostream = new java.io.ByteArrayOutputStream();
var output = new Packages.org.apache.batik.transcoder.TranscoderOutput(ostream);
// Save the image.
t.transcode(input, output);
// Flush and close the stream.
ostream.flush();
ostream.close();
var out = new java.io.FileOutputStream(new java.io.File(new java.net.URI(url.toString())));
try{
out.write( ostream.toByteArray() );
}catch(e){
}finally{
out.flush();
out.close();
}
};
(function(){
var log = Envjs.logger('Envjs.Timer.Rhino');
/**
* Rhino provides a very succinct 'sync'
* @param {Function} fn
*/
try{
Envjs.sync = sync;
Envjs.spawn = spawn;
//print('sync and spawn are available');
} catch(e){
//print('sync and spawn are not available : ' + e);
//sync unavailable on AppEngine
Envjs.sync = function(fn){
console.log('Threadless platform, sync is safe');
return fn;
};
Envjs.spawn = function(fn){
console.log('Threadless platform, spawn shares main thread.');
return fn();
};
};
/**
* sleep thread for specified duration
* @param {Object} milliseconds
*/
Envjs.sleep = function(milliseconds){
try{
return java.lang.Thread.currentThread().sleep(milliseconds);
}catch(e){
console.log('Threadless platform, cannot sleep.');
}
};
/**
* provides callback hook for when the system exits
*/
Envjs.onExit = function(callback){
var rhino = Packages.org.mozilla.javascript,
contextFactory = __context__.getFactory(),
listener = new rhino.ContextFactory.Listener({
contextReleased: function(context){
if(context === __context__)
console.log('context released', context);
contextFactory.removeListener(this);
if(callback)
callback();
}
});
contextFactory.addListener(listener);
};
}());
(function(){
var log = Envjs.logger('Envjs.XMLHttpRequest.Rhino');
/**
* Get 'Current Working Directory'
*/
Envjs.getcwd = function() {
return java.lang.System.getProperty('user.dir');
}
/**
* Used to write to a local file
* @param {Object} text
* @param {Object} url
*/
Envjs.writeToFile = function(text, url){
//Envjs.debug("writing text to url : " + url);
var out = new java.io.FileWriter(
new java.io.File(
new java.net.URI(url.toString())));
out.write( text, 0, text.length );
out.flush();
out.close();
};
/**
* Used to write to a local file
* @param {Object} text
* @param {Object} suffix
*/
Envjs.writeToTempFile = function(text, suffix){
//console.log("writing text to temp url : %s");
// Create temp file.
var temp = java.io.File.createTempFile("envjs-tmp", suffix);
// Delete temp file when program exits.
temp.deleteOnExit();
// Write to temp file
var out = new java.io.FileWriter(temp);
out.write(text, 0, text.length);
out.close();
return temp.getAbsolutePath().toString()+'';
};
/**
* Used to read the contents of a local file
* @param {Object} url
*/
Envjs.readFromFile = function( url ){
if(typeof url == 'string')
url = Envjs.uri(url);
//console.log("reading from url : %s", url);
var fileReader = new java.io.FileReader(
new java.io.File(
new java.net.URI( url )));
var stringwriter = new java.io.StringWriter(),
buffer = java.lang.reflect.Array.newInstance(java.lang.Character.TYPE, 1024),
length;
while ((length = fileReader.read(buffer, 0, 1024)) != -1) {
stringwriter.write(buffer, 0, length);
}
stringwriter.close();
return stringwriter.toString()+"";
};
/**
* Used to delete a local file
* @param {Object} url
*/
Envjs.deleteFile = function(url){
var file = new java.io.File( new java.net.URI( url ) );
file["delete"]();
};
/**
* establishes connection and calls responsehandler
* @param {Object} xhr
* @param {Object} responseHandler
* @param {Object} data
*/
Envjs.connection = function(xhr, responseHandler, data){
var url = java.net.URL(xhr.url),
connection,
header,
outstream,
buffer,
length,
binary = false,
name, value,
contentEncoding,
instream,
responseXML,
i;
if ( /^file\:/.test(url) ) {
Envjs.localXHR(url, xhr, connection, data);
} else {
connection = url.openConnection();
//handle redirects manually since cookie support sucks out of the box
connection.setFollowRedirects(false);
connection.setRequestMethod( xhr.method );
// Add headers to Java connection
for (header in xhr.headers){
connection.addRequestProperty(header+'', xhr.headers[header]+'');
}
connection.addRequestProperty("Accept-Encoding", 'gzip');
//write data to output stream if required
//TODO: if they have set the request header for a chunked
//request body, implement a chunked output stream
if(data){
if(data instanceof Document){
if ( xhr.method == "PUT" || xhr.method == "POST" ) {
connection.setDoOutput(true);
outstream = connection.getOutputStream(),
xml = (new XMLSerializer()).serializeToString(data);
buffer = new java.lang.String(xml).getBytes('UTF-8');
outstream.write(buffer, 0, buffer.length);
outstream.close();
}
}else if(data.length&&data.length>0){
if ( xhr.method == "PUT" || xhr.method == "POST" ) {
connection.setDoOutput(true);
outstream = connection.getOutputStream();
buffer = new java.lang.String(data).getBytes('UTF-8');
outstream.write(buffer, 0, buffer.length);
outstream.close();
}
}
connection.connect();
}else{
connection.connect();
}
}
if(connection){
try{
length = connection.getHeaderFields().size();
// Stick the response headers into responseHeaders
for (i = 0; i < length; i++) {
name = connection.getHeaderFieldKey(i);
value = connection.getHeaderField(i);
if (name)
xhr.responseHeaders[name+''] = value+'';
}
}catch(e){
console.log('failed to load response headers \n%s',e);
}
xhr.readyState = 4;
xhr.status = parseInt(connection.responseCode,10) || undefined;
xhr.statusText = connection.responseMessage || "";
contentEncoding = connection.getContentEncoding() || "utf-8";
instream = null;
responseXML = null;
try{
//console.log('contentEncoding %s', contentEncoding);
if( contentEncoding.equalsIgnoreCase("gzip") ||
contentEncoding.equalsIgnoreCase("decompress")){
//zipped content
binary = true;
outstream = new java.io.ByteArrayOutputStream();
buffer = java.lang.reflect.Array.newInstance(java.lang.Byte.TYPE, 1024);
instream = new java.util.zip.GZIPInputStream(connection.getInputStream())
}else{
//this is a text file
outstream = new java.io.StringWriter();
buffer = java.lang.reflect.Array.newInstance(java.lang.Character.TYPE, 1024);
instream = new java.io.InputStreamReader(connection.getInputStream());
}
}catch(e){
if (connection.getResponseCode() == 404){
console.log('failed to open connection stream \n %s %s',
e.toString(), e);
}else{
console.log('failed to open connection stream \n %s %s',
e.toString(), e);
}
instream = connection.getErrorStream();
}
while ((length = instream.read(buffer, 0, 1024)) != -1) {
outstream.write(buffer, 0, length);
}
outstream.close();
instream.close();
if(binary){
xhr.responseText = new java.lang.String(outstream.toByteArray(), 'UTF-8')+'';
}else{
xhr.responseText = outstream.toString()+'';
}
}
if(responseHandler){
//console.log('calling ajax response handler');
if(!xhr.async){
responseHandler();
}else{
//console.log('synchronizing ajax response handler via setTimeout');
setTimeout(responseHandler, 1);
}
}
};
}());
(function(){
var log = Envjs.logger('Envjs.Window.Rhino');
//Since we're running in rhino I guess we can safely assume
//java is 'enabled'. I'm sure this requires more thought
//than I've given it here
Envjs.javaEnabled = true;
Envjs.homedir = java.lang.System.getProperty("user.home");
Envjs.tmpdir = java.lang.System.getProperty("java.io.tmpdir");
Envjs.os_name = java.lang.System.getProperty("os.name");
Envjs.os_arch = java.lang.System.getProperty("os.arch");
Envjs.os_version = java.lang.System.getProperty("os.version");
Envjs.lang = java.lang.System.getProperty("user.lang");
Envjs.gc = function(){ gc(); };
/**
* Makes an object window-like by proxying object accessors
* @param {Object} scope
* @param {Object} parent
*/
Envjs.proxy = function(scope, parent) {
try{
if(scope+'' == '[object global]'){
return scope;
}else{
return __context__.initStandardObjects();
}
}catch(e){
console.log('failed to init standard objects %s %s \n%s', scope, parent, e);
}
};
}());
/**
* @author john resig & the envjs team
* @uri http://www.envjs.com/
* @copyright 2008-2010
* @license MIT
*/
//CLOSURE_END
}());