1 /*
2 * Copyright (c) 2002-2026 Gargoyle Software Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 * https://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 package org.htmlunit;
16
17 import org.htmlunit.javascript.JavaScriptEngine;
18
19 /**
20 * This object contains the result of executing a chunk of script code.
21 *
22 * @author Mike Bowler
23 * @author Marc Guillemot
24 * @author Ronald Brill
25 */
26 public final class ScriptResult {
27
28 /** The object that was returned from the script engine. */
29 private final Object javaScriptResult_;
30
31 /**
32 * Creates a new instance.
33 *
34 * @param javaScriptResult the object that was returned from the script engine
35 */
36 public ScriptResult(final Object javaScriptResult) {
37 javaScriptResult_ = javaScriptResult;
38 }
39
40 /**
41 * Returns the object that was the output of the script engine.
42 * @return the result from the script engine
43 */
44 public Object getJavaScriptResult() {
45 return javaScriptResult_;
46 }
47
48 /**
49 * {@inheritDoc}
50 */
51 @Override
52 public String toString() {
53 return "ScriptResult[result=" + javaScriptResult_ + "]";
54 }
55
56 /**
57 * Utility method testing if a script result is {@code false}.
58 * @param scriptResult a script result (may be {@code null})
59 * @return {@code true} if <code>scriptResult</code> is {@code false}
60 */
61 public static boolean isFalse(final ScriptResult scriptResult) {
62 return scriptResult != null && Boolean.FALSE.equals(scriptResult.getJavaScriptResult());
63 }
64
65 /**
66 * Utility method testing if a script result is undefined (there was no return value).
67 * @param scriptResult a script result (may be {@code null})
68 * @return {@code true} if <code>scriptResult</code> is undefined (there was no return value)
69 */
70 public static boolean isUndefined(final ScriptResult scriptResult) {
71 return scriptResult != null && JavaScriptEngine.isUndefined(scriptResult.getJavaScriptResult());
72 }
73 }