Beispiel #1
0
  @Override
  public IRubyObject interpret(
      Ruby runtime, ThreadContext context, IRubyObject self, Block aBlock) {
    // Serialization killed our dynamic scope.  We can just create an empty one
    // since serialization cannot serialize an eval (which is the only thing
    // which is capable of having a non-empty dynamic scope).
    if (scope == null) {
      scope = DynamicScope.newDynamicScope(staticScope);
    }

    StaticScope theStaticScope = scope.getStaticScope();

    // Each root node has a top-level scope that we need to push
    context.preScopedBody(scope);

    if (theStaticScope.getModule() == null) {
      theStaticScope.setModule(runtime.getObject());
    }

    try {
      return bodyNode.interpret(runtime, context, self, aBlock);
    } finally {
      context.postScopedBody();
    }
  }
Beispiel #2
0
  /**
   * Evaluate the given string.
   *
   * @param context the current thread's context
   * @param self the self to evaluate under
   * @param src The string containing the text to be evaluated
   * @param file The filename to use when reporting errors during the evaluation
   * @param lineNumber that the eval supposedly starts from
   * @return An IRubyObject result from the evaluation
   */
  public static IRubyObject evalSimple(
      ThreadContext context, IRubyObject self, RubyString src, String file, int lineNumber) {
    // this is ensured by the callers
    assert file != null;

    Ruby runtime = src.getRuntime();

    // no binding, just eval in "current" frame (caller's frame)
    RubyString source = src.convertToString();

    DynamicScope evalScope = context.getCurrentScope().getEvalScope(runtime);
    evalScope.getStaticScope().determineModule();

    try {
      Node node = runtime.parseEval(source.getByteList(), file, evalScope, lineNumber);

      if (runtime.getInstanceConfig().getCompileMode() == CompileMode.OFFIR) {
        // SSS FIXME: AST interpreter passed both a runtime (which comes from the source string)
        // and the thread-context rather than fetch one from the other.  Why is that?
        return Interpreter.interpretSimpleEval(runtime, file, lineNumber, "(eval)", node, self);
      } else {
        return INTERPRET_EVAL(
            runtime, context, file, lineNumber, node, "(eval)", self, Block.NULL_BLOCK);
      }
    } catch (JumpException.BreakJump bj) {
      throw runtime.newLocalJumpError(
          RubyLocalJumpError.Reason.BREAK, (IRubyObject) bj.getValue(), "unexpected break");
    } catch (StackOverflowError soe) {
      throw runtime.newSystemStackError("stack level too deep", soe);
    }
  }
Beispiel #3
0
  public RootNode(ISourcePosition position, DynamicScope scope, Node bodyNode) {
    super(position);

    assert bodyNode != null : "bodyNode is not null";

    this.scope = scope;
    this.staticScope = scope.getStaticScope();
    this.bodyNode = bodyNode;
  }
Beispiel #4
0
  /**
   * Evaluate the given string under the specified binding object. If the binding is not a Proc or
   * Binding object (RubyProc or RubyBinding) throw an appropriate type error.
   *
   * @param context the thread context for the current thread
   * @param self the self against which eval was called; used as self in the eval in 1.9 mode
   * @param src The string containing the text to be evaluated
   * @param binding The binding object under which to perform the evaluation
   * @return An IRubyObject result from the evaluation
   */
  public static IRubyObject evalWithBinding(
      ThreadContext context, IRubyObject self, IRubyObject src, Binding binding) {
    Ruby runtime = src.getRuntime();
    DynamicScope evalScope;

    if (runtime.is1_9()) {
      // in 1.9, eval scopes are local to the binding
      evalScope = binding.getEvalScope(runtime);
    } else {
      // in 1.8, eval scopes are local to the parent scope
      evalScope = binding.getDynamicScope().getEvalScope(runtime);
    }

    // FIXME:  This determine module is in a strange location and should somehow be in block
    evalScope.getStaticScope().determineModule();

    Frame lastFrame = context.preEvalWithBinding(binding);
    try {
      // Binding provided for scope, use it
      RubyString source = src.convertToString();
      Node node =
          runtime.parseEval(source.getByteList(), binding.getFile(), evalScope, binding.getLine());
      Block block = binding.getFrame().getBlock();

      if (runtime.getInstanceConfig().getCompileMode() == CompileMode.OFFIR) {
        // SSS FIXME: AST interpreter passed both a runtime (which comes from the source string)
        // and the thread-context rather than fetch one from the other.  Why is that?
        return Interpreter.interpretBindingEval(
            runtime, binding.getFile(), binding.getLine(), binding.getMethod(), node, self, block);
      } else {
        return INTERPRET_EVAL(
            runtime,
            context,
            binding.getFile(),
            binding.getLine(),
            node,
            binding.getMethod(),
            self,
            block);
      }
    } catch (JumpException.BreakJump bj) {
      throw runtime.newLocalJumpError(
          RubyLocalJumpError.Reason.BREAK, (IRubyObject) bj.getValue(), "unexpected break");
    } catch (JumpException.RedoJump rj) {
      throw runtime.newLocalJumpError(
          RubyLocalJumpError.Reason.REDO, (IRubyObject) rj.getValue(), "unexpected redo");
    } catch (StackOverflowError soe) {
      throw runtime.newSystemStackError("stack level too deep", soe);
    } finally {
      context.postEvalWithBinding(binding, lastFrame);
    }
  }
Beispiel #5
0
 @Override
 public Object interpret(
     ThreadContext context,
     StaticScope currScope,
     DynamicScope currDynScope,
     IRubyObject self,
     Object[] temp) {
   int i = 0;
   while (!currDynScope.getStaticScope().isArgumentScope()) {
     currDynScope = currDynScope.getParentScope();
     i++;
   }
   return context.runtime.newFixnum(i);
 }
Beispiel #6
0
 @Override
 public Object retrieve(
     ThreadContext context, IRubyObject self, DynamicScope currDynScope, Object[] temp) {
   return currDynScope.getStaticScope();
 }