Java Code Examples for org.mozilla.javascript.Context#compileFunction()

The following examples show how to use org.mozilla.javascript.Context#compileFunction() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SdpProcessor.java    From openwebrtc-android-sdk with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private SdpProcessor() {
    Context context = Context.enter();
    context.setOptimizationLevel(-1);
    context.setLanguageVersion(Context.VERSION_1_8);
    mScope = context.initStandardObjects();

    try {
        context.evaluateString(mScope, sSdpJsSource, "sdp.js", 1, null);
        mSdpToJsonFunction = context.compileFunction(mScope, "function sdpToJson(sdp) { return JSON.stringify(SDP.parse(sdp)); }", "sdpToJson", 1, null);
        mJsonToSdpFunction = context.compileFunction(mScope, "function jsonToSdp(sdp) { return SDP.generate(JSON.parse(sdp)); }", "jsonToSdp", 1, null);
    } finally {
        Context.exit();
    }
}
 
Example 2
Source File: LessCompiler.java    From projectforge-webapp with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Initializes this <code>LessCompiler</code>.
 * <p>
 * It is not needed to call this method manually, as it is called implicitly by the compile methods if needed.
 * </p>
 */
public synchronized void init() {
  final long start = System.currentTimeMillis();

  try {
    final Context cx = Context.enter();
    cx.setOptimizationLevel(-1);
    cx.setLanguageVersion(Context.VERSION_1_7);

    final Global global = new Global();
    global.init(cx);

    scope = cx.initStandardObjects(global);

    final List<URL> jsUrls = new ArrayList<URL>(2 + customJs.size());
    jsUrls.add(envJs);
    jsUrls.add(lessJs);
    jsUrls.addAll(customJs);

    for(final URL url : jsUrls){
      final InputStreamReader inputStreamReader = new InputStreamReader(url.openConnection().getInputStream());
      try{
        cx.evaluateReader(scope, inputStreamReader, url.toString(), 1, null);
      }finally{
        inputStreamReader.close();
      }
    }
    doIt = cx.compileFunction(scope, COMPILE_STRING, "doIt.js", 1, null);
  }
  catch (final Exception e) {
    final String message = "Failed to initialize LESS compiler.";
    log.error(message, e);
    throw new IllegalStateException(message, e);
  }finally{
    Context.exit();
  }

  if (log.isDebugEnabled()) {
    log.debug("Finished initialization of LESS compiler in " + (System.currentTimeMillis() - start) + " ms.");
  }
}