@@ -718,18 +718,7 @@ public Script compile(final HtmlPage owningPage, final Scriptable scope, final S
718718 LOG .trace ("Javascript compile " + sourceName + newline + sourceCode + newline );
719719 }
720720
721- final HtmlUnitContextAction action = new HtmlUnitContextAction (owningPage ) {
722- @ Override
723- public Object doRun (final Context cx ) {
724- return cx .compileString (sourceCode , sourceName , startLine , null );
725- }
726-
727- @ Override
728- protected String getSourceCode (final Context cx ) {
729- return sourceCode ;
730- }
731- };
732-
721+ final HtmlUnitCompileContextAction action = new HtmlUnitCompileContextAction (owningPage , sourceCode , sourceName , startLine );
733722 return (Script ) getContextFactory ().callSecured (action , owningPage );
734723 }
735724
@@ -862,6 +851,49 @@ public boolean isScriptRunning() {
862851 return Boolean .TRUE .equals (javaScriptRunning_ .get ());
863852 }
864853
854+ /**
855+ * Special ContextAction only for compiling. This reduces some code and avoid
856+ * some calls.
857+ */
858+ private final class HtmlUnitCompileContextAction implements ContextAction <Object > {
859+ private final HtmlPage page_ ;
860+ private final String sourceCode_ ;
861+ private final String sourceName_ ;
862+ private final int startLine_ ;
863+
864+ HtmlUnitCompileContextAction (final HtmlPage page , final String sourceCode , final String sourceName , final int startLine ) {
865+ page_ = page ;
866+ sourceCode_ = sourceCode ;
867+ sourceName_ = sourceName ;
868+ startLine_ = startLine ;
869+ }
870+
871+ @ Override
872+ public Object run (final Context cx ) {
873+ try {
874+ final Object response ;
875+ cx .putThreadLocal (KEY_STARTING_PAGE , page_ );
876+ synchronized (page_ ) { // 2 scripts can't be executed in parallel for one page
877+ if (page_ != page_ .getEnclosingWindow ().getEnclosedPage ()) {
878+ return null ; // page has been unloaded
879+ }
880+ response = cx .compileString (sourceCode_ , sourceName_ , startLine_ , null );
881+
882+ }
883+
884+ return response ;
885+ }
886+ catch (final Exception e ) {
887+ handleJavaScriptException (new ScriptException (page_ , e , sourceCode_ ), true );
888+ return null ;
889+ }
890+ catch (final TimeoutError e ) {
891+ handleJavaScriptTimeoutError (page_ , e );
892+ return null ;
893+ }
894+ }
895+ }
896+
865897 /**
866898 * Facility for ContextAction usage.
867899 * ContextAction should be preferred because according to Rhino doc it
@@ -881,7 +913,6 @@ public final Object run(final Context cx) {
881913
882914 try {
883915 final Object response ;
884- cx .putThreadLocal (KEY_STARTING_PAGE , page_ );
885916 synchronized (page_ ) { // 2 scripts can't be executed in parallel for one page
886917 if (page_ != page_ .getEnclosingWindow ().getEnclosedPage ()) {
887918 return null ; // page has been unloaded
@@ -896,6 +927,7 @@ public final Object run(final Context cx) {
896927 if (postponedActionsBlocker_ == null ) {
897928 doProcessPostponedActions ();
898929 }
930+
899931 return response ;
900932 }
901933 catch (final Exception e ) {
0 commit comments