예제 #1
0
  public void testRubyExceptionWithoutCause() throws Exception {
    try {
      RubyRuntimeAdapter evaler = JavaEmbedUtils.newRuntimeAdapter();

      evaler.eval(runtime, "no_method_with_this_name");
      fail("Expected ScriptException");
    } catch (RaiseException re) {
      assertEquals(
          "(NameError) undefined local variable or method `no_method_with_this_name' for main:Object",
          re.getMessage());
    }
  }
  private <T> T call(
      MethodType type,
      Class<T> returnType,
      Object receiver,
      String methodName,
      Block block,
      EmbedEvalUnit unit,
      Object... args) {
    if (methodName == null || methodName.length() == 0) {
      return null;
    }
    Ruby runtime = container.getProvider().getRuntime();
    RubyObject rubyReceiver = getReceiverObject(runtime, receiver);

    boolean sharing_variables = true;
    Object obj = container.getAttribute(AttributeName.SHARING_VARIABLES);
    if (obj != null && obj instanceof Boolean && ((Boolean) obj) == false) {
      sharing_variables = false;
    }
    try {
      if (sharing_variables) {
        ManyVarsDynamicScope scope;
        if (unit != null && unit.getScope() != null) scope = unit.getScope();
        else scope = EmbedRubyRuntimeAdapterImpl.getManyVarsDynamicScope(container, 0);
        container.getVarMap().inject(scope, 0, rubyReceiver);
        runtime.getCurrentContext().pushScope(scope);
      }
      IRubyObject result = callEachType(type, rubyReceiver, methodName, block, args);
      if (sharing_variables) {
        container.getVarMap().retrieve(rubyReceiver);
      }
      if (!(result instanceof RubyNil) && returnType != null) {
        Object ret = JavaEmbedUtils.rubyToJava(runtime, result, returnType);
        return ret != null ? returnType.cast(ret) : null;
      }
      return null;
    } catch (RaiseException e) {
      runtime.printError(e.getException());
      throw new InvokeFailedException(e.getMessage(), e);
    } catch (Throwable e) {
      throw new InvokeFailedException(e);
    } finally {
      if (sharing_variables) {
        runtime.getCurrentContext().popScope();
      }
    }
  }
예제 #3
0
 public void stop() throws Exception {
   try {
     // We call the script with receiver = null - this causes the method to be called on the top
     // level
     // script
     container.callMethod(null, "vertx_stop");
   } catch (InvokeFailedException e) {
     Throwable cause = e.getCause();
     if (cause instanceof RaiseException) {
       // Gosh, this is a bit long winded!
       RaiseException re = (RaiseException) cause;
       String msg = "(NoMethodError) undefined method `vertx_stop'";
       if (re.getMessage().startsWith(msg)) {
         // OK - method is not mandatory
         return;
       }
     }
     throw e;
   }
 }