Exemple #1
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);
      }
    }
  }
Exemple #2
0
  @Override
  public boolean equals(final Object other) {
    if (other instanceof ScriptObjectMirror) {
      return sobj.equals(((ScriptObjectMirror) other).sobj);
    }

    return false;
  }
Exemple #3
0
 @Override
 public int hashCode() {
   return sobj.hashCode();
 }
Exemple #4
0
 @Override
 public boolean isArray() {
   return sobj.isArray();
 }
Exemple #5
0
 @Override
 public String getClassName() {
   return sobj.getClassName();
 }