Search in sources :

Example 16 with Context

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

the class FunctionCallerTest method callerArgumentsCallee.

/**
 * Tests the caller arguments
 */
@Test
public void callerArgumentsCallee() throws Exception {
    final String script = "function f() {\n" + "  var caller = arguments.callee.caller;\n" + "  output += (caller == g) + ', ';\n" + "  output += (caller.arguments.callee == g);\n" + "}\n" + "function g() {\n" + "  f(123)\n" + "}\n" + "var output = '';\n" + "g();\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, true", result);
            return null;
        }
    };
    Utils.runWithOptimizationLevel(action, -1);
}
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 17 with Context

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

the class FunctionCallerTest method arguments.

/**
 * Tests the caller arguments
 */
@Test
public void arguments() throws Exception {
    final String script = "function f() {\n" + "  g('hello', 'world');\n" + "}\n" + "function g() {\n" + "  output += g.arguments.length + '-';\n" + "  output += g.arguments[0] + '-';\n" + "  h('i', 'you');\n" + "}\n" + "function h() {\n" + "  output += g.arguments.length;\n" + "}\n" + "var output = '';\n" + "f();\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("2-hello-2", 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 18 with Context

use of net.sourceforge.htmlunit.corejs.javascript.Context 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 19 with Context

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

the class ScriptRuntimeTest method test.

private static void test(final String script, final Object expected) {
    final ContextAction<Object> action = new ContextAction<Object>() {

        @Override
        public Object run(final Context cx) {
            try {
                Scriptable scope = cx.initSafeStandardObjects();
                final Object o = cx.evaluateString(scope, script, "test_script", 1, null);
                assertEquals(expected, o);
                return o;
            } catch (final Exception e) {
                throw new RuntimeException(e);
            }
        }
    };
    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)

Example 20 with Context

use of net.sourceforge.htmlunit.corejs.javascript.Context in project htmlunit by HtmlUnit.

the class WorkerJob method loadAndExecute.

void loadAndExecute(final WebClient webClient, final String url, final Context context, final boolean checkMimeType) throws IOException {
    final HtmlPage page = (HtmlPage) owningWindow_.getDocument().getPage();
    final URL fullUrl = page.getFullyQualifiedUrl(url);
    final WebRequest webRequest = new WebRequest(fullUrl);
    final WebResponse response = webClient.loadWebResponse(webRequest);
    if (checkMimeType && !MimeType.isJavascriptMimeType(response.getContentType())) {
        throw Context.reportRuntimeError("NetworkError: importScripts response is not a javascript response");
    }
    final String scriptCode = response.getContentAsString();
    final JavaScriptEngine javaScriptEngine = (JavaScriptEngine) webClient.getJavaScriptEngine();
    final DedicatedWorkerGlobalScope thisScope = this;
    final ContextAction<Object> action = cx -> {
        final Script script = javaScriptEngine.compile(page, thisScope, scriptCode, fullUrl.toExternalForm(), 1);
        // script might be null here e.g. if there is a syntax error
        if (script != null) {
            return javaScriptEngine.execute(page, thisScope, script);
        }
        return null;
    };
    final ContextFactory cf = javaScriptEngine.getContextFactory();
    if (context != null) {
        action.run(context);
    } else {
        final JavaScriptJob job = new WorkerJob(cf, action, "loadAndExecute " + url);
        owningWindow_.getWebWindow().getJobManager().addJob(job, page);
    }
}
Also used : AbstractJavaScriptConfiguration(com.gargoylesoftware.htmlunit.javascript.configuration.AbstractJavaScriptConfiguration) WindowOrWorkerGlobalScopeMixin(com.gargoylesoftware.htmlunit.javascript.host.WindowOrWorkerGlobalScopeMixin) URL(java.net.URL) Script(net.sourceforge.htmlunit.corejs.javascript.Script) HtmlUnitScriptable(com.gargoylesoftware.htmlunit.javascript.HtmlUnitScriptable) FF(com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.FF) EDGE(com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.EDGE) ClassConfiguration(com.gargoylesoftware.htmlunit.javascript.configuration.ClassConfiguration) BrowserVersion(com.gargoylesoftware.htmlunit.BrowserVersion) ContextAction(net.sourceforge.htmlunit.corejs.javascript.ContextAction) FF_ESR(com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.FF_ESR) Event(com.gargoylesoftware.htmlunit.javascript.host.event.Event) JavaScriptEngine(com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine) MimeType(com.gargoylesoftware.htmlunit.util.MimeType) Context(net.sourceforge.htmlunit.corejs.javascript.Context) WebRequest(com.gargoylesoftware.htmlunit.WebRequest) Scriptable(net.sourceforge.htmlunit.corejs.javascript.Scriptable) JsxSetter(com.gargoylesoftware.htmlunit.javascript.configuration.JsxSetter) Window(com.gargoylesoftware.htmlunit.javascript.host.Window) CHROME(com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.CHROME) IOException(java.io.IOException) JavaScriptJob(com.gargoylesoftware.htmlunit.javascript.background.JavaScriptJob) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) WebResponse(com.gargoylesoftware.htmlunit.WebResponse) JsxGetter(com.gargoylesoftware.htmlunit.javascript.configuration.JsxGetter) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction) JsxClass(com.gargoylesoftware.htmlunit.javascript.configuration.JsxClass) List(java.util.List) ContextFactory(net.sourceforge.htmlunit.corejs.javascript.ContextFactory) IE(com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.IE) WindowOrWorkerGlobalScope(com.gargoylesoftware.htmlunit.javascript.host.WindowOrWorkerGlobalScope) Log(org.apache.commons.logging.Log) WebClient(com.gargoylesoftware.htmlunit.WebClient) LogFactory(org.apache.commons.logging.LogFactory) Undefined(net.sourceforge.htmlunit.corejs.javascript.Undefined) Function(net.sourceforge.htmlunit.corejs.javascript.Function) JS_WORKER_IMPORT_SCRIPTS_ACCEPTS_ALL(com.gargoylesoftware.htmlunit.BrowserVersionFeatures.JS_WORKER_IMPORT_SCRIPTS_ACCEPTS_ALL) MessageEvent(com.gargoylesoftware.htmlunit.javascript.host.event.MessageEvent) EventTarget(com.gargoylesoftware.htmlunit.javascript.host.event.EventTarget) BasicJavaScriptJob(com.gargoylesoftware.htmlunit.javascript.background.BasicJavaScriptJob) Script(net.sourceforge.htmlunit.corejs.javascript.Script) WebResponse(com.gargoylesoftware.htmlunit.WebResponse) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) URL(java.net.URL) ContextFactory(net.sourceforge.htmlunit.corejs.javascript.ContextFactory) WebRequest(com.gargoylesoftware.htmlunit.WebRequest) JavaScriptJob(com.gargoylesoftware.htmlunit.javascript.background.JavaScriptJob) BasicJavaScriptJob(com.gargoylesoftware.htmlunit.javascript.background.BasicJavaScriptJob) JavaScriptEngine(com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine)

Aggregations

Context (net.sourceforge.htmlunit.corejs.javascript.Context)50 Scriptable (net.sourceforge.htmlunit.corejs.javascript.Scriptable)40 ContextAction (net.sourceforge.htmlunit.corejs.javascript.ContextAction)39 ScriptableObject (net.sourceforge.htmlunit.corejs.javascript.ScriptableObject)20 Test (org.junit.jupiter.api.Test)15 ContextFactory (net.sourceforge.htmlunit.corejs.javascript.ContextFactory)11 Function (net.sourceforge.htmlunit.corejs.javascript.Function)6 WebClient (com.gargoylesoftware.htmlunit.WebClient)5 HtmlPage (com.gargoylesoftware.htmlunit.html.HtmlPage)5 JavaScriptEngine (com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine)5 Method (java.lang.reflect.Method)5 ArrayList (java.util.ArrayList)5 JsxFunction (com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)4 Script (net.sourceforge.htmlunit.corejs.javascript.Script)4 JavaScriptJob (com.gargoylesoftware.htmlunit.javascript.background.JavaScriptJob)3 Window (com.gargoylesoftware.htmlunit.javascript.host.Window)3 BrowserVersion (com.gargoylesoftware.htmlunit.BrowserVersion)2 JS_WORKER_IMPORT_SCRIPTS_ACCEPTS_ALL (com.gargoylesoftware.htmlunit.BrowserVersionFeatures.JS_WORKER_IMPORT_SCRIPTS_ACCEPTS_ALL)2 WebRequest (com.gargoylesoftware.htmlunit.WebRequest)2 WebResponse (com.gargoylesoftware.htmlunit.WebResponse)2