public class JavaScriptEngine extends Object implements AbstractJavaScriptEngine<org.htmlunit.corejs.javascript.Script>
Like all classes in this package, this class is not intended for direct use and may change without notice.
Modifier and Type | Field and Description |
---|---|
static Object[] |
emptyArgs
ScriptRuntime.emptyArgs.
|
static String |
KEY_STARTING_PAGE
Key used to place the
HtmlPage for which the JavaScript code is executed
as thread local attribute in current context. |
static String |
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.
|
static Object |
Undefined
org.htmlunit.corejs.javascript.Undefined.instance.
|
Constructor and Description |
---|
JavaScriptEngine(WebClient webClient)
Creates an instance for the specified
WebClient . |
Modifier and Type | Method and Description |
---|---|
void |
addPostponedAction(PostponedAction action)
Adds an action that should be executed first when the script currently being executed has finished.
|
static void |
applyPolyfills(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.RhinoException |
asJavaScriptException(Window window,
DOMException exception)
INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.
Encapsulates the given DOMException into a Rhino-compatible exception. |
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.
|
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.
|
<T> T |
callSecured(org.htmlunit.corejs.javascript.ContextAction<T> action,
HtmlPage page)
Forwards this to the
HtmlUnitContextFactory but with checking shutdown handling. |
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.
|
org.htmlunit.corejs.javascript.Script |
compile(HtmlPage owningPage,
String sourceCode,
String sourceName,
int startLine)
Deprecated.
|
static HtmlUnitScriptable |
configureClass(ClassConfiguration config,
org.htmlunit.corejs.javascript.Scriptable window,
BrowserVersion browserVersion)
Configures the specified class for access via JavaScript.
|
static void |
configureRhino(WebClient webClient,
BrowserVersion browserVersion,
HtmlUnitScriptable scriptable)
INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.
|
static org.htmlunit.corejs.javascript.EcmaError |
constructError(String error,
String message) |
static String |
evaluateProxyAutoConfig(BrowserVersion browserVersion,
String content,
URL url)
Evaluates the
FindProxyForURL method of the specified content. |
Object |
execute(HtmlPage page,
org.htmlunit.corejs.javascript.Script script)
Deprecated.
|
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.
|
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.
|
Object |
execute(HtmlPage page,
String sourceCode,
String sourceName,
int startLine)
Deprecated.
|
HtmlUnitContextFactory |
getContextFactory() |
Class<? extends HtmlUnitScriptable> |
getJavaScriptClass(Class<?> c)
Gets the class of the JavaScript object for the node class.
|
JavaScriptConfiguration |
getJavaScriptConfiguration()
Gets the associated configuration.
|
JavaScriptExecutor |
getJavaScriptExecutor()
Returns the JavaScriptExecutor.
|
long |
getJavaScriptTimeout()
Returns the javascript timeout.
|
protected void |
handleJavaScriptException(ScriptException scriptException,
boolean triggerOnError)
Handles an exception that occurred during execution of JavaScript code.
|
protected void |
handleJavaScriptTimeoutError(HtmlPage page,
TimeoutError e)
Handles an exception that occurred during execution of JavaScript code.
|
void |
holdPosponedActions()
INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.
Indicates that no postponed action should be executed. |
void |
initialize(WebWindow webWindow,
Page page)
Performs initialization for the given webWindow.
|
static boolean |
isNaN(Object obj) |
boolean |
isScriptRunning()
Indicates if JavaScript is running in current thread.
|
static boolean |
isUndefined(Object obj) |
static org.htmlunit.corejs.javascript.Scriptable |
newArray(org.htmlunit.corejs.javascript.Scriptable scope,
int length)
Create an array with a specified initial length.
|
static org.htmlunit.corejs.javascript.Scriptable |
newArray(org.htmlunit.corejs.javascript.Scriptable scope,
Object[] elements)
Create an array with a specified initial length.
|
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 |
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 |
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 |
void |
prepareShutdown()
Disable starting of new js threads.
|
void |
processPostponedActions()
INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.
Process postponed actions, if any. |
static org.htmlunit.corejs.javascript.EcmaError |
rangeError(String message)
Report a runtime error using the error reporter for the current thread.
|
void |
registerWindowAndMaybeStartEventLoop(WebWindow webWindow)
Register WebWindow with the JavaScriptExecutor.
|
static RuntimeException |
reportRuntimeError(String message)
Report a runtime error using the error reporter for the current thread.
|
void |
setJavaScriptTimeout(long timeout)
Sets the javascript timeout.
|
void |
shutdown()
Shutdown the JavaScriptEngine.
|
static RuntimeException |
throwAsScriptRuntimeEx(Throwable e)
Rethrow the exception wrapping it as the script runtime exception.
|
static boolean |
toBoolean(Object value)
Convert the value to a JavaScript boolean value.
|
static int |
toInt32(Object o) |
static double |
toInteger(Object o) |
static double |
toInteger(Object[] args,
int index) |
static double |
toNumber(Object value)
Convert the value to a JavaScript Number value.
|
static String |
toString(Object value)
Convert the value to a JavaScript String value.
|
static org.htmlunit.corejs.javascript.EcmaError |
typeError(String message)
Report a runtime error using the error reporter for the current thread.
|
static String |
uncompressJavaScript(String scriptSource,
String scriptName)
Tries to uncompress the JavaScript code in the provided response.
|
public static final Object[] emptyArgs
public static final Object Undefined
public static final String KEY_STARTING_SCOPE
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.
public static final String KEY_STARTING_PAGE
HtmlPage
for which the JavaScript code is executed
as thread local attribute in current context.public HtmlUnitContextFactory getContextFactory()
getContextFactory
in interface AbstractJavaScriptEngine<org.htmlunit.corejs.javascript.Script>
HtmlUnitContextFactory
public void initialize(WebWindow webWindow, Page page)
initialize
in interface AbstractJavaScriptEngine<org.htmlunit.corejs.javascript.Script>
webWindow
- the web window to initialize forpage
- the page that will become the enclosing pagepublic JavaScriptExecutor getJavaScriptExecutor()
public static void configureRhino(WebClient webClient, BrowserVersion browserVersion, HtmlUnitScriptable scriptable)
webClient
- the WebClientbrowserVersion
- the BrowserVersionscriptable
- the window or the DedicatedWorkerGlobalScopepublic static void applyPolyfills(WebClient webClient, BrowserVersion browserVersion, org.htmlunit.corejs.javascript.Context context, HtmlUnitScriptable scriptable) throws IOException
webClient
- the WebClientbrowserVersion
- the BrowserVersioncontext
- the current contextscriptable
- the window or the DedicatedWorkerGlobalScopeIOException
- in case of problemspublic static HtmlUnitScriptable configureClass(ClassConfiguration config, org.htmlunit.corejs.javascript.Scriptable window, BrowserVersion browserVersion) throws InstantiationException, IllegalAccessException
config
- the configuration settings for the class to be configuredwindow
- the scope within which to configure the classbrowserVersion
- the browser versionInstantiationException
- if the new class cannot be instantiatedIllegalAccessException
- if we don't have access to create the new instancepublic void registerWindowAndMaybeStartEventLoop(WebWindow webWindow)
registerWindowAndMaybeStartEventLoop
in interface AbstractJavaScriptEngine<org.htmlunit.corejs.javascript.Script>
webWindow
- the WebWindow to be registered.public void prepareShutdown()
prepareShutdown
in interface AbstractJavaScriptEngine<org.htmlunit.corejs.javascript.Script>
public void shutdown()
shutdown
in interface AbstractJavaScriptEngine<org.htmlunit.corejs.javascript.Script>
public org.htmlunit.corejs.javascript.Script compile(HtmlPage owningPage, org.htmlunit.corejs.javascript.Scriptable scope, String sourceCode, String sourceName, int startLine)
compile
in interface AbstractJavaScriptEngine<org.htmlunit.corejs.javascript.Script>
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@Deprecated public org.htmlunit.corejs.javascript.Script compile(HtmlPage owningPage, String sourceCode, String sourceName, int startLine)
compile
in interface AbstractJavaScriptEngine<org.htmlunit.corejs.javascript.Script>
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 startspublic final <T> T callSecured(org.htmlunit.corejs.javascript.ContextAction<T> action, HtmlPage page)
HtmlUnitContextFactory
but with checking shutdown handling.T
- return type of the actionaction
- the contextActionpage
- the pagepublic Object execute(HtmlPage page, org.htmlunit.corejs.javascript.Scriptable scope, String sourceCode, String sourceName, int startLine)
execute
in interface AbstractJavaScriptEngine<org.htmlunit.corejs.javascript.Script>
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@Deprecated public Object execute(HtmlPage page, String sourceCode, String sourceName, int startLine)
execute
in interface AbstractJavaScriptEngine<org.htmlunit.corejs.javascript.Script>
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 startspublic Object execute(HtmlPage page, org.htmlunit.corejs.javascript.Scriptable scope, org.htmlunit.corejs.javascript.Script script)
execute
in interface AbstractJavaScriptEngine<org.htmlunit.corejs.javascript.Script>
page
- the page that the code will execute withinscope
- the scope in which to executescript
- the script to execute@Deprecated public Object execute(HtmlPage page, org.htmlunit.corejs.javascript.Script script)
execute
in interface AbstractJavaScriptEngine<org.htmlunit.corejs.javascript.Script>
page
- the page that the code will execute withinscript
- the script to executepublic Object callFunction(HtmlPage page, org.htmlunit.corejs.javascript.Function javaScriptFunction, org.htmlunit.corejs.javascript.Scriptable thisObject, Object[] args, DomNode node)
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 contextpublic Object callFunction(HtmlPage page, org.htmlunit.corejs.javascript.Function function, org.htmlunit.corejs.javascript.Scriptable scope, org.htmlunit.corejs.javascript.Scriptable thisObject, Object[] args)
page
- the interactive page that caused this script to executedfunction
- the JavaScript function to executescope
- the execution scopethisObject
- the 'this' objectargs
- the function's argumentspublic boolean isScriptRunning()
This allows code to know if there own evaluation is has been triggered by some JS code.
isScriptRunning
in interface AbstractJavaScriptEngine<org.htmlunit.corejs.javascript.Script>
true
if JavaScript is runningpublic void addPostponedAction(PostponedAction action)
addPostponedAction
in interface AbstractJavaScriptEngine<org.htmlunit.corejs.javascript.Script>
action
- the actionprotected void handleJavaScriptException(ScriptException scriptException, boolean triggerOnError)
scriptException
- the exceptiontriggerOnError
- if true, this triggers the onerror handlerprotected void handleJavaScriptTimeoutError(HtmlPage page, TimeoutError e)
page
- the page in which the script causing this exception was executede
- the timeout error that was thrown from the script enginepublic void holdPosponedActions()
holdPosponedActions
in interface AbstractJavaScriptEngine<org.htmlunit.corejs.javascript.Script>
public void processPostponedActions()
processPostponedActions
in interface AbstractJavaScriptEngine<org.htmlunit.corejs.javascript.Script>
public Class<? extends HtmlUnitScriptable> getJavaScriptClass(Class<?> c)
c
- the node class DomNode
or some subclass.null
if none foundpublic JavaScriptConfiguration getJavaScriptConfiguration()
getJavaScriptConfiguration
in interface AbstractJavaScriptEngine<org.htmlunit.corejs.javascript.Script>
public long getJavaScriptTimeout()
getJavaScriptTimeout
in interface AbstractJavaScriptEngine<org.htmlunit.corejs.javascript.Script>
public void setJavaScriptTimeout(long timeout)
setJavaScriptTimeout
in interface AbstractJavaScriptEngine<org.htmlunit.corejs.javascript.Script>
timeout
- the timeoutpublic static double toNumber(Object value)
value
- a JavaScript valuepublic static String toString(Object value)
value
- a JavaScript valuepublic static boolean toBoolean(Object value)
value
- a JavaScript valuepublic static RuntimeException throwAsScriptRuntimeEx(Throwable e)
e
- the exception to rethrowpublic static RuntimeException reportRuntimeError(String message)
message
- the error message to reportpublic static org.htmlunit.corejs.javascript.EcmaError typeError(String message)
message
- the error message to reportpublic static org.htmlunit.corejs.javascript.EcmaError rangeError(String message)
message
- the error message to reportpublic static org.htmlunit.corejs.javascript.EcmaError constructError(String error, String message)
error
- the errormessage
- the messagepublic static org.htmlunit.corejs.javascript.RhinoException asJavaScriptException(Window window, DOMException exception)
DOMException
into a Rhino-compatible exception.window
- the window to be used as parent scopeexception
- the exception to encapsulatepublic static org.htmlunit.corejs.javascript.Scriptable newArray(org.htmlunit.corejs.javascript.Scriptable scope, int length)
scope
- the scope to create the object inlength
- the initial length (JavaScript arrays may have additional properties added
dynamically).public static org.htmlunit.corejs.javascript.Scriptable newArrayIteratorTypeKeys(org.htmlunit.corejs.javascript.Scriptable scope, org.htmlunit.corejs.javascript.Scriptable arrayLike)
scope
- the scope to create the object inarrayLike
- the backendpublic static org.htmlunit.corejs.javascript.Scriptable newArrayIteratorTypeValues(org.htmlunit.corejs.javascript.Scriptable scope, org.htmlunit.corejs.javascript.Scriptable arrayLike)
scope
- the scope to create the object inarrayLike
- the backendpublic static org.htmlunit.corejs.javascript.Scriptable newArrayIteratorTypeEntries(org.htmlunit.corejs.javascript.Scriptable scope, org.htmlunit.corejs.javascript.Scriptable arrayLike)
scope
- the scope to create the object inarrayLike
- the backendpublic static org.htmlunit.corejs.javascript.Scriptable newArray(org.htmlunit.corejs.javascript.Scriptable scope, Object[] elements)
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[].public static int toInt32(Object o)
o
- the object to convertpublic static double toInteger(Object o)
o
- the object to convertpublic static double toInteger(Object[] args, int index)
args
- an arrayindex
- the index in the arraypublic static boolean isUndefined(Object obj)
obj
- the value to checkpublic static boolean isNaN(Object obj)
obj
- the value to checkpublic static String uncompressJavaScript(String scriptSource, String scriptName)
scriptSource
- the soucescriptName
- the namepublic static String evaluateProxyAutoConfig(BrowserVersion browserVersion, String content, URL url)
FindProxyForURL
method of the specified content.browserVersion
- the browser version to usecontent
- the JavaScript contenturl
- the URL to be retrievedCopyright © 2002–2024 Gargoyle Software Inc.. All rights reserved.