コード例 #1
0
ファイル: JavaAdapter.java プロジェクト: geogebra/geogebra
  /**
   * Utility method which dynamically binds a Context to the current thread, if none already exists.
   */
  public static Object callMethod(
      ContextFactory factory,
      final Scriptable thisObj,
      final Function f,
      final Object[] args,
      final long argsToWrap) {
    if (f == null) {
      // See comments in getFunction
      return null;
    }
    if (factory == null) {
      factory = ContextFactory.getGlobal();
    }

    final Scriptable scope = f.getParentScope();
    if (argsToWrap == 0) {
      return Context.call(factory, f, scope, thisObj, args);
    }

    Context cx = Context.getCurrentContext();
    if (cx != null) {
      return doCall(cx, scope, thisObj, f, args, argsToWrap);
    } else {
      return factory.call(
          new ContextAction() {
            public Object run(Context cx) {
              return doCall(cx, scope, thisObj, f, args, argsToWrap);
            }
          });
    }
  }
コード例 #2
0
ファイル: JavaAdapter.java プロジェクト: geogebra/geogebra
 public static Scriptable runScript(final Script script) {
   return (Scriptable)
       ContextFactory.getGlobal()
           .call(
               new ContextAction() {
                 public Object run(Context cx) {
                   ScriptableObject global = ScriptRuntime.getGlobal(cx);
                   script.exec(cx, global);
                   return global;
                 }
               });
 }
コード例 #3
0
ファイル: Evaluator.java プロジェクト: CyberIntelMafia/neox
 public static Object eval(String source, Map<String, Scriptable> bindings) {
   Context cx = ContextFactory.getGlobal().enterContext();
   try {
     Scriptable scope = cx.initStandardObjects();
     if (bindings != null) {
       for (String id : bindings.keySet()) {
         Scriptable object = bindings.get(id);
         object.setParentScope(scope);
         scope.put(id, scope, object);
       }
     }
     return cx.evaluateString(scope, source, "source", 1, null);
   } finally {
     Context.exit();
   }
 }
コード例 #4
0
ファイル: ScriptParser.java プロジェクト: dojo/dwb
  protected void parse() throws EvaluatorException {
    // Parse script source
    CompilerEnvirons ce = new CompilerEnvirons();
    ScriptParserErrorReporter errorReporter = new ScriptParserErrorReporter();

    ce.setGenerateDebugInfo(true);
    ce.initFromContext(ContextFactory.getGlobal().enterContext());
    ce.setErrorReporter(errorReporter);

    Parser p = new Parser(ce, errorReporter);
    ScriptOrFnNode ast = p.parse(this.scriptSource, "script", 0);

    recursiveParse(ast);

    this.scriptParsed = true;
  }
コード例 #5
0
ファイル: JavaMembers.java プロジェクト: hulstores/geogebra
 JavaMembers(Scriptable scope, Class<?> cl, boolean includeProtected) {
   try {
     Context cx = ContextFactory.getGlobal().enterContext();
     ClassShutter shutter = cx.getClassShutter();
     if (shutter != null && !shutter.visibleToScripts(cl.getName())) {
       throw Context.reportRuntimeError1("msg.access.prohibited", cl.getName());
     }
     this.includePrivate = cx.hasFeature(Context.FEATURE_ENHANCED_JAVA_ACCESS);
     this.members = new HashMap<String, Object>();
     this.staticMembers = new HashMap<String, Object>();
     this.cl = cl;
     reflect(scope, includeProtected);
   } finally {
     Context.exit();
   }
 }
コード例 #6
0
ファイル: JsPipeline.java プロジェクト: squaremo/swimrabbit
 public void input(Datum in) {
   Context c = ContextFactory.getGlobal().enterContext();
   Object maybeFunc = _topLevel.get("handle", _topLevel);
   if (Scriptable.NOT_FOUND == maybeFunc || !(maybeFunc instanceof Function)) {
     // TODO better exception
     throw new RuntimeException("No handle function");
   }
   Function func = (Function) maybeFunc;
   Object[] args = new Object[2];
   args[0] = in;
   args[1] = _ops;
   Object result = func.call(c, _topLevel, _topLevel, args);
   // There's a couple of ways this interface could work: commit as a
   // side-effect, and commit as a return value.  We use side-effect
   // for now, so the return value is ignored.
 }
コード例 #7
0
ファイル: JsPipeline.java プロジェクト: squaremo/swimrabbit
 public Scriptable initialState(Map<String, Object> zero) {
   Context context = ContextFactory.getGlobal().enterContext();
   // Always interpret, since otherwise we have difficulty serialising.
   context.setOptimizationLevel(-1);
   // this arrangement means _global isn't mutable, and
   // variables go in _topLevel.
   ScriptableObject global = context.initStandardObjects(null, true);
   global.sealObject();
   _global = global;
   _topLevel = context.newObject(_global);
   _topLevel.setPrototype(_global);
   _topLevel.setParentScope(null);
   // TODO complain if it's not there
   String script = zero.get("script").toString();
   context.evaluateString(_topLevel, script, "<script>", 1, null);
   context.exit();
   return _topLevel;
 }
コード例 #8
0
  private static Function<Object, String> 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, String>() {
      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;
      }
    };
  }
コード例 #9
0
  protected String compileUnits(JRCompilationUnit[] units, String classpath, File tempDirFile)
      throws JRException {
    Context context = ContextFactory.getGlobal().enterContext();
    try {
      StringBuffer errors = new StringBuffer();
      int errorCount = 0;
      for (int i = 0; i < units.length; i++) {
        JRCompilationUnit unit = units[i];
        JavaScriptCompileData compileData = new JavaScriptCompileData();
        for (Iterator it = unit.getExpressions().iterator(); it.hasNext(); ) {
          JRExpression expr = (JRExpression) it.next();
          int id = unit.getCompileTask().getExpressionId(expr).intValue();
          JavaScriptCompileData.Expression jsExpr = JavaScriptEvaluator.createJSExpression(expr);

          // compile the default expression to catch syntax errors
          try {
            context.compileString(jsExpr.getDefaultExpression(), "expression", 0, null);
          } catch (EvaluatorException e) {
            ++errorCount;
            appendError(errors, errorCount, e);
          }

          compileData.addExpression(id, jsExpr);
        }
        unit.setCompileData(compileData);
      }

      String errorsMessage = null;
      if (errorCount > 0) {
        errorsMessage = errorCount + " error(s):\n" + errors;
      }
      return errorsMessage;
    } finally {
      Context.exit();
    }
  }
コード例 #10
0
 public void enterContext() {
   if (Context.getCurrentContext() != null) {
     Context.exit();
   }
   ContextFactory.getGlobal().enterContext(mcJavascriptContext);
 }