org.mozilla.javascript.Context.exit()方法的使用及代码示例

x33g5p2x  于2022-01-18 转载在 JavaScript  
字(9.2k)|赞(0)|评价(0)|浏览(186)

本文整理了Java中org.mozilla.javascript.Context.exit()方法的一些代码示例,展示了Context.exit()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Context.exit()方法的具体详情如下:
包路径:org.mozilla.javascript.Context
类名称:Context
方法名:exit

Context.exit介绍

[英]Exit a block of code requiring a Context. Calling exit() will remove the association between the current thread and a Context if the prior call to ContextFactory#enterContext() on this thread newly associated a Context with this thread. Once the current thread no longer has an associated Context, it cannot be used to execute JavaScript until it is again associated with a Context.
[中]退出需要上下文的代码块。如果此线程上对ContextFactory#enterContext()的上一次调用新将上下文与此线程关联,则调用exit()将删除当前线程与上下文之间的关联。一旦当前线程不再具有关联的上下文,它就不能用于执行JavaScript,直到它再次与上下文关联。

代码示例

代码示例来源:origin: pentaho/pentaho-kettle

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

代码示例来源:origin: facebook/stetho

/**
 * 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();
 }
}

代码示例来源:origin: apache/incubator-druid

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

代码示例来源:origin: apache/incubator-druid

@Override
public double estimateSelectivity(BitmapIndexSelector indexSelector)
{
 final Context cx = Context.enter();
 try {
  return Filters.estimateSelectivity(dimension, indexSelector, makeStringPredicate(cx));
 }
 finally {
  Context.exit();
 }
}

代码示例来源:origin: apache/incubator-druid

@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();
 }
}

代码示例来源:origin: apache/incubator-druid

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

代码示例来源:origin: facebook/stetho

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

代码示例来源:origin: facebook/stetho

/**
  * 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;
 }
}

代码示例来源:origin: pentaho/pentaho-kettle

@AfterClass
public static void tearDown() throws Exception {
 scope = null;
 ctx = null;
 Context.exit();
}

代码示例来源:origin: apache/incubator-druid

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

代码示例来源:origin: apache/incubator-druid

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

代码示例来源:origin: apache/incubator-druid

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

代码示例来源:origin: xtuhcy/gecco

@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();
}

代码示例来源:origin: EngineHub/WorldEdit

Context.exit();

代码示例来源:origin: EngineHub/WorldEdit

@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();
  }
}

代码示例来源:origin: apache/incubator-druid

final Function fnReset = context.compileFunction(scope, reset, "reset", 1, null);
final Function fnCombine = context.compileFunction(scope, combine, "combine", 1, null);
Context.exit();

代码示例来源:origin: EngineHub/WorldEdit

@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();
  }
}

代码示例来源:origin: pentaho/pentaho-kettle

return false;
} finally {
 Context.exit();

代码示例来源:origin: pentaho/pentaho-kettle

} finally {
 if ( jscx != null ) {
  Context.exit();

代码示例来源:origin: pentaho/pentaho-kettle

Context.exit();

相关文章

微信公众号

Context类方法