提交 4a9e7c12 编写于 作者: S sundar

8010136: Make jrunscript's init.js to work on nashorn

Reviewed-by: lagergren, hannesw
上级 f257643d
/* /*
* Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -187,11 +187,31 @@ function jlist(list) { ...@@ -187,11 +187,31 @@ function jlist(list) {
} }
/** /**
* This is java.lang.System properties wrapped by jmap. * This is java.lang.System properties wrapped by JSAdapter.
* For eg. to access java.class.path property, you can use * For eg. to access java.class.path property, you can use
* the syntax sysProps["java.class.path"] * the syntax sysProps["java.class.path"]
*/ */
var sysProps = jmap(java.lang.System.getProperties()); var sysProps = new JSAdapter({
__get__ : function (name) {
return java.lang.System.getProperty(name);
},
__has__ : function (name) {
return java.lang.System.getProperty(name) != null;
},
__getIds__ : function() {
return java.lang.System.getProperties().keySet().toArray();
},
__delete__ : function(name) {
java.lang.System.clearProperty(name);
return true;
},
__put__ : function (name, value) {
java.lang.System.setProperty(name, value);
},
toString: function() {
return "<system properties>";
}
});
// stdout, stderr & stdin // stdout, stderr & stdin
var out = java.lang.System.out; var out = java.lang.System.out;
...@@ -199,9 +219,18 @@ var err = java.lang.System.err; ...@@ -199,9 +219,18 @@ var err = java.lang.System.err;
// can't use 'in' because it is a JavaScript keyword :-( // can't use 'in' because it is a JavaScript keyword :-(
var inp = java.lang.System["in"]; var inp = java.lang.System["in"];
// useful imports for often used io, net classes var BufferedInputStream = java.io.BufferedInputStream;
importPackage(java.io); var BufferedOutputStream = java.io.BufferedOutputStream;
importPackage(java.net); var BufferedReader = java.io.BufferedReader;
var DataInputStream = java.io.DataInputStream;
var File = java.io.File;
var FileInputStream = java.io.FileInputStream;
var FileOutputStream = java.io.FileOutputStream;
var InputStream = java.io.InputStream;
var InputStreamReader = java.io.InputStreamReader;
var OutputStream = java.io.OutputStream;
var Reader = java.io.Reader;
var URL = java.net.URL;
/** /**
* Generic any object to input stream mapper * Generic any object to input stream mapper
...@@ -302,7 +331,8 @@ function streamClose(stream) { ...@@ -302,7 +331,8 @@ function streamClose(stream) {
* *
* @param str input from which script is loaded and evaluated * @param str input from which script is loaded and evaluated
*/ */
function load(str) { if (typeof(load) == 'undefined') {
var load = function(str) {
var stream = inStream(str); var stream = inStream(str);
var bstream = new BufferedInputStream(stream); var bstream = new BufferedInputStream(stream);
var reader = new BufferedReader(new InputStreamReader(bstream)); var reader = new BufferedReader(new InputStreamReader(bstream));
...@@ -314,6 +344,7 @@ function load(str) { ...@@ -314,6 +344,7 @@ function load(str) {
engine.put(engine.FILENAME, oldFilename); engine.put(engine.FILENAME, oldFilename);
streamClose(stream); streamClose(stream);
} }
}
} }
// file system utilities // file system utilities
...@@ -458,7 +489,7 @@ function dirname(pathname) { ...@@ -458,7 +489,7 @@ function dirname(pathname) {
* @param dir name of the new directory * @param dir name of the new directory
*/ */
function mkdir(dir) { function mkdir(dir) {
var dir = pathToFile(dir); dir = pathToFile(dir);
println(dir.mkdir()? "created" : "can not create dir"); println(dir.mkdir()? "created" : "can not create dir");
} }
...@@ -469,7 +500,7 @@ function mkdir(dir) { ...@@ -469,7 +500,7 @@ function mkdir(dir) {
* @param dir input path name * @param dir input path name
*/ */
function mkdirs(dir) { function mkdirs(dir) {
var dir = pathToFile(dir); dir = pathToFile(dir);
println(dir.mkdirs()? "created" : "can not create dirs"); println(dir.mkdirs()? "created" : "can not create dirs");
} }
...@@ -479,7 +510,7 @@ function mkdirs(dir) { ...@@ -479,7 +510,7 @@ function mkdirs(dir) {
* @param pathname name of the file * @param pathname name of the file
*/ */
function rm(pathname) { function rm(pathname) {
file = pathToFile(pathname); var file = pathToFile(pathname);
if (!file.exists()) { if (!file.exists()) {
println("file not found: " + pathname); println("file not found: " + pathname);
return false; return false;
...@@ -674,25 +705,29 @@ function exec(cmd) { ...@@ -674,25 +705,29 @@ function exec(cmd) {
$exit = process.exitValue(); $exit = process.exitValue();
} }
/** if (typeof(exit) == 'undefined') {
/**
* Exit the shell program. * Exit the shell program.
* *
* @param exitCode integer code returned to OS shell. * @param exitCode integer code returned to OS shell.
* optional, defaults to 0 * optional, defaults to 0
*/ */
function exit(code) { var exit = function (code) {
if (code) { if (code) {
java.lang.System.exit(code + 0); java.lang.System.exit(code + 0);
} else { } else {
java.lang.System.exit(0); java.lang.System.exit(0);
} }
}
} }
/** if (typeof(quit) == 'undefined') {
/**
* synonym for exit * synonym for exit
*/ */
function quit(code) { var quit = function (code) {
exit(code); exit(code);
}
} }
// XML utilities // XML utilities
...@@ -776,7 +811,7 @@ function XSLTransform(inp, style, out) { ...@@ -776,7 +811,7 @@ function XSLTransform(inp, style, out) {
} }
var factory = javax.xml.transform.TransformerFactory.newInstance(); var factory = javax.xml.transform.TransformerFactory.newInstance();
var tranformer; var transformer;
if (style) { if (style) {
transformer = factory.newTransformer(XMLSource(style)); transformer = factory.newTransformer(XMLSource(style));
} else { } else {
...@@ -839,19 +874,21 @@ function echo(x) { ...@@ -839,19 +874,21 @@ function echo(x) {
} }
} }
/** if (typeof(printf) == 'undefined') {
/**
* This is C-like printf * This is C-like printf
* *
* @param format string to format the rest of the print items * @param format string to format the rest of the print items
* @param args variadic argument list * @param args variadic argument list
*/ */
function printf(format, args/*, more args*/) { var printf = function (format, args/*, more args*/) {
var array = java.lang.reflect.Array.newInstance(java.lang.Object, var array = java.lang.reflect.Array.newInstance(java.lang.Object,
arguments.length - 1); arguments.length - 1);
for (var i = 0; i < array.length; i++) { for (var i = 0; i < array.length; i++) {
array[i] = arguments[i+1]; array[i] = arguments[i+1];
} }
return java.lang.System.out.printf(format, array); java.lang.System.out.printf(format, array);
}
} }
/** /**
...@@ -882,3 +919,27 @@ function read(prompt, multiline) { ...@@ -882,3 +919,27 @@ function read(prompt, multiline) {
return reader.readLine(); return reader.readLine();
} }
} }
if (typeof(println) == 'undefined') {
var print = function(str, newline) {
if (typeof(str) == 'undefined') {
str = 'undefined';
} else if (str == null) {
str = 'null';
}
if (!(out instanceof java.io.PrintWriter)) {
out = new java.io.PrintWriter(out);
}
out.print(String(str));
if (newline) {
out.print('\n');
}
out.flush();
}
var println = function(str) {
print(str, true);
};
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册