Esempio n. 1
0
  private IRubyObject uclassUnmarshall() throws IOException {
    RubySymbol className = (RubySymbol) unmarshalObject(false);

    RubyClass type = (RubyClass) runtime.getClassFromPath(className.asJavaString());

    // singleton, raise error
    if (type.isSingleton()) throw runtime.newTypeError("singleton can't be loaded");

    // All "C" marshalled objects descend from core classes, which are all at least RubyObject
    RubyObject result = (RubyObject) unmarshalObject();

    // if result is a module or type doesn't extend result's class...
    if (result.getMetaClass() == runtime.getModule()
        || !type.isKindOfModule(result.getMetaClass())) {
      // if allocators do not match, error
      // Note: MRI is a bit different here, and tests TYPE(type.allocate()) != TYPE(result)
      if (type.getAllocator() != result.getMetaClass().getRealClass().getAllocator()) {
        throw runtime.newArgumentError("dump format error (user class)");
      }
    }

    result.setMetaClass(type);

    return result;
  }
Esempio n. 2
0
  private IRubyObject defaultObjectUnmarshal() throws IOException {
    RubySymbol className = (RubySymbol) unmarshalObject(false);

    RubyClass type = null;
    try {
      type = getClassFromPath(runtime, className.toString());
    } catch (RaiseException e) {
      if (runtime.getModule("NameError").isInstance(e.getException())) {
        throw runtime.newArgumentError("undefined class/module " + className.asJavaString());
      }

      throw e;
    }

    assert type != null : "type shouldn't be null.";

    IRubyObject result = (IRubyObject) type.unmarshal(this);

    return result;
  }
Esempio n. 3
0
  public ErbScriptEngine(ErbScriptEngineFactory factory) {
    super(factory);

    final ClassLoader oldClassLoader = Thread.currentThread().getContextClassLoader();
    try {
      Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
      runtime = Ruby.newInstance();
      runtime.evalScriptlet(
          "require 'java';require 'erb';self.send :include, ERB::Util;class ERB;def get_binding;binding;end;attr_reader :props;def set_props(p);@props = p;"
              + "for name,v in @props;instance_eval \"def #{name}; @props['#{name}'];end\";end;end;end;");

      erbModule = runtime.getClassFromPath("ERB");
      bindingSym = RubySymbol.newSymbol(runtime, "get_binding");
    } finally {
      Thread.currentThread().setContextClassLoader(oldClassLoader);
    }
  }
Esempio n. 4
0
    static ByteOrder getByteOrderOption(ThreadContext context, IRubyObject[] args) {

      ByteOrder order = ByteOrder.nativeOrder();

      if (args.length > 3 && args[3] instanceof RubyHash) {
        RubyHash options = (RubyHash) args[3];
        IRubyObject byte_order =
            options.fastARef(RubySymbol.newSymbol(context.getRuntime(), "byte_order"));
        if (byte_order instanceof RubySymbol || byte_order instanceof RubyString) {
          String orderName = byte_order.asJavaString();
          if ("network".equals(orderName) || "big".equals(orderName)) {
            order = ByteOrder.BIG_ENDIAN;

          } else if ("little".equals(orderName)) {
            order = ByteOrder.LITTLE_ENDIAN;
          }
        }
      }

      return order;
    }
Esempio n. 5
0
  private IRubyObject unmarshalObjectDirectly(int type, MarshalState state, boolean callProc)
      throws IOException {
    IRubyObject rubyObj = null;
    switch (type) {
      case 'I':
        MarshalState childState = new MarshalState(true);
        rubyObj = unmarshalObject(childState);
        if (childState.isIvarWaiting()) {
          defaultVariablesUnmarshal(rubyObj);
        }
        return rubyObj;
      case '0':
        rubyObj = runtime.getNil();
        break;
      case 'T':
        rubyObj = runtime.getTrue();
        break;
      case 'F':
        rubyObj = runtime.getFalse();
        break;
      case '"':
        rubyObj = RubyString.unmarshalFrom(this);
        break;
      case 'i':
        rubyObj = RubyFixnum.unmarshalFrom(this);
        break;
      case 'f':
        rubyObj = RubyFloat.unmarshalFrom(this);
        break;
      case '/':
        rubyObj = RubyRegexp.unmarshalFrom(this);
        break;
      case ':':
        rubyObj = RubySymbol.unmarshalFrom(this);
        break;
      case '[':
        rubyObj = RubyArray.unmarshalFrom(this);
        break;
      case '{':
        rubyObj = RubyHash.unmarshalFrom(this, false);
        break;
      case '}':
        // "hashdef" object, a hash with a default
        rubyObj = RubyHash.unmarshalFrom(this, true);
        break;
      case 'c':
        rubyObj = RubyClass.unmarshalFrom(this);
        break;
      case 'm':
        rubyObj = RubyModule.unmarshalFrom(this);
        break;
      case 'e':
        RubySymbol moduleName = (RubySymbol) unmarshalObject();
        RubyModule tp = null;
        try {
          tp = runtime.getClassFromPath(moduleName.asJavaString());
        } catch (RaiseException e) {
          if (runtime.getModule("NameError").isInstance(e.getException())) {
            throw runtime.newArgumentError("undefined class/module " + moduleName.asJavaString());
          }
          throw e;
        }

        rubyObj = unmarshalObject();

        tp.extend_object(rubyObj);
        tp.callMethod(runtime.getCurrentContext(), "extended", rubyObj);
        break;
      case 'l':
        rubyObj = RubyBignum.unmarshalFrom(this);
        break;
      case 'S':
        rubyObj = RubyStruct.unmarshalFrom(this);
        break;
      case 'o':
        rubyObj = defaultObjectUnmarshal();
        break;
      case 'u':
        rubyObj = userUnmarshal(state);
        break;
      case 'U':
        rubyObj = userNewUnmarshal();
        break;
      case 'C':
        rubyObj = uclassUnmarshall();
        break;
      default:
        throw getRuntime().newArgumentError("dump format error(" + (char) type + ")");
    }

    if (callProc) {
      return doCallProcForObj(rubyObj);
    }

    return rubyObj;
  }