Package org.jruby.runtime

Examples of org.jruby.runtime.ThreadContext


        return null;
    }

    public void run() {
        RubiniusCMethod method = (RubiniusCMethod)methods.get("__script__");
        ThreadContext context = runtime.getCurrentContext();
        StaticScope scope = new LocalStaticScope(null);

        if (scope.getModule() == null) {
            scope.setModule(runtime.getObject());
        }
       
        scope.setVariables(new String[method.locals]);
       
        context.setFile(method.file);
        context.setLine(-1);
        context.preScopedBody(DynamicScope.newDynamicScope(scope,null));
        RubiniusMachine.INSTANCE.exec(context, runtime.getObject(), method.code, method.literals, new IRubyObject[0]);
    }
View Full Code Here


        this.runtime = runtime;
        this.iseq = iseq;
    }
   
    public IRubyObject run() {
        ThreadContext context = runtime.getCurrentContext();
        StaticScope scope = new LocalStaticScope(null, iseq.locals);
        context.setFile(iseq.filename);
        context.setLine(-1);
        return ym.exec(context, scope, iseq.body);
    }
View Full Code Here

    /** rb_mod_include
     *
     */
    @JRubyMethod(name = "include", required = 1, rest = true, visibility = PRIVATE)
    public RubyModule include(IRubyObject[] modules) {
        ThreadContext context = getRuntime().getCurrentContext();
        // MRI checks all types first:
        for (int i = modules.length; --i >= 0; ) {
            IRubyObject obj = modules[i];
            if (!obj.isModule()) throw context.getRuntime().newTypeError(obj, context.getRuntime().getModule());
        }
        for (int i = modules.length - 1; i >= 0; i--) {
            modules[i].callMethod(context, "append_features", this);
            modules[i].callMethod(context, "included", this);
        }
View Full Code Here

    /**
     * Attempts to remove this variable from top self or receiver.
     *
     */
    public void remove() {
        ThreadContext context = receiver.getRuntime().getCurrentContext();
        try {
            DynamicScope currentScope = context.getCurrentScope();
            ManyVarsDynamicScope scope = (ManyVarsDynamicScope) context.getCurrentScope();
            scope = new ManyVarsDynamicScope(new EvalStaticScope(currentScope.getStaticScope()), currentScope);
        } catch (ArrayIndexOutOfBoundsException e) {
            //no context is left.
            //no operation is needed.
        }
View Full Code Here

    public void setRubyObject(IRubyObject rubyObject) {
        updateRubyObject(rubyObject);
    }

    protected RubyModule getRubyClass(Ruby runtime) {
        ThreadContext context = runtime.getCurrentContext();
        StaticScope scope = context.getCurrentScope().getStaticScope();
        RubyModule rubyClass = scope.getModule();
        return rubyClass;
    }
View Full Code Here

      }
    };
    this.settings.on_header_value = new HTTPDataCallback() {
      public int cb (http_parser.lolevel.HTTPParser p, ByteBuffer buf, int pos, int len) {
        byte[] data = fetchBytes(buf, pos, len);
        ThreadContext context = headers.getRuntime().getCurrentContext();
        IRubyObject key, val;
        int new_field = 0;

        if (_current_header != null) {
          new_field = 1;
          _last_header = _current_header;
          _current_header = null;
        }

        key = (IRubyObject)runtime.newString(_last_header);
        val = headers.op_aref(context, key);

        if (new_field == 1) {
          if (val.isNil()) {
            if (header_value_type == runtime.newSymbol("arrays")) {
              headers.op_aset(context, key, RubyArray.newArrayLight(runtime, runtime.newString("")));
            } else {
              headers.op_aset(context, key, runtime.newString(""));
            }
          } else {
            if (header_value_type == runtime.newSymbol("mixed")) {
              if (val instanceof RubyString) {
                headers.op_aset(context, key, RubyArray.newArrayLight(runtime, val, runtime.newString("")));
              } else {
                ((RubyArray)val).add(runtime.newString(""));
              }
            } else if (header_value_type == runtime.newSymbol("arrays")) {
              ((RubyArray)val).add(runtime.newString(""));
            } else {
              ((RubyString)val).cat(", ".getBytes());
            }
          }
          val = headers.op_aref(context, key);
        }

        if (val instanceof RubyArray) {
          val = ((RubyArray)val).entry(-1);
        }

        ((RubyString)val).cat(data);

        return 0;
      }
    };

    this.settings.on_message_begin = new HTTPCallback() {
      public int cb (http_parser.lolevel.HTTPParser p) {
        headers = new RubyHash(runtime);

        requestUrl = runtime.newString("");
        requestPath = runtime.newString("");
        queryString = runtime.newString("");
        fragment = runtime.newString("");

        upgradeData = runtime.newString("");

        IRubyObject ret = runtime.getNil();

        if (callback_object != null) {
          if (((RubyObject)callback_object).respond_to_p(runtime.newSymbol("on_message_begin")).toJava(Boolean.class) == Boolean.TRUE) {
            ThreadContext context = callback_object.getRuntime().getCurrentContext();
            ret = callback_object.callMethod(context, "on_message_begin");
          }
        } else if (on_message_begin != null) {
          ThreadContext context = on_message_begin.getRuntime().getCurrentContext();
          ret = on_message_begin.callMethod(context, "call");
        }

        if (ret == runtime.newSymbol("stop")) {
          throw new StopException();
        } else {
          return 0;
        }
      }
    };
    this.settings.on_message_complete = new HTTPCallback() {
      public int cb (http_parser.lolevel.HTTPParser p) {
        IRubyObject ret = runtime.getNil();

        if (callback_object != null) {
          if (((RubyObject)callback_object).respond_to_p(runtime.newSymbol("on_message_complete")).toJava(Boolean.class) == Boolean.TRUE) {
            ThreadContext context = callback_object.getRuntime().getCurrentContext();
            ret = callback_object.callMethod(context, "on_message_complete");
          }
        } else if (on_message_complete != null) {
          ThreadContext context = on_message_complete.getRuntime().getCurrentContext();
          ret = on_message_complete.callMethod(context, "call");
        }

        if (ret == runtime.newSymbol("stop")) {
          throw new StopException();
        } else {
          return 0;
        }
      }
    };
    this.settings.on_headers_complete = new HTTPCallback() {
      public int cb (http_parser.lolevel.HTTPParser p) {
        IRubyObject ret = runtime.getNil();

        if (callback_object != null) {
          if (((RubyObject)callback_object).respond_to_p(runtime.newSymbol("on_headers_complete")).toJava(Boolean.class) == Boolean.TRUE) {
            ThreadContext context = callback_object.getRuntime().getCurrentContext();
            ret = callback_object.callMethod(context, "on_headers_complete", headers);
          }
        } else if (on_headers_complete != null) {
          ThreadContext context = on_headers_complete.getRuntime().getCurrentContext();
          ret = on_headers_complete.callMethod(context, "call", headers);
        }

        if (ret == runtime.newSymbol("stop")) {
          throw new StopException();
        } else {
          return 0;
        }
      }
    };
    this.settings.on_body = new HTTPDataCallback() {
      public int cb (http_parser.lolevel.HTTPParser p, ByteBuffer buf, int pos, int len) {
        IRubyObject ret = runtime.getNil();
        byte[] data = fetchBytes(buf, pos, len);

        if (callback_object != null) {
          if (((RubyObject)callback_object).respond_to_p(runtime.newSymbol("on_body")).toJava(Boolean.class) == Boolean.TRUE) {
            ThreadContext context = callback_object.getRuntime().getCurrentContext();
            ret = callback_object.callMethod(context, "on_body", callback_object.getRuntime().newString(new String(data)));
          }
        } else if (on_body != null) {
          ThreadContext context = on_body.getRuntime().getCurrentContext();
          ret = on_body.callMethod(context, "call", on_body.getRuntime().newString(new String(data)));
        }

        if (ret == runtime.newSymbol("stop")) {
          throw new StopException();
View Full Code Here

            throws Throwable {
        Loader loader = new Loader();
        Class c = loader.loadClass(name, javaClass);
        Method method = c.getMethod(methodName, new Class[] { Ruby.class, IRubyObject.class });
        Ruby runtime = self.getRuntime();
        ThreadContext tc = runtime.getCurrentContext();
       
        tc.pushRubyClass(self.getType());

        try {
            return (IRubyObject) method.invoke(null, new Object[] { runtime, self });
        } catch (InvocationTargetException e) {
            throw unrollException(e);
        } finally {
            tc.popRubyClass();
        }
    }
View Full Code Here

        RubyArray list = null;
       
        if (!configuration.isEvalParse() && scriptLines != null) {
            if (scriptLines instanceof RubyHash) {
                RubyString filename = runtime.newString(file);
                ThreadContext context = runtime.getCurrentContext();
                IRubyObject object = ((RubyHash) scriptLines).op_aref(context, filename);
               
                list = (RubyArray) (object instanceof RubyArray ? object : runtime.newArray());
               
                ((RubyHash) scriptLines).op_aset(context, filename, list);
View Full Code Here

    }
   
    public void trace(IRubyObject value) {
        if (traces == null) return;
       
        ThreadContext context = value.getRuntime().getCurrentContext();
       
        if (context.isWithinTrace()) return;
       
        try {
            context.setWithinTrace(true);

            for (int i = 0; i < traces.size(); i++) {
                ((RubyProc)traces.get(i)).call(context, new IRubyObject[] {value});
            }
        } finally {
            context.setWithinTrace(false);
        }
    }
View Full Code Here

     * if none has previously been created or the old ThreadContext has been
     * collected.
     */
    public ThreadContext getCurrentContext() {
        SoftReference sr = null;
        ThreadContext context = null;
       
        while (context == null) {
            // loop until a context is available, to clean up softrefs that might have been collected
            if ((sr = (SoftReference)localContext.get()) == null) {
                sr = adoptCurrentThread();
View Full Code Here

TOP

Related Classes of org.jruby.runtime.ThreadContext

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.