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);
}
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);
}
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);
}
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);
}
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);
}
}
Aggregations