Пример #1
0
  @Override
  public Object call(final Object thiz, final Object... args) {
    final Global oldGlobal = Context.getGlobal();
    final boolean globalChanged = (oldGlobal != global);

    try {
      if (globalChanged) {
        Context.setGlobal(global);
      }

      if (sobj instanceof ScriptFunction) {
        final Object[] modArgs = globalChanged ? wrapArray(args, oldGlobal) : args;
        final Object self = globalChanged ? wrap(thiz, oldGlobal) : thiz;
        return wrap(
            ScriptRuntime.apply(
                (ScriptFunction) sobj, unwrap(self, global), unwrapArray(modArgs, global)),
            global);
      }

      throw new RuntimeException("not a function: " + toString());
    } catch (final NashornException ne) {
      throw ne.initEcmaError(global);
    } catch (final RuntimeException | Error e) {
      throw e;
    } catch (final Throwable t) {
      throw new RuntimeException(t);
    } finally {
      if (globalChanged) {
        Context.setGlobal(oldGlobal);
      }
    }
  }
Пример #2
0
  /**
   * Call member function
   *
   * @param functionName function name
   * @param args arguments
   * @return return value of function
   */
  public Object callMember(final String functionName, final Object... args) {
    functionName.getClass(); // null check
    final Global oldGlobal = Context.getGlobal();
    final boolean globalChanged = (oldGlobal != global);

    try {
      if (globalChanged) {
        Context.setGlobal(global);
      }

      final Object val = sobj.get(functionName);
      if (val instanceof ScriptFunction) {
        final Object[] modArgs = globalChanged ? wrapArray(args, oldGlobal) : args;
        return wrap(
            ScriptRuntime.apply((ScriptFunction) val, sobj, unwrapArray(modArgs, global)), global);
      } else if (val instanceof JSObject && ((JSObject) val).isFunction()) {
        return ((JSObject) val).call(sobj, args);
      }

      throw new NoSuchMethodException("No such function " + functionName);
    } catch (final NashornException ne) {
      throw ne.initEcmaError(global);
    } catch (final RuntimeException | Error e) {
      throw e;
    } catch (final Throwable t) {
      throw new RuntimeException(t);
    } finally {
      if (globalChanged) {
        Context.setGlobal(oldGlobal);
      }
    }
  }