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

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

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

ContextFactory.enterContext介绍

[英]Get a context associated with the current thread, creating one if need be. The Context stores the execution state of the JavaScript engine, so it is required that the context be entered before execution may begin. Once a thread has entered a Context, then getCurrentContext() may be called to find the context that is associated with the current thread.

Calling enterContext() will return either the Context currently associated with the thread, or will create a new context and associate it with the current thread. Each call to enterContext() must have a matching call to Context#exit().

Context cx = contextFactory.enterContext(); 
try { 
... 
cx.evaluateString(...); 
} finally { 
Context.exit(); 
}

Instead of using enterContext(), exit() pair consider using #call(ContextAction) which guarantees proper association of Context instances with the current thread. With this method the above example becomes:

ContextFactory.call(new ContextAction() { 
public Object run(Context cx) { 
... 
cx.evaluateString(...); 
return null; 
} 
});

[中]获取与当前线程关联的上下文,必要时创建上下文。上下文存储JavaScript引擎的执行状态,因此需要在开始执行之前输入上下文。一旦线程输入了上下文,就可以调用getCurrentContext()来查找与当前线程关联的上下文。
调用enterContext()将返回当前与线程关联的上下文,或者将创建新上下文并将其与当前线程关联。对enterContext()的每个调用都必须有一个与上下文#exit()匹配的调用。
[2]不使用SaleCutExter(),Ext()对使用“调用”(CONExACTACT)来考虑,这保证了上下文实例与当前线程的适当关联。使用此方法,上述示例变为:

ContextFactory.call(new ContextAction() { 
public Object run(Context cx) { 
... 
cx.evaluateString(...); 
return null; 
} 
});

代码示例

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

@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: apache/incubator-druid

@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

@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: galenframework/galen

public static String getVersion() {
  return ContextFactory.getGlobal().enterContext().getImplementationVersion();
}

代码示例来源: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: spring-projects/spring-framework

@Before
public void setUp() {
  request = new MockHttpServletRequest();
  response = new MockHttpServletResponse();
  jsContext = ContextFactory.getGlobal().enterContext();
  jsScope = jsContext.initStandardObjects();
  view = new MappingJackson2JsonView();
}

代码示例来源:origin: spring-projects/spring-framework

@Before
public void setUp() {
  request = new MockHttpServletRequest();
  response = new MockHttpServletResponse();
  jsContext = ContextFactory.getGlobal().enterContext();
  jsScope = jsContext.initStandardObjects();
  view = new MappingJackson2XmlView();
}

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

Context context = contextFactory.enterContext();
context.setOptimizationLevel(JavaScriptConfig.DEFAULT_OPTIMIZATION_LEVEL);

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

Scriptable scope;
cx = ContextFactory.getGlobal().enterContext();

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

Script jsscript;
jscx = ContextFactory.getGlobal().enterContext();
jsscope = jscx.initStandardObjects( null, false );
try {

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

jscx = ContextFactory.getGlobal().enterContext();
jscx.setOptimizationLevel( -1 );
jsscope = jscx.initStandardObjects( null, false );

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

jscx = ContextFactory.getGlobal().enterContext();
jscx.setOptimizationLevel( -1 );
jsscope = jscx.initStandardObjects( null, false );

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

data.cx = ContextFactory.getGlobal().enterContext();

代码示例来源:origin: rhino/js

/**
 * @deprecated use {@link #enterContext()} instead
 * @return a Context associated with the current thread
 */
public final Context enter()
{
  return enterContext(null);
}

代码示例来源:origin: geogebra/geogebra

/**
 * @deprecated use {@link #enterContext()} instead
 * @return a Context associated with the current thread
 */
@Deprecated
public final Context enter()
{
  return enterContext(null);
}

代码示例来源:origin: org.apache.druid/java-util

@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: io.druid/druid-processing

@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: com.n3twork.druid/druid-processing

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

相关文章