Class JavaScriptEngine
java.lang.Object
org.htmlunit.javascript.JavaScriptEngine
- All Implemented Interfaces:
AbstractJavaScriptEngine<org.htmlunit.corejs.javascript.Script>
public class JavaScriptEngine
extends Object
implements AbstractJavaScriptEngine<org.htmlunit.corejs.javascript.Script>
A wrapper for the Rhino JavaScript engine
that provides browser specific features.
Like all classes in this package, this class is not intended for direct use and may change without notice.
- Author:
- Mike Bowler, Chen Jun, David K. Taylor, Chris Erskine, Ben Curren, David D. Kilzer, Marc Guillemot, Daniel Gredler, Ahmed Ashour, Amit Manjhi, Ronald Brill, Frank Danek, Lai Quang Duong
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final Object[]ScriptRuntime.emptyArgs.static final StringKey used to place theHtmlPagefor which the JavaScript code is executed as thread local attribute in current context.static final StringKey used to place the scope in which the execution of some JavaScript code started as thread local attribute in current context.static final Objectorg.htmlunit.corejs.javascript.Undefined.instance. -
Constructor Summary
ConstructorsConstructorDescriptionJavaScriptEngine(WebClient webClient) Creates an instance for the specifiedWebClient. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddPostponedAction(PostponedAction action) Adds an action that should be executed first when the script currently being executed has finished.static voidapplyPolyfills(WebClient webClient, BrowserVersion browserVersion, org.htmlunit.corejs.javascript.Context context, HtmlUnitScriptable scriptable) INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.static org.htmlunit.corejs.javascript.RhinoExceptionasJavaScriptException(Window window, DOMException exception) INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.
Encapsulates the givenDOMExceptioninto a Rhino-compatible exception.callFunction(HtmlPage page, org.htmlunit.corejs.javascript.Function javaScriptFunction, org.htmlunit.corejs.javascript.Scriptable thisObject, Object[] args, DomNode node) Calls a JavaScript function and return the result.callFunction(HtmlPage page, org.htmlunit.corejs.javascript.Function function, org.htmlunit.corejs.javascript.Scriptable scope, org.htmlunit.corejs.javascript.Scriptable thisObject, Object[] args) Calls the given function taking care of synchronization issues.final <T> TcallSecured(org.htmlunit.corejs.javascript.ContextAction<T> action, HtmlPage page) Forwards this to theHtmlUnitContextFactorybut with checking shutdown handling.org.htmlunit.corejs.javascript.ScriptDeprecated.org.htmlunit.corejs.javascript.Scriptcompile(HtmlPage owningPage, org.htmlunit.corejs.javascript.Scriptable scope, String sourceCode, String sourceName, int startLine) Compiles the specified JavaScript code in the context of a given scope.static HtmlUnitScriptableconfigureClass(ClassConfiguration config, org.htmlunit.corejs.javascript.Scriptable window, BrowserVersion browserVersion) Configures the specified class for access via JavaScript.static voidconfigureRhino(WebClient webClient, BrowserVersion browserVersion, HtmlUnitScriptable scriptable) INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.static org.htmlunit.corejs.javascript.EcmaErrorconstructError(String error, String message) static StringevaluateProxyAutoConfig(BrowserVersion browserVersion, String content, URL url) Evaluates theFindProxyForURLmethod of the specified content.Deprecated.Deprecated.execute(HtmlPage page, org.htmlunit.corejs.javascript.Scriptable scope, String sourceCode, String sourceName, int startLine) Executes the specified JavaScript code in the context of a given page.execute(HtmlPage page, org.htmlunit.corejs.javascript.Scriptable scope, org.htmlunit.corejs.javascript.Script script) Executes the specified JavaScript code in the context of a given page.Class<? extends HtmlUnitScriptable> getJavaScriptClass(Class<?> c) Gets the class of the JavaScript object for the node class.Gets the associated configuration.Returns the JavaScriptExecutor.longReturns the javascript timeout.protected voidhandleJavaScriptException(ScriptException scriptException, boolean triggerOnError) Handles an exception that occurred during execution of JavaScript code.protected voidHandles an exception that occurred during execution of JavaScript code.voidINTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.
Indicates that no postponed action should be executed.voidinitialize(WebWindow webWindow, Page page) Performs initialization for the given webWindow.static booleanbooleanIndicates if JavaScript is running in current thread.static booleanisUndefined(Object obj) static org.htmlunit.corejs.javascript.ScriptablenewArray(org.htmlunit.corejs.javascript.Scriptable scope, int length) Create an array with a specified initial length.static org.htmlunit.corejs.javascript.ScriptableCreate an array with a specified initial length.static org.htmlunit.corejs.javascript.ScriptablenewArrayIteratorTypeEntries(org.htmlunit.corejs.javascript.Scriptable scope, org.htmlunit.corejs.javascript.Scriptable arrayLike) INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.
Create a new ArrayIterator of type NativeArrayIterator.ARRAY_ITERATOR_TYPE.ENTRIESstatic org.htmlunit.corejs.javascript.ScriptablenewArrayIteratorTypeKeys(org.htmlunit.corejs.javascript.Scriptable scope, org.htmlunit.corejs.javascript.Scriptable arrayLike) INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.
Create a new ArrayIterator of type NativeArrayIterator.ARRAY_ITERATOR_TYPE.KEYSstatic org.htmlunit.corejs.javascript.ScriptablenewArrayIteratorTypeValues(org.htmlunit.corejs.javascript.Scriptable scope, org.htmlunit.corejs.javascript.Scriptable arrayLike) INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.
Create a new ArrayIterator of type NativeArrayIterator.ARRAY_ITERATOR_TYPE.VALUESvoidDisable starting of new js threads.voidINTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.
Process postponed actions, if any.static org.htmlunit.corejs.javascript.EcmaErrorrangeError(String message) Report a runtime error using the error reporter for the current thread.voidregisterWindowAndMaybeStartEventLoop(WebWindow webWindow) Register WebWindow with the JavaScriptExecutor.static RuntimeExceptionreportRuntimeError(String message) Report a runtime error using the error reporter for the current thread.voidsetJavaScriptTimeout(long timeout) Sets the javascript timeout.voidshutdown()Shutdown the JavaScriptEngine.static RuntimeExceptionRethrow the exception wrapping it as the script runtime exception.static booleanConvert the value to a JavaScript boolean value.static intstatic doublestatic doublestatic doubleConvert the value to a JavaScript Number value.static StringConvert the value to a JavaScript String value.static org.htmlunit.corejs.javascript.EcmaErrorReport a runtime error using the error reporter for the current thread.static StringuncompressJavaScript(String scriptSource, String scriptName) Tries to uncompress the JavaScript code in the provided response.
-
Field Details
-
emptyArgs
ScriptRuntime.emptyArgs. -
Undefined
org.htmlunit.corejs.javascript.Undefined.instance. -
KEY_STARTING_SCOPE
Key used to place the scope in which the execution of some JavaScript code started as thread local attribute in current context.This is needed to resolve some relative locations relatively to the page in which the script is executed and not to the page which location is changed.
- See Also:
-
KEY_STARTING_PAGE
-
-
Constructor Details
-
JavaScriptEngine
-
-
Method Details
-
getContextFactory
- Specified by:
getContextFactoryin interfaceAbstractJavaScriptEngine<org.htmlunit.corejs.javascript.Script>- Returns:
- this JavaScript engine's
HtmlUnitContextFactory
-
initialize
Performs initialization for the given webWindow.- Specified by:
initializein interfaceAbstractJavaScriptEngine<org.htmlunit.corejs.javascript.Script>- Parameters:
webWindow- the web window to initialize forpage- the page that will become the enclosing page
-
getJavaScriptExecutor
Returns the JavaScriptExecutor.- Returns:
- the JavaScriptExecutor or null if javascript is disabled or no executor was required so far.
-
configureRhino
public static void configureRhino(WebClient webClient, BrowserVersion browserVersion, HtmlUnitScriptable scriptable) INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.- Parameters:
webClient- the WebClientbrowserVersion- the BrowserVersionscriptable- the window or the DedicatedWorkerGlobalScope
-
applyPolyfills
public static void applyPolyfills(WebClient webClient, BrowserVersion browserVersion, org.htmlunit.corejs.javascript.Context context, HtmlUnitScriptable scriptable) throws IOException INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.- Parameters:
webClient- the WebClientbrowserVersion- the BrowserVersioncontext- the current contextscriptable- the window or the DedicatedWorkerGlobalScope- Throws:
IOException- in case of problems
-
configureClass
public static HtmlUnitScriptable configureClass(ClassConfiguration config, org.htmlunit.corejs.javascript.Scriptable window, BrowserVersion browserVersion) throws InstantiationException, IllegalAccessException Configures the specified class for access via JavaScript.- Parameters:
config- the configuration settings for the class to be configuredwindow- the scope within which to configure the classbrowserVersion- the browser version- Returns:
- the created prototype
- Throws:
InstantiationException- if the new class cannot be instantiatedIllegalAccessException- if we don't have access to create the new instance
-
registerWindowAndMaybeStartEventLoop
Register WebWindow with the JavaScriptExecutor.- Specified by:
registerWindowAndMaybeStartEventLoopin interfaceAbstractJavaScriptEngine<org.htmlunit.corejs.javascript.Script>- Parameters:
webWindow- the WebWindow to be registered.
-
prepareShutdown
public void prepareShutdown()Disable starting of new js threads.- Specified by:
prepareShutdownin interfaceAbstractJavaScriptEngine<org.htmlunit.corejs.javascript.Script>
-
shutdown
public void shutdown()Shutdown the JavaScriptEngine.- Specified by:
shutdownin interfaceAbstractJavaScriptEngine<org.htmlunit.corejs.javascript.Script>
-
compile
public org.htmlunit.corejs.javascript.Script compile(HtmlPage owningPage, org.htmlunit.corejs.javascript.Scriptable scope, String sourceCode, String sourceName, int startLine) Compiles the specified JavaScript code in the context of a given scope.- Specified by:
compilein interfaceAbstractJavaScriptEngine<org.htmlunit.corejs.javascript.Script>- Parameters:
owningPage- the page from which the code startedscope- the scope in which to execute the javascript codesourceCode- the JavaScript code to executesourceName- the name that will be displayed on error conditionsstartLine- the line at which the script source starts- Returns:
- the result of executing the specified code
-
compile
@Deprecated public org.htmlunit.corejs.javascript.Script compile(HtmlPage owningPage, String sourceCode, String sourceName, int startLine) Deprecated.Compiles the specified JavaScript code in the context of a given HTML page.- Specified by:
compilein interfaceAbstractJavaScriptEngine<org.htmlunit.corejs.javascript.Script>- Parameters:
owningPage- the page that the code will execute withinsourceCode- the JavaScript code to executesourceName- the name that will be displayed on error conditionsstartLine- the line at which the script source starts- Returns:
- the result of executing the specified code
-
callSecured
public final <T> T callSecured(org.htmlunit.corejs.javascript.ContextAction<T> action, HtmlPage page) Forwards this to theHtmlUnitContextFactorybut with checking shutdown handling.- Type Parameters:
T- return type of the action- Parameters:
action- the contextActionpage- the page- Returns:
- the result of the call
-
execute
public Object execute(HtmlPage page, org.htmlunit.corejs.javascript.Scriptable scope, String sourceCode, String sourceName, int startLine) Executes the specified JavaScript code in the context of a given page.- Specified by:
executein interfaceAbstractJavaScriptEngine<org.htmlunit.corejs.javascript.Script>- Parameters:
page- the page that the code will execute withinscope- the scope in which to executesourceCode- the JavaScript code to executesourceName- the name that will be displayed on error conditionsstartLine- the line at which the script source starts- Returns:
- the result of executing the specified code
-
execute
@Deprecated public Object execute(HtmlPage page, String sourceCode, String sourceName, int startLine) Deprecated.Executes the specified JavaScript code in the context of a given page.- Specified by:
executein interfaceAbstractJavaScriptEngine<org.htmlunit.corejs.javascript.Script>- Parameters:
page- the page that the code will execute withinsourceCode- the JavaScript code to executesourceName- the name that will be displayed on error conditionsstartLine- the line at which the script source starts- Returns:
- the result of executing the specified code
-
execute
public Object execute(HtmlPage page, org.htmlunit.corejs.javascript.Scriptable scope, org.htmlunit.corejs.javascript.Script script) Executes the specified JavaScript code in the context of a given page.- Specified by:
executein interfaceAbstractJavaScriptEngine<org.htmlunit.corejs.javascript.Script>- Parameters:
page- the page that the code will execute withinscope- the scope in which to executescript- the script to execute- Returns:
- the result of executing the specified code
-
execute
Deprecated.Executes the specified JavaScript code in the context of a given page.- Specified by:
executein interfaceAbstractJavaScriptEngine<org.htmlunit.corejs.javascript.Script>- Parameters:
page- the page that the code will execute withinscript- the script to execute- Returns:
- the result of executing the specified code
-
callFunction
public Object callFunction(HtmlPage page, org.htmlunit.corejs.javascript.Function javaScriptFunction, org.htmlunit.corejs.javascript.Scriptable thisObject, Object[] args, DomNode node) Calls a JavaScript function and return the result.- Parameters:
page- the pagejavaScriptFunction- the function to callthisObject- the this object for class method callsargs- the list of arguments to pass to the functionnode- the HTML element that will act as the context- Returns:
- the result of the function call
-
callFunction
public Object callFunction(HtmlPage page, org.htmlunit.corejs.javascript.Function function, org.htmlunit.corejs.javascript.Scriptable scope, org.htmlunit.corejs.javascript.Scriptable thisObject, Object[] args) Calls the given function taking care of synchronization issues.- Parameters:
page- the interactive page that caused this script to executedfunction- the JavaScript function to executescope- the execution scopethisObject- the 'this' objectargs- the function's arguments- Returns:
- the function result
-
isScriptRunning
public boolean isScriptRunning()Indicates if JavaScript is running in current thread.This allows code to know if there own evaluation is has been triggered by some JS code.
- Specified by:
isScriptRunningin interfaceAbstractJavaScriptEngine<org.htmlunit.corejs.javascript.Script>- Returns:
trueif JavaScript is running
-
addPostponedAction
Adds an action that should be executed first when the script currently being executed has finished.- Specified by:
addPostponedActionin interfaceAbstractJavaScriptEngine<org.htmlunit.corejs.javascript.Script>- Parameters:
action- the action
-
handleJavaScriptException
Handles an exception that occurred during execution of JavaScript code.- Parameters:
scriptException- the exceptiontriggerOnError- if true, this triggers the onerror handler
-
handleJavaScriptTimeoutError
Handles an exception that occurred during execution of JavaScript code.- Parameters:
page- the page in which the script causing this exception was executede- the timeout error that was thrown from the script engine
-
holdPosponedActions
public void holdPosponedActions()INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.
Indicates that no postponed action should be executed.- Specified by:
holdPosponedActionsin interfaceAbstractJavaScriptEngine<org.htmlunit.corejs.javascript.Script>
-
processPostponedActions
public void processPostponedActions()INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.
Process postponed actions, if any.- Specified by:
processPostponedActionsin interfaceAbstractJavaScriptEngine<org.htmlunit.corejs.javascript.Script>
-
getJavaScriptClass
Gets the class of the JavaScript object for the node class.- Parameters:
c- the node classDomNodeor some subclass.- Returns:
nullif none found
-
getJavaScriptConfiguration
Gets the associated configuration.- Specified by:
getJavaScriptConfigurationin interfaceAbstractJavaScriptEngine<org.htmlunit.corejs.javascript.Script>- Returns:
- the configuration
-
getJavaScriptTimeout
public long getJavaScriptTimeout()Returns the javascript timeout.- Specified by:
getJavaScriptTimeoutin interfaceAbstractJavaScriptEngine<org.htmlunit.corejs.javascript.Script>- Returns:
- the javascript timeout
-
setJavaScriptTimeout
public void setJavaScriptTimeout(long timeout) Sets the javascript timeout.- Specified by:
setJavaScriptTimeoutin interfaceAbstractJavaScriptEngine<org.htmlunit.corejs.javascript.Script>- Parameters:
timeout- the timeout
-
toNumber
Convert the value to a JavaScript Number value.- Parameters:
value- a JavaScript value- Returns:
- the corresponding double value converted using the ECMA rules
-
toString
-
toBoolean
Convert the value to a JavaScript boolean value.- Parameters:
value- a JavaScript value- Returns:
- the corresponding boolean value converted using the ECMA rules
-
throwAsScriptRuntimeEx
Rethrow the exception wrapping it as the script runtime exception.- Parameters:
e- the exception to rethrow- Returns:
- RuntimeException as dummy the method always throws
-
reportRuntimeError
Report a runtime error using the error reporter for the current thread.- Parameters:
message- the error message to report- Returns:
- RuntimeException as dummy the method always throws
-
typeError
Report a runtime error using the error reporter for the current thread.- Parameters:
message- the error message to report- Returns:
- EcmaError
-
rangeError
Report a runtime error using the error reporter for the current thread.- Parameters:
message- the error message to report- Returns:
- EcmaError
-
constructError
-
asJavaScriptException
public static org.htmlunit.corejs.javascript.RhinoException asJavaScriptException(Window window, DOMException exception) INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.
Encapsulates the givenDOMExceptioninto a Rhino-compatible exception.- Parameters:
window- the window to be used as parent scopeexception- the exception to encapsulate- Returns:
- the created exception
-
newArray
public static org.htmlunit.corejs.javascript.Scriptable newArray(org.htmlunit.corejs.javascript.Scriptable scope, int length) Create an array with a specified initial length.- Parameters:
scope- the scope to create the object inlength- the initial length (JavaScript arrays may have additional properties added dynamically).- Returns:
- the new array object
-
newArrayIteratorTypeKeys
public static org.htmlunit.corejs.javascript.Scriptable newArrayIteratorTypeKeys(org.htmlunit.corejs.javascript.Scriptable scope, org.htmlunit.corejs.javascript.Scriptable arrayLike) INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.
Create a new ArrayIterator of type NativeArrayIterator.ARRAY_ITERATOR_TYPE.KEYS- Parameters:
scope- the scope to create the object inarrayLike- the backend- Returns:
- the new NativeArrayIterator
-
newArrayIteratorTypeValues
public static org.htmlunit.corejs.javascript.Scriptable newArrayIteratorTypeValues(org.htmlunit.corejs.javascript.Scriptable scope, org.htmlunit.corejs.javascript.Scriptable arrayLike) INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.
Create a new ArrayIterator of type NativeArrayIterator.ARRAY_ITERATOR_TYPE.VALUES- Parameters:
scope- the scope to create the object inarrayLike- the backend- Returns:
- the new NativeArrayIterator
-
newArrayIteratorTypeEntries
public static org.htmlunit.corejs.javascript.Scriptable newArrayIteratorTypeEntries(org.htmlunit.corejs.javascript.Scriptable scope, org.htmlunit.corejs.javascript.Scriptable arrayLike) INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.
Create a new ArrayIterator of type NativeArrayIterator.ARRAY_ITERATOR_TYPE.ENTRIES- Parameters:
scope- the scope to create the object inarrayLike- the backend- Returns:
- the new NativeArrayIterator
-
newArray
public static org.htmlunit.corejs.javascript.Scriptable newArray(org.htmlunit.corejs.javascript.Scriptable scope, Object[] elements) Create an array with a specified initial length.- Parameters:
scope- the scope to create the object inelements- the initial elements. Each object in this array must be an acceptable JavaScript type and type of array should be exactly Object[], not SomeObjectSubclass[].- Returns:
- the new array object
-
toInt32
- Parameters:
o- the object to convert- Returns:
- int value
-
toInteger
- Parameters:
o- the object to convert- Returns:
- double value
-
toInteger
- Parameters:
args- an arrayindex- the index in the array- Returns:
- double value
-
isUndefined
- Parameters:
obj- the value to check- Returns:
- whether obj is undefined
-
isNaN
- Parameters:
obj- the value to check- Returns:
- whether obj is NAN
-
uncompressJavaScript
-
evaluateProxyAutoConfig
public static String evaluateProxyAutoConfig(BrowserVersion browserVersion, String content, URL url) Evaluates theFindProxyForURLmethod of the specified content.- Parameters:
browserVersion- the browser version to usecontent- the JavaScript contenturl- the URL to be retrieved- Returns:
- semicolon-separated result
-