1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.htmlunit.javascript;
16
17 import static org.htmlunit.BrowserVersionFeatures.JS_ARRAY_SORT_ACCEPTS_INCONSISTENT_COMPERATOR;
18 import static org.htmlunit.BrowserVersionFeatures.JS_PROPERTY_DESCRIPTOR_NAME;
19
20 import java.io.Serializable;
21 import java.util.function.Consumer;
22
23 import org.htmlunit.BrowserVersion;
24 import org.htmlunit.ScriptException;
25 import org.htmlunit.ScriptPreProcessor;
26 import org.htmlunit.WebClient;
27 import org.htmlunit.corejs.javascript.Callable;
28 import org.htmlunit.corejs.javascript.CompilerEnvirons;
29 import org.htmlunit.corejs.javascript.Context;
30 import org.htmlunit.corejs.javascript.ContextAction;
31 import org.htmlunit.corejs.javascript.ContextFactory;
32 import org.htmlunit.corejs.javascript.ErrorReporter;
33 import org.htmlunit.corejs.javascript.Evaluator;
34 import org.htmlunit.corejs.javascript.EvaluatorException;
35 import org.htmlunit.corejs.javascript.Function;
36 import org.htmlunit.corejs.javascript.Script;
37 import org.htmlunit.corejs.javascript.Scriptable;
38 import org.htmlunit.corejs.javascript.debug.Debugger;
39 import org.htmlunit.html.HtmlElement;
40 import org.htmlunit.html.HtmlPage;
41
42
43
44
45
46
47
48
49
50
51 public class HtmlUnitContextFactory extends ContextFactory {
52
53 private static final int INSTRUCTION_COUNT_THRESHOLD = 10_000;
54
55 private final WebClient webClient_;
56 private final BrowserVersion browserVersion_;
57 private long timeout_;
58 private Debugger debugger_;
59 private boolean deminifyFunctionCode_;
60
61
62
63
64
65
66 public HtmlUnitContextFactory(final WebClient webClient) {
67 super();
68 webClient_ = webClient;
69 browserVersion_ = webClient.getBrowserVersion();
70 }
71
72
73
74
75
76
77
78 public void setTimeout(final long timeout) {
79 timeout_ = timeout;
80 }
81
82
83
84
85
86
87
88 public long getTimeout() {
89 return timeout_;
90 }
91
92
93
94
95
96
97
98
99 public void setDebugger(final Debugger debugger) {
100 debugger_ = debugger;
101 }
102
103
104
105
106
107
108
109 public Debugger getDebugger() {
110 return debugger_;
111 }
112
113
114
115
116
117
118 public void setDeminifyFunctionCode(final boolean deminify) {
119 deminifyFunctionCode_ = deminify;
120 }
121
122
123
124
125
126
127 public boolean isDeminifyFunctionCode() {
128 return deminifyFunctionCode_;
129 }
130
131
132
133
134 private class TimeoutContext extends Context {
135 private long startTime_;
136
137 protected TimeoutContext(final ContextFactory factory) {
138 super(factory);
139 }
140
141 public void startClock() {
142 startTime_ = System.currentTimeMillis();
143 }
144
145 public void terminateScriptIfNecessary() {
146 if (timeout_ > 0) {
147 final long currentTime = System.currentTimeMillis();
148 if (currentTime - startTime_ > timeout_) {
149
150
151 throw new TimeoutError(timeout_, currentTime - startTime_);
152 }
153 }
154 }
155
156 @Override
157 protected Script compileString(String source, final Evaluator compiler,
158 final ErrorReporter compilationErrorReporter, final String sourceName,
159 final int lineno, final Object securityDomain,
160 final Consumer<CompilerEnvirons> compilerEnvironsProcessor) {
161
162
163
164 final boolean isWindowEval = compiler != null;
165
166
167 if (!isWindowEval) {
168
169
170
171
172
173
174
175
176 final int length = source.length();
177 int start = 0;
178 while ((start < length) && (source.charAt(start) <= ' ')) {
179 start++;
180 }
181 if (start + 3 < length
182 && source.charAt(start++) == '<'
183 && source.charAt(start++) == '!'
184 && source.charAt(start++) == '-'
185 && source.charAt(start++) == '-') {
186 source = source.replaceFirst("<!--", "// <!--");
187 }
188 }
189
190
191 final HtmlPage page = (HtmlPage) Context.getCurrentContext()
192 .getThreadLocal(JavaScriptEngine.KEY_STARTING_PAGE);
193 source = preProcess(page, source, sourceName, lineno, null);
194
195 return super.compileString(source, compiler, compilationErrorReporter,
196 sourceName, lineno, securityDomain, compilerEnvironsProcessor);
197 }
198
199 @Override
200 protected Function compileFunction(final Scriptable scope, String source,
201 final Evaluator compiler, final ErrorReporter compilationErrorReporter,
202 final String sourceName, final int lineno, final Object securityDomain) {
203
204 if (deminifyFunctionCode_) {
205 final Function f = super.compileFunction(scope, source, compiler,
206 compilationErrorReporter, sourceName, lineno, securityDomain);
207 source = decompileFunction(f, 4).trim().replace("\n ", "\n");
208 }
209 return super.compileFunction(scope, source, compiler,
210 compilationErrorReporter, sourceName, lineno, securityDomain);
211 }
212 }
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227 protected String preProcess(
228 final HtmlPage htmlPage, final String sourceCode, final String sourceName, final int lineNumber,
229 final HtmlElement htmlElement) {
230
231 String newSourceCode = sourceCode;
232 final ScriptPreProcessor preProcessor = webClient_.getScriptPreProcessor();
233 if (preProcessor != null) {
234 newSourceCode = preProcessor.preProcess(htmlPage, sourceCode, sourceName, lineNumber, htmlElement);
235 if (newSourceCode == null) {
236 newSourceCode = "";
237 }
238 }
239 return newSourceCode;
240 }
241
242
243
244
245 @Override
246 protected Context makeContext() {
247 final TimeoutContext cx = new TimeoutContext(this);
248 cx.setLanguageVersion(Context.VERSION_ES6);
249 cx.setLocale(browserVersion_.getBrowserLocale());
250 cx.setTimeZone(browserVersion_.getSystemTimezone());
251
252
253 cx.setClassShutter(fullClassName -> false);
254
255
256 cx.setInterpretedMode(true);
257
258
259 cx.setInstructionObserverThreshold(INSTRUCTION_COUNT_THRESHOLD);
260
261 cx.setErrorReporter(new HtmlUnitErrorReporter(webClient_.getJavaScriptErrorListener()));
262
263 cx.getWrapFactory().setJavaPrimitiveWrap(false);
264
265 if (debugger_ != null) {
266 cx.setDebugger(debugger_, null);
267 }
268
269 cx.setMaximumInterpreterStackDepth(5_000);
270
271 return cx;
272 }
273
274
275
276
277
278
279
280
281
282 @Override
283 protected void observeInstructionCount(final Context cx, final int instructionCount) {
284 final TimeoutContext tcx = (TimeoutContext) cx;
285 tcx.terminateScriptIfNecessary();
286 }
287
288
289
290
291 @Override
292 protected Object doTopCall(final Callable callable,
293 final Context cx, final Scriptable scope,
294 final Scriptable thisObj, final Object[] args) {
295
296 final TimeoutContext tcx = (TimeoutContext) cx;
297 tcx.startClock();
298 return super.doTopCall(callable, cx, scope, thisObj, args);
299 }
300
301
302
303
304 @Override
305 protected Object doTopCall(final Script script,
306 final Context cx, final Scriptable scope,
307 final Scriptable thisObj) {
308
309 final TimeoutContext tcx = (TimeoutContext) cx;
310 tcx.startClock();
311 return super.doTopCall(script, cx, scope, thisObj);
312 }
313
314
315
316
317
318
319
320
321
322
323 public final <T> T callSecured(final ContextAction<T> action, final HtmlPage page) {
324 try {
325 return call(action);
326 }
327 catch (final StackOverflowError e) {
328 webClient_.getJavaScriptErrorListener().scriptException(page, new ScriptException(page, e));
329 return null;
330 }
331 }
332
333
334
335
336 @Override
337 protected boolean hasFeature(final Context cx, final int featureIndex) {
338 switch (featureIndex) {
339 case Context.FEATURE_RESERVED_KEYWORD_AS_IDENTIFIER:
340 case Context.FEATURE_OLD_UNDEF_NULL_THIS:
341 case Context.FEATURE_LITTLE_ENDIAN:
342 case Context.FEATURE_LOCATION_INFORMATION_IN_ERROR:
343 case Context.FEATURE_INTL_402:
344 case Context.FEATURE_HTMLUNIT_FN_ARGUMENTS_IS_RO_VIEW:
345 return true;
346 case Context.FEATURE_E4X:
347 case Context.FEATURE_NON_ECMA_GET_YEAR:
348 return false;
349 case Context.FEATURE_HTMLUNIT_MEMBERBOX_NAME:
350 return browserVersion_.hasFeature(JS_PROPERTY_DESCRIPTOR_NAME);
351 case Context.FEATURE_HTMLUNIT_ARRAY_SORT_COMPERATOR_ACCEPTS_BOOL:
352 return browserVersion_.hasFeature(JS_ARRAY_SORT_ACCEPTS_INCONSISTENT_COMPERATOR);
353 default:
354 return super.hasFeature(cx, featureIndex);
355 }
356 }
357
358 private static final class HtmlUnitErrorReporter implements ErrorReporter, Serializable {
359
360 private final JavaScriptErrorListener javaScriptErrorListener_;
361
362
363
364
365
366
367 HtmlUnitErrorReporter(final JavaScriptErrorListener javaScriptErrorListener) {
368 javaScriptErrorListener_ = javaScriptErrorListener;
369 }
370
371
372
373
374
375
376
377
378
379
380 @Override
381 public void warning(
382 final String message, final String sourceName, final int line,
383 final String lineSource, final int lineOffset) {
384 javaScriptErrorListener_.warn(message, sourceName, line, lineSource, lineOffset);
385 }
386
387
388
389
390
391
392
393
394
395
396 @Override
397 public void error(final String message, final String sourceName, final int line,
398 final String lineSource, final int lineOffset) {
399
400
401 throw new EvaluatorException(message, sourceName, line, lineSource, lineOffset);
402 }
403
404
405
406
407
408
409
410
411
412
413
414 @Override
415 public EvaluatorException runtimeError(
416 final String message, final String sourceName, final int line,
417 final String lineSource, final int lineOffset) {
418
419
420 return new EvaluatorException(message, sourceName, line, lineSource, lineOffset);
421 }
422 }
423 }