public void dispose( StepMetaInterface smi, StepDataInterface sdi ) { try { if ( data.cx != null ) { Context.exit(); } } catch ( Exception er ) { // Eat this error, it's typically : "Calling Context.exit without previous Context.enter" // logError(BaseMessages.getString(PKG, "System.Log.UnexpectedError"), er); } super.dispose( smi, sdi ); }
/** * Initializes a proper javascript scope (runtime environment holding variables). * @return a javascript scope */ private @NonNull ScriptableObject initJsScope() { final Context jsContext = JsRuntimeRepl.enterJsContext(); try { ScriptableObject scope = initJsScope(jsContext); return scope; } finally { Context.exit(); } }
public boolean applyObject(final Object input) { // one and only one context per thread final Context cx = Context.enter(); try { return applyInContext(cx, input); } finally { Context.exit(); } }
@Override public double estimateSelectivity(BitmapIndexSelector indexSelector) { final Context cx = Context.enter(); try { return Filters.estimateSelectivity(dimension, indexSelector, makeStringPredicate(cx)); } finally { Context.exit(); } }
@Override public <T> T getBitmapResult(BitmapIndexSelector selector, BitmapResultFactory<T> bitmapResultFactory) { final Context cx = Context.enter(); try { return Filters.matchPredicate(dimension, selector, bitmapResultFactory, makeStringPredicate(cx)); } finally { Context.exit(); } }
public JavaScriptPredicateFactory(final String script, final ExtractionFn extractionFn) { Preconditions.checkNotNull(script, "script must not be null"); this.script = script; this.extractionFn = extractionFn; final Context cx = Context.enter(); try { cx.setOptimizationLevel(9); scope = cx.initStandardObjects(); fnApply = cx.compileFunction(scope, script, "script", 1, null); } finally { Context.exit(); } }
@Override public @Nullable Object evaluate(@NonNull String expression) throws Throwable { Object result; final Context jsContext = enterJsContext(); try { result = jsContext.evaluateString(mJsScope, expression, "chrome", 1, null); // Google chrome automatically saves the last expression to `$_`, we do the same Object jsValue = Context.javaToJS(result, mJsScope); ScriptableObject.putProperty(mJsScope, "$_", jsValue); } finally { Context.exit(); } return Context.jsToJava(result, Object.class); }
/** * Setups a proper javascript context so that it can run javascript code properly under android. * For android we need to disable bytecode generation since the android vms don't understand JVM bytecode. * @return a proper javascript context */ static @NonNull Context enterJsContext() { final Context jsContext = Context.enter(); // If we cause the context to throw a runtime exception from this point // we need to make sure that exit the context. try { jsContext.setLanguageVersion(Context.VERSION_1_8); // We can't let Rhino to optimize the JS and to use a JIT because it would generate JVM bytecode // and android runs on DEX bytecode. Instead we need to go in interpreted mode. jsContext.setOptimizationLevel(-1); } catch (RuntimeException e) { // Something bad happened to the javascript context but it might still be usable. // The first thing to do is to exit the context and then propagate the error. Context.exit(); throw e; } return jsContext; } }
@AfterClass public static void tearDown() throws Exception { scope = null; ctx = null; Context.exit(); }
private static Function compile(String function) { final ContextFactory contextFactory = ContextFactory.getGlobal(); final Context context = contextFactory.enterContext(); context.setOptimizationLevel(JavaScriptConfig.DEFAULT_OPTIMIZATION_LEVEL); final ScriptableObject scope = context.initStandardObjects(); final org.mozilla.javascript.Function fn = context.compileFunction(scope, function, "fn", 1, null); Context.exit(); return new Function() { @Override public double apply(Object[] args) { // ideally we need a close() function to discard the context once it is not used anymore Context cx = Context.getCurrentContext(); if (cx == null) { cx = contextFactory.enterContext(); } return Context.toNumber(fn.call(cx, scope, scope, args)); } }; }
private static Function<Object, String> compile(String function) { final ContextFactory contextFactory = ContextFactory.getGlobal(); final Context context = contextFactory.enterContext(); context.setOptimizationLevel(JavaScriptConfig.DEFAULT_OPTIMIZATION_LEVEL); final ScriptableObject scope = context.initStandardObjects(); final org.mozilla.javascript.Function fn = context.compileFunction(scope, function, "fn", 1, null); Context.exit(); return new Function<Object, String>() { @Override public String apply(Object input) { // ideally we need a close() function to discard the context once it is not used anymore Context cx = Context.getCurrentContext(); if (cx == null) { cx = contextFactory.enterContext(); } final Object res = fn.call(cx, scope, scope, new Object[]{input}); return res != null ? Context.toString(res) : null; } }; }
private static Function<Object, Object> compile(String function) { final ContextFactory contextFactory = ContextFactory.getGlobal(); final Context context = contextFactory.enterContext(); context.setOptimizationLevel(9); final ScriptableObject scope = context.initStandardObjects(); final org.mozilla.javascript.Function fn = context.compileFunction(scope, function, "fn", 1, null); Context.exit(); return new Function<Object, Object>() { @Override public Object apply(Object input) { // ideally we need a close() function to discard the context once it is not used anymore Context cx = Context.getCurrentContext(); if (cx == null) { cx = contextFactory.enterContext(); } final Object res = fn.call(cx, scope, scope, new Object[]{input}); return res != null ? Context.toObject(res, scope) : null; } }; }
@Override @SuppressWarnings({ "unchecked" }) public void render(HttpRequest request, HttpResponse response, BeanMap beanMap, SpiderBean bean) { Context cx = Context.enter(); ScriptableObject scope = cx.initSafeStandardObjects(); String windowScript = "var window = {};var document = {};"; cx.evaluateString(scope, windowScript, "window", 1, null); HtmlParser parser = new HtmlParser(request.getUrl(), response.getContent()); for (Element ele : parser.$("script")) { String sc = ele.html(); if (StringUtils.isNotEmpty(sc)) { try { cx.evaluateString(scope, sc, "", 1, null); } catch (Exception ex) { // ex.printStackTrace(); } } } Map<String, Object> fieldMap = new HashMap<String, Object>(); Set<Field> jsVarFields = ReflectionUtils.getAllFields(bean.getClass(), ReflectionUtils.withAnnotation(JSVar.class)); for (Field jsVarField : jsVarFields) { Object value = injectJsVarField(request, beanMap, jsVarField, cx, scope); if(value != null) { fieldMap.put(jsVarField.getName(), value); } } beanMap.putAll(fieldMap); Context.exit(); }
Context.exit();
@Override public Object eval(String script, ScriptContext context) throws ScriptException { Scriptable scope = setupScope(cx, context); String filename = (filename = (String) get(ScriptEngine.FILENAME)) == null ? "<unknown>" : filename; try { return cx.evaluateString(scope, script, filename, 1, null); } catch (RhinoException e) { String msg; int line = (line = e.lineNumber()) == 0 ? -1 : line; if (e instanceof JavaScriptException) { msg = String.valueOf(((JavaScriptException) e).getValue()); } else { msg = e.getMessage(); } ScriptException scriptException = new ScriptException(msg, e.sourceName(), line); scriptException.initCause(e); throw scriptException; } finally { Context.exit(); } }
final Function fnReset = context.compileFunction(scope, reset, "reset", 1, null); final Function fnCombine = context.compileFunction(scope, combine, "combine", 1, null); Context.exit();
@Override public Object eval(Reader reader, ScriptContext context) throws ScriptException { Scriptable scope = setupScope(cx, context); String filename = (filename = (String) get(ScriptEngine.FILENAME)) == null ? "<unknown>" : filename; try { return cx.evaluateReader(scope, reader, filename, 1, null); } catch (RhinoException e) { String msg; int line = (line = e.lineNumber()) == 0 ? -1 : line; if (e instanceof JavaScriptException) { msg = String.valueOf(((JavaScriptException) e).getValue()); } else { msg = e.getMessage(); } ScriptException scriptException = new ScriptException(msg, e.sourceName(), line); scriptException.initCause(e); throw scriptException; } catch (IOException e) { throw new ScriptException(e); } finally { Context.exit(); } }
return false; } finally { Context.exit();
} finally { if ( jscx != null ) { Context.exit();
Context.exit();