Search in sources :

Example 21 with Scriptable

use of net.sourceforge.htmlunit.corejs.javascript.Scriptable in project htmlunit-core-js by HtmlUnit.

the class HostExceptionsTest method doTest.

private static void doTest(final String jsExpression) throws Exception {
    final String script = "var foo = new MyScriptable(); try { " + jsExpression + "} catch(e) {}";
    final ContextAction<Object> action = new ContextAction<Object>() {

        @Override
        public Object run(final Context cx) {
            final Scriptable scope = cx.initSafeStandardObjects();
            try {
                ScriptableObject.defineClass(scope, MyScriptable.class);
            } catch (final Exception e) {
                throw new RuntimeException();
            }
            cx.evaluateString(scope, script, "test.js", 0, null);
            return null;
        }
    };
    Utils.runWithAllOptimizationLevels(action);
}
Also used : Context(net.sourceforge.htmlunit.corejs.javascript.Context) ContextAction(net.sourceforge.htmlunit.corejs.javascript.ContextAction) ScriptableObject(net.sourceforge.htmlunit.corejs.javascript.ScriptableObject) Scriptable(net.sourceforge.htmlunit.corejs.javascript.Scriptable)

Example 22 with Scriptable

use of net.sourceforge.htmlunit.corejs.javascript.Scriptable in project htmlunit-core-js by HtmlUnit.

the class LookupSetterTest method test.

private void test(final String expected, final String src) throws Exception {
    final ContextAction<Object> action = new ContextAction<Object>() {

        @Override
        public Object run(final Context cx) {
            try {
                final Scriptable scope = cx.initSafeStandardObjects(new TopScope());
                ScriptableObject.defineClass(scope, Foo.class);
                cx.evaluateString(scope, defineSetterAndGetterX, "initX", 1, null);
                Object result = String.valueOf(cx.evaluateString(scope, src, "test", 1, null));
                assertEquals(expected, result);
                return null;
            } catch (final Exception e) {
                if (e instanceof RuntimeException)
                    throw (RuntimeException) e;
                throw new RuntimeException(e);
            }
        }
    };
    Utils.runWithAllOptimizationLevels(action);
}
Also used : Context(net.sourceforge.htmlunit.corejs.javascript.Context) ContextAction(net.sourceforge.htmlunit.corejs.javascript.ContextAction) ScriptableObject(net.sourceforge.htmlunit.corejs.javascript.ScriptableObject) Scriptable(net.sourceforge.htmlunit.corejs.javascript.Scriptable)

Example 23 with Scriptable

use of net.sourceforge.htmlunit.corejs.javascript.Scriptable in project htmlunit-core-js by HtmlUnit.

the class NativeErrorTest method stack.

/**
 * @throws Exception if the test fails
 */
@Test
public void stack() throws Exception {
    final String script = "function test() {\n" + "  try {\n" + "    null.method();\n" + "  } catch (e) {\n" + "    if (e.stack)\n" + "      output += e.stack.indexOf('\tat test.js:3 (test)') != -1;\n" + "    else\n" + "      output += 'undefined';\n" + "  }\n" + "}\n" + "var output = '';\n" + "test();\n" + "output";
    final ContextAction<Object> action = new ContextAction<Object>() {

        @Override
        public Object run(final Context cx) {
            final Scriptable scope = cx.initSafeStandardObjects();
            final Object result = cx.evaluateString(scope, script, "test.js", 1, null);
            assertEquals("true", result);
            return null;
        }
    };
    Utils.runWithAllOptimizationLevels(action);
}
Also used : Context(net.sourceforge.htmlunit.corejs.javascript.Context) ContextAction(net.sourceforge.htmlunit.corejs.javascript.ContextAction) Scriptable(net.sourceforge.htmlunit.corejs.javascript.Scriptable) Test(org.junit.jupiter.api.Test)

Example 24 with Scriptable

use of net.sourceforge.htmlunit.corejs.javascript.Scriptable in project htmlunit-core-js by HtmlUnit.

the class NativeErrorTest method stackNewError.

/**
 * @throws Exception if the test fails
 */
@Test
public void stackNewError() throws Exception {
    final ContextFactory cf = new ContextFactory();
    final String script = "function test() {\n" + "  try {\n" + "    throw new Error();\n" + "  } catch (e) {\n" + "    if (e.stack)\n" + "      output += typeof e.stack;\n" + "    else\n" + "      output += 'undefined';\n" + "  }\n" + "}\n" + "var output = '';\n" + "test();\n" + "output";
    final ContextAction<Object> action = new ContextAction<Object>() {

        @Override
        public Object run(final Context cx) {
            final Scriptable scope = cx.initSafeStandardObjects();
            final Object result = cx.evaluateString(scope, script, "test.js", 1, null);
            assertEquals("string", result);
            return null;
        }
    };
    Utils.runWithAllOptimizationLevels(cf, action);
}
Also used : ContextFactory(net.sourceforge.htmlunit.corejs.javascript.ContextFactory) Context(net.sourceforge.htmlunit.corejs.javascript.Context) ContextAction(net.sourceforge.htmlunit.corejs.javascript.ContextAction) Scriptable(net.sourceforge.htmlunit.corejs.javascript.Scriptable) Test(org.junit.jupiter.api.Test)

Example 25 with Scriptable

use of net.sourceforge.htmlunit.corejs.javascript.Scriptable in project htmlunit-core-js by HtmlUnit.

the class NativeGlobalTest method doTest.

private static void doTest(final double expected, final String src) throws Exception {
    final ContextAction<Object> action = new ContextAction<Object>() {

        @Override
        public Object run(final Context cx) {
            cx.setLanguageVersion(Context.VERSION_ES6);
            final Scriptable scope = cx.initSafeStandardObjects();
            Object result = cx.evaluateString(scope, src, "test.js", 0, null);
            assertEquals(expected, result);
            return null;
        }
    };
    Utils.runWithAllOptimizationLevels(action);
}
Also used : Context(net.sourceforge.htmlunit.corejs.javascript.Context) ContextAction(net.sourceforge.htmlunit.corejs.javascript.ContextAction) Scriptable(net.sourceforge.htmlunit.corejs.javascript.Scriptable)

Aggregations

Scriptable (net.sourceforge.htmlunit.corejs.javascript.Scriptable)67 Context (net.sourceforge.htmlunit.corejs.javascript.Context)39 ContextAction (net.sourceforge.htmlunit.corejs.javascript.ContextAction)36 ScriptableObject (net.sourceforge.htmlunit.corejs.javascript.ScriptableObject)27 Test (org.junit.jupiter.api.Test)14 HtmlUnitScriptable (com.gargoylesoftware.htmlunit.javascript.HtmlUnitScriptable)12 JsxFunction (com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)12 Function (net.sourceforge.htmlunit.corejs.javascript.Function)10 HtmlPage (com.gargoylesoftware.htmlunit.html.HtmlPage)8 JavaScriptEngine (com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine)8 Window (com.gargoylesoftware.htmlunit.javascript.host.Window)7 ContextFactory (net.sourceforge.htmlunit.corejs.javascript.ContextFactory)7 WebClient (com.gargoylesoftware.htmlunit.WebClient)6 LambdaConstructor (net.sourceforge.htmlunit.corejs.javascript.LambdaConstructor)6 LambdaFunction (net.sourceforge.htmlunit.corejs.javascript.LambdaFunction)6 DomNode (com.gargoylesoftware.htmlunit.html.DomNode)5 Method (java.lang.reflect.Method)5 BaseFunction (net.sourceforge.htmlunit.corejs.javascript.BaseFunction)4 FunctionObject (net.sourceforge.htmlunit.corejs.javascript.FunctionObject)4 NativeObject (net.sourceforge.htmlunit.corejs.javascript.NativeObject)4