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

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

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

Context.compileFunction介绍

[英]Compile a JavaScript function.

The function source must be a function definition as defined by ECMA (e.g., "function f(a) { return a; }").
[中]编译一个JavaScript函数。
函数源必须是ECMA定义的函数定义(例如,“函数f(a){返回a;}”)。

代码示例

代码示例来源: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: stackoverflow.com

String script = "function abc(x,y) {return x+y;}";
Context context = Context.enter();
try {
  ScriptableObject scope = context.initStandardObjects();
  Scriptable that = context.newObject(scope);
  Function fct = context.compileFunction(scope, script, "script", 1, null);
  Object result = fct.call(
      context, scope, that, new Object[] {2, 3});
  System.out.println(Context.jsToJava(result, int.class));
} finally {
  Context.exit();
}

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

final Function fnAggregate = context.compileFunction(scope, aggregate, "aggregate", 1, null);
final Function fnReset = context.compileFunction(scope, reset, "reset", 1, null);
final Function fnCombine = context.compileFunction(scope, combine, "combine", 1, 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, 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: 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: rnewson/couchdb-lucene

public Function compileFunction(final Context context,
                ScriptableObject scope) throws JSONException {
  return context.compileFunction(scope, getFunction(), null, 0, null);
}

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

final Function compileFunction(Scriptable scope, String source,
                Evaluator compiler,
                ErrorReporter compilationErrorReporter,
                String sourceName, int lineno,
                Object securityDomain)
{
  try {
    return (Function) compileImpl(scope, null, source, sourceName,
                   lineno, securityDomain, true,
                   compiler, compilationErrorReporter);
  }
  catch (IOException ioe) {
    // Should never happen because we just made the reader
    // from a String
    throw new RuntimeException();
  }
}

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

final Function compileFunction(Scriptable scope, String source,
                Evaluator compiler,
                ErrorReporter compilationErrorReporter,
                String sourceName, int lineno,
                Object securityDomain)
{
  try {
    return (Function) compileImpl(scope, null, source, sourceName,
                   lineno, securityDomain, true,
                   compiler, compilationErrorReporter);
  }
  catch (IOException ioe) {
    // Should never happen because we just made the reader
    // from a String
    throw new RuntimeException();
  }
}

代码示例来源:origin: io.teknek/nit-compiler

@Override
public Object createInstance(NitDesc nitDesc) throws NitException {
 Context context = Context.enter();
 Scriptable scope = context.initStandardObjects();
 // scope //source //sourcename //linenumber // security domain
 Function function = context.compileFunction(scope, nitDesc.getScript(), "nit-source", 1, null);
 return function;
}

代码示例来源:origin: com.n3twork.druid/druid-processing

public JavaScriptPredicate(final String script)
{
 Preconditions.checkNotNull(script, "script must not be null");
 this.script = script;
 final Context cx = Context.enter();
 try {
  cx.setOptimizationLevel(9);
  scope = cx.initStandardObjects();
  fnApply = cx.compileFunction(scope, script, "script", 1, null);
 } finally {
  Context.exit();
 }
}

代码示例来源:origin: io.druid/druid-processing

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: org.apache.druid/druid-processing

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: org.kohsuke.httpunit/httpunit

/**
   * get the (cached) handler Function for this event Handler
   * on first access compile the function
   * @return
   */
  Function getHandler() {
    if (_handler == null) {
      String attribute = _baseElement.getAttributeWithNoDefault( _handlerName );
      if (attribute != null && Context.getCurrentContext() != null) {
        _handler = Context.getCurrentContext().compileFunction( _baseElement, "function " + AbstractDomComponent.createAnonymousFunctionName() + "() { " + attribute + "}", "httpunit", 0, null );
      }
    }
    return _handler;
  }
}

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

/**
   * get the (cached) handler Function for this event Handler
   * on first access compile the function
   * @return
   */
  Function getHandler() {
    if (_handler == null) {
      String attribute = _baseElement.getAttributeWithNoDefault( _handlerName );
      if (attribute != null && Context.getCurrentContext() != null) {
        _handler = Context.getCurrentContext().compileFunction( _baseElement, "function " + AbstractDomComponent.createAnonymousFunctionName() + "() { " + attribute + "}", "httpunit", 0, null );
      }
    }
    return _handler;
  }
}

代码示例来源:origin: javanettasks/httpunit

/**
   * get the (cached) handler Function for this event Handler
   * on first access compile the function
   * @return
   */
  Function getHandler() {
    if (_handler == null) {
      String attribute = _baseElement.getAttributeWithNoDefault( _handlerName );
      if (attribute != null && Context.getCurrentContext() != null) {
        _handler = Context.getCurrentContext().compileFunction( _baseElement, "function " + AbstractDomComponent.createAnonymousFunctionName() + "() { " + attribute + "}", "httpunit", 0, null );
      }
    }
    return _handler;
  }
}

代码示例来源:origin: com.couchbase.mock/CouchbaseMock

private Reducer(String reduceTxt, Context cx) {
  Scriptable scope = new ImporterTopLevel(cx);
  cx.evaluateString(scope, REDUCE_JS, "reduce.js", 1, null);
  Scriptable builtins = (Scriptable) scope.get("BUILTIN_REDUCERS", scope);
  if (builtins.has(reduceTxt, builtins)) {
    reduceFunc = (Function)builtins.get(reduceTxt, builtins);
  } else {
    reduceFunc = cx.compileFunction(scope, reduceTxt, "reduce", 1, null);
  }
}

代码示例来源:origin: couchbase/CouchbaseMock

private Reducer(String reduceTxt, Context cx) {
  Scriptable scope = new ImporterTopLevel(cx);
  cx.evaluateString(scope, REDUCE_JS, "reduce.js", 1, null);
  Scriptable builtins = (Scriptable) scope.get("BUILTIN_REDUCERS", scope);
  if (builtins.has(reduceTxt, builtins)) {
    reduceFunc = (Function)builtins.get(reduceTxt, builtins);
  } else {
    reduceFunc = cx.compileFunction(scope, reduceTxt, "reduce", 1, null);
  }
}

代码示例来源:origin: com.couchbase.mock/CouchbaseMock

private Indexer(String mapTxt, Context cx) {
  scope = new ImporterTopLevel(cx);
  cx.evaluateString(scope, INDEX_JS, "index.js", 1, null); // Index source
  cx.evaluateString(scope, JavascriptRun.getCollateJS(), "collate.js", 1, null); // Collation
  mapFunction = cx.compileFunction(scope, mapTxt, "map", 1, null);
  // var index = new Index()
  indexResults = cx.newObject(scope, "Index");
  indexFunction = (Function) indexResults.getPrototype().get("indexDoc", indexResults);
  // var emit = index.emit
  Function emitFunc = (Function) indexResults.getPrototype().get("emit", indexResults);
  emitFunc = new BoundFunction(cx, scope, emitFunc, indexResults, NO_ARGS);
  scope.put("emit", scope, emitFunc);
}

代码示例来源:origin: couchbase/CouchbaseMock

private Indexer(String mapTxt, Context cx) {
  scope = new ImporterTopLevel(cx);
  cx.evaluateString(scope, INDEX_JS, "index.js", 1, null); // Index source
  cx.evaluateString(scope, JavascriptRun.getCollateJS(), "collate.js", 1, null); // Collation
  mapFunction = cx.compileFunction(scope, mapTxt, "map", 1, null);
  // var index = new Index()
  indexResults = cx.newObject(scope, "Index");
  indexFunction = (Function) indexResults.getPrototype().get("indexDoc", indexResults);
  // var emit = index.emit
  Function emitFunc = (Function) indexResults.getPrototype().get("emit", indexResults);
  emitFunc = new BoundFunction(cx, scope, emitFunc, indexResults, NO_ARGS);
  scope.put("emit", scope, emitFunc);
}

相关文章

微信公众号

Context类方法