Exemplo n.º 1
0
  @Override
  public Object newObject(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;
        return wrap(
            ScriptRuntime.construct((ScriptFunction) sobj, unwrapArray(modArgs, global)), global);
      }

      throw new RuntimeException("not a constructor: " + 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);
      }
    }
  }