Class Global
- All Implemented Interfaces:
Serializable
,ConstProperties
,DebuggableObject
,IdFunctionCall
,Scriptable
,SymbolScriptable
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from class org.mozilla.javascript.TopLevel
TopLevel.Builtins
Nested classes/interfaces inherited from class org.mozilla.javascript.ScriptableObject
ScriptableObject.KeyComparator
-
Field Summary
FieldsModifier and TypeFieldDescription(package private) boolean
private ShellConsole
private PrintStream
(package private) NativeArray
(package private) boolean
private InputStream
private PrintStream
private String[]
private QuitAction
private boolean
(package private) static final long
Fields inherited from class org.mozilla.javascript.ScriptableObject
CONST, DONTENUM, EMPTY, PERMANENT, READONLY, UNINITIALIZED_CONST
Fields inherited from interface org.mozilla.javascript.Scriptable
NOT_FOUND
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic void
defineClass
(Context cx, Scriptable thisObj, Object[] args, Function funObj) Load a Java class that defines a JavaScript object using the conventions outlined in ScriptableObject.defineClass.static Object
deserialize
(Context cx, Scriptable thisObj, Object[] args, Function funObj) static Object
doctest
(Context cx, Scriptable thisObj, 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 boolean
doctestOutputMatches
(String expected, String actual) Compare actual result of doctest to expected, modulo some acceptable differences.private static Object
static void
gc
(Context cx, Scriptable thisObj, Object[] args, Function funObj) private static String
getCharCodingFromType
(String type) private static Class
<?> getConsole
(Charset cs) getErr()
getIn()
private static Global
getInstance
(Function function) getOut()
String[]
getPrompts
(Context cx) static void
help
(Context cx, Scriptable thisObj, Object[] args, Function funObj) Print a help message.void
void
init
(ContextFactory factory) void
initQuitAction
(QuitAction quitAction) Set the action to call from quit().installRequire
(Context cx, List<String> modulePath, boolean sandboxed) boolean
static void
load
(Context cx, Scriptable thisObj, Object[] args, Function funObj) Load and execute a set of JavaScript source files.static void
loadClass
(Context cx, Scriptable thisObj, Object[] args, Function funObj) Load and execute a script compiled to a class file.private boolean
(package private) static void
pipe
(boolean fromProcess, InputStream from, OutputStream to) static Object
print
(Context cx, Scriptable thisObj, Object[] args, Function funObj) Print the string values of its arguments.static void
quit
(Context cx, Scriptable thisObj, Object[] args, Function funObj) Call embedding-specific quit action passing its argument as int32 exit code.static Object
readFile
(Context cx, Scriptable thisObj, 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 Object
readline
(Context cx, Scriptable thisObj, Object[] args, Function funObj) The readline reads one line from the standard input.private static String
readReader
(Reader reader) private static String
readReader
(Reader reader, int initialBufferSize) private static String
static Object
readUrl
(Context cx, Scriptable thisObj, 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 RuntimeException
reportRuntimeError
(String msgId) (package private) static RuntimeException
reportRuntimeError
(String msgId, String msgArg) static Object
runCommand
(Context cx, Scriptable thisObj, 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.int
runDoctest
(Context cx, Scriptable scope, String session, String sourceName, int lineNumber) private static int
runProcess
(String[] cmd, String[] environment, File wd, InputStream in, OutputStream out, OutputStream err) Runs the given process using Runtime.exec().static void
seal
(Context cx, Scriptable thisObj, Object[] args, Function funObj) The seal function seals all supplied arguments.static void
serialize
(Context cx, Scriptable thisObj, Object[] args, Function funObj) void
setErr
(PrintStream err) void
setIn
(InputStream in) void
setOut
(PrintStream out) void
setSealedStdLib
(boolean value) static Object
spawn
(Context cx, Scriptable thisObj, Object[] args, Function funObj) The spawn function runs a given function or script in a different thread.static Object
sync
(Context cx, Scriptable thisObj, 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 InputStream
toInputStream
(Object value) static Object
toint32
(Context cx, Scriptable thisObj, Object[] args, Function funObj) Convert the argument to int32 number.private static OutputStream
toOutputStream
(Object value) static double
version
(Context cx, Scriptable thisObj, Object[] args, Function funObj) Get and set the language version.static Object
write
(Context cx, Scriptable thisObj, Object[] args, Function funObj) Print just as in "print," but without the trailing newline.Methods inherited from class org.mozilla.javascript.ImporterTopLevel
execIdCall, findPrototypeId, get, getClassName, has, importPackage, init, initPrototypeId, initStandardObjects
Methods inherited from class org.mozilla.javascript.TopLevel
cacheBuiltins, getBuiltinCtor, getBuiltinCtor, getBuiltinPrototype, getBuiltinPrototype
Methods inherited from class org.mozilla.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.mozilla.javascript.ScriptableObject
applyDescriptorToAttributeBitset, associateValue, avoidObjectDetection, buildDataDescriptor, callMethod, callMethod, checkPropertyChange, checkPropertyDefinition, defineClass, defineClass, defineClass, defineConst, defineConstProperty, defineFunctionProperties, defineOwnProperties, defineOwnProperty, defineProperty, defineProperty, defineProperty, defineProperty, defineProperty, defineProperty, delete, 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 Details
-
serialVersionUID
static final long serialVersionUID- See Also:
-
history
NativeArray history -
attemptedJLineLoad
boolean attemptedJLineLoad -
console
-
inStream
-
outStream
-
errStream
-
sealedStdLib
private boolean sealedStdLib -
initialized
boolean initialized -
quitAction
-
prompts
-
doctestCanonicalizations
-
-
Constructor Details
-
Global
public Global() -
Global
-
-
Method Details
-
isInitialized
public boolean isInitialized() -
initQuitAction
Set the action to call from quit(). -
init
-
init
-
installRequire
-
help
Print a help message.This method is defined as a JavaScript function.
-
gc
-
print
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
Print just as in "print," but without the trailing newline. -
doPrint
-
quit
Call embedding-specific quit action passing its argument as int32 exit code.This method is defined as a JavaScript function.
-
version
Get and set the language version.This method is defined as a JavaScript function.
-
load
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 thisObj, Object[] args, Function funObj) throws IllegalAccessException, InstantiationException, 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:
IllegalAccessException
- if access is not available to a reflected class memberInstantiationException
- if unable to instantiate the named classInvocationTargetException
- if an exception is thrown during execution of methods of the named class- See Also:
-
loadClass
public static void loadClass(Context cx, Scriptable thisObj, Object[] args, Function funObj) throws IllegalAccessException, InstantiationException 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:
IllegalAccessException
- if access is not available to the classInstantiationException
- if unable to instantiate the named class
-
getClass
-
serialize
public static void serialize(Context cx, Scriptable thisObj, Object[] args, Function funObj) throws IOException - Throws:
IOException
-
deserialize
public static Object deserialize(Context cx, Scriptable thisObj, Object[] args, Function funObj) throws IOException, ClassNotFoundException - Throws:
IOException
ClassNotFoundException
-
getPrompts
-
doctest
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, String session, String sourceName, int lineNumber) -
doctestOutputMatches
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
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
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 thethis
object 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 Object runCommand(Context cx, Scriptable thisObj, Object[] args, Function funObj) throws 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:
IOException
-
seal
The seal function seals all supplied arguments. -
readFile
public static Object readFile(Context cx, Scriptable thisObj, Object[] args, Function funObj) throws 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:
IOException
-
readUrl
public static Object readUrl(Context cx, Scriptable thisObj, Object[] args, Function funObj) throws 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:
IOException
-
toint32
Convert the argument to int32 number. -
loadJLine
-
getConsole
-
getIn
-
setIn
-
getOut
-
setOut
-
getErr
-
setErr
-
setSealedStdLib
public void setSealedStdLib(boolean value) -
getInstance
-
runProcess
private static int runProcess(String[] cmd, String[] environment, File wd, InputStream in, OutputStream out, OutputStream err) throws IOException Runs 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:
IOException
- If there was an error executing the process.
-
pipe
- Throws:
IOException
-
toInputStream
- Throws:
IOException
-
toOutputStream
-
readUrl
private static String readUrl(String filePath, String charCoding, boolean urlIsFile) throws IOException - Throws:
IOException
-
readline
public static Object readline(Context cx, Scriptable thisObj, Object[] args, Function funObj) throws IOException The readline reads one line from the standard input. "Prompt" is optional.Usage:
readline(prompt)
- Throws:
IOException
-
getCharCodingFromType
-
readReader
- Throws:
IOException
-
readReader
- Throws:
IOException
-
reportRuntimeError
-
reportRuntimeError
-