Example #1
0
  /**
   * 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 Undefined.instance;
    }
    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);
            }
          });
    }
  }
Example #2
0
 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;
                 }
               });
 }
Example #3
0
 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();
   }
 }