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

The following examples show how to use org.mozilla.javascript.Context#evaluateReader() . 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: RhinoWorkerUtils.java    From pushfish-android with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public static Scriptable parse(File source, String encoding, Action<Context> contextConfig) {
    Context context = Context.enter();
    if (contextConfig != null) {
        contextConfig.execute(context);
    }

    Scriptable scope = context.initStandardObjects();
    try {
        Reader reader = new InputStreamReader(new FileInputStream(source), encoding);
        try {
            context.evaluateReader(scope, reader, source.getName(), 0, null);
        } finally {
            reader.close();
        }
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    } finally {
        Context.exit();
    }

    return scope;
}
 
Example 2
Source File: RhinoWorkerUtils.java    From pushfish-android with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public static Scriptable parse(File source, String encoding, Action<Context> contextConfig) {
    Context context = Context.enter();
    if (contextConfig != null) {
        contextConfig.execute(context);
    }

    Scriptable scope = context.initStandardObjects();
    try {
        Reader reader = new InputStreamReader(new FileInputStream(source), encoding);
        try {
            context.evaluateReader(scope, reader, source.getName(), 0, null);
        } finally {
            reader.close();
        }
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    } finally {
        Context.exit();
    }

    return scope;
}
 
Example 3
Source File: RequireJarTest.java    From rhino-android with Apache License 2.0 6 votes vote down vote up
@Override
public void testSetMainForAlreadyLoadedModule() throws Exception {
    final Context cx = createContext();
    final Scriptable scope = cx.initStandardObjects();
    final Require require = getSandboxedRequire(cx, scope, false);
    require.install(scope);
    cx.evaluateReader(scope, getReader("testSetMainForAlreadyLoadedModule.js"), 
            "testSetMainForAlreadyLoadedModule.js", 1, null);
    try {
        require.requireMain(cx, "assert");
        fail();
    }
    catch(IllegalStateException e) {
        assertEquals(e.getMessage(), "Attempt to set main module after it was loaded");
    }
}
 
Example 4
Source File: RhinoWorkerUtils.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public static Scriptable parse(File source, String encoding, Action<Context> contextConfig) {
    Context context = Context.enter();
    if (contextConfig != null) {
        contextConfig.execute(context);
    }

    Scriptable scope = context.initStandardObjects();
    try {
        Reader reader = new InputStreamReader(new FileInputStream(source), encoding);
        try {
            context.evaluateReader(scope, reader, source.getName(), 0, null);
        } finally {
            reader.close();
        }
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    } finally {
        Context.exit();
    }

    return scope;
}
 
Example 5
Source File: RhinoWorkerUtils.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public static Scriptable parse(File source, String encoding, Action<Context> contextConfig) {
    Context context = Context.enter();
    if (contextConfig != null) {
        contextConfig.execute(context);
    }

    Scriptable scope = context.initStandardObjects();
    try {
        Reader reader = new InputStreamReader(new FileInputStream(source), encoding);
        try {
            context.evaluateReader(scope, reader, source.getName(), 0, null);
        } finally {
            reader.close();
        }
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    } finally {
        Context.exit();
    }

    return scope;
}
 
Example 6
Source File: BaseScript.java    From pentaho-reporting with GNU Lesser General Public License v2.1 6 votes vote down vote up
protected void executeScript( final Map<String, Object> params ) {
  final Context cx = Context.getCurrentContext();
  // env.js has methods that pass the 64k Java limit, so we can't compile
  // to bytecode. Interpreter mode to the rescue!
  cx.setOptimizationLevel( -1 );
  cx.setLanguageVersion( Context.VERSION_1_7 );

  final Object wrappedParams;
  if ( params != null ) {
    wrappedParams = Context.javaToJS( params, scope );
  } else {
    wrappedParams = Context.javaToJS( new HashMap<String, Object>(), scope );
  }

  try {
    ScriptableObject.defineProperty( scope, "params", wrappedParams, 0 );
    cx.evaluateReader( scope, new FileReader( source ), this.source, 1, null );
  } catch ( IOException ex ) {
    logger.error( "Failed to read " + source + ": " + ex.toString() );
  }
}
 
Example 7
Source File: JavaScriptMessageMapperRhino.java    From ditto with Eclipse Public License 2.0 5 votes vote down vote up
static void loadJavascriptLibrary(final Context cx,
        final Scriptable scope,
        final Reader reader,
        final String libraryName) {

    try {
        cx.evaluateReader(scope, reader, libraryName, 1, null);
    } catch (final IOException e) {
        throw new IllegalStateException("Could not load script <" + libraryName + ">", e);
    }
}
 
Example 8
Source File: CaliperObjectBenchmark.java    From rhino-android with Apache License 2.0 5 votes vote down vote up
static void runCode(Context cx, Scriptable scope, String fileName)
    throws IOException
{
    FileReader rdr = new FileReader(fileName);
    try {
        cx.evaluateReader(scope, rdr, "test.js", 1, null);
    } finally {
        rdr.close();
    }
}
 
Example 9
Source File: RequireJarTest.java    From rhino-android with Apache License 2.0 5 votes vote down vote up
@Override
public void testRelativeId() throws Exception {
    final Context cx = createContext();
    final Scriptable scope = cx.initStandardObjects();
    final Require require = getSandboxedRequire(cx, scope, false);
    require.install(scope);
    cx.evaluateReader(scope, getReader("testRelativeId.js"), 
            "testRelativeId.js", 1, null);
}
 
Example 10
Source File: RequireTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public void testRelativeId() throws Exception {
    final Context cx = createContext();
    final Scriptable scope = cx.initStandardObjects();
    final Require require = getSandboxedRequire(cx, scope, false);
    require.install(scope);
    cx.evaluateReader(scope, getReader("testRelativeId.js"),
            "testRelativeId.js", 1, null);
}
 
Example 11
Source File: RequireTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public void testSetMainForAlreadyLoadedModule() throws Exception {
    final Context cx = createContext();
    final Scriptable scope = cx.initStandardObjects();
    final Require require = getSandboxedRequire(cx, scope, false);
    require.install(scope);
    cx.evaluateReader(scope, getReader("testSetMainForAlreadyLoadedModule.js"),
            "testSetMainForAlreadyLoadedModule.js", 1, null);
    try {
        require.requireMain(cx, "assert");
        fail();
    }
    catch(IllegalStateException e) {
        assertEquals(e.getMessage(), "Attempt to set main module after it was loaded");
    }
}
 
Example 12
Source File: RequireJarTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public void testRelativeId() throws Exception {
    final Context cx = createContext();
    final Scriptable scope = cx.initStandardObjects();
    final Require require = getSandboxedRequire(cx, scope, false);
    require.install(scope);
    cx.evaluateReader(scope, getReader("testRelativeId.js"),
            "testRelativeId.js", 1, null);
}
 
Example 13
Source File: RequireJarTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public void testSetMainForAlreadyLoadedModule() throws Exception {
    final Context cx = createContext();
    final Scriptable scope = cx.initStandardObjects();
    final Require require = getSandboxedRequire(cx, scope, false);
    require.install(scope);
    cx.evaluateReader(scope, getReader("testSetMainForAlreadyLoadedModule.js"),
            "testSetMainForAlreadyLoadedModule.js", 1, null);
    try {
        require.requireMain(cx, "assert");
        fail();
    }
    catch(IllegalStateException e) {
        assertEquals(e.getMessage(), "Attempt to set main module after it was loaded");
    }
}
 
Example 14
Source File: BaseScope.java    From pentaho-reporting with GNU Lesser General Public License v2.1 5 votes vote down vote up
public static Object load( final Context cx, final Scriptable thisObj,
                           final Object[] args, final Function funObj ) {

  final String file = args[ 0 ].toString();
  try {
    final BaseScope scope = (BaseScope) thisObj;
    cx.evaluateReader( scope, new FileReader( scope.basePath + "/" + file ), file, 1, null );
  } catch ( Exception e ) {
    logger.error( e );
    return Context.toBoolean( false );
  }
  return Context.toBoolean( true );
}
 
Example 15
Source File: BaseScope.java    From pentaho-reporting with GNU Lesser General Public License v2.1 5 votes vote down vote up
public static Object lib( final Context cx, final Scriptable thisObj,
                          final Object[] args, final Function funObj ) {

  final String file = args[ 0 ].toString();
  try {
    final BaseScope scope = (BaseScope) thisObj;
    cx.evaluateReader( scope, new FileReader( scope.systemPath + "/" + file ), file, 1, null );
  } catch ( Exception e ) {
    logger.error( e );
    return Context.toBoolean( false );
  }
  return Context.toBoolean( true );
}
 
Example 16
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.");
  }
}