All Downloads are FREE. Search and download functionalities are using the official Maven repository.

testsrc.org.mozilla.javascript.tests.Evaluator Maven / Gradle / Ivy

Go to download

A distribution of rhino which releases snapshots from a submodule folder containing forked sources.

The newest version!
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package org.mozilla.javascript.tests;
import org.mozilla.javascript.*;
import java.util.Collections;
import java.util.Map;

public class Evaluator {

  public static Object eval(String source) {
    return eval(source, null);
  }

  public static Object eval(String source, String id, Scriptable object) {
    return eval(source, Collections.singletonMap(id, object));
  }

  public static Object eval(String source, Map bindings) {
    Context cx = ContextFactory.getGlobal().enterContext();
    try {
      Scriptable scope = cx.initStandardObjects();
      if (bindings != null) {
          for (Map.Entry entry : bindings.entrySet()) {
            final Scriptable object = entry.getValue();
            object.setParentScope(scope);
            scope.put(entry.getKey(), scope, object);
          }
      }
      return cx.evaluateString(scope, source, "source", 1, null);
    } finally {
      Context.exit();
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy