Class Global
- java.lang.Object
-
- org.htmlunit.corejs.javascript.ScriptableObject
-
- org.htmlunit.corejs.javascript.IdScriptableObject
-
- org.htmlunit.corejs.javascript.TopLevel
-
- org.htmlunit.corejs.javascript.ImporterTopLevel
-
- org.htmlunit.corejs.javascript.tools.shell.Global
-
- All Implemented Interfaces:
java.io.Serializable,ConstProperties,DebuggableObject,IdFunctionCall,Scriptable,SymbolScriptable
public class Global extends ImporterTopLevel
This class provides for sharing functions across multiple threads. This is of particular interest to server applications.- See Also:
- Serialized Form
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class org.htmlunit.corejs.javascript.TopLevel
TopLevel.Builtins
-
Nested classes/interfaces inherited from class org.htmlunit.corejs.javascript.ScriptableObject
ScriptableObject.KeyComparator
-
-
Field Summary
Fields Modifier and Type Field Description (package private) booleanattemptedJLineLoadprivate ShellConsoleconsoleprivate java.util.HashMap<java.lang.String,java.lang.String>doctestCanonicalizationsprivate java.io.PrintStreamerrStream(package private) NativeArrayhistory(package private) booleaninitializedprivate java.io.InputStreaminStreamprivate java.io.PrintStreamoutStreamprivate java.lang.String[]promptsprivate QuitActionquitActionprivate booleansealedStdLib(package private) static longserialVersionUID-
Fields inherited from class org.htmlunit.corejs.javascript.ScriptableObject
CONST, DONTENUM, EMPTY, PERMANENT, READONLY, UNINITIALIZED_CONST
-
Fields inherited from interface org.htmlunit.corejs.javascript.Scriptable
NOT_FOUND
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static voiddefineClass(Context cx, Scriptable scope, Scriptable thisObj, java.lang.Object[] args, Function funObj)Load a Java class that defines a JavaScript object using the conventions outlined in ScriptableObject.defineClass.static java.lang.Objectdeserialize(Context cx, Scriptable scope, Scriptable thisObj, java.lang.Object[] args, Function funObj)static java.lang.Objectdoctest(Context cx, Scriptable scope, Scriptable thisObj, java.lang.Object[] args, Function funObj)Example: doctest("js> function f() {\n > return 3;\n > }\njs> f();\n3\n"); returns 2 (since 2 tests were executed).private booleandoctestOutputMatches(java.lang.String expected, java.lang.String actual)Compare actual result of doctest to expected, modulo some acceptable differences.private static java.lang.ObjectdoPrint(java.lang.Object[] args, Function funObj, boolean newline)static voidgc(Context cx, Scriptable scope, Scriptable thisObj, java.lang.Object[] args, Function funObj)private static java.lang.StringgetCharCodingFromType(java.lang.String type)private static java.lang.Class<?>getClass(java.lang.Object[] args)ShellConsolegetConsole(java.nio.charset.Charset cs)java.io.PrintStreamgetErr()java.io.InputStreamgetIn()private static GlobalgetInstance(Function function)java.io.PrintStreamgetOut()java.lang.String[]getPrompts(Context cx)static voidhelp(Context cx, Scriptable scope, Scriptable thisObj, java.lang.Object[] args, Function funObj)Print a help message.voidinit(Context cx)voidinit(ContextFactory factory)voidinitQuitAction(QuitAction quitAction)Set the action to call from quit().RequireinstallRequire(Context cx, java.util.List<java.lang.String> modulePath, boolean sandboxed)booleanisInitialized()static voidload(Context cx, Scriptable scope, Scriptable thisObj, java.lang.Object[] args, Function funObj)Load and execute a set of JavaScript source files.static voidloadClass(Context cx, Scriptable scope, Scriptable thisObj, java.lang.Object[] args, Function funObj)Load and execute a script compiled to a class file.private booleanloadJLine(java.nio.charset.Charset cs)(package private) static voidpipe(boolean fromProcess, java.io.InputStream from, java.io.OutputStream to)static java.lang.Objectprint(Context cx, Scriptable scope, Scriptable thisObj, java.lang.Object[] args, Function funObj)Print the string values of its arguments.static voidquit(Context cx, Scriptable scope, Scriptable thisObj, java.lang.Object[] args, Function funObj)Call embedding-specific quit action passing its argument as int32 exit code.static java.lang.ObjectreadFile(Context cx, Scriptable scope, Scriptable thisObj, java.lang.Object[] args, Function funObj)The readFile reads the given file content and convert it to a string using the specified character coding or default character coding if explicit coding argument is not given.static java.lang.Objectreadline(Context cx, Scriptable scope, Scriptable thisObj, java.lang.Object[] args, Function funObj)The readline reads one line from the standard input.private static java.lang.StringreadReader(java.io.Reader reader)private static java.lang.StringreadReader(java.io.Reader reader, int initialBufferSize)private static java.lang.StringreadUrl(java.lang.String filePath, java.lang.String charCoding, boolean urlIsFile)static java.lang.ObjectreadUrl(Context cx, Scriptable scope, Scriptable thisObj, java.lang.Object[] args, Function funObj)The readUrl opens connection to the given URL, read all its data and converts them to a string using the specified character coding or default character coding if explicit coding argument is not given.(package private) static java.lang.RuntimeExceptionreportRuntimeError(java.lang.String msgId)(package private) static java.lang.RuntimeExceptionreportRuntimeError(java.lang.String msgId, java.lang.String msgArg)static java.lang.ObjectrunCommand(Context cx, Scriptable scope, Scriptable thisObj, java.lang.Object[] args, Function funObj)Execute the specified command with the given argument and options as a separate process and return the exit status of the process.intrunDoctest(Context cx, Scriptable scope, java.lang.String session, java.lang.String sourceName, int lineNumber)private static intrunProcess(java.lang.String[] cmd, java.lang.String[] environment, java.io.File wd, java.io.InputStream in, java.io.OutputStream out, java.io.OutputStream err)Runs the given process using Runtime.exec().static voidseal(Context cx, Scriptable scope, Scriptable thisObj, java.lang.Object[] args, Function funObj)The seal function seals all supplied arguments.static voidserialize(Context cx, Scriptable scope, Scriptable thisObj, java.lang.Object[] args, Function funObj)voidsetErr(java.io.PrintStream err)voidsetIn(java.io.InputStream in)voidsetOut(java.io.PrintStream out)voidsetSealedStdLib(boolean value)static java.lang.Objectspawn(Context cx, Scriptable scope, Scriptable thisObj, java.lang.Object[] args, Function funObj)The spawn function runs a given function or script in a different thread.static java.lang.Objectsync(Context cx, Scriptable scope, Scriptable thisObj, java.lang.Object[] args, Function funObj)The sync function creates a synchronized function (in the sense of a Java synchronized method) from an existing function.private static java.io.InputStreamtoInputStream(java.lang.Object value)static java.lang.Objecttoint32(Context cx, Scriptable scope, Scriptable thisObj, java.lang.Object[] args, Function funObj)Convert the argument to int32 number.private static java.io.OutputStreamtoOutputStream(java.lang.Object value)static doubleversion(Context cx, Scriptable scope, Scriptable thisObj, java.lang.Object[] args, Function funObj)Get and set the language version.static java.lang.Objectwrite(Context cx, Scriptable scope, Scriptable thisObj, java.lang.Object[] args, Function funObj)Print just as in "print," but without the trailing newline.-
Methods inherited from class org.htmlunit.corejs.javascript.ImporterTopLevel
execIdCall, findPrototypeId, get, getClassName, has, importPackage, init, initPrototypeId, initStandardObjects
-
Methods inherited from class org.htmlunit.corejs.javascript.TopLevel
cacheBuiltins, getBuiltinCtor, getBuiltinCtor, getBuiltinPrototype, getBuiltinPrototype
-
Methods inherited from class org.htmlunit.corejs.javascript.IdScriptableObject
activatePrototypeMap, addIdFunctionProperty, defaultGet, defaultHas, defaultPut, defineOwnProperty, delete, delete, ensureType, exportAsJSClass, fillConstructorProperties, findInstanceIdInfo, findInstanceIdInfo, findPrototypeId, get, getAttributes, getAttributes, getInstanceIdName, getInstanceIdValue, getMaxInstanceId, getOwnPropertyDescriptor, has, hasPrototypeMap, initPrototypeConstructor, initPrototypeMethod, initPrototypeMethod, initPrototypeMethod, initPrototypeValue, initPrototypeValue, instanceIdInfo, put, put, setAttributes, setInstanceIdAttributes, setInstanceIdValue
-
Methods inherited from class org.htmlunit.corejs.javascript.ScriptableObject
applyDescriptorToAttributeBitset, associateValue, avoidObjectDetection, buildDataDescriptor, callMethod, callMethod, checkPropertyChange, checkPropertyDefinition, defineClass, defineClass, defineClass, defineConst, defineConstProperty, defineFunctionProperties, defineOwnProperties, defineOwnProperty, defineProperty, defineProperty, defineProperty, defineProperty, defineProperty, defineProperty, defineProperty, delete, deleteProperty, deleteProperty, deleteProperty, ensureScriptable, ensureScriptableObject, ensureSymbolScriptable, equivalentValues, get, get, getAllIds, getArrayPrototype, getAssociatedValue, getAttributes, getAttributes, getAttributes, getClassPrototype, getDefaultValue, getDefaultValue, getExternalArrayData, getExternalArrayLength, getFunctionPrototype, getGeneratorFunctionPrototype, getGetterOrSetter, getGetterOrSetter, getIds, getObjectPrototype, getParentScope, getProperty, getProperty, getProperty, getPropertyIds, getPrototype, getTopLevelScope, getTopScopeValue, getTypedProperty, getTypedProperty, getTypeOf, has, hasInstance, hasProperty, hasProperty, hasProperty, isAccessorDescriptor, isConst, isDataDescriptor, isEmpty, isExtensible, isFalse, isGenericDescriptor, isGetterOrSetter, isSealed, isTrue, preventExtensions, put, putConst, putConstProperty, putProperty, putProperty, putProperty, querySlot, redefineProperty, sameValue, sealObject, setAttributes, setAttributes, setAttributes, setAttributes, setCommonDescriptorProperties, setExternalArrayData, setGetterOrSetter, setParentScope, setPrototype, size
-
-
-
-
Field Detail
-
serialVersionUID
static final long serialVersionUID
- See Also:
- Constant Field Values
-
history
NativeArray history
-
attemptedJLineLoad
boolean attemptedJLineLoad
-
console
private ShellConsole console
-
inStream
private java.io.InputStream inStream
-
outStream
private java.io.PrintStream outStream
-
errStream
private java.io.PrintStream errStream
-
sealedStdLib
private boolean sealedStdLib
-
initialized
boolean initialized
-
quitAction
private QuitAction quitAction
-
prompts
private java.lang.String[] prompts
-
doctestCanonicalizations
private java.util.HashMap<java.lang.String,java.lang.String> doctestCanonicalizations
-
-
Constructor Detail
-
Global
public Global()
-
Global
public Global(Context cx)
-
-
Method Detail
-
isInitialized
public boolean isInitialized()
-
initQuitAction
public void initQuitAction(QuitAction quitAction)
Set the action to call from quit().
-
init
public void init(ContextFactory factory)
-
init
public void init(Context cx)
-
installRequire
public Require installRequire(Context cx, java.util.List<java.lang.String> modulePath, boolean sandboxed)
-
help
public static void help(Context cx, Scriptable scope, Scriptable thisObj, java.lang.Object[] args, Function funObj)
Print a help message.This method is defined as a JavaScript function.
-
gc
public static void gc(Context cx, Scriptable scope, Scriptable thisObj, java.lang.Object[] args, Function funObj)
-
print
public static java.lang.Object print(Context cx, Scriptable scope, Scriptable thisObj, java.lang.Object[] args, Function funObj)
Print the string values of its arguments.This method is defined as a JavaScript function. Note that its arguments are of the "varargs" form, which allows it to handle an arbitrary number of arguments supplied to the JavaScript function.
-
write
public static java.lang.Object write(Context cx, Scriptable scope, Scriptable thisObj, java.lang.Object[] args, Function funObj)
Print just as in "print," but without the trailing newline.
-
doPrint
private static java.lang.Object doPrint(java.lang.Object[] args, Function funObj, boolean newline)
-
quit
public static void quit(Context cx, Scriptable scope, Scriptable thisObj, java.lang.Object[] args, Function funObj)
Call embedding-specific quit action passing its argument as int32 exit code.This method is defined as a JavaScript function.
-
version
public static double version(Context cx, Scriptable scope, Scriptable thisObj, java.lang.Object[] args, Function funObj)
Get and set the language version.This method is defined as a JavaScript function.
-
load
public static void load(Context cx, Scriptable scope, Scriptable thisObj, java.lang.Object[] args, Function funObj)
Load and execute a set of JavaScript source files.This method is defined as a JavaScript function.
-
defineClass
public static void defineClass(Context cx, Scriptable scope, Scriptable thisObj, java.lang.Object[] args, Function funObj) throws java.lang.IllegalAccessException, java.lang.InstantiationException, java.lang.reflect.InvocationTargetException
Load a Java class that defines a JavaScript object using the conventions outlined in ScriptableObject.defineClass.This method is defined as a JavaScript function.
- Throws:
java.lang.IllegalAccessException- if access is not available to a reflected class memberjava.lang.InstantiationException- if unable to instantiate the named classjava.lang.reflect.InvocationTargetException- if an exception is thrown during execution of methods of the named class- See Also:
ScriptableObject.defineClass(Scriptable,Class)
-
loadClass
public static void loadClass(Context cx, Scriptable scope, Scriptable thisObj, java.lang.Object[] args, Function funObj) throws java.lang.IllegalAccessException, java.lang.InstantiationException, java.lang.NoSuchMethodException, java.lang.reflect.InvocationTargetException
Load and execute a script compiled to a class file.This method is defined as a JavaScript function. When called as a JavaScript function, a single argument is expected. This argument should be the name of a class that implements the Script interface, as will any script compiled by jsc.
- Throws:
java.lang.IllegalAccessException- if access is not available to the classjava.lang.InstantiationException- if unable to instantiate the named classjava.lang.NoSuchMethodExceptionjava.lang.reflect.InvocationTargetException
-
getClass
private static java.lang.Class<?> getClass(java.lang.Object[] args)
-
serialize
public static void serialize(Context cx, Scriptable scope, Scriptable thisObj, java.lang.Object[] args, Function funObj) throws java.io.IOException
- Throws:
java.io.IOException
-
deserialize
public static java.lang.Object deserialize(Context cx, Scriptable scope, Scriptable thisObj, java.lang.Object[] args, Function funObj) throws java.io.IOException, java.lang.ClassNotFoundException
- Throws:
java.io.IOExceptionjava.lang.ClassNotFoundException
-
getPrompts
public java.lang.String[] getPrompts(Context cx)
-
doctest
public static java.lang.Object doctest(Context cx, Scriptable scope, Scriptable thisObj, java.lang.Object[] args, Function funObj)
Example: doctest("js> function f() {\n > return 3;\n > }\njs> f();\n3\n"); returns 2 (since 2 tests were executed).
-
runDoctest
public int runDoctest(Context cx, Scriptable scope, java.lang.String session, java.lang.String sourceName, int lineNumber)
-
doctestOutputMatches
private boolean doctestOutputMatches(java.lang.String expected, java.lang.String actual)Compare actual result of doctest to expected, modulo some acceptable differences. Currently just trims the strings before comparing, but should ignore differences in line numbers for error messages for example.- Parameters:
expected- the expected stringactual- the actual string- Returns:
- true iff actual matches expected modulo some acceptable differences
-
spawn
public static java.lang.Object spawn(Context cx, Scriptable scope, Scriptable thisObj, java.lang.Object[] args, Function funObj)
The spawn function runs a given function or script in a different thread.js> function g() { a = 7; } js> a = 3; 3 js> spawn(g) Thread[Thread-1,5,main] js> a 3
-
sync
public static java.lang.Object sync(Context cx, Scriptable scope, Scriptable thisObj, java.lang.Object[] args, Function funObj)
The sync function creates a synchronized function (in the sense of a Java synchronized method) from an existing function. The new function synchronizes on the the second argument if it is defined, or otherwise thethisobject of its invocation. js> var o = { f : sync(function(x) { print("entry"); Packages.java.lang.Thread.sleep(x*1000); print("exit"); })}; js> spawn(function() {o.f(5);}); Thread[Thread-0,5,main] entry js> spawn(function() {o.f(5);}); Thread[Thread-1,5,main] js> exit entry exit
-
runCommand
public static java.lang.Object runCommand(Context cx, Scriptable scope, Scriptable thisObj, java.lang.Object[] args, Function funObj) throws java.io.IOException
Execute the specified command with the given argument and options as a separate process and return the exit status of the process.Usage:
runCommand(command) runCommand(command, arg1, ..., argN) runCommand(command, arg1, ..., argN, options)
All except the last arguments to runCommand are converted to strings and denote command name and its arguments. If the last argument is a JavaScript object, it is an option object. Otherwise it is converted to string denoting the last argument and options objects assumed to be empty. The following properties of the option object are processed:args- provides an array of additional command argumentsenv- explicit environment object. All its enumerable properties define the corresponding environment variable names.input- the process input. If it is not java.io.InputStream, it is converted to string and sent to the process as its input. If not specified, no input is provided to the process.output- the process output instead of java.lang.System.out. If it is not instance of java.io.OutputStream, the process output is read, converted to a string, appended to the output property value converted to string and put as the new value of the output property.err- the process error output instead of java.lang.System.err. If it is not instance of java.io.OutputStream, the process error output is read, converted to a string, appended to the err property value converted to string and put as the new value of the err property.dir- the working direcotry to run the commands.
- Throws:
java.io.IOException
-
seal
public static void seal(Context cx, Scriptable scope, Scriptable thisObj, java.lang.Object[] args, Function funObj)
The seal function seals all supplied arguments.
-
readFile
public static java.lang.Object readFile(Context cx, Scriptable scope, Scriptable thisObj, java.lang.Object[] args, Function funObj) throws java.io.IOException
The readFile reads the given file content and convert it to a string using the specified character coding or default character coding if explicit coding argument is not given.Usage:
readFile(filePath) readFile(filePath, charCoding)
The first form converts file's context to string using the default character coding.- Throws:
java.io.IOException
-
readUrl
public static java.lang.Object readUrl(Context cx, Scriptable scope, Scriptable thisObj, java.lang.Object[] args, Function funObj) throws java.io.IOException
The readUrl opens connection to the given URL, read all its data and converts them to a string using the specified character coding or default character coding if explicit coding argument is not given.Usage:
readUrl(url) readUrl(url, charCoding)
The first form converts file's context to string using the default charCoding.- Throws:
java.io.IOException
-
toint32
public static java.lang.Object toint32(Context cx, Scriptable scope, Scriptable thisObj, java.lang.Object[] args, Function funObj)
Convert the argument to int32 number.
-
loadJLine
private boolean loadJLine(java.nio.charset.Charset cs)
-
getConsole
public ShellConsole getConsole(java.nio.charset.Charset cs)
-
getIn
public java.io.InputStream getIn()
-
setIn
public void setIn(java.io.InputStream in)
-
getOut
public java.io.PrintStream getOut()
-
setOut
public void setOut(java.io.PrintStream out)
-
getErr
public java.io.PrintStream getErr()
-
setErr
public void setErr(java.io.PrintStream err)
-
setSealedStdLib
public void setSealedStdLib(boolean value)
-
runProcess
private static int runProcess(java.lang.String[] cmd, java.lang.String[] environment, java.io.File wd, java.io.InputStream in, java.io.OutputStream out, java.io.OutputStream err) throws java.io.IOExceptionRuns the given process using Runtime.exec(). If any of in, out, err is null, the corresponding process stream will be closed immediately, otherwise it will be closed as soon as all data will be read from/written to process- Returns:
- Exit value of process.
- Throws:
java.io.IOException- If there was an error executing the process.
-
pipe
static void pipe(boolean fromProcess, java.io.InputStream from, java.io.OutputStream to) throws java.io.IOException- Throws:
java.io.IOException
-
toInputStream
private static java.io.InputStream toInputStream(java.lang.Object value) throws java.io.IOException- Throws:
java.io.IOException
-
toOutputStream
private static java.io.OutputStream toOutputStream(java.lang.Object value)
-
readUrl
private static java.lang.String readUrl(java.lang.String filePath, java.lang.String charCoding, boolean urlIsFile) throws java.io.IOException- Throws:
java.io.IOException
-
readline
public static java.lang.Object readline(Context cx, Scriptable scope, Scriptable thisObj, java.lang.Object[] args, Function funObj) throws java.io.IOException
The readline reads one line from the standard input. "Prompt" is optional.Usage:
readline(prompt)
- Throws:
java.io.IOException
-
getCharCodingFromType
private static java.lang.String getCharCodingFromType(java.lang.String type)
-
readReader
private static java.lang.String readReader(java.io.Reader reader) throws java.io.IOException- Throws:
java.io.IOException
-
readReader
private static java.lang.String readReader(java.io.Reader reader, int initialBufferSize) throws java.io.IOException- Throws:
java.io.IOException
-
reportRuntimeError
static java.lang.RuntimeException reportRuntimeError(java.lang.String msgId)
-
reportRuntimeError
static java.lang.RuntimeException reportRuntimeError(java.lang.String msgId, java.lang.String msgArg)
-
-